StoreOrder.php 29 KB

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