StoreOrderController.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\api\store;
  12. use app\Request;
  13. use app\services\activity\coupon\StoreCouponIssueServices;
  14. use app\services\activity\combination\StorePinkServices;
  15. use app\services\activity\promotions\StorePromotionsServices;
  16. use app\services\order\StoreOrderPromotionsServices;
  17. use app\services\order\StoreOrderCartInfoServices;
  18. use app\services\order\StoreOrderDeliveryServices;
  19. use app\services\order\StoreOrderRefundServices;
  20. use app\services\order\StoreOrderServices;
  21. use app\services\order\store\BranchOrderServices;
  22. use app\services\order\store\WriteOffOrderServices;
  23. use app\services\pay\OrderOfflineServices;
  24. use app\services\serve\ServeServices;
  25. use app\services\store\SystemStoreStaffServices;
  26. use app\services\user\UserRechargeServices;
  27. use app\services\user\UserServices;
  28. use app\services\other\ExpressServices;
  29. use crmeb\services\SystemConfigService;
  30. /**
  31. * 门店订单类
  32. * Class StoreOrderController
  33. * @package app\controller\api\store
  34. */
  35. class StoreOrderController
  36. {
  37. /**
  38. * @var BranchOrderServices
  39. */
  40. protected $services;
  41. /**
  42. * @var int
  43. */
  44. protected $uid;
  45. /**
  46. * 门店店员信息
  47. * @var array
  48. */
  49. protected $staffInfo;
  50. /**
  51. * 门店id
  52. * @var int|mixed
  53. */
  54. protected $store_id;
  55. /**
  56. * 门店店员ID
  57. * @var int|mixed
  58. */
  59. protected $staff_id;
  60. /**
  61. * StoreOrderController constructor.
  62. * @param BranchOrderServices $services
  63. */
  64. public function __construct(BranchOrderServices $services, Request $request)
  65. {
  66. $this->services = $services;
  67. $this->uid = (int)$request->uid();
  68. }
  69. protected function getStaffInfo()
  70. {
  71. /** @var SystemStoreStaffServices $staffServices */
  72. $staffServices = app()->make(SystemStoreStaffServices::class);
  73. $this->staffInfo = $staffServices->getStaffInfoByUid($this->uid)->toArray();
  74. $this->store_id = (int)$this->staffInfo['store_id'] ?? 0;
  75. $this->staff_id = (int)$this->staffInfo['id'] ?? 0;
  76. }
  77. /**
  78. * 订单数据统计
  79. * @param Request $request
  80. * @return mixed
  81. */
  82. public function statistics()
  83. {
  84. $this->getStaffInfo();
  85. $store_id = $this->store_id;
  86. $data = $this->services->getOrderData($store_id);
  87. return app('json')->successful($data);
  88. }
  89. /**
  90. * 订单每月统计数据
  91. * @param Request $request
  92. * @return mixed
  93. */
  94. public function data(Request $request)
  95. {
  96. [$is_manager, $time, $type] = $request->getMore([
  97. ['is_manager', 0],
  98. ['data', '', '', 'time'],
  99. ['type', 4],
  100. ], true);
  101. $this->getStaffInfo();
  102. if (!$is_manager || !$this->staffInfo['is_manager']) {
  103. $is_manager = 0;
  104. }
  105. $store_id = $this->store_id;
  106. $staff_id = $is_manager ? 0 : $this->staff_id;
  107. $where = ['pid' => 0, 'store_id' => $store_id, 'paid' => 1, 'refund_status' => [0, 3], 'is_del' => 0, 'is_system_del' => 0, 'time' => $time];
  108. if ($staff_id) {
  109. $where['staff_id'] = $staff_id;
  110. }
  111. $data = [];
  112. switch ($type) {
  113. case 1://配送
  114. $data = $this->services->getOrderDataPriceCount($where + ['type' => 7]);
  115. break;
  116. case 2://收银
  117. $data = $this->services->getOrderDataPriceCount($where + ['type' => 6]);
  118. break;
  119. case 3://核销
  120. $data = $this->services->getOrderDataPriceCount($where + ['type' => 5]);
  121. break;
  122. case 4://充值
  123. /** @var UserRechargeServices $userRechargeServices */
  124. $userRechargeServices = app()->make(UserRechargeServices::class);
  125. $data = $userRechargeServices->getDataPriceCount($store_id, $staff_id, $time);
  126. break;
  127. default:
  128. return app('json')->fail('没有此类型');
  129. }
  130. return app('json')->success($data);
  131. }
  132. /**
  133. * 订单列表
  134. * @param Request $request
  135. * @return mixed
  136. */
  137. public function lst(Request $request)
  138. {
  139. $where = $request->getMore([
  140. ['is_manager', 0],
  141. ['status', ''],
  142. ['is_del', 0],
  143. ['data', '', '', 'time'],
  144. ['type', ''],
  145. ['field_key', ''],
  146. ['field_value', ''],
  147. ['keyword', '', '', 'real_name']
  148. ]);
  149. $this->getStaffInfo();
  150. $is_manager = $where['is_manager'];
  151. unset($where['is_manager']);
  152. $where['store_id'] = $this->store_id;
  153. if (!$is_manager && !$this->staffInfo['is_manager'] && !$this->staffInfo['order_status']) {
  154. $where['staff_id'] = $this->staff_id;
  155. }
  156. $where['is_system_del'] = 0;
  157. if (!in_array($where['status'], [-1, -2, -3])) {
  158. $where['pid'] = 0;
  159. }
  160. return app('json')->successful($this->services->getStoreOrderList($where, ['*'], ['pink']));
  161. }
  162. /**
  163. * 订单详情
  164. * @param Request $request
  165. * @param StoreOrderServices $services
  166. * @param UserServices $userServices
  167. * @param $id
  168. * @return mixed
  169. */
  170. public function detail(Request $request, StoreOrderServices $services, UserServices $userServices, StoreOrderPromotionsServices $storeOrderPromotiosServices, $id)
  171. {
  172. $order = $this->services->getOne(['id' => $id], '*', ['store', 'refund' => function ($query) {
  173. $query->field('id,store_order_id,refund_num,refunded_price,refund_explain');
  174. }]);
  175. if (!$order) return app('json')->fail('订单不存在');
  176. $order = $order->toArray();
  177. $order['split'] = null;
  178. $order['refunded_price'] = $order['refund'][0]['refunded_price'] ?? 0;
  179. $order['refund_explain'] = $order['refund'][0]['refund_explain'] ?? '';
  180. $orderInfo = $services->tidyOrder($order, true);
  181. //核算优惠金额
  182. $vipTruePrice = 0;
  183. foreach ($orderInfo['cartInfo'] ?? [] as $key => $cart) {
  184. $vipTruePrice = bcadd((string)$vipTruePrice, (string)$cart['vip_sum_truePrice'], 2);
  185. }
  186. $orderInfo['total_price'] = floatval(bcsub((string)$orderInfo['total_price'], (string)$vipTruePrice, 2));
  187. //优惠活动优惠详情
  188. $orderInfo['promotions_detail'] = $storeOrderPromotiosServices->getOrderPromotionsDetail((int)$order['id']);
  189. if ($orderInfo['give_coupon']) {
  190. $couponIds = is_string($orderInfo['give_coupon']) ? explode(',', $orderInfo['give_coupon']) : $orderInfo['give_coupon'];
  191. /** @var StoreCouponIssueServices $couponIssueService */
  192. $couponIssueService = app()->make(StoreCouponIssueServices::class);
  193. $orderInfo['give_coupon'] = $couponIssueService->getColumn([['id', 'IN', $couponIds]], 'id,coupon_title');
  194. }
  195. $orderInfo['cartInfo'] = array_merge($orderInfo['cartInfo']);
  196. $orderInfo['vip_true_price'] = $vipTruePrice;
  197. $orderInfo['pinkStatus'] = null;
  198. if ($orderInfo['type'] == 3) {
  199. /** @var StorePinkServices $pinkService */
  200. $pinkService = app()->make(StorePinkServices::class);
  201. $orderInfo['pinkStatus'] = $pinkService->value(['order_id' => $orderInfo['order_id']], 'status');
  202. }
  203. $nickname = $userServices->value(['uid' => $order['uid']], 'nickname');
  204. $orderInfo['nickname'] = $nickname;
  205. return app('json')->successful('ok', $orderInfo);
  206. }
  207. /**
  208. * 订单发货获取订单信息
  209. * @param Request $request
  210. * @param $orderId
  211. * @return mixed
  212. * @throws \think\Exception
  213. * @throws \think\db\exception\DataNotFoundException
  214. * @throws \think\db\exception\ModelNotFoundException
  215. * @throws \think\exception\DbException
  216. */
  217. public function deliveryInfo(UserServices $userServices, $orderId)
  218. {
  219. $order = $this->services->getOne(['order_id' => $orderId], 'store_id,real_name,user_phone,user_address,order_id,uid,status,paid,id');
  220. if (!$order) return app('json')->fail('订单不存在');
  221. $store_id = (int)$order['store_id'];
  222. if ($order['paid']) {
  223. $order['nickname'] = $userServices->value(['uid' => $order['uid']], 'nickname');
  224. $order = $order->hidden(['uid', 'status', 'paid'])->toArray();
  225. }
  226. $data = SystemConfigService::more(['city_delivery_status', 'self_delivery_status', 'dada_delivery_status', 'uu_delivery_status']);
  227. $order['config_export_open'] = store_config($store_id, 'store_config_export_open');
  228. $result = [
  229. 'express_temp_id' => store_config($store_id, 'store_config_export_temp_id'),
  230. 'to_name' => store_config($store_id, 'store_config_export_to_name'),
  231. 'id' => store_config($store_id, 'store_config_export_id'),
  232. 'to_tel' => store_config($store_id, 'store_config_export_to_tel'),
  233. 'to_add' => store_config($store_id, 'store_config_export_to_address'),
  234. 'city_delivery_status' => $data['city_delivery_status'] && ($data['self_delivery_status'] || $data['dada_delivery_status'] || $data['uu_delivery_status']),
  235. 'self_delivery_status' => $data['city_delivery_status'] && $data['self_delivery_status'],
  236. 'dada_delivery_status' => $data['city_delivery_status'] && $data['dada_delivery_status'],
  237. 'uu_delivery_status' => $data['city_delivery_status'] && $data['uu_delivery_status'],
  238. ];
  239. $result = array_merge($result, $order);
  240. return app('json')->successful('ok', $result);
  241. }
  242. /**
  243. * 获取面单信息
  244. * @param ServeServices $services
  245. * @return mixed
  246. */
  247. public function getExportTemp(Request $request, ServeServices $services)
  248. {
  249. [$com] = $request->getMore([
  250. ['com', ''],
  251. ], true);
  252. return app('json')->success($services->express()->temp($com));
  253. }
  254. /**
  255. * 物流公司
  256. * @param ExpressServices $services
  257. * @return mixed
  258. */
  259. public function getExportAll(ExpressServices $services)
  260. {
  261. return app('json')->success($services->expressList());
  262. }
  263. /**
  264. * 订单发货
  265. * @param Request $request
  266. * @return mixed
  267. * @throws \think\db\exception\DataNotFoundException
  268. * @throws \think\db\exception\ModelNotFoundException
  269. * @throws \think\exception\DbException
  270. */
  271. public function delivery(Request $request, StoreOrderDeliveryServices $services, $id)
  272. {
  273. $data = $request->postMore([
  274. ['type', 1],
  275. ['delivery_name', ''],//快递公司名称
  276. ['delivery_id', ''],//快递单号
  277. ['delivery_code', ''],//快递公司编码
  278. ['express_record_type', 2],//发货记录类型
  279. ['express_temp_id', ""],//电子面单模板
  280. ['to_name', ''],//寄件人姓名
  281. ['to_tel', ''],//寄件人电话
  282. ['to_addr', ''],//寄件人地址
  283. ['sh_delivery_name', ''],//送货人姓名
  284. ['sh_delivery_id', ''],//送货人电话
  285. ['sh_delivery_uid', ''],//送货人ID
  286. ['delivery_type', 1],//送货类型
  287. ['station_type', 1],//送货类型
  288. ['cargo_weight', 0],//重量
  289. ['mark', ''],//备注
  290. ['remark', ''],//配送备注
  291. ['fictitious_content', '']//虚拟发货内容
  292. ]);
  293. $this->getStaffInfo();
  294. return app('json')->successful('发货成功!', $services->delivery((int)$id, $data, (int)$this->staff_id));
  295. }
  296. /**
  297. * 订单拆单发送货
  298. * @param $id 订单id
  299. * @return mixed
  300. */
  301. public function split_delivery(StoreOrderDeliveryServices $services, Request $request, $id)
  302. {
  303. $data = $request->postMore([
  304. ['type', 1],
  305. ['delivery_name', ''],//快递公司名称
  306. ['delivery_id', ''],//快递单号
  307. ['delivery_code', ''],//快递公司编码
  308. ['express_record_type', 2],//发货记录类型
  309. ['express_temp_id', ""],//电子面单模板
  310. ['to_name', ''],//寄件人姓名
  311. ['to_tel', ''],//寄件人电话
  312. ['to_addr', ''],//寄件人地址
  313. ['sh_delivery_name', ''],//送货人姓名
  314. ['sh_delivery_id', ''],//送货人电话
  315. ['sh_delivery_uid', ''],//送货人ID
  316. ['fictitious_content', ''],//虚拟发货内容
  317. ['cart_ids', []]
  318. ]);
  319. if (!$id) {
  320. return app('json')->fail('缺少发货ID');
  321. }
  322. if (!$data['cart_ids']) {
  323. return app('json')->fail('请选择发货商品');
  324. }
  325. foreach ($data['cart_ids'] as $cart) {
  326. if (!isset($cart['cart_id']) || !$cart['cart_id'] || !isset($cart['cart_num']) || !$cart['cart_num']) {
  327. return app('json')->fail('请重新选择发货商品,或发货件数');
  328. }
  329. }
  330. $this->getStaffInfo();
  331. $services->splitDelivery((int)$id, $data, (int)$this->staff_id);
  332. return app('json')->success('SUCCESS');
  333. }
  334. /**
  335. * 获取订单可拆分发货商品列表
  336. * @param $id
  337. * @param StoreOrderCartInfoServices $services
  338. * @return mixed
  339. */
  340. public function split_cart_info($id, StoreOrderCartInfoServices $services)
  341. {
  342. if (!$id) {
  343. return app('json')->fail('缺少发货ID');
  344. }
  345. return app('json')->success($services->getSplitCartList((int)$id));
  346. }
  347. /**
  348. * 订单改价
  349. * @param Request $request
  350. * @param StoreOrderServices $services
  351. * @return mixed
  352. * @throws \Exception
  353. */
  354. public function price(Request $request, StoreOrderServices $services)
  355. {
  356. [$order_id, $price] = $request->postMore([
  357. ['order_id', ''],
  358. ['price', '']
  359. ], true);
  360. $order = $this->services->getOne(['order_id' => $order_id], 'id,user_phone,id,paid,pay_price,order_id,total_price,total_postage,pay_postage,gain_integral');
  361. if (!$order) return app('json')->fail('订单不存在');
  362. if ($order['paid']) {
  363. return app('json')->fail('订单已支付');
  364. }
  365. if ($price === '') return app('json')->fail('请填写实际支付金额');
  366. if ($price < 0) return app('json')->fail('实际支付金额不能小于0元');
  367. if ($order['pay_price'] == $price) return app('json')->successful('改价成功');
  368. $services->updateOrder($order['id'], ['total_price' => $order['total_price'], 'pay_price' => $price]);
  369. return app('json')->successful('改价成功');
  370. }
  371. /**
  372. * 订单备注
  373. * @param Request $request
  374. * @return mixed
  375. * @throws \think\db\exception\DataNotFoundException
  376. * @throws \think\db\exception\ModelNotFoundException
  377. * @throws \think\exception\DbException
  378. */
  379. public function remark(Request $request)
  380. {
  381. [$order_id, $remark] = $request->postMore([
  382. ['order_id', ''],
  383. ['remark', '']
  384. ], true);
  385. $this->getStaffInfo();
  386. $order = $this->services->getOne(['order_id' => $order_id, 'store_id' => $this->store_id], 'id,remark');
  387. if (!$order) return app('json')->fail('订单不存在');
  388. if (!strlen(trim($remark))) return app('json')->fail('请填写备注内容');
  389. $order->remark = $remark;
  390. if (!$order->save())
  391. return app('json')->fail('备注失败');
  392. return app('json')->successful('备注成功');
  393. }
  394. /**
  395. * 订单支付
  396. * @param Request $request
  397. * @return mixed
  398. * @throws \think\db\exception\DataNotFoundException
  399. * @throws \think\db\exception\ModelNotFoundException
  400. * @throws \think\exception\DbException
  401. */
  402. public function offline(Request $request, OrderOfflineServices $services)
  403. {
  404. [$order_id] = $request->postMore([['order_id', '']], true);
  405. $this->getStaffInfo();
  406. $orderInfo = $this->services->getOne(['order_id' => $order_id, 'store_id' => $this->store_id], 'id');
  407. if (!$orderInfo) return app('json')->fail('参数错误');
  408. $id = $orderInfo->id;
  409. $services->orderOffline((int)$id);
  410. return app('json')->successful('确认成功!');
  411. }
  412. /**
  413. * 订单退款
  414. * @param Request $request
  415. * @return mixed
  416. * @throws \think\Exception
  417. * @throws \think\db\exception\DataNotFoundException
  418. * @throws \think\db\exception\ModelNotFoundException
  419. * @throws \think\exception\DbException
  420. */
  421. public function refund(Request $request, StoreOrderRefundServices $services)
  422. {
  423. [$orderId, $price, $type, $refuse_reason] = $request->postMore([
  424. ['order_id', ''],
  425. ['price', '0'],
  426. ['type', 1],
  427. ['refuse_reason', '']
  428. ], true);
  429. if (!strlen(trim($orderId))) return app('json')->fail('参数错误');
  430. $this->getStaffInfo();
  431. $orderRefund = $services->getOne(['order_id' => $orderId, 'store_id' => $this->store_id]);
  432. if (!$orderRefund) {
  433. return app('json')->fail('数据不存在!');
  434. }
  435. if ($orderRefund['is_cancel'] == 1) {
  436. return app('json')->fail('用户已取消申请');
  437. }
  438. $orderInfo = $this->services->get((int)$orderRefund['store_order_id']);
  439. if (!$orderInfo) {
  440. return app('json')->fail('数据不存在');
  441. }
  442. if (!in_array($orderRefund['refund_type'], [1, 2, 5])) {
  443. return app('json')->fail('售后订单状态不支持该操作');
  444. }
  445. if ($type == 1) {
  446. $data['refund_type'] = 6;
  447. } else if ($type == 2) {
  448. $data['refund_type'] = 3;
  449. $data['refuse_reason'] = $refuse_reason;
  450. } else {
  451. return app('json')->fail('退款修改状态错误');
  452. }
  453. $data['refunded_time'] = time();
  454. //拒绝退款
  455. if ($type == 2) {
  456. $services->refuseRefund((int)$orderRefund['id'], $data, $orderRefund);
  457. return app('json')->successful('修改退款状态成功!');
  458. } else {
  459. if ($orderRefund['refund_price'] == $orderRefund['refunded_price']) return app('json')->fail('已退完支付金额!不能再退款了');
  460. if (!$price) {
  461. return app('json')->fail('请输入退款金额');
  462. }
  463. $data['refunded_price'] = bcadd($price, $orderRefund['refunded_price'], 2);
  464. $bj = bccomp((float)$orderRefund['refund_price'], (float)$data['refunded_price'], 2);
  465. if ($bj < 0) {
  466. return app('json')->fail('退款金额大于支付金额,请修改退款金额');
  467. }
  468. $refundData['pay_price'] = $orderInfo['pay_price'];
  469. $refundData['refund_price'] = $price;
  470. if ($orderInfo['refund_price'] > 0) {
  471. mt_srand();
  472. $refundData['refund_id'] = $orderInfo['order_id'] . rand(100, 999);
  473. }
  474. //修改订单退款状态
  475. if ($services->agreeRefund($orderRefund['id'], $refundData)) {
  476. $services->update((int)$orderRefund['id'], $data);
  477. return app('json')->success('退款成功');
  478. } else {
  479. $services->storeProductOrderRefundYFasle((int)$orderInfo['id'], $price);
  480. return app('json')->fail('退款失败');
  481. }
  482. }
  483. }
  484. /**
  485. * 商家同意退货退款
  486. * @return mixed
  487. */
  488. public function agreeRefund(Request $request, StoreOrderRefundServices $services)
  489. {
  490. [$id] = $request->getMore([
  491. ['id', '']
  492. ], true);
  493. $services->agreeRefundProdcut((int)$id);
  494. return app('json')->success('操作成功');
  495. }
  496. /**
  497. * 订单取消 未支付的订单回退积分,回退优惠券,回退库存
  498. * @param Request $request
  499. * @param StoreOrderServices $orderServices
  500. * @param $id
  501. * @return mixed
  502. * @throws \think\db\exception\DataNotFoundException
  503. * @throws \think\db\exception\DbException
  504. * @throws \think\db\exception\ModelNotFoundException
  505. */
  506. public function cancel(Request $request, $id)
  507. {
  508. if (!$id) return app('json')->fail('参数错误');
  509. $this->getStaffInfo();
  510. if ($this->services->cancelOrder($id, $this->store_id))
  511. return app('json')->successful('取消订单成功');
  512. return app('json')->fail('取消订单失败');
  513. }
  514. /**
  515. * 订单删除
  516. * @param Request $request
  517. * @return mixed
  518. */
  519. public function del(Request $request, $id)
  520. {
  521. if (!$id) return app('json')->fail('参数错误!');
  522. $orderInfo = $this->services->get($id);
  523. $this->getStaffInfo();
  524. if (!$orderInfo || $orderInfo['store_id'] != $this->store_id)
  525. return app('json')->fail('订单不存在');
  526. if (!$orderInfo->is_del)
  527. return app('json')->fail('订单用户未删除无法删除');
  528. $orderInfo->is_system_del = 1;
  529. if ($orderInfo->save())
  530. return app('json')->success('SUCCESS');
  531. else
  532. return app('json')->fail('ERROR');
  533. }
  534. /**
  535. * 扫码获取核销订单列表信息
  536. * @param Request $request
  537. * @param WriteOffOrderServices $writeOffOrderServices
  538. * @param $type
  539. * @return mixed
  540. * @throws \think\db\exception\DataNotFoundException
  541. * @throws \think\db\exception\DbException
  542. * @throws \think\db\exception\ModelNotFoundException
  543. */
  544. public function writeoffOrderinfo(Request $request, WriteOffOrderServices $writeOffOrderServices, StoreOrderCartInfoServices $orderCartInfo, $type)
  545. {
  546. [$verifyCode, $codeType] = $request->postMore([
  547. ['verify_code', ''],
  548. ['code_type', 1]
  549. ], true);
  550. $uid = (int)$request->uid();
  551. if (!$verifyCode || $verifyCode == 'undefined') return app('json')->fail('Lack of write-off code');
  552. if (strlen($verifyCode) == 12) {//核销码
  553. $orderInfo = $writeOffOrderServices->writeoffOrderInfo($uid, $verifyCode, $type);
  554. if ($orderInfo) {
  555. $data = [$orderInfo];
  556. }
  557. } else {//找用户
  558. $data = $writeOffOrderServices->userUnWriteoffOrder($uid, $verifyCode, $type);
  559. }
  560. $res = [];
  561. if ($data) {
  562. /** @var StoreOrderServices $orderServices */
  563. $orderServices = app()->make(StoreOrderServices::class);
  564. $res = $orderServices->tidyOrderList($data, false);
  565. foreach ($res as &$orderInfo) {
  566. $orderInfo['image'] = '';
  567. if (isset($orderInfo['cart_id'][0])) {
  568. $cartInfo = $orderCartInfo->getOne(['oid' => $orderInfo['id'], 'cart_id' => $orderInfo['cart_id'][0]], 'cart_info');
  569. if ($cartInfo) $orderInfo['image'] = $cartInfo['cart_info']['productInfo']['image'] ?? '';
  570. }
  571. }
  572. }
  573. return app('json')->success($res);
  574. }
  575. /**
  576. * 扫码获取核销订单列表信息
  577. * @param Request $request
  578. * @param $id
  579. * @return mixed
  580. */
  581. public function orderCartInfo(Request $request, WriteOffOrderServices $writeOffOrderServices, $type)
  582. {
  583. $uid = (int)$request->uid();
  584. [$oid] = $request->postMore([
  585. ['oid', '']
  586. ], true);
  587. return app('json')->success($writeOffOrderServices->getOrderCartInfo($uid, $oid, $type));
  588. }
  589. /**
  590. * 核销订单
  591. * @param Request $request
  592. * @param WriteOffOrderServices $writeOffOrderServices
  593. * @param $type
  594. * @return mixed
  595. * @throws \think\db\exception\DataNotFoundException
  596. * @throws \think\db\exception\DbException
  597. * @throws \think\db\exception\ModelNotFoundException
  598. */
  599. public function wirteoff(Request $request, WriteOffOrderServices $writeOffOrderServices, $type)
  600. {
  601. [$oid, $cart_ids] = $request->postMore([
  602. ['oid', ''],
  603. ['cart_ids', []]
  604. ], true);
  605. if (!$oid || !$cart_ids) {
  606. return app('json')->fail('请选择要核销的订单商品');
  607. }
  608. foreach ($cart_ids as $cart) {
  609. if (!isset($cart['cart_id']) || !$cart['cart_id'] || !isset($cart['cart_num']) || !$cart['cart_num'] || $cart['cart_num'] <= 0) {
  610. return app('json')->fail('请重新选择核销商品,或核销件数');
  611. }
  612. }
  613. $uid = (int)$request->uid();
  614. $orderInfo = $writeOffOrderServices->writeoffOrderInfo($uid, '', (int)$type, $oid);
  615. return app('json')->success('核销成功', $writeOffOrderServices->writeoffOrder($uid, $orderInfo, $cart_ids, $type));
  616. }
  617. /**
  618. * @param Request $request
  619. * @param StoreOrderRefundServices $services
  620. * @return mixed
  621. * @throws \think\db\exception\DataNotFoundException
  622. * @throws \think\db\exception\DbException
  623. * @throws \think\db\exception\ModelNotFoundException
  624. */
  625. public function refundList(Request $request, StoreOrderRefundServices $services)
  626. {
  627. $where = $request->getMore([
  628. ['order_id', ''],
  629. ['time', ''],
  630. ['refund_type', 0],
  631. ['keyword', '', '', 'real_name']
  632. ]);
  633. $this->getStaffInfo();
  634. $store_id = $this->store_id;
  635. $where['store_id'] = $store_id;
  636. $data = $services->refundList($where)['list'];
  637. return app('json')->success($data);
  638. }
  639. /**
  640. * 门店退款列表
  641. * @param StoreOrderRefundServices $services
  642. * @param $id
  643. * @return mixed
  644. */
  645. public function refundDetail(StoreOrderRefundServices $services, $id)
  646. {
  647. $uni = $services->value(['id' => $id], 'order_id');
  648. $data = $services->refundDetail($uni);
  649. return app('json')->successful('ok', $data);
  650. }
  651. /**
  652. * 修改备注
  653. * @param $id
  654. * @return mixed
  655. */
  656. public function refundRemark(StoreOrderRefundServices $services, Request $request)
  657. {
  658. [$remark, $order_id] = $request->postMore([
  659. ['remark', ''],
  660. ['order_id', ''],
  661. ], true);
  662. if (!$remark)
  663. return app('json')->fail('请输入要备注的内容');
  664. if (!$order_id)
  665. return app('json')->fail('缺少参数');
  666. if (!$order = $services->get(['order_id' => $order_id])) {
  667. return app('json')->fail('修改的订单不存在!');
  668. }
  669. $order->remark = $remark;
  670. if ($order->save()) {
  671. return app('json')->success('备注成功');
  672. } else
  673. return app('json')->fail('备注失败');
  674. }
  675. }