StoreOrder.php 29 KB

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