StoreOrder.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\adminapi\controller\v1\order;
  12. use app\adminapi\controller\AuthController;
  13. use app\adminapi\validate\order\StoreOrderValidate;
  14. use app\jobs\MiniOrderJob;
  15. use app\jobs\OrderExpressJob;
  16. use app\services\serve\ServeServices;
  17. use app\services\wechat\WechatUserServices;
  18. use crmeb\services\FileService;
  19. use app\services\order\{StoreOrderCartInfoServices,
  20. StoreOrderDeliveryServices,
  21. StoreOrderRefundServices,
  22. StoreOrderStatusServices,
  23. StoreOrderTakeServices,
  24. StoreOrderWriteOffServices,
  25. StoreOrderServices
  26. };
  27. use app\services\pay\OrderOfflineServices;
  28. use app\services\shipping\ExpressServices;
  29. use app\services\system\store\SystemStoreServices;
  30. use app\services\user\UserServices;
  31. use think\facade\App;
  32. /**
  33. * 订单管理
  34. * Class StoreOrder
  35. * @package app\adminapi\controller\v1\order
  36. */
  37. class StoreOrder extends AuthController
  38. {
  39. /**
  40. * StoreOrder constructor.
  41. * @param App $app
  42. * @param StoreOrderServices $service
  43. * @method temp
  44. */
  45. public function __construct(App $app, StoreOrderServices $service)
  46. {
  47. parent::__construct($app);
  48. $this->services = $service;
  49. }
  50. /**
  51. * 获取订单类型数量
  52. * @return mixed
  53. */
  54. public function chart()
  55. {
  56. $where = $this->request->getMore([
  57. ['data', '', '', 'time'],
  58. ['type', ''],
  59. ['pay_type', ''],
  60. ['field_key', 'all'],
  61. ['real_name', ''],
  62. ]);
  63. $data = $this->services->orderCount($where);
  64. return app('json')->success($data);
  65. }
  66. /**
  67. * 订单列表
  68. * @return mixed
  69. * @throws \think\db\exception\DataNotFoundException
  70. * @throws \think\db\exception\DbException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. */
  73. public function lst()
  74. {
  75. $where = $this->request->getMore([
  76. ['status', ''],
  77. ['real_name', ''],
  78. ['is_del', ''],
  79. ['data', '', '', 'time'],
  80. ['type', ''],
  81. ['pay_type', ''],
  82. ['order', ''],
  83. ['field_key', ''],
  84. ]);
  85. $where['is_system_del'] = 0;
  86. $where['pid'] = 0;
  87. if ($where['status'] == 1) $where = $where + ['shipping_type' => 1];
  88. return app('json')->success($this->services->getOrderList($where, ['*'], ['split' => function ($query) {
  89. $query->field('id,pid');
  90. }, 'pink', 'invoice', 'division']));
  91. }
  92. /**
  93. * 核销码核销
  94. * @param StoreOrderWriteOffServices $services
  95. * @return mixed
  96. * @throws \think\db\exception\DataNotFoundException
  97. * @throws \think\db\exception\DbException
  98. * @throws \think\db\exception\ModelNotFoundException
  99. */
  100. public function write_order(StoreOrderWriteOffServices $services)
  101. {
  102. [$code, $confirm] = $this->request->getMore([
  103. ['code', ''],
  104. ['confirm', 0]
  105. ], true);
  106. if (!$code) return app('json')->fail('参数错误');
  107. $orderInfo = $services->writeOffOrder($code, (int)$confirm);
  108. if ($confirm == 0) {
  109. return app('json')->success('验证成功', $orderInfo);
  110. }
  111. return app('json')->success('核销成功');
  112. }
  113. /**
  114. * 订单号核销
  115. * @param StoreOrderWriteOffServices $services
  116. * @param $order_id
  117. * @return mixed
  118. * @throws \think\db\exception\DataNotFoundException
  119. * @throws \think\db\exception\DbException
  120. * @throws \think\db\exception\ModelNotFoundException
  121. */
  122. public function write_update(StoreOrderWriteOffServices $services, $order_id)
  123. {
  124. $orderInfo = $this->services->getOne(['order_id' => $order_id, 'is_del' => 0]);
  125. if ($orderInfo->shipping_type != 2 && $orderInfo->delivery_type != 'send') {
  126. return app('json')->fail('核销订单未查到');
  127. } else {
  128. if (!$orderInfo->verify_code) {
  129. return app('json')->fail('参数错误');
  130. }
  131. $orderInfo = $services->writeOffOrder($orderInfo->verify_code, 1);
  132. if ($orderInfo) {
  133. return app('json')->success('验证成功');
  134. } else {
  135. return app('json')->fail('核销失败');
  136. }
  137. }
  138. }
  139. /**
  140. * 订单改价表单
  141. * @param $id
  142. * @return mixed
  143. * @throws \FormBuilder\Exception\FormBuilderException
  144. */
  145. public function edit($id)
  146. {
  147. if (!$id) return app('json')->fail('参数错误');
  148. return app('json')->success($this->services->updateForm($id));
  149. }
  150. /**
  151. * 订单改价
  152. * @param $id
  153. * @return mixed
  154. * @throws \Exception
  155. */
  156. public function update($id)
  157. {
  158. if (!$id) return app('json')->fail('参数错误');
  159. $data = $this->request->postMore([
  160. ['order_id', ''],
  161. ['total_price', 0],
  162. ['total_postage', 0],
  163. ['pay_price', 0],
  164. ['pay_postage', 0],
  165. ['gain_integral', 0],
  166. ]);
  167. $this->validate($data, StoreOrderValidate::class);
  168. if ($data['total_price'] < 0) return app('json')->fail('订单金额必须填写');
  169. if ($data['pay_price'] < 0) return app('json')->fail('订单金额必须填写');
  170. $this->services->updateOrder((int)$id, $data);
  171. return app('json')->success('修改成功');
  172. }
  173. /**
  174. * 获取快递公司
  175. * @return mixed
  176. */
  177. public function express(ExpressServices $services)
  178. {
  179. [$status] = $this->request->getMore([
  180. ['status', ''],
  181. ], true);
  182. if ($status != '') $data['status'] = $status;
  183. if ($status == 'undefined') $data['status'] = 1;
  184. $data['is_show'] = 1;
  185. return app('json')->success($services->express($data));
  186. }
  187. /**
  188. * 批量删除用户已经删除的订单
  189. * @return mixed
  190. */
  191. public function del_orders()
  192. {
  193. [$ids] = $this->request->postMore([
  194. ['ids', []],
  195. ], true);
  196. if (!count($ids)) return app('json')->fail('请选择需要删除的订单');
  197. if ($this->services->getOrderIdsCount($ids))
  198. return app('json')->fail('您选择的的订单存在用户未删除的订单');
  199. if ($this->services->batchUpdate($ids, ['is_system_del' => 1]))
  200. return app('json')->success('删除成功');
  201. else
  202. return app('json')->fail('删除失败');
  203. }
  204. /**
  205. * 删除订单
  206. * @param $id
  207. * @return mixed
  208. */
  209. public function del($id)
  210. {
  211. if (!$id || !($orderInfo = $this->services->get($id)))
  212. return app('json')->fail('订单不存在');
  213. if (!$orderInfo->is_del)
  214. return app('json')->fail('您选择的的订单存在用户未删除的订单');
  215. $orderInfo->is_system_del = 1;
  216. if ($orderInfo->save()) {
  217. /** @var StoreOrderRefundServices $refundServices */
  218. $refundServices = app()->make(StoreOrderRefundServices::class);
  219. $refundServices->update(['store_order_id' => $id], ['is_system_del' => 1]);
  220. return app('json')->success('删除成功');
  221. } else
  222. return app('json')->fail('删除失败');
  223. }
  224. /**
  225. * 订单发送货
  226. * @param $id
  227. * @param StoreOrderDeliveryServices $services
  228. * @return mixed
  229. */
  230. public function update_delivery($id, StoreOrderDeliveryServices $services)
  231. {
  232. $data = $this->request->postMore([
  233. ['type', 1],
  234. ['delivery_name', ''],//快递公司名称
  235. ['delivery_id', ''],//快递单号
  236. ['delivery_code', ''],//快递公司编码
  237. ['express_record_type', 2],//发货记录类型:2=电子面单;3=商家寄件
  238. ['express_temp_id', ""],//电子面单模板
  239. ['to_name', ''],//寄件人姓名
  240. ['to_tel', ''],//寄件人电话
  241. ['to_addr', ''],//寄件人地址
  242. ['sh_delivery_name', ''],//送货人姓名
  243. ['sh_delivery_id', ''],//送货人电话
  244. ['sh_delivery_uid', ''],//送货人ID
  245. ['fictitious_content', ''],//虚拟发货内容
  246. ['day_type', 0], //顺丰传 0今天,1明天,2后台
  247. ['pickup_time', []],//开始时间 9:00,结束时间 10:00 开始时间和结束时间之间不能小于一个小时
  248. ]);
  249. return app('json')->success('操作成功', $services->delivery((int)$id, $data));
  250. }
  251. /**
  252. * 订单拆单发送货
  253. * @param $id
  254. * @param StoreOrderDeliveryServices $services
  255. * @return mixed
  256. * @throws \think\db\exception\DataNotFoundException
  257. * @throws \think\db\exception\DbException
  258. * @throws \think\db\exception\ModelNotFoundException
  259. */
  260. public function split_delivery($id, StoreOrderDeliveryServices $services)
  261. {
  262. $data = $this->request->postMore([
  263. ['type', 1],
  264. ['delivery_name', ''],//快递公司名称
  265. ['delivery_id', ''],//快递单号
  266. ['delivery_code', ''],//快递公司编码
  267. ['express_record_type', 2],//发货记录类型
  268. ['express_temp_id', ""],//电子面单模板
  269. ['to_name', ''],//寄件人姓名
  270. ['to_tel', ''],//寄件人电话
  271. ['to_addr', ''],//寄件人地址
  272. ['sh_delivery_name', ''],//送货人姓名
  273. ['sh_delivery_id', ''],//送货人电话
  274. ['sh_delivery_uid', ''],//送货人ID
  275. ['fictitious_content', ''],//虚拟发货内容
  276. ['cart_ids', []],
  277. ['day_type', 0], //顺丰传 0今天,1明天,2后台
  278. ['pickup_time', []],//开始时间 9:00,结束时间 10:00 开始时间和结束时间之间不能小于一个小时
  279. ['service_type', ''],//快递业务类型
  280. ]);
  281. if (!$id) {
  282. return app('json')->fail('参数错误');
  283. }
  284. if (!$data['cart_ids']) {
  285. return app('json')->fail('请选择发货商品');
  286. }
  287. foreach ($data['cart_ids'] as $cart) {
  288. if (!isset($cart['cart_id']) || !$cart['cart_id'] || !isset($cart['cart_num']) || !$cart['cart_num']) {
  289. return app('json')->fail('请重新选择发货商品或发货件数');
  290. }
  291. }
  292. return app('json')->success('操作成功', $services->splitDelivery((int)$id, $data));
  293. }
  294. /**
  295. * 获取寄件预扣金额
  296. * @param ServeServices $services
  297. * @return \think\Response
  298. * @author 等风来
  299. * @email 136327134@qq.com
  300. * @date 2023/6/16
  301. */
  302. public function getPrice(ServeServices $services)
  303. {
  304. $data = $this->request->postMore([
  305. ['kuaidicom', ''],
  306. ['send_address', ''],
  307. ['orderId', ''],
  308. ['service_type', ''],
  309. ['cart_ids', []],
  310. ]);
  311. $orderInfo = $this->services->get($data['orderId'], ['user_address', 'cart_id']);
  312. if (!$orderInfo) {
  313. return app('json')->fail('订单没有查询到');
  314. }
  315. $weight = '0';
  316. if ($data['cart_ids']) {
  317. $cartIds = array_column($data['cart_ids'], 'cart_id');
  318. $cartList = app()->make(StoreOrderCartInfoServices::class)->getColumn([
  319. ['cart_id', 'in', $cartIds]
  320. ], 'cart_info', 'cart_id');
  321. foreach ($data['cart_ids'] as $cart) {
  322. if (!isset($cart['cart_id']) || !$cart['cart_id'] || !isset($cart['cart_num']) || !$cart['cart_num']) {
  323. return app('json')->fail('请重新选择发货商品或发货件数');
  324. }
  325. if (isset($cartList[$cart['cart_id']])) {
  326. $value = is_string($cartList[$cart['cart_id']]) ? json_decode($cartList[$cart['cart_id']], true) : $cartList[$cart['cart_id']];
  327. $weightnew = bcmul($value['attrInfo']['weight'], (string)$cart['cart_num'], 2);
  328. $weight = bcadd($weightnew, $weight, 2);
  329. }
  330. }
  331. } else {
  332. $orderCartInfoList = app()->make(StoreOrderCartInfoServices::class)->getCartInfoPrintProduct($data['orderId']);
  333. foreach ($orderCartInfoList as $item) {
  334. $weightnew = bcmul($item['attrInfo']['weight'], (string)$item['cart_num'], 2);
  335. $weight = bcadd($weightnew, $weight, 2);
  336. }
  337. }
  338. $data['address'] = $orderInfo['user_address'];
  339. if ($weight > 0) {
  340. $data['weight'] = $weight;
  341. }
  342. return app('json')->success($services->express()->getPrice($data));
  343. }
  344. /**
  345. * 获取订单可拆分发货商品列表
  346. * @param $id
  347. * @param StoreOrderCartInfoServices $services
  348. * @return mixed
  349. */
  350. public function split_cart_info($id, StoreOrderCartInfoServices $services)
  351. {
  352. if (!$id) {
  353. return app('json')->fail('参数错误');
  354. }
  355. return app('json')->success($services->getSplitCartList((int)$id));
  356. }
  357. /**
  358. * 获取订单拆分子订单列表
  359. * @param $id
  360. * @return mixed
  361. * @throws \think\db\exception\DataNotFoundException
  362. * @throws \think\db\exception\DbException
  363. * @throws \think\db\exception\ModelNotFoundException
  364. */
  365. public function split_order($id)
  366. {
  367. if (!$id) {
  368. return app('json')->fail('参数错误');
  369. }
  370. return app('json')->success($this->services->getSplitOrderList(['pid' => $id, 'is_system_del' => 0], ['*'], ['split', 'pink', 'invoice']));
  371. }
  372. /**
  373. * 确认收货
  374. * @param $id 订单id
  375. * @return mixed
  376. * @throws \Exception
  377. */
  378. public function take_delivery(StoreOrderTakeServices $services, $id)
  379. {
  380. if (!$id) return app('json')->fail('参数错误');
  381. $order = $this->services->get($id);
  382. if (!$order)
  383. return app('json')->fail('订单不存在');
  384. if ($order['status'] == 2)
  385. return app('json')->fail('不能重复收货');
  386. if ($order['paid'] == 1 && $order['status'] == 1)
  387. $data['status'] = 2;
  388. else if ($order['pay_type'] == 'offline')
  389. $data['status'] = 2;
  390. else
  391. return app('json')->fail('请先发货或者送货');
  392. if (!$this->services->update($id, $data)) {
  393. return app('json')->fail('收货失败,请稍候再试');
  394. } else {
  395. $services->storeProductOrderUserTakeDelivery($order);
  396. return app('json')->success('收货成功');
  397. }
  398. }
  399. /**
  400. * 获取配置信息
  401. * @return mixed
  402. */
  403. public function getDeliveryInfo()
  404. {
  405. return app('json')->success([
  406. 'express_temp_id' => sys_config('config_export_temp_id'),
  407. 'id' => sys_config('config_export_id'),
  408. 'to_name' => sys_config('config_export_to_name'),
  409. 'to_tel' => sys_config('config_export_to_tel'),
  410. 'to_add' => sys_config('config_export_to_address'),
  411. 'export_open' => (bool)((int)sys_config('config_export_open'))
  412. ]);
  413. }
  414. /**
  415. * 退款表单生成
  416. * @param $id 订单id
  417. * @return mixed
  418. * @throws \FormBuilder\Exception\FormBuilderException
  419. */
  420. public function refund(StoreOrderRefundServices $services, $id)
  421. {
  422. if (!$id) {
  423. return app('json')->fail('参数错误');
  424. }
  425. return app('json')->success($services->refundOrderForm((int)$id, 'order'));
  426. }
  427. /**
  428. * 订单退款
  429. * @param $id 订单id
  430. * @return mixed
  431. * @throws \think\db\exception\DataNotFoundException
  432. * @throws \think\db\exception\ModelNotFoundException
  433. * @throws \think\exception\DbException
  434. */
  435. public function update_refund(StoreOrderRefundServices $services, $id)
  436. {
  437. $data = $this->request->postMore([
  438. ['refund_price', 0],
  439. ['cart_ids', []]
  440. ]);
  441. if (!$id) {
  442. return app('json')->fail('参数错误');
  443. }
  444. $order = $this->services->get($id);
  445. if (!$order) {
  446. return app('json')->fail('订单不存在');
  447. }
  448. $refundData = [
  449. 'refund_reason' => '后台主动退款',
  450. 'refund_explain' => '后台主动退款',
  451. 'refund_img' => json_encode([]),
  452. ];
  453. $res = $services->applyRefund((int)$id, $order['uid'], $order, $data['cart_ids'], 1, (float)$data['refund_price'], $refundData);
  454. if (!$res) {
  455. return app('json')->fail('退款单生成失败');
  456. }
  457. $orderRefund = $services->getOrderOne(['store_order_id' => $id]);
  458. $data['refund_status'] = 2;
  459. $data['refund_type'] = 6;
  460. $data['refunded_time'] = time();
  461. //0元退款
  462. if ($orderRefund['refund_price'] == 0 && in_array($orderRefund['refund_type'], [1, 5])) {
  463. $refund_price = 0;
  464. } else {
  465. if (!$data['refund_price']) {
  466. return app('json')->fail('请输入退款金额');
  467. }
  468. if ($orderRefund['refund_price'] == $orderRefund['refunded_price']) {
  469. return app('json')->fail('已退完支付金额,不能再退款了');
  470. }
  471. $refund_price = $data['refund_price'];
  472. }
  473. $data['refunded_price'] = bcadd($data['refund_price'], $orderRefund['refunded_price'], 2);
  474. $bj = bccomp((string)$orderRefund['refund_price'], (string)$data['refunded_price'], 2);
  475. if ($bj < 0) {
  476. return app('json')->fail('退款金额大于支付金额,请修改退款金额');
  477. }
  478. $refund_data['pay_price'] = $order['pay_price'];
  479. $refund_data['refund_price'] = $refund_price;
  480. if ($order['refund_price'] > 0) {
  481. mt_srand();
  482. $refund_data['refund_id'] = $order['order_id'] . rand(100, 999);
  483. }
  484. ($order['pid'] > 0) ? $refund_data['order_id'] = $this->services->value(['id' => (int)$order['pid']], 'order_id') : $refund_data['order_id'] = $order['order_id'];
  485. /** @var WechatUserServices $wechatUserServices */
  486. $wechatUserServices = app()->make(WechatUserServices::class);
  487. $refund_data['open_id'] = $wechatUserServices->uidToOpenid((int)$order['uid'], 'routine') ?? '';
  488. $refund_data['refund_no'] = $orderRefund['order_id'];
  489. $refund_data['order_id'] = $orderRefund['order_id'];
  490. //修改订单退款状态
  491. unset($data['refund_price']);
  492. if ($services->agreeRefund($orderRefund['id'], $refund_data)) {
  493. $services->update($orderRefund['id'], $data);
  494. return app('json')->success('退款成功');
  495. } else {
  496. $services->storeProductOrderRefundYFasle((int)$orderRefund['id'], $refund_price);
  497. return app('json')->fail('退款失败');
  498. }
  499. }
  500. /**
  501. * 订单详情
  502. * @param $id 订单id
  503. * @return mixed
  504. * @throws \ReflectionException
  505. */
  506. public function order_info($id)
  507. {
  508. if (!$id || !($orderInfo = $this->services->get($id, [], ['refund', 'invoice']))) {
  509. return app('json')->fail('订单不存在');
  510. }
  511. /** @var UserServices $services */
  512. $services = app()->make(UserServices::class);
  513. $userInfo = $services->get($orderInfo['uid']);
  514. if (!$userInfo) return app('json')->fail('用户信息不存在');
  515. $userInfo = $userInfo->hidden(['pwd', 'add_ip', 'last_ip', 'login_type']);
  516. $userInfo['spread_name'] = '无';
  517. if ($userInfo['spread_uid']) {
  518. $spreadName = $services->value(['uid' => $userInfo['spread_uid']], 'nickname');
  519. if ($spreadName) {
  520. $userInfo['spread_name'] = $spreadName;
  521. } else {
  522. $userInfo['spread_uid'] = '';
  523. }
  524. } else {
  525. $userInfo['spread_uid'] = '';
  526. }
  527. $orderInfo = $this->services->tidyOrder($orderInfo->toArray(), true, true);
  528. //核算优惠金额
  529. $vipTruePrice = $levelPrice = $memberPrice = 0;
  530. foreach ($orderInfo['cartInfo'] as $cart) {
  531. $vipTruePrice = bcadd((string)$vipTruePrice, (string)$cart['vip_sum_truePrice'], 2);
  532. if ($cart['price_type'] == 'member') $memberPrice = bcadd((string)$memberPrice, (string)$cart['vip_sum_truePrice'], 2);
  533. if ($cart['price_type'] == 'level') $levelPrice = bcadd((string)$levelPrice, (string)$cart['vip_sum_truePrice'], 2);
  534. }
  535. $orderInfo['vip_true_price'] = $vipTruePrice;
  536. $orderInfo['levelPrice'] = $levelPrice;
  537. $orderInfo['memberPrice'] = $memberPrice;
  538. $orderInfo['total_price'] = bcadd($orderInfo['total_price'], $orderInfo['vip_true_price'], 2);
  539. if ($orderInfo['store_id'] && $orderInfo['shipping_type'] == 2) {
  540. /** @var $storeServices */
  541. $storeServices = app()->make(SystemStoreServices::class);
  542. $orderInfo['_store_name'] = $storeServices->value(['id' => $orderInfo['store_id']], 'name');
  543. } else
  544. $orderInfo['_store_name'] = '';
  545. $orderInfo['spread_name'] = $services->value(['uid' => $orderInfo['spread_uid']], 'nickname') ?? '无';
  546. $orderInfo['_info'] = app()->make(StoreOrderCartInfoServices::class)->getOrderCartInfo((int)$orderInfo['id']);
  547. $cart_num = 0;
  548. $refund_num = array_sum(array_column($orderInfo['refund'], 'refund_num'));
  549. foreach ($orderInfo['_info'] as $items) {
  550. $cart_num += $items['cart_info']['cart_num'];
  551. }
  552. $orderInfo['is_all_refund'] = $refund_num == $cart_num;
  553. $userInfo = $userInfo->toArray();
  554. return app('json')->success(compact('orderInfo', 'userInfo'));
  555. }
  556. /**
  557. * 查询物流信息
  558. * @param $id 订单id
  559. * @return mixed
  560. */
  561. public function get_express($id, ExpressServices $services)
  562. {
  563. if (!$id || !($orderInfo = $this->services->get($id)))
  564. return app('json')->fail('订单不存在');
  565. if ($orderInfo['delivery_type'] != 'express' || !$orderInfo['delivery_id'])
  566. return app('json')->fail('快递单号不存在');
  567. $cacheName = $orderInfo['order_id'] . $orderInfo['delivery_id'];
  568. $data['delivery_name'] = $orderInfo['delivery_name'];
  569. $data['delivery_id'] = $orderInfo['delivery_id'];
  570. $data['result'] = $services->query($cacheName, $orderInfo['delivery_id'], $orderInfo['delivery_code'] ?? null, $orderInfo['user_phone']);
  571. return app('json')->success($data);
  572. }
  573. /**
  574. * 获取修改配送信息表单结构
  575. * @param $id 订单id
  576. * @return mixed
  577. * @throws \FormBuilder\Exception\FormBuilderException
  578. */
  579. public function distribution(StoreOrderDeliveryServices $services, $id)
  580. {
  581. if (!$id) {
  582. return app('json')->fail('参数错误');
  583. }
  584. return app('json')->success($services->distributionForm((int)$id));
  585. }
  586. /**
  587. * 修改配送信息
  588. * @param $id 订单id
  589. * @return mixed
  590. */
  591. public function update_distribution(StoreOrderDeliveryServices $services, $id)
  592. {
  593. $data = $this->request->postMore([['delivery_name', ''], ['delivery_code', ''], ['delivery_id', '']]);
  594. if (!$id) return app('json')->fail('参数错误');
  595. $services->updateDistribution($id, $data);
  596. return app('json')->success('操作成功');
  597. }
  598. /**
  599. * 不退款表单结构
  600. * @param StoreOrderRefundServices $services
  601. * @param $id
  602. * @return mixed
  603. * @throws \FormBuilder\Exception\FormBuilderException
  604. */
  605. public function no_refund(StoreOrderRefundServices $services, $id)
  606. {
  607. if (!$id) return app('json')->fail('参数错误');
  608. return app('json')->success($services->noRefundForm((int)$id));
  609. }
  610. /**
  611. * 订单不退款
  612. * @param StoreOrderRefundServices $services
  613. * @param $id
  614. * @return mixed
  615. */
  616. public function update_un_refund(StoreOrderRefundServices $services, $id)
  617. {
  618. if (!$id || !($orderInfo = $this->services->get($id)))
  619. return app('json')->fail('订单不存在');
  620. [$refund_reason] = $this->request->postMore([['refund_reason', '']], true);
  621. if (!$refund_reason) {
  622. return app('json')->fail('拒绝理由不能为空');
  623. }
  624. $orderInfo->refund_reason = $refund_reason;
  625. $orderInfo->refund_status = 0;
  626. $orderInfo->refund_type = 3;
  627. $orderInfo->save();
  628. if ($orderInfo->pid > 0) {
  629. $res1 = $this->services->getCount([
  630. ['pid', '=', $orderInfo->pid],
  631. ['refund_type', '>', 0],
  632. ['refund_type', '<>', 3],
  633. ]);
  634. if ($res1 == 0) {
  635. $this->services->update($orderInfo->pid, ['refund_status' => 0]);
  636. }
  637. }
  638. $services->storeProductOrderRefundNo((int)$id, $refund_reason);
  639. //提醒推送
  640. event('NoticeListener', [['orderInfo' => $orderInfo], 'send_order_refund_no_status']);
  641. //自定义消息-订单拒绝退款
  642. $orderInfo['time'] = date('Y-m-d H:i:s');
  643. $orderInfo['phone'] = $orderInfo['user_phone'];
  644. event('CustomNoticeListener', [$orderInfo['uid'], $orderInfo, 'order_refund_fail']);
  645. return app('json')->success('操作成功');
  646. }
  647. /**
  648. * 线下支付
  649. * @param $id 订单id
  650. * @return mixed
  651. */
  652. public function pay_offline(OrderOfflineServices $services, $id)
  653. {
  654. if (!$id) return app('json')->fail('参数错误');
  655. $res = $services->orderOffline((int)$id);
  656. if ($res) {
  657. return app('json')->success('操作成功');
  658. } else {
  659. return app('json')->fail('操作失败');
  660. }
  661. }
  662. /**
  663. * 退积分表单获取
  664. * @param $id
  665. * @return mixed
  666. * @throws \FormBuilder\Exception\FormBuilderException
  667. */
  668. public function refund_integral(StoreOrderRefundServices $services, $id)
  669. {
  670. if (!$id)
  671. return app('json')->fail('参数错误');
  672. return app('json')->success($services->refundIntegralForm((int)$id));
  673. }
  674. /**
  675. * 退积分保存
  676. * @param $id
  677. * @return mixed
  678. */
  679. public function update_refund_integral(StoreOrderRefundServices $services, $id)
  680. {
  681. [$back_integral] = $this->request->postMore([['back_integral', 0]], true);
  682. if (!$id || !($orderInfo = $this->services->get($id))) {
  683. return app('json')->fail('订单不存在');
  684. }
  685. if ($orderInfo->is_del) {
  686. return app('json')->fail('订单已删除无法退积分');
  687. }
  688. if ($back_integral <= 0) {
  689. return app('json')->fail('请输入积分');
  690. }
  691. if ($orderInfo['use_integral'] == $orderInfo['back_integral']) {
  692. return app('json')->fail('已退完积分');
  693. }
  694. $data['back_integral'] = bcadd((string)$back_integral, (string)$orderInfo['back_integral'], 2);
  695. $bj = bccomp((string)$orderInfo['use_integral'], (string)$data['back_integral'], 2);
  696. if ($bj < 0) {
  697. return app('json')->fail('退积分大于支付积分,请修改退积分');
  698. }
  699. //积分退款处理
  700. $orderInfo->back_integral = $data['back_integral'];
  701. if ($services->refundIntegral($orderInfo, $back_integral)) {
  702. return app('json')->success('退积分成功');
  703. } else {
  704. return app('json')->fail('退积分失败');
  705. }
  706. }
  707. /**
  708. * 修改备注
  709. * @param $id
  710. * @return mixed
  711. */
  712. public function remark($id)
  713. {
  714. $data = $this->request->postMore([['remark', '']]);
  715. if (!$data['remark'])
  716. return app('json')->fail('备注不能为空');
  717. if (!$id)
  718. return app('json')->fail('参数错误');
  719. if (!$order = $this->services->get($id)) {
  720. return app('json')->fail('订单不存在');
  721. }
  722. $order->remark = $data['remark'];
  723. if ($order->save()) {
  724. return app('json')->success('备注成功');
  725. } else
  726. return app('json')->fail('备注失败');
  727. }
  728. /**
  729. * 获取订单状态列表并分页
  730. * @param $id
  731. * @return mixed
  732. */
  733. public function status(StoreOrderStatusServices $services, $id)
  734. {
  735. if (!$id) return app('json')->fail('参数错误');
  736. return app('json')->success($services->getStatusList(['oid' => $id])['list']);
  737. }
  738. /**
  739. * 小票打印机打印
  740. * @param $id
  741. * @return mixed
  742. * @throws \think\db\exception\DataNotFoundException
  743. * @throws \think\db\exception\DbException
  744. * @throws \think\db\exception\ModelNotFoundException
  745. */
  746. public function order_print($id)
  747. {
  748. if (!$id) return app('json')->fail('参数错误');
  749. $res = $this->services->orderPrintTicket($id, true);
  750. if ($res) {
  751. return app('json')->success('操作成功');
  752. } else {
  753. return app('json')->fail('操作失败');
  754. }
  755. }
  756. /**
  757. * 电子面单模板
  758. * @param $com
  759. * @return mixed
  760. */
  761. public function expr_temp(ServeServices $services, $com)
  762. {
  763. if (!$com) {
  764. return app('json')->fail('快递公司编号缺失');
  765. }
  766. $list = $services->express()->temp($com);
  767. return app('json')->success($list);
  768. }
  769. /**
  770. * 获取模板
  771. */
  772. public function express_temp(ServeServices $services)
  773. {
  774. $data = $this->request->getMore([['com', '']]);
  775. if (!$data['com']) {
  776. return app('json')->fail('快递公司编号缺失');
  777. }
  778. $tpd = $services->express()->temp($data['com']);
  779. return app('json')->success($tpd['data']);
  780. }
  781. /**
  782. * 订单发货后打印电子面单
  783. * @param $orderId
  784. * @param StoreOrderDeliveryServices $storeOrderDeliveryServices
  785. * @return mixed
  786. */
  787. public function order_dump($order_id, StoreOrderDeliveryServices $storeOrderDeliveryServices)
  788. {
  789. $storeOrderDeliveryServices->orderDump($order_id);
  790. return app('json')->success('打印成功');
  791. }
  792. /**
  793. * 获取快递信息
  794. * @param ServeServices $services
  795. * @return \think\Response
  796. * @author 等风来
  797. * @email 136327134@qq.com
  798. * @date 2023/5/15
  799. */
  800. public function getKuaidiComs(ServeServices $services)
  801. {
  802. MiniOrderJob::dispatch('syncOrderShipping');
  803. return app('json')->success($services->express()->getKuaidiComs());
  804. }
  805. /**
  806. * 取消商家寄件
  807. * @param $id
  808. * @return \think\Response
  809. * @author 等风来
  810. * @email 136327134@qq.com
  811. * @date 2023/5/15
  812. */
  813. public function shipmentCancelOrder($id)
  814. {
  815. if (!$id) {
  816. return app('json')->fail('缺少参数');
  817. }
  818. $msg = $this->request->post('msg', '');
  819. if (!$msg) {
  820. return app('json')->fail('请填写取消寄件原因');
  821. }
  822. if ($this->services->shipmentCancelOrder((int)$id, $msg)) {
  823. return app('json')->success('取消成功');
  824. } else {
  825. return app('json')->fail('取消失败');
  826. }
  827. }
  828. /**
  829. * 导入批量发货
  830. * @return \think\Response|void
  831. * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
  832. */
  833. public function importExpress()
  834. {
  835. [$file] = $this->request->getMore([
  836. ['file', '']
  837. ], true);
  838. if (!$file) return app('json')->fail('请上传文件');
  839. $file = public_path() . substr($file, 1);
  840. // 获取文件后缀
  841. $suffix = strtolower(pathinfo($file, PATHINFO_EXTENSION));
  842. if (!in_array($suffix, ['xls', 'xlsx'])) {
  843. return app('json')->fail('文件格式不正确,请上传xls或xlsx格式的文件!');
  844. }
  845. $expressData = app()->make(FileService::class)->readExcel($file, 'express', 2, ucfirst($suffix));
  846. foreach ($expressData as $item) {
  847. OrderExpressJob::dispatch([$item]);
  848. }
  849. return app('json')->success('批量发货成功');
  850. }
  851. /**
  852. * 配货单
  853. * @param $order_id
  854. * @return \think\Response
  855. * @throws \think\db\exception\DataNotFoundException
  856. * @throws \think\db\exception\DbException
  857. * @throws \think\db\exception\ModelNotFoundException
  858. * @author: 吴汐
  859. * @email: 442384644@qq.com
  860. * @date: 2023/10/11
  861. */
  862. public function printShipping($order_id)
  863. {
  864. if (!$order_id) {
  865. return app('json')->fail('参数错误');
  866. }
  867. $data = $this->services->printShippingData($order_id);
  868. return app('json')->success($data);
  869. }
  870. /**
  871. * 修改收货地址
  872. * @param $id
  873. * @return \think\Response
  874. * @throws \think\db\exception\DataNotFoundException
  875. * @throws \think\db\exception\DbException
  876. * @throws \think\db\exception\ModelNotFoundException
  877. * @author wuhaotian
  878. * @email 442384644@qq.com
  879. * @date 2025/9/8
  880. */
  881. public function editAddress($id)
  882. {
  883. if (!$id) return app('json')->fail('参数错误');
  884. $data = $this->request->postMore([
  885. ['real_name', ''],
  886. ['user_phone', ''],
  887. ['user_address', '']
  888. ]);
  889. if (!$data['real_name']) return app('json')->fail('请填写收货人姓名');
  890. if (!$data['user_phone']) return app('json')->fail('请填写收货人电话');
  891. if (!$data['user_address']) return app('json')->fail('请填写收货人地址');
  892. $this->services->editAddress($id, $data);
  893. return app('json')->success('修改成功');
  894. }
  895. }