StoreOrder.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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\store\order\StoreRefundOrderRepository;
  15. use app\common\repositories\store\service\StoreServiceRepository;
  16. use app\controller\merchant\Common;
  17. use crmeb\basic\BaseController;
  18. use think\App;
  19. use think\exception\HttpResponseException;
  20. use think\exception\ValidateException;
  21. use think\facade\Db;
  22. use think\response\Json;
  23. class StoreOrder extends BaseController
  24. {
  25. public function __construct(App $app)
  26. {
  27. parent::__construct($app);
  28. }
  29. public function orderStatistics($merId, StoreOrderRepository $repository)
  30. {
  31. $order = $repository->OrderTitleNumber($merId, null);
  32. $order['refund'] = app()->make(StoreRefundOrderRepository::class)->getWhereCount(['is_system_del' => 0, 'mer_id' => $merId]);
  33. $common = app()->make(Common::class);
  34. $data = [];
  35. $data['today'] = $common->mainGroup('today', $merId);
  36. $data['yesterday'] = $common->mainGroup('yesterday', $merId);
  37. $data['month'] = $common->mainGroup('month', $merId);
  38. return app('json')->success(compact('order', 'data'));
  39. }
  40. public function orderDetail($merId, StoreOrderRepository $repository)
  41. {
  42. [$page, $limit] = $this->getPage();
  43. list($start, $stop) = $this->request->params([
  44. ['start', strtotime(date('Y-m'))],
  45. ['stop', time()],
  46. ], true);
  47. if ($start == $stop) return app('json')->fail('参数有误');
  48. if ($start > $stop) {
  49. $middle = $stop;
  50. $stop = $start;
  51. $start = $middle;
  52. }
  53. $where = $this->request->has('start') ? ['dateRange' => compact('start', 'stop')] : [];
  54. $list = $repository->orderGroupNumPage($where, $page, $limit, $merId);
  55. return app('json')->success($list);
  56. }
  57. public function orderList($merId, StoreOrderRepository $repository)
  58. {
  59. [$page, $limit] = $this->getPage();
  60. $where['status'] = $this->request->param('status');
  61. $where['is_verify'] = $this->request->param('is_verify');
  62. $where['search'] = $this->request->param('store_name');
  63. $where['mer_id'] = $merId;
  64. $where['is_del'] = 0;
  65. if($where['status'] == 2) $where['order_type'] = 0;
  66. return app('json')->success($repository->merchantGetList($where, $page, $limit));
  67. }
  68. public function order($merId, $id, StoreOrderRepository $repository)
  69. {
  70. $detail = $repository->getDetail($id);
  71. if (!$detail)
  72. return app('json')->fail('订单不存在');
  73. if ($detail['mer_id'] != $merId)
  74. return app('json')->fail('没有权限');
  75. return app('json')->success($detail->toArray());
  76. }
  77. protected function checkOrderAuth($merId, $id)
  78. {
  79. if (!app()->make(StoreOrderRepository::class)->existsWhere(['mer_id' => $merId, 'order_id' => $id]))
  80. throw new ValidateException('没有权限');
  81. }
  82. public function mark($merId, $id, StoreOrderRepository $repository)
  83. {
  84. $this->checkOrderAuth($merId, $id);
  85. $data = $this->request->params(['remark']);
  86. $repository->update($id, $data);
  87. return app('json')->success('备注成功');
  88. }
  89. public function price($merId, $id, StoreOrderRepository $repository)
  90. {
  91. $this->checkOrderAuth($merId, $id);
  92. $data = $this->request->params(['total_price', 'pay_postage']);
  93. if ($data['total_price'] < 0 || $data['pay_postage'] < 0)
  94. return app('json')->fail('金额不可未负数');
  95. if (!$repository->merStatusExists((int)$id, $merId))
  96. return app('json')->fail('订单信息或状态错误');
  97. $repository->eidt($id, $data);
  98. return app('json')->success('修改成功');
  99. }
  100. /**
  101. * TODO 发货操作
  102. * @param $merId
  103. * @param $id
  104. * @param StoreOrderRepository $repository
  105. * @return Json
  106. * @author Qinii
  107. * @day 6/1/22
  108. */
  109. public function delivery($merId, $id, StoreOrderRepository $repository)
  110. {
  111. $this->checkOrderAuth($merId, $id);
  112. $type = $this->request->param('delivery_type');
  113. $split = $this->request->params(['is_split',['split',[]]]);
  114. if (!$repository->merDeliveryExists($id, $merId))
  115. return app('json')->fail('订单信息或状态错误');
  116. switch ($type)
  117. {
  118. case 3: //虚拟发货
  119. $data = $this->request->params([
  120. 'delivery_type',
  121. 'remark',
  122. ]);
  123. $data['delivery_name'] = '';
  124. $data['delivery_id'] = '';
  125. $method = 'delivery';
  126. break;
  127. case 4: //电子面单
  128. if (!systemConfig('crmeb_serve_dump'))
  129. return app('json')->fail('电子面单功能未开启');
  130. $data = $this->request->params([
  131. 'delivery_type',
  132. 'delivery_name',
  133. 'from_name',
  134. 'from_tel',
  135. 'from_addr',
  136. 'temp_id',
  137. 'remark',
  138. ]);
  139. if (!$data['from_name'] ||
  140. !$data['delivery_name'] ||
  141. !$data['from_tel'] ||
  142. !$data['from_addr'] ||
  143. !$data['temp_id']
  144. )
  145. return app('json')->fail('填写配送信息');
  146. $method = 'dump';
  147. break;
  148. case 5: //同城配送
  149. if (systemConfig('delivery_status') != 1)
  150. return app('json')->fail('未开启同城配送');
  151. $data = $this->request->params([
  152. 'delivery_type',
  153. 'station_id',
  154. 'mark',
  155. ['cargo_weight',0],
  156. 'remark',
  157. ]);
  158. if ($data['cargo_weight'] < 0) return app('json')->fail('包裹重量能为负数');
  159. if (!$data['station_id']) return app('json')->fail('请选择门店');
  160. $method = 'cityDelivery';
  161. break;
  162. default: //快递
  163. $data = $this->request->params([
  164. 'delivery_type',
  165. 'delivery_type',
  166. 'delivery_name',
  167. 'delivery_id',
  168. 'remark',
  169. ]);
  170. if (!$data['delivery_type'] || !$data['delivery_name'] || !$data['delivery_id'])
  171. return app('json')->fail('填写配送信息');
  172. $method = 'delivery';
  173. break;
  174. }
  175. $repository->runDelivery($id,$merId, $data, $split, $method);
  176. return app('json')->success('发货成功');
  177. }
  178. public function payPrice($merId, StoreOrderRepository $repository)
  179. {
  180. list($start, $stop, $month) = $this->request->params([
  181. ['start', strtotime(date('Y-m'))],
  182. ['stop', time()],
  183. 'month'
  184. ], true);
  185. if ($month) {
  186. $start = date('Y/m/d', strtotime(getStartModelTime('month')));
  187. $stop = date('Y/m/d H:i:s', strtotime('+ 1day'));
  188. $front = date('Y/m/d', strtotime('first Day of this month', strtotime('-1 day', strtotime('first Day of this month'))));
  189. $end = date('Y/m/d H:i:s', strtotime($start . ' -1 second'));
  190. } else {
  191. if ($start == $stop) return app('json')->fail('参数有误');
  192. if ($start > $stop) {
  193. $middle = $stop;
  194. $stop = $start;
  195. $start = $middle;
  196. }
  197. $space = bcsub($stop, $start, 0);//间隔时间段
  198. $front = bcsub($start, $space, 0);//第一个时间段
  199. $front = date('Y/m/d H:i:s', $front);
  200. $start = date('Y/m/d H:i:s', $start);
  201. $stop = date('Y/m/d H:i:s', $stop);
  202. $end = date('Y/m/d H:i:s', strtotime($start . ' -1 second'));
  203. }
  204. $frontPrice = $repository->dateOrderPrice($front . '-' . $end, $merId);
  205. $afterPrice = $repository->dateOrderPrice($start . '-' . date('Y/m/d H:i:s', strtotime($stop . '-1 second')), $merId);
  206. $chartInfo = $repository->chartTimePrice($start, date('Y/m/d H:i:s', strtotime($stop . '-1 second')), $merId);
  207. $data['chart'] = $chartInfo;//营业额图表数据
  208. $data['time'] = $afterPrice;//时间区间营业额
  209. $increase = (float)bcsub((string)$afterPrice, (string)$frontPrice, 2); //同比上个时间区间增长营业额
  210. $growthRate = abs($increase);
  211. if ($growthRate == 0) $data['growth_rate'] = 0;
  212. else if ($frontPrice == 0) $data['growth_rate'] = bcmul($growthRate, 100, 0);
  213. else $data['growth_rate'] = (int)bcmul((string)bcdiv((string)$growthRate, (string)$frontPrice, 2), '100', 0);//时间区间增长率
  214. $data['increase_time'] = abs($increase); //同比上个时间区间增长营业额
  215. $data['increase_time_status'] = $increase >= 0 ? 1 : 2; //同比上个时间区间增长营业额增长 1 减少 2
  216. return app('json')->success($data);
  217. }
  218. /**
  219. * @param StoreOrderRepository $repository
  220. * @return Json
  221. * @author xaboy
  222. * @day 2020/8/27
  223. */
  224. public function payNumber($merId, StoreOrderRepository $repository)
  225. {
  226. list($start, $stop, $month) = $this->request->params([
  227. ['start', strtotime(date('Y-m'))],
  228. ['stop', time()],
  229. 'month'
  230. ], true);
  231. if ($month) {
  232. $start = date('Y/m/d', strtotime(getStartModelTime('month')));
  233. $stop = date('Y/m/d H:i:s', strtotime('+ 1day'));
  234. $front = date('Y/m/d', strtotime('first Day of this month', strtotime('-1 day', strtotime('first Day of this month'))));
  235. $end = date('Y/m/d H:i:s', strtotime($start . ' -1 second'));
  236. } else {
  237. if ($start == $stop) return app('json')->fail('参数有误');
  238. if ($start > $stop) {
  239. $middle = $stop;
  240. $stop = $start;
  241. $start = $middle;
  242. }
  243. $space = bcsub($stop, $start, 0);//间隔时间段
  244. $front = bcsub($start, $space, 0);//第一个时间段
  245. $front = date('Y/m/d H:i:s', $front);
  246. $start = date('Y/m/d H:i:s', $start);
  247. $stop = date('Y/m/d H:i:s', $stop);
  248. $end = date('Y/m/d H:i:s', strtotime($start . ' -1 second'));
  249. }
  250. $frontNumber = $repository->dateOrderNum($front . '-' . $end, $merId);
  251. $afterNumber = $repository->dateOrderNum($start . '-' . date('Y/m/d H:i:s', strtotime($stop . '-1 second')), $merId);
  252. $chartInfo = $repository->chartTimeNum($start . '-' . date('Y/m/d H:i:s', strtotime($stop . '-1 second')), $merId);
  253. $data['chart'] = $chartInfo;//订单数图表数据
  254. $data['time'] = $afterNumber;//时间区间订单数
  255. $increase = $afterNumber - $frontNumber; //同比上个时间区间增长订单数
  256. $growthRate = abs($increase);
  257. if ($growthRate == 0) $data['growth_rate'] = 0;
  258. else if ($frontNumber == 0) $data['growth_rate'] = bcmul($growthRate, 100, 0);
  259. else $data['growth_rate'] = (int)bcmul((string)bcdiv((string)$growthRate, (string)$frontNumber, 2), '100', 0);//时间区间增长率
  260. $data['increase_time'] = abs($increase); //同比上个时间区间增长营业额
  261. $data['increase_time_status'] = $increase >= 0 ? 1 : 2; //同比上个时间区间增长营业额增长 1 减少 2
  262. return app('json')->success($data);
  263. }
  264. public function getFormData($merId)
  265. {
  266. $config = [
  267. 'mer_from_com',
  268. 'mer_from_name',
  269. 'mer_from_tel',
  270. 'mer_from_addr',
  271. 'mer_config_siid',
  272. 'mer_config_temp_id'
  273. ];
  274. $data = merchantConfig($merId,$config);
  275. return app('json')->success($data);
  276. }
  277. public function getDeliveryConfig()
  278. {
  279. $data = systemConfig(['crmeb_serve_dump','delivery_status']);
  280. return app('json')->success($data);
  281. }
  282. public function getDeliveryOptions($merId, DeliveryStationRepository $repository)
  283. {
  284. if (!systemConfig('delivery_status')) {
  285. return app('json')->success([]);
  286. }
  287. $where = [
  288. 'status' => 1,
  289. 'mer_id' => $merId,
  290. 'type' => systemConfig('delivery_type'),
  291. ];
  292. $data = $repository->getOptions($where)->toArray();
  293. $type = systemConfig('delivery_type') == 1 ? 'UU' : '达达';
  294. if (empty($data)) return app('json')->fail('请前往商户后台添加'.$type.'发货点');
  295. return app('json')->success($data);
  296. }
  297. public function verify($merId,$id,StoreOrderRepository $orderRepository)
  298. {
  299. $order = $orderRepository->getWhere(['order_id' => $id,'mer_id' => $merId]);
  300. if (!$order) return app('json')->fail('数据不存在');
  301. $orderRepository->verifyOrder($order->verify_code, $merId, 0);
  302. return app('json')->success('订单核销成功');
  303. }
  304. }