StoreOrderController.php 29 KB

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