Order.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php namespace Place_order\Lib\Source;
  2. use Dever;
  3. use Place;
  4. class Order
  5. {
  6. # 获取订单信息
  7. public function getInfo($info, $view = false)
  8. {
  9. if ($info['status'] == 1) {
  10. $m = 3600;
  11. # 支付倒计时
  12. $info['time'] = time() - $info['cdate'];
  13. if ($info['time'] >= $m) {
  14. # 已过期,自动取消
  15. $info['time'] = -1;
  16. $this->cancel($info);
  17. $info['status'] = 9;
  18. } else {
  19. $info['time'] = $m - $info['time'];
  20. }
  21. }
  22. $info['scash'] = $info['cash'];
  23. $info['cash'] = $info['cash'];
  24. $info['detail'] = $this->getDetail($info['id']);
  25. $info['info'] = '共' . intval($info['num']) . '件商品';
  26. if ($view) {
  27. $this->getView($info);
  28. }
  29. return $info;
  30. }
  31. # 获取订单里每个商品
  32. public function getDetail($id)
  33. {
  34. $result = Dever::db('source_detail', 'place_order')->select(['order_id' => $id], ['col' => 'id,name,pic,sku_name,sku_id,cash,num']);
  35. if ($result) {
  36. foreach ($result as &$v) {
  37. $v['cash'] = Dever::load('price', 'place')->getText($v['cash'], 3);
  38. }
  39. }
  40. return $result;
  41. }
  42. # 获取订单详情
  43. public function getView($info)
  44. {
  45. # 收货地址
  46. if ($info['address_id']) {
  47. $info['address'] = Dever::load('address', 'place_user')->getInfo($info['address_id'], $info['uid']);
  48. }
  49. if (in_[$info['status'], [3,4,5]]) {
  50. $info = Dever::load('source/express', 'place_order')->getInfo($info, $info['address_id']);
  51. $info['status_name'] = Dever::db('source', 'place_order')->value('status', $info['status']);
  52. }
  53. $info['status_list'] = [
  54. 1 => [
  55. 'selected' => false,
  56. 'name' => '待付款',
  57. 'time' => $info['cdate_str'],
  58. ],
  59. 2 => [
  60. 'selected' => false,
  61. 'name' => '待发货',
  62. 'time' => $info['pdate_str'] ?? '',
  63. ],
  64. 3 => [
  65. 'selected' => false,
  66. 'name' => '待签收',
  67. 'time' => $info['ddate_str'] ?? '',
  68. ],
  69. 4 => [
  70. 'selected' => false,
  71. 'name' => '待收货',
  72. 'time' => $info['qdate_str'] ?? '',
  73. ],
  74. 5 => [
  75. 'selected' => false,
  76. 'name' => '已完成',
  77. 'time' => $info['fdate_str'] ?? '',
  78. ],
  79. ];
  80. foreach ($info['status_list'] as $k => &$status_list) {
  81. if ($info['status'] >= $k) {
  82. $status_list['selected'] = true;
  83. }
  84. }
  85. if ($info['status'] == 1) {
  86. $info['status_msg'] = '下单成功,待支付';
  87. } elseif ($info['status'] == 2) {
  88. $info['status_msg'] = '支付成功,待发货';
  89. } elseif ($info['status'] == 3) {
  90. $info['status_msg'] = '配送中,等待签收';
  91. } elseif ($info['status'] == 4) {
  92. $info['status_msg'] = '已签收,等待确认';
  93. } elseif ($info['status'] == 5 || $info['status'] == 6) {
  94. $info['status_msg'] = '订单已完成';
  95. } elseif ($info['status'] > 6) {
  96. $info['status_msg'] = '订单已取消';
  97. }
  98. $info['refund'] = false;
  99. $info['refund_list'] = Dever::db('source_refund', 'place_order')->select(['order_id' => $info['id']]);
  100. if ($info['refund_list']) {
  101. foreach ($info['refund_list'] as $k => $v) {
  102. if ($v['status'] < 4) {
  103. $info['refund'] = $v;
  104. }
  105. }
  106. }
  107. if ($info['refund']) {
  108. if ($info['refund']['status'] > 2) {
  109. $info['refund'] = Dever::load('source/express', 'place_order')->getInfo($info['refund'], $info['address_id'], 2);
  110. }
  111. $info['refund']['cdate_str'] = date('Y-m-d H:i:s', $info['refund']['cdate']);
  112. if (isset($info['refund']['sdate']) && $info['refund']['sdate']) {
  113. $info['refund']['sdate_str'] = date('Y-m-d H:i:s', $info['refund']['sdate']);
  114. }
  115. if (isset($info['refund']['ddate']) && $info['refund']['ddate']) {
  116. $info['refund']['ddate_str'] = date('Y-m-d H:i:s', $info['refund']['ddate']);
  117. }
  118. if (isset($info['refund']['fdate']) && $info['refund']['fdate']) {
  119. $info['refund']['fdate_str'] = date('Y-m-d H:i:s', $info['refund']['fdate']);
  120. }
  121. if ($info['refund']['type'] == 1) {
  122. $info['refund_status_list'] = [
  123. 1 => [
  124. 'selected' => false,
  125. 'name' => '退货退款',
  126. 'time' => $info['refund']['cdate_str'],
  127. ],
  128. 2 => [
  129. 'selected' => false,
  130. 'name' => '已审核',
  131. 'time' => $info['refund']['sdate_str'] ?? '',
  132. ],
  133. 3 => [
  134. 'selected' => false,
  135. 'name' => '买家已发货',
  136. 'time' => $info['refund']['ddate_str'] ?? '',
  137. ],
  138. 4 => [
  139. 'selected' => false,
  140. 'name' => '已完成',
  141. 'time' => $info['refund']['fdate_str'] ?? '',
  142. ],
  143. ];
  144. }
  145. if ($info['refund']['type'] == 2) {
  146. $info['refund_status_list'] = [
  147. 1 => [
  148. 'selected' => false,
  149. 'name' => '仅退款',
  150. 'time' => $info['cdate_str'],
  151. ],
  152. 2 => [
  153. 'selected' => false,
  154. 'name' => '已审核',
  155. 'time' => $info['sdate_str'] ?? '',
  156. ],
  157. 4 => [
  158. 'selected' => false,
  159. 'name' => '已完成',
  160. 'time' => $info['fdate_str'] ?? '',
  161. ],
  162. ];
  163. }
  164. foreach ($info['refund_status_list'] as $k => $v) {
  165. if ($info['refund']['status'] >= $k) {
  166. $info['refund_status_list'][$k]['selected'] = true;
  167. }
  168. }
  169. }
  170. return $info;
  171. }
  172. # 取消订单
  173. public function cancel($order, $status = 9)
  174. {
  175. if ($order['status'] == 1) {
  176. $state = Dever::db('source', 'place_order')->update($order['id'], ['status' => $status, 'fdate' => time()]);
  177. if ($state) {
  178. # 恢复库存
  179. $this->resell($order['id']);
  180. }
  181. return 'ok';
  182. }
  183. }
  184. # 支付成功
  185. public function success($order, $status)
  186. {
  187. if ($status == 1) {
  188. $update['status'] = 2;
  189. $msg = '支付成功';
  190. } else {
  191. $update['status'] = 7;
  192. $msg = '支付失败';
  193. }
  194. $update['pdate'] = time();
  195. $state = Dever::db('source', 'place_order')->update($order['id'], $update);
  196. Dever::load('source/log', 'place_order')->up(1, $order['uid'], $data['order_id'], $msg);
  197. if ($state && $update['status'] == 7) {
  198. # 恢复库存
  199. $this->resell($order['id']);
  200. }
  201. return $state;
  202. }
  203. # 确认收货
  204. public function finish_commit(){}
  205. public function finish($type = 1, $order, $yes = false)
  206. {
  207. if ($yes || ($order['status'] >= 3 && $order['status'] <= 6)) {
  208. if ($type == 1) {
  209. $uid = $order['uid'];
  210. } else {
  211. $uid = '';
  212. }
  213. # 检查有没有退款
  214. $refund = Dever::db('source_refund', 'place_order')->find(['order_id' => $order['id'], 'refund_status' => 1]);
  215. $status = 5;
  216. if ($refund) {
  217. $status = 6;
  218. }
  219. Dever::db('source', 'place_order')->update($order['id'], ['status' => $status, 'fdate' => time()]);
  220. Dever::load('source/log', 'place_order')->up($type, $uid, $order['id'], '确认收货,订单完成');
  221. # 增加次数
  222. if ($status) {
  223. $detail = Dever::db('source_detail', 'place_order')->select(['order_id' => $order['id']]);
  224. if ($detail) {
  225. foreach ($detail as $k => $v) {
  226. $refund_detail = Dever::db('source_refund_detail', 'place_order')->select(['detail_id' => $v['id'], 'refund_status' => 1]);
  227. $unum = $v['unum'];
  228. if ($refund_detail) {
  229. foreach ($refund_detail as $v1) {
  230. $unum -= $v1['num'];
  231. }
  232. }
  233. if ($unum > 0) {
  234. Dever::load('source/user', 'place_order')->up($order['uid'], $v['source_id'], $v['sku_id'], 1, $unum);
  235. }
  236. }
  237. }
  238. # 获取当前用户上级 增加佣金 获取实付款
  239. //Dever::load('log', 'place_score')->add(Place::$uid, '推广', '购买');
  240. }
  241. }
  242. }
  243. # 恢复库存
  244. public function resell($id)
  245. {
  246. $result = Dever::db('source_detail', 'place_order')->select(['order_id' => $id]);
  247. if ($result) {
  248. foreach ($result as $v) {
  249. $state = Dever::load('stock', 'place_source')->sell($v['info_id'], $v['sku_id'], $v['num']*-1);
  250. }
  251. }
  252. }
  253. }