StoreOrderController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. <?php
  2. namespace app\api\controller\admin;
  3. use app\Request;
  4. use app\models\user\{
  5. User, UserBill
  6. };
  7. use crmeb\repositories\OrderRepository;
  8. use crmeb\repositories\ShortLetterRepositories;
  9. use crmeb\services\{
  10. MiniProgramService, UtilService, WechatService
  11. };
  12. use app\models\store\{
  13. StoreCart, StoreOrder, StoreOrderStatus, StorePink, StoreService
  14. };
  15. use app\models\system\SystemStoreStaff;
  16. /**
  17. * 订单类
  18. * Class StoreOrderController
  19. * @package app\api\controller\admin\order
  20. */
  21. class StoreOrderController
  22. {
  23. /**
  24. * 订单数据统计
  25. * @param Request $request
  26. * @return mixed
  27. */
  28. public function statistics(Request $request)
  29. {
  30. $uid = $request->uid();
  31. if (!StoreService::orderServiceStatus($uid))
  32. return app('json')->fail('权限不足');
  33. $dataCount = StoreOrder::getOrderDataAdmin();
  34. $dataPrice = StoreOrder::getOrderTimeData();
  35. $data = array_merge($dataCount, $dataPrice);
  36. return app('json')->successful($data);
  37. }
  38. /**
  39. * 订单每月统计数据
  40. * @param Request $request
  41. * @return mixed
  42. */
  43. public function data(Request $request)
  44. {
  45. $uid = $request->uid();
  46. if (!StoreService::orderServiceStatus($uid))
  47. return app('json')->fail('权限不足');
  48. list($page, $limit, $start, $stop) = UtilService::getMore([
  49. ['page', 1],
  50. ['limit', 7],
  51. ['start', ''],
  52. ['stop', '']
  53. ], $request, true);
  54. if (!$limit) return app('json')->successful([]);
  55. $data = StoreOrder::getOrderDataPriceCount($page, $limit, $start, $stop);
  56. if ($data) return app('json')->successful($data->toArray());
  57. return app('json')->successful([]);
  58. }
  59. /**
  60. * 订单列表
  61. * @param Request $request
  62. * @return mixed
  63. */
  64. public function lst(Request $request)
  65. {
  66. $uid = $request->uid();
  67. if (!StoreService::orderServiceStatus($uid))
  68. return app('json')->fail('权限不足');
  69. $where = UtilService::getMore([
  70. ['status', ''],
  71. ['is_del', 0],
  72. ['data', ''],
  73. ['type', ''],
  74. ['order', ''],
  75. ['page', 0],
  76. ['limit', 0]
  77. ], $request);
  78. if (!$where['limit']) return app('json')->successful([]);
  79. return app('json')->successful(StoreOrder::orderList($where));
  80. }
  81. /**
  82. * 订单详情
  83. * @param Request $request
  84. * @param $orderId
  85. * @return mixed
  86. * @throws \think\Exception
  87. * @throws \think\db\exception\DataNotFoundException
  88. * @throws \think\db\exception\ModelNotFoundException
  89. * @throws \think\exception\DbException
  90. */
  91. public function detail(Request $request, $orderId)
  92. {
  93. $uid = $request->uid();
  94. if (!StoreService::orderServiceStatus($uid))
  95. return app('json')->fail('权限不足');
  96. $order = StoreOrder::getAdminOrderDetail($orderId, 'id,uid,order_id,add_time,status,total_num,total_price,total_postage,pay_price,pay_postage,paid,refund_status,remark,pink_id,combination_id,mark,seckill_id,bargain_id,delivery_type,pay_type,real_name,user_phone,user_address,coupon_price,freight_price,delivery_name,delivery_type,delivery_id');
  97. if (!$order) return app('json')->fail('订单不存在');
  98. $order = $order->toArray();
  99. $nickname = User::getUserInfo($order['uid'], 'nickname')['nickname'];
  100. $orderInfo = StoreOrder::tidyAdminOrder([$order], true)[0];
  101. unset($orderInfo['uid'], $orderInfo['seckill_id'], $orderInfo['pink_id'], $orderInfo['combination_id'], $orderInfo['bargain_id'], $orderInfo['status'], $orderInfo['total_postage']);
  102. $orderInfo['nickname'] = $nickname;
  103. return app('json')->successful('ok', $orderInfo);
  104. }
  105. /**
  106. * 订单发货获取订单信息
  107. * @param Request $request
  108. * @param $orderId
  109. * @return mixed
  110. * @throws \think\Exception
  111. * @throws \think\db\exception\DataNotFoundException
  112. * @throws \think\db\exception\ModelNotFoundException
  113. * @throws \think\exception\DbException
  114. */
  115. public function delivery_gain(Request $request, $orderId)
  116. {
  117. $uid = $request->uid();
  118. if (!StoreService::orderServiceStatus($uid))
  119. return app('json')->fail('权限不足');
  120. $order = StoreOrder::getAdminOrderDetail($orderId, 'real_name,user_phone,user_address,order_id,uid,status,paid');
  121. if (!$order) return app('json')->fail('订单不存在');
  122. if ($order['paid']) {
  123. $order['nickname'] = User::getUserInfo($order['uid'], 'nickname')['nickname'];
  124. $order = $order->hidden(['uid', 'status', 'paid'])->toArray();
  125. return app('json')->successful('ok', $order);
  126. }
  127. return app('json')->fail('状态错误');
  128. }
  129. /**
  130. * 订单发货
  131. * @param Request $request
  132. * @return mixed
  133. * @throws \think\db\exception\DataNotFoundException
  134. * @throws \think\db\exception\ModelNotFoundException
  135. * @throws \think\exception\DbException
  136. */
  137. public function delivery_keep(Request $request)
  138. {
  139. $uid = $request->uid();
  140. if (!StoreService::orderServiceStatus($uid))
  141. return app('json')->fail('权限不足');
  142. list($order_id, $delivery_type, $delivery_name, $delivery_id) = UtilService::postMore([
  143. ['order_id', ''],
  144. ['delivery_type', 0],
  145. ['delivery_name', ''],
  146. ['delivery_id', ''],
  147. ], $request, true);
  148. $order = StoreOrder::getAdminOrderDetail($order_id, 'id,status,paid');
  149. if (!$order) return app('json')->fail('订单不存在');
  150. if (!$order['status'] && $order['paid']) {
  151. $deliveryTypeArr = ['send', 'express', 'fictitious'];
  152. if (!strlen(trim($delivery_type))) return app('json')->fail('请填写发货方式');
  153. if (!in_array($delivery_type, $deliveryTypeArr)) return app('json')->fail('发货方式错误');
  154. if ($delivery_type == 'express') {
  155. if (!strlen(trim($delivery_name))) return app('json')->fail('请选择快递公司');
  156. if (!strlen(trim($delivery_id))) return app('json')->fail('请填写快递单号');
  157. }
  158. if ($delivery_type == 'send') {
  159. if (!strlen(trim($delivery_name))) return app('json')->fail('请填写发货人');
  160. if (!strlen(trim($delivery_id))) return app('json')->fail('请填写发货手机号');
  161. }
  162. $data['status'] = 1;
  163. $data['delivery_type'] = $delivery_type;
  164. $data['delivery_name'] = $delivery_name;
  165. $data['delivery_id'] = $delivery_id;
  166. $res = StoreOrder::edit($data, $order['id'], 'id');
  167. if ($res) {
  168. if ($delivery_type == 'express') {
  169. StoreOrderStatus::status($order['id'], 'delivery_goods', '已发货 快递公司:' . $data['delivery_name'] . ' 快递单号:' . $data['delivery_id']);
  170. } else if ($delivery_type == 'send') {
  171. StoreOrderStatus::status($order['id'], 'delivery', '已配送 发货人:' . $delivery_name . ' 发货人电话:' . $delivery_id);
  172. } else if ($delivery_type == 'fictitious') {
  173. StoreOrderStatus::status($order['id'], 'delivery_fictitious', '虚拟产品已发货');
  174. }
  175. }
  176. event('StoreProductOrderDeliveryGoodsAfter', [$data, $order['id']]);
  177. return app('json')->successful('发货成功!');
  178. }
  179. return app('json')->fail('状态错误');
  180. }
  181. /**
  182. * 订单改价
  183. * @param Request $request
  184. * @return mixed
  185. * @throws \think\db\exception\DataNotFoundException
  186. * @throws \think\db\exception\ModelNotFoundException
  187. * @throws \think\exception\DbException
  188. */
  189. public function price(Request $request)
  190. {
  191. $uid = $request->uid();
  192. if (!StoreService::orderServiceStatus($uid))
  193. return app('json')->fail('权限不足');
  194. list($order_id, $price) = UtilService::postMore([
  195. ['order_id', ''],
  196. ['price', '']
  197. ], $request, true);
  198. $order = StoreOrder::getAdminOrderDetail($order_id, 'id,paid,user_phone,pay_price,order_id,total_price,total_postage,pay_postage,gain_integral');
  199. if (!$order) return app('json')->fail('订单不存在');
  200. $order = $order->toArray();
  201. if (!$order['paid']) {
  202. if ($price === '') return app('json')->fail('请填写实际支付金额');
  203. if ($price < 0) return app('json')->fail('实际支付金额不能小于0元');
  204. if ($order['pay_price'] == $price) return app('json')->successful('修改成功');
  205. $order['order_id'] = StoreOrder::changeOrderId($order['order_id']);
  206. if (!StoreOrder::edit(['pay_price' => $price, 'order_id' => $order['order_id']], $order['id'], 'id'))
  207. return app('json')->fail('状态错误');
  208. $order['pay_price'] = $price;
  209. event('StoreProductOrderEditAfter', [$order, $order['id']]);
  210. StoreOrderStatus::status($order['id'], 'order_edit', '修改实际支付金额' . $price);
  211. //改价短信提醒
  212. if ($price != $order['pay_price']) {
  213. $switch = sys_config('price_revision_switch') ? true : false;
  214. ShortLetterRepositories::send($switch, $order['user_phone'], ['order_id' => $order_id, 'pay_price' => $order['pay_price']], 'PRICE_REVISION_CODE');
  215. }
  216. return app('json')->successful('修改成功');
  217. }
  218. return app('json')->fail('状态错误');
  219. }
  220. /**
  221. * 订单备注
  222. * @param Request $request
  223. * @return mixed
  224. * @throws \think\db\exception\DataNotFoundException
  225. * @throws \think\db\exception\ModelNotFoundException
  226. * @throws \think\exception\DbException
  227. */
  228. public function remark(Request $request)
  229. {
  230. $uid = $request->uid();
  231. if (!StoreService::orderServiceStatus($uid))
  232. return app('json')->fail('权限不足');
  233. list($order_id, $remark) = UtilService::postMore([
  234. ['order_id', ''],
  235. ['remark', '']
  236. ], $request, true);
  237. $order = StoreOrder::getAdminOrderDetail($order_id, 'id');
  238. if (!$order) return app('json')->fail('订单不存在');
  239. $order = $order->toArray();
  240. if (!strlen(trim($remark))) return app('json')->fail('请填写备注内容');
  241. if (!StoreOrder::edit(['remark' => $remark], $order['id']))
  242. return app('json')->fail('备注失败');
  243. return app('json')->successful('备注成功');
  244. }
  245. /**
  246. * 订单交易额/订单数量时间统计
  247. * @param Request $request
  248. * @return bool
  249. */
  250. public function time(Request $request)
  251. {
  252. $uid = $request->uid();
  253. if (!StoreService::orderServiceStatus($uid))
  254. return app('json')->fail('权限不足');
  255. list($start, $stop, $type) = UtilService::getMore([
  256. ['start', strtotime(date('Y-m'))],
  257. ['stop', time()],
  258. ['type', 1]
  259. ], $request, true);
  260. if ($start == $stop) {
  261. return app('json')->fail('开始时间不能等于结束时间');
  262. }
  263. if ($start > $stop) {
  264. $middle = $stop;
  265. $stop = $start;
  266. $start = $middle;
  267. }
  268. $space = bcsub($stop, $start, 0);//间隔时间段
  269. $front = bcsub($start, $space, 0);//第一个时间段
  270. if ($type == 1) {//销售额
  271. $frontPrice = StoreOrder:: getOrderTimeBusinessVolumePrice($front, $start);
  272. $afterPrice = StoreOrder:: getOrderTimeBusinessVolumePrice($start, $stop);
  273. $chartInfo = StoreOrder::chartTimePrice($start, $stop);
  274. $data['chart'] = $chartInfo;//营业额图表数据
  275. $data['time'] = $afterPrice;//时间区间营业额
  276. $increase = (float)bcsub($afterPrice, $frontPrice, 2); //同比上个时间区间增长营业额
  277. $growthRate = abs($increase);
  278. if ($growthRate == 0) $data['growth_rate'] = 0;
  279. else if ($frontPrice == 0) $data['growth_rate'] = $growthRate;
  280. else $data['growth_rate'] = (int)bcmul(bcdiv($growthRate, $frontPrice, 2), 100, 0);//时间区间增长率
  281. $data['increase_time'] = abs($increase); //同比上个时间区间增长营业额
  282. $data['increase_time_status'] = $increase >= 0 ? 1 : 2; //同比上个时间区间增长营业额增长 1 减少 2
  283. } else {//订单数
  284. $frontNumber = StoreOrder:: getOrderTimeBusinessVolumeNumber($front, $start);
  285. $afterNumber = StoreOrder:: getOrderTimeBusinessVolumeNumber($start, $stop);
  286. $chartInfo = StoreOrder::chartTimeNumber($start, $stop);
  287. $data['chart'] = $chartInfo;//订单数图表数据
  288. $data['time'] = $afterNumber;//时间区间订单数
  289. $increase = (int)bcsub($afterNumber, $frontNumber, 0); //同比上个时间区间增长订单数
  290. $growthRate = abs($increase);
  291. if ($growthRate == 0) $data['growth_rate'] = 0;
  292. else if ($frontNumber == 0) $data['growth_rate'] = $growthRate;
  293. else $data['growth_rate'] = (int)bcmul(bcdiv($growthRate, $frontNumber, 2), 100, 0);//时间区间增长率
  294. $data['increase_time'] = abs($increase); //同比上个时间区间增长营业额
  295. $data['increase_time_status'] = $increase >= 0 ? 1 : 2; //同比上个时间区间增长营业额增长 1 减少 2
  296. }
  297. return app('json')->successful($data);
  298. }
  299. /**
  300. * 订单支付
  301. * @param Request $request
  302. * @return mixed
  303. * @throws \think\db\exception\DataNotFoundException
  304. * @throws \think\db\exception\ModelNotFoundException
  305. * @throws \think\exception\DbException
  306. */
  307. public function offline(Request $request)
  308. {
  309. list($orderId) = UtilService::postMore([['order_id', '']], $request, true);
  310. $orderInfo = StoreOrder::getAdminOrderDetail($orderId, 'id');
  311. if (!$orderInfo) return app('json')->fail('参数错误');
  312. $id = $orderInfo->id;
  313. $res = StoreOrder::updateOffline($id);
  314. if ($res) {
  315. event('StoreProductOrderOffline', [$id]);
  316. StoreOrderStatus::status($id, 'offline', '线下付款');
  317. return app('json')->successful('修改成功!');
  318. }
  319. return app('json')->fail(StoreOrder::getErrorInfo('修改失败!'));
  320. }
  321. /**
  322. * 订单退款
  323. * @param Request $request
  324. * @return mixed
  325. * @throws \think\Exception
  326. * @throws \think\db\exception\DataNotFoundException
  327. * @throws \think\db\exception\ModelNotFoundException
  328. * @throws \think\exception\DbException
  329. */
  330. public function refund(Request $request)
  331. {
  332. list($orderId, $price, $type) = UtilService::postMore([
  333. ['order_id', ''],
  334. ['price', 0],
  335. ['type', 1],
  336. ], $request, true);
  337. if (!strlen(trim($orderId))) return app('json')->fail('参数错误');
  338. $orderInfo = StoreOrder::getAdminOrderDetail($orderId);
  339. if (!$orderInfo) return app('json')->fail('数据不存在!');
  340. $orderInfo = $orderInfo->toArray();
  341. if ($type == 1)
  342. $data['refund_status'] = 2;
  343. else if ($type == 2)
  344. $data['refund_status'] = 0;
  345. else
  346. return app('json')->fail('退款修改状态错误');
  347. if ($orderInfo['pay_price'] == 0 || $type == 2) {
  348. StoreOrder::update($data, ['order_id' => $orderId]);
  349. return app('json')->successful('修改退款状态成功!');
  350. }
  351. if ($orderInfo['pay_price'] == $orderInfo['refund_price']) return app('json')->fail('已退完支付金额!不能再退款了');
  352. if (!$price) return app('json')->fail('请输入退款金额');
  353. $data['refund_price'] = bcadd($price, $orderInfo['refund_price'], 2);
  354. $bj = bccomp((float)$orderInfo['pay_price'], (float)$data['refund_price'], 2);
  355. if ($bj < 0) return app('json')->fail('退款金额大于支付金额,请修改退款金额');
  356. $refundData['pay_price'] = $orderInfo['pay_price'];
  357. $refundData['refund_price'] = $price;
  358. if ($orderInfo['pay_type'] == 'weixin') {
  359. if ($orderInfo['is_channel'] == 1) {// 小程序
  360. try {
  361. MiniProgramService::payOrderRefund($orderInfo['order_id'], $refundData);
  362. } catch (\Exception $e) {
  363. return app('json')->fail($e->getMessage());
  364. }
  365. } else {// 公众号
  366. try {
  367. WechatService::payOrderRefund($orderInfo['order_id'], $refundData);
  368. } catch (\Exception $e) {
  369. return app('json')->fail($e->getMessage());
  370. }
  371. }
  372. } else if ($orderInfo['pay_type'] == 'yue') {//余额
  373. StoreOrder::beginTrans();
  374. $userInfo = User::getUserInfo($orderInfo['uid'], 'now_money');
  375. if (!$userInfo) {
  376. StoreOrder::rollbackTrans();
  377. return app('json')->fail('订单用户不存在');
  378. }
  379. $res1 = User::bcInc($orderInfo['uid'], 'now_money', $price, 'uid');
  380. $res2 = $res2 = UserBill::income('商品退款', $orderInfo['uid'], 'now_money', 'pay_product_refund', $price, $orderInfo['id'], bcadd($userInfo['now_money'], $price, 2), '订单退款到余额' . floatval($price) . '元');
  381. try {
  382. OrderRepository::storeOrderYueRefund($orderInfo, $refundData);
  383. } catch (\Exception $e) {
  384. StoreOrder::rollbackTrans();
  385. return app('json')->fail($e->getMessage());
  386. }
  387. $res = $res1 && $res2;
  388. StoreOrder::checkTrans($res);
  389. if (!$res) return app('json')->fail('余额退款失败!');
  390. }
  391. $resEdit = StoreOrder::edit($data, $orderInfo['id'], 'id');
  392. if ($resEdit) {
  393. $data['type'] = $type;
  394. if ($data['type'] == 1) StorePink::setRefundPink($orderInfo['id']);
  395. try {
  396. OrderRepository::storeProductOrderRefundY($data, $orderInfo['id']);
  397. } catch (\Exception $e) {
  398. return app('json')->fail($e->getMessage());
  399. }
  400. StoreOrderStatus::status($orderInfo['id'], 'refund_price', '退款给用户' . $price . '元');
  401. //退佣金
  402. $brokerage_list = UserBill::where('category', 'now_money')
  403. ->where('type', 'brokerage')
  404. ->where('link_id', $orderId)
  405. ->where('pm', 1)
  406. ->select()->toArray();
  407. if ($brokerage_list)
  408. foreach ($brokerage_list as $item) {
  409. $usermoney = User::where('uid', $item['uid'])->value('brokerage_price');
  410. if ($item['number'] > $usermoney)
  411. $item['number'] = $usermoney;
  412. User::bcDec($item['uid'], 'brokerage_price', $item['number'], 'uid');
  413. UserBill::expend('退款退佣金', $item['uid'], 'now_money', 'brokerage', $item['number'], $orderId, bcsub($usermoney, $item['number'], 2), '订单退款扣除佣金' . floatval($item['number']) . '元');
  414. }
  415. return app('json')->successful('修改成功!');
  416. } else {
  417. StoreOrderStatus::status($orderInfo['id'], 'refund_price', '退款给用户' . $price . '元失败');
  418. return app('json')->successful('修改失败!');
  419. }
  420. }
  421. /**
  422. * 门店核销
  423. * @param Request $request
  424. */
  425. public function order_verific(Request $request)
  426. {
  427. list($verify_code, $is_confirm) = UtilService::postMore([
  428. ['verify_code', ''],
  429. ['is_confirm', 0]
  430. ], $request, true);
  431. if (!$verify_code) return app('json')->fail('缺少核销码');
  432. $orderInfo = StoreOrder::where('verify_code', $verify_code)->where('paid', 1)->where('refund_status', 0)->find();
  433. if (!$orderInfo) return app('json')->fail('核销的订单不存在或未支付或已退款');
  434. if ($orderInfo->status > 0) return app('json')->fail('订单已经核销');
  435. if ($orderInfo->combination_id && $orderInfo->pink_id) {
  436. $res = StorePink::where('id', $orderInfo->pink_id)->where('status', '<>', 2)->count();
  437. if ($res) return app('json')->fail('拼团订单暂未成功无法核销!');
  438. }
  439. if (!$is_confirm) {
  440. $orderInfo['image'] = StoreCart::getProductImage($orderInfo->cart_id);
  441. return app('json')->success($orderInfo->toArray());
  442. }
  443. StoreOrder::beginTrans();
  444. try {
  445. $uid = $request->uid();
  446. if (SystemStoreStaff::verifyStatus($uid) && ($storeStaff = SystemStoreStaff::where('uid', $uid)->field(['store_id', 'id'])->find())) {
  447. $orderInfo->store_id = $storeStaff['store_id'];
  448. $orderInfo->clerk_id = $storeStaff['id'];
  449. }
  450. $orderInfo->status = 2;
  451. if ($orderInfo->save()) {
  452. OrderRepository::storeProductOrderTakeDeliveryAdmin($orderInfo);
  453. StoreOrderStatus::status($orderInfo->id, 'take_delivery', '已核销');
  454. event('ShortMssageSend', [$orderInfo['order_id'], 'Receiving']);
  455. StoreOrder::commitTrans();
  456. return app('json')->success('核销成功');
  457. } else {
  458. StoreOrder::rollbackTrans();
  459. return app('json')->fail('核销失败');
  460. }
  461. } catch (\PDOException $e) {
  462. StoreOrder::rollbackTrans();
  463. return app('json')->fail($e->getMessage());
  464. }
  465. }
  466. }