优化市场结算脚本

master
chenzhi 4 years ago
parent 382f2527a0
commit 1b0124606a

@ -123,6 +123,9 @@ return array(
"tab_company_statement_info",
"tab_company_statement_pool",
"tab_financial_summary",
"tab_pay_statement_info"
"tab_pay_statement_info",
"tab_settleup_marketorder",
"tab_market_altogether",
"tab_coefficient"
]
);

@ -344,167 +344,6 @@ class TimingController extends AdminController {
}
public function caculateMarketList() {
$map = [];
$map['my_time'] = date('Y-m',strtotime('-1 month'));
if ($_REQUEST['count_date']) {
$map['my_time'] = $_REQUEST['count_date'];
}
if ($_REQUEST['count_date'] == 'all') {
$map['my_time'] = ['neq', date('Y-m',time())];
}
$spendMap['tab_spend.pay_status'] = 1;
//获取spend表中的数据同时根据会长id进行group分类
$data = M('Spend','tab_')
->field("FROM_UNIXTIME(pay_time,'%Y-%m') as my_time,sum(pay_amount) as pay_amount,game_id,tab_spend.game_name,SUBSTRING_INDEX(tab_spend.`game_name`,\"(\",1) as game_names,promote_id,promote_account,company_relation,company_belong,CASE WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(`chain`,\"/\",2),\"/\",-1)='' THEN promote_id ELSE SUBSTRING_INDEX(SUBSTRING_INDEX(`chain`,\"/\",2),\"/\",-1) END as root_id,tab_game.ratio,tab_game.relation_game_id,tab_spend.sdk_version ")
->join("left join tab_promote on promote_id = tab_promote.id")
->join("left join tab_game on tab_game.id=tab_spend.game_id")
->where($spendMap)
->group("my_time,game_id,root_id")
->order("my_time Desc")
->select(false);
//关联表获取会长账号名
$data = M()->table('('.$data.') as a')
->field("my_time,pay_amount,a.game_id,game_name,game_names,a.promote_id,a.promote_account,a.company_relation,a.company_belong,CASE WHEN root_id is null THEN 0 ELSE root_id END as root_id,CASE WHEN account is null THEN '官方渠道' ELSE account END as account,tab_ratio.ratio as ratio,turnover_ratio,a.ratio as game_ratio,a.sdk_version")
->join("left join tab_promote on root_id = tab_promote.id")
->join("left join tab_promote_game_ratio as tab_ratio on tab_ratio.game_id = a.relation_game_id and tab_ratio.promote_id=root_id and tab_ratio.status=1")
->where($map)
->group("my_time,game_id,root_id")
->order("my_time Desc")
->select();
// dump($data);die();
foreach($data as $key => $value) {
echo "{$value['my_time']}-{$value['promote_account']}-{$value['game_name']}:{$value['pay_amount']}\n";
$mytime = $value['my_time'];
$nextMonth = date('Y-m',strtotime("$mytime + 1 month"));
$game_id = $value['game_id'];
$gameInfo = M('game','tab_')->where(['id'=>$game_id])->getField('game_name');
$gameName = substr($gameInfo,0,strpos($gameInfo,'('));
$gameMap['game_name'] = ['like','%'.$gameName.'%'];
$gameIdArr = M('game','tab_')->field('id')->where($gameMap)->select();
$gameNameIdArr = [];
foreach($gameIdArr as $gameKey => $gameValue){
$gameNameIdArr[] = $gameValue['id'];
}
$promote_id = $value['promote_id'];
$pay_status = 1;
$promoteInfo = M('promote','tab_')->where(['id'=>$promote_id])->find();
$level = $promoteInfo['level'];
if($promote_id != 0) {
$promoteIdArr = [];
if ($level == 1) {
$rootPromoteId = $promote_id;
$promoteIdArr[] = $promote_id;
}else {
$chain = $promoteInfo['chain'];
$chainArr = explode('/',$chain);
$rootPromoteId = $chainArr[1];
}
$where1['chain'] = array('like', '%/' . $rootPromoteId. '/%');
$promoteIdInfo = M('promote','tab_')->field('id')->where($where1)->select();
foreach($promoteIdInfo as $key2 => $v1) {
$promoteIdArr[] = $v1['id'];
}
$where['promote_id'] = ['in',$promoteIdArr];
if (!$promoteIdArr) {
$where['promote_id']= 0;
}
}else {
$where['promote_id'] = 0;
}
$where['game_id'] = ['in',$gameNameIdArr];
$where['pay_status'] = 1;
$begTime = strtotime($mytime);
$endTime = strtotime($nextMonth);
$where['pay_time'] = ['between',[$begTime,$endTime]];
$spendInfo = M('spend','tab_')->field('pay_amount,pay_way')->where($where)->select();
$initNum = 0;
foreach($spendInfo as $key1 => $spendValue) {
$pay_way = $spendValue['pay_way'];
$channel_rate = $this->getChannelConfig($pay_way) / 100;
$initNum += $spendValue['pay_amount'] * $channel_rate;
}
$data[$key]['pay_amount'] = $value['pay_amount'] - $initNum;
$data[$key]['upstream'] = caculateUpstream($value['game_id'],$value['pay_amount']);
if ($value['ratio']) {
$data[$key]['downstream'] = $value['pay_amount'] * ($value['ratio']*0.01);
} else {
$data[$key]['downstream'] = $value['pay_amount'] * ($value['game_ratio']*0.01);
}
$value['turnover_ratio'] = json_decode($value['turnover_ratio'],true);
// dd($value);
if (is_array($value['turnover_ratio'])) {
foreach($value['turnover_ratio'] as $k =>$v) {
if(($value['pay_amount']>=$v['turnover']&&$v['instanceof']==1) || $value['pay_amount']>$v['turnover']) {
$data[$key]['downstream'] = $value['pay_amount'] * ($v['ratio']*0.01);
}
}
}
$data[$key]['highquality'] = number_format(($value['pay_amount'] - $initNum - $data[$key]['upstream']-$data[$key]['downstream']) * (1-0.0672),2,'.','');
if (!$data[$key]['company_belong']) {
$data[$key]['company_belong'] = getCompanyBlong(0);
} else {
$data[$key]['company_belong'] = getCompanyBlong($data[$key]['company_belong']);
}
$data[$key]['company_relation'] = $data[$key]['company_relation']?'只维护':'自主开发及维护';
}
foreach($data as $key => $value) {
$is_hav = M('market_balance','tab_')->where(['my_time'=>$value['my_time'],
'game_id'=>$value['game_id'],
'promote_id'=>$value['promote_id'],
'root_id'=>$value['root_id']])->find();
if (!$is_hav) {
M('market_balance','tab_')->add($value);
} else {
M('market_balance','tab_')
->where(['my_time'=>$value['my_time'],
'game_id'=>$value['game_id'],
'promote_id'=>$value['promote_id'],
'root_id'=>$value['root_id']])
->save(['pay_amount'=>$value['pay_amount'],
'ratio'=>$value['ratio'],
'turnover_ratio'=>$value['turnover_ratio'],
'game_ratio'=>$value['game_ratio'],
'upstream'=>$value['upstream'],
'downstream'=>$value['downstream'],
'highquality'=>$value['highquality']]);
}
}
echo 'success';
// dump($data);die();
}
public function getChannelConfig($pay_way = 0,$config=false) {
if(!$config) {
@ -764,7 +603,7 @@ class TimingController extends AdminController {
*/
public function getSpecialComplement($start_time=0,$end_time =0,$promote_id = 0) {
$data = M("company_statement","tab_")
$data = SM("company_statement","tab_")
// ->where("statement_begin_time<={$start_time} and statement_end_time<={$end_time} and withdraw_type=3")
->where("(statement_begin_time <={$end_time} AND ( statement_end_time = 0 OR statement_end_time >= {$start_time})) and withdraw_type=3")
->select();
@ -772,12 +611,12 @@ class TimingController extends AdminController {
$return = [];
foreach ($data as $key => $value) {
$info = [];
$pool_data = M("company_statement_pool","tab_")
$pool_data = SM("company_statement_pool","tab_")
->field("id")
->where("(statement_begin_time <={$end_time} AND ( statement_end_time = 0 OR statement_end_time >= {$start_time})) and withdraw_type=3 and statement_ids like '%{$value['id']}%'")
->select();
foreach ($pool_data as $pk => $pv) {
$info = M("company_statement_info","tab_")
$info = SM("company_statement_info","tab_")
->field("statement_info")
->where("(statement_begin_time <={$end_time} AND ( statement_end_time = 0 OR statement_end_time >= {$start_time})) and withdraw_type=3 and pool_id = {$pv['id']} and company_id = {$value['company_id']}")
->find()['statement_info'];
@ -1410,17 +1249,17 @@ class TimingController extends AdminController {
unset($promote_data[$key]['market_percentage']);
$marker_data = M("settleup_marketorder","tab_")
$marker_data = SM("settleup_marketorder","tab_")
->where(['pay_time'=>$value['pay_time'],'promote_id'=>$value['promote_id'],'game_name'=>$value['game_name'],'admin_id'=>$value['admin_id'],'is_settlement'=>$value['is_settlement']])
->find();
echo "日期:{$value['pay_time']},游戏:{$value['game_name']},推广员:{$value['promote_account']}\n";
echo "市场员:{$value['admin_id']} 推广员:{$value['promote_id']} 公司id{$value['company_id']} 游戏关联id{$value['relation_game_id']} 支付时间:{$value['pay_time']} 金额:{$value['pay_amount']}\n";
// dump($promote_data[$key]);die();
if (!$marker_data) {
M("settleup_marketorder","tab_")
SM("settleup_marketorder","tab_")
->add($promote_data[$key]);
} else {
M("settleup_marketorder","tab_")
SM("settleup_marketorder","tab_")
->where(['pay_time'=>$value['pay_time'],'promote_id'=>$value['promote_id'],'game_name'=>$value['game_name'],'admin_id'=>$value['admin_id'],'is_settlement'=>$value['is_settlement']])
->save($promote_data[$key]);
}
@ -1448,13 +1287,13 @@ class TimingController extends AdminController {
$value['remark'] = "";
$value['create_time'] = time();
$sign = M("market_altogether","tab_")
$sign = SM("market_altogether","tab_")
->where(['admin_id'=>$value['admin_id'],'pay_time'=>$time])
->find();
if (!$sign) {
M("market_altogether","tab_")->add($value);
SM("market_altogether","tab_")->add($value);
} elseif(!$sign['real_name']) {
M("market_altogether","tab_")->where(['admin_id'=>$value['admin_id'],'pay_time'=>$time])->save(['real_name'=>$value['real_name']]);
SM("market_altogether","tab_")->where(['admin_id'=>$value['admin_id'],'pay_time'=>$time])->save(['real_name'=>$value['real_name']]);
}
}
@ -1489,26 +1328,35 @@ class TimingController extends AdminController {
$map['pay_time'] = date('Y-m',strtotime(date('Y',time()).'-'.(date('m',time())-1).'-01'));;
// unset($map['pay_time']);
} else{
M("market_altogether","tab_")->where(['pay_time'=>$pay_time])->delete();
SM("market_altogether","tab_")->where(['pay_time'=>$pay_time])->delete();
$this->insertAllUserBonus($pay_time);
}
$map['is_settlement'] = 1;
$all_data = M("settleup_marketorder","tab_")
->field("tab_settleup_marketorder.id,pay_time,promote_account,company_belong,tab_settleup_marketorder.real_name,develop_type,game_name,
sum(pay_amount) pay_amount,sum(company_profit) company_profit,group_id,market_percentage,nickname,admin_id,
sum(if((develop_type = 1 or develop_type = 2), pay_amount, 0)) performance_revenue,sum(if(develop_type = 3, pay_amount, 0)) appraisal_bonuses,
sum(company_tax) company_tax, sum(cp_amount) cp_amount,sum(promote_amount) promote_amount")
->join("left join sys_auth_group_access access on admin_id=access.uid")
->join("left join sys_auth_group auth on access.group_id=auth.id")
->join("left join sys_member member on access.uid=member.uid")
->where(['group_id'=>['in','11,12,21']])
->where($map)
->group("admin_id,pay_time")
->select();
//获取admin_id
$accessData = M("auth_group_access")
->alias("access")
->join("left join sys_auth_group auth on access.group_id=auth.id")
->join("left join sys_member member on access.uid=member.uid")
->where(['group_id'=>['in','11,12,21']])
->getField("access.uid,group_id,market_percentage,nickname");
$all_data = SM("settleup_marketorder","tab_")
->field("id,admin_id,pay_time,promote_account,company_belong,tab_settleup_marketorder.real_name,develop_type,game_name,
sum(pay_amount) pay_amount,sum(company_profit) company_profit,
sum(if((develop_type = 1 or develop_type = 2), pay_amount, 0)) performance_revenue,sum(if(develop_type = 3, pay_amount, 0)) appraisal_bonuses,
sum(company_tax) company_tax, sum(cp_amount) cp_amount,sum(promote_amount) promote_amount")
->where($map)
->where(["admin_id"=>['in',array_keys($accessData)]])
->group("admin_id,pay_time")
->select();
foreach ($all_data as $key => $value) {
$admin_id = $value['admin_id'];
if(isset($accessData[$admin_id])){
$value = array_merge($value,$accessData[$admin_id]);
}
$sum_amount = $value['performance_revenue'] + $value['appraisal_bonuses'];
@ -1526,7 +1374,7 @@ class TimingController extends AdminController {
// echo "{$value['nickname']}:{$value['pay_time']}\n";
// dump($market_percentage);
$settleup_marketorder = M("settleup_marketorder","tab_")
$settleup_marketorder = SM("settleup_marketorder","tab_")
->where(['admin_id'=>$value['admin_id'],'pay_time'=>$value['pay_time'],'is_settlement'=>1])
->select();
@ -1606,14 +1454,14 @@ class TimingController extends AdminController {
$insert['performance_profit'] = number_format($all_data[$key]['performance_profit'], 2, '.', '');
// $insert['wait_commission'] = ($insert['wait_performance_commission']+$insert['wait_achievement_commission']);
$is_hav = M("market_altogether","tab_")
$is_hav = SM("market_altogether","tab_")
->where(['admin_id'=>$value['admin_id'],'pay_time'=>$value['pay_time']])
->find();
if (!$is_hav) {
M("market_altogether","tab_")->add($insert);
SM("market_altogether","tab_")->add($insert);
} else {
M("market_altogether","tab_")
SM("market_altogether","tab_")
->where(['admin_id'=>$value['admin_id'],'pay_time'=>$value['pay_time']])
->save($insert);
}
@ -1635,7 +1483,7 @@ class TimingController extends AdminController {
unset($map['pay_time']);
}
$data = M("market_altogether","tab_")
$data = SM("market_altogether","tab_")
->where($map)
->select();
// dump($map);die();
@ -1685,7 +1533,7 @@ class TimingController extends AdminController {
$map['pay_time'] = ['in',$where];
$map['admin_id'] = $value['admin_id'];
M("market_altogether","tab_")
SM("market_altogether","tab_")
->where($map)
->save($save);
}

Loading…
Cancel
Save