order.model.php 22 KB

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