OrderDispatch.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. <?php
  2. namespace addons\shopro\service\order;
  3. use app\admin\model\shopro\order\Order;
  4. use app\admin\model\shopro\order\OrderItem;
  5. use app\admin\model\shopro\order\Action as OrderAction;
  6. use app\admin\model\shopro\order\Express as OrderExpress;
  7. use app\admin\model\shopro\activity\Groupon as ActivityGroupon;
  8. use app\admin\model\shopro\activity\GrouponLog as ActivityGrouponLog;
  9. use app\admin\model\shopro\dispatch\Dispatch as DispatchModel;
  10. use app\admin\model\shopro\dispatch\DispatchAutosend;
  11. use addons\shopro\library\express\Express as ExpressLib;
  12. class OrderDispatch
  13. {
  14. public $order = null;
  15. public function __construct($params = [])
  16. {
  17. if (isset($params['order_id']) && !empty($params['order_id'])) {
  18. $this->order = Order::find($params['order_id']);
  19. if (!$this->order) {
  20. error_stop('订单不存在');
  21. }
  22. }
  23. }
  24. /**
  25. * 执行发货
  26. *
  27. * @return object
  28. */
  29. public function confirm($params)
  30. {
  31. $admin = auth_admin();
  32. $method = $params['method'] ?? '';
  33. if (!in_array($method, ['input', 'api', 'upload'])) {
  34. error_stop('请使用正确的发货方式');
  35. }
  36. if ($this->order->status !== Order::STATUS_PAID && !$this->order->isOffline($this->order)) {
  37. error_stop("该订单{$this->order->status_text},不能发货");
  38. }
  39. if ($this->order->apply_refund_status === Order::APPLY_REFUND_STATUS_APPLY) {
  40. error_stop("该订单已申请退款,暂不能发货");
  41. }
  42. switch ($method) {
  43. case 'api':
  44. list($orderItems, $express) = $this->doByApi($params);
  45. break;
  46. case 'input':
  47. list($orderItems, $express) = $this->doByInput($params);
  48. break;
  49. case 'upload':
  50. list($orderItems, $express) = $this->doByUpload($params);
  51. break;
  52. }
  53. // 添加包裹信息
  54. $orderExpress = OrderExpress::create([
  55. 'user_id' => $this->order->user_id,
  56. 'order_id' => $this->order->id,
  57. 'express_name' => $express['name'],
  58. 'express_code' => $express['code'],
  59. 'express_no' => $express['no'],
  60. 'method' => $method,
  61. 'driver' => $express['driver'] ?? null,
  62. 'ext' => $express['ext'] ?? null
  63. ]);
  64. // 修改订单商品发货状态
  65. foreach ($orderItems as $orderItem) {
  66. $orderItem->order_express_id = $orderExpress->id;
  67. $orderItem->dispatch_status = OrderItem::DISPATCH_STATUS_SENDED;
  68. $orderItem->ext = array_merge($orderItem->ext, ['send_time' => time()]); // item 发货时间
  69. $orderItem->save();
  70. OrderAction::add($this->order, $orderItem, $admin, 'admin', "商品{$orderItem->goods_title}已发货");
  71. }
  72. $this->subscribeExpressInfo($orderExpress);
  73. // 订单发货后
  74. $data = [
  75. 'order' => $this->order,
  76. 'items' => $orderItems,
  77. 'express' => $orderExpress,
  78. 'dispatch_type' => 'express',
  79. ];
  80. \think\Hook::listen('order_dispatch_after', $data);
  81. return $express;
  82. }
  83. // 手动发货
  84. private function doByInput($params)
  85. {
  86. $orderItems = $this->getDispatchOrderItems($params, 'express');
  87. $express = $params['express'] ?? null;
  88. if (empty($express['name']) || empty($express['code']) || empty($express['no']) || strpos($express['no'], '=') !== false) {
  89. error_stop('请输入正确的快递信息');
  90. }
  91. return [$orderItems, $express];
  92. }
  93. // API发货
  94. private function doByApi($params)
  95. {
  96. $orderItems = $this->getDispatchOrderItems($params, 'express');
  97. $sender = $params['sender'] ?? null;
  98. $expressLib = new ExpressLib();
  99. $data = [
  100. 'order' => $this->order,
  101. 'sender' => $sender,
  102. 'consignee' => $this->order->address
  103. ];
  104. $express = $expressLib->eOrder($data, $orderItems);
  105. return [$orderItems, $express];
  106. }
  107. // 上传发货模板发货 TODO: 如果发货单比较多,循环更新可能会比较慢,考虑解析完模版信息以后,把数据返回前端,再次执行批量发货流程
  108. private function doByUpload($params)
  109. {
  110. $orderItems = $this->getDispatchOrderItems($params, 'express');
  111. $express = $params['express'] ?? null;
  112. if (empty($express['name']) || empty($express['code']) || empty($express['no'])) {
  113. error_stop('请输入正确的快递信息');
  114. }
  115. return [$orderItems, $express];
  116. }
  117. // 获取可发货的订单商品
  118. public function getDispatchOrderItems($params = null, $dispatch_type = 'express')
  119. {
  120. $orderItemIds = $params['order_item_ids'] ?? [];
  121. $whereCanDispatch['order_id'] = $this->order->id;
  122. $whereCanDispatch['dispatch_status'] = OrderItem::DISPATCH_STATUS_NOSEND;
  123. $whereCanDispatch['aftersale_status'] = ['<>', OrderItem::AFTERSALE_STATUS_ING];
  124. $whereCanDispatch['refund_status'] = OrderItem::REFUND_STATUS_NOREFUND;
  125. $whereCanDispatch['dispatch_type'] = $dispatch_type;
  126. if (empty($orderItemIds)) {
  127. $orderItems = OrderItem::where($whereCanDispatch)->select();
  128. } else {
  129. $orderItems = OrderItem::where('id', 'in', $orderItemIds)->where($whereCanDispatch)->select();
  130. if (count($orderItems) !== count($orderItemIds)) {
  131. error_stop('选中商品暂不能发货');
  132. }
  133. }
  134. if (!$orderItems) {
  135. error_stop('该订单无可发货商品');
  136. }
  137. return $orderItems;
  138. }
  139. /**
  140. * 取消发货
  141. *
  142. */
  143. public function cancel($params)
  144. {
  145. $admin = auth_user();
  146. $order_express_id = $params['order_express_id'] ?? 0;
  147. $orderExpress = OrderExpress::where('id', $order_express_id)->find();
  148. if (!$orderExpress) {
  149. error_stop('未找到发货单');
  150. }
  151. // 1.检测是不是用api发的 有些快递不支持取消接口 所以不判断了,统一手动取消
  152. // if ($orderExpress->method === 'api') {
  153. // // TODO: 走取消运单接口
  154. // $expressLib = new ExpressLib();
  155. // $data = [
  156. // 'express_no' => $orderExpress['express_no'],
  157. // 'express_code' => $orderExpress['express_code'],
  158. // 'order_code' => $orderExpress['ext']['Order']['OrderCode']
  159. // ];
  160. // $express = $expressLib->cancel($data);
  161. // }
  162. // 2. 变更发货状态
  163. $orderItems = OrderItem::where([
  164. 'order_id' => $this->order->id,
  165. 'order_express_id' => $orderExpress->id
  166. ])->where('dispatch_type', 'express')->select();
  167. foreach ($orderItems as $orderItem) {
  168. $orderItem->order_express_id = 0;
  169. $orderItem->dispatch_status = OrderItem::DISPATCH_STATUS_NOSEND;
  170. $orderItem->save();
  171. OrderAction::add($this->order, null, $admin, 'admin', "已取消发货");
  172. }
  173. // 删除发货单
  174. $orderExpress->delete();
  175. return true;
  176. }
  177. /**
  178. * 修改发货信息
  179. *
  180. */
  181. public function change($params)
  182. {
  183. $admin = auth_user();
  184. $order_express_id = $params['order_express_id'] ?? 0;
  185. $orderExpress = OrderExpress::where('id', $order_express_id)->find();
  186. if (!$orderExpress) {
  187. error_stop('未找到发货单');
  188. }
  189. // 1.1 检测是不是用api发的 如果是则提醒取消运单再重新走发货流程 此时什么都不用做
  190. if ($orderExpress->method === 'api') {
  191. error_stop('该发货单已被推送第三方平台,请取消后重新发货');
  192. }
  193. // 1.2 如果不是则手动变更运单信息(快递公司、运单号)
  194. $express = $params['express'] ?? null;
  195. if (empty($express['name']) || empty($express['code']) || empty($express['no']) || strpos($express['no'], '=') !== false) {
  196. error_stop('请输入正确的快递信息');
  197. }
  198. $orderExpress->save([
  199. 'express_name' => $express['name'],
  200. 'express_code' => $express['code'],
  201. 'express_no' => $express['no'],
  202. 'method' => 'input'
  203. ]);
  204. OrderAction::add($this->order, null, $admin, 'admin', "变更发货信息");
  205. $this->subscribeExpressInfo($orderExpress);
  206. // 修改发货信息
  207. $data = [
  208. 'order' => $this->order,
  209. 'express' => $orderExpress,
  210. 'dispatch_type' => 'express',
  211. ];
  212. \think\Hook::listen('order_dispatch_change', $data);
  213. return $express;
  214. }
  215. // 解析批量发货信息,筛选出能发货的订单
  216. public function multiple($params)
  217. {
  218. // 上传发货模板
  219. if (!empty($params['file'])) {
  220. $express = $params['express'];
  221. $file = $params['file']->getPathname();
  222. $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
  223. $PHPExcel = $reader->load($file);
  224. $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
  225. $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
  226. if ($allRow <= 2) {
  227. error_stop('您的发货列表为空');
  228. }
  229. $orderExpressMap = [];
  230. $orderId = 0;
  231. $orderSn = "";
  232. for ($currentRow = 2; $currentRow <= $allRow - 1; $currentRow++) {
  233. $orderId = $currentSheet->getCellByColumnAndRow(1, $currentRow)->getValue() ?? $orderId;
  234. $orderSn = $currentSheet->getCellByColumnAndRow(2, $currentRow)->getValue() ?? $orderSn;
  235. $orderItemId = $currentSheet->getCellByColumnAndRow(8, $currentRow)->getValue();
  236. if (empty($orderItemId)) {
  237. error_stop("发货单格式不正确");
  238. }
  239. $orderExpressNo = $currentSheet->getCellByColumnAndRow(15, $currentRow)->getValue();
  240. if (empty($orderExpressNo)) {
  241. error_stop("请填写 订单ID-{$orderId} 的运单号");
  242. }
  243. $orderExpressMap["$orderId.$orderSn"][$orderExpressNo][] = $orderItemId;
  244. }
  245. $list = [];
  246. foreach ($orderExpressMap as $orderFlag => $orderExpress) {
  247. foreach ($orderExpress as $expressNo => $orderItemIds) {
  248. $order = explode('.', $orderFlag);
  249. $list[] = [
  250. 'order_id' => $order[0],
  251. 'order_sn' => $order[1],
  252. 'order_item_ids' => $orderItemIds,
  253. 'express' => [
  254. 'name' => $express['name'],
  255. 'code' => $express['code'],
  256. 'no' => $expressNo
  257. ]
  258. ];
  259. }
  260. }
  261. return $list;
  262. } else {
  263. $list = [];
  264. $orderIds = $params['order_ids'] ?? [];
  265. if (empty($orderIds)) {
  266. error_stop('请选择发货订单');
  267. }
  268. foreach ($orderIds as $orderId) {
  269. $list[] = [
  270. 'order_id' => $orderId,
  271. 'order_sn' => Order::where('id', $orderId)->value('order_sn'),
  272. 'order_item_ids' => $this->getDispatchOrderItemIds($orderId)
  273. ];
  274. }
  275. return $list;
  276. }
  277. }
  278. // 获取可发货的订单商品
  279. private function getDispatchOrderItemIds($orderId)
  280. {
  281. $whereCanDispatch = [
  282. 'order_id' => $orderId,
  283. 'dispatch_status' => OrderItem::DISPATCH_STATUS_NOSEND,
  284. 'aftersale_status' => ['neq', OrderItem::AFTERSALE_STATUS_ING],
  285. 'refund_status' => OrderItem::REFUND_STATUS_NOREFUND,
  286. 'dispatch_type' => 'express'
  287. ];
  288. $orderItems = OrderItem::where($whereCanDispatch)->column('id');
  289. return $orderItems;
  290. }
  291. // 订阅物流追踪
  292. private function subscribeExpressInfo($orderExpress)
  293. {
  294. try {
  295. $expressLib = new ExpressLib();
  296. $a = $expressLib->subscribe([
  297. 'express_code' => $orderExpress['express_code'],
  298. 'express_no' => $orderExpress['express_no']
  299. ]);
  300. } catch (\Exception $e) {
  301. // Nothing TODO
  302. return;
  303. }
  304. }
  305. /**
  306. * 手动发货
  307. *
  308. * @param array $params
  309. * @return void
  310. */
  311. public function customDispatch($params)
  312. {
  313. $admin = auth_admin();
  314. $custom_type = $params['custom_type'] ?? 'text';
  315. $custom_content = $params['custom_content'] ?? ($custom_type == 'text' ? '' : []);
  316. if ($this->order->status !== Order::STATUS_PAID && !$this->order->isOffline($this->order)) {
  317. error_stop("该订单{$this->order->status_text},不能发货");
  318. }
  319. if ($this->order->apply_refund_status === Order::APPLY_REFUND_STATUS_APPLY) {
  320. error_stop("该订单已申请退款,暂不能发货");
  321. }
  322. // 获取手动发货的 items
  323. $orderItems = $this->getDispatchOrderItems($params, 'custom');
  324. $customExt = [ // 手动发货信息
  325. 'dispatch_content_type' => $custom_type,
  326. 'dispatch_content' => $custom_content
  327. ];
  328. // 修改订单商品发货状态
  329. foreach ($orderItems as $orderItem) {
  330. $orderItem->dispatch_status = OrderItem::DISPATCH_STATUS_SENDED;
  331. $orderItem->ext = array_merge($orderItem->ext, $customExt, ['send_time' => time()]); // item 发货时间
  332. $orderItem->save();
  333. OrderAction::add($this->order, $orderItem, $admin, 'admin', "商品{$orderItem->goods_title}已发货");
  334. }
  335. // 订单发货后
  336. $data = [
  337. 'order' => $this->order,
  338. 'items' => $orderItems,
  339. 'express' => null,
  340. 'dispatch_type' => 'custom',
  341. ];
  342. \think\Hook::listen('order_dispatch_after', $data);
  343. }
  344. /**
  345. * 拼团完成时触发检测自动发货
  346. *
  347. * @return bool
  348. */
  349. public function grouponCheckDispatchAndSend()
  350. {
  351. $this->systemCheckAutoSend();
  352. return true;
  353. }
  354. /**
  355. * 普通商品自动发货
  356. *
  357. * @return bool
  358. */
  359. public function checkDispatchAndSend()
  360. {
  361. // 拼团不自动发货,等成团完成才发货
  362. $orderExt = $this->order['ext'];
  363. $buy_type = ($orderExt && isset($orderExt['buy_type'])) ? $orderExt['buy_type'] : '';
  364. if ($this->order['activity_type'] && strpos($this->order['activity_type'], 'groupon') !== false && $buy_type == 'groupon') {
  365. return true; // 这里不对拼团的订单进行自动发货,等拼团成功在检测
  366. }
  367. // 检测需要自动发货的 item
  368. $this->systemCheckAutoSend();
  369. return true;
  370. }
  371. /**
  372. * 系统检测自动发货
  373. */
  374. private function systemCheckAutoSend()
  375. {
  376. $autosendItems = [];
  377. // 判断订单是否有需要发货的商品,并进行自动发货(autosend)
  378. foreach ($this->order->items as $key => $item) {
  379. // 判断不是未发货状态,或者退款完成,continue
  380. if (
  381. $item['dispatch_status'] == OrderItem::DISPATCH_STATUS_NOSEND
  382. && $item['aftersale_status'] != OrderItem::AFTERSALE_STATUS_ING
  383. && $item['refund_status'] == OrderItem::REFUND_STATUS_NOREFUND
  384. ) {
  385. // 订单可以发货
  386. switch ($item['dispatch_type']) {
  387. case 'autosend':
  388. // 自动发货
  389. $autosendItems[] = $item;
  390. }
  391. }
  392. }
  393. if ($autosendItems) {
  394. $this->autoSendItems($autosendItems, ['oper_type' => 'system']);
  395. }
  396. }
  397. /**
  398. * 当前订单需要自动发货的所有商品
  399. *
  400. * @param object|array $items
  401. * @return void
  402. */
  403. private function autoSendItems($items)
  404. {
  405. foreach ($items as $item) {
  406. $autosendExt = $this->getAutosendContent($item);
  407. $item->dispatch_status = OrderItem::DISPATCH_STATUS_SENDED;
  408. $item->ext = array_merge($item->ext, $autosendExt, ['send_time' => time()]); // item 发货时间
  409. $item->save();
  410. OrderAction::add($this->order, $item, null, 'system', "商品{$item->goods_title}已发货");
  411. }
  412. $data = [
  413. 'order' => $this->order,
  414. 'items' => $items,
  415. 'express' => null,
  416. 'dispatch_type' => 'custom',
  417. ];
  418. // 发货后事件,消息通知
  419. \think\Hook::listen('order_dispatch_after', $data);
  420. }
  421. /**
  422. * 获取商品的自动发货模板数据
  423. *
  424. * @param object|array $item
  425. * @return array
  426. */
  427. private function getAutosendContent($item)
  428. {
  429. // 获取配送模板
  430. $result = [];
  431. $dispatch = DispatchModel::with([$item['dispatch_type']])->show()->where('type', $item['dispatch_type'])->where('id', $item['dispatch_id'])->find();
  432. if ($dispatch && $dispatch->autosend) {
  433. $autosend = $dispatch->autosend;
  434. if (in_array($autosend['type'], ['text', 'params'])) {
  435. $result['dispatch_content_type'] = $autosend['type'];
  436. $result['dispatch_content'] = $autosend['content'];
  437. }
  438. }
  439. return $result;
  440. }
  441. }