StoreOrderController.php 23 KB

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