order.model.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. <?php
  2. /**
  3. * 订单管理
  4. *
  5. *
  6. *
  7. *
  8. */
  9. defined('InShopNC') or exit('Access Invalid!');
  10. class orderModel extends Model
  11. {
  12. /**
  13. * 取单条订单信息
  14. *
  15. * @param unknown_type $condition
  16. * @param array $extend 追加返回那些表的信息,如array('order_common','order_goods','store')
  17. * @return unknown
  18. */
  19. public function getOrderInfo($condition = array(), $extend = array(), $fields = '*', $order = '',$group = '')
  20. {
  21. $order_info = $this->table('order')->field($fields)->where($condition)->group($group)->order($order)->find();
  22. if (empty($order_info)) {
  23. return array();
  24. }
  25. if (isset($order_info['order_state'])) {
  26. $order_info['state_desc'] = orderState($order_info);
  27. }
  28. if (isset($order_info['payment_code'])) {
  29. $order_info['payment_name'] = orderPaymentName($order_info['payment_code']);
  30. }
  31. //追加返回订单扩展表信息
  32. if (in_array('order_common',$extend)) {
  33. $order_info['extend_order_common'] = $this->getOrderCommonInfo(array('order_id'=>$order_info['order_id']));
  34. $order_info['extend_order_common']['reciver_info'] = unserialize($order_info['extend_order_common']['reciver_info']);
  35. $order_info['extend_order_common']['invoice_info'] = unserialize($order_info['extend_order_common']['invoice_info']);
  36. }
  37. //追加返回店铺信息
  38. if (in_array('store',$extend)) {
  39. $order_info['extend_store'] = Model('store')->getStoreInfo(array('store_id'=>$order_info['store_id']));
  40. }
  41. //返回买家信息
  42. if (in_array('member',$extend)) {
  43. $order_info['extend_member'] = Model('member')->getMemberInfoByID($order_info['buyer_id']);
  44. }
  45. //追加返回商品信息
  46. if (in_array('order_goods',$extend)) {
  47. //取商品列表
  48. $order_goods_list = $this->getOrderGoodsList(array('order_id'=>$order_info['order_id']));
  49. $order_info['extend_order_goods'] = $order_goods_list;
  50. }
  51. return $order_info;
  52. }
  53. public function getOrderCommonInfo($condition = array(), $field = '*') {
  54. return $this->table('order_common')->where($condition)->find();
  55. }
  56. public function getOrderPayInfo($condition = array(), $master = false) {
  57. return $this->table('order_pay')->where($condition)->master($master)->find();
  58. }
  59. /**
  60. * 取得支付单列表
  61. *
  62. * @param unknown_type $condition
  63. * @param unknown_type $pagesize
  64. * @param unknown_type $filed
  65. * @param unknown_type $order
  66. * @param string $key 以哪个字段作为下标,这里一般指pay_id
  67. * @return unknown
  68. */
  69. public function getOrderPayList($condition, $pagesize = '', $filed = '*', $order = '', $key = '') {
  70. return $this->table('order_pay')->field($filed)->where($condition)->order($order)->page($pagesize)->key($key)->select();
  71. }
  72. /**
  73. * 取得订单列表(未被删除)
  74. * @param unknown $condition
  75. * @param string $pagesize
  76. * @param string $field
  77. * @param string $order
  78. * @param string $limit
  79. * @param unknown $extend 追加返回那些表的信息,如array('order_common','order_goods','store')
  80. * @return Ambigous <multitype:boolean Ambigous <string, mixed> , unknown>
  81. */
  82. public function getNormalOrderList($condition, $pagesize = '*', $field = '*', $order = 'order_id desc', $limit = '', $extend = array(),$master = false){
  83. $condition['delete_state'] = 0;
  84. return $this->getOrderList($condition, $pagesize, $field, $order, $limit, $extend,$master);
  85. }
  86. public function getNormalOrderListEx($condition, $master = false){
  87. $condition['delete_state'] = 0;
  88. return $this->getOrderList($condition, '', '*', 'order_id desc', '', [],$master);
  89. }
  90. /**
  91. * 取得订单列表(所有)
  92. * @param unknown $condition
  93. * @param string $pagesize
  94. * @param string $field
  95. * @param string $order
  96. * @param string $limit
  97. * @param unknown $extend 追加返回那些表的信息,如array('order_common','order_goods','store')
  98. * @return Ambigous <multitype:boolean Ambigous <string, mixed> , unknown>
  99. */
  100. public function getOrderList($condition, $pagesize = '', $field = '*', $order = 'order_id desc', $limit = '', $extend = array(), $master = false)
  101. {
  102. $list = $this->table('order')->field($field)->where($condition)->page($pagesize)->order($order)->limit($limit)->master($master)->select();
  103. if (empty($list)) return array();
  104. $order_list = array();
  105. foreach ($list as $order)
  106. {
  107. if (isset($order['order_state'])) {
  108. $order['state_desc'] = orderState($order);
  109. }
  110. $bonus_amount = unserialize($order['bonus_amount']);
  111. if ($bonus_amount == false) {
  112. $order['bonus_amount'] = [];
  113. } else {
  114. $order['bonus_amount'] = $bonus_amount;
  115. }
  116. if (isset($order['payment_code'])) {
  117. $order['payment_name'] = orderPaymentName($order['payment_code']);
  118. }
  119. if (!empty($extend)) $order_list[$order['order_id']] = $order;
  120. }
  121. if (empty($order_list)) $order_list = $list;
  122. //追加返回订单扩展表信息
  123. if (in_array('order_common',$extend)) {
  124. $order_common_list = $this->getOrderCommonList(array('order_id'=>array('in',array_keys($order_list))));
  125. foreach ($order_common_list as $value) {
  126. $order_list[$value['order_id']]['extend_order_common'] = $value;
  127. $order_list[$value['order_id']]['extend_order_common']['reciver_info'] = @unserialize($value['reciver_info']);
  128. $order_list[$value['order_id']]['extend_order_common']['invoice_info'] = @unserialize($value['invoice_info']);
  129. }
  130. }
  131. //追加返回店铺信息
  132. if (in_array('store',$extend)) {
  133. $store_id_array = array();
  134. foreach ($order_list as $value) {
  135. if (!in_array($value['store_id'],$store_id_array)) $store_id_array[] = $value['store_id'];
  136. }
  137. $store_list = Model('store')->getStoreList(array('store_id'=>array('in',$store_id_array)));
  138. $store_new_list = array();
  139. foreach ($store_list as $store) {
  140. $store_new_list[$store['store_id']] = $store;
  141. }
  142. foreach ($order_list as $order_id => $order) {
  143. $order_list[$order_id]['extend_store'] = $store_new_list[$order['store_id']];
  144. }
  145. }
  146. //追加返回买家信息
  147. if (in_array('member',$extend)) {
  148. foreach ($order_list as $order_id => $order) {
  149. $order_list[$order_id]['extend_member'] = Model('member')->getMemberInfoByID($order['buyer_id']);
  150. }
  151. }
  152. //追加返回商品信息
  153. if (in_array('order_goods',$extend)) {
  154. //取商品列表
  155. $order_goods_list = $this->getOrderGoodsList(array('order_id'=>array('in',array_keys($order_list))));
  156. if (!empty($order_goods_list)) {
  157. foreach ($order_goods_list as $value) {
  158. $order_list[$value['order_id']]['extend_order_goods'][] = $value;
  159. }
  160. } else {
  161. $order_list[$value['order_id']]['extend_order_goods'] = array();
  162. }
  163. }
  164. if (in_array('order_address',$extend)) {
  165. $order_common_list = $this->getOrderCommonList(array('order_id'=>array('in',array_keys($order_list))));
  166. foreach ($order_common_list as $value) {
  167. $order_list[$value['order_id']]['reciver_info'] = @unserialize($value['reciver_info']);
  168. $order_list[$value['order_id']]['reciver_name'] = $value['reciver_name'];
  169. }
  170. }
  171. return $order_list;
  172. }
  173. /**
  174. * 取得(买/卖家)订单某个数量缓存
  175. * @param string $type 买/卖家标志,允许传入 buyer、store
  176. * @param int $id 买家ID、店铺ID
  177. * @param string $key 允许传入 NewCount、PayCount、SendCount、EvalCount,分别取相应数量缓存,只许传入一个
  178. * @return array
  179. */
  180. public function getOrderCountCache($type, $id, $key)
  181. {
  182. if (!C('cache_open')) return array();
  183. $type = 'ordercount'.$type;
  184. $ins = Cache::getInstance('cacheredis');
  185. $order_info = $ins->hget($id,$type,$key);
  186. return !is_array($order_info) ? array($key => $order_info) : $order_info;
  187. }
  188. /**
  189. * 设置(买/卖家)订单某个数量缓存
  190. * @param string $type 买/卖家标志,允许传入 buyer、store
  191. * @param int $id 买家ID、店铺ID
  192. * @param array $data
  193. */
  194. public function editOrderCountCache($type, $id, $data)
  195. {
  196. if (!C('cache_open') || empty($type) || !intval($id) || !is_array($data)) return ;
  197. $ins = Cache::getInstance('cacheredis');
  198. $type = 'ordercount'.$type;
  199. $ins->hset($id,$type,$data);
  200. }
  201. /**
  202. * 取得买卖家订单数量某个缓存
  203. * @param string $type $type 买/卖家标志,允许传入 buyer、store
  204. * @param int $id 买家ID、店铺ID
  205. * @param string $key 允许传入 NewCount、PayCount、SendCount、EvalCount,分别取相应数量缓存,只许传入一个
  206. * @return int
  207. */
  208. public function getOrderCountByID($type, $id, $key)
  209. {
  210. $cache_info = $this->getOrderCountCache($type, $id, $key);
  211. if (is_string($cache_info[$key])) {
  212. //从缓存中取得
  213. $count = $cache_info[$key];
  214. } else {
  215. //从数据库中取得
  216. $field = $type == 'buyer' ? 'buyer_id' : 'store_id';
  217. $condition = array($field => $id);
  218. $func = 'getOrderState'.$key;
  219. $count = $this->$func($condition);
  220. $this->editOrderCountCache($type,$id,array($key => $count));
  221. }
  222. return $count;
  223. }
  224. /**
  225. * 删除(买/卖家)订单全部数量缓存
  226. * @param string $type 买/卖家标志,允许传入 buyer、store
  227. * @param int $id 买家ID、店铺ID
  228. * @return bool
  229. */
  230. public function delOrderCountCache($type, $id) {
  231. if (!C('cache_open')) return true;
  232. $ins = Cache::getInstance('cacheredis');
  233. $type = 'ordercount'.$type;
  234. return $ins->hdel($id,$type);
  235. }
  236. /**
  237. * 待付款订单数量
  238. * @param unknown $condition
  239. */
  240. public function getOrderStateNewCount($condition = array()) {
  241. $condition['order_state'] = ORDER_STATE_NEW;
  242. return $this->getOrderCount($condition);
  243. }
  244. /**
  245. * 待发货订单数量
  246. * @param unknown $condition
  247. */
  248. public function getOrderStatePayCount($condition = array()) {
  249. $condition['order_state'] = ORDER_STATE_PAY;
  250. return $this->getOrderCount($condition);
  251. }
  252. /**
  253. * 待收货订单数量
  254. * @param unknown $condition
  255. */
  256. public function getOrderStateSendCount($condition = array()) {
  257. $condition['order_state'] = ORDER_STATE_SEND;
  258. return $this->getOrderCount($condition);
  259. }
  260. /**
  261. * 待评价订单数量
  262. * @param unknown $condition
  263. */
  264. public function getOrderStateEvalCount($condition = array()) {
  265. $condition['order_state'] = ORDER_STATE_SUCCESS;
  266. $condition['evaluation_state'] = 0;
  267. return $this->getOrderCount($condition);
  268. }
  269. /**
  270. * 交易中的订单数量
  271. * @param unknown $condition
  272. */
  273. public function getOrderStateTradeCount($condition = array()) {
  274. $condition['order_state'] = array(array('neq',ORDER_STATE_CANCEL),array('neq',ORDER_STATE_SUCCESS),'and');
  275. return $this->getOrderCount($condition);
  276. }
  277. /**
  278. * 取得订单数量
  279. * @param unknown $condition
  280. */
  281. public function getOrderCount($condition) {
  282. return $this->table('order')->where($condition)->count();
  283. }
  284. /**
  285. * 取得订单商品表详细信息
  286. * @param unknown $condition
  287. * @param string $fields
  288. * @param string $order
  289. */
  290. public function getOrderGoodsInfo($condition = array(), $fields = '*', $order = '') {
  291. return $this->table('order_goods')->where($condition)->field($fields)->order($order)->find();
  292. }
  293. /**
  294. * 取得订单商品表列表
  295. * @param unknown $condition
  296. * @param string $fields
  297. * @param string $limit
  298. * @param string $page
  299. * @param string $order
  300. * @param string $group
  301. * @param string $key
  302. */
  303. public function getOrderGoodsList($condition = array(), $fields = '*', $limit = null, $page = null, $order = 'rec_id desc', $group = null, $key = null) {
  304. return $this->table('order_goods')->field($fields)->where($condition)->limit($limit)->order($order)->group($group)->key($key)->page($page)->select();
  305. }
  306. /**
  307. * 取得订单扩展表列表
  308. * @param unknown $condition
  309. * @param string $fields
  310. * @param string $limit
  311. */
  312. public function getOrderCommonList($condition = array(), $fields = '*', $order = '', $limit = null) {
  313. return $this->table('order_common')->field($fields)->where($condition)->order($order)->limit($limit)->select();
  314. }
  315. /**
  316. * 插入订单支付表信息
  317. * @param array $data
  318. * @return int 返回 insert_id
  319. */
  320. public function addOrderPay($data) {
  321. return $this->table('order_pay')->insert($data);
  322. }
  323. /**
  324. * 插入订单表信息
  325. * @param array $data
  326. * @return int 返回 insert_id
  327. */
  328. public function addOrder($data) {
  329. $insert = $this->table('order')->insert($data);
  330. if ($insert) {
  331. //更新缓存
  332. QueueClient::push('delOrderCountCache',array('buyer_id'=>$data['buyer_id'],'store_id'=>$data['store_id']));
  333. }
  334. return $insert;
  335. }
  336. /**
  337. * 插入订单扩展表信息
  338. * @param array $data
  339. * @return int 返回 insert_id
  340. */
  341. public function addOrderCommon($data) {
  342. return $this->table('order_common')->insert($data);
  343. }
  344. /**
  345. * 插入订单扩展表信息
  346. * @param array $data
  347. * @return int 返回 insert_id
  348. */
  349. public function addOrderGoods($data) {
  350. return $this->table('order_goods')->insertAll($data);
  351. }
  352. /**
  353. * 添加订单日志
  354. */
  355. public function addOrderLog($data) {
  356. $data['log_role'] = str_replace(array('buyer','seller','system','admin'),array('买家','商家','系统','管理员'), $data['log_role']);
  357. $data['log_time'] = time();
  358. return $this->table('order_log')->insert($data);
  359. }
  360. /**
  361. * 更改订单信息
  362. *
  363. * @param unknown_type $data
  364. * @param unknown_type $condition
  365. */
  366. public function editOrder($data,$condition,$limit = '') {
  367. $update = $this->table('order')->where($condition)->limit($limit)->update($data);
  368. if ($update) {
  369. //更新缓存
  370. QueueClient::push('delOrderCountCache',$condition);
  371. }
  372. return $update;
  373. }
  374. /**
  375. * 更改订单信息
  376. *
  377. * @param unknown_type $data
  378. * @param unknown_type $condition
  379. */
  380. public function editOrderCommon($data,$condition) {
  381. return $this->table('order_common')->where($condition)->update($data);
  382. }
  383. /**
  384. * 更改订单信息
  385. *
  386. * @param unknown_type $data
  387. * @param unknown_type $condition
  388. */
  389. public function editOrderGoods($data,$condition) {
  390. return $this->table('order_goods')->where($condition)->update($data);
  391. }
  392. /**
  393. * 更改订单支付信息
  394. *
  395. * @param unknown_type $data
  396. * @param unknown_type $condition
  397. */
  398. public function editOrderPay($data,$condition) {
  399. return $this->table('order_pay')->where($condition)->update($data);
  400. }
  401. /**
  402. * 订单操作历史列表
  403. * @param unknown $order_id
  404. * @return Ambigous <multitype:, unknown>
  405. */
  406. public function getOrderLogList($condition) {
  407. return $this->table('order_log')->where($condition)->select();
  408. }
  409. /**
  410. * 取得单条订单操作记录
  411. * @param unknown $condition
  412. * @param string $order
  413. */
  414. public function getOrderLogInfo($condition = array(), $order = '') {
  415. return $this->table('order_log')->where($condition)->order($order)->find();
  416. }
  417. /**
  418. * 返回是否允许某些操作
  419. * @param unknown $operate
  420. * @param unknown $order_info
  421. */
  422. public function getOrderOperateState($operate,$order_info)
  423. {
  424. if (!is_array($order_info) || empty($order_info)) return false;
  425. switch ($operate)
  426. {
  427. //买家取消订单
  428. case 'buyer_cancel':
  429. $state = ($order_info['order_state'] == ORDER_STATE_NEW) || ($order_info['payment_code'] == 'offline' && $order_info['order_state'] == ORDER_STATE_PAY);
  430. break;
  431. //申请退款
  432. case 'refund_cancel':
  433. $state = $order_info['refund'] == 1 && !intval($order_info['lock_state']);
  434. break;
  435. //商家取消订单
  436. case 'store_cancel':
  437. $state = ($order_info['order_state'] == ORDER_STATE_NEW) ||
  438. ($order_info['payment_code'] == 'offline' &&
  439. in_array($order_info['order_state'],array(ORDER_STATE_PAY,ORDER_STATE_SEND)));
  440. break;
  441. //平台取消订单
  442. case 'system_cancel':
  443. $state = ($order_info['order_state'] == ORDER_STATE_NEW) ||
  444. ($order_info['payment_code'] == 'offline' && $order_info['order_state'] == ORDER_STATE_PAY);
  445. break;
  446. //平台收款
  447. case 'system_receive_pay':
  448. $state = $order_info['order_state'] == ORDER_STATE_NEW && $order_info['payment_code'] == 'online';
  449. break;
  450. //买家投诉
  451. case 'complain':
  452. $state = in_array($order_info['order_state'],array(ORDER_STATE_PAY,ORDER_STATE_SEND)) ||
  453. intval($order_info['finnshed_time']) > (time() - C('complain_time_limit'));
  454. break;
  455. case 'payment':
  456. $state = $order_info['order_state'] == ORDER_STATE_NEW && $order_info['payment_code'] == 'online';
  457. break;
  458. //调整运费
  459. case 'modify_price':
  460. $state = ($order_info['order_state'] == ORDER_STATE_NEW) ||
  461. ($order_info['payment_code'] == 'offline' && $order_info['order_state'] == ORDER_STATE_PAY);
  462. $state = floatval($order_info['shipping_fee']) > 0 && $state;
  463. break;
  464. //调整商品价格
  465. case 'spay_price':
  466. $state = ($order_info['order_state'] == ORDER_STATE_NEW) ||
  467. ($order_info['payment_code'] == 'offline' && $order_info['order_state'] == ORDER_STATE_PAY);
  468. $state = floatval($order_info['goods_amount']) > 0 && $state;
  469. break;
  470. //发货
  471. case 'send':
  472. $state = !$order_info['lock_state'] && $order_info['order_state'] == ORDER_STATE_PAY;
  473. break;
  474. //收货
  475. case 'receive':
  476. $state = !$order_info['lock_state'] && $order_info['order_state'] == ORDER_STATE_SEND;
  477. break;
  478. //评价
  479. case 'evaluation':
  480. $state = !$order_info['lock_state'] && !$order_info['evaluation_state'] && $order_info['order_state'] == ORDER_STATE_SUCCESS;
  481. break;
  482. //锁定
  483. case 'lock':
  484. $state = intval($order_info['lock_state']) ? true : false;
  485. break;
  486. //快递跟踪
  487. case 'deliver':
  488. $state = !empty($order_info['shipping_code']) && in_array($order_info['order_state'],array(ORDER_STATE_SEND,ORDER_STATE_SUCCESS));
  489. break;
  490. //放入回收站
  491. case 'delete':
  492. $state = in_array($order_info['order_state'], array(ORDER_STATE_CANCEL,ORDER_STATE_SUCCESS)) && $order_info['delete_state'] == 0;
  493. break;
  494. //永久删除、从回收站还原
  495. case 'drop':
  496. case 'restore':
  497. $state = in_array($order_info['order_state'], array(ORDER_STATE_CANCEL,ORDER_STATE_SUCCESS)) && $order_info['delete_state'] == 1;
  498. break;
  499. //分享
  500. case 'share':
  501. $state = true;
  502. break;
  503. }
  504. return $state;
  505. }
  506. /**
  507. * 联查订单表订单商品表
  508. *
  509. * @param array $condition
  510. * @param string $field
  511. * @param number $page
  512. * @param string $order
  513. * @return array
  514. */
  515. public function getOrderAndOrderGoodsList($condition, $field = '*', $page = 0, $order = 'rec_id desc') {
  516. return $this->table('order_goods,order')->join('inner')->on('order_goods.order_id=order.order_id')->where($condition)->field($field)->page($page)->order($order)->select();
  517. }
  518. /**
  519. * 订单销售记录 订单状态为20、30、40时
  520. * @param unknown $condition
  521. * @param string $field
  522. * @param number $page
  523. * @param string $order
  524. */
  525. public function getOrderAndOrderGoodsSalesRecordList($condition, $field="*", $page = 0, $order = 'rec_id desc') {
  526. $condition['order_state'] = array('in', array(ORDER_STATE_PAY, ORDER_STATE_SEND, ORDER_STATE_SUCCESS));
  527. return $this->getOrderAndOrderGoodsList($condition, $field, $page, $order);
  528. }
  529. public function setOrderDelivery($order_sn,$express_id,$shipper_code,$ship_time)
  530. {
  531. $items = $this->table('order')->field('order_id')->where(array('order_sn' => $order_sn))->select();
  532. if(empty($items) || count($items) == 0) {
  533. return false;
  534. } else {
  535. $order_id = $items[0]['order_id'];
  536. }
  537. $f1 = $this->table('order_common')->where(array('order_id' => $order_id))->update(array('shipping_express_id' => $express_id,'shipping_time' => $ship_time));
  538. $f2 = $this->table('order')->where(array('order_id' => $order_id))->update(array('order_state' => ORDER_STATE_SEND,'shipping_code' => $shipper_code,'delay_time' => $ship_time));
  539. return ($f1 && $f2);
  540. }
  541. }