SupplierFlowingWaterServices.php 9.7 KB

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