Order.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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\kefuapi\controller;
  12. use app\Request;
  13. use app\services\order\StoreOrderWriteOffServices;
  14. use think\facade\App;
  15. use app\services\order\DeliveryServiceServices;
  16. use app\services\product\product\StoreProductServices;
  17. use app\services\serve\ServeServices;
  18. use app\services\shipping\ExpressServices;
  19. use app\services\user\UserServices;
  20. use app\services\order\StoreOrderServices;
  21. use app\services\order\StoreOrderRefundServices;
  22. use app\services\order\StoreOrderDeliveryServices;
  23. use app\services\system\store\SystemStoreServices;
  24. use app\adminapi\validate\order\StoreOrderValidate;
  25. use app\services\kefu\service\StoreServiceRecordServices;
  26. /**
  27. * Class Order
  28. * @package app\kefuapi\controller
  29. */
  30. class Order extends AuthController
  31. {
  32. /**
  33. * Order constructor.
  34. * @param App $app
  35. * @param StoreOrderServices $services
  36. */
  37. public function __construct(App $app, StoreOrderServices $services)
  38. {
  39. parent::__construct($app);
  40. $this->services = $services;
  41. }
  42. /**
  43. * 获取订单列表
  44. * @param Request $request
  45. * @param $uid
  46. * @return mixed
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\DbException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. */
  51. public function getUserOrderList(Request $request, StoreServiceRecordServices $services, $uid)
  52. {
  53. $where = $request->getMore([
  54. ['type', '', '', 'status'],
  55. ['search', '', '', 'real_name'],
  56. ]);
  57. $where['uid'] = $uid;
  58. $where['is_del'] = 0;
  59. $where['is_system_del'] = 0;
  60. if ($where['status'] == -1) $where['refund_type'] = [1, 3, 6];
  61. if (!$services->count(['to_uid' => $uid])) {
  62. return app('json')->fail(410092);
  63. }
  64. return app('json')->success($this->services->getOrderApiList($where));
  65. }
  66. /**
  67. * 订单发货
  68. * @return mixed
  69. * @throws \think\db\exception\DataNotFoundException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. * @throws \think\exception\DbException
  72. */
  73. public function delivery_keep(StoreOrderDeliveryServices $services, $id)
  74. {
  75. $data = $this->request->postMore([
  76. ['type', 1],
  77. ['delivery_name', ''],//快递公司名称
  78. ['delivery_id', ''],//快递单号
  79. ['delivery_code', ''],//快递公司编码
  80. ['express_record_type', 2],//发货记录类型
  81. ['express_temp_id', ""],//电子面单模板
  82. ['to_name', ''],//寄件人姓名
  83. ['to_tel', ''],//寄件人电话
  84. ['to_addr', ''],//寄件人地址
  85. ['sh_delivery_name', ''],//送货人姓名
  86. ['sh_delivery_id', ''],//送货人电话
  87. ['sh_delivery_uid', ''],//送货人ID
  88. ['fictitious_content', '']//虚拟发货内容
  89. ]);
  90. $services->delivery((int)$id, $data);
  91. return app('json')->success(410273);
  92. }
  93. /**
  94. * 修改支付金额等
  95. * @param $id
  96. * @return mixed|\think\response\Json|void
  97. */
  98. public function edit($id)
  99. {
  100. if (!$id) {
  101. return app('json')->fail(100026);
  102. }
  103. return app('json')->success($this->services->updateForm($id));
  104. }
  105. /**
  106. * 修改订单
  107. * @param $id
  108. * @return mixed
  109. */
  110. public function update($id)
  111. {
  112. if (!$id) {
  113. return app('json')->fail(100100);
  114. }
  115. $data = $this->request->postMore([
  116. ['order_id', ''],
  117. ['total_price', 0],
  118. ['total_postage', 0],
  119. ['pay_price', 0],
  120. ['pay_postage', 0],
  121. ['gain_integral', 0],
  122. ]);
  123. validate(StoreOrderValidate::class)->check($data);
  124. if ($data['total_price'] < 0) {
  125. return app('json')->fail(410093);
  126. }
  127. if ($data['pay_price'] < 0) {
  128. return app('json')->fail(410093);
  129. }
  130. $this->services->updateOrder((int)$id, $data);
  131. return app('json')->success(100001);
  132. }
  133. /**
  134. * 订单备注
  135. * @param Request $request
  136. * @return mixed
  137. * @throws \think\db\exception\DataNotFoundException
  138. * @throws \think\db\exception\ModelNotFoundException
  139. * @throws \think\exception\DbException
  140. */
  141. public function remark(Request $request)
  142. {
  143. [$order_id, $remark] = $request->postMore([
  144. ['order_id', ''],
  145. ['remark', '']
  146. ], true);
  147. $order = $this->services->getOne(['order_id' => $order_id], 'id,remark');
  148. if (!$order) {
  149. return app('json')->fail(410173);
  150. }
  151. if (!strlen(trim($remark))) {
  152. return app('json')->fail(410177);
  153. }
  154. $order->remark = $remark;
  155. if (!$order->save()) {
  156. return app('json')->fail(100025);
  157. }
  158. return app('json')->success(100024);
  159. }
  160. /**
  161. * 退款表单生成
  162. * @param $id 订单id
  163. * @return mixed
  164. * @throws \FormBuilder\Exception\FormBuilderException
  165. */
  166. public function refundForm(StoreOrderRefundServices $services, $id)
  167. {
  168. if (!$id) {
  169. return app('json')->fail(100100);
  170. }
  171. return app('json')->success($services->refundOrderForm((int)$id));
  172. }
  173. /**
  174. * 订单退款
  175. * @param Request $request
  176. * @return mixed
  177. * @throws \think\Exception
  178. * @throws \think\db\exception\DataNotFoundException
  179. * @throws \think\db\exception\ModelNotFoundException
  180. * @throws \think\exception\DbException
  181. */
  182. public function refund(Request $request, StoreOrderRefundServices $services)
  183. {
  184. [$orderId, $price, $type] = $request->postMore([
  185. ['order_id', ''],
  186. ['price', '0'],
  187. ['type', 1],
  188. ], true);
  189. if (!strlen(trim($orderId))) return app('json')->fail(100100);
  190. $orderInfo = $this->services->getOne(['order_id' => $orderId]);
  191. if (!$orderInfo) return app('json')->fail(100026);
  192. //仅退款类型
  193. if ($orderInfo['refund_type'] != 1) {
  194. return app('json')->fail(410094);
  195. }
  196. if ($type == 1) {
  197. $data['refund_status'] = 2;
  198. $data['refund_type'] = 6;
  199. } else if ($type == 2) {
  200. $data['refund_status'] = 0;
  201. $data['refund_type'] = 3;
  202. } else {
  203. return app('json')->fail(410181);
  204. }
  205. if ($orderInfo['pay_price'] == 0 || $type == 2) {
  206. $orderInfo->refund_status = $data['refund_status'];
  207. $orderInfo->save();
  208. return app('json')->success(410182);
  209. }
  210. if ($orderInfo['pay_price'] == $orderInfo['refund_price']) return app('json')->fail(410183);
  211. if (!$price) {
  212. return app('json')->fail(410184);
  213. }
  214. $data['refund_price'] = bcadd($price, $orderInfo['refund_price'], 2);
  215. $bj = bccomp((float)$orderInfo['pay_price'], (float)$data['refund_price'], 2);
  216. if ($bj < 0) {
  217. return app('json')->fail(410185);
  218. }
  219. $refundData['pay_price'] = $orderInfo['pay_price'];
  220. $refundData['refund_price'] = $price;
  221. if ($orderInfo['refund_price'] > 0) {
  222. $refundData['refund_id'] = $orderInfo['order_id'] . rand(100, 999);
  223. }
  224. //退款处理
  225. $services->payOrderRefund($type, $orderInfo, $refundData);
  226. //修改订单退款状态
  227. if ($this->services->update((int)$orderInfo['id'], $data)) {
  228. $services->storeProductOrderRefundY($data, $orderInfo, $price);
  229. return app('json')->success(410186);
  230. } else {
  231. $services->storeProductOrderRefundYFasle((int)$orderInfo['id'], $price);
  232. return app('json')->fail(410187);
  233. }
  234. }
  235. /**
  236. * 订单详情
  237. * @param $id 订单id
  238. * @return mixed
  239. */
  240. public function orderInfo(StoreProductServices $productServices, $id)
  241. {
  242. if (!$id || !($orderInfo = $this->services->get($id))) {
  243. return app('json')->fail(410173);
  244. }
  245. /** @var UserServices $services */
  246. $services = app()->make(UserServices::class);
  247. $userInfo = $services->get($orderInfo['uid']);
  248. if (!$userInfo) {
  249. return app('json')->fail(410032);
  250. }
  251. $userInfo = $userInfo->hidden(['pwd', 'add_ip', 'last_ip', 'login_type']);
  252. $userInfo['spread_name'] = '';
  253. if ($userInfo['spread_uid'])
  254. $userInfo['spread_name'] = $services->value(['uid' => $userInfo['spread_uid']], 'nickname');
  255. $orderInfo = $this->services->tidyOrder($orderInfo->toArray(), true);
  256. $productId = array_column($orderInfo['cartInfo'], 'product_id');
  257. $cateData = $productServices->productIdByProductCateName($productId);
  258. foreach ($orderInfo['cartInfo'] as &$item) {
  259. $item['class_name'] = $cateData[$item['product_id']] ?? '';
  260. }
  261. if ($orderInfo['store_id'] && $orderInfo['shipping_type'] == 2) {
  262. /** @var $storeServices */
  263. $storeServices = app()->make(SystemStoreServices::class);
  264. $orderInfo['_store_name'] = $storeServices->value(['id' => $orderInfo['store_id']], 'name');
  265. } else {
  266. $orderInfo['_store_name'] = '';
  267. }
  268. $userInfo = $userInfo->toArray();
  269. return app('json')->success(compact('orderInfo', 'userInfo'));
  270. }
  271. /**
  272. * 获取物流
  273. * @param ExpressServices $services
  274. * @return mixed
  275. */
  276. public function export(ExpressServices $services)
  277. {
  278. return app('json')->success($services->express());
  279. }
  280. /**
  281. *
  282. * 获取面单信息
  283. * @param string $com
  284. * @return mixed
  285. */
  286. public function getExportTemp(ServeServices $services)
  287. {
  288. [$com] = $this->request->getMore([
  289. ['com', ''],
  290. ], true);
  291. return app('json')->success($services->express()->temp($com));
  292. }
  293. /**
  294. * 获取所有配送员列表
  295. * @param DeliveryServiceServices $services
  296. * @return mixed
  297. */
  298. public function getDeliveryAll(DeliveryServiceServices $services)
  299. {
  300. $list = $services->getDeliveryList();
  301. return app('json')->success($list['list']);
  302. }
  303. /**
  304. * 获取配置信息
  305. * @return mixed
  306. */
  307. public function getDeliveryInfo()
  308. {
  309. return app('json')->success([
  310. 'express_temp_id' => sys_config('config_export_temp_id'),
  311. 'to_name' => sys_config('config_export_to_name'),
  312. 'id' => sys_config('config_export_id'),
  313. 'to_tel' => sys_config('config_export_to_tel'),
  314. 'to_add' => sys_config('config_export_to_address')
  315. ]);
  316. }
  317. /**
  318. * 门店核销
  319. * @param Request $request
  320. */
  321. public function order_verific(StoreOrderWriteOffServices $services, $id)
  322. {
  323. $orderInfo = $this->services->get(['id' => $id], ['verify_code', 'uid']);
  324. if (!$orderInfo) {
  325. return app('json')->fail(410173);
  326. }
  327. if (!$orderInfo->verify_code) {
  328. return app('json')->fail(410272);
  329. }
  330. $services->writeOffOrder($orderInfo->verify_code, 1, $orderInfo->uid);
  331. return app('json')->success(410189);
  332. }
  333. }