Order.php 12 KB

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