<?php
namespace Admin\Controller;
use OSS\Core\OssException;
class FinanceController extends ThinkController
{
function index($p = 1) {
//总流水查询
$page = intval($p);
$page = $page ? $page : 1; //默认显示第一页数据
$row=10;
if(isset($_REQUEST['row'])) {$row = $_REQUEST['row'];}else{$row = 10;}
// var_dump($row);die();
$map = [];
if(!empty(I('count_date'))){
$map['count_date']=I('count_date');
}
// if (!empty(I('game_id'))) {
// $map['game_id']=I('game_id');
// }
// if (!empty(I('partner_id'))) {
// $map['partner_id']=I('collaborate_id');
// }
// $data = M("spend_count","tab_")
// ->field("count_date,cash_count,balance_coin_count,inside_cash_count,all_cash_count")
// ->where($map)
// ->page($page,$row)
// ->group("count_date")
// ->select();
// if (empty(I('game_id'))) {
// $field = "count_date,sum(cash_count) as cash_count,sum(balance_coin_count) as balance_coin_count,
// sum(inside_cash_count) as inside_cash_count,sum(cash_count+balance_coin_count) as all_cash_count";
// $InsideField = "inside_cash_count";
// $map['inside_cash_count'] = ['neq',0];
// $insideData = D("spend_count")->getSpendCountData($map,"count_date,inside_cash_count as count","promote_id,count_date");
// unset($map['inside_cash_count']);
// } else {
// $field = "count_date,sum(cash_count) as cash_count,sum(balance_coin_count) as balance_coin_count,
// sum(bind_coin_count) as inside_cash_count,sum(cash_count+balance_coin_count) as all_cash_count";
// $InsideField = "bind_coin_count";
// $map['bind_coin_count'] = ['neq',0];
// $insideData = D("spend_count")->getSpendCountData($map,"count_date,bind_coin_count as count","promote_id,count_date,game_id");
// unset($map['bind_coin_count']);
// }
// //内充流水与绑定流水数据
// $insideDataSum = [];
// foreach ($insideData as $key => $value) {
// if (isset($insideDataSum[$value['count_date']])) {
// $insideDataSum[$value['count_date']] = $insideDataSum[$value['count_date']] + $value['count'];
// } else {
// $insideDataSum[$value['count_date']] = $value['count'];
// }
// }
// var_dump($insideDataSum);
// var_dump(array_sum($insideDataSum));
// echo D("spend_count")->_sql();
// var_dump($insideData);die();
// $group = "count_date";
// $order = "id DESC";
// $data = D("spend_count")->getSpendCountData($map,$field,$group,$order,$page,$row);
//
// foreach ($data as $key => $value) {
// $data[$key]['all_cash_count'] = $value['all_cash_count'] + $insideDataSum[$value['count_date']];
// $data[$key]['inside_cash_count'] = $insideDataSum[$value['count_date']];
// }
//
// $sumCash = D("spend_count")->sumSpendCountField($map,"cash_count");
// $sumBalance = D("spend_count")->sumSpendCountField($map,"balance_coin_count");
// $sumInside = array_sum($insideDataSum);
// $allField = "cash_count+balance_coin_count";
// $sumAll = D("spend_count")->sumSpendCountField($map,$allField)+array_sum($insideDataSum);
//
// $count = D("spend_count")->getSpendCountData($map,$field,$group,'');
$order = "id DESC";
$data = M('spend_month_count','tab_')
->field("cash_count,balance_coin_count,inside_cash_count,all_count,count_date,balance_coin_deposit,bind_coin_count")
->where($map)
->page($page,$row)
->order($order)
->select();
$count = M('spend_month_count','tab_')
->field("cash_count,balance_coin_count,inside_cash_count,all_count,count_date")
->where($map)
->order($order)
->select();
$sumData = M('spend_month_count','tab_')
->field("sum(cash_count) as cash_count,sum(balance_coin_count) as balance_coin_count,sum(inside_cash_count) as inside_cash_count,sum(all_count) as all_count,count_date,sum(balance_coin_deposit) as balance_coin_deposit,sum(bind_coin_count) as bind_coin_count")
->where($map)
->order($order)
->find();
// echo D("spend_count")->_sql();die();
$count = count($count);
$page = set_pagination($count,$row);
// var_dump($page);die();
if($page) {$this->assign('_page', $page);}
$this->meta_title = '月结统计';
// $this->assign('sumCash',$sumCash);
// $this->assign('sumBalance',$sumBalance);
// $this->assign('sumInside',$sumInside);
// $this->assign('sumAll',$sumAll);
$this->assign('sumData',$sumData);
$this->assign('data',$data);
$this->display();
}
function gameFinance($p = 1) {
$page = intval($p);
$page = $page ? $page : 1; //默认显示第一页数据
$row=10;
if(isset($_REQUEST['row'])) {$row = $_REQUEST['row'];}else{$row = 10;}
$map = [];
if(!empty(I('count_date'))){
$map['count_date']=I('count_date');
}
if (!empty(I('root_name'))) {
$map['promote_account'] = array('like','%'.I("root_name").'%');
}
if (!empty(I("game_id"))) {
$map['game_id'] = I("game_id");
}
if(!empty(I('root_id'))||I('root_id')=='0') {
$root_id = I('root_id');
if ($root_id == '0') {
$map['_string'] = "promote_id = {$root_id}";
} else if($root_id){
$map['_string'] = "promote_id = {$root_id} or parent_id = {$root_id} or root_id = {$root_id}";
}
}
// if (!empty(I('partner_id'))) {
// $map['partner_id']=I('collaborate_id');
// }
if (I('sign') == 2& & empty(I('root_id'))) {
$map['_string'] = "parent_id >0 or root_id>0";
}
if (I('sign') == 3& & empty(I('root_id'))) {
$map['_string'] = "parent_id >0 and root_id>0";
}
// var_dump($map);die();
if(!empty(I('parent_id'))) {
$parent_id = I('parent_id');
$map['_string'] = "promote_id = {$parent_id} or parent_id = {$parent_id}";
}
if (!empty(I('promote_id'))) {
$promote_id = I('promote_id');
$map['_string'] = "promote_id = {$promote_id}";
}
$map['inside_cash_count'] = ['neq',0];
$insideData = D("spend_count")->getSpendCountData($map,"game_id,inside_cash_count as count","promote_id,count_date");
unset($map['inside_cash_count']);
//内充流水与绑定流水数据
$insideDataSum = [];
foreach ($insideData as $key => $value) {
if (isset($insideDataSum[$value['game_id']])) {
$insideDataSum[$value['game_id']] = $insideDataSum[$value['game_id']] + $value['count'];
} else {
$insideDataSum[$value['game_id']] = $value['count'];
}
}
// var_dump($insideDataSum);die();
$field = "count_date,sum(cash_count) as cash_count,sum(balance_coin_count) as balance_coin_count,
sum(inside_cash_count) as inside_cash_count,sum(cash_count+balance_coin_count) as all_cash_count,game_name,game_id,partner_name";
$group = "game_id";
$order = "id DESC";
$data = D("spend_count")->getSpendCountData($map,$field,$group,$order,$page,$row);
foreach ($data as $key => $value) {
$data[$key]['inside_cash_count'] = number_format($insideDataSum[$value['game_id']],2,'.','');
$data[$key]['all_cash_count'] = number_format($insideDataSum[$value['game_id']]+$data[$key]['all_cash_count'],2,'.','');
}
$sumCash = D("spend_count")->sumSpendCountField($map,"cash_count");
$sumBalance = D("spend_count")->sumSpendCountField($map,"balance_coin_count");
$sumInside = number_format(array_sum($insideDataSum),2,'.','');
$sumAll = D("spend_count")->sumSpendCountField($map,"cash_count+balance_coin_count") + array_sum($insideDataSum);
$count = D("spend_count")->getSpendCountData($map,$field,$group,$order);
$count = count($count);
$page = set_pagination($count,$row);
if($page) {$this->assign('_page', $page);}
$this->meta_title = '每月情况查看详情';
$this->assign('from',I('from'));
$this->assign('sumCash',$sumCash);
$this->assign('sumBalance',$sumBalance);
$this->assign('sumInside',$sumInside);
$this->assign('sumAll',$sumAll);
$this->assign('data',$data);
$this->display();
}
function gameFinanceDetail($p = 1) {
if (!empty(I("promote_id"))) {
$promote_id[] = I("promote_id");
}
if (!empty(I("root_id"))) {
$root_id = I("root_id");
$promote_id = $this->getPromoteList($root_id);
array_push($promote_id,$root_id);
}
if ($_REQUEST['root_id']=='0') {
$map['promote_id'] = $_REQUEST['root_id'];
}
if (!empty(I("parent_id"))) {
$parent_id = I("parent_id");
$promote_id = $this->getPromoteList($parent_id);
array_push($promote_id,$parent_id);
}
$promote_id = implode(',',$promote_id);
if ($promote_id) {
$map['promote_id'] = ['in',$promote_id];
}
$page = intval($p);
$page = $page ? $page : 1; //默认显示第一页数据
$row=10;
if(isset($_REQUEST['row'])) {$row = $_REQUEST['row'];}else{$row = 10;}
if (!empty(I("count_date"))) {
$month = I("count_date");
$tarry = explode('-',$month);
$startTime=mktime(0,0,0,$tarry[1],1,$tarry[0]);
$endTime =mktime(0,0,0,$tarry[1]-0+1,1,$tarry[0])-1;
$map['pay_time'] = array('between',array($startTime,$endTime));
}
$map['pay_status'] = 1;
if (!empty(I("game_id"))) {
$map['game_id'] = I("game_id");
}
if (!empty(I('user_account'))) {
$map['user_account'] = array('like','%'.I("user_account").'%');
}
if (!empty(I('user_nickname'))) {
$map['game_player_name'] = array('like',I("user_nickname").'%');
}
// var_dump($map);die();
$field = "pay_order_number,FROM_UNIXTIME(pay_time) as pay_time,user_account,user_nickname,game_name,promote_account,spend_ip,server_name,game_player_name,pay_amount,cost,pay_way";
$group = "";
$order = "pay_time DESC";
$data = D('spend')->getSpendData($map,$field,$group,$order,$page,$row);
// echo D("spend")->_sql();die();
foreach($data as $key => $value) {
$data[$key]['pay_way'] = getPayType($value['pay_way']);
}
$sum = D('spend')->sumSpend($map,'pay_amount');
$count = D("spend")->getSpendData($map,$field,$group,$order);
$count = count($count);
$page = set_pagination($count,$row);
if($page) {$this->assign('_page', $page);}
$this->meta_title = '游戏充值流水';
$this->assign('data',$data);
$this->assign('sum_amount',$sum);
$this->display();
}
//按月份获取会长统计
public function getRootUser($p = 1)
{
$page = intval($p);
$page = $page ? $page : 1; //默认显示第一页数据
$row=10;
if(isset($_REQUEST['row'])) {$row = $_REQUEST['row'];}else{$row = 10;}
//获取所有数据
if (!empty(I("count_date"))) {
$map['count_date'] = I("count_date");
}
if (!empty(I("root_name"))) {
$map['promote_account']=['like','%'.I("root_name").'%'];
}
$map['parent_id'] = 0;
$map['root_id'] = 0;
$subQueryAll = M('spend_user_count','tab_')
->field('promote_id,promote_account,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count,sum(inside_cash_count) inside_cash_count, sum(all_count) as all_count, sum(bind_coin_count) as bind_coin_count')
->where($map)
->group('promote_id')
->page($page,$row)
->select();
$sumData = M('spend_user_count','tab_')
->field('sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count,sum(inside_cash_count) inside_cash_count, sum(all_count) as all_count, sum(bind_coin_count) as bind_coin_count')
->where($map)
->find();
$count = M('spend_user_count','tab_')
->field('promote_id,promote_account,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count,sum(inside_cash_count) inside_cash_count, sum(all_count) as all_count')
->where($map)
->group('promote_id')
->select();
$count = count($count);
$page = set_pagination($count,$row);
if($page) {$this->assign('_page', $page);}
$this->meta_title = '会长流水';
$this->assign('sumData',$sumData);
$this->assign('data',$subQueryAll);
$this->display();
}
public function getParentUser($p = 1)
{
$page = intval($p);
$page = $page ? $page : 1; //默认显示第一页数据
$row=10;
if(isset($_REQUEST['row'])) {$row = $_REQUEST['row'];}else{$row = 10;}
if (!empty(I("count_date")) ) {
$month = I("count_date");
$map['count_date'] =$month;
}
$parentData = [];
if (!empty(I("root_id"))) {
$root_id = I("root_id");
if ($page == 1) {
$parentData = $this->getParentSpendData($root_id,$month);
}
$map['parent_id'] =$root_id;
} else if(!empty(I("sign"))){
$map['parent_id'] = ['neq',0];
$map['root_id'] = ['eq',0];
} else {
if (I("root_id") == 0) {
$root_id = I("root_id");
$map['promote_id'] = $root_id;
}
}
if (!empty(I("root_name"))) {
$rootname = I("root_name");
$map['promote_account'] =['like','%'.$rootname.'%'];
}
$subQueryAll = M('spend_user_count','tab_')
->field('promote_id,promote_account,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count,sum(inside_cash_count) inside_cash_count, sum(all_count) as all_count, sum(bind_coin_count) as bind_coin_count')
->where($map)
->group('promote_id')
->page($page,$row)
->select();
if (empty(I("root_name"))) {
$subQueryAll = array_merge($parentData,$subQueryAll);
}
$sumData = M('spend_user_count','tab_')
->field('promote_id,promote_account,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count,sum(inside_cash_count) inside_cash_count, sum(all_count) as all_count, sum(bind_coin_count) as bind_coin_count')
->where($map)
->find();
if (empty(I("root_name"))) {
$sumData['cash_count'] = number_format($sumData['cash_count'] + $parentData[0]['cash_count'], 2, '.', '');
$sumData['balance_coin_count'] = number_format($sumData['balance_coin_count'] + $parentData[0]['balance_coin_count'], 2, '.', '');
$sumData['inside_cash_count'] = number_format($sumData['inside_cash_count'] + $parentData[0]['inside_cash_count'], 2, '.', '');
$sumData['all_count'] = number_format($sumData['all_count'] + $parentData[0]['all_count'], 2, '.', '');
$sumData['bind_coin_count'] = number_format($sumData['bind_coin_count'] + $parentData[0]['bind_coin_count'], 2, '.', '');
}
$count = M('spend_user_count','tab_')
->field('promote_id,promote_account,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count,sum(inside_cash_count) inside_cash_count, sum(all_count) as all_count')
->where($map)
->group('promote_id')
->select();
$count = count($count);
$page = set_pagination($count,$row);
if($page) {$this->assign('_page', $page);}
$this->meta_title = '组长流水';
$this->assign('sumData',$sumData);
$this->assign('data',$subQueryAll);
$this->display();
}
public function getPromoteUser($p = 1)
{
$page = intval($p);
$page = $page ? $page : 1; //默认显示第一页数据
$row=10;
if(isset($_REQUEST['row'])) {$row = $_REQUEST['row'];}else{$row = 10;}
if (!empty(I("count_date"))) {
$month = I("count_date");
$map['count_date'] =$month;
}
$parentData = [];
if (!empty(I("parent_id"))) {
$parent_id = I("parent_id");
if ($page == 1) {
$parentData = $this->getParentSpendData($parent_id,$month);
}
$map['parent_id'] =$parent_id;
}
if (!empty(I('sign'))) {
$map['parent_id'] = ['neq',0];
if (!empty(I('root_id'))) {
$map['root_id'] = I('root_id');
} else {
$map['root_id'] = ['neq',0];
}
}
// var_dump($map);die();
// var_dump($map);die();
$subQueryAll = M('spend_user_count','tab_')
->field('promote_id,promote_account,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count,sum(inside_cash_count) inside_cash_count, sum(all_count) as all_count, sum(bind_coin_count) as bind_coin_count')
->where($map)
->group('promote_id')
->page($page,$row)
->select();
$subQueryAll = array_merge($parentData,$subQueryAll);
$sumData = M('spend_user_count','tab_')
->field('promote_id,promote_account,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count,sum(inside_cash_count) inside_cash_count, sum(all_count) as all_count, sum(bind_coin_count) as bind_coin_count')
->where($map)
->find();
$sumData['cash_count'] = number_format($sumData['cash_count']+$parentData[0]['cash_count'],2,'.','');
$sumData['balance_coin_count'] = number_format($sumData['balance_coin_count']+$parentData[0]['balance_coin_count'],2,'.','');
$sumData['inside_cash_count'] = number_format($sumData['inside_cash_count']+$parentData[0]['inside_cash_count'],2,'.','');
$sumData['all_count'] = number_format($sumData['all_count']+$parentData[0]['all_count'],2,'.','');
$sumData['bind_coin_count'] = number_format($sumData['bind_coin_count']+$parentData[0]['bind_coin_count'],2,'.','');
$count = M('spend_user_count','tab_')
->field('promote_id,promote_account,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count,sum(inside_cash_count) inside_cash_count, sum(all_count) as all_count')
->where($map)
->group('promote_id')
->select();
$count = count($count);
$page = set_pagination($count,$row);
if($page) {$this->assign('_page', $page);}
$this->meta_title = '推广员流水';
$sum = array_sum($sumData);
$this->assign('sumData',$sumData);
$this->assign('data',$subQueryAll);
$this->display();
# code...
}
function getPromoteList($root_id = 0) {
$data = M('promote','tab_')
->field("id")
->where("parent_id={$root_id} or grand_id={$root_id}")
->select();
$handleData = [];
foreach ($data as $key => $value) {
array_push($handleData,$data[$key]['id']);
}
return $handleData;
}
function getGameList() {
if (I('partner_id')) {
$map['partner_id'] = I('partner_id');
}
$data = M('game','tab_')->field("id,game_name")->where($map)->select();
$this->ajaxReturn($data,'json');
}
function getParentSpendData($root_id,$date) {
$data = [];
if (!$root_id) {
$this->error('数据错误');
} else {
$map = [];
if ($root_id) {
$map['promote_id'] = $root_id;
}
if ($date) {
$map['count_date'] = $date;
}
$data = M('spend_user_count','tab_')
->field('promote_id,promote_account, sum(cash_count) as cash_count,sum(balance_coin_count) as balance_coin_count,sum(inside_cash_count) as inside_cash_count, sum(all_count) as all_count, sum(bind_coin_count) as bind_coin_count')
->where($map)->select();
$map1['parent_id'] = $data[0]['promote_id'];
$map1['count_date'] = $date;
$sunData = M('spend_user_count','tab_')
->field('promote_id,promote_account, sum(cash_count) as cash_count,sum(balance_coin_count) as balance_coin_count,sum(inside_cash_count) as inside_cash_count, sum(all_count) as all_count, sum(bind_coin_count) as bind_coin_count')
->where($map1)->find();
$data[0]['cash_count'] = number_format($data[0]['cash_count']-$sunData['cash_count'],2,'.','');
$data[0]['balance_coin_count'] = number_format($data[0]['balance_coin_count']-$sunData['balance_coin_count'],2,'.','');
$data[0]['inside_cash_count'] = number_format($data[0]['inside_cash_count']-$sunData['inside_cash_count'],2,'.','');
$data[0]['all_count'] = number_format($data[0]['all_count']-$sunData['all_count'],2,'.','');
$data[0]['bind_coin_count'] = number_format($data[0]['bind_coin_count']-$sunData['bind_coin_count'],2,'.','');
// $map['inside_cash_count'] = ['neq',0];
// $insideData = D("spend_count")->getSpendCountData($map,"promote_id,inside_cash_count as count","promote_id,count_date");
// unset($map['bind_coin_count']);
// //内充流水与绑定流水数据
// $insideDataSum = 0;
// foreach ($insideData as $key => $value) {
// if (isset($insideDataSum)) {
// $insideDataSum = $insideDataSum + $value['count'];
// } else {
// $insideDataSum = $value['count'];
// }
// }
}
// if (!$data[0]['cash_count']& & !$data[0]['balance_coin_count']& & !$data[0]['inside_cash_count']& & !$data[0]['all_count']) {
// $promoteData = M('promote','tab_')->field('account as promote_account')->where("id={$root_id}")->find();
//
// $data[0]['promote_id'] = $root_id;
// $data[0]['promote_account'] = $promoteData['promote_account'];
// $data[0]['cash_count'] = number_format(0.00,2,'.','');
// $data[0]['balance_coin_count'] = number_format(0.00,2,'.','');
// $data[0]['inside_cash_count'] = number_format(0.00,2,'.','');
// $data[0]['all_count'] = number_format(0.00,2,'.','');
// }
// $data[0]['inside_cash_count'] = number_format($insideDataSum,2,'.','');
// $data[0]['all_count'] = number_format($insideDataSum+$data[0]['all_count'],2,'.','');
// var_dump($data);die();
return $data;
}
}