301 lines
10 KiB
PHP
301 lines
10 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Admin;
|
||
|
||
use App\Models\Bank;
|
||
use App\Models\BankRel;
|
||
use App\Models\DrawMoney;
|
||
use App\Models\HandeDoLog;
|
||
use App\Models\Merchant;
|
||
use App\Models\MerchantCommission;
|
||
use App\Models\MerchantMoneyLog;
|
||
use App\Models\MerchantRatio;
|
||
use App\Models\MerchantWallet;
|
||
use App\Models\Order;
|
||
use App\Services\SearchServices;
|
||
use Illuminate\Http\Request;
|
||
use App\Http\Controllers\Controller;
|
||
|
||
class DrawMoneyController extends BaseDefaultController
|
||
{
|
||
//
|
||
public function setPagesInfo()
|
||
{
|
||
$this->pages = [
|
||
'name' => '申请提现'
|
||
];
|
||
}
|
||
|
||
|
||
public function indexData()
|
||
{
|
||
|
||
return ['status' => DrawMoney::drawStatus(), 'merchant' => Merchant::get()];
|
||
}
|
||
|
||
public function shareData($show = '')
|
||
{
|
||
if ($show) {
|
||
$merchant_id = $show->merchant_id;
|
||
//商户钱包信息
|
||
$data['wallte'] = MerchantWallet::where('merchant_id', $merchant_id)->first();
|
||
//该商户支付交易结算总额
|
||
$data['order_total_money'] = Order::getMerchantTotalMoney($merchant_id);
|
||
//推广总佣金
|
||
$data['commisson_money'] = MerchantCommission::getMoneyTotal($merchant_id);
|
||
//推广结算成功佣金
|
||
$data['commisson_ok_money'] = MerchantCommission::getMoneyTotal($merchant_id, 1);
|
||
//申请提现资金
|
||
$data['draw_money_wait_number'] = $this->setModel()::getDrawMoney(['status_in' => [0, 2]]);
|
||
//今天冻结结算资金
|
||
$day_seizure = $this->daySeizure($merchant_id);
|
||
$data['today_money'] = $day_seizure['day_total_money'];
|
||
$data['day_seizure_money'] = $day_seizure['day_seizure_money'];
|
||
//完成提现总额
|
||
$data['draw_money_ok_total'] = DrawMoney::getDrawMoney(['status' => 1, 'merchant_id' => $merchant_id]);
|
||
//提现次数
|
||
$data['draw_info'] = $this->getDrawMoneyStatus($merchant_id);
|
||
|
||
return $data;
|
||
}
|
||
|
||
}
|
||
|
||
//已经申请提现多少,冻结资金+已提现金额未处理=综合总金额
|
||
public function getWaitDrawMoney()
|
||
{
|
||
|
||
}
|
||
|
||
//提现次数
|
||
public function getDrawMoneyStatus($merchant_id)
|
||
{
|
||
//提现总额今天
|
||
$data['draw_total'] = $this->setModel()::where('merchant_id', $merchant_id)->whereRaw('DATE_FORMAT(created_at,"%Y-%m-%d") =? ', [date('Y-m-d')])->sum('draw_money');
|
||
$data['draw_number'] = $this->setModel()::where('merchant_id', $merchant_id)->whereRaw('DATE_FORMAT(created_at,"%Y-%m-%d") =? ', [date('Y-m-d')])->count('*');
|
||
return $data;
|
||
}
|
||
|
||
//每天扣押款
|
||
public function daySeizure($merchant_id)
|
||
{
|
||
|
||
//统计今天成就结算总额
|
||
$to_day_total_money = Order::getMerchantTotalToDayMoney($merchant_id, 1);
|
||
$day_seizure_money = 0;
|
||
//每天押款多少比率
|
||
if ($to_day_total_money > 0) {
|
||
$day_seizure_ratio = Merchant::getDayDrawRatio($merchant_id);
|
||
$day_seizure_money = round($to_day_total_money * $day_seizure_ratio / 100, 2);
|
||
}
|
||
return ['day_total_money' => $to_day_total_money, 'day_seizure_money' => $day_seizure_money];
|
||
}
|
||
|
||
|
||
protected function extendValidate($request, $model, $id)
|
||
{
|
||
//检查是否金额对的上
|
||
|
||
|
||
}
|
||
|
||
|
||
public function checkRule($id = '')
|
||
{
|
||
if (!$id) {
|
||
return [
|
||
|
||
|
||
];
|
||
}
|
||
return [
|
||
|
||
|
||
];
|
||
}
|
||
|
||
public function setErrorMsg()
|
||
{
|
||
$messages = [
|
||
'draw_money.required' => '提现金额不能为空',
|
||
'money.required' => '到账金额不能为空',
|
||
'fee_money.required' => '手续费不能为空',
|
||
'bank_realname.required' => '收款人不能为空',
|
||
];
|
||
return $messages;
|
||
}
|
||
|
||
public function setModel()
|
||
{
|
||
return new DrawMoney();
|
||
}
|
||
|
||
public function checkSelf($show)
|
||
{
|
||
|
||
if ($show->model_id != $this->getMerchantId() && $show->model_type == 'merchant') {
|
||
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public function postDataDb($request, $id = '')
|
||
{
|
||
$data = $request->all();
|
||
//1是成功,3是失败
|
||
if (in_array($request->input('status'), [3, 1])) {
|
||
//提现处理时间
|
||
$data['success_at'] = date('Y-m-d H:i:s');
|
||
}
|
||
$data['admin_name'] = admin('realname');
|
||
$data['admin_id'] = admin('id');
|
||
|
||
$data['begindb'] = 1;
|
||
return $data;
|
||
}
|
||
|
||
public function saveAfter($request, $model, $id = '')
|
||
{
|
||
|
||
//取得最后的一次日志记录
|
||
$draw_log = HandeDoLog::getLastLog('draw_money', $model->id);
|
||
|
||
if (empty($draw_log)) {
|
||
|
||
//插入日志
|
||
$do_r = HandeDoLog::insertLog('draw_money', $model->id, $request->input('status'), '提现操作');
|
||
//1表示提现成功
|
||
if ($do_r->do_value == 1) {
|
||
//提现成功,更新钱包的提现金额
|
||
$waller_r = MerchantWallet::moneyIncr($model->merchant_id, $model->draw_money, 'draw_money');
|
||
return $waller_r;
|
||
}
|
||
//如果是提现失败
|
||
if ($do_r->do_value == 3) {
|
||
//提现失败,更新钱包的提现金额
|
||
$money_log = MerchantMoneyLog::drawMoney($model->merchant_id, $model->draw_money, '提现失败,撤回资金', $model->id);
|
||
//增加钱包余额
|
||
$waller_r = MerchantWallet::moneyIncr($model->merchant_id, $model->draw_money);
|
||
if ($waller_r && $money_log) {
|
||
return 1;
|
||
}
|
||
}
|
||
|
||
} else {
|
||
//如果已经有处理过
|
||
//判断最近这次处理是什么类型
|
||
if ($draw_log['do_value'] == 1) {
|
||
//已经提现成功,不给操作,返回false;
|
||
return false;
|
||
}
|
||
//未处理和正在处理都是返回空的真
|
||
if (in_array($draw_log['do_value'], [0, 2])) {
|
||
//准备处理
|
||
if ($request->input('status') == 1) {
|
||
//提现成功,更新钱包的提现金额
|
||
$waller_r = MerchantWallet::moneyIncr($model->merchant_id, $model->draw_money, 'draw_money');
|
||
return $waller_r;
|
||
} else {
|
||
return true;
|
||
}
|
||
}
|
||
//提现失败
|
||
if ($draw_log['do_value'] == 3) {
|
||
//已经提现成功,不给操作,返回false
|
||
$money_log = MerchantMoneyLog::drawMoney($model->merchant_id, $model->draw_money, '提现失败,撤回资金', $model->id);
|
||
//增加钱包余额
|
||
$waller_r = MerchantWallet::moneyIncr($model->merchant_id, $model->draw_money);
|
||
if ($waller_r && $money_log) {
|
||
return 1;
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
}
|
||
|
||
|
||
public function apiJson(Request $request)
|
||
{
|
||
$offset = $request->input('page', 1);
|
||
$pagesize = $request->input('limit', 1);
|
||
$offset = ($offset - 1) * $pagesize;
|
||
|
||
$order_id = $request->input('sort', 'id');
|
||
$order_type = $request->input('order', 'desc');
|
||
$debug = $request->input('debug', 0);
|
||
$model = $this->setModel();
|
||
|
||
$search = new SearchServices($model, $request->all(), 'draw_money');
|
||
$model = $search->getModel();
|
||
|
||
$total = $model->count();
|
||
$result = $model->with(['merchants'=>function($query){
|
||
$query->with(['froms'=>function($query2){
|
||
$query2->with('froms');
|
||
}]);
|
||
}])->skip($offset)->orderBy($order_id, $order_type)->orderBy('id', 'desc')->take($pagesize)->get();;
|
||
$narr = array();
|
||
//找出merchant_id
|
||
$merchant_id=[];
|
||
foreach ($result as $k => $v) {
|
||
$merchant_id[]=$v->merchant_id;
|
||
}
|
||
//查找费率表
|
||
|
||
foreach ($result as $k => $v) {
|
||
$v['edit_url'] = action($this->route['controller_name'] . '@edit', ['id' => $v->id]);
|
||
$v['edit_post_url'] = action($this->route['controller_name'] . '@update', ['id' => $v->id]);
|
||
$v['draw_type_name'] = $v->DrawTypeName;
|
||
$v['merchant_name'] = $v->merchants['name'];
|
||
$v['show_url'] = admin_url('DrawMoney', 'show', ['id' => $v->id]);
|
||
$v['merchant_ratio']=$v->merchants['ratio'];
|
||
$v['next_ratio']=$v->merchants['froms']['ratio'];
|
||
$v['next_next_ratio']=$v->merchants['froms']['froms']['ratio'];
|
||
$v['merchant_level']=$v->merchants['level'];
|
||
$v['yl_money']=0;
|
||
//原始交易金额
|
||
$origin_money=$v['draw_money']/(1000-$v['merchant_ratio'])*1000;
|
||
$v['origin_money']=round($origin_money,2);
|
||
//3级,代理,股东
|
||
|
||
//商户
|
||
if($v['next_ratio']=='' && $v['next_next_ratio']=='')
|
||
{
|
||
//提现金额/1-费率*100=原始金额*盈利费率=最低盈利金额
|
||
$v['yl_money']= $v['origin_money']*(($v['merchant_ratio']-7)/1000);
|
||
}elseif( $v['next_ratio'] && $v['next_next_ratio']=='')
|
||
{
|
||
$v['yl_money']= $v['origin_money']*(($v['next_ratio']-7)/1000);
|
||
}else
|
||
{
|
||
$v['yl_money']= $v['origin_money']*(($v['next_next_ratio']-7)/1000);
|
||
}
|
||
$v['yl_money']=round( $v['yl_money'],2);
|
||
$text_class = '';
|
||
if ($v['status'] == 1) {
|
||
$text_class = "text-grep";
|
||
}
|
||
if ($v['status'] == 3) {
|
||
$text_class = "text-danger";
|
||
}
|
||
$v['status_name_tpl'] = '<span class="' . $text_class . '">' . $v->StatusName . '</span>';
|
||
$narr[] = $v;
|
||
|
||
}
|
||
$json = [
|
||
"status" => 1,
|
||
'code' => $total > 0 ? 0 : 1,
|
||
'msg' => $total > 0 ? '请求数据成功' : '暂无数据',
|
||
'count' => $total,
|
||
'data' => $narr
|
||
];
|
||
|
||
if ($debug) {
|
||
return $this->jsonDebug($json);
|
||
}
|
||
return response()->json($json);
|
||
}
|
||
}
|