StoreOrder.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\api\server;
  12. use app\common\repositories\delivery\DeliveryStationRepository;
  13. use app\common\repositories\store\order\StoreOrderRepository;
  14. use app\common\repositories\delivery\DeliveryServiceRepository;
  15. use app\common\repositories\store\order\StoreRefundOrderRepository;
  16. use app\common\repositories\store\staff\StaffsRepository;
  17. use app\validate\merchant\OrderValidate;
  18. use app\controller\merchant\Common;
  19. use crmeb\basic\BaseController;
  20. use think\App;
  21. use think\exception\ValidateException;
  22. use think\response\Json;
  23. /**
  24. * Class StoreOrder
  25. * app\controller\api\server
  26. * 移动端客服 - 订单管理
  27. */
  28. class StoreOrder extends BaseController
  29. {
  30. public function __construct(App $app)
  31. {
  32. parent::__construct($app);
  33. }
  34. /**
  35. * 数据统计
  36. * @param $merId
  37. * @param StoreOrderRepository $repository
  38. * @return Json
  39. * @author Qinii
  40. */
  41. public function orderStatistics($merId, StoreOrderRepository $repository)
  42. {
  43. $order = $repository->OrderTitleNumber($merId, null);
  44. $order['refund'] = app()->make(StoreRefundOrderRepository::class)->getWhereCount(['is_system_del' => 0, 'mer_id' => $merId]);
  45. $common = app()->make(Common::class);
  46. $data = [];
  47. $data['today'] = $common->mainGroup('today', $merId);
  48. $data['yesterday'] = $common->mainGroup('yesterday', $merId);
  49. $data['month'] = $common->mainGroup('month', $merId);
  50. return app('json')->success(compact('order', 'data'));
  51. }
  52. /**
  53. * 数据统计 - 时间筛选
  54. * @param $merId
  55. * @param StoreOrderRepository $repository
  56. * @return Json
  57. * @author Qinii
  58. */
  59. public function orderDetail($merId, StoreOrderRepository $repository)
  60. {
  61. [$page, $limit] = $this->getPage();
  62. list($start, $stop) = $this->request->params([
  63. ['start', strtotime(date('Y-m'))],
  64. ['stop', time()],
  65. ], true);
  66. if ($start == $stop) return app('json')->fail('参数有误');
  67. if ($start > $stop) {
  68. $middle = $stop;
  69. $stop = $start;
  70. $start = $middle;
  71. }
  72. $where = $this->request->has('start') ? ['dateRange' => compact('start', 'stop')] : [];
  73. $list = $repository->orderGroupNumPage($where, $page, $limit, $merId);
  74. return app('json')->success($list);
  75. }
  76. /**
  77. * 订单列表
  78. * @param $merId
  79. * @param StoreOrderRepository $repository
  80. * @return Json
  81. * @author Qinii
  82. */
  83. public function orderList($merId, StoreOrderRepository $repository)
  84. {
  85. [$page, $limit] = $this->getPage();
  86. $where['status'] = $this->request->param('status');
  87. $where['is_verify'] = $this->request->param('is_verify');
  88. $where['search'] = $this->request->param('store_name');
  89. $where['mer_id'] = $merId;
  90. $where['is_del'] = 0;
  91. if($where['status'] == 2) $where['order_type'] = 0;
  92. return app('json')->success($repository->merchantGetList($where, $page, $limit));
  93. }
  94. /**
  95. * 订单详情
  96. * @param $merId
  97. * @param $id
  98. * @param StoreOrderRepository $repository
  99. * @return Json
  100. * @author Qinii
  101. */
  102. public function order($merId, $id, StoreOrderRepository $repository)
  103. {
  104. $detail = $repository->getDetail($id);
  105. if (!$detail)
  106. return app('json')->fail('订单不存在');
  107. if ($detail['mer_id'] != $merId)
  108. return app('json')->fail('没有权限');
  109. return app('json')->success($detail->toArray());
  110. }
  111. /**
  112. * 检查用户是否有权限
  113. * @param $merId
  114. * @param $id
  115. * @return void
  116. * @author Qinii
  117. */
  118. protected function checkOrderAuth($merId, $id)
  119. {
  120. if (!app()->make(StoreOrderRepository::class)->existsWhere(['mer_id' => $merId, 'order_id' => $id]))
  121. throw new ValidateException('没有权限');
  122. }
  123. /**
  124. * 备注
  125. * @param $merId
  126. * @param $id
  127. * @param StoreOrderRepository $repository
  128. * @return Json
  129. * @author Qinii
  130. */
  131. public function mark($merId, $id, StoreOrderRepository $repository)
  132. {
  133. $this->checkOrderAuth($merId, $id);
  134. $data = $this->request->params(['remark']);
  135. $repository->update($id, $data);
  136. return app('json')->success('备注成功');
  137. }
  138. /**
  139. * 修改价格
  140. * @param $merId
  141. * @param $id
  142. * @param StoreOrderRepository $repository
  143. * @return Json
  144. * @author Qinii
  145. */
  146. public function price($merId, $id, StoreOrderRepository $repository)
  147. {
  148. $this->checkOrderAuth($merId, $id);
  149. $data = $this->request->params(['total_price', 'pay_postage']);
  150. if ($data['total_price'] < 0 || $data['pay_postage'] < 0)
  151. return app('json')->fail('金额不可未负数');
  152. if (!$repository->merStatusExists((int)$id, $merId))
  153. return app('json')->fail('订单信息或状态错误');
  154. $repository->eidt($id, $data);
  155. return app('json')->success('修改成功');
  156. }
  157. /**
  158. * 发货操作
  159. * @param $merId
  160. * @param $id
  161. * @param StoreOrderRepository $repository
  162. * @return Json
  163. * @author Qinii
  164. * @day 6/1/22
  165. */
  166. public function delivery($merId, $id, StoreOrderRepository $repository)
  167. {
  168. $this->checkOrderAuth($merId, $id);
  169. $type = $this->request->param('delivery_type');
  170. $split = $this->request->params(['is_split',['split',[]]]);
  171. if (!$repository->merDeliveryExists($id, $merId))
  172. return app('json')->fail('订单信息或状态错误');
  173. switch ($type)
  174. {
  175. case 2:
  176. $data = $this->request->params(['delivery_type', 'delivery_name', 'delivery_id', 'remark',]);
  177. if (!$data['delivery_type'] || !$data['delivery_name'] || !$data['delivery_id'])
  178. return app('json')->fail('填写配送信息');
  179. $ser = app()->make(DeliveryServiceRepository::class)->get($data['delivery_name']);
  180. $data['delivery_name'] = $ser['name'] ?? $data['delivery_name'];
  181. $method = 'delivery';
  182. break;
  183. case 3: //虚拟发货
  184. $data = $this->request->params([
  185. 'delivery_type',
  186. 'remark',
  187. ]);
  188. $data['delivery_name'] = '';
  189. $data['delivery_id'] = '';
  190. $method = 'delivery';
  191. break;
  192. case 4: //电子面单
  193. if (!systemConfig('crmeb_serve_dump'))
  194. return app('json')->fail('电子面单功能未开启');
  195. $data = $this->request->params([
  196. 'delivery_type',
  197. 'delivery_name',
  198. 'from_name',
  199. 'from_tel',
  200. 'from_addr',
  201. 'temp_id',
  202. 'remark',
  203. ]);
  204. if (!$data['from_name'] ||
  205. !$data['delivery_name'] ||
  206. !$data['from_tel'] ||
  207. !$data['from_addr'] ||
  208. !$data['temp_id']
  209. )
  210. return app('json')->fail('填写配送信息');
  211. $method = 'dump';
  212. break;
  213. case 5: //同城配送
  214. if (systemConfig('delivery_status') != 1)
  215. return app('json')->fail('未开启同城配送');
  216. $data = $this->request->params([
  217. 'delivery_type',
  218. 'station_id',
  219. 'mark',
  220. ['cargo_weight',0],
  221. 'remark',
  222. ]);
  223. if ($data['cargo_weight'] < 0) return app('json')->fail('包裹重量能为负数');
  224. if (!$data['station_id']) return app('json')->fail('请选择门店');
  225. $method = 'cityDelivery';
  226. break;
  227. default: //快递
  228. $data = $this->request->params([
  229. 'delivery_type',
  230. 'delivery_type',
  231. 'delivery_name',
  232. 'delivery_id',
  233. 'remark',
  234. ]);
  235. if (!$data['delivery_type'] || !$data['delivery_name'] || !$data['delivery_id'])
  236. return app('json')->fail('填写配送信息');
  237. $method = 'delivery';
  238. break;
  239. }
  240. $repository->runDelivery($id,$merId, $data, $split, $method, $this->request->serviceInfo()->service_id);
  241. return app('json')->success('发货成功');
  242. }
  243. /**
  244. * 订单金额统计 图表
  245. * @param $merId
  246. * @param $id
  247. * @param StoreOrderRepository $repository
  248. * @return Json
  249. * @author Qinii
  250. */
  251. public function payPrice($merId, StoreOrderRepository $repository)
  252. {
  253. list($start, $stop, $month) = $this->request->params([
  254. ['start', strtotime(date('Y-m'))],
  255. ['stop', time()],
  256. 'month'
  257. ], true);
  258. if ($month) {
  259. $start = date('Y/m/d', strtotime(getStartModelTime('month')));
  260. $stop = date('Y/m/d H:i:s', strtotime('+ 1day'));
  261. $front = date('Y/m/d', strtotime('first Day of this month', strtotime('-1 day', strtotime('first Day of this month'))));
  262. $end = date('Y/m/d H:i:s', strtotime($start . ' -1 second'));
  263. } else {
  264. if ($start == $stop) return app('json')->fail('参数有误');
  265. if ($start > $stop) {
  266. $middle = $stop;
  267. $stop = $start;
  268. $start = $middle;
  269. }
  270. $space = bcsub($stop, $start, 0);//间隔时间段
  271. $front = bcsub($start, $space, 0);//第一个时间段
  272. $front = date('Y/m/d H:i:s', $front);
  273. $start = date('Y/m/d H:i:s', $start);
  274. $stop = date('Y/m/d H:i:s', $stop);
  275. $end = date('Y/m/d H:i:s', strtotime($start . ' -1 second'));
  276. }
  277. $frontPrice = $repository->dateOrderPrice($front . '-' . $end, $merId);
  278. $afterPrice = $repository->dateOrderPrice($start . '-' . date('Y/m/d H:i:s', strtotime($stop . '-1 second')), $merId);
  279. $chartInfo = $repository->chartTimePrice($start, date('Y/m/d H:i:s', strtotime($stop . '-1 second')), $merId);
  280. $data['chart'] = $chartInfo;//营业额图表数据
  281. $data['time'] = $afterPrice;//时间区间营业额
  282. $increase = (float)bcsub((string)$afterPrice, (string)$frontPrice, 2); //同比上个时间区间增长营业额
  283. $growthRate = abs($increase);
  284. if ($growthRate == 0) $data['growth_rate'] = 0;
  285. else if ($frontPrice == 0) $data['growth_rate'] = bcmul($growthRate, 100, 0);
  286. else $data['growth_rate'] = (int)bcmul((string)bcdiv((string)$growthRate, (string)$frontPrice, 2), '100', 0);//时间区间增长率
  287. $data['increase_time'] = abs($increase); //同比上个时间区间增长营业额
  288. $data['increase_time_status'] = $increase >= 0 ? 1 : 2; //同比上个时间区间增长营业额增长 1 减少 2
  289. return app('json')->success($data);
  290. }
  291. /**
  292. * 订单数量统计 图表
  293. * @param StoreOrderRepository $repository
  294. * @return Json
  295. * @author xaboy
  296. * @day 2020/8/27
  297. */
  298. public function payNumber($merId, StoreOrderRepository $repository)
  299. {
  300. list($start, $stop, $month) = $this->request->params([
  301. ['start', strtotime(date('Y-m'))],
  302. ['stop', time()],
  303. 'month'
  304. ], true);
  305. if ($month) {
  306. $start = date('Y/m/d', strtotime(getStartModelTime('month')));
  307. $stop = date('Y/m/d H:i:s', strtotime('+ 1day'));
  308. $front = date('Y/m/d', strtotime('first Day of this month', strtotime('-1 day', strtotime('first Day of this month'))));
  309. $end = date('Y/m/d H:i:s', strtotime($start . ' -1 second'));
  310. } else {
  311. if ($start == $stop) return app('json')->fail('参数有误');
  312. if ($start > $stop) {
  313. $middle = $stop;
  314. $stop = $start;
  315. $start = $middle;
  316. }
  317. $space = bcsub($stop, $start, 0);//间隔时间段
  318. $front = bcsub($start, $space, 0);//第一个时间段
  319. $front = date('Y/m/d H:i:s', $front);
  320. $start = date('Y/m/d H:i:s', $start);
  321. $stop = date('Y/m/d H:i:s', $stop);
  322. $end = date('Y/m/d H:i:s', strtotime($start . ' -1 second'));
  323. }
  324. $frontNumber = $repository->dateOrderNum($front . '-' . $end, $merId);
  325. $afterNumber = $repository->dateOrderNum($start . '-' . date('Y/m/d H:i:s', strtotime($stop . '-1 second')), $merId);
  326. $chartInfo = $repository->chartTimeNum($start . '-' . date('Y/m/d H:i:s', strtotime($stop . '-1 second')), $merId);
  327. $data['chart'] = $chartInfo;//订单数图表数据
  328. $data['time'] = $afterNumber;//时间区间订单数
  329. $increase = $afterNumber - $frontNumber; //同比上个时间区间增长订单数
  330. $growthRate = abs($increase);
  331. if ($growthRate == 0) $data['growth_rate'] = 0;
  332. else if ($frontNumber == 0) $data['growth_rate'] = bcmul($growthRate, 100, 0);
  333. else $data['growth_rate'] = (int)bcmul((string)bcdiv((string)$growthRate, (string)$frontNumber, 2), '100', 0);//时间区间增长率
  334. $data['increase_time'] = abs($increase); //同比上个时间区间增长营业额
  335. $data['increase_time_status'] = $increase >= 0 ? 1 : 2; //同比上个时间区间增长营业额增长 1 减少 2
  336. return app('json')->success($data);
  337. }
  338. /**
  339. * 获取商户配置
  340. * @param $merId
  341. * @return Json
  342. * @author Qinii
  343. */
  344. public function getFormData($merId)
  345. {
  346. $config = [
  347. 'mer_from_com',
  348. 'mer_from_name',
  349. 'mer_from_tel',
  350. 'mer_from_addr',
  351. 'mer_config_siid',
  352. 'mer_config_temp_id'
  353. ];
  354. $data = merchantConfig($merId,$config);
  355. return app('json')->success($data);
  356. }
  357. /**
  358. * 获取配送配置
  359. * @return Json
  360. * @author Qinii
  361. */
  362. public function getDeliveryConfig()
  363. {
  364. $data = systemConfig(['crmeb_serve_dump','delivery_status']);
  365. return app('json')->success($data);
  366. }
  367. /**
  368. * 获取同城配送配置
  369. * @return Json
  370. * @author Qinii
  371. */
  372. public function getDeliveryOptions($merId, DeliveryStationRepository $repository)
  373. {
  374. if (!systemConfig('delivery_status')) {
  375. return app('json')->success([]);
  376. }
  377. $where = [
  378. 'status' => 1,
  379. 'mer_id' => $merId,
  380. 'type' => systemConfig('delivery_type'),
  381. ];
  382. $data = $repository->getOptions($where)->toArray();
  383. $type = systemConfig('delivery_type') == 1 ? 'UU' : '达达';
  384. if (empty($data)) return app('json')->fail('请前往商户后台添加'.$type.'发货点');
  385. return app('json')->success($data);
  386. }
  387. /**
  388. * 订单核销
  389. * @param $merId
  390. * @param $id
  391. * @param StoreOrderRepository $orderRepository
  392. * @return Json
  393. * @author Qinii
  394. */
  395. public function verify($merId,$id,StoreOrderRepository $orderRepository)
  396. {
  397. $order = $orderRepository->getWhere(['order_id' => $id,'mer_id' => $merId]);
  398. if (!$order) return app('json')->fail('数据不存在');
  399. $data = $this->request->params(['verify_code','data']);
  400. // 根据订单ID、商家ID、验证码和订单类型查询订单,并连带查询订单产品信息
  401. $order = $orderRepository->getWhere(['order_id' => $id, 'mer_id' => $merId, 'verify_code' => $data['verify_code'], 'order_type' => 1], '*', ['orderProduct']);
  402. // 如果订单不存在,则抛出验证异常
  403. if (!$order) return app('json')->fail('订单不存在');
  404. // 如果订单未支付,则抛出验证异常
  405. if (!$order->paid) return app('json')->fail('订单未支付');
  406. // 如果订单已全部核销,则抛出验证异常
  407. if ($order['status']) return app('json')->fail('订单已全部核销,请勿重复操作');
  408. $orderRepository->verifyOrder($order, $data, $this->request->serviceInfo()->service_id);
  409. return app('json')->success('订单核销成功');
  410. }
  411. public function options($merId)
  412. {
  413. $where = [
  414. 'status' => 1,
  415. 'mer_id' => $merId,
  416. ];
  417. $data = app()->make(DeliveryServiceRepository::class)->getOptions($where);
  418. return app('json')->success($data);
  419. }
  420. /**
  421. * 预约订单派单
  422. *
  423. * @param integer $id
  424. * @return json
  425. */
  426. public function reservationDispatch(int $merId, int $id, StoreOrderRepository $repository)
  427. {
  428. $params = $this->request->params(['staffs_id']);
  429. if (!$params['staffs_id']) {
  430. return app('json')->fail('请选择服务人员');
  431. }
  432. $res = $repository->reservationDispatch($id, $merId, $params, $this->request->serviceInfo()->service_id);
  433. if (!$res) {
  434. return app('json')->fail('派单失败');
  435. }
  436. return app('json')->success('派单成功');
  437. }
  438. /**
  439. * 派单员工列表
  440. *
  441. * @param integer $merId
  442. * @param StaffsRepository $repository
  443. * @return void
  444. */
  445. public function staffList(int $merId, StaffsRepository $repository)
  446. {
  447. $where = $this->request->params(['keyword']);
  448. $where['mer_id'] = $merId;
  449. $where['status'] = 1;
  450. [$page, $limit] = $this->getPage();
  451. $list = $repository->getList($where, $page, $limit);
  452. return app('json')->success($list);
  453. }
  454. /**
  455. * 预约订单改派
  456. *
  457. * @param integer $id
  458. * @return json
  459. */
  460. public function reservationUpdateDispatch(int $merId, int $id, StoreOrderRepository $repository)
  461. {
  462. $params = $this->request->params(['staffs_id']);
  463. if (!$params['staffs_id']) {
  464. return app('json')->fail('请选择服务人员');
  465. }
  466. $res = $repository->reservationUpdateDispatch($id, $merId, $params, $this->request->serviceInfo()->service_id);
  467. if (!$res) {
  468. return app('json')->fail('改派失败');
  469. }
  470. return app('json')->success('改派成功');
  471. }
  472. /**
  473. * 预约订单改期
  474. *
  475. * @param [type] $id
  476. * @return void
  477. */
  478. public function reservationReschedule(int $merId, int $id, StoreOrderRepository $repository)
  479. {
  480. $params = $this->request->params(
  481. [
  482. 'order_type',
  483. 'reservation_date',
  484. 'real_name',
  485. 'user_phone',
  486. 'user_address',
  487. 'order_extend',
  488. 'part_start',
  489. 'part_end'
  490. ]
  491. );
  492. $validate = app()->make(OrderValidate::class);
  493. if (!$validate->sceneReservationReschedule($params)) {
  494. return app('json')->fail($validate->getError());
  495. }
  496. $res = $repository->reservationReschedule($id, $merId, $params, $this->request->serviceInfo()->service_id);
  497. if (!$res) {
  498. return app('json')->fail('改约失败');
  499. }
  500. return app('json')->success('改约成功');
  501. }
  502. /**
  503. * 预约订单核销
  504. *
  505. * @param integer $id
  506. * @return void
  507. */
  508. public function reservationVerify(int $merId, int $id, StoreOrderRepository $repository)
  509. {
  510. $res = $repository->reservationVerify($id, $merId, $this->request->serviceInfo()->service_id);
  511. if (!$res) {
  512. return app('json')->fail('核销失败');
  513. }
  514. return app('json')->success('核销成功');
  515. }
  516. /**
  517. * 预约配置
  518. *
  519. * @return json
  520. */
  521. public function reservationConfig(int $merId)
  522. {
  523. if (!$merId) {
  524. throw new ValidateException('参数错误');
  525. }
  526. $config = merchantConfig($merId, ['enable_assigned', 'enable_checkin', 'checkin_radius', 'enable_trace', 'trace_form_id']);
  527. $config['enable_assigned'] = $config['enable_assigned'] ?: 0;
  528. $config['enable_checkin'] = $config['enable_checkin'] ?: 0;
  529. $config['checkin_radius'] = $config['checkin_radius'] ?: 0;
  530. $config['enable_trace'] = $config['enable_trace'] ?: 0;
  531. $config['trace_form_id'] = $config['trace_form_id'] ?: 0;
  532. return app('json')->success($config);
  533. }
  534. }