You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1214 lines
49 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace Admin\Controller;
use Home\Controller\FileController;
use User\Api\UserApi as UserApi;
use Admin\Model\WithdrawModel;
/**
* 推广查询控制器
* @author 王贺
*/
class QueryController extends ThinkController
{
//生成提现单号
public function produceWithdrawNumber()
{
$prefix = 'WD_';
$number = '';
while (true) {
$randomNum = rand(10, 99);
$number = $prefix . time() . $randomNum;
$map['widthdraw_number'] = $number;
$res = M('withdraw', 'tab_')->where($map)->getField('id');
if (!$res) {
break;
}
}
return $number;
}
public function settlement($p = 0)
{
$page = intval(I('p', 1)); //默认显示第一页数据
$row = intval(I('row', 10));
$group = I('group', 1);
$this->assign('group', $group);
$promoteId = I('promote_id', 0);
if ($group == 1) {
if (empty($promoteId)) {
$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));
$sortBy = I('sort_by', '');
$sortColumn = I('sort_column', '');
$order = 'tab_spend.pay_time';
$orderType = 'desc';
if (in_array($sortColumn, ['user_num', 'sum_amount'])) {
$order = $sortColumn;
}
if ($sortBy == 1) {
$orderType = 'desc';
} 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;
$spendMap['tab_spend.pay_time'] = ['between', [$beginTime, $endTime]];
$spendMap['tab_spend.promote_id'] = ['in', $promoteIds];
$spendField = 'tab_spend.promote_account, tab_spend.game_name, sum(tab_spend.pay_amount) as sum_amount, tab_spend.selle_ratio, sum(if(tab_spend.selle_ratio > 0, tab_spend.pay_amount * tab_spend.selle_ratio, 0)) as settlement_amount';
$userMap['tab_user.promote_id'] = ['in', $promoteIds];
$userMap['_string'] = 'tab_user.promote_id = tab_spend.promote_id';
$userField = M('user', 'tab_')->field('count(tab_user.id) as user_num')
->where($userMap)
->buildSql();
$field = "{$spendField}, {$userField} as user_num";
$query = M('spend', 'tab_')->field($field)
->where($spendMap)
->group('tab_spend.selle_ratio, tab_spend.game_id, tab_spend.promote_id')
->order("{$order} {$orderType}")
->page($page, $row)
->buildSql();
$records = M()->table($query)
->alias('record')
->select();
$countQuery = M('spend', 'tab_')->field('tab_spend.id')
->where($spendMap)
->group('tab_spend.selle_ratio, tab_spend.game_id, tab_spend.promote_id')
->buildSql();
$count = M()->table($countQuery)
->alias('tab_spend')
->count();
$page = set_pagination($count, $row);
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);
}
}
$totalField = 'sum(if(tab_spend.selle_ratio > 0, tab_spend.pay_amount * tab_spend.selle_ratio, 0)) as settlement_amount';
$total = array_sum(array_column($records, 'settlement_amount'));//当页
$zTotal = null_to_0(M('spend', 'tab_')->field($totalField)->where($spendMap)->find()['settlement_amount']);//累计
$zTotal = bcdiv($zTotal, 100 ,2);
unset($spendMap['tab_spend.pay_time']);
$spendMap['_string'] = 'tab_spend.pay_time' . total(1);
$tTotal = null_to_0(M('spend', 'tab_')->field($totalField)->where($spendMap)->find()['settlement_amount']);//今日
$tTotal = bcdiv($tTotal, 100 ,2);
$spendMap['_string'] = 'tab_spend.pay_time' . total(5);
$yTotal = null_to_0(M('spend', 'tab_')->field($totalField)->where($spendMap)->find()['settlement_amount']);//昨日
$yTotal = bcdiv($yTotal, 100 ,2);
if($page) {
$this->assign('_page', $page);
}
$this->assign('records', $records);
$this->assign('total', $total);
$this->assign('tTotal', $tTotal);
$this->assign('yTotal', $yTotal);
$this->assign('zTotal', $zTotal);
$this->assign('commonset', M('Kuaijieicon')->where(['url' => 'Query/settlement', 'status' => 1])->find());
$this->m_title = '推广结算';
$this->display();
}
} elseif ($group == 2) {
$map['_string'] = '1 = 1';
$createTime = strtotime(I('create_time', ''));
$withdrawNumber = I('widthdraw_number', '');
$status = intval(I('status', ''));
$opType = intval(I('op_type', 0));
if ($createTime) {
$createTimeEnd = $createTime + 3600 * 24 - 1;
$map['create_time'] = ['between', [$createTime, $createTimeEnd]];
}
if ($promoteId) {
$map['promote_id'] = $promoteId;
}
if ($withdrawNumber) {
$map['widthdraw_number'] = ['like', "{$withdrawNumber}%"];
}
if (isset($_REQUEST['status'])) {
$map['status'] = $status;
}
if ($opType) {
$map['op_type'] = $opType;
}
$records = M('withdraw', 'tab_')->field('id, promote_account, widthdraw_number, sum_money, op_id, op_type, last_op_id, last_op_type, settlement_begin_time, settlement_end_time, create_time, status, respond')
->where($map)
->order('create_time desc')
->page($page, $row)
->select();
$count = M('withdraw', 'tab_')->field('id, promote_account, widthdraw_number, sum_money, op_id, op_type, last_op_id, last_op_type, settlement_begin_time, settlement_end_time, create_time, status, respond')
->where($map)
->count();
if (!empty($records)) {
foreach ($records as &$record) {
$record['settlement_begin_time'] = $record['settlement_begin_time'] ? date('Y-m-d H:i:s', $record['settlement_begin_time']) : '--';
$record['settlement_end_time'] = date('Y-m-d H:i:s', $record['settlement_end_time']);
$record['create_time'] = date('Y-m-d H:i:s', $record['create_time']);
$record['status_text'] = promoteWithdrawStatus($record['status']);
if ($record['status'] == -2) {
$record['status_text'] = '<span style="color: #999;">' . $record['status_text'] . '</span>';
}
}
}
$page = set_pagination($count, $row);
if($page) {
$this->assign('_page', $page);
}
$this->assign('records', $records);
$this->display();
}
}
public function settlementWithdraw()
{
$beginTime = strtotime(I('begin_time', ''));
$endTime = strtotime(I('end_time', ''));
$promoteId = intval(I('promote_id', 0));
if ($beginTime == 0 || $endTime == 0) {
$data['status'] = 0;
$data['msg'] = '时间参数错误';
$this->ajaxReturn($data);
}
if ($beginTime > $endTime) {
$data['status'] = 0;
$data['msg'] = '开始时间必须小于等于结束时间';
$this->ajaxReturn($data);
}
if ($promoteId == 0) {
$data['status'] = 0;
$data['msg'] = '请选择会长账号';
$this->ajaxReturn($data);
}
$promote = M('promote', 'tab_')->find($promoteId);
if (empty($promote)) {
$data['status'] = 0;
$data['msg'] = '数据异常';
$this->ajaxReturn($data);
}
if ($promote['level'] != 1) {
$data['status'] = 0;
$data['msg'] = '非会长账号,无法执行此操作';
$this->ajaxReturn($data);
}
if ($promote['ver_status'] != 1) {
$data['status'] = 0;
$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) {
$data['status'] = 0;
$data['msg'] = '累计结算低于100元无法提现';
$this->ajaxReturn($data);
}
$spendIds = $spendModel->where($map)->getField('id', true);
if (empty($spendIds)) {
$data['status'] = 0;
$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;
M()->startTrans();//开启事物
$withdrawId = M('withdraw', 'tab_')->add($add);
if (!$withdrawId) {
M()->rollback();//事物回滚
$data['status'] = 0;
$data['msg'] = '提现失败';
$this->ajaxReturn($data);
}
$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);
}
M()->commit();//事物提交
$data['status'] = 1;
$data['msg'] = '提现成功';
$this->ajaxReturn($data);
}
public function renewReview()
{
$id = intval(I('id', 0));
$data = M('withdraw', 'tab_')->find($id);
if (empty($data) || $data['status'] != -1) {
$data['status'] = 0;
$data['msg'] = '数据异常';
$this->ajaxReturn($data);
}
$save['id'] = $id;
$save['status'] = 0;
$save['last_up_update_time'] = time();
$save['last_op_id'] = is_login();
$save['last_op_type'] = 2;
$res = M('withdraw', 'tab_')->save($save);
if ($res === false) {
$data['status'] = 0;
$data['msg'] = '提交失败';
} else {
$data['status'] = 1;
$data['msg'] = '提交成功';
}
$this->ajaxReturn($data);
}
public function cancelWithdraw()
{
$id = intval(I('id', 0));
$data = M('withdraw', 'tab_')->find($id);
if (empty($data) || !in_array($data['status'], [-1, 0])) {
$data['status'] = 0;
$data['msg'] = '数据异常';
$this->ajaxReturn($data);
}
$spendMap['withdraw_id'] = $id;
$spendIds = M('Spend', 'tab_')->where($spendMap)->getField('id', true);
if (empty($spendIds)) {
$data['status'] = 0;
$data['msg'] = '数据异常';
$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);
if ($withdrawRes === false) {
M('withdraw', 'tab_')->rollback();//事物回滚
$data['status'] = 0;
$data['msg'] = '撤销失败';
$this->ajaxReturn($data);
}
$spendSave['selle_status'] = 0;
$spendSave['withdraw_id'] = 0;
$spendRes = M('spend', 'tab_')->where($spendMap)->save($spendSave);
if ($spendRes === false) {
M('withdraw', 'tab_')->rollback();//事物回滚
$data['status'] = 0;
$data['msg'] = '撤销失败';
} else {
M('withdraw', 'tab_')->commit();//事物提交
$data['status'] = 1;
$data['msg'] = '撤销成功';
}
$this->ajaxReturn($data);
}
public function downloadProve()
{
$id = intval(I('id', 0));
if ($id) {
$withdraw = M('withdraw', 'tab_')->find($id);
if (empty($withdraw) || $withdraw['status'] != 2 || !$withdraw['transfer_proof']) {
$this->error('网络异常');
}
$file = new FileController();
$file->download($withdraw['transfer_proof']);
} else {
$this->error('参数异常');
}
}
public function cpsettlement($p = 0)
{
$page = intval($p);
$page = $page ? $page : 1; //默认显示第一页数据
if (isset($_REQUEST['row'])) {
$row = $_REQUEST['row'];
} else {
$row = 10;
}
$this->assign('setdate', date("Y-m-d", strtotime("-1 day")));
if ($_REQUEST['sum_money'] == 2) {
$order = 'total desc';
} else if ($_REQUEST['sum_money'] == 1) {
$order = 'total asc';
}
$group = I('group', 1);
$this->assign('group', $group);
$this->m_title = '开发者结算';
$this->assign('commonset', M('Kuaijieicon')->where(['url' => 'Query/cpsettlement', 'status' => 1])->find());
if (isset($_REQUEST['timestart']) && $_REQUEST['timestart'] != '' && $_REQUEST['group'] == 1) {
$starttime = strtotime($_REQUEST['timestart']);
$endtime = strtotime($_REQUEST['timeend']) + 24 * 3600 - 1;
if (isset($_REQUEST['developers_id'])) {
$map['g.developers'] = $_REQUEST['developers_id'];
} else {
$this->error('请选择开发者');
}
if (isset($_REQUEST['selle_status'])) {
if ($_REQUEST['selle_status'] == "未结算") {
$map['s.selle_status'] = 0;
} else if ($_REQUEST['selle_status'] == "已结算") {
$map['s.selle_status'] = 1;
}
} else {
$map['s.selle_status'] = 0;
}
//判断是否结算绑币
if ($_REQUEST['bind_coin'] == 0) {
$map['s.pay_way'] = array('neq', -1);
}
$map['s.pay_status'] = 1;
$map['pay_time'] = array('BETWEEN', array($starttime, $endtime));
$model = array(
'm_name' => 'Spend as s',
'order' => $order,
'title' => '渠道结算',
'group' => 'g.developers,g.id',
'fields' => 'sum(s.pay_amount) as total,s.selle_ratio,s.id,g.developers,s.selle_status,g.id as gid,g.game_name,s.pay_status,s.pay_amount',
'template_list' => 'cpsettlement',
);
$user = A('Spend', 'Event');
$this->meta_title = '开发者结算';
$user->cpsettl_list($model, $p, $map);
} else if ($_REQUEST['group'] == 2) {
if (isset($_REQUEST['timestart']) && $_REQUEST['timeend'] != '') {
$starttime = strtotime($_REQUEST['timestart']);
$endtime = strtotime($_REQUEST['timeend']) + 24 * 3600 - 1;
$map['starttime'] = array('egt', $starttime);
$map['endtime'] = array('elt', $endtime);
} elseif (isset($_REQUEST['timestart'])) {
$starttime = strtotime($_REQUEST['timestart']);
$map['starttime'] = array('egt', $starttime);
} elseif (isset($_REQUEST['timeend'])) {
$endtime = strtotime($_REQUEST['timeend']) + 24 * 3600 - 1;
$map['endtime'] = array('elt', $endtime);
}
if (isset($_REQUEST['start']) && $_REQUEST['end'] != '') {
$starttime = strtotime($_REQUEST['start']);
$endtime = strtotime($_REQUEST['end']) + 24 * 3600 - 1;
$map['create_time'] = array('BETWEEN', array($starttime, $endtime));
} elseif (isset($_REQUEST['start'])) {
$starttime = strtotime($_REQUEST['start']);
$map['create_time'] = array('BETWEEN', array($starttime, time()));
} elseif (isset($_REQUEST['end'])) {
$endtime = strtotime($_REQUEST['end']) + 24 * 3600 - 1;
$map['create_time'] = array('LT', $endtime);
}
if (isset($_REQUEST['developers_id'])) {
$map['developers'] = $_REQUEST['developers_id'];
} else {
$map['developers'] = array('neq', 0);
}
$data = M('TotalSettlement', 'tab_')->where($map)->order('create_time desc')->page($page, $row)->select();
$count = M('TotalSettlement', 'tab_')->where($map)->order('create_time desc')->count();
$page = set_pagination($count, $row);
if ($page) {
$this->assign('_page', $page);
}
$this->assign('data', $data);
$this->assign('meta_title', '开发者结算记录');
$this->display();
} else {
$this->meta_title = '开发者结算列表';
$this->display();
}
}
public function generatesettlementAll()
{
$request = I('request.ids');
if (empty($request)) {
$this->error('请选择要操作的数据');
}
if (is_array($request)) {
foreach ($request as $k => $v) {
$query = explode(',', $v);
$ids[] = $k;
$_REQUEST[$k]['cooperation'] = $query[0];
$_REQUEST[$k]['cps_ratio'] = $query[1];
$_REQUEST[$k]['cpa_price'] = $query[2];
$_REQUEST[$k]['unum'] = $query[3];
$_REQUEST[$k]['spay_amount'] = $query[4];
$_REQUEST[$k]['game_id'] = $query[5];
}
unset($_REQUEST['ids']);
} elseif (is_numeric($request)) {
$id = $ids[] = $request;
$_REQUEST[$id]['ids'] = $id;
$_REQUEST[$id]['cooperation'] = $_REQUEST['cooperation'];
$_REQUEST[$id]['cps_ratio'] = $_REQUEST['cps_ratio'];
$_REQUEST[$id]['cpa_price'] = $_REQUEST['cpa_price'];
$_REQUEST[$id]['unum'] = $_REQUEST['unum'];
$_REQUEST[$id]['spay_amount'] = $_REQUEST['spay_amount'];
} else {
$this->error('参数有误!!!');
}
sort(array_unique($ids));
if (is_array($ids)) {
$promote_id = get_promote_id($_REQUEST['promote_account']);
$create_time = time();
foreach ($ids as $k => $v) {
if (get_settlement($_REQUEST['timestart'], $_REQUEST['timeend'], $promote_id, $_REQUEST[$v]['game_id'])) {
continue;
}
$data[$k]['game_id'] = $_REQUEST[$v]['game_id'];
$data[$k]['game_name'] = get_game_name($_REQUEST[$v]['game_id']);
$data[$k]['promote_account'] = $_REQUEST['promote_account'];
$data[$k]['promote_id'] = $promote_id;
$data[$k]['total_money'] = $_REQUEST[$v]['spay_amount'];
$data[$k]['total_number'] = $_REQUEST[$v]['unum'];
$data[$k]['starttime'] = strtotime($_REQUEST['timestart']);
$data[$k]['endtime'] = strtotime($_REQUEST['timeend']) + 24 * 60 * 60 - 1;
$data[$k]['pattern'] = $_REQUEST[$v]['cooperation'] == 'CPS' ? 0 : 1;
$data[$k]['ratio'] = $_REQUEST[$v]['cps_ratio'];
$data[$k]['money'] = $_REQUEST[$v]['cpa_price'];
$data[$k]['create_time'] = $create_time;
$data[$k]['bind_coin_status'] = $_REQUEST['bind_coin'];
$data[$k]['settlement_number'] = 'JS-' . date('Ymd') . date('His') . sp_random_string(4);
if (get_settlement($data[$k]['starttime'], $data[$k]['endtime'], $data[$k]['promote_id'], $data[$k]['game_id'])) {
$this->error('该结算周期不可结算,请重新选择');
}
if ($data[$k]['pattern']) {
$data[$k]['sum_money'] = $data[$k]['total_number'] * $data[$k]['money'];
} else {
$data[$k]['sum_money'] = $data[$k]['total_money'] * $data[$k]['ratio'] / 100;
}
if ($data[$k]['game_id'] == '' || $data[$k]['promote_id'] == '' || $data[$k]['starttime'] == '' || $data[$k]['endtime'] == '') {
$this->error('必要参数不存在');
}
$map['fgame_id'] = $data[$k]['game_id'];
// $map['is_check']=1;
$map['puid'] = 0;
$map['register_time'] = array('BETWEEN', array($data[$k]['starttime'], $data[$k]['endtime']));
$allid = get_subordinate_promote_($data[$k]['promote_account']);
$allid[] = $data[$k]['promote_account'];
$map['promote_account'] = array('in', $allid);
$u = M('User', 'tab_');
$user = $u->where($map)->setField('settle_check', 1);
unset($map['register_time']);
unset($map['puid']);
$map['pay_status'] = 1;
$map['pay_time'] = array('BETWEEN', array($data[$k]['starttime'], $data[$k]['endtime']));
$s = M('spend', 'tab_');
$spend = $s->where($map)->setField('settle_check', 1);
}
}
$data = array_values($data);
$result = M('settlement', 'tab_')->addAll($data);
if ($result) {
$settMap['promote_id'] = $promote_id;
$settMap['starttime'] = strtotime($_REQUEST['timestart']);
$settMap['endtime'] = strtotime($_REQUEST['timeend']) + 24 * 60 * 60 - 1;
$settMap['create_time'] = $data[0]['create_time'];
$settMap['bind_coin_status'] = $_REQUEST['bind_coin'];
$dataSett = M('settlement', 'tab_')->field(array(
'settlement_number',
'starttime', 'endtime',
'promote_id', 'promote_account',
'sum(total_money) as total_money',
'sum(total_number) as total_number',
'sum(sum_money) as sum_money',
'status',
'ti_status',
'bind_coin_status',
'create_time'))->where($settMap)
->group('promote_id,starttime,endtime,create_time,bind_coin_status')
->order('create_time desc')
->select();
M('TotalSettlement', 'tab_')->addAll($dataSett);
$this->success('结算成功', U('Query/settlement', array('group' => 2)));
} else {
/*if (is_array($ids)) {
$promote_id = get_promote_id($_REQUEST['promote_account']);
foreach ($ids as $k => $v) {
if(get_settlement($_REQUEST['timestart'],$_REQUEST['timeend'],$promote_id,$_REQUEST[$v]['game_id'])){
continue;
}
$errorStartTime = strtotime($_REQUEST['timestart']);
$errorEndTime = strtotime($_REQUEST['timeend'])+24*60*60-1;
$game_id = $_REQUEST[$v]['game_id'];
$map['fgame_id']=$game_id;
$map['register_time']=array('BETWEEN',array($errorStartTime,$errorEndTime));
$map['promote_id']=array('in',$promote_id);
$u=M('User','tab_');
$user=$u->where($map)->setField('settle_check',0);
unset($map['register_time']);
$map['pay_time']=array('BETWEEN',array($errorStartTime,$errorEndTime));
$s=M('spend','tab_');
$spend=$s->where($map)->setField('settle_check',0);
}
}*/
$this->error('结算失败');
}
}
public function generatesettlement()
{
//批量结算要加判断
$data['game_id'] = $_REQUEST['game_id'];
$data['game_name'] = get_game_name($_REQUEST['game_id']);
$data['promote_id'] = $_REQUEST['promote_id'];
$data['promote_account'] = get_promote_name($_REQUEST['promote_id']);
$data['total_money'] = $_REQUEST['spay_amount'];
$data['total_number'] = $_REQUEST['unum'];
$data['starttime'] = strtotime($_REQUEST['starttime']);
$data['endtime'] = strtotime($_REQUEST['endtime']) + 24 * 60 * 60 - 1;
$data['pattern'] = $_REQUEST['cooperation'] == 'CPS' ? 0 : 1;
$data['ratio'] = $_REQUEST['cps_ratio'];
$data['money'] = $_REQUEST['cpa_price'];
$data['create_time'] = time();
$data['settlement_number'] = 'JS-' . date('Ymd') . date('His') . sp_random_string(4);
if (get_settlement($data['starttime'], $data['promote_id'], $data['game_id'])) {
$this->error('该结算周期不可结算,请重新选择');
}
if ($data['pattern']) {
$data['sum_money'] = $data['total_number'] * $data['money'];
} else {
$data['sum_money'] = $data['total_money'] * $data['ratio'] / 100;
}
if ($data['game_id'] == '' || $data['promote_id'] == '' || $data['starttime'] == '' || $data['endtime'] == '') {
$this->error('必要参数不存在');
}
$map['fgame_id'] = $data['game_id'];
// $map['is_check']=1;
$map['register_time'] = array('BETWEEN', array($data['starttime'], $data['endtime']));
$allid = get_subordinate_promote_($data['promote_account']);
$allid[] = $data['promote_account'];
$map['promote_id'] = array('in', $data['promote_id']);
$u = M('User', 'tab_');
$user = $u->where($map)->setField('settle_check', 1);
unset($map['register_time']);
$map['pay_time'] = array('BETWEEN', array($data['starttime'], $data['endtime']));
$s = M('spend', 'tab_');
$spend = $s->where($map)->setField('settle_check', 1);
$result = M('settlement', 'tab_')->add($data);
if ($result) {
$this->success('结算成功');
} else {
$this->error('结算失败');
}
}
public function generatecpsettlement()
{//cp结算
$request = I('request.ids');
if (empty($request)) {
$this->error('请选择要操作的数据');
}
$starttime = strtotime($_REQUEST['timestart']);
$endtime = strtotime($_REQUEST['endtime']) + 24 * 3600 - 1;
$map['s.pay_status'] = 1;
$map['s.selle_status'] = 0;
foreach ($request as $key => $value) {
$query = explode(',', $value);
$ids[] = $query[0];
$requestData[$key]['game_id'] = $query[0];
$requestData[$key]['selle_ratio'] = $query[1];
$requestData[$key]['total'] = $query[2];
}
$map['s.game_id'] = array('in', $ids);
$map['pay_time'] = array('BETWEEN', array($starttime, $endtime));
$spe = M('spend as s', 'tab_');
$smap = array('s.selle_time' => date('Y-m-d', time()), 's.selle_status' => 1);
$data = $spe
->field('s.id,s.selle_status,s.selle_time')
->join('tab_game as g on g.id=s.game_id', 'LEFT')
->where($map)
->setField($smap);
if ($data) {
$create_time = time();
foreach ($requestData as $key => $value) {
$datas[$key]['game_id'] = $value['game_id'];
$datas[$key]['game_name'] = get_game_name($value['game_id']);
$datas[$key]['promote_id'] = '0';
$datas[$key]['promote_account'] = '0';
$datas[$key]['total_money'] = $value['total'];
$datas[$key]['total_number'] = 0;
$datas[$key]['starttime'] = $starttime;
$datas[$key]['endtime'] = $endtime;
$datas[$key]['pattern'] = 0;//$_REQUEST['cooperation']=='CPS'?0:1;
$datas[$key]['ratio'] = $value['selle_ratio'];
$datas[$key]['sum_money'] = $value['selle_ratio'] * $value['total'] / 100;
$datas[$key]['money'] = 0;
$datas[$key]['developers'] = $_REQUEST['developers_id'];
$datas[$key]['create_time'] = $create_time;
$datas[$key]['settlement_number'] = 'JS-' . date('Ymd') . date('His') . sp_random_string(4);
$datas[$key]['bind_coin_status'] = $_REQUEST['bind_coin'];
}
$datas = array_values($datas);
$result = M('settlement', 'tab_')->addAll($datas);
$settMap['developers'] = $_REQUEST['developers_id'];
$settMap['starttime'] = $starttime;
$settMap['endtime'] = $endtime;
$settMap['create_time'] = $create_time;
$dataSett = M('settlement', 'tab_')->field(array(
'settlement_number',
'starttime', 'endtime',
'promote_id', 'promote_account',
'sum(total_money) as total_money',
'sum(total_number) as total_number',
'sum(sum_money) as sum_money',
'status',
'ti_status',
'bind_coin_status',
'developers',
'create_time',))->where($settMap)
->group('developers,starttime,endtime,create_time,bind_coin_status')
->order('create_time desc')
->select();
M('TotalSettlement', 'tab_')->addAll($dataSett);
\Think\Log::actionLog('Query/generatecpsettlement', 'Query', 1);
$this->success('结算成功', U('Query/cpsettlement', array('group' => 2)));
} else {
$this->error('结算失败');
}
$map1 = array('status' => 1, 'selle_status' => 1);
$total = null_to_0(D('spend')->where($map1)->sum('pay_amount'));
$ttotal = null_to_0(D('spend')->where('pay_time' . total(1))->where($map1)->sum('pay_amount'));
$ytotal = null_to_0(D('spend')->where('pay_time' . total(5))->where($map1)->sum('pay_amount'));
$this->assign('total', $total);
$this->assign('ttotal', $ttotal);
$this->assign('ytotal', $ytotal);
}
public function changeratio()
{
$gid = I('request.game_id');
if (empty($gid)) {
$this->ajaxReturn(0, "请选择要操作的数据", 0);
exit;
}
$starttime = strtotime($_REQUEST['timestart'] . '-01');
$endtime = strtotime($_REQUEST['timestart'] . "+1 month -1 day") + 24 * 3600 - 1;
$map['s.pay_status'] = 1;
$map['s.selle_status'] = 0;
$map['s.game_id'] = $_REQUEST['game_id'];
$map['pay_time'] = array('BETWEEN', array($starttime, $endtime));
$spe = M('spend as s', 'tab_');
$data = $spe
->field('s.id,s.selle_status,s.selle_ratio')
->join('tab_game as g on g.id=s.game_id', 'LEFT')
->where($map)
->setField('s.selle_ratio', $_POST['ratio']);
if ($data === false) {
$this->ajaxReturn(array('status' => 0));
} else {
$this->ajaxReturn(['status' => 1, 'data' => $data]);
}
}
public function cp_withdraw($p = 0)
{
if (isset($_REQUEST['settlement_number'])) {
$map['settlement_number'] = array('like', '%' . $_REQUEST['settlement_number'] . '%');
}
if (isset($_REQUEST['status'])) {
$map['status'] = $_REQUEST['status'];
}
if (isset($_REQUEST['developers'])) {
if ($_REQUEST['developers'] == '全部') {
unset($_REQUEST['developers']);
} else {
$map['developers'] = $_REQUEST['developers'];
}
} else {
$map['developers'] = array('neq', 0);
}
if (isset($_REQUEST['timestart']) && isset($_REQUEST['timeend'])) {
$starttime = strtotime($_REQUEST['timestart']);
$endtime = strtotime($_REQUEST['timeend']) + 24 * 3600 - 1;
$map['audit_time'] = array('BETWEEN', array($starttime, $endtime));
unset($_REQUEST['timestart']);
unset($_REQUEST['timeend']);
} elseif (isset($_REQUEST['timestart'])) {
$map['audit_time'] = ['GT', strtotime(I('timestart'))];
unset($_REQUEST['timestart']);
} elseif (isset($_REQUEST['timeend'])) {
$map['audit_time'] = ['LT', strtotime(I('timeend')) + 86399];
unset($_REQUEST['timeend']);
}
if ($_REQUEST['create_time'] == 2) {
$order = 'create_time desc';
} elseif ($_REQUEST['create_time'] == 1) {
$order = 'create_time asc';
} else {
$order = 'create_time desc';
}
if ($_REQUEST['sum_money'] == 2) {
$order = 'sum_money desc';
} elseif ($_REQUEST['sum_money'] == 1) {
$order = 'sum_money asc';
}
$model = array(
'm_name' => 'withdraw',
'order' => $order,
'title' => '渠道提现',
'template_list' => 'cp_withdraw',
);
$map1 = $map;
$map1['status'] = 1;
if (isset($_REQUEST['status']) && $_REQUEST['status'] == 0) {
$total = '0.00';
$ttotal = '0.00';
$ytotal = '0.00';
} else {
$total = null_to_0(D('withdraw')->where($map1)->sum('sum_money'));
$ttotal = null_to_0(D('withdraw')->where('audit_time' . total(1))->where($map1)->sum('sum_money'));
$ytotal = null_to_0(D('withdraw')->where('audit_time' . total(5))->where($map1)->sum('sum_money'));
}
$this->assign('stotal', $total);
$this->assign('ttotal', $ttotal);
$this->assign('ytotal', $ytotal);
$user = A('Bill', 'Event');
$this->m_title = '开发者提现';
$this->assign('commonset', M('Kuaijieicon')->where(['url' => 'Query/cp_withdraw', 'status' => 1])->find());
$user->money_list($model, $p, $map);
}
public function withdraw()
{
$adminid = C('USER_ADMINISTRATOR');//获取超管id
$adminmobile = M('UcenterMember')->field('mobile')->find($adminid);
$this->assign('adminmobile', $adminmobile['mobile']);
if (isset($_REQUEST['widthdraw_number'])) {
$map['widthdraw_number'] = $_REQUEST['widthdraw_number'];
}
if (isset($_REQUEST['ti_status'])) {
$map['status'] = $_REQUEST['ti_status'];
}
if (isset($_REQUEST['op_type'])) {
$map['op_type'] = $_REQUEST['op_type'];
}
if (isset($_REQUEST['promote_account'])) {
if ($_REQUEST['promote_account'] == '全部') {
unset($_REQUEST['promote_account']);
} else {
$map['promote_account'] = $_REQUEST['promote_account'];
}
} else {
$map['promote_id'] = array('gt', 0);
}
$order = 'create_time desc';
if (isset($_REQUEST['data_order'])) {
$dataOrder = explode(',', $_REQUEST['data_order']);
if (count($dataOrder) == 2 && in_array($dataOrder[1], ['create_time', 'sum_money'])) {
if (intval($dataOrder[0]) == 4) {
$order = $dataOrder[1] . ' desc';
} elseif (intval($dataOrder[0]) == 3) {
$order = $dataOrder[1] . ' asc';
}
}
}
$model = array(
'm_name' => 'withdraw',
'order' => $order,
'title' => '渠道提现',
'template_list' => 'withdraw',
);
$map1 = array('status' => ['neq', -1]);
//为数据权限添加
setPowerPromoteIds($map1);
$total = null_to_0(D('withdraw')->where($map1)->sum('sum_money'));
$ttotal = null_to_0(D('withdraw')->where('create_time' . total(1))->where($map1)->sum('sum_money'));
$ytotal = null_to_0(D('withdraw')->where('create_time' . total(5))->where($map1)->sum('sum_money'));
$this->assign('total', $total);
$this->assign('ttotal', $ttotal);
$this->assign('ytotal', $ytotal);
$user = A('Bill', 'Event');
$this->m_title = '推广提现';
$this->assign('commonset', M('Kuaijieicon')->where(['url' => 'Query/withdraw', 'status' => 1])->find());
//为数据权限添加
setPowerPromoteIds($map);
$this->assign('is_admin', is_administrator());
$user->money_list($model, $p, $map);
}
public function set_withdraw_status()
{
$withdraw = M('withdraw', "tab_");
$ids = I('ids', 0);
$status = 1;
if (empty($ids)) {
$this->error('参数异常');
}
if (is_array($ids)) {
$map['id'] = ['in', $ids];
} else {
$map['id'] = $ids;
}
$map['status'] = 0;
$save['audit_time'] = time();
$save['status'] = $status;
$res = $withdraw->where($map)->save($save);
if ($res === false) {
$this->error('审核失败');
} else {
$this->success('审核成功', U('withdraw'));
}
}
public function set_withdraw()
{
$case = I('case', '');
if (empty($case)) {
$data['status'] = 0;
$data['msg'] = '参数异常';
$this->ajaxReturn($data);
}
switch ($case) {
case 'deny':
$data = $this->set_withdraw_deny();
break;
case 'upload_transfer_proof':
$data = $this->set_withdraw_transfer_proof();
break;
default:
$data['status'] = 0;
$data['msg'] = '参数异常';
break;
}
$this->ajaxReturn($data);
}
public function set_withdraw_deny()
{
$withdraw = M('withdraw', "tab_");
$ids = I('ids', 0);
$respond = I('respond', '');
$status = -1;
if (empty($ids)) {
$data['status'] = 0;
$data['msg'] = '参数异常';
return $data;
}
if (empty($respond)) {
$data['status'] = 0;
$data['msg'] = '请填写驳回说明';
return $data;
}
$idsData = explode(',', $ids);
if (is_array($ids) || (is_array($idsData) && count($idsData) > 1)) {
$map['id'] = ['in', $ids];
} else {
$map['id'] = $ids;
}
$map['status'] = 0;
$save['audit_time'] = time();
$save['status'] = $status;
$save['respond'] = $respond;
$res = $withdraw->where($map)->save($save);
if ($res === false) {
$data['status'] = 0;
$data['msg'] = '驳回失败';
} else {
$data['status'] = 1;
$data['msg'] = '驳回成功';
}
return $data;
}
public function set_withdraw_transfer_proof()
{
$withdraw = M('withdraw', "tab_");
$ids = intval(I('ids', 0));
$transferProof = intval(I('transfer_proof', 0));
$status = 2;
if (empty($ids)) {
$data['status'] = 0;
$data['msg'] = '参数异常';
return $data;
}
if (empty($transferProof)) {
$data['status'] = 0;
$data['msg'] = '请上传汇款证明';
return $data;
}
$downloadId = M('document_download')->where(array('file_id' => $transferProof))->getField('id');
if (empty($downloadId)) {
$fileData = M('File')->field('size,ext')->find($transferProof);
$downloadSave['content'] = '推广员提现订单汇款证明';
$downloadSave['file_id'] = $transferProof;
$downloadSave['size'] = $fileData['size'];
$downloadRes = M('document_download')->add($downloadSave);
if (!$downloadRes) {
$data['status'] = 0;
$data['msg'] = '保存失败';
return $data;
}
$downloadId = $downloadRes;
}
$map['id'] = $ids;
$map['status'] = 1;
$save['audit_time'] = time();
$save['status'] = $status;
$save['transfer_proof'] = $downloadId;
$res = $withdraw->where($map)->save($save);
if ($res === false) {
$data['status'] = 0;
$data['msg'] = '保存失败';
} else {
$data['status'] = 1;
$data['msg'] = '保存成功';
}
return $data;
}
private function set_total_settlement_ti_status($settlement_number)
{
$totalSettlementMap['settlement_number'] = $settlement_number;
$totalSettlement = M('TotalSettlement', 'tab_');
$totalSettlement->where($totalSettlementMap)->save(array("ti_status" => $_REQUEST['status']));
$dataTotalSettlement = $totalSettlement->where($totalSettlementMap)->find();
$settMap['promote_id'] = $dataTotalSettlement['promote_id'];
$settMap['starttime'] = $dataTotalSettlement['starttime'];
$settMap['endtime'] = $dataTotalSettlement['endtime'];
$settMap['create_time'] = $dataTotalSettlement['create_time'];
M("settlement", "tab_")->where($settMap)->save(array('ti_status' => $_REQUEST['status']));
}
protected function upPromote($promote_id)
{
$model = D('Promote');
$data['id'] = $promote_id;
$data['money'] = 0;
return $model->save($data);
}
public function details($promote_id = 0, $create_time = 0)
{
$map['promote_id'] = $promote_id;
$map['create_time'] = $create_time;
$data = M('settlement', 'tab_')->where($map)->select();
$total = M('settlement', 'tab_')->where($map)->sum('sum_money');
$this->assign('total', $total);
$this->assign('list_data', $data);
$this->display();
}
public function detailscps($developers_id = 0, $create_time = 0, $p = 0)
{
$page = intval($p);
$page = $page ? $page : 1; //默认显示第一页数据
$row = !empty(C('ADMIN_LIST_ROW')) ? C('ADMIN_LIST_ROW') : 10;
$map['developers_id'] = $developers_id;
$map['create_time'] = $create_time;
$data = M('settlement', 'tab_')->where($map)->page($page, $row)->select();
$count = M('settlement', 'tab_')->where($map)->count();
if ($count > $row) {
$page = new \Think\Page($count, $row);
$page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
$this->assign('_page', $page->show());
}
$total = M('settlement', 'tab_')->where($map)->sum('sum_money');
$this->assign('total', $total);
$this->assign('list_data', $data);
$this->display();
}
public function withdrawDetails($id)
{
$page = intval(I('p', 1));
$row = intval(I('row', 10));
$id = intval($id);
$map['withdraw_id'] = $id;
$model = M('Spend', 'tab_');
$data = $model
->field('pay_order_number,game_name,user_account,promote_account,pay_amount,pay_way,if(selle_ratio >= 0,selle_ratio,0) as selle_ratio,pay_time,selle_status,pay_status,withdraw_id')
->where($map)
->order('id desc')
->page($page, $row)
->select();
$count = $model->where($map)->count('id');
//提现状态
$withdrawStatus = M('withdraw', 'tab_')
->where(array('id' => $id))
->getField('status');
$withdrawStatus = \Home\Controller\FinanceController::$withdrawStatus[$withdrawStatus];
foreach ($data as &$list) {
$list['income'] = bcmul($list['pay_amount'], bcdiv($list['selle_ratio'], 100, 2), 2);
$list['pay_time'] = date('Y-m-d H:i:s', $list['pay_time']);
}
$page = set_pagination($count, $row);
if ($page) {
$this->assign('_page', $page);
}
$this->assign('listData', $data);
$this->assign('withdrawStatus', $withdrawStatus);
$this->display();
}
/**
* 审核推广提现
* @author <jszsl001@163.com>
*/
public function set_withdraw_agree()
{
//行为日志判断
if (I('status') == 1 && I('msg_type') == 5 && I('field') == 'status') {
//同意推广提现
action_log('tg_tx_agree', 'withdraw', UID, UID);
}
if (I('status') == 2 && I('msg_type') == 6 && I('field') == 'status') {
//拒绝推广提现
action_log('tg_tx_disagree', 'withdraw', UID, UID);
}
//判断打款类型
if (I('withdraw_type', 0, 'intval') == 1) {
//手动打款,直接就改状态
$this->set_withdraw_status();
} else {
$adminid = C('USER_ADMINISTRATOR');//获取超管id
$adminmobile = M('UcenterMember')->field('mobile')->find($adminid);
$dx = A('Phone');
$res = -1;
$res = $dx->check_tel_code($adminmobile['mobile'], $_POST['code']);
switch ($res) {
case '-1':
$this->error('短信验证码无效,请重新获取');
break;
case '-2':
$this->error('时间超时,请重新获取短信验证码');
break;
case '-3':
$this->error('短信验证码不正确,请重新输入');
break;
}
$withdraw = M('withdraw', "tab_");
$pay = new \Think\Pay('alipay', C('alipay'));
$withdraw_way = $_POST['withdraw_way'];
//支付宝
if ($withdraw_way == 1) {
$map['id'] = $_POST['ids'];
$dind = $withdraw->where($map)->find();
if ($dind['status'] == 1) {
$this->ajaxReturn(['status' => -1, 'info' => '请不要重复打款']);
}
$widthdrawNo = "TX_" . date('Ymd') . date('His') . sp_random_string(4);
$vo = new \Think\Pay\PayVo();
$vo->setOrderNo($dind['settlement_number'])
->setBatchNo($widthdrawNo)
->setTable('Withdraw')
->setPayMethod("transfer")
->setDetailData('渠道结算提现,订单' . $dind["settlement_number"]);
$res = $pay->buildRequestForm($vo);
if ($res == 10000) {
$this->ajaxReturn(['status' => 1, 'info' => '打款成功']);
} else {
$this->ajaxReturn(['status' => 0, 'info' => '打款失败']);
}
}
}
}
}