123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- <?php
- declare (strict_types = 1);
- namespace app\api\controller;
- use app\api\model\User;
- use app\api\model\user\UserIdcards;
- use app\api\model\user\Withdraw as WithdrawModel;
- use app\api\service\User as UserService;
- use app\common\model\store\Setting as SettingModel;
- use think\facade\Db;
- use app\common\library\helper;
- /**
- * 提现管理
- * Class Withdraw
- * @package app\api
- */
- class Withdraw extends Controller
- {
- /**
- * 提现列表
- */
- public function list()
- {
- $params = $this->request->param();
- $model = new WithdrawModel;
- $list = $model->getList($params);
- return $this->renderSuccess(compact('list'));
- }
- /**
- * 提现详情
- * @param $id 记录id
- */
- public function detail($id = 0)
- {
- $userId = UserService::getCurrentLoginUserId();
- $detail = WithdrawModel::where('id', $id)->where('user_id', $userId)->find();
- if (empty($detail)) {
- return $this->renderError('参数无效');
- }
- return $this->renderSuccess(compact('detail'));
- }
- public function apply0()
- {
- $user = UserService::getCurrentLoginUser(true);
- $args = $this->postData();
- if (empty($args['amount'])) {
- return $this->renderError('提现金额不能为空');
- }
- $args['create_time'] = date("Y-m-d H:i:s", time());
- $args['user_id'] = $user->user_id;
- $args['transaction_no'] = WithdrawModel::makeTransactionNo();
- $args['tx_account'] = $user->mobile;
- $args['real_name'] = '';
- $args['remark'] = '佣金提现';
- $args['transaction_type'] = 2; // 提现方式 1-支付宝 2-微信
- if (empty($user->open_id)) {
- return $this->renderError('您尚未绑定微信,请绑定后重试');
- }
- if ($args['amount'] <= 0) {
- return $this->renderError('提现金额不能为空');
- }
- if ($args['amount'] > $user->ktxyj_amount) {
- return $this->renderError('输入金额大于可提现佣金余额,无法提现,请重新输入');
- }
- // 判断用户是否当日提现次数超过10次,大于10次提示“您今日提现过于频繁,请次日再来~”
- $dayCount = WithdrawModel::where('user_id', $args['user_id'])
- ->whereBetween('create_time', [date('Y-m-d').' 00:00:00', date('Y-m-d').' 23:59:59'])
- ->count();
- if ($dayCount > 10) {
- return $this->renderError('您今日提现过于频繁,请次日再来~');
- }
- // 单次提现金额不能小于0.3元
- if ($args['amount'] < 0.3) {
- return $this->renderError('最低提现金额为0.3元');
- }
-
- // 判断用户是否当日提现金额超过5000元,当日累计申请提现(含本次)大于5000元时提示“您今日累计提现数值较大,请次日再来~”
- $dayAmount = WithdrawModel::where('user_id', $args['user_id'])
- ->whereBetween('create_time', [date('Y-m-d').' 00:00:00', date('Y-m-d').' 23:59:59'])
- ->sum('amount');
- if ($dayAmount + $args['amount'] > 5000) {
- return $this->renderError('您今日累计提现数值较大,请次日再来');
- }
- // 佣金免打税区间 佣金最低提现额度 佣金最高提现额度
- $withdrawSetting = SettingModel::getItem('withdrawal');
- // 获取用户年累计提现金额
- $yearYjAmount = WithdrawModel::where('user_id', $args['user_id'])
- ->whereBetween('pay_time',[date('Y').'-01-01 00:00:00', date('Y').'-12-31 23:59:59'])
- ->where('pay_state', 1)
- ->sum('amount');
- if ($withdrawSetting['free_tax_max'] > 0) {
- if ($yearYjAmount + $args['amount'] < $withdrawSetting['free_tax_max']) {
- $args['tax'] = 0;
- }
- else{
- $args['tax'] = $withdrawSetting['tax_ratio'] > 0 ? helper::bcadd($withdrawSetting['tax_ratio'] / 100 * $args['amount'],0,4) : 0;
- }
- } else {
- $args['tax'] = $withdrawSetting['tax_ratio'] > 0 ? helper::bcadd($withdrawSetting['tax_ratio'] / 100 * $args['amount'],0,4) : 0;
- }
- if ($withdrawSetting['withdraw_min'] > 0 && $args['amount'] < $withdrawSetting['withdraw_min']) {
- return $this->renderError('提现金额不能小于最低提现额度');
- }
- if ($withdrawSetting['withdraw_max'] > 0 && $args['amount'] > $withdrawSetting['withdraw_max']) {
- return $this->renderError('提现金额不能超过最高提现额度');
- }
- Db::startTrans();
- try {
- User::where('user_id', $args['user_id'])->update([
- 'ktxyj_amount' => $user->ktxyj_amount-$args['amount'],
- 'frozen_yj_amount' => $user->frozen_yj_amount+$args['amount'],
- ]);
- $userWithdraw = WithdrawModel::create($args);
- Db::commit();
- return $this->renderSuccess($userWithdraw->toArray(), '提交成功');
- } catch (\Exception $e) {
- Db::rollback();
- return $this->renderError('提交失败');
- }
- }
- public function apply()
- {
- $user = UserService::getCurrentLoginUser(true);
- $args = $this->postData();
- if (empty($args['amount'])) {
- return $this->renderError('提现金额不能为空');
- }
- $flag = UserIdcards::checkUserHasIdcard($user->user_id);
- if ($flag == false){
- return $this->renderSuccess(['needVerify'=>true],'请先完成实名认证');
- }
- $args['create_time'] = date("Y-m-d H:i:s", time());
- $args['user_id'] = $user->user_id;
- $args['transaction_no'] = WithdrawModel::makeTransactionNo();
- $args['tx_account'] = $user->mobile;
- $args['real_name'] = '';
- $args['remark'] = '佣金提现';
- $args['transaction_type'] = 2; // 提现方式 1-支付宝 2-微信
- if (empty($user->open_id)) {
- return $this->renderError('您尚未绑定微信,请绑定后重试');
- }
- if ($args['amount'] <= 0) {
- return $this->renderError('提现金额不能为空');
- }
- if ($args['amount'] > $user->ktxyj_amount) {
- return $this->renderError('输入金额大于可提现佣金余额,无法提现,请重新输入');
- }
- //待审核or已审核通过和未打款
- $withdrawing = WithdrawModel::where('user_id', $args['user_id'])
- ->where(function ($q){
- $q->where(function ($q){
- $q->where(['audit'=>1,'pay_state'=>0]);
- })->whereOr(function ($q){
- $q->where('audit',0);
- });
- })->find();
- if (!empty($withdrawing)) {
- return $this->renderError('您的提现正在审核中,请稍后再试');
- }
- // 判断用户是否当日提现次数超过10次,大于10次提示“您今日提现过于频繁,请次日再来~”
- $dayCount = WithdrawModel::where('user_id', $args['user_id'])
- ->whereBetween('create_time', [date('Y-m-d').' 00:00:00', date('Y-m-d').' 23:59:59'])
- ->count();
- if ($dayCount > 10) {
- return $this->renderError('您今日提现过于频繁,请次日再来~');
- }
- // 单次提现金额不能小于0.3元
- if ($args['amount'] < 0.3) {
- return $this->renderError('最低提现金额为0.3元');
- }
- // 判断用户是否当日提现金额超过5000元,当日累计申请提现(含本次)大于5000元时提示“您今日累计提现数值较大,请次日再来~”
- $dayAmount = WithdrawModel::where('user_id', $args['user_id'])
- ->whereBetween('create_time', [date('Y-m-d').' 00:00:00', date('Y-m-d').' 23:59:59'])
- ->sum('amount');
- //todo 5000要与商户平台保持一致
- if ($dayAmount + $args['amount'] > 5000) {
- return $this->renderError('您今日累计提现数值较大,请次日再来');
- }
- // 佣金免打税区间 佣金最低提现额度 佣金最高提现额度
- $withdrawSetting = SettingModel::getItem('withdrawal');
- // 获取用户年累计提现金额
- if ($withdrawSetting['withdraw_min'] > 0 && $args['amount'] < $withdrawSetting['withdraw_min']) {
- return $this->renderError('提现金额不能小于最低提现额度');
- }
- if ($withdrawSetting['withdraw_max'] > 0 && $args['amount'] > $withdrawSetting['withdraw_max']) {
- return $this->renderError('提现金额不能超过最高提现额度');
- }
- $currYearMonth = intval(date('Ym'));
- //本月累计提现金额
- $x = $this->getCurrMonthAmount($args['user_id'],$args['amount'],$currYearMonth);
- $args['amount_sum_monthly'] = $x;
- //本次需缴纳税费
- $taxArr = $this->getCurrWithdrawTax($args['user_id'],$x,$currYearMonth);
- if ($taxArr['currTax'] < 0 ){
- return $this->renderError('信息异常,请重试');
- }
- $args['tax'] = $taxArr['currTax'];
- //本月累计缴纳税费
- $args['tax_sum_monthly'] = bcadd(strval($taxArr['currTax']),strval($taxArr['hasTaxed']),2);
- $args['year_month'] = $currYearMonth;
- Db::startTrans();
- try {
- User::where('user_id', $args['user_id'])->update([
- 'ktxyj_amount' => $user->ktxyj_amount - $args['amount'],
- 'frozen_yj_amount' => $user->frozen_yj_amount + $args['amount'],
- ]);
- $userWithdraw = WithdrawModel::create($args);
- Db::commit();
- return $this->renderSuccess($userWithdraw->toArray(), '提交成功');
- } catch (\Exception $e) {
- Db::rollback();
- return $this->renderError('提交失败');
- }
- }
- /**
- * 计算当前提现金额应缴个人所得税
- * @return array|\think\response\Json
- * @throws \app\common\exception\BaseException
- */
- public function calTempTax(){
- $args = $this->postData();
- if (empty($args['amount'])) {
- return $this->renderError('提现金额不能为空');
- }
- $userId = UserService::getCurrentLoginUserId();
- $currYearMonth = intval(date('Ym'));
- //本月累计提现金额
- $x = $this->getCurrMonthAmount($userId,$args['amount'],$currYearMonth);
- log_record('x::'.$x,'debug');
- $currTax = $this->getCurrWithdrawTax($userId,$x,$currYearMonth)['currTax'];
- log_record('currTax::'.$currTax,'debug');
- return $this->renderSuccess(["tax"=>$currTax]);
- }
- //本月累计提现金额(含本次)
- private function getCurrMonthAmount($userId,$amount,$currYearMonth){
- $yearYjAmount = WithdrawModel::where('user_id', $userId)
- ->where('year_month',$currYearMonth)
- ->where('pay_state', 1)
- ->sum('amount');
- //本月累计提现金额
- return bcadd(strval($yearYjAmount),strval($amount),4);
- }
- /**
- * 本次提现应缴个人所得税
- * @param $userId
- * @param $x
- * @param $currYearMonth
- * @return array
- */
- private function getCurrWithdrawTax($userId,$x,$currYearMonth){
- $hasTaxed = WithdrawModel::where('user_id', $userId)
- ->where('year_month',$currYearMonth)
- ->where('pay_state', 1)
- ->sum('tax');
- log_record('$hasTaxed::'.$hasTaxed,'debug');
- $currTax = WithdrawModel::calculatorTax($x,$hasTaxed);
- return ['hasTaxed'=>$hasTaxed,'currTax'=>$currTax];
- }
- }
|