SupplierExtractServices.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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\services\supplier\finance\SupplierFlowingWaterServices;
  13. use app\dao\supplier\finance\SupplierExtractDao;
  14. use app\dao\store\StoreUserDao;
  15. use app\services\BaseServices;
  16. use app\services\store\SystemStoreServices;
  17. use app\services\store\SystemStoreStaffServices;
  18. use app\services\supplier\SystemSupplierServices;
  19. use app\services\system\admin\SystemAdminServices;
  20. use crmeb\services\FormBuilder as Form;
  21. use think\exception\ValidateException;
  22. use think\facade\Route as Url;
  23. /**
  24. * 门店提现
  25. * Class StoreExtractServices
  26. * @package app\services\store\finance
  27. * @mixin SupplierExtractDao
  28. */
  29. class SupplierExtractServices extends BaseServices
  30. {
  31. /**
  32. * 构造方法
  33. * StoreUser constructor.
  34. * @param StoreUserDao $dao
  35. */
  36. public function __construct(SupplierExtractDao $dao)
  37. {
  38. $this->dao = $dao;
  39. }
  40. /**
  41. * 获取一条提现记录
  42. * @param int $id
  43. * @param array $field
  44. * @return array|\think\Model|null
  45. */
  46. public function getExtract(int $id, array $field = [])
  47. {
  48. return $this->dao->get($id, $field);
  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 index(array $where, array $whereData = [])
  59. {
  60. $list = $this->getStoreExtractList($where);
  61. //待审核金额
  62. $where['status'] = 0;
  63. $extract_statistics['price'] = $this->dao->getExtractMoneyByWhere($where, 'extract_price');
  64. //待转账金额
  65. $where['status'] = 1;
  66. $where['pay_status'] = 0;
  67. $extract_statistics['unPayPrice'] = $this->dao->getExtractMoneyByWhere($where, 'extract_price');
  68. //累计提现
  69. $where['status'] = 1;
  70. $where['pay_status'] = 1;
  71. $extract_statistics['paidPrice'] = $this->dao->getExtractMoneyByWhere($where, 'extract_price');
  72. $extract_statistics['price_count'] = 0;
  73. //未提现金额
  74. /** @var SupplierFlowingWaterServices $financeFlowServices */
  75. $financeFlowServices = app()->make(SupplierFlowingWaterServices::class);
  76. $price_not = $financeFlowServices->getSumFinance(['supplier_id' => isset($where['supplier_id']) && $where['supplier_id'] ? $where['supplier_id'] : 0], $whereData);
  77. $extract_statistics['price_not'] = max($price_not, 0);
  78. $extract_statistics['extract_min_price'] = sys_config('supplier_extract_min_price');
  79. $extract_statistics['extract_max_price'] = sys_config('supplier_extract_max_price');
  80. return compact('extract_statistics', 'list');
  81. }
  82. /**
  83. * 获取提现列表
  84. * @param array $where
  85. * @param string $field
  86. * @return array
  87. * @throws \think\db\exception\DataNotFoundException
  88. * @throws \think\db\exception\DbException
  89. * @throws \think\db\exception\ModelNotFoundException
  90. */
  91. public function getStoreExtractList(array $where, string $field = '*')
  92. {
  93. [$page, $limit] = $this->getPageValue();
  94. $list = $this->dao->getExtractList($where, $field, ['supplier'], $page, $limit);
  95. if ($list) {
  96. /** @var SystemAdminServices $adminServices */
  97. $adminServices = app()->make(SystemAdminServices::class);
  98. $adminIds = array_unique(array_column($list, 'admin_id'));
  99. $adminInfos = [];
  100. if ($adminIds) $adminInfos = $adminServices->getColumn([['id', 'in', $adminIds]], 'id,real_name', 'id');
  101. foreach ($list as &$item) {
  102. $item['add_time'] = $item['add_time'] ? date("Y-m-d H:i:s", $item['add_time']) : '';
  103. $item['admin_name'] = $item['admin_id'] ? ($adminInfos[$item['admin_id']]['real_name'] ?? '') : '';
  104. }
  105. }
  106. $count = $this->dao->count($where);
  107. return compact('list', 'count');
  108. }
  109. /**
  110. * 提现申请
  111. * @param int $supplierId
  112. * @param array $data
  113. * @return bool
  114. * @throws \think\db\exception\DataNotFoundException
  115. * @throws \think\db\exception\DbException
  116. * @throws \think\db\exception\ModelNotFoundException
  117. */
  118. public function cash(int $supplierId, array $data)
  119. {
  120. /** @var SystemSupplierServices $systemSupplier */
  121. $systemSupplier = app()->make(SystemSupplierServices::class);
  122. $supplierInfo = $systemSupplier->getSupplierInfo($supplierId);
  123. $insertData = [];
  124. switch ($data['extract_type']) {
  125. case 'bank':
  126. if (!$supplierInfo['bank_code'] || !$supplierInfo['bank_address']) {
  127. throw new ValidateException('请先设置提现银行与开户行信息');
  128. }
  129. $insertData['bank_code'] = $supplierInfo['bank_code'];
  130. $insertData['bank_name'] = $supplierInfo['bank_name'];
  131. $insertData['bank_address'] = $supplierInfo['bank_address'];
  132. break;
  133. case 'alipay':
  134. if (!$supplierInfo['alipay_account'] || !$supplierInfo['alipay_qrcode_url']) {
  135. throw new ValidateException('请先设置提现支付宝信息');
  136. }
  137. $insertData['alipay_account'] = $supplierInfo['alipay_account'];
  138. $insertData['qrcode_url'] = $supplierInfo['alipay_qrcode_url'];
  139. break;
  140. case 'weixin':
  141. if (!$supplierInfo['wechat'] || !$supplierInfo['wechat_qrcode_url']) {
  142. throw new ValidateException('请先设置提现微信信息');
  143. }
  144. $insertData['wechat'] = $supplierInfo['wechat'];
  145. $insertData['qrcode_url'] = $supplierInfo['wechat_qrcode_url'];
  146. break;
  147. default:
  148. throw new ValidateException('暂不支持该类型提现');
  149. break;
  150. }
  151. $insertData['supplier_id'] = $supplierInfo['id'];
  152. $insertData['extract_type'] = $data['extract_type'];
  153. $insertData['extract_price'] = $data['money'];
  154. $insertData['add_time'] = time();
  155. $insertData['supplier_mark'] = $data['mark'];
  156. $insertData['status'] = 0;
  157. if (!$this->dao->save($insertData)) {
  158. return false;
  159. }
  160. return true;
  161. }
  162. /**
  163. * 拒绝
  164. * @param $id
  165. * @return mixed
  166. */
  167. public function refuse(int $id, string $message, int $adminId)
  168. {
  169. $extract = $this->getExtract($id);
  170. if (!$extract) {
  171. throw new ValidateException('操作记录不存在!');
  172. }
  173. if ($extract->status == 1) {
  174. throw new ValidateException('已经提现,错误操作');
  175. }
  176. if ($extract->status == -1) {
  177. throw new ValidateException('您的提现申请已被拒绝,请勿重复操作!');
  178. }
  179. if ($this->dao->update($id, ['fail_time' => time(), 'fail_msg' => $message, 'status' => -1, 'admin_id' => $adminId])) {
  180. return true;
  181. } else {
  182. throw new ValidateException('操作失败!');
  183. }
  184. }
  185. /**
  186. * 通过
  187. * @param $id
  188. * @return mixed
  189. */
  190. public function adopt(int $id, int $adminId)
  191. {
  192. $extract = $this->getExtract($id);
  193. if (!$extract) {
  194. throw new ValidateException('操作记录不存!');
  195. }
  196. if ($extract->status == 1) {
  197. throw new ValidateException('您已提现,请勿重复提现!');
  198. }
  199. if ($extract->status == -1) {
  200. throw new ValidateException('您的提现申请已被拒绝!');
  201. }
  202. if ($this->dao->update($id, ['status' => 1, 'admin_id' => $adminId])) {
  203. return true;
  204. } else {
  205. throw new ValidateException('操作失败!');
  206. }
  207. }
  208. /**
  209. * 转账页面
  210. * @param int $id
  211. * @return string
  212. */
  213. public function add_transfer(int $id)
  214. {
  215. $field = array();
  216. $title = '转账信息';
  217. $field[] = Form::input('voucher_title', '转账说明', '')->maxlength(30)->required();
  218. $field[] = Form::frameImage('voucher_image', '转账凭证', Url::buildUrl(config('admin.admin_prefix') . '/widget.images/index', array('fodder' => 'voucher_image')), '')->icon('ios-add')->width('960px')->height('505px')->modal(['footer-hide' => true]);
  219. return create_form($title, $field, Url::buildUrl('/supplier/extract/save_transfer/' . $id), 'POST');
  220. }
  221. }