推广结算--更新

master
chenxiaojun 5 years ago
parent f1e8811df3
commit f244b42ab7

@ -8,6 +8,7 @@
*/
namespace Admin\Controller;
use Admin\Model\SpendModel;
use Admin\Model\WithdrawModel;
use Think\Think;
use Org\RedisSDK\Redis;
@ -1059,22 +1060,49 @@ public function auto_rrdae(){
$this->promoteWithdraw(2);
}
//会长周结月补点
public function promoteWithdrawRecoup()
{
$this->promoteWithdraw(3);
}
//会长提现 $settlement 1-周结算 2-月结算
private function promoteWithdraw($settlement = 1, $initial = false)
{
$withdrawModel = new WithdrawModel();
$map['id'] = 84;
$map['level'] = 1;
$map['ver_status'] = 1;
$map['settlement_type'] = $settlement;
$promotes = M('promote', 'tab_')->field('id, account, chain')->where($map)->select();
$map['settlement_type'] = ($settlement == 3) ? 1 : $settlement;
$promotes = M('promote', 'tab_')->field('id, account, chain, settlement_type')->where($map)->select();
$success = 0;
$error_withdraw = 0;
$error_spend = 0;
$error_balance = 0;
$error_unset = 0;
$error_repeat = 0;
if (!empty($promotes)) {
foreach ($promotes as $promote) {
$result = $this->promoteWithdrawByPromote($promote, $initial);
if ($settlement == 3) {
$result = $withdrawModel->promoteWithdrawRecoupByPromote($promote);
} else {
switch ($promote['settlement_type']) {
case 1:
$result = $withdrawModel->promoteWithdrawWeeklyByPromote($promote, $initial);
break;
case 2:
$result = $withdrawModel->promoteWithdrawPerMonthByPromote($promote, $initial);
break;
}
}
switch ($result) {
case -4:
$error_repeat++;
break;
case -3:
$error_unset++;
break;
case -2:
$error_balance++;
break;
@ -1093,127 +1121,4 @@ public function auto_rrdae(){
echo "success{$success} error_withdraw{$error_withdraw} error_spend{$error_spend} error_balance{$error_balance}";
}
private function promoteWithdrawWeeklyByPromote($promote, $initial = false)
{
$promoteMap['chain'] = ['like', "{$promote['chain']}{$promote['id']}/%"];
$promoteIds = M('promote', 'tab_')->where($promoteMap)->getField('id', true);
$promoteIds[] = $promote['id'];
$settlementType = $promote['settlement_type'];
$settlementEndTime = strtotime(date('Y-m-d')) - 1;
}
private function promoteWithdrawByPromote($promote, $initial = false)
{
$promoteMap['chain'] = ['like', "{$promote['chain']}{$promote['id']}/%"];
$promoteIds = M('promote', 'tab_')->where($promoteMap)->getField('id', true);
$promoteIds[] = $promote['id'];
$settlementType = $promote['settlement_type'];
$settlementEndTime = strtotime(date('Y-m-d')) - 1;
if ($initial) {
$settlementType = 5;
$settlementBeginTime = 0;
} else {
switch ($promote['settlement_type']) {
case 2:
$settlementBeginTime = strtotime(date('Y-m', strtotime('-1 month', time())));
break;
case 1:
default:
$settlementBeginTime = strtotime(date('Y-m-d', strtotime('-7 day', time())));
break;
}
}
$spendMap['pay_status'] = 1;
$spendMap['selle_status'] = 0;
$spendMap['promote_id'] = ['in', $promoteIds];
$spendMap['pay_time'] = ['between', [$settlementBeginTime, $settlementEndTime]];
$spendModel = M('spend', 'tab_');
$promoteGameRatioMap['status'] = 1;
$promoteGameRatioMap['promote_id'] = $promote['id'];
$promoteGameRatios = D('promote_game_ratio')->field('game_id, ratio, turnover_ratio, begin_time, end_time')
->where($promoteGameRatioMap)
->select();
if (empty($promoteGameRatios)) {
$balance = $spendModel->field("sum(if(selle_ratio > 0, pay_amount * selle_ratio, 0)) as balance")
->where($spendMap)
->find()['balance'];
} else {
$balance = 0;
foreach ($promoteGameRatios as $promoteGameRatio) {
if ($settlementType == 1) {
$thisSettlementBeginTime = $settlementBeginTime;
$thisSettlementEndTime = $settlementEndTime;
if ($promoteGameRatio['begin_time'] > $settlementBeginTime) {
$thisSettlementBeginTime = $promoteGameRatio['begin_time'];
} elseif (!empty($promoteGameRatio['end_time']) && $promoteGameRatio['end_time'] < $settlementEndTime) {
$thisSettlementEndTime = $promoteGameRatio['end_time'];
}
$spendMap['pay_time'] = ['between', [$thisSettlementBeginTime, $thisSettlementEndTime]];
$balance += $spendModel->field("sum(if(selle_ratio > 0, pay_amount * selle_ratio, 0)) as balance")
->where($spendMap)
->find()['balance'];
} elseif ($settlementType == 2) {
if ($promoteGameRatio['begin_time'] > $settlementBeginTime || (!empty($promoteGameRatio['end_time']) && $promoteGameRatio['end_time'] < $thisTime) || empty($promoteGameRatio['turnover_ratio'])) {
$spendMap['game_id'] = $promoteGameRatio['game_id'];
$balance += $spendModel->field("sum(if(selle_ratio > 0, pay_amount * selle_ratio, 0)) as balance")
->where($spendMap)
->find()['balance'];
}
if (empty($promoteGameRatio['turnover_ratio'])) {
}
}
}
$sumAmount = $spendModel->field("sum(pay_amount) as sum_amount")
->where($spendMap)
->find()['sum_amount'];
}
$balance = bcdiv($balance, 100, 2);
if ($balance < 100) {
return -2;//余额不足
}
$thisTime = time();
$add['sum_money'] = $balance;
$add['promote_id'] = $promote['id'];
$add['promote_account'] = $promote['account'];
$add['create_time'] = $thisTime;
$add['status'] = 0;
$add['widthdraw_number'] = D('withdraw')->produceWithdrawNumber();
$add['settlement_begin_time'] = $settlementBeginTime;
$add['settlement_end_time'] = $settlementEndTime;
$add['spend_ids'] = '';
$add['op_id'] = 0;
$add['op_type'] = 3;
$add['last_op_id'] = $add['op_id'];
$add['last_op_type'] = 3;
$add['settlement_type'] = $settlementType;
M()->startTrans();//开启事物
$withdrawId = D('withdraw')->add($add);
if (!$withdrawId) {
M()->rollback();//事物回滚
return 0;//提现失败
}
$save['selle_status'] = 1;
$save['selle_time'] = $thisTime;
$save['withdraw_id'] = $withdrawId;
$res = $spendModel->where($spendMap)->save($save);
if ($res === false) {
D('withdraw')->delete($withdrawId);
M()->rollback();//事物回滚
return -1;
}
M()->commit();//事物提交
return 1;
}
}

@ -31,6 +31,8 @@ class QueryController extends ThinkController
public function settlement($p = 0)
{
$settlementType = intval(I('settlement_type', 0));
$settlementTypeList = WithdrawModel::$settlementTypeList;
$page = intval(I('p', 1)); //默认显示第一页数据
$row = intval(I('row', 10));
$group = I('group', 1);
@ -39,13 +41,50 @@ class QueryController extends ThinkController
$promoteId = I('promote_id', 0);
if ($group == 1) {
if (empty($promoteId)) {
unset($settlementTypeList[WithdrawModel::SETTLEMENT_TYPE_OTHER]);
$time = I('time', '');
if (empty($promoteId) || empty($settlementType) || empty($time)) {
$this->assign('settlementTypeList', $settlementTypeList);
$this->display();
} else {
$promoteIds = M('promote', 'tab_')->where(['chain' => ['like', "/{$promoteId}/%"]])->getField('id', true);
$promoteIds[] = $promoteId;
$beginTime = I('timestart', get_lastweek_name(7));
$endTime = I('timeend', get_lastweek_name(1));
if ($settlementType == 1) {
$time = explode(' 至 ', $time);
if (count($time) != 2) {
$this->display();die;
}
$beginTime = strtotime($time[0]);
$endTime = strtotime($time[1]) + 3600 * 24 - 1;
} else {
$beginTime = strtotime($time);
$endTime = strtotime('+1 month', $beginTime) - 1;
}
$promote = M('promote', 'tab_')->find($promoteId);
if (empty($promote) || $promote['level'] != 1 || empty($promote['settlement_type'])) {
$this->error('网络异常');
}
if ($promote['settlement_type'] == 1) {
unset($settlementTypeList[WithdrawModel::SETTLEMENT_TYPE_MONTH]);
} else {
unset($settlementTypeList[WithdrawModel::SETTLEMENT_TYPE_WEEK]);
unset($settlementTypeList[WithdrawModel::SETTLEMENT_TYPE_RECOUP]);
}
if ($settlementType == 3) {
$withdrawMap['promote_id'] = $promoteId;
$withdrawMap['status'] = ['neq', -2];
$withdrawMap['settlement_begin_time'] = $beginTime;
$withdrawMap['settlement_end_time'] = $endTime;
$withdraw = D('withdraw')->where($withdrawMap)->find();
if (!empty($withdraw)) {
$this->assign('settlementTypeList', $settlementTypeList);
$this->display();die;
}
}
$sortBy = I('sort_by', '');
$sortColumn = I('sort_column', '');
$order = 'day';
@ -59,8 +98,6 @@ class QueryController extends ThinkController
} elseif ($sortBy == 2) {
$orderType = 'asc';
}
$beginTime = strtotime($beginTime);
$endTime = strtotime($endTime) + 3600 * 24 - 1;
$spendMap['tab_spend.pay_status'] = 1;
$spendMap['tab_spend.selle_status'] = 0;
@ -96,15 +133,19 @@ class QueryController extends ThinkController
}
if (!empty($records)) {
$promote = M('promote', 'tab_')->find($promoteId);
foreach ($records as &$record) {
$record['promote_account'] = $promote['account'] ?? '未知';
$record['settlement_amount'] = bcdiv($record['settlement_amount'], 100, 2);
}
}
if ($settlementType == 3) {
$totalField = 'sum(if(tab_spend.selle_ratio > 0, tab_spend.pay_amount * tab_spend.selle_ratio, 0)) as settlement_amount';
$zTotal = null_to_0(M('spend', 'tab_')->field($totalField)->where($spendMap)->find()['settlement_amount']);//累计
} else {
$totalField = 'sum(tab_spend.pay_amount) as sum_amount';
$zTotal = null_to_0(M('spend', 'tab_')->field($totalField)->where($spendMap)->find()['sum_amount']);//累计
}
$zTotal = bcdiv($zTotal, 100 ,2);
if(I('export', 0) == 1) {
@ -139,6 +180,7 @@ class QueryController extends ThinkController
if($page) {
$this->assign('_page', $page);
}
$this->assign('settlementTypeList', $settlementTypeList);
$this->assign('records', $records);
$this->assign('zTotal', $zTotal);
$this->assign('commonset', M('Kuaijieicon')->where(['url' => 'Query/settlement', 'status' => 1])->find());
@ -151,7 +193,6 @@ class QueryController extends ThinkController
$withdrawNumber = I('widthdraw_number', '');
$status = intval(I('status', ''));
$opType = intval(I('op_type', 0));
$settlementType = intval(I('settlement_type', 0));
if ($createTime) {
$createTimeEnd = $createTime + 3600 * 24 - 1;
$map['create_time'] = ['between', [$createTime, $createTimeEnd]];
@ -244,6 +285,7 @@ class QueryController extends ThinkController
if($page) {
$this->assign('_page', $page);
}
$this->assign('settlementTypeList', $settlementTypeList);
$this->assign('records', $records);
$this->display();
}
@ -266,22 +308,22 @@ class QueryController extends ThinkController
public function settlementWithdraw()
{
$beginTime = strtotime(I('begin_time', ''));
$endTime = strtotime(I('end_time', ''));
$promoteId = intval(I('promote_id', 0));
if ($beginTime == 0 || $endTime == 0) {
$settlementType = intval(I('settlement_type', 0));
$time = I('time', '');
if (empty($promoteId)) {
$data['status'] = 0;
$data['msg'] = '时间参数错误';
$data['msg'] = '请选择会长账号';
$this->ajaxReturn($data);
}
if ($beginTime > $endTime) {
if (empty($settlementType)) {
$data['status'] = 0;
$data['msg'] = '开始时间必须小于等于结束时间';
$data['msg'] = '请选择提现模式';
$this->ajaxReturn($data);
}
if ($promoteId == 0) {
if (empty($time)) {
$data['status'] = 0;
$data['msg'] = '请选择会长账号';
$data['msg'] = '请选择充值时间';
$this->ajaxReturn($data);
}
@ -301,78 +343,75 @@ class QueryController extends ThinkController
$data['msg'] = '该会长未通过资质认证审核,暂时无法提现';
$this->ajaxReturn($data);
}
$endTime += 3600 * 24 - 1;
$spendModel = M('spend', 'tab_');
$map['chain'] = ['like', $promote['chain'] . $promote['id'] . '/%'];
$promoteIds = M('promote', 'tab_')->where($map)->getField('id', true);
$promoteIds[] = $promote['id'];
$promoteIds = implode(',', $promoteIds);
$map = [];
$map['pay_status'] = 1;
$map['selle_status'] = 0;
$map['promote_id'] = ['in', $promoteIds];
$map['pay_time'] = ['between', [$beginTime, $endTime]];
$balance = $spendModel->field("sum(pay_amount * selle_ratio) as balance")
->where($map)
->find()['balance'];
$balance = bcdiv($balance, 100, 2);
if ($balance < 100) {
if ($promote['settlement_type'] == 1) {
if (!in_array($settlementType, [1, 3])) {
$data['status'] = 0;
$data['msg'] = '累计结算低于100元无法提现';
$data['msg'] = '参数异常';
$this->ajaxReturn($data);
}
$spendIds = $spendModel->where($map)->getField('id', true);
if (empty($spendIds)) {
} else {
if ($settlementType != 2) {
$data['status'] = 0;
$data['msg'] = '数异常';
$data['msg'] = '参数异常';
$this->ajaxReturn($data);
}
}
$thisTime = time();
$add['sum_money'] = $balance;
$add['promote_id'] = $promote['id'];
$add['promote_account'] = $promote['account'];
$add['create_time'] = $thisTime;
$add['status'] = 0;
$add['widthdraw_number'] = D('withdraw')->produceWithdrawNumber();
$add['settlement_begin_time'] = $beginTime;
$add['settlement_end_time'] = $endTime;
$add['spend_ids'] = '';
$add['op_id'] = is_login();
$add['op_type'] = 2;
$add['last_op_id'] = $add['op_id'];
$add['last_op_type'] = 2;
$add['settlement_type'] = 3;
$add['game_ratio'] = '';
M()->startTrans();//开启事物
$withdrawId = M('withdraw', 'tab_')->add($add);
if (!$withdrawId) {
M()->rollback();//事物回滚
if ($settlementType == 1) {
$time = explode(' 至 ', $time);
if (count($time) != 2) {
$data['status'] = 0;
$data['msg'] = '提现失败';
$data['msg'] = '参数异常';
$this->ajaxReturn($data);
}
$beginTime = strtotime($time[0]);
$endTime = strtotime($time[1]) + 3600 * 24 - 1;
} else {
$beginTime = strtotime($time);
$endTime = strtotime('+1 month', $beginTime) - 1;
}
$map = [];
$map['id'] = ['in', $spendIds];
$save['selle_status'] = 1;
$save['selle_time'] = $thisTime;
$save['withdraw_id'] = $withdrawId;
$res = $spendModel->where($map)->save($save);
if ($res === false) {
M('withdraw', 'tab_')->delete($withdrawId);
M()->rollback();//事物回滚
$data['status'] = 0;
$data['msg'] = '提现失败';
$this->ajaxReturn($data);
$withdrawModel = new WithdrawModel();
$data['begin_time'] = $beginTime;
$data['end_time'] = $endTime;
$data['op_id'] = is_login();
$data['op_type'] = 2;
switch ($settlementType) {
case 1:
$result = $withdrawModel->promoteWithdrawWeeklyByPromote($promote, false, $data);
break;
case 2:
$result = $withdrawModel->promoteWithdrawPerMonthByPromote($promote, false, $data);
break;
case 3:
$result = $withdrawModel->promoteWithdrawRecoupByPromote($promote, $data);
break;
}
M()->commit();//事物提交
$data = [];
$data['status'] = 0;
switch ($result) {
case -4:
case -3:
$data['msg'] = '无可补点订单';
break;
case -2:
if ($settlementType == 3) {
$data['msg'] = '无可补点订单';
} else {
$data['msg'] = '累计结算低于100元无法提现';
}
break;
case -1:
case 0:
$data['msg'] = '操作失败';
break;
case 1:
$data['status'] = 1;
$data['msg'] = '提现成功';
$data['msg'] = '操作成功';
break;
}
$this->ajaxReturn($data);
}
@ -412,6 +451,22 @@ class QueryController extends ThinkController
$this->ajaxReturn($data);
}
$time = time();
$data['status'] = 1;
$data['msg'] = '撤销成功';
$withdrawSave['id'] = $id;
$withdrawSave['status'] = -2;
$withdrawSave['last_up_update_time'] = $time;
$withdrawSave['last_op_id'] = is_login();
$withdrawSave['last_op_type'] = 2;
if ($data['settlement_type'] == 3) {
$withdrawRes = M('withdraw', 'tab_')->save($withdrawSave);
if ($withdrawRes === false) {
$data['status'] = 0;
$data['msg'] = '撤销失败';
$this->ajaxReturn($data);
}
} else {
$spendMap['withdraw_id'] = $id;
$spendIds = M('Spend', 'tab_')->where($spendMap)->getField('id', true);
if (empty($spendIds)) {
@ -420,13 +475,7 @@ class QueryController extends ThinkController
$this->ajaxReturn($data);
}
$spendIds = implode(',', $spendIds);
$time = time();
$withdrawSave['id'] = $id;
$withdrawSave['status'] = -2;
$withdrawSave['spend_ids'] = $spendIds;
$withdrawSave['last_up_update_time'] = $time;
$withdrawSave['last_op_id'] = is_login();
$withdrawSave['last_op_type'] = 2;
M('withdraw', 'tab_')->startTrans();//开启事物
$withdrawRes = M('withdraw', 'tab_')->save($withdrawSave);
@ -446,9 +495,9 @@ class QueryController extends ThinkController
$data['msg'] = '撤销失败';
} else {
M('withdraw', 'tab_')->commit();//事物提交
$data['status'] = 1;
$data['msg'] = '撤销成功';
}
}
$this->ajaxReturn($data);
}
@ -1392,4 +1441,50 @@ class QueryController extends ThinkController
}
}
public function getPromoteSettlementType()
{
$promoteId = intval(I('post.promote_id', 0));
$settlementType = [];
if ($promoteId > 0) {
$promote = D('promote')->field('level, settlement_type')->where(array('id' => $promoteId))->find();
if (empty($promote) || $promote['level'] != 1 || empty($promote['settlement_type'])) {
$settlementType = [];
} else {
$settlementType = WithdrawModel::$settlementTypeList;
unset($settlementType[WithdrawModel::SETTLEMENT_TYPE_OTHER]);
if ($promote['settlement_type'] == 1) {
unset($settlementType[WithdrawModel::SETTLEMENT_TYPE_MONTH]);
} else {
unset($settlementType[WithdrawModel::SETTLEMENT_TYPE_WEEK]);
unset($settlementType[WithdrawModel::SETTLEMENT_TYPE_RECOUP]);
}
}
}
$this->ajaxReturn(['settlement_type' => $settlementType]);
}
public function getRecoupAmount()
{
$withdrawModel = new WithdrawModel();
$promoteId = intval(I('promote_id', 0));
$settlementType = intval(I('settlement_type', 0));
$time = I('time', '');
if (empty($promoteId) || $settlementType != 3) {
$this->ajaxReturn(['balance' => 0]);
}
$promote = M('promote', 'tab_')->find($promoteId);
if (empty($promote) || $promote['level'] != 1 || $promote['ver_status'] != 1 || $promote['settlement_type'] != 1 || empty($time)) {
$this->ajaxReturn(['balance' => 0]);
}
$data['begin_time'] = strtotime($time);
$data['end_time'] = strtotime('+1 month', $data['begin_time']) - 1;
$balance = $withdrawModel->getRecoupAmount($promote, $data);
$this->ajaxReturn(['balance' => $balance]);
}
}

@ -23,9 +23,8 @@ class WithdrawModel extends Model{
//结算类型
const SETTLEMENT_TYPE_WEEK = 1;
const SETTLEMENT_TYPE_MONTH = 2;
const SETTLEMENT_TYPE_ADMIN = 3;
const SETTLEMENT_TYPE_PROMOTE = 4;
const SETTLEMENT_TYPE_OTHER = 5;
const SETTLEMENT_TYPE_RECOUP = 3;
const SETTLEMENT_TYPE_OTHER = 9;
/**
* 构造函数
@ -49,7 +48,7 @@ class WithdrawModel extends Model{
public static $settlementTypeList = [
self::SETTLEMENT_TYPE_WEEK => '周结',
self::SETTLEMENT_TYPE_MONTH => '月结',
self::SETTLEMENT_TYPE_ADMIN => '管理员提现',
self::SETTLEMENT_TYPE_RECOUP => '补点',
self::SETTLEMENT_TYPE_OTHER => '其他',
];
@ -124,11 +123,22 @@ class WithdrawModel extends Model{
}
//生成提现单号
public function produceWithdrawNumber()
//生成提现单号 $type 1-周结 2-月结 3-补点
public function produceWithdrawNumber($type = 1)
{
$prefix = 'WD_';
$number = '';
switch ($type) {
case 1:
$prefix = "{$prefix}W_";
break;
case 2:
$prefix = "{$prefix}M_";
break;
case 3:
$prefix = "{$prefix}R_";
break;
}
while (true) {
$randomNum = rand(10, 99);
$number = $prefix . time() . $randomNum;
@ -140,4 +150,402 @@ class WithdrawModel extends Model{
}
return $number;
}
//周结
public function promoteWithdrawWeeklyByPromote($promote, $initial = false, $data = [])
{
$promoteIds = $this->getPromoteChildren($promote);
$settlementType = $promote['settlement_type'];
$settlementBeginTime = $data['begin_time'] ?? strtotime(date('Y-m-d', strtotime('-7 day', time())));
$settlementEndTime = $data['end_time'] ?? strtotime(date('Y-m-d')) - 1;
if ($initial) {
$settlementType = 9;
$settlementBeginTime = 0;
}
$spendMap['pay_status'] = 1;
$spendMap['selle_status'] = 0;
$spendMap['promote_id'] = ['in', $promoteIds];
$spendMap['pay_time'] = ['between', [$settlementBeginTime, $settlementEndTime]];
$spendModel = M('spend', 'tab_');
$balance = $spendModel->field("sum(if(selle_ratio > 0, pay_amount * selle_ratio, 0)) as balance")
->where($spendMap)
->find()['balance'];
$balance = $balance ?? 0;
$balance = bcdiv($balance, 100, 2);
if ($balance < 100) {
return -2;//余额不足
}
$gameRatios = [];
if ($initial === false) {
$beginTime = date('Y-m-d', $settlementBeginTime);
$endTime = date('Y-m-d', $settlementEndTime);
$gameRatios = $this->getGameRatio($spendMap, $beginTime, $endTime);
}
$gameRatios = $gameRatios ? json_encode($gameRatios) : '';
$thisTime = time();
$add['sum_money'] = $balance;
$add['promote_id'] = $promote['id'];
$add['promote_account'] = $promote['account'];
$add['create_time'] = $thisTime;
$add['status'] = 0;
$add['widthdraw_number'] = D('withdraw')->produceWithdrawNumber(1);
$add['settlement_begin_time'] = $settlementBeginTime;
$add['settlement_end_time'] = $settlementEndTime;
$add['spend_ids'] = '';
$add['op_id'] = $data['op_id'] ?? 0;
$add['op_type'] = $data['op_type'] ?? 3;
$add['last_op_id'] = $add['op_id'];
$add['last_op_type'] = $add['op_type'];
$add['settlement_type'] = $settlementType;
$add['game_ratio'] = $gameRatios;
M()->startTrans();//开启事物
$withdrawId = D('withdraw')->add($add);
if (!$withdrawId) {
M()->rollback();//事物回滚
return 0;//提现失败
}
$save['selle_status'] = 1;
$save['selle_time'] = $thisTime;
$save['withdraw_id'] = $withdrawId;
$res = $spendModel->where($spendMap)->save($save);
if ($res === false) {
D('withdraw')->delete($withdrawId);
M()->rollback();//事物回滚
return -1;
}
M()->commit();//事物提交
return 1;
}
//月结
public function promoteWithdrawPerMonthByPromote($promote, $initial = false, $data = [])
{
$promoteIds = $this->getPromoteChildren($promote);
$settlementType = $promote['settlement_type'];
$settlementBeginTime = $data['begin_time'] ?? strtotime(date('Y-m', strtotime('-1 month', time())));
$settlementEndTime = $data['end_time'] ?? strtotime(date('Y-m')) - 1;
if ($initial) {
$settlementType = 9;
$settlementBeginTime = 0;
}
$beginTime = date('Y-m-d', $settlementBeginTime);
$endTime = date('Y-m-d', $settlementEndTime);
$spendMap['pay_status'] = 1;
$spendMap['selle_status'] = 0;
$spendMap['promote_id'] = ['in', $promoteIds];
$spendMap['pay_time'] = ['between', [$settlementBeginTime, $settlementEndTime]];
$spendWhere = $spendMap;
$spendModel = M('spend', 'tab_');
$gameRatios = [];
$promoteGameRatioMap['status'] = 1;
$promoteGameRatioMap['promote_id'] = $promote['id'];
$promoteGameRatios = D('promote_game_ratio')->field('game_id, ratio, turnover_ratio, begin_time, end_time')
->where($promoteGameRatioMap)
->order('update_time desc')
->select();
if ($initial === false) {
$beginTime = date('Y-m-d', $settlementBeginTime);
$endTime = date('Y-m-d', $settlementEndTime);
$gameRatios = $this->getGameRatio($spendMap, $beginTime, $endTime);
}
if (empty($promoteGameRatios) || $initial) {
$balance = $spendModel->field("sum(if(selle_ratio > 0, pay_amount * selle_ratio, 0)) as balance")
->where($spendMap)
->find()['balance'];
$balance = $balance ?? 0;
} else {
$balance = 0;
$notInGameIds = [-1];
foreach ($promoteGameRatios as $promoteGameRatio) {
$spendWhere['game_id'] = $promoteGameRatio['game_id'];
if (!empty($promoteGameRatio['turnover_ratio']) && $promoteGameRatio['begin_time'] <= $settlementBeginTime && (empty($promoteGameRatio['end_time']) || $promoteGameRatio['end_time'] >= $settlementEndTime)) {
$notInGameIds[] = $promoteGameRatio['game_id'];
$ratio = $promoteGameRatio['ratio'];
$promoteGameRatio['turnover_ratio'] = json_decode($promoteGameRatio['turnover_ratio'], true);
$turnoverRatios = array_reverse($promoteGameRatio['turnover_ratio']);
$sumAmount = $spendModel->field("sum(pay_amount) as sum_amount")
->where($spendWhere)
->find()['sum_amount'];
$sumAmount = $sumAmount ?? 0;
foreach ($turnoverRatios as $turnoverRatio) {
if ($sumAmount >= $turnoverRatio['turnover']) {
$ratio = $turnoverRatio['ratio'];
break;
}
}
$thisBalance = $spendModel->field("sum(pay_amount * {$ratio}) as balance")
->where($spendWhere)
->find()['balance'];
$thisBalance = $thisBalance ?? 0;
$balance = bcadd($balance, $thisBalance, 2);
$gameRatios[$promoteGameRatio['game_id']] = [];
$gameRatios[$promoteGameRatio['game_id']][] = [
'selle_ratio' => $ratio,
'sum_amount' => $sumAmount,
'begin_time' => $beginTime,
'end_time' => $endTime,
];
}
}
$spendWhere['game_id'] = ['not in', $notInGameIds];
$otherBalance = $spendModel->field("sum(if(selle_ratio > 0, pay_amount * selle_ratio, 0)) as balance")
->where($spendWhere)
->find()['balance'];
$otherBalance = $otherBalance ?? 0;
$balance = bcadd($balance, $otherBalance, 2);
}
$balance = bcdiv($balance, 100, 2);
if ($balance < 100) {
return -2;//余额不足
}
$gameRatios = $gameRatios ? json_encode($gameRatios) : '';
$thisTime = time();
$add['sum_money'] = $balance;
$add['promote_id'] = $promote['id'];
$add['promote_account'] = $promote['account'];
$add['create_time'] = $thisTime;
$add['status'] = 0;
$add['widthdraw_number'] = D('withdraw')->produceWithdrawNumber(2);
$add['settlement_begin_time'] = $settlementBeginTime;
$add['settlement_end_time'] = $settlementEndTime;
$add['spend_ids'] = '';
$add['op_id'] = $data['op_id'] ?? 0;
$add['op_type'] = $data['op_type'] ?? 3;
$add['last_op_id'] = $add['op_id'];
$add['last_op_type'] = $add['op_type'];
$add['settlement_type'] = $settlementType;
$add['game_ratio'] = $gameRatios;
M()->startTrans();//开启事物
$withdrawId = D('withdraw')->add($add);
if (!$withdrawId) {
M()->rollback();//事物回滚
return 0;//提现失败
}
$save['selle_status'] = 1;
$save['selle_time'] = $thisTime;
$save['withdraw_id'] = $withdrawId;
$res = $spendModel->where($spendMap)->save($save);
if ($res === false) {
D('withdraw')->delete($withdrawId);
M()->rollback();//事物回滚
return -1;
}
M()->commit();//事物提交
return 1;
}
//补点
public function promoteWithdrawRecoupByPromote($promote, $data = [])
{
$promoteGameRatioMap['status'] = 1;
$promoteGameRatioMap['promote_id'] = $promote['id'];
$promoteGameRatios = D('promote_game_ratio')->field('game_id, ratio, turnover_ratio, begin_time, end_time')
->where($promoteGameRatioMap)
->order('update_time desc')
->select();
if (empty($promoteGameRatios)) {
return -3;
}
$promoteIds = $this->getPromoteChildren($promote);
$settlementType = 3;
$settlementBeginTime = $data['begin_time'] ?? strtotime(date('Y-m', strtotime('-1 month', time())));
$settlementEndTime = $data['end_time'] ?? strtotime(date('Y-m')) - 1;
$withdrawMap['promote_id'] = $promote['id'];
$withdrawMap['status'] = ['neq', -2];
$withdrawMap['settlement_begin_time'] = $settlementBeginTime;
$withdrawMap['settlement_end_time'] = $settlementEndTime;
$withdraw = D('withdraw')->where($withdrawMap)->find();
if (!empty($withdraw)) {
return -4;
}
$spendMap['pay_status'] = 1;
$spendMap['promote_id'] = ['in', $promoteIds];
$spendMap['pay_time'] = ['between', [$settlementBeginTime, $settlementEndTime]];
$spendModel = M('spend', 'tab_');
$balance = 0;
$gameRatios = [];
foreach ($promoteGameRatios as $promoteGameRatio) {
if (!empty($promoteGameRatio['turnover_ratio']) && $promoteGameRatio['begin_time'] <= $settlementBeginTime && (empty($promoteGameRatio['end_time']) || $promoteGameRatio['end_time'] >= $settlementEndTime)) {
$spendMap['game_id'] = $promoteGameRatio['game_id'];
$promoteGameRatio['turnover_ratio'] = json_decode($promoteGameRatio['turnover_ratio'], true);
$turnoverRatios = array_reverse($promoteGameRatio['turnover_ratio']);
$sumAmount = $spendModel->field("sum(pay_amount) as sum_amount")
->where($spendMap)
->find()['sum_amount'];
$sumAmount = $sumAmount ?? 0;
$ratio = 0;
foreach ($turnoverRatios as $turnoverRatio) {
if ($sumAmount >= $turnoverRatio['turnover']) {
$ratio = $turnoverRatio['ratio'];
break;
}
}
if ($ratio > 0) {
$ratio = bcsub($ratio, $promoteGameRatio['ratio'], 2);
$thisBalance = bcdiv(bcmul($sumAmount, $ratio, 2), 100, 2);
$balance = bcadd($balance, $thisBalance, 2);
$gameRatios[$promoteGameRatio['game_id']][] = [
'selle_ratio' => $ratio,
'sum_amount' => $sumAmount,
'begin_time' => date('Y-m-d', $settlementBeginTime),
'end_time' => date('Y-m-d', $settlementEndTime),
];
}
}
}
if ($balance == 0) {
return -2;//无余额
}
$gameRatios = $gameRatios ? json_encode($gameRatios) : '';
$thisTime = time();
$add['sum_money'] = $balance;
$add['promote_id'] = $promote['id'];
$add['promote_account'] = $promote['account'];
$add['create_time'] = $thisTime;
$add['status'] = 0;
$add['widthdraw_number'] = D('withdraw')->produceWithdrawNumber(3);
$add['settlement_begin_time'] = $settlementBeginTime;
$add['settlement_end_time'] = $settlementEndTime;
$add['spend_ids'] = '';
$add['op_id'] = $data['op_id'] ?? 0;
$add['op_type'] = $data['op_type'] ?? 3;
$add['last_op_id'] = $add['op_id'];
$add['last_op_type'] = $add['op_type'];
$add['settlement_type'] = $settlementType;
$add['game_ratio'] = $gameRatios;
$withdrawId = D('withdraw')->add($add);
if (!$withdrawId) {
return 0;//补点失败
}
return 1;
}
public function getRecoupAmount($promote, $data = [])
{
$promoteGameRatioMap['status'] = 1;
$promoteGameRatioMap['promote_id'] = $promote['id'];
$promoteGameRatios = D('promote_game_ratio')->field('game_id, ratio, turnover_ratio, begin_time, end_time')
->where($promoteGameRatioMap)
->order('update_time desc')
->select();
if (empty($promoteGameRatios)) {
return 0;
}
$promoteIds = $this->getPromoteChildren($promote);
$settlementBeginTime = $data['begin_time'] ?? strtotime(date('Y-m', strtotime('-1 month', time())));
$settlementEndTime = $data['end_time'] ?? strtotime(date('Y-m')) - 1;
$spendMap['pay_status'] = 1;
$spendMap['promote_id'] = ['in', $promoteIds];
$spendMap['pay_time'] = ['between', [$settlementBeginTime, $settlementEndTime]];
$spendModel = M('spend', 'tab_');
$balance = 0;
foreach ($promoteGameRatios as $promoteGameRatio) {
if (!empty($promoteGameRatio['turnover_ratio']) && $promoteGameRatio['begin_time'] <= $settlementBeginTime && (empty($promoteGameRatio['end_time']) || $promoteGameRatio['end_time'] >= $settlementEndTime)) {
$spendMap['game_id'] = $promoteGameRatio['game_id'];
$promoteGameRatio['turnover_ratio'] = json_decode($promoteGameRatio['turnover_ratio'], true);
$turnoverRatios = array_reverse($promoteGameRatio['turnover_ratio']);
$sumAmount = $spendModel->field("sum(pay_amount) as sum_amount")
->where($spendMap)
->find()['sum_amount'];
$sumAmount = $sumAmount ?? 0;
$ratio = 0;
foreach ($turnoverRatios as $turnoverRatio) {
if ($sumAmount >= $turnoverRatio['turnover']) {
$ratio = $turnoverRatio['ratio'];
break;
}
}
if ($ratio > 0) {
$ratio = bcsub($ratio, $promoteGameRatio['ratio'], 2);
$thisBalance = bcdiv(bcmul($sumAmount, $ratio, 2), 100, 2);
$balance = bcadd($balance, $thisBalance, 2);
}
}
}
return $balance;
}
private function getSpendMessageByGame($map)
{
$field = 'game_id, selle_ratio, sum(pay_amount) as sum_amount, pay_time';
$spendModel = M('spend', 'tab_');
$gameSpendRatios = $spendModel->field($field)
->where($map)
->group('game_id, selle_ratio')
->order('game_id asc, pay_time asc')
->select();
return $gameSpendRatios;
}
private function getPromoteChildren($promote) {
$promoteMap['chain'] = ['like', "{$promote['chain']}{$promote['id']}/%"];
$promoteIds = M('promote', 'tab_')->where($promoteMap)->getField('id', true);
$promoteIds[] = $promote['id'];
return $promoteIds;
}
private function getGameRatio($map, $beginTime, $endTime)
{
$gameRatios = [];
$gameSpendRatios = $this->getSpendMessageByGame($map);
foreach ($gameSpendRatios as $gameSpendRatio) {
$gameRatios[$gameSpendRatio['game_id']][] = [
'selle_ratio' => $gameSpendRatio['selle_ratio'],
'sum_amount' => $gameSpendRatio['sum_amount'],
'begin_time' => date('Y-m-d', $gameSpendRatio['pay_time']),
'end_time' => date('Y-m-d', $gameSpendRatio['pay_time']),
];
}
foreach ($gameRatios as $key => $gameRatio) {
$gameRatios[$key][0]['begin_time'] = $beginTime;
$gameRatios[$key][count($gameRatio) - 1]['end_time'] = $endTime;
if (count($gameRatio) > 1) {
foreach ($gameRatios[$key] as $childKey => $childList) {
$gameRatios[$key][$childKey]['end_time'] = isset($gameRatios[$key][$childKey + 1]) ? $gameRatios[$key][$childKey + 1]['begin_time'] : $gameRatios[$key][$childKey]['end_time'];
}
}
}
return $gameRatios;
}
}

@ -55,14 +55,25 @@
<div class="input-list search-title-box">
<label>搜索:</label>
</div>
<div class="input-list input-list-promote search_label_rehab">
<select id="promote_id" name="promote_id" class="select_gallery" style="width:120px;">
<option value="">会长账号</option>
<volist name=":get_promote_list_by_id()" id="vo">
<option promote-id="{$vo.id}" value="{$vo.id}">{$vo.account}</option>
</volist>
</select>
</div>
<if condition="I('group') eq 1 or $group eq 1">
<div class="input-list">
<input type="text" id="time-start" name="timestart" autocomplete="off" class="" value="{:I('timestart', get_lastweek_name(7))}" placeholder="充值起始时间" />
-
<div class="input-append date" id="datetimepicker" style="display:inline-block">
<input type="text" id="time-end" name="timeend" autocomplete="off" class="" value="{:I('timeend', get_lastweek_name(1))}" placeholder="充值结束时间" />
<span class="add-on"><i class="icon-th"></i></span>
<div class="input-list input-list-promote search_label_rehab">
<select id="settlement_type" name="settlement_type" class="select_gallery" style="width:120px;">
<option value="">提现模式</option>
<?php foreach ($settlementTypeList as $key => $vo) :?>
<option value="{$key}">{$vo}</option>
<?php endforeach ;?>
</select>
</div>
<div class="input-list" id="pay_time">
<input type="text" class="txt range-date" id="time" name="time" placeholder="充值时间" value="{:I('time')}" style="width: 180px;" readonly="readonly">
</div>
</if>
<if condition="I('group') eq 2 or $group eq 2">
@ -70,14 +81,6 @@
<input type="text" id="create_time" name="create_time" class="" value="{:I('create_time', '')}" placeholder="提现时间" />
</div>
</if>
<div class="input-list input-list-promote search_label_rehab">
<select id="promote_id" name="promote_id" class="select_gallery" style="width:120px;">
<option value="">会长账号</option>
<volist name=":get_promote_list_by_id()" id="vo">
<option promote-id="{$vo.id}" value="{$vo.id}">{$vo.account}</option>
</volist>
</select>
</div>
<input type="hidden" id="sort_by" name="sort_by" value="{:I('sort_by', '')}" class="sortBy">
<input type="hidden" id="sort_column" name="sort_column" value="{:I('sort_column', '')}" class="sortBy">
@ -109,9 +112,9 @@
<div class="input-list">
<select name="settlement_type" class="select_gallery">
<option value="">提现模式</option>
<?php foreach (getPromoteWithdrawSettlementType() as $key => $value) :?>
<option value="<?=$key?>"><?=$value?></option>
<?php endforeach;?>
<?php foreach ($settlementTypeList as $key => $vo) :?>
<option value="{$key}">{$vo}</option>
<?php endforeach ;?>
</select>
</div>
<div class="input-list">
@ -165,7 +168,13 @@
</volist>
<tr class="data_summary">
<td>汇总</td>
<td colspan="10">累计结算:{$zTotal}元</td>
<td colspan="10">
<if condition="3 eq I('settlement_type', 0)">
累计充值:{$zTotal}元
<else/>
累计结算:{$zTotal}元
</if>
</td>
</tr>
</empty>
</tbody>
@ -255,6 +264,9 @@
<script type="text/javascript" src="__STATIC__/datetimepicker/js/bootstrap-datetimepicker.min.js"></script>
<script type="text/javascript" src="__STATIC__/datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js" charset="UTF-8"></script>
<script src="__STATIC__/layer/layer.js" type="text/javascript"></script>
<link rel="stylesheet" href="__STATIC__/flatpickr/flatpickr.min.css">
<script src="__STATIC__/flatpickr/flatpickr.min.js"></script>
<script src="__STATIC__/flatpickr/l10n/zh.js"></script>
<script>
<volist name = ":I('get.')" id = "vo">
Think.setValue('{$key}', "{$vo}");
@ -273,11 +285,21 @@
var url = $(this).attr('url');
var query = $('.top_nav_list').find('input').serialize();
var promoteId = $('#promote_id').val();
var settlementType = $('#settlement_type').val();
var time = $('#time').val();
var group = parseInt("{$group}");
if (promoteId == '' && group === 1) {
layer.msg('请选择推广员', {time: 1000});
return false;
}
if (settlementType == '') {
layer.msg('请选择提现模式', {time: 1000});
return false;
}
if (time == '') {
layer.msg('请选择充值时间', {time: 1000});
return false;
}
query += "&"+$('.top_nav_list').find('select').serialize();
query = query.replace(/(&|^)(\w*?\d*?\-*?_*?)*?=?((?=&)|(?=$))/g,'');
@ -319,7 +341,7 @@
}
});
var date="{$setdate}";
$('#time-start').datetimepicker({
$('#create_time').datetimepicker({
format: 'yyyy-mm-dd',
language:"zh-CN",
minView:2,
@ -330,28 +352,66 @@
endDate:date
});
$('#datetimepicker').datetimepicker({
format: 'yyyy-mm-dd',
language:"zh-CN",
minView:2,
autoclose:true,
pickerPosition:'bottom-left',
scrollMonth:false,
scrollTime:false,
scrollInput:false,
endDate:date
})
$('#create_time').datetimepicker({
format: 'yyyy-mm-dd',
language:"zh-CN",
minView:2,
autoclose:true,
scrollMonth:false,
scrollTime:false,
scrollInput:false,
endDate:date
switch (parseInt("{:(I('settlement_type', 0))}")) {
case 1:
setTimeByWeek();
break;
case 2:
case 3:
setTimeByMonth();
break;
default:
break;
}
});
function setTimeByWeek() {
var defaultDate = $('#time').val()
defaultDate = defaultDate == '' ? [] : defaultDate.split(' 至 ')
$('#time').flatpickr({
mode: 'range',
locale: 'zh',
dateFormat: "Y-m-d",
defaultDate: defaultDate,
onChange: function (selectedDates, dateStr, instance) {
if (dateStr.indexOf('至') === -1) {
var thisDate = new Date(Date.parse(dateStr));
if (thisDate.getDay() !== 1) {
layer.msg('请选择周一到周日');
return false;
}
} else {
var thisDate = dateStr.split(' 至 ');
var startDate = new Date(Date.parse(thisDate[0]));
if (startDate.getDay() !== 1 || Date.parse(thisDate[1]) / 1000 - Date.parse(thisDate[0]) / 1000 !== 518400) {
layer.msg('请选择周一到周日');
$('#time').val('');
return false;
}
}
}
});
}
function setTimeByMonth() {
$('#time').datetimepicker({
format: 'yyyy-mm',
language: "zh-CN",
autoclose: true,
scrollMonth: false,
scrollTime: false,
scrollInput: false,
startView: 'year',
minView:'year',
maxView:'year',
});
})
}
function resetTime() {
$('#time').remove();
var html = '<input type="text" class="txt range-date" id="time" name="time" placeholder="充值时间" value="" style="width: 180px;" readonly="readonly">';
$('#pay_time').html(html);
}
function detailsInfo(promote_id,create_time){
layer.open({
@ -365,25 +425,28 @@
}
$('#withdraw').click(function (e) {
var beginTime = $('#time-start').val();
var endTime = $('#time-end').val();
var promoteId = parseInt($('#promote_id').val());
var balance = parseFloat("{$zTotal}");
var msg = getWithdraw(promoteId, beginTime, endTime);
msg += "收益结算时间:" + $('#time-start').val() + " 至 " + $('#time-end').val();
var settlementType = $('#settlement_type').val();
var time = $('#time').val();
var msg = '';
msg += "收益结算时间:" + time;
msg += "<br>";
if (settlementType == 3) {
msg += "补点金额:" + getRecoupAmount(promoteId, settlementType, time);
} else {
msg += "提现金额:{$zTotal}元";
}
if (Date.parse(beginTime) / 1000 > Date.parse(endTime) / 1000) {
layer.msg('开始时间必须小于等于结束时间');
if (promoteId <= 0) {
layer.msg('请选择会长账号', {time: 1000});
return false;
}
if (promoteId <= 0) {
layer.msg('请选择会长账号');
if (settlementType == '') {
layer.msg('请选择提现模式', {time: 1000});
return false;
}
if (balance < 100) {
layer.msg('累计结算低于100元无法提现', {icon: 5});
if (time == '') {
layer.msg('请选择充值时间', {time: 1000});
return false;
}
@ -397,7 +460,7 @@
type: 'post',
url: '{:U("settlementWithdraw")}',
dataType: 'json',
data: {'begin_time': beginTime, 'end_time': endTime, 'promote_id': promoteId},
data: {'promote_id': promoteId, 'settlement_type': settlementType, 'time': time},
success: function (data) {
layer.closeAll('loading');
if (data.status == 1) {
@ -416,25 +479,6 @@
});
});
function getWithdraw(promoteId, beginTime, endTime)
{
var msg = '';
$.ajax({
type: 'post',
url: '{:U("getWithdraw")}',
dataType: 'json',
async: false,
data: {promote_id:promoteId, begin_time:beginTime, end_time:endTime},
success: function (data) {
msg = data.msg;
},
error: function (result) {
layer.msg('网络异常', {icon: 5});
}
});
return msg;
}
$('.renew-review').click(function () {
var id = parseInt($(this).attr('data-id'));
@ -494,6 +538,61 @@
$(".export-btn").on("click",function(e){
e.preventDefault();
window.location.href=$(this).attr("href")
})
});
$('#promote_id').change(function () {
var promoteId = parseInt($(this).val());
var selectTag = $('#settlement_type');
$.ajax({
type: 'post',
url: '{:U("getPromoteSettlementType")}',
dataType: 'json',
data: {promote_id:promoteId},
success: function(response) {
var options = '<option value="0">提现模式</option>'
for (var i in response.settlement_type) {
var settlementType = response.settlement_type[i]
options += '<option value="' + i + '">' + settlementType + '</option>'
}
selectTag.html(options)
}
});
});
$('#settlement_type').change(function () {
var settlementType = parseInt($(this).val());
switch (settlementType) {
case 1:
resetTime();
setTimeByWeek();
break;
case 2:
case 3:
resetTime();
setTimeByMonth();
break;
default:
break;
}
});
function getRecoupAmount(promoteId, settlementType, time) {
var balance = 0;
$.ajax({
type: 'post',
url: '{:U("getRecoupAmount")}',
dataType: 'json',
async: false,
data: {'promote_id': promoteId, 'settlement_type': settlementType, 'time': time},
success: function (data) {
balance = data.balance;
},
error: function (result) {
layer.msg('网络异常', {icon: 5});
}
});
return balance;
}
</script>
</block>

@ -959,3 +959,6 @@ ADD COLUMN `last_turnover_ratio` varchar(2000) NOT NULL COMMENT '上次流水分
-- 2020 01-07 cxj
ALTER TABLE `tab_withdraw`
ADD COLUMN `game_ratio` varchar(5000) NOT NULL COMMENT '游戏提现实际分成比例';
ALTER TABLE `tab_withdraw`
MODIFY COLUMN `settlement_type` tinyint(3) NOT NULL DEFAULT 1 COMMENT '结算类型1-周结 2-月结 9-其他';

Loading…
Cancel
Save