Order.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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\kefu;
  12. use app\Request;
  13. use app\services\activity\coupon\StoreCouponIssueServices;
  14. use app\services\activity\combination\StorePinkServices;
  15. use app\services\order\store\WriteOffOrderServices;
  16. use think\facade\App;
  17. use app\services\store\DeliveryServiceServices;
  18. use app\services\order\StoreOrderPromotionsServices;
  19. use app\services\product\product\StoreProductServices;
  20. use app\services\serve\ServeServices;
  21. use app\services\other\ExpressServices;
  22. use app\services\user\UserServices;
  23. use app\services\order\StoreOrderServices;
  24. use app\services\order\StoreOrderRefundServices;
  25. use app\services\order\StoreOrderDeliveryServices;
  26. use app\services\store\SystemStoreServices;
  27. use app\validate\admin\order\StoreOrderValidate;
  28. use app\services\message\service\StoreServiceRecordServices;
  29. use \app\common\controller\Order as CommonOrder;
  30. /**
  31. * Class Order
  32. * @package app\kefuapi\controller
  33. */
  34. class Order extends AuthController
  35. {
  36. use CommonOrder;
  37. /**
  38. * Order constructor.
  39. * @param App $app
  40. * @param StoreOrderServices $services
  41. */
  42. public function __construct(App $app, StoreOrderServices $services)
  43. {
  44. parent::__construct($app);
  45. $this->services = $services;
  46. }
  47. /**
  48. * 获取订单列表
  49. * @param Request $request
  50. * @param $uid
  51. * @return mixed
  52. * @throws \think\db\exception\DataNotFoundException
  53. * @throws \think\db\exception\DbException
  54. * @throws \think\db\exception\ModelNotFoundException
  55. */
  56. public function getUserOrderList(Request $request, StoreServiceRecordServices $services, StoreOrderRefundServices $storeOrderRefundServices, $uid)
  57. {
  58. $where = $request->getMore([
  59. ['type', '', '', 'status'],
  60. ['search', '', '', 'real_name'],
  61. ]);
  62. $where['uid'] = $uid;
  63. $where['is_del'] = 0;
  64. $where['is_system_del'] = 0;
  65. $where['refund_type'] = [0, 1, 3, 6];
  66. if (!$services->count(['to_uid' => $uid])) {
  67. return $this->fail('用户uid不再当前聊天用户范围内');
  68. }
  69. if ($where['status'] == -1) {
  70. $list = $storeOrderRefundServices->refundList(['uid' => $where['uid'], 'real_name' => $where['real_name'], 'refund_type' => [1, 2, 4, 5]])['list'] ?? [];
  71. } else {
  72. $where['store_id'] = 0;
  73. $list = $this->services->getOrderApiList($where + ['pid' => 0], ['*'], ['pink', 'invoice']);
  74. }
  75. return $this->success($list);
  76. }
  77. /**
  78. * 订单发货
  79. * @return mixed
  80. * @throws \think\db\exception\DataNotFoundException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. * @throws \think\exception\DbException
  83. */
  84. public function delivery_keep(StoreOrderDeliveryServices $services, $id)
  85. {
  86. $data = $this->request->postMore([
  87. ['type', 1],
  88. ['delivery_name', ''],//快递公司名称
  89. ['delivery_id', ''],//快递单号
  90. ['delivery_code', ''],//快递公司编码
  91. ['express_record_type', 2],//发货记录类型
  92. ['express_temp_id', ""],//电子面单模板
  93. ['to_name', ''],//寄件人姓名
  94. ['to_tel', ''],//寄件人电话
  95. ['to_addr', ''],//寄件人地址
  96. ['sh_delivery_name', ''],//送货人姓名
  97. ['sh_delivery_id', ''],//送货人电话
  98. ['sh_delivery_uid', ''],//送货人ID
  99. ['fictitious_content', '']//虚拟发货内容
  100. ]);
  101. return $this->success('发货成功!', $services->delivery((int)$id, $data));
  102. }
  103. /**
  104. * 修改支付金额等
  105. * @param $id
  106. * @return mixed|\think\response\Json|void
  107. */
  108. public function edit($id)
  109. {
  110. if (!$id) {
  111. return $this->fail('Data does not exist!');
  112. }
  113. return $this->success($this->services->updateForm($id));
  114. }
  115. /**
  116. * 修改订单
  117. * @param $id
  118. * @return mixed
  119. */
  120. public function update($id)
  121. {
  122. if (!$id) {
  123. return $this->fail('Missing order ID');
  124. }
  125. $data = $this->request->postMore([
  126. ['order_id', ''],
  127. ['total_price', 0],
  128. ['total_postage', 0],
  129. ['pay_price', 0],
  130. ['pay_postage', 0],
  131. ['gain_integral', 0],
  132. ]);
  133. validate(StoreOrderValidate::class)->check($data);
  134. if ($data['total_price'] < 0) {
  135. return $this->fail('Please enter the total price');
  136. }
  137. if ($data['pay_price'] < 0) {
  138. return $this->fail('Please enter the actual payment amount');
  139. }
  140. $this->services->updateOrder((int)$id, $data);
  141. return $this->success('Modified success');
  142. }
  143. /**
  144. * 订单备注
  145. * @param Request $request
  146. * @return mixed
  147. * @throws \think\db\exception\DataNotFoundException
  148. * @throws \think\db\exception\ModelNotFoundException
  149. * @throws \think\exception\DbException
  150. */
  151. public function remark(Request $request)
  152. {
  153. [$order_id, $remark] = $request->postMore([
  154. ['order_id', ''],
  155. ['remark', '']
  156. ], true);
  157. $order = $this->services->getOne(['order_id' => $order_id], 'id,remark');
  158. /** @var StoreOrderRefundServices $refundServices */
  159. $refundServices = app()->make(StoreOrderRefundServices::class);
  160. $order = $order ?: $refundServices->get(['order_id' => $order_id]);
  161. if (!$order) {
  162. return $this->fail('订单不存在');
  163. }
  164. if (!strlen(trim($remark))) {
  165. return $this->fail('请填写备注内容');
  166. }
  167. $order->remark = $remark;
  168. if (!$order->save()) {
  169. return $this->fail('备注失败');
  170. }
  171. return $this->success('备注成功');
  172. }
  173. /**
  174. * 退款表单生成
  175. * @param $id 订单id
  176. * @return mixed
  177. * @throws \FormBuilder\Exception\FormBuilderException
  178. */
  179. public function refundForm(StoreOrderRefundServices $services, $id)
  180. {
  181. if (!$id) {
  182. return $this->fail('Data does not exist!');
  183. }
  184. return $this->success($services->refundOrderForm((int)$id));
  185. }
  186. /**
  187. * 订单退款
  188. * @param Request $request
  189. * @return mixed
  190. * @throws \think\Exception
  191. * @throws \think\db\exception\DataNotFoundException
  192. * @throws \think\db\exception\ModelNotFoundException
  193. * @throws \think\exception\DbException
  194. */
  195. public function refundOld(Request $request, StoreOrderRefundServices $services)
  196. {
  197. [$orderId, $price, $type] = $request->postMore([
  198. ['order_id', ''],
  199. ['price', '0'],
  200. ['type', 1],
  201. ], true);
  202. if (!strlen(trim($orderId))) return $this->fail('参数错误');
  203. $orderRefund = $services->getOne(['order_id' => $orderId]);
  204. if (!$orderRefund) {
  205. return app('json')->fail('数据不存在!');
  206. }
  207. if ($orderRefund['is_cancel'] == 1) {
  208. return app('json')->fail('用户已取消申请');
  209. }
  210. $orderInfo = $this->services->get((int)$orderRefund['store_order_id']);
  211. if (!$orderInfo) {
  212. return app('json')->fail('数据不存在');
  213. }
  214. if (!in_array($orderRefund['refund_type'], [1, 2, 5])) {
  215. return app('json')->fail('售后订单状态不支持该操作');
  216. }
  217. if ($type == 1) {
  218. $data['refund_type'] = 6;
  219. } else if ($type == 2) {
  220. $data['refund_type'] = 3;
  221. } else {
  222. return app('json')->fail('退款修改状态错误');
  223. }
  224. $data['refunded_time'] = time();
  225. //拒绝退款
  226. if ($type == 2) {
  227. $services->refuseRefund((int)$orderRefund['id'], $data, $orderRefund);
  228. return $this->success('修改退款状态成功!');
  229. } else {
  230. if ($orderRefund['refund_price'] == $orderRefund['refunded_price']) return $this->fail('已退完支付金额!不能再退款了');
  231. if (!$price) {
  232. return $this->fail('请输入退款金额');
  233. }
  234. $data['refunded_price'] = bcadd($price, $orderRefund['refunded_price'], 2);
  235. $bj = bccomp((float)$orderRefund['refund_price'], (float)$data['refunded_price'], 2);
  236. if ($bj < 0) {
  237. return $this->fail('退款金额大于支付金额,请修改退款金额');
  238. }
  239. $refundData['pay_price'] = $orderInfo['pay_price'];
  240. $refundData['refund_price'] = $price;
  241. //修改订单退款状态
  242. if ($services->agreeRefund((int)$orderRefund['id'], $refundData)) {
  243. $services->update((int)$orderRefund['id'], $data);
  244. return $this->success('退款成功');
  245. } else {
  246. $services->storeProductOrderRefundYFasle((int)$orderInfo['id'], $price);
  247. return $this->fail('退款失败');
  248. }
  249. }
  250. }
  251. /**
  252. * 订单详情
  253. * @param $id 订单id
  254. * @return mixed
  255. */
  256. public function orderInfo(StoreProductServices $productServices, StoreOrderPromotionsServices $storeOrderPromotiosServices, $id)
  257. {
  258. if (!$id || !($orderInfo = $this->services->get($id))) {
  259. return $this->fail('订单不存在');
  260. }
  261. /** @var UserServices $services */
  262. $services = app()->make(UserServices::class);
  263. $userInfo = $services->getUserWithTrashedInfo((int)$orderInfo['uid']);
  264. if (!$userInfo) {
  265. return $this->fail('用户信息不存在');
  266. }
  267. $userInfo = $userInfo->hidden(['pwd', 'add_ip', 'last_ip', 'login_type']);
  268. $userInfo['spread_name'] = '';
  269. if ($userInfo['spread_uid'])
  270. $userInfo['spread_name'] = $services->value(['uid' => $userInfo['spread_uid']], 'nickname');
  271. $orderInfo = $this->services->tidyOrder($orderInfo->toArray(), true);
  272. /** @var StorePinkServices $pinkService */
  273. $pinkService = app()->make(StorePinkServices::class);
  274. $orderInfo['pinkStatus'] = $pinkService->value(['order_id' => $orderInfo['order_id']], 'status');
  275. $productId = array_column($orderInfo['cartInfo'], 'product_id');
  276. $cateData = $productServices->productIdByProductCateName($productId);
  277. foreach ($orderInfo['cartInfo'] as &$item) {
  278. $item['class_name'] = $cateData[$item['product_id']] ?? '';
  279. }
  280. if ($orderInfo['store_id'] && $orderInfo['shipping_type'] == 2) {
  281. /** @var $storeServices */
  282. $storeServices = app()->make(SystemStoreServices::class);
  283. $orderInfo['_store_name'] = $storeServices->value(['id' => $orderInfo['store_id']], 'name');
  284. } else {
  285. $orderInfo['_store_name'] = '';
  286. }
  287. //核算优惠金额
  288. $vipTruePrice = 0;
  289. foreach ($orderInfo['cartInfo'] ?? [] as $cart) {
  290. $vipTruePrice = bcadd((string)$vipTruePrice, (string)$cart['vip_sum_truePrice'], 2);
  291. }
  292. $orderInfo['vip_true_price'] = $vipTruePrice;
  293. $orderInfo['total_price'] = bcadd((string)$orderInfo['total_price'], (string)$orderInfo['vip_true_price'], 2);
  294. //优惠活动优惠详情
  295. $orderInfo['promotions_detail'] = $storeOrderPromotiosServices->getOrderPromotionsDetail((int)$orderInfo['id']);
  296. if ($orderInfo['give_coupon']) {
  297. $couponIds = is_string($orderInfo['give_coupon']) ? explode(',', $orderInfo['give_coupon']) : $orderInfo['give_coupon'];
  298. /** @var StoreCouponIssueServices $couponIssueService */
  299. $couponIssueService = app()->make(StoreCouponIssueServices::class);
  300. $orderInfo['give_coupon'] = $couponIssueService->getColumn([['id', 'IN', $couponIds]], 'id,coupon_title');
  301. }
  302. $userInfo = $userInfo->toArray();
  303. return $this->success(compact('orderInfo', 'userInfo'));
  304. }
  305. /**
  306. * 获取物流
  307. * @param ExpressServices $services
  308. * @return mixed
  309. */
  310. public function export(ExpressServices $services)
  311. {
  312. [$status] = $this->request->getMore([
  313. ['status', ''],
  314. ], true);
  315. if ($status != '') $data['status'] = $status;
  316. $data['is_show'] = 1;
  317. return $this->success($services->express($data));
  318. }
  319. /**
  320. *
  321. * 获取面单信息
  322. * @param string $com
  323. * @return mixed
  324. */
  325. public function getExportTemp(ServeServices $services)
  326. {
  327. [$com] = $this->request->getMore([
  328. ['com', ''],
  329. ], true);
  330. return $this->success($services->express()->temp($com));
  331. }
  332. /**
  333. * 获取所有配送员列表
  334. * @param DeliveryServiceServices $services
  335. * @return mixed
  336. */
  337. public function getDeliveryAll(DeliveryServiceServices $services)
  338. {
  339. $list = $services->getDeliveryList();
  340. return $this->success($list['list']);
  341. }
  342. /**
  343. * 获取配置信息
  344. * @return mixed
  345. */
  346. public function getDeliveryInfo()
  347. {
  348. return $this->success([
  349. 'express_temp_id' => sys_config('config_export_temp_id'),
  350. 'to_name' => sys_config('config_export_to_name'),
  351. 'id' => sys_config('config_export_id'),
  352. 'to_tel' => sys_config('config_export_to_tel'),
  353. 'to_add' => sys_config('config_export_to_address')
  354. ]);
  355. }
  356. /**
  357. * 订单核销
  358. * @param WriteOffOrderServices $writeOffOrderServices
  359. * @param $id
  360. * @return mixed
  361. * @throws \think\db\exception\DataNotFoundException
  362. * @throws \think\db\exception\DbException
  363. * @throws \think\db\exception\ModelNotFoundException
  364. */
  365. public function order_verific(WriteOffOrderServices $writeOffOrderServices, $id)
  366. {
  367. $orderInfo = $this->services->get(['id' => $id], ['*'], ['pink']);
  368. if (!$orderInfo) {
  369. return $this->fail('核销订单未查到');
  370. }
  371. if (!$orderInfo->verify_code) {
  372. return $this->fail('Lack of write-off code');
  373. }
  374. $writeOffOrderServices->writeOffOrder(0, $orderInfo->toArray(), [], 0);
  375. return $this->success('Write off successfully');
  376. }
  377. /**
  378. * 退款订单详情
  379. * @param StoreOrderRefundServices $services
  380. * @param UserServices $userServices
  381. * @param $id
  382. * @return mixed
  383. */
  384. public function refundDetail(StoreOrderRefundServices $services, UserServices $userServices, $id)
  385. {
  386. $order = $services->get(['id' => $id], ['id', 'order_id', 'uid']);
  387. $uni = $order['order_id'];
  388. $data['orderInfo'] = $services->refundDetail($uni);
  389. $userInfo = $userServices->getUserWithTrashedInfo((int)$order['uid']);
  390. if (!$userInfo) return app('json')->fail('用户信息不存在');
  391. $userInfo = $userInfo->hidden(['pwd', 'add_ip', 'last_ip', 'login_type']);
  392. $data['userInfo'] = $userInfo;
  393. return app('json')->success($data);
  394. }
  395. }