FinancialRepository.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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\common\repositories\system\financial;
  12. use app\common\dao\system\financial\FinancialDao;
  13. use app\common\repositories\BaseRepository;
  14. use app\common\repositories\system\merchant\MerchantRepository;
  15. use FormBuilder\Factory\Elm;
  16. use think\exception\ValidateException;
  17. use think\facade\Db;
  18. use think\facade\Route;
  19. class FinancialRepository extends BaseRepository
  20. {
  21. public function __construct(FinancialDao $dao)
  22. {
  23. $this->dao = $dao;
  24. }
  25. public function financialAccountForm($id)
  26. {
  27. $merchant = app()->make(MerchantRepository::class)->search(['mer_id' => $id])->find();
  28. $form = Elm::createForm(Route::buildUrl('merchantFinancialAccountSave',['id' => $id])->build());
  29. $form->setRule([
  30. Elm::radio('financial_type', '转账类型', $merchant->financial_type)
  31. ->setOptions([
  32. ['value' => 1, 'label' => '银行卡'],
  33. ['value' => 2, 'label' => '微信'],
  34. ['value' => 3, 'label' => '支付宝'],
  35. ])->control([
  36. [
  37. 'value' => 1,
  38. 'rule'=> [
  39. Elm::input('name', '姓名')->value($merchant->financial_bank->name??'')->required(),
  40. Elm::input('bank', '开户银行')->value($merchant->financial_bank->bank??'')->required(),
  41. Elm::input('bank_code', '银行卡号')->value($merchant->financial_bank->bank_code??'')->required(),
  42. ]
  43. ],
  44. [
  45. 'value' => 2,
  46. 'rule'=> [
  47. Elm::input('name', '姓名')->value($merchant->financial_wechat->name??'')->required(),
  48. Elm::input('wechat', '微信号')->value($merchant->financial_wechat->wechat??'')->required(),
  49. Elm::frameImage('wechat_code', '收款二维码', '/' . config('admin.merchant_prefix') . '/setting/uploadPicture?field=wechat_code&type=1')->value($merchant->financial_wechat->wechat_code??'')->modal(['modal' => false])->width('896px')->height('480px'),
  50. ]
  51. ],
  52. [
  53. 'value' => 3,
  54. 'rule'=> [
  55. Elm::input('name', '姓名')->value($merchant->financial_alipay->name??'')->required(),
  56. Elm::input('alipay', '支付宝账号')->value($merchant->financial_alipay->alipay??'')->required(),
  57. Elm::frameImage('alipay_code', '收款二维码', '/' . config('admin.merchant_prefix') . '/setting/uploadPicture?field=alipay_code&type=1')->value($merchant->financial_alipay->alipay_code??'')->modal(['modal' => false])->width('896px')->height('480px'),
  58. ]
  59. ],
  60. ]),
  61. ]);
  62. return $form->setTitle('转账信息');
  63. }
  64. /**
  65. * TODO 保存转账信息
  66. * @param int $merId
  67. * @param array $data
  68. * @author Qinii
  69. * @day 3/18/21
  70. */
  71. public function saveAccount(int $merId,array $data)
  72. {
  73. switch ($data['financial_type'])
  74. {
  75. case 1:
  76. $key = 'financial_bank' ;
  77. $update = [
  78. 'name' => $data['name'],
  79. 'bank' => $data['bank'],
  80. 'bank_code' => $data['bank_code'],
  81. ];
  82. break;
  83. case 2:
  84. $key = 'financial_wechat' ;
  85. $update = [
  86. 'name' => $data['name'],
  87. //'idcard' => $data['idcard'],
  88. 'wechat' => $data['wechat'],
  89. 'wechat_code' => $data['wechat_code'],
  90. ];
  91. break;
  92. case 3:
  93. $key = 'financial_alipay' ;
  94. $update = [
  95. 'name' => $data['name'],
  96. //'idcard' => $data['idcard'],
  97. 'alipay' => $data['alipay'],
  98. 'alipay_code' => $data['alipay_code'],
  99. ];
  100. break;
  101. }
  102. return app()->make(MerchantRepository::class)->update($merId,[$key => json_encode($update),'financial_type' => $data['financial_type']]);
  103. }
  104. public function applyForm(int $merId)
  105. {
  106. $merchant = app()->make(MerchantRepository::class)->search(['mer_id' => $merId])->field('mer_id,mer_name,mer_money,financial_bank,financial_wechat,financial_alipay,financial_type')->find();
  107. $extract_minimum_line = systemConfig('extract_minimum_line') ?: 0;
  108. $extract_minimum_num = systemConfig('extract_minimum_num');
  109. $_line = bcsub($merchant->mer_money,$extract_minimum_line,2);
  110. $_extract = ($_line < 0) ? 0 : $_line;
  111. $form = Elm::createForm(Route::buildUrl('merchantFinancialCreateSave')->build());
  112. $form->setRule([
  113. [
  114. 'type' => 'span',
  115. 'title' => '商户名称:',
  116. 'children' => [$merchant->mer_name]
  117. ],
  118. [
  119. 'type' => 'span',
  120. 'title' => '商户ID:',
  121. 'children' => ["$merId"]
  122. ],
  123. // [
  124. // 'type' => 'span',
  125. // 'title' => '',
  126. // 'children' => []
  127. // ],
  128. [
  129. 'type' => 'span',
  130. 'title' => '提示:',
  131. 'children' => ['最低可提现额度:'.$extract_minimum_line.'元;最低提现金额:'.$extract_minimum_num.'元']
  132. ],
  133. [
  134. 'type' => 'span',
  135. 'title' => '商户余额:',
  136. 'children' => [$merchant->mer_money]
  137. ],
  138. [
  139. 'type' => 'span',
  140. 'title' => '商户可提现金额:',
  141. 'children' => [$_extract]
  142. ],
  143. Elm::radio('financial_type', '转账类型:', $merchant->financial_type)
  144. ->setOptions([
  145. ['value' => 1, 'label' => '银行卡'],
  146. ['value' => 2, 'label' => '微信'],
  147. ['value' => 3, 'label' => '支付宝'],
  148. ])->control([
  149. [
  150. 'value' => 1,
  151. 'rule'=> [
  152. [
  153. 'type' => 'span',
  154. 'title' => '姓名',
  155. 'native' => false,
  156. 'children' => [$merchant->financial_bank->name??'未填写']
  157. ],
  158. [
  159. 'type' => 'span',
  160. 'title' => '开户银行',
  161. 'native' => false,
  162. 'children' => [$merchant->financial_bank->bank??'未填写']
  163. ],
  164. [
  165. 'type' => 'span',
  166. 'title' => '银行卡号',
  167. 'native' => false,
  168. 'children' => [$merchant->financial_bank->bank_code??'未填写']
  169. ],
  170. ]
  171. ],
  172. [
  173. 'value' => 2,
  174. 'rule'=> [
  175. [
  176. 'type' => 'span',
  177. 'title' => '姓名',
  178. 'native' => false,
  179. 'children' => [$merchant->financial_wechat->name??'未填写']
  180. ],
  181. [
  182. 'type' => 'span',
  183. 'title' => '微信号',
  184. 'native' => false,
  185. 'children' => [$merchant->financial_wechat->wechat??'未填写']
  186. ],
  187. [
  188. 'type' => 'img',
  189. 'title' => '收款二维码',
  190. 'native' => false,
  191. 'attrs' => ['src' => $merchant->financial_wechat->wechat_code ?? ''],
  192. 'style' => ['width' => '86px','height' => '48px']
  193. ],
  194. ]
  195. ],
  196. [
  197. 'value' => 3,
  198. 'rule'=> [
  199. [
  200. 'type' => 'span',
  201. 'title' => '姓名',
  202. 'native' => false,
  203. 'children' => [$merchant->financial_alipay->name??'未填写']
  204. ],
  205. [
  206. 'type' => 'span',
  207. 'title' => '支付宝账号',
  208. 'native' => false,
  209. 'children' => [$merchant->financial_alipay->alipay??'未填写']
  210. ],
  211. [
  212. 'type' => 'img',
  213. 'title' => '收款二维码',
  214. 'native' => false,
  215. 'attrs' => ['src' => $merchant->financial_alipay->alipay_code ?? ''],
  216. 'style' => ['width' => '86px','height' => '48px']
  217. ],
  218. ]
  219. ],
  220. ]),
  221. Elm::number('extract_money', '申请金额:')->value($extract_minimum_num)->required(),
  222. ]);
  223. return $form->setTitle('申请转账');
  224. }
  225. /**
  226. * TODO 保存申请
  227. * @param int $merId
  228. * @param array $data
  229. * @author Qinii
  230. * @day 3/19/21
  231. */
  232. public function saveApply(int $merId,array $data)
  233. {
  234. $make = app()->make(MerchantRepository::class);
  235. $merchant = $make->search(['mer_id' => $merId])->field('mer_id,mer_name,mer_money,financial_bank,financial_wechat,financial_alipay')->find();
  236. if($merchant['mer_money'] <= 0) throw new ValidateException('余额不足');
  237. if($data['financial_type'] == 1){
  238. $financial_account = $merchant->financial_bank;
  239. }elseif ($data['financial_type'] == 2){
  240. $financial_account = $merchant->financial_wechat;
  241. }elseif ($data['financial_type'] == 3){
  242. $financial_account = $merchant->financial_alipay;
  243. }
  244. if(empty($financial_account)) throw new ValidateException('未填写银行转账信息');
  245. $extract_maxmum_num = systemConfig('extract_maxmum_num');
  246. if($extract_maxmum_num !== 0 && $data['extract_money'] > $extract_maxmum_num) throw new ValidateException('单次申请金额不得大于'.$extract_maxmum_num.'元');
  247. //最低提现额度
  248. $extract_minimum_line = systemConfig('extract_minimum_line') ?: 0;
  249. if($extract_minimum_line > 0 ){
  250. $_line = bcsub($merchant->mer_money,$extract_minimum_line,2);
  251. if($_line < 0) throw new ValidateException('余额大于'.$extract_minimum_line.'才可提现');
  252. if($data['extract_money'] > $_line) throw new ValidateException('提现金额大于可提现金额');
  253. }
  254. //最低提现金额
  255. $extract_minimum_num = systemConfig('extract_minimum_num');
  256. if($data['extract_money'] < $extract_minimum_num) throw new ValidateException('最低提现金额'.$extract_minimum_num);
  257. //可提现金额
  258. $_line = bcsub($merchant->mer_money,$extract_minimum_line,2);
  259. if($_line < 0) throw new ValidateException('余额大于'.$extract_minimum_line.'才可提现');
  260. //最低提现金额
  261. if($data['extract_money'] < $extract_minimum_num) throw new ValidateException('最低提现金额'.$extract_minimum_num);
  262. //不足提现最低金额
  263. if($_line < $extract_minimum_num) throw new ValidateException('提现金额不足');
  264. $_money = bcsub($merchant['mer_money'],$data['extract_money'],2);
  265. $sn = date('YmdHis'.$merId);
  266. $ret = [
  267. 'status' => 0,
  268. 'mer_id' => $merId,
  269. 'mer_money' => $_money,
  270. 'financial_sn' => $sn,
  271. 'extract_money' => $data['extract_money'],
  272. 'financial_type' => $data['financial_type'],
  273. 'financial_account' => json_encode($financial_account),
  274. 'financial_status' => 0,
  275. 'mer_admin_id' => $data['mer_admin_id'],
  276. 'mark' => $datap['mark']??'',
  277. ];
  278. Db::transaction(function()use($merId,$ret,$data,$make){
  279. $this->dao->create($ret);
  280. $make->subMoney($merId,(float)$data['extract_money']);
  281. });
  282. }
  283. /**
  284. * TODO 商户列表
  285. * @param array $where
  286. * @param int $page
  287. * @param int $limit
  288. * @return array
  289. * @author Qinii
  290. * @day 3/19/21
  291. */
  292. public function getAdminList(array $where,int $page,int $limit)
  293. {
  294. $where['is_del'] = 0;
  295. $query = $this->dao->search($where)->with([
  296. 'merchant' => function($query){
  297. $query->field('mer_id,mer_name,is_trader,mer_avatar,mer_phone,mer_address');
  298. }
  299. ]);
  300. $count = $query->count();
  301. $list = $query->page($page, $limit)->select();
  302. return compact('count','list');
  303. }
  304. /**
  305. * TODO 取消/拒绝 变更状态返还余额
  306. * @param $merId
  307. * @param $id
  308. * @param $data
  309. * @author Qinii
  310. * @day 3/19/21
  311. */
  312. public function cancel(?int $merId,int $id,array $data)
  313. {
  314. $where = [
  315. 'financial_id' => $id,
  316. 'is_del' => 0,
  317. 'status' => 0
  318. ];
  319. if($merId) $where['mer_id'] = $merId;
  320. $res = $this->dao->getWhere($where);
  321. if(!$res) throw new ValidateException('数据不存在');
  322. if($res['financial_status'] == 1) throw new ValidateException('当前状态无法完成此操作');
  323. $merId = $merId?? $res['mer_id'];
  324. Db::transaction(function()use($merId,$res,$id,$data) {
  325. $this->dao->update($id,$data);
  326. app()->make(MerchantRepository::class)->addMoney($merId, (float)$res['extract_money']);
  327. });
  328. }
  329. /**
  330. * TODO
  331. * @param $id
  332. * @return \FormBuilder\Form
  333. * @author Qinii
  334. * @day 4/21/21
  335. */
  336. public function markForm($id)
  337. {
  338. $data = $this->dao->get($id);
  339. $form = Elm::createForm(Route::buildUrl('merchantFinancialMark', ['id' => $id])->build());
  340. $form->setRule([
  341. Elm::text('mark', '备注', $data['mark'])->required(),
  342. ]);
  343. return $form->setTitle('修改备注');
  344. }
  345. /**
  346. * TODO
  347. * @param $id
  348. * @return \FormBuilder\Form
  349. * @author Qinii
  350. * @day 4/21/21
  351. */
  352. public function adminMarkForm($id)
  353. {
  354. $data = $this->dao->get($id);
  355. $form = Elm::createForm(Route::buildUrl('systemFinancialMark', ['id' => $id])->build());
  356. $form->setRule([
  357. Elm::text('admin_mark', '备注', $data['admin_mark'])->required(),
  358. ]);
  359. return $form->setTitle('修改备注');
  360. }
  361. /**
  362. * TODO 详情
  363. * @param $id
  364. * @return array|\think\Model|null
  365. * @author Qinii
  366. * @day 4/22/21
  367. */
  368. public function detail($id,$merId=0)
  369. {
  370. $where[$this->dao->getPk()] = $id;
  371. if($merId) $where['mer_id'] = $merId;
  372. $data = $this->dao->getSearch($where)->with(['merchant' => function($query){
  373. $query->field('mer_id,mer_name,mer_avatar');
  374. }])->find();
  375. if(!$data) throw new ValidateException('数据不存在');
  376. return $data;
  377. }
  378. /**
  379. * TODO 头部统计
  380. * @return array
  381. * @author Qinii
  382. * @day 4/22/21
  383. */
  384. public function getTitle()
  385. {
  386. $make = app()->make(MerchantRepository::class);
  387. //应付商户金额 = 所有商户的余额之和
  388. $all = $make->search(['is_del' => 0])->sum('mer_money');
  389. //商户可提现金额 = (每个商户的余额 - 平台设置的最低提现额度) 之和
  390. $extract_minimum_line = systemConfig('extract_minimum_line') ?: 0;
  391. $ret = $make->search(['is_del' => 0])->where('mer_money','>',$extract_minimum_line)
  392. ->field("sum(mer_money - $extract_minimum_line) as money")
  393. ->find();
  394. $money = $ret->money;
  395. //申请转账的商户数 = 申请提现且未转账的商户数量
  396. $where = [
  397. "financial_status" => 0
  398. ];
  399. $query = $this->dao->getSearch($where)->where('status','>',-1);
  400. $count = $query->group('mer_id')->count();
  401. //申请转账的总金额 = 申请提现已通过审核,且未转账的申请金额 之和
  402. $where['status'] = 1;
  403. $all_ = $this->dao->getSearch($where)->sum('extract_money');
  404. $where['status'] = 0;
  405. $all_0 = $this->dao->getSearch($where)->sum('extract_money');
  406. $stat = [
  407. [
  408. 'className' => 'el-icon-s-goods',
  409. 'count' => $all,
  410. 'field' => '元',
  411. 'name' => '应付商户金额'
  412. ],
  413. [
  414. 'className' => 'el-icon-s-goods',
  415. 'count' => $money,
  416. 'field' => '元',
  417. 'name' => '商户可提现金额'
  418. ],
  419. [
  420. 'className' => 'el-icon-s-goods',
  421. 'count' => $count,
  422. 'field' => '个',
  423. 'name' => '申请转账的商户数'
  424. ],
  425. [
  426. 'className' => 'el-icon-s-goods',
  427. 'count' => $all_,
  428. 'field' => '元',
  429. 'name' => '申请转账的总金额'
  430. ],
  431. [
  432. 'className' => 'el-icon-s-goods',
  433. 'count' => $all_0,
  434. 'field' => '元',
  435. 'name' => '待审核的总金额'
  436. ],
  437. ];
  438. return $stat;
  439. }
  440. }