Order.php 12 KB

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