StoreFinanceFlowServices.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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\store\finance;
  12. use app\dao\store\finance\StoreFinanceFlowDao;
  13. use app\dao\store\StoreUserDao;
  14. use app\services\BaseServices;
  15. use app\services\order\StoreOrderCreateServices;
  16. use app\services\order\StoreOrderServices;
  17. use app\services\pay\PayServices;
  18. use app\services\store\SystemStoreStaffServices;
  19. /**
  20. * 门店流水
  21. * Class StoreExtractServices
  22. * @package app\services\store\finance
  23. * @mixin StoreFinanceFlowDao
  24. */
  25. class StoreFinanceFlowServices extends BaseServices
  26. {
  27. /**
  28. * 支付类型
  29. * @var string[]
  30. */
  31. public $pay_type = ['weixin' => '微信支付', 'yue' => '余额支付', 'offline' => '线下支付', 'alipay' => '支付宝支付', 'cash' => '现金支付', 'automatic' => '自动转账', 'store' => '微信支付'];
  32. /**
  33. * 交易类型
  34. * @var string[]
  35. */
  36. public $type = [
  37. 1 => '支付订单',
  38. 2 => '支付订单',
  39. 3 => '订单手续费',
  40. 4 => '退款订单',
  41. 5 => '充值返点',
  42. 6 => '付费会员返点',
  43. 7 => '充值订单',
  44. 8 => '付费订单',
  45. 9 => '收银订单',
  46. 10 => '核销订单',
  47. 11 => '分配订单',
  48. 12 => '配送订单',
  49. 13 => '同城配送订单',
  50. ];
  51. /**
  52. * 构造方法
  53. * StoreUser constructor.
  54. * @param StoreUserDao $dao
  55. */
  56. public function __construct(StoreFinanceFlowDao $dao)
  57. {
  58. $this->dao = $dao;
  59. }
  60. /**
  61. * 显示资源列表
  62. * @param array $where
  63. * @return array
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\DbException
  66. * @throws \think\db\exception\ModelNotFoundException
  67. */
  68. public function getList(array $where)
  69. {
  70. [$page, $limit] = $this->getPageValue();
  71. $list = $this->dao->getList($where, '*', $page, $limit, ['user', 'systemStoreStaff', 'systemStore' => function ($query) {
  72. $query->field('id,name')->bind(['store_name' => 'name']);
  73. }]);
  74. foreach ($list as &$item) {
  75. $item['type_name'] = isset($this->type[$item['type']]) ? $this->type[$item['type']] : '其他类型';
  76. $item['pay_type_name'] = isset($this->pay_type[$item['pay_type']]) ? $this->pay_type[$item['pay_type']] : '其他方式';
  77. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
  78. $item['trade_time'] = $item['trade_time'] ? date('Y-m-d H:i:s', $item['trade_time']) : $item['add_time'];
  79. $item['user_nickname'] = $item['user_nickname'] ?: '游客';
  80. }
  81. $count = $this->dao->getCount($where);
  82. return compact('list', 'count');
  83. }
  84. /**
  85. * 门店账单
  86. * @param $where
  87. * @throws \think\db\exception\DataNotFoundException
  88. * @throws \think\db\exception\DbException
  89. * @throws \think\db\exception\ModelNotFoundException
  90. */
  91. public function getFundRecord($where)
  92. {
  93. [$page, $limit] = $this->getPageValue();
  94. $where['is_del'] = 0;
  95. $data = $this->dao->getFundRecord($where, $page, $limit);
  96. $i = 1;
  97. foreach ($data['list'] as &$item) {
  98. $item['id'] = $i;
  99. $i++;
  100. $item['entry_num'] = bcsub($item['income_num'], $item['exp_num'], 2);
  101. switch ($where['timeType']) {
  102. case "day" :
  103. $item['title'] = "日账单";
  104. $item['add_time'] = date('Y-m-d', $item['add_time']);
  105. break;
  106. case "week" :
  107. $item['title'] = "周账单";
  108. $item['add_time'] = '第' . $item['day'] . '周(' . date('m', $item['add_time']) . '月)';
  109. break;
  110. case "month" :
  111. $item['title'] = "月账单";
  112. $item['add_time'] = date('Y-m', $item['add_time']);
  113. break;
  114. }
  115. }
  116. return $data;
  117. }
  118. /**
  119. * 店员交易统计头部数据
  120. * @param $where
  121. * @return mixed
  122. */
  123. public function getStatisticsHeader($where)
  124. {
  125. $data = [];
  126. $data['legend'] = '业绩统计';
  127. $color = ['#2EC479', '#7F7AE5', '#FFA21B', '#46A3FF', '#FF6046', '#5cadff', '#b37feb', '#19be6b', '#ff9900'];
  128. $data['series'] = [];
  129. $list = $this->dao->getStatisticsHeader($where, 'staff_id', 'pay_price');
  130. if ($list) {
  131. $lists = [];
  132. $i = 0;
  133. foreach ($list as $item) {
  134. $data['series'][$i] = $item['total_number'] ?? 0;
  135. $data['xAxis'][$i] = $item['staff_name'] ?? '';
  136. if ($i < 5) {
  137. $lists[$i] = $item;
  138. } else {
  139. $lists[5]['staff_name'] = '其他';
  140. $lists[5]['total_number'] = (float)bcadd((string)($lists[5]['total_number'] ?? 0), (string)$item['total_number'], 2);
  141. }
  142. $i++;
  143. }
  144. foreach ($lists as $key => &$item) {
  145. $data['bing_data'][$key]['itemStyle']['color'] = $color[$key];
  146. $data['bing_data'][$key]['name'] = $item['staff_name'] ?? '';
  147. $data['bing_data'][$key]['value'] = $item['total_number'] ?? 0;
  148. $data['bing_xdata'][$key] = $item['staff_name'] ?? '';
  149. }
  150. $data['yAxis']['maxnum'] = $data['series'] ? max($data['series']) : 0;
  151. } else {
  152. /** @var SystemStoreStaffServices $systemStoreStaffServices */
  153. $systemStoreStaffServices = app()->make(SystemStoreStaffServices::class);
  154. $storeList = $systemStoreStaffServices->getSelectList(['store_id' => $where['store_id'], 'is_del' => 0, 'status' => 1]);
  155. foreach ($storeList as $store) {
  156. $data['series'][] = 0;
  157. $data['xAxis'][] = $store['label'] ?? '';
  158. }
  159. }
  160. return $data;
  161. }
  162. /**
  163. * 获取一段时间订单统计数量、金额
  164. * @param $where
  165. * @param $time
  166. * @return array
  167. * @throws \think\db\exception\DataNotFoundException
  168. * @throws \think\db\exception\DbException
  169. * @throws \think\db\exception\ModelNotFoundException
  170. */
  171. public function getTypeHeader($where, $time)
  172. {
  173. [$start, $end, $timeType, $xAxis] = $time;
  174. $order = $this->dao->orderAddTimeList($where, [$start, $end], $timeType, '*', 'pay_price');
  175. $price = array_column($order, 'price', 'day');
  176. $count = array_column($order, 'count', 'day');
  177. $datas = $series = [];
  178. foreach ($xAxis as $i => $key) {
  179. $datas['销售业绩金额'][] = isset($price[$key]) ? floatval($price[$key]) : 0;
  180. $datas['销售业绩单数'][] = isset($count[$key]) ? floatval($count[$key]) : 0;
  181. $arr = explode('-', $key);
  182. if (count($arr) >= 3) {
  183. $xAxis[$i] = $arr[1] . '-' . $arr[2];
  184. }
  185. }
  186. foreach ($datas as $key => $item) {
  187. $series[] = [
  188. 'name' => $key,
  189. 'data' => $item,
  190. 'type' => 'line',
  191. 'smooth' => 'true',
  192. 'yAxisIndex' => 1,
  193. ];
  194. }
  195. $data['order']['xAxis'] = $xAxis;
  196. $data['order']['series'] = $series;
  197. $color = ['#2EC479', '#7F7AE5', '#FFA21B', '#46A3FF', '#FF6046', '#5cadff', '#b37feb', '#19be6b', '#ff9900'];
  198. $data['bing']['series'] = [];
  199. $list = $this->dao->getStatisticsHeader($where, 'type', 'pay_price');
  200. foreach ($list as $key => &$item) {
  201. $item['type_name'] = isset($this->type[$item['type']]) ? $this->type[$item['type']] : '其他类型';
  202. $data['bing']['bing_data'][$key]['itemStyle']['color'] = $color[$key];
  203. $data['bing']['bing_data'][$key]['name'] = $item['type_name'];
  204. $data['bing']['bing_data'][$key]['value'] = $item['total_number'];
  205. $data['bing']['bing_xdata'][$key] = $item['type_name'];
  206. $data['bing']['series'][$key] = $item['total_number'];
  207. $data['bing']['xAxis'][$key] = $item['type_name'];
  208. }
  209. $data['bing']['yAxis']['maxnum'] = $data['bing']['series'] ? max($data['bing']['series']) : 0;
  210. return $data;
  211. }
  212. /**
  213. * 获取百分比
  214. * @param $num
  215. * @return string|null
  216. */
  217. public function getPercent($num)
  218. {
  219. return bcdiv($num, '100', 4);
  220. }
  221. /**
  222. * 写入流水账单
  223. * @param $order
  224. * @param int $type
  225. * @throws \think\db\exception\DataNotFoundException
  226. * @throws \think\db\exception\DbException
  227. * @throws \think\db\exception\ModelNotFoundException
  228. */
  229. public function setFinance($order, $type = 1, $price = 0)
  230. {
  231. /** @var StoreOrderServices $storeOrderServices */
  232. $storeOrderServices = app()->make(StoreOrderServices::class);
  233. switch ($type) {
  234. case 1 ://商品订单
  235. if ($order['store_id'] > 0) {
  236. //门店订单
  237. //2.1修改门店流水按照下单支付金额计算,
  238. if ($order['type'] == 8) {
  239. $order['pay_price'] = $order['total_price'];
  240. }
  241. $total_price = $order['pay_price'];
  242. //商品总价+支付邮费
  243. // $total_price = bcadd($total_price, $order['pay_postage'], 2);
  244. $append = [
  245. 'pay_price' => $total_price,
  246. 'total_price' => $total_price,
  247. 'rate' => 1
  248. ];
  249. //支付订单
  250. $this->savaData($order, $order['pay_price'], 1, 1, 1);
  251. //现金支付增加
  252. if ($order['pay_type'] == PayServices::CASH_PAY) {
  253. //交易订单记录
  254. $this->savaData($order, $total_price, 0, 2, 1, $append);
  255. }
  256. //门店订单
  257. $this->savaData($order, $total_price, 1, 2, 1, $append);
  258. if ($order['shipping_type'] == 1) {//配送订单
  259. //分配订单费率
  260. $rate = sys_config('store_self_order_rate');
  261. $type = 12;
  262. } elseif ($order['shipping_type'] == 2) {
  263. //核销订单费率
  264. $rate = sys_config('store_writeoff_order_rate');
  265. $type = 10;
  266. } else if ($order['shipping_type'] == 4) {
  267. //收银订单费率
  268. $rate = sys_config('store_cashier_order_rate');
  269. $type = 9;
  270. } else {
  271. //分配订单费率
  272. $rate = sys_config('store_self_order_rate');
  273. $type = 11;
  274. }
  275. $total_price = bcmul($total_price, $this->getPercent($rate), 2);
  276. $append['rate'] = $rate;
  277. //交易订单记录
  278. $this->savaData($order, $total_price, 1, $type, 2, $append);
  279. $this->savaData($order, $total_price, 0, 3, 1, $append);
  280. } else {
  281. $orderList = $storeOrderServices->getSonOrder($order['id'], '*', 1);
  282. if ($orderList) {
  283. foreach ($orderList as $order) {
  284. $total_price = $order['pay_price'];
  285. //商品总价+支付邮费
  286. // $total_price = bcadd($total_price, $order['pay_postage']);
  287. $append = [
  288. 'pay_price' => $total_price,
  289. 'total_price' => $total_price,
  290. 'rate' => 1
  291. ];
  292. //支付订单
  293. $this->savaData($order, $order['pay_price'], 1, 1, 1);
  294. //门店订单
  295. $this->savaData($order, $total_price, 1, 2, 1, $append);
  296. if ($order['shipping_type'] == 1) {//配送订单
  297. //分配订单费率
  298. $rate = sys_config('store_self_order_rate');
  299. $type = 12;
  300. } elseif ($order['shipping_type'] == 2) {
  301. //核销订单费率
  302. $rate = sys_config('store_writeoff_order_rate');
  303. $type = 10;
  304. } else if ($order['shipping_type'] == 4) {
  305. //收银订单费率
  306. $rate = sys_config('store_cashier_order_rate');
  307. $type = 9;
  308. } else {
  309. //分配订单费率
  310. $rate = sys_config('store_self_order_rate');
  311. $type = 11;
  312. }
  313. $total_price = bcmul($total_price, $this->getPercent($rate), 2);
  314. $append['rate'] = $rate;
  315. //交易订单记录
  316. $this->savaData($order, $total_price, 1, $type, 2);
  317. $this->savaData($order, $total_price, 0, 3, 1, $append);
  318. }
  319. }
  320. }
  321. break;
  322. case 2://充值订单
  323. //充值订单返点
  324. $store_recharge_order_rate = sys_config('store_recharge_order_rate');
  325. $order['pay_type'] = $order['recharge_type'];
  326. $append = [
  327. 'pay_price' => $order['price'],
  328. 'total_price' => $order['price'],
  329. 'rate' => $store_recharge_order_rate
  330. ];
  331. //订单账单
  332. $this->savaData($order, $order['price'], 1, 7, 2, $append);
  333. //收银台充值线下付款记录一条负记录
  334. if ($order['recharge_type'] = PayServices::OFFLINE_PAY) {
  335. $this->savaData($order, $order['price'], 0, 7, 1, $append);
  336. }
  337. //返点
  338. $pay_price = bcmul($order['price'], $this->getPercent($store_recharge_order_rate), 2);
  339. $this->savaData($order, $pay_price, 1, 5, 1, $append);
  340. break;
  341. case 3://付费会员订单
  342. //购买付费会员返点
  343. $store_svip_order_rate = sys_config('store_svip_order_rate');
  344. $append = [
  345. 'pay_price' => $order['pay_price'],
  346. 'total_price' => $order['pay_price'],
  347. 'rate' => $store_svip_order_rate
  348. ];
  349. //订单账单
  350. $this->savaData($order, $order['pay_price'], 1, 8, 2, $append);
  351. //收银台充值线下付款记录一条负记录
  352. if ($order['pay_type'] = PayServices::OFFLINE_PAY) {
  353. $this->savaData($order, $order['pay_price'], 0, 8, 1, $append);
  354. }
  355. //返点
  356. $pay_price = bcmul($order['pay_price'], $this->getPercent($store_svip_order_rate), 2);
  357. $this->savaData($order, $pay_price, 1, 6, 1, $append);
  358. break;
  359. case 4://退款
  360. //取下单流水记录费率
  361. $rate = $this->dao->value(['link_id' => $order['order_id'], 'type' => 3, 'trade_type' => 1], 'rate');
  362. if (!$rate) {
  363. //获取失败,如果是子订单;在查询主订单
  364. if (isset($order['pid']) && $order['pid']) {
  365. $order_id = $storeOrderServices->value(['id' => $order['pid']], 'order_id');
  366. if ($order_id) $rate = $this->dao->value(['link_id' => $order_id, 'type' => 3, 'trade_type' => 1], 'rate');
  367. }
  368. }
  369. if (!$rate) {//未获取到,下单保存费率;获取系统配置
  370. if ($order['shipping_type'] == 2) {
  371. //核销订单费率
  372. $rate = sys_config('store_writeoff_order_rate');
  373. } else if ($order['shipping_type'] == 4) {
  374. //收银订单费率
  375. $rate = sys_config('store_cashier_order_rate');
  376. } else {
  377. //分配订单费率
  378. $rate = sys_config('store_self_order_rate');
  379. }
  380. }
  381. $total_price = bcmul($price, $this->getPercent($rate), 2);
  382. $append['rate'] = $rate;
  383. //退款
  384. $this->savaData($order, $price, 0, 4, 1, $append);
  385. $this->savaData($order, $total_price, 1, 3, 1, $append);
  386. break;
  387. case 5://充值退款
  388. //取充值流水记录费率
  389. $rate = $this->dao->value(['link_id' => $order['order_id'], 'type' => 5, 'trade_type' => 1], 'rate');
  390. if (!$rate) {//获取失败,取系统配置
  391. $rate = sys_config('store_recharge_order_rate');
  392. }
  393. $order['pay_type'] = $order['recharge_type'];
  394. $append = [
  395. 'pay_price' => $order['price'],
  396. 'total_price' => $order['price'],
  397. 'rate' => $rate
  398. ];
  399. //订单账单
  400. $this->savaData($order, $price, 0, 4, 2, $append);
  401. //返点扣除
  402. $pay_price = bcmul($price, $this->getPercent($rate), 2);
  403. $this->savaData($order, $pay_price, 0, 5, 1, $append);
  404. break;
  405. case 6://配送订单
  406. $append = ['pay_price' => $order['cargo_price'],];
  407. $this->savaData($order, $price, 0, 13, 1, $append);
  408. break;
  409. case 7://取消配送订单
  410. $append = ['pay_price' => $order['cargo_price'],];
  411. $this->savaData($order, $price, 1, 13, 1, $append);
  412. break;
  413. }
  414. }
  415. /**
  416. * 写入数据
  417. * @param $order
  418. * @param $number
  419. * @param $pm
  420. * @param $type
  421. * @param $trade_type
  422. * @param array $append
  423. * @throws \Exception
  424. */
  425. public function savaData($order, $number, $pm, $type, $trade_type, array $append = [])
  426. {
  427. /** @var StoreOrderCreateServices $storeOrderCreateServices */
  428. $storeOrderCreateServices = app()->make(StoreOrderCreateServices::class);
  429. $order_id = $storeOrderCreateServices->getNewOrderId('ls');
  430. $data = [
  431. 'store_id' => $order['store_id'] ?? $order['relation_id'] ?? 0,
  432. 'uid' => $order['uid'] ?? 0,
  433. 'staff_id' => $order['staff_id'] ?? 0,
  434. 'order_id' => $order_id,
  435. 'link_id' => $order['order_id'] ?? '',
  436. 'pay_type' => $order['pay_type'] ?? '',
  437. 'trade_time' => $order['pay_time'] ?? $order['add_time'] ?? '',
  438. 'pm' => $pm,
  439. 'number' => $trade_type == 1 ? ($number ?: 0) : 0,
  440. 'type' => $type,
  441. 'trade_type' => $trade_type,
  442. 'add_time' => time()
  443. ];
  444. $data = array_merge($data, $append);
  445. $this->dao->save($data);
  446. }
  447. /**
  448. * 关联门店店员
  449. * @param $link_id
  450. * @param int $staff_id
  451. * @return mixed
  452. */
  453. public function setStaff($link_id, int $staff_id)
  454. {
  455. return $this->dao->update(['link_id' => $link_id], ['staff_id' => $staff_id]);
  456. }
  457. /**
  458. * 可提现金额
  459. * @param array $where
  460. * @return int|string
  461. * @throws \think\db\exception\DataNotFoundException
  462. * @throws \think\db\exception\DbException
  463. * @throws \think\db\exception\ModelNotFoundException
  464. */
  465. public function getSumFinance(array $where, array $whereData)
  466. {
  467. $field = 'sum(if(pm = 1,number,0)) as income_num,sum(if(pm = 0,number,0)) as exp_num';
  468. $data = $this->dao->getList($whereData, $field);
  469. if (!$data) return 0;
  470. $income_num = isset($data[0]['income_num']) ? $data[0]['income_num'] : 0;
  471. $exp_num = isset($data[0]['exp_num']) ? $data[0]['exp_num'] : 0;
  472. $number = bcsub($income_num, $exp_num, 2);
  473. //已提现金额
  474. /** @var StoreExtractServices $storeExtractServices */
  475. $storeExtractServices = app()->make(StoreExtractServices::class);
  476. $where['not_status'] = -1;
  477. $extract_price = $storeExtractServices->dao->getExtractMoneyByWhere($where, 'extract_price');
  478. $price_not = bcsub((string)$number, (string)$extract_price, 2);
  479. return $price_not;
  480. }
  481. }