BranchOrderServices.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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\services\order\store;
  12. use app\dao\order\StoreOrderDao;
  13. use app\services\BaseServices;
  14. use app\services\order\OtherOrderServices;
  15. use app\services\order\StoreOrderRefundServices;
  16. use app\services\order\StoreOrderServices;
  17. use app\services\store\StoreUserServices;
  18. use app\services\user\UserCardServices;
  19. use app\services\user\UserRechargeServices;
  20. use think\exception\ValidateException;
  21. /**
  22. * Class StoreOrderWapServices
  23. * @package app\services\order
  24. * @mixin StoreOrderDao
  25. */
  26. class BranchOrderServices extends BaseServices
  27. {
  28. /**
  29. * StoreOrderWapServices constructor.
  30. * @param StoreOrderDao $dao
  31. */
  32. public function __construct(StoreOrderDao $dao)
  33. {
  34. $this->dao = $dao;
  35. }
  36. /**
  37. * 获取订单数量
  38. * @param int $store_id
  39. * @param int $staff_id
  40. * @return array
  41. */
  42. public function getOrderData(int $store_id, int $staff_id = 0)
  43. {
  44. $where = ['pid' => 0, 'store_id' => $store_id, 'refund_status' => [0, 3], 'is_del' => 0, 'is_system_del' => 0];
  45. $data['order_count'] = (string)$this->dao->count($where);
  46. $where = $where + ['paid' => 1];
  47. $data['sum_price'] = (string)$this->dao->sum($where, 'pay_price', true);
  48. $countWhere = ['store_id' => $store_id];
  49. if ($staff_id) {
  50. $countWhere['staff_id'] = $staff_id;
  51. }
  52. $pid_where = ['pid' => 0];
  53. $not_pid_where = ['not_pid' => 1];
  54. $data['unpaid_count'] = (string)$this->dao->count(['status' => 0] + $countWhere + $pid_where);
  55. $data['unshipped_count'] = (string)$this->dao->count(['status' => 1] + $countWhere + $pid_where);
  56. $data['unwriteoff_count'] = (string)$this->dao->count(['status' => 5] + $countWhere + $pid_where);
  57. $data['received_count'] = (string)$this->dao->count(['status' => 2] + $countWhere + $pid_where);
  58. $data['evaluated_count'] = (string)$this->dao->count(['status' => 3] + $countWhere + $pid_where);
  59. $data['complete_count'] = (string)$this->dao->count(['status' => 4] + $countWhere + $pid_where);
  60. /** @var StoreOrderRefundServices $storeOrderRefundServices */
  61. $storeOrderRefundServices = app()->make(StoreOrderRefundServices::class);
  62. $refund_where = ['store_id' => $store_id, 'is_cancel' => 0];
  63. $data['refunding_count'] = (string)$storeOrderRefundServices->count($refund_where + ['refund_type' => [1, 2, 4, 5]]);
  64. $data['refunded_count'] = (string)$storeOrderRefundServices->count($refund_where + ['refund_type' => [3, 6]]);
  65. $data['refund_count'] = (string)$storeOrderRefundServices->count($refund_where);
  66. return $data;
  67. }
  68. /**
  69. * 订单统计详情列表
  70. * @param int $store_id
  71. * @param int $staff_id
  72. * @param int $type
  73. * @param array $time
  74. * @return array
  75. * @throws \think\db\exception\DataNotFoundException
  76. * @throws \think\db\exception\DbException
  77. * @throws \think\db\exception\ModelNotFoundException
  78. */
  79. public function time(int $store_id, int $staff_id = 0, int $type = 1, array $time = [])
  80. {
  81. if (!$time) {
  82. return [[], []];
  83. }
  84. [$start, $stop, $front, $front_stop] = $time;
  85. $order_where = ['pid' => 0, 'is_del' => 0, 'paid' => 1, 'store_id' => $store_id];
  86. if ($type != 3) {
  87. $order_where['refund_status'] = [0, 3];
  88. }
  89. if ($staff_id) $order_where['staff_id'] = $staff_id;
  90. switch ($type) {
  91. case 1://配送
  92. case 2://配送
  93. $order_where['type'] = 7;
  94. break;
  95. case 3://退款
  96. $order_where['status'] = -3;
  97. break;
  98. case 4://收银订单
  99. $order_where['type'] = 6;
  100. break;
  101. case 5://核销
  102. $order_where['type'] = 5;
  103. break;
  104. }
  105. if ($type == 2) {//数量
  106. $frontPrice = $this->dao->count($order_where + ['time' => [$front, $front_stop]]);
  107. $nowPrice = $this->dao->count($order_where + ['time' => [$start, $stop]]);
  108. } else {//金额
  109. $frontPrice = $this->dao->sum($order_where + ['time' => [$front, $front_stop]], 'pay_price', true);
  110. $nowPrice = $this->dao->sum($order_where + ['time' => [$start, $stop]], 'pay_price', true);
  111. }
  112. [$page, $limit] = $this->getPageValue();
  113. $list = $this->dao->getOrderList($order_where + ['time' => [$start, $stop]], ['id', 'order_id', 'uid', 'spread_uid', 'pay_price', 'add_time'], $page, $limit);
  114. foreach ($list as &$item) {
  115. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
  116. }
  117. return [[$nowPrice, $frontPrice], $list];
  118. }
  119. /**
  120. * 订单每月统计数据(按天分组)
  121. * @param array $where
  122. * @param array|string[] $field
  123. * @return array
  124. */
  125. public function getOrderDataPriceCount(array $where, array $field = ['sum(pay_price) as price', 'count(id) as count', 'FROM_UNIXTIME(add_time, \'%m-%d\') as time'])
  126. {
  127. [$page, $limit] = $this->getPageValue();
  128. $order_where = ['is_del' => 0, 'is_system_del' => 0, 'paid' => 1, 'refund_status' => [0, 3]];
  129. $where = array_merge($where, $order_where);
  130. return $this->dao->getOrderDataPriceCount($where, $field, $page, $limit);
  131. }
  132. /**
  133. * 获取订单列表
  134. * @param array $where
  135. * @param array $with
  136. * @param false $is_count
  137. * @return array|null
  138. * @throws \think\db\exception\DataNotFoundException
  139. * @throws \think\db\exception\DbException
  140. * @throws \think\db\exception\ModelNotFoundException
  141. */
  142. public function getStoreOrderList(array $where, array $field = ['*'], array $with = [], $is_count = false)
  143. {
  144. [$page, $limit] = $this->getPageValue();
  145. $list = $this->dao->getOrderList($where, $field, $page, $limit, $with, 'id desc');
  146. if ($is_count) {
  147. $count = $this->dao->count($where);
  148. return compact('list', 'count');
  149. }
  150. /** @var StoreOrderServices $orderServices */
  151. $orderServices = app()->make(StoreOrderServices::class);
  152. $list = $orderServices->tidyOrderList($list);
  153. foreach ($list as &$item) {
  154. $refund_num = array_sum(array_column($item['refund'], 'refund_num'));
  155. $cart_num = 0;
  156. foreach ($item['_info'] as $items) {
  157. if (isset($items['cart_info']['is_gift']) && $items['cart_info']['is_gift']) continue;
  158. $cart_num += $items['cart_info']['cart_num'];
  159. }
  160. $item['is_all_refund'] = $refund_num == $cart_num;
  161. }
  162. return $list;
  163. }
  164. /**
  165. * 取消订单
  166. * @param $id
  167. * @param int $store_id
  168. * @return bool
  169. * @throws \think\db\exception\DataNotFoundException
  170. * @throws \think\db\exception\DbException
  171. * @throws \think\db\exception\ModelNotFoundException
  172. */
  173. public function cancelOrder($id, int $store_id = 0)
  174. {
  175. $where = ['id' => $id, 'is_del' => 0, 'store_id' => $store_id];
  176. $order = $this->dao->getOne($where);
  177. if (!$order) {
  178. throw new ValidateException('没有查到此订单');
  179. }
  180. if ($order->paid) {
  181. throw new ValidateException('订单已经支付无法取消');
  182. }
  183. /** @var StoreOrderRefundServices $refundServices */
  184. $refundServices = app()->make(StoreOrderRefundServices::class);
  185. $this->transaction(function () use ($refundServices, $order) {
  186. //回退积分和优惠卷
  187. $res = $refundServices->integralAndCouponBack($order);
  188. //回退库存和销量
  189. $res = $res && $refundServices->regressionStock($order);
  190. $order->is_del = 1;
  191. if (!($res && $order->save())) {
  192. throw new ValidateException('取消订单失败');
  193. }
  194. });
  195. return true;
  196. }
  197. /**
  198. * 门店首页头部统计
  199. * @param int $store_id
  200. * @param $time
  201. * @return array
  202. */
  203. public function homeStatics(int $store_id, $time)
  204. {
  205. $data = [];
  206. $where = ['time' => $time];
  207. if ($store_id) $where['store_id'] = $store_id;
  208. $order_where = ['paid' => 1, 'pid' => 0, 'is_system_del' => 0, 'refund_status' => [0, 3]];
  209. //门店营收
  210. $data['store_income'] = $this->dao->sum($order_where + $where, 'pay_price', true);
  211. //消耗余额
  212. $data['store_use_yue'] = $this->dao->sum(['pay_type' => 'yue'] + $order_where + $where, 'pay_price', true);
  213. //收银订单
  214. $data['cashier_order_price'] = $this->dao->sum(['type' => 6] + $order_where + $where, 'pay_price', true);
  215. //分配订单
  216. $data['store_order_price'] = $this->dao->sum(['type' => 7] + $order_where + $where, 'pay_price', true);
  217. //核销订单
  218. $data['store_writeoff_order_price'] = $this->dao->sum(['shipping_type' => 2] + $order_where + $where, 'pay_price', true);
  219. /** @var StoreUserServices $storeUserServices */
  220. $storeUserServices = app()->make(StoreUserServices::class);
  221. $data['store_user_count'] = $storeUserServices->count($where);
  222. //门店成交用户数
  223. $data['store_pay_user_count'] = count(array_unique($this->dao->getColumn($order_where + $where, 'uid', '', true)));
  224. /** @var OtherOrderServices $vipOrderServices */
  225. $vipOrderServices = app()->make(OtherOrderServices::class);
  226. $data['vip_price'] = $vipOrderServices->sum(['paid' => 1, 'type' => [0, 1, 2, 4]] + $where, 'pay_price', true);
  227. /** @var UserRechargeServices $userRecharge */
  228. $userRecharge = app()->make(UserRechargeServices::class);
  229. $data['recharge_price'] = $userRecharge->sum(['paid' => 1] + $where, 'price', true);
  230. /** @var UserCardServices $userCard */
  231. $userCard = app()->make(UserCardServices::class);
  232. $data['card_count'] = $userCard->count($where + ['is_submit' => 1]);
  233. return $data;
  234. }
  235. /**
  236. * 门店首页运营统计
  237. * @param int $store_id
  238. * @param array $time
  239. * @return array
  240. */
  241. public function operateChart(int $store_id, array $time)
  242. {
  243. [$start, $end, $timeType, $timeKey] = $time;
  244. $where = [];
  245. if ($store_id == -1) {
  246. $where[] = ['store_id', '>', 0];
  247. } else {
  248. $where['store_id'] = $store_id;
  249. }
  250. $order = $this->dao->orderAddTimeList($where, [$start, $end], $timeType);
  251. /** @var StoreUserServices $storeUserServices */
  252. $storeUserServices = app()->make(StoreUserServices::class);
  253. $storeUser = $storeUserServices->userTimeList($where, [$start, $end], $timeType);
  254. $order = array_column($order, 'price', 'day');
  255. $storeUser = array_column($storeUser, 'count', 'day');
  256. $data = $series = [];
  257. $xAxis = [];
  258. foreach ($timeKey as $key) {
  259. $data['门店收款'][] = isset($order[$key]) ? floatval($order[$key]) : 0;
  260. $data['新增用户数'][] = isset($storeUser[$key]) ? floatval($storeUser[$key]) : 0;
  261. $xAxis[] = date('m-d', strtotime($key));
  262. }
  263. foreach ($data as $key => $item) {
  264. $series[] = [
  265. 'name' => $key,
  266. 'data' => $item,
  267. 'type' => 'line',
  268. 'smooth' => 'true',
  269. 'yAxisIndex' => 1,
  270. ];
  271. }
  272. return compact('xAxis', 'series');
  273. }
  274. /**
  275. * 首页交易统计
  276. * @param int $store_id
  277. * @param array $time
  278. * @return array
  279. * @throws \think\db\exception\DataNotFoundException
  280. * @throws \think\db\exception\DbException
  281. * @throws \think\db\exception\ModelNotFoundException
  282. */
  283. public function orderChart(int $store_id, array $time)
  284. {
  285. $chartdata = [];
  286. $where = ['time' => $time, 'pid' => 0];
  287. if ($store_id) $where['store_id'] = $store_id;
  288. $order_where = ['paid' => 1, 'pid' => 0, 'is_system_del' => 0, 'refund_status' => [0, 3]];
  289. $list = $this->dao->getOrderList($where + $order_where, ['id', 'order_id', 'uid', 'pay_price', 'pay_time'], 0, 10);
  290. $chartdata['order_list'] = $list;
  291. $chartdata['bing_xdata'] = ['收银订单', '充值订单', '分配订单', '核销订单', '付费会员订单'];
  292. $color = ['#2EC479', '#7F7AE5', '#FFA21B', '#46A3FF', '#FF6046'];
  293. //收银订单
  294. $pay[] = $this->dao->sum(['type' => 6] + $order_where + $where, 'pay_price', true);
  295. /** @var UserRechargeServices $userRecharge */
  296. $userRecharge = app()->make(UserRechargeServices::class);
  297. $pay[] = $userRecharge->sum(['paid' => 1] + $where, 'price', true);
  298. //分配订单
  299. $pay[] = $this->dao->sum(['type' => 7] + $order_where + $where, 'pay_price', true);
  300. //核销订单
  301. $pay[] = $this->dao->sum(['type' => 5] + $order_where + $where, 'pay_price', true);
  302. /** @var OtherOrderServices $vipOrderServices */
  303. $vipOrderServices = app()->make(OtherOrderServices::class);
  304. $pay[] = $vipOrderServices->sum(['paid' => 1, 'type' => [0, 1, 2, 4]] + $where, 'pay_price', true);
  305. foreach ($pay as $key => $item) {
  306. $bing_data[] = ['name' => $chartdata['bing_xdata'][$key], 'value' => $pay[$key], 'itemStyle' => ['color' => $color[$key]]];
  307. }
  308. $chartdata['bing_data'] = $bing_data;
  309. return $chartdata;
  310. }
  311. }