order.model.php 22 KB

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