CapitalFlowServices.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\statistic;
  12. use app\dao\system\statistics\CapitalFlowDao;
  13. use app\services\BaseServices;
  14. use app\services\pay\PayServices;
  15. use crmeb\exceptions\AdminException;
  16. class CapitalFlowServices extends BaseServices
  17. {
  18. /**
  19. * @param CapitalFlowDao $dao
  20. */
  21. public function __construct(CapitalFlowDao $dao)
  22. {
  23. $this->dao = $dao;
  24. }
  25. /**
  26. * 添加资金流水
  27. * @param $orderInfo
  28. * @param string $type
  29. */
  30. public function setFlow($orderInfo, $type = '')
  31. {
  32. $data['flow_id'] = 'ZJ' . date('Ymdhis', time()) . rand('1000', '9999');
  33. switch ($type) {
  34. case 'order':
  35. $data['order_id'] = $orderInfo['order_id'];
  36. $data['uid'] = $orderInfo['uid'];
  37. $data['price'] = $orderInfo['pay_price'];
  38. $data['trading_type'] = 1;
  39. $data['pay_type'] = $orderInfo['pay_type'];
  40. break;
  41. case 'refund':
  42. $data['order_id'] = $orderInfo['order_id'];
  43. $data['uid'] = $orderInfo['uid'];
  44. $data['price'] = bcmul('-1', $orderInfo['refund_price'], 2);
  45. $data['trading_type'] = 2;
  46. $data['pay_type'] = $orderInfo['pay_type'];
  47. break;
  48. case 'recharge':
  49. $data['order_id'] = $orderInfo['order_id'];
  50. $data['uid'] = $orderInfo['uid'];
  51. $data['price'] = $orderInfo['price'];
  52. $data['trading_type'] = 3;
  53. $data['pay_type'] = $orderInfo['recharge_type'];
  54. break;
  55. case 'refund_recharge':
  56. $data['order_id'] = $orderInfo['order_id'];
  57. $data['uid'] = $orderInfo['uid'];
  58. $data['price'] = bcmul('-1', $orderInfo['price'], 2);
  59. $data['trading_type'] = 4;
  60. $data['pay_type'] = $orderInfo['recharge_type'];
  61. break;
  62. case 'luck':
  63. $data['order_id'] = $orderInfo['order_id'];
  64. $data['uid'] = $orderInfo['uid'];
  65. $data['price'] = $orderInfo['price'];
  66. $data['trading_type'] = 5;
  67. $data['pay_type'] = $orderInfo['pay_type'];
  68. break;
  69. case 'extract':
  70. $data['order_id'] = $orderInfo['order_id'];
  71. $data['uid'] = $orderInfo['uid'];
  72. $data['price'] = $orderInfo['price'];
  73. $data['trading_type'] = 6;
  74. $data['pay_type'] = $orderInfo['pay_type'];
  75. break;
  76. case 'pay_member':
  77. $data['order_id'] = $orderInfo['order_id'];
  78. $data['uid'] = $orderInfo['uid'];
  79. $data['price'] = $orderInfo['pay_price'];
  80. $data['trading_type'] = 7;
  81. $data['pay_type'] = $orderInfo['pay_type'];
  82. break;
  83. case 'offline_scan':
  84. $data['order_id'] = $orderInfo['order_id'];
  85. $data['uid'] = $orderInfo['uid'];
  86. $data['price'] = $orderInfo['pay_price'];
  87. $data['trading_type'] = 8;
  88. $data['pay_type'] = $orderInfo['pay_type'];
  89. break;
  90. default:
  91. break;
  92. }
  93. $data['nickname'] = $orderInfo['nickname'];
  94. $data['phone'] = $orderInfo['phone'];
  95. $data['add_time'] = time();
  96. $this->dao->save($data);
  97. }
  98. /**
  99. * 获取资金流水
  100. * @param $where
  101. * @return array
  102. * @throws \think\db\exception\DataNotFoundException
  103. * @throws \think\db\exception\DbException
  104. * @throws \think\db\exception\ModelNotFoundException
  105. */
  106. public function getFlowList($where)
  107. {
  108. $export = $where['export'] ?? 0;
  109. unset($where['export']);
  110. [$page, $limit] = $this->getPageValue();
  111. $status = ['全部', '支付订单', '订单退款', '充值订单', '充值退款', '抽奖红包', '佣金提现', '购买会员', '线下收银'];
  112. $list = $this->dao->getList($where, $page, $limit);
  113. foreach ($list as &$item) {
  114. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  115. $item['trading_type'] = $status[$item['trading_type']];
  116. $item['pay_type_name'] = PayServices::PAY_TYPE[$item['pay_type'] != 'routine' ? $item['pay_type'] : 'weixin'];
  117. }
  118. $count = $this->dao->count($where);
  119. if ($export) {
  120. $fileKey = ['flow_id', 'order_id', 'nickname', 'phone', 'price', 'trading_type', 'pay_type_name', 'add_time', 'mark'];
  121. $header = ['交易单号', '关联订单', '用户', '电话', '金额', '订单类型', '支付类型', '交易时间', '备注'];
  122. $fileName = '账单导出' . date('YmdHis') . rand(1000, 9999);
  123. return compact('list', 'fileKey', 'header', 'fileName');
  124. } else {
  125. return compact('list', 'count', 'status');
  126. }
  127. }
  128. /**
  129. * 添加备注
  130. * @param $id
  131. * @param $data
  132. * @return bool
  133. */
  134. public function setMark($id, $data)
  135. {
  136. $res = $this->dao->update($id, $data);
  137. if ($res) {
  138. return true;
  139. } else {
  140. throw new AdminException(100025);
  141. }
  142. }
  143. /**
  144. * 获取账单记录
  145. * @param $where
  146. * @return array
  147. */
  148. public function getFlowRecord($where)
  149. {
  150. [$page, $limit] = $this->getPageValue();
  151. $data = $this->dao->getRecordList($where, $page, $limit);
  152. $i = 1;
  153. foreach ($data['list'] as &$item) {
  154. $item['id'] = $i;
  155. $i++;
  156. $item['entry_price'] = bcadd($item['income_price'], $item['exp_price'], 2);
  157. switch ($where['type']) {
  158. case "day" :
  159. $item['title'] = "日账单";
  160. $item['add_time'] = date('Y-m-d', $item['add_time']);
  161. break;
  162. case "week" :
  163. $item['title'] = "周账单";
  164. $item['add_time'] = '第' . $item['day'] . '周(' . date('m', $item['add_time']) . '月)';
  165. break;
  166. case "month" :
  167. $item['title'] = "月账单";
  168. $item['add_time'] = date('Y-m', $item['add_time']);
  169. break;
  170. }
  171. }
  172. return $data;
  173. }
  174. }