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]; } }