FinanceController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. namespace addons\Store\api\modules\v2\controllers;
  3. use addons\Store\common\models\Store;
  4. use addons\Store\common\models\StoreAdmin;
  5. use addons\Store\common\models\StoreBank;
  6. use addons\Store\common\models\StoreSettings;
  7. use addons\Store\common\models\StoreWithdrawLog;
  8. use addons\Store\common\enums\StoreEnum;
  9. use addons\Store\common\services\FinanceService;
  10. use common\enums\ApiStatusEnum;
  11. use common\enums\StatusEnum;
  12. use common\models\user\User;
  13. use common\globals\GlobalInvoke;
  14. use common\enums\WithdrawWayEnum;
  15. use common\models\common\PlatformSetting;
  16. use Yii;
  17. use api\controllers\OnAuthController;
  18. /**
  19. * 默认控制器
  20. *
  21. * Class DefaultController
  22. * @package addons\Store\api\modules\v2\controllers
  23. */
  24. class FinanceController extends BaseController
  25. {
  26. public $modelClass = '';
  27. /**
  28. * 不用进行登录验证的方法
  29. *
  30. * 例如: ['index', 'update', 'create', 'view', 'delete']
  31. * 默认全部需要验证
  32. *
  33. * @var array
  34. */
  35. protected $authOptional = ['withdrawal','edit-store-bank','finance'];
  36. /**
  37. * 提现申请
  38. * @Author bing
  39. * @DateTime 2022-04-26 18:04:50
  40. * @return void
  41. * @copyright: Copyright (c) 2022 广东七件事集团
  42. */
  43. public function actionWithdrawal()
  44. {
  45. if (\Yii::$app->request->isGet) {
  46. $store = Store::getOne([['id' => $this->store_id]], true, [], false, 'id,user_id,balance');
  47. $user = User::getOne([['id' => $store['user_id']]], true, [], false, 'id,nickname,avatar_url,mobile');
  48. $store_banks = StoreBank::lists(['where' => [['store_id' => $store['id']], ['status' => StatusEnum::ENABLED]], 'id,mall_id,store_id,username,bank,card_no']);
  49. $balance_cash_service_fee = \Yii::$app->services->setting->mall->get($this->mall_id, 'withdraw_balance.balance_cash_service_fee');
  50. $store['balance_cash_service_fee'] = $balance_cash_service_fee;
  51. if(!empty($store_banks)){
  52. foreach ($store_banks as &$item){
  53. $item['bank_name'] = $item['bank'];
  54. $item['bank_card_no'] = $item['card_no'];
  55. }
  56. }
  57. return $this->success('ok', ['user' => $user, 'store' => $store, 'store_banks' => $store_banks], ApiStatusEnum::CODE_SUCCESS, 0);
  58. } else {
  59. $params = \Yii::$app->request->post();
  60. if (empty($params['withdrawal_money'])) return $this->error('请输入金额');
  61. if ($params['withdrawal_money'] < 0) return $this->error('输入金额必须大于0');
  62. $store = Store::getOne([['id' => $this->store_id]], false, [], false, 'id,mall_id,user_id,balance');
  63. $balance_cash_service_fee = \Yii::$app->services->setting->mall->get($this->mall_id, 'withdraw_balance.balance_cash_service_fee');
  64. if ($params['withdrawal_money'] > $store['balance']) return $this->error('输入金额大于可提现金额');
  65. $params['mall_id'] = $this->mall_id;
  66. $params['store_id'] = $this->store_id;
  67. $params['balance_cash_service_fee'] = $balance_cash_service_fee;
  68. if (empty($params['remark'])) $params['remark'] = '';
  69. $res = StoreWithdrawLog::withdrawApply($params);
  70. if ($res == false) return $this->error(StoreWithdrawLog::getStaticError());
  71. return $this->success('操作成功');
  72. }
  73. }
  74. //编辑银行卡
  75. public function actionEditStoreBank()
  76. {
  77. if (\Yii::$app->request->isPost) {
  78. $params = \Yii::$app->request->post();
  79. $params['mall_id'] = $this->mall_id;
  80. $params['store_id'] = $this->store_id;
  81. $res = StoreBank::setData($params);
  82. if ($res == false) $this->error('操作失败');
  83. return $this->success();
  84. } else {
  85. $params = \Yii::$app->request->get();
  86. $store_bank_info = StoreBank::getOne([['id' => $params['id']], ['mall_id' => $this->mall_id], ['store_id' => $this->store_id]], true);
  87. return $this->success('ok', ['store_bank_info' => $store_bank_info],ApiStatusEnum::CODE_SUCCESS,0);
  88. }
  89. }
  90. /**
  91. * 资产
  92. * @return mixed|void
  93. */
  94. public function actionFinance()
  95. {
  96. $store = Store::findOne(['id' => $this->store_id]);
  97. // $balance_cash_service_fee = \Yii::$app->services->setting->mall->get($this->mall_id, 'withdraw_balance.balance_cash_service_fee');
  98. $user = User::getOne([['id' => $store->user_id]], true, [], false, 'id,nickname,avatar_url,mobile');
  99. $finance = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'finance');
  100. $storeSetting = StoreSettings::findOne(['store_id' => $this->store_id]);
  101. $freeze_is_enable = 0;
  102. $finance_name = '资产';
  103. if(!empty($finance)){
  104. if(!empty($finance['freeze_is_enable'])){
  105. $freeze_is_enable = $finance['freeze_is_enable'];
  106. }
  107. if(!empty($finance['finance_name'])){
  108. $finance_name = $finance['finance_name'];
  109. }
  110. }
  111. //提现条件
  112. if(isset($finance['balance_cash_type'])){
  113. $cashType = json_decode($finance['balance_cash_type'],true);
  114. }else{
  115. //默认银行卡/余额提现
  116. $cashType = ['balance','bank'];
  117. }
  118. $balance_cash_service_fee = 0;
  119. $balance_cash_service_fee_limit = 0;
  120. $balance_cash_service_fee_month = 0;
  121. if (!empty($storeSetting->is_enable_withdraw_setting)) {
  122. $finance['balance_cash_service_fee_limit'] = $storeSetting->balance_cash_service_fee_limit;
  123. $finance['balance_cash_service_fee_month'] = $storeSetting->balance_cash_service_fee_month;
  124. $finance['balance_cash_service_fee_diff'] = $storeSetting->balance_cash_service_fee_diff;
  125. $finance['balance_cash_service_fee'] = $storeSetting->balance_cash_service_fee;
  126. }
  127. if(!empty($finance['balance_cash_service_fee_limit'])){
  128. $balance_cash_service_fee_limit = $finance['balance_cash_service_fee_limit'];
  129. $balance_cash_service_fee_month = $finance['balance_cash_service_fee_month'];
  130. $balance_cash_service_fee = $finance['balance_cash_service_fee_diff'] ?? 0;
  131. }else{
  132. if(isset($finance['balance_cash_service_fee'])){
  133. $balance_cash_service_fee = $finance['balance_cash_service_fee'] ?? 0;
  134. }else{
  135. $balance_cash_service_fee = \Yii::$app->services->setting->mall->get($this->mall_id, 'withdraw_balance.balance_cash_service_fee');
  136. }
  137. }
  138. if (!$cashType) {
  139. return $this->error('系统未设置提现方式');
  140. }
  141. $cash__type_name = [];
  142. $get_system_setting = PlatformSetting::getConfByGroup('system', true);
  143. if (!empty($cashType)) {
  144. $cash_type = $cashType;
  145. $cash__type_name = [];
  146. $default_withdraw_way = WithdrawWayEnum::getDefaultWithDrawWayMap(WithdrawWayEnum::WITHDRAW_TYPE_2);
  147. $extension_withdraw_way = GlobalInvoke::exec(GlobalInvoke::INVOKE_GET_WITHDRAW_EXTENSION_WAY,$this->mall_id,['mall_id'=>$this->mall_id]);
  148. $all_withdraw = array_merge($default_withdraw_way,$extension_withdraw_way);
  149. $cash_type_keys = array_keys($all_withdraw);
  150. if (in_array('bank', $cash_type_keys) || in_array('joinpay', $cash_type_keys)) {
  151. $bank_card_list = StoreBank::lists(['where' => [['store_id' => $this->store_id], ['status' => StatusEnum::ENABLED]], 'select' => 'id,mall_id,store_id,username as payer_name,bank as bank_name,card_no as bank_card_no']);
  152. }
  153. foreach ($cash_type as $i => $item) {
  154. if (isset($all_withdraw[$item])) {
  155. $taxFee = 0;
  156. $name = '';
  157. if (isset($all_withdraw[$item])) {
  158. if (is_array($all_withdraw[$item])) {
  159. $name = $all_withdraw[$item]['name'] ?? '';
  160. $taxFee = $all_withdraw[$item]['taxFee'] ?? 0;
  161. } else {
  162. $name = $all_withdraw[$item];
  163. }
  164. }
  165. $cash__type_name[$i]['type'] = $item;
  166. $cash__type_name[$i]['taxFee'] = $taxFee;
  167. $cash__type_name[$i]['name'] = $name;
  168. $cash__type_name[$i]['img'] = $get_system_setting['api_url']['value'] . '/resources/img/payment/' . $item . '.png';
  169. if ('bank' == $item || 'joinpay' == $item) {
  170. $cash__type_name[$i]['bank_card_list'] = $bank_card_list;
  171. }
  172. }
  173. }
  174. }
  175. $return = [
  176. 'balance' => $store->balance, //可提现金额
  177. 'unsettled_amount' => $store->unsettled_amount,//待结算金额
  178. // 'store_banks' => $store_banks,
  179. 'balance_cash_service_fee' => $balance_cash_service_fee,
  180. 'balance_cash_service_fee_limit' => $balance_cash_service_fee_limit,
  181. 'balance_cash_service_fee_month' => $balance_cash_service_fee_month,
  182. 'freeze_is_enable' => $freeze_is_enable,
  183. 'finance_name' => $finance_name,
  184. 'user' => $user,
  185. 'cash_type_name' => $cash__type_name,
  186. ];
  187. return $this->success('操作成功', $return,ApiStatusEnum::CODE_SUCCESS,JSON_UNESCAPED_UNICODE);
  188. }
  189. /**
  190. * 添加银行卡
  191. * @return void
  192. */
  193. public function actionAddBankCard()
  194. {
  195. try {
  196. $params = \Yii::$app->request->post();
  197. $res = \Yii::$app->services->validate->validate($params, [
  198. [['username', 'bank', 'card_no'], 'required'],
  199. ['card_no', 'string', 'max' => 64],
  200. [['username'], 'string', 'max' => 50],
  201. [['bank'], 'string', 'max' => 255],
  202. ]);
  203. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  204. $bank_card_info = StoreBank::findONe(['mall_id' => $this->mall_id, 'card_no' => $params['card_no'], 'store_id' => $this->store_id, 'status' => StatusEnum::ENABLED]);
  205. if (!empty($bank_card_info)) {
  206. throw new \Exception('银行卡已经被绑定');
  207. }
  208. $params['mall_id'] = $this->mall_id;
  209. $params['store_id'] = $this->store_id;
  210. $res = StoreBank::setData($params);
  211. if (!$res) throw new \Exception(StoreBank::getStaticError());
  212. return $this->success('添加成功');
  213. } catch (\Exception $exception) {
  214. return $this->error('操作失败:' . $exception->getMessage());
  215. }
  216. }
  217. /**
  218. * 提现
  219. * @throws \Exception
  220. */
  221. public function actionWithdraw()
  222. {
  223. $params = \Yii::$app->request->post();
  224. $need_auth = GlobalInvoke::exec(GlobalInvoke::REAL_AUTH, $this->mall_id, ['user_id' => $this->user_id,'mall_id' => $this->mall_id,'type' => 'cash']);
  225. if($need_auth === true){
  226. //需要先进行实名认证
  227. return $this->error('请先进行实名认证',[],ApiStatusEnum::CODE_ERROR_NEED_REAL_AUTH);
  228. }
  229. $form = new StoreWithdrawLog();
  230. $params['mall_id'] = $this->mall_id;
  231. $params['user_id'] = $this->user_id;
  232. $params['store_id'] = $this->store_id;
  233. $form->attributes = $params;
  234. $data = $form->saveRecord($params);
  235. if ($data['state']) {
  236. return $this->success('操作成功', $data['data']);
  237. } else {
  238. return $this->error($data['msg']);
  239. }
  240. }
  241. /**
  242. * 获取提现列表
  243. *
  244. * @return void
  245. */
  246. public function actionGetWithdrawList()
  247. {
  248. $service = new FinanceService();
  249. $service->mall_id = $this->mall_id;
  250. $service->store_id = $this->store_id;
  251. $this->success(
  252. '操作成功',
  253. $service->getWithdrawList(
  254. $this->request->get('type', 0),
  255. $this->request->get('limit', 20),
  256. $this->platform
  257. )
  258. );
  259. }
  260. }