SupplierFlowingWaterServices.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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\supplier\finance;
  12. use app\dao\supplier\finance\SupplierFlowingWaterDao;
  13. use app\services\supplier\finance\SupplierTransactionsServices;
  14. use app\services\BaseServices;
  15. use app\services\order\StoreOrderCreateServices;
  16. use app\services\order\StoreOrderCartInfoServices;
  17. use app\services\order\StoreOrderRefundServices;
  18. use app\services\order\StoreOrderServices;
  19. use app\services\pay\PayServices;
  20. /**
  21. * 供应商流水
  22. * Class SupplierFlowingWaterServices
  23. * @package app\services\supplier\finance
  24. * @mixin SupplierFlowingWaterDao
  25. */
  26. class SupplierFlowingWaterServices extends BaseServices
  27. {
  28. /**
  29. * 支付类型
  30. * @var string[]
  31. */
  32. public $pay_type = ['weixin' => '微信支付', 'yue' => '余额支付', 'offline' => '线下支付', 'alipay' => '支付宝支付', 'cash' => '现金支付', 'automatic' => '自动转账', 'store' => '微信支付'];
  33. /**
  34. * 交易类型
  35. * @var string[]
  36. */
  37. public $type = [
  38. 1 => '支付订单',
  39. 2 => '退款订单'
  40. ];
  41. /**
  42. * 构造方法
  43. * @param SupplierFlowingWaterDao $dao
  44. */
  45. public function __construct(SupplierFlowingWaterDao $dao)
  46. {
  47. $this->dao = $dao;
  48. }
  49. /**
  50. * 显示资源列表
  51. * @param array $where
  52. * @return array
  53. * @throws \think\db\exception\DataNotFoundException
  54. * @throws \think\db\exception\DbException
  55. * @throws \think\db\exception\ModelNotFoundException
  56. */
  57. public function getList(array $where)
  58. {
  59. [$page, $limit] = $this->getPageValue();
  60. $list = $this->dao->getList($where, '*', $page, $limit, ['user', 'supplier' => function ($query) {
  61. $query->field('id,supplier_name')->bind(['supplier_name']);
  62. }]);
  63. foreach ($list as &$item) {
  64. $item['type_name'] = isset($this->type[$item['type']]) ? $this->type[$item['type']] : '其他类型';
  65. $item['pay_type_name'] = isset($this->pay_type[$item['pay_type']]) ? $this->pay_type[$item['pay_type']] : '其他方式';
  66. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
  67. $item['trade_time'] = $item['trade_time'] ? date('Y-m-d H:i:s', $item['trade_time']) : $item['add_time'];
  68. $item['user_nickname'] = $item['user_nickname'] ?: '游客';
  69. }
  70. $count = $this->dao->getCount($where);
  71. return compact('list', 'count');
  72. }
  73. /**
  74. * 供应商账单
  75. * @param $where
  76. * @throws \think\db\exception\DataNotFoundException
  77. * @throws \think\db\exception\DbException
  78. * @throws \think\db\exception\ModelNotFoundException
  79. */
  80. public function getFundRecord($where)
  81. {
  82. [$page, $limit] = $this->getPageValue();
  83. $where['is_del'] = 0;
  84. $data = $this->dao->getFundRecord($where, $page, $limit);
  85. $i = 1;
  86. foreach ($data['list'] as &$item) {
  87. $item['id'] = $i;
  88. $i++;
  89. $item['entry_num'] = bcsub($item['income_num'], $item['exp_num'], 2);
  90. switch ($where['timeType']) {
  91. case "day" :
  92. $item['title'] = "日账单";
  93. $item['add_time'] = date('Y-m-d', $item['add_time']);
  94. break;
  95. case "week" :
  96. $item['title'] = "周账单";
  97. $item['add_time'] = '第' . $item['day'] . '周(' . date('m', $item['add_time']) . '月)';
  98. break;
  99. case "month" :
  100. $item['title'] = "月账单";
  101. $item['add_time'] = date('Y-m', $item['add_time']);
  102. break;
  103. }
  104. }
  105. return $data;
  106. }
  107. /**
  108. * 获取百分比
  109. * @param $num
  110. * @return string|null
  111. */
  112. public function getPercent($num)
  113. {
  114. return bcdiv($num, '100', 4);
  115. }
  116. /**写入流水账单
  117. * @param $oid
  118. * @param $type
  119. * @return bool|void
  120. * @throws \Exception
  121. */
  122. public function setSupplierFinance($oid, $type = 1)
  123. {
  124. /** @var SupplierTransactionsServices $transactionsServices */
  125. $transactionsServices = app()->make(SupplierTransactionsServices::class);
  126. /** @var StoreOrderCartInfoServices $cartInfoServices */
  127. $cartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  128. /** @var StoreOrderServices $storeOrderServices */
  129. $storeOrderServices = app()->make(StoreOrderServices::class);
  130. /** @var StoreOrderRefundServices $storeOrderRefundServices */
  131. $storeOrderRefundServices = app()->make(StoreOrderRefundServices::class);
  132. $order = $storeOrderServices->get($oid);
  133. if (!$order) {
  134. return true;
  135. }
  136. if ($order['supplier_id'] <= 0) return true;
  137. if ($type == 1) {
  138. $res = $this->search([
  139. 'type' => 1,
  140. 'link_id' => $order['order_id'],
  141. 'supplier_id' => $order['supplier_id'],
  142. 'pay_type' => $order['pay_type'] ?? '',
  143. 'uid' => $order['uid'] ?? 0,
  144. ])->find();
  145. if ($res) return true;
  146. }
  147. $data = $cartInfoServices->getOrderCartInfoSettlePrice($order['id']);
  148. $pay_postage = 0;
  149. if (isset($order['shipping_type']) && !in_array($order['shipping_type'], [2, 4])) {
  150. $pay_postage = floatval($storeOrderRefundServices->getOrderSumPrice($data['info'], 'postage_price', false));
  151. }
  152. if ($order['type'] == 8) {
  153. $order['pay_price'] = $order['total_price'];
  154. }
  155. $append = [
  156. 'pay_price' => $order['pay_price'],
  157. 'pay_postage' => $pay_postage,
  158. 'total_price' => $order['total_price'],
  159. ];
  160. switch ($type) {
  161. case 1 ://支付
  162. $number = bcadd((string)$data['settlePrice'], $pay_postage, 2);
  163. //支付订单
  164. $this->savaData($order, $number, 1, 1, $append);
  165. //交易订单记录
  166. $transactionsServices->savaData($order, 1, 1, $append);
  167. break;
  168. case 2://退款
  169. $number = bcadd((string)$data['refundSettlePrice'], $pay_postage, 2);
  170. $this->savaData($order, $number, 0, 2, $append);
  171. //交易订单记录
  172. $transactionsServices->savaData($order, 0, 2, $append);
  173. break;
  174. }
  175. }
  176. /**
  177. * 写入数据
  178. * @param $order
  179. * @param $number
  180. * @param $pm
  181. * @param $type
  182. * @param $trade_type
  183. * @param array $append
  184. * @throws \Exception
  185. */
  186. public function savaData($order, $number, $pm, $type, array $append = [])
  187. {
  188. /** @var StoreOrderCreateServices $storeOrderCreateServices */
  189. $storeOrderCreateServices = app()->make(StoreOrderCreateServices::class);
  190. $order_id = $storeOrderCreateServices->getNewOrderId('ls');
  191. $data = [
  192. 'supplier_id' => $order['supplier_id'] ?? 0,
  193. 'uid' => $order['uid'] ?? 0,
  194. 'order_id' => $order_id,
  195. 'link_id' => $order['order_id'] ?? '',
  196. 'pay_type' => $order['pay_type'] ?? '',
  197. 'trade_time' => $order['pay_time'] ?? $order['add_time'] ?? '',
  198. 'pm' => $pm,
  199. 'number' => $number ?: 0,
  200. 'type' => $type,
  201. 'add_time' => time()
  202. ];
  203. $data = array_merge($data, $append);
  204. $this->dao->save($data);
  205. }
  206. /**
  207. * 关联门店店员
  208. * @param $link_id
  209. * @param int $staff_id
  210. * @return mixed
  211. */
  212. public function setStaff($link_id, int $staff_id)
  213. {
  214. return $this->dao->update(['link_id' => $link_id], ['staff_id' => $staff_id]);
  215. }
  216. /**
  217. * 可提现金额
  218. * @param array $where
  219. * @return int|string
  220. * @throws \think\db\exception\DataNotFoundException
  221. * @throws \think\db\exception\DbException
  222. * @throws \think\db\exception\ModelNotFoundException
  223. */
  224. public function getSumFinance(array $where, array $whereData)
  225. {
  226. $field = 'sum(if(pm = 1,number,0)) as income_num,sum(if(pm = 0,number,0)) as exp_num';
  227. $data = $this->dao->getList($whereData, $field);
  228. if (!$data) return 0;
  229. $income_num = isset($data[0]['income_num']) ? $data[0]['income_num'] : 0;
  230. $exp_num = isset($data[0]['exp_num']) ? $data[0]['exp_num'] : 0;
  231. $number = bcsub($income_num, $exp_num, 2);
  232. //已提现金额
  233. /** @var SupplierExtractServices $extractServices */
  234. $extractServices = app()->make(SupplierExtractServices::class);
  235. $where['not_status'] = -1;
  236. $extract_price = $extractServices->dao->getExtractMoneyByWhere($where, 'extract_price');
  237. $price_not = bcsub((string)$number, (string)$extract_price, 2);
  238. return $price_not;
  239. }
  240. }