StoreOrder.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\OrderExpressJob;
  15. use app\services\serve\ServeServices;
  16. use app\services\wechat\WechatUserServices;
  17. use crmeb\services\FileService;
  18. use app\services\order\{StoreOrderCartInfoServices,
  19. StoreOrderDeliveryServices,
  20. StoreOrderRefundServices,
  21. StoreOrderStatusServices,
  22. StoreOrderTakeServices,
  23. StoreOrderWriteOffServices,
  24. StoreOrderServices
  25. };
  26. use app\services\pay\OrderOfflineServices;
  27. use app\services\shipping\ExpressServices;
  28. use app\services\system\store\SystemStoreServices;
  29. use app\services\user\UserServices;
  30. use think\facade\App;
  31. /**
  32. * 订单管理
  33. * Class StoreOrder
  34. * @package app\adminapi\controller\v1\order
  35. */
  36. class StoreOrder extends AuthController
  37. {
  38. /**
  39. * StoreOrder constructor.
  40. * @param App $app
  41. * @param StoreOrderServices $service
  42. * @method temp
  43. */
  44. public function __construct(App $app, StoreOrderServices $service)
  45. {
  46. parent::__construct($app);
  47. $this->services = $service;
  48. }
  49. /**
  50. * 获取订单类型数量
  51. * @return mixed
  52. */
  53. public function chart()
  54. {
  55. $where = $this->request->getMore([
  56. ['data', '', '', 'time'],
  57. ['type', ''],
  58. ['pay_type', ''],
  59. ['field_key', 'all'],
  60. ['real_name', ''],
  61. ]);
  62. $data = $this->services->orderCount($where);
  63. return app('json')->success($data);
  64. }
  65. /**
  66. * 订单列表
  67. * @return mixed
  68. * @throws \think\db\exception\DataNotFoundException
  69. * @throws \think\db\exception\DbException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. */
  72. public function lst()
  73. {
  74. $where = $this->request->getMore([
  75. ['status', ''],
  76. ['real_name', ''],
  77. ['is_del', ''],
  78. ['data', '', '', 'time'],
  79. ['type', ''],
  80. ['pay_type', ''],
  81. ['order', ''],
  82. ['field_key', ''],
  83. ]);
  84. $where['is_system_del'] = 0;
  85. $where['pid'] = 0;
  86. if ($where['status'] == 1) $where = $where + ['shipping_type' => 1];
  87. return app('json')->success($this->services->getOrderList($where, ['*'], ['split' => function ($query) {
  88. $query->field('id,pid');
  89. }, 'pink', 'invoice', 'division']));
  90. }
  91. /**
  92. * 核销码核销
  93. * @param StoreOrderWriteOffServices $services
  94. * @return mixed
  95. * @throws \think\db\exception\DataNotFoundException
  96. * @throws \think\db\exception\DbException
  97. * @throws \think\db\exception\ModelNotFoundException
  98. */
  99. public function write_order(StoreOrderWriteOffServices $services)
  100. {
  101. [$code, $confirm] = $this->request->getMore([
  102. ['code', ''],
  103. ['confirm', 0]
  104. ], true);
  105. if (!$code) return app('json')->fail(100100);
  106. $orderInfo = $services->writeOffOrder($code, (int)$confirm);
  107. if ($confirm == 0) {
  108. return app('json')->success(400151, $orderInfo);
  109. }
  110. return app('json')->success(400152);
  111. }
  112. /**
  113. * 订单号核销
  114. * @param StoreOrderWriteOffServices $services
  115. * @param $order_id
  116. * @return mixed
  117. * @throws \think\db\exception\DataNotFoundException
  118. * @throws \think\db\exception\DbException
  119. * @throws \think\db\exception\ModelNotFoundException
  120. */
  121. public function write_update(StoreOrderWriteOffServices $services, $order_id)
  122. {
  123. $orderInfo = $this->services->getOne(['order_id' => $order_id, 'is_del' => 0]);
  124. if ($orderInfo->shipping_type != 2 && $orderInfo->delivery_type != 'send') {
  125. return app('json')->fail(400153);
  126. } else {
  127. if (!$orderInfo->verify_code) {
  128. return app('json')->fail(100100);
  129. }
  130. $orderInfo = $services->writeOffOrder($orderInfo->verify_code, 1);
  131. if ($orderInfo) {
  132. return app('json')->success(400151);
  133. } else {
  134. return app('json')->fail(400154);
  135. }
  136. }
  137. }
  138. /**
  139. * 订单改价表单
  140. * @param $id
  141. * @return mixed
  142. * @throws \FormBuilder\Exception\FormBuilderException
  143. */
  144. public function edit($id)
  145. {
  146. if (!$id) return app('json')->fail(100100);
  147. return app('json')->success($this->services->updateForm($id));
  148. }
  149. /**
  150. * 订单改价
  151. * @param $id
  152. * @return mixed
  153. * @throws \Exception
  154. */
  155. public function update($id)
  156. {
  157. if (!$id) return app('json')->fail(100100);
  158. $data = $this->request->postMore([
  159. ['order_id', ''],
  160. ['total_price', 0],
  161. ['total_postage', 0],
  162. ['pay_price', 0],
  163. ['pay_postage', 0],
  164. ['gain_integral', 0],
  165. ]);
  166. $this->validate($data, StoreOrderValidate::class);
  167. if ($data['total_price'] < 0) return app('json')->fail(400155);
  168. if ($data['pay_price'] < 0) return app('json')->fail(400155);
  169. $this->services->updateOrder((int)$id, $data);
  170. return app('json')->success(100001);
  171. }
  172. /**
  173. * 获取快递公司
  174. * @return mixed
  175. */
  176. public function express(ExpressServices $services)
  177. {
  178. [$status] = $this->request->getMore([
  179. ['status', ''],
  180. ], true);
  181. if ($status != '') $data['status'] = $status;
  182. if ($status == 'undefined') $data['status'] = 1;
  183. $data['is_show'] = 1;
  184. return app('json')->success($services->express($data));
  185. }
  186. /**
  187. * 批量删除用户已经删除的订单
  188. * @return mixed
  189. */
  190. public function del_orders()
  191. {
  192. [$ids] = $this->request->postMore([
  193. ['ids', []],
  194. ], true);
  195. if (!count($ids)) return app('json')->fail(400156);
  196. if ($this->services->getOrderIdsCount($ids))
  197. return app('json')->fail(400157);
  198. if ($this->services->batchUpdate($ids, ['is_system_del' => 1]))
  199. return app('json')->success(100002);
  200. else
  201. return app('json')->fail(100008);
  202. }
  203. /**
  204. * 删除订单
  205. * @param $id
  206. * @return mixed
  207. */
  208. public function del($id)
  209. {
  210. if (!$id || !($orderInfo = $this->services->get($id)))
  211. return app('json')->fail(400118);
  212. if (!$orderInfo->is_del)
  213. return app('json')->fail(400157);
  214. $orderInfo->is_system_del = 1;
  215. if ($orderInfo->save()) {
  216. /** @var StoreOrderRefundServices $refundServices */
  217. $refundServices = app()->make(StoreOrderRefundServices::class);
  218. $refundServices->update(['store_order_id' => $id], ['is_system_del' => 1]);
  219. return app('json')->success(100002);
  220. } else
  221. return app('json')->fail(100008);
  222. }
  223. /**
  224. * 订单发送货
  225. * @param $id
  226. * @param StoreOrderDeliveryServices $services
  227. * @return mixed
  228. */
  229. public function update_delivery($id, StoreOrderDeliveryServices $services)
  230. {
  231. $data = $this->request->postMore([
  232. ['type', 1],
  233. ['delivery_name', ''],//快递公司名称
  234. ['delivery_id', ''],//快递单号
  235. ['delivery_code', ''],//快递公司编码
  236. ['express_record_type', 2],//发货记录类型:2=电子面单;3=商家寄件
  237. ['express_temp_id', ""],//电子面单模板
  238. ['to_name', ''],//寄件人姓名
  239. ['to_tel', ''],//寄件人电话
  240. ['to_addr', ''],//寄件人地址
  241. ['sh_delivery_name', ''],//送货人姓名
  242. ['sh_delivery_id', ''],//送货人电话
  243. ['sh_delivery_uid', ''],//送货人ID
  244. ['fictitious_content', ''],//虚拟发货内容
  245. ['day_type', 0], //顺丰传 0今天,1明天,2后台
  246. ['pickup_time', []],//开始时间 9:00,结束时间 10:00 开始时间和结束时间之间不能小于一个小时
  247. ]);
  248. return app('json')->success(100010, $services->delivery((int)$id, $data));
  249. }
  250. /**
  251. * 订单拆单发送货
  252. * @param $id
  253. * @param StoreOrderDeliveryServices $services
  254. * @return mixed
  255. * @throws \think\db\exception\DataNotFoundException
  256. * @throws \think\db\exception\DbException
  257. * @throws \think\db\exception\ModelNotFoundException
  258. */
  259. public function split_delivery($id, StoreOrderDeliveryServices $services)
  260. {
  261. $data = $this->request->postMore([
  262. ['type', 1],
  263. ['delivery_name', ''],//快递公司名称
  264. ['delivery_id', ''],//快递单号
  265. ['delivery_code', ''],//快递公司编码
  266. ['express_record_type', 2],//发货记录类型
  267. ['express_temp_id', ""],//电子面单模板
  268. ['to_name', ''],//寄件人姓名
  269. ['to_tel', ''],//寄件人电话
  270. ['to_addr', ''],//寄件人地址
  271. ['sh_delivery_name', ''],//送货人姓名
  272. ['sh_delivery_id', ''],//送货人电话
  273. ['sh_delivery_uid', ''],//送货人ID
  274. ['fictitious_content', ''],//虚拟发货内容
  275. ['cart_ids', []],
  276. ['day_type', 0], //顺丰传 0今天,1明天,2后台
  277. ['pickup_time', []],//开始时间 9:00,结束时间 10:00 开始时间和结束时间之间不能小于一个小时
  278. ['service_type', ''],//快递业务类型
  279. ]);
  280. if (!$id) {
  281. return app('json')->fail(100100);
  282. }
  283. if (!$data['cart_ids']) {
  284. return app('json')->fail(400158);
  285. }
  286. foreach ($data['cart_ids'] as $cart) {
  287. if (!isset($cart['cart_id']) || !$cart['cart_id'] || !isset($cart['cart_num']) || !$cart['cart_num']) {
  288. return app('json')->fail(400159);
  289. }
  290. }
  291. $services->splitDelivery((int)$id, $data);
  292. return app('json')->success(100010);
  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(400159);
  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(100100);
  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(100100);
  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(100100);
  381. $order = $this->services->get($id);
  382. if (!$order)
  383. return app('json')->fail(400118);
  384. if ($order['status'] == 2)
  385. return app('json')->fail(400114);
  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(400115);
  392. if (!$this->services->update($id, $data)) {
  393. return app('json')->fail(400116);
  394. } else {
  395. $services->storeProductOrderUserTakeDelivery($order);
  396. return app('json')->success(400117);
  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(100100);
  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(100100);
  443. }
  444. $order = $this->services->get($id);
  445. if (!$order) {
  446. return app('json')->fail(400118);
  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->get(['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(400146);
  467. }
  468. if ($orderRefund['refund_price'] == $orderRefund['refunded_price']) {
  469. return app('json')->fail(400147);
  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(400148);
  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(400149);
  495. } else {
  496. $services->storeProductOrderRefundYFasle((int)$orderRefund['id'], $refund_price);
  497. return app('json')->fail(400150);
  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(400118);
  510. }
  511. /** @var UserServices $services */
  512. $services = app()->make(UserServices::class);
  513. $userInfo = $services->get($orderInfo['uid']);
  514. if (!$userInfo) return app('json')->fail(400119);
  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(400118);
  565. if ($orderInfo['delivery_type'] != 'express' || !$orderInfo['delivery_id'])
  566. return app('json')->fail(400120);
  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(100100);
  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(100100);
  595. $services->updateDistribution($id, $data);
  596. return app('json')->success(100010);
  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(100100);
  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(400118);
  620. [$refund_reason] = $this->request->postMore([['refund_reason', '']], true);
  621. if (!$refund_reason) {
  622. return app('json')->fail(400113);
  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(100010);
  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(100100);
  655. $res = $services->orderOffline((int)$id);
  656. if ($res) {
  657. return app('json')->success(100010);
  658. } else {
  659. return app('json')->fail(100005);
  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(100100);
  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(400118);
  684. }
  685. if ($orderInfo->is_del) {
  686. return app('json')->fail(400160);
  687. }
  688. if ($back_integral <= 0) {
  689. return app('json')->fail(400161);
  690. }
  691. if ($orderInfo['use_integral'] == $orderInfo['back_integral']) {
  692. return app('json')->fail(400162);
  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(400163);
  698. }
  699. //积分退款处理
  700. $orderInfo->back_integral = $data['back_integral'];
  701. if ($services->refundIntegral($orderInfo, $back_integral)) {
  702. return app('json')->success(400164);
  703. } else {
  704. return app('json')->fail(400165);
  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(400106);
  717. if (!$id)
  718. return app('json')->fail(100100);
  719. if (!$order = $this->services->get($id)) {
  720. return app('json')->fail(400118);
  721. }
  722. $order->remark = $data['remark'];
  723. if ($order->save()) {
  724. return app('json')->success(100024);
  725. } else
  726. return app('json')->fail(100025);
  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(100100);
  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(100100);
  749. $res = $this->services->orderPrintTicket($id, true);
  750. if ($res) {
  751. return app('json')->success(100010);
  752. } else {
  753. return app('json')->fail(100005);
  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(400123);
  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(400123);
  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(400121);
  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. return app('json')->success($services->express()->getKuaidiComs());
  803. }
  804. /**
  805. * 取消商家寄件
  806. * @param $id
  807. * @return \think\Response
  808. * @author 等风来
  809. * @email 136327134@qq.com
  810. * @date 2023/5/15
  811. */
  812. public function shipmentCancelOrder($id)
  813. {
  814. if (!$id) {
  815. return app('json')->fail('缺少参数');
  816. }
  817. $msg = $this->request->post('msg', '');
  818. if (!$msg) {
  819. return app('json')->fail('请填写取消寄件原因');
  820. }
  821. if ($this->services->shipmentCancelOrder((int)$id, $msg)) {
  822. return app('json')->success('取消成功');
  823. } else {
  824. return app('json')->fail('取消失败');
  825. }
  826. }
  827. /**
  828. * 导入批量发货
  829. * @return \think\Response|void
  830. * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
  831. */
  832. public function importExpress()
  833. {
  834. [$file] = $this->request->getMore([
  835. ['file', '']
  836. ], true);
  837. if (!$file) return app('json')->fail(400168);
  838. $file = public_path() . substr($file, 1);
  839. $expressData = app()->make(FileService::class)->readExcel($file, 'express', 2);
  840. foreach ($expressData as $item) {
  841. OrderExpressJob::dispatch([$item]);
  842. }
  843. return app('json')->success('批量发货成功');
  844. }
  845. /**
  846. * 配货单
  847. * @param $order_id
  848. * @return \think\Response
  849. * @throws \think\db\exception\DataNotFoundException
  850. * @throws \think\db\exception\DbException
  851. * @throws \think\db\exception\ModelNotFoundException
  852. * @author: 吴汐
  853. * @email: 442384644@qq.com
  854. * @date: 2023/10/11
  855. */
  856. public function printShipping($order_id)
  857. {
  858. if (!$order_id) {
  859. return app('json')->fail('参数错误');
  860. }
  861. $data = $this->services->printShippingData($order_id);
  862. return app('json')->success($data);
  863. }
  864. }