@ -755,6 +755,187 @@ class TimingController extends AdminController {
}
}
/**
* 获取下游公司的特殊补点金额
* @param int $start_time
* @param int $end_time
* @return array
*/
public function getSpecialComplement($start_time=0,$end_time =0) {
$data = M("company_statement","tab_")->where("statement_begin_time< ={$start_time} and statement_end_time< ={$end_time} and withdraw_type=3 and company_type=2")->select();
$return = [];
foreach ($data as $key => $value) {
$statement_info = json_decode($value['statement_info'],true);
foreach ($statement_info as $skey => $sval) {
$info = $sval['game_list'];
foreach ($info as $k => $v) {
//
if (isset($return[$statement_info['promote_id'].$v['relation_game_id']])) {
$return[$value['company_id']."-".$sval['promote_id']."-".$v['relation_game_id']] = $v['sum_money'];
} else {
$return[$value['company_id']."-".$sval['promote_id']."-".$v['relation_game_id']] += $v['sum_money'];
}
}
}
}
return $return;
}
// $admin_id = 0,$promote_id = 0,$company_id=0,$relation_game_id=0,$date ='',$pay_amount = 0
public function getCompanyGameRadio($admin_id = 0,$promote_id = 0,$company_id=0,$relation_game_id=0,$date ='',$pay_amount = 0) {
// $company_id=273;
// $relation_game_id=213;
// $pay_amount = 440;
// $date = "2020-07";
// $admin_id = 50;
// $promote_id = 3711;
$begintime = strtotime(date("Y-m-1",strtotime($date)));
$endtime = strtotime(date("Y-m-t",strtotime($date)))+86399;
$company_game_ratio = M("company_game_ratio","tab_")
->where(['company_id'=>$company_id,'relation_game_id'=>$relation_game_id])
->where("(end_time>={$begintime} and (end_time< ={$endtime} and end_time!=0)) or (begin_time>={$begintime} and begin_time< ={$endtime}) or (begin_time< ={$begintime} and (end_time>={$endtime} or end_time=0))")
->order("begin_time ASC")
->select();
$company_data = M('promote_company','tab_')
->field("company_belong")
->where("id=$company_id")
->find();
$game_ratio_mould = M("game_ratio_mould","tab_")
->where(['relation_game_id'=>$relation_game_id,'company_belong'=>$company_data['company_belong']])
->find();
$mould = json_decode($game_ratio_mould['turnover_ratio'],true);
$amount = 0;
if (!$company_game_ratio) {
$spend = $this->getSpendData($begintime,$endtime,$company_id,$relation_game_id,$admin_id,$promote_id);
$amount += $this->setSpendData($spend,$mould,$game_ratio_mould['ratio'],$pay_amount);
}
foreach ($company_game_ratio as $key => $value) {
$turnover_ratio = json_decode($value['turnover_ratio'],true);
if($value['begin_time'] > $begintime) {
if ($key == 0) {
$spend = $this->getSpendData($begintime,$value['begin_time']-1,$company_id,$relation_game_id,$admin_id,$promote_id);
$amount += $this->setSpendData($spend,$mould,$game_ratio_mould['ratio'],$pay_amount);
}
if ($value['end_time']< $endtime& & ($value['end_time']!=0)) {
$spend = $this->getSpendData($value['begin_time'],$value['end_time'],$company_id,$relation_game_id,$admin_id,$promote_id);
$amount += $this->setSpendData($spend,$turnover_ratio,$value['ratio'],$pay_amount);
} else {
$spend = $this->getSpendData($value['begin_time'],$endtime,$company_id,$relation_game_id,$admin_id,$promote_id);
$amount += $this->setSpendData($spend,$turnover_ratio,$value['ratio'],$pay_amount);
break;
}
} elseif ($value['begin_time']< =$begintime) {
$value['begin_time']=$begintime;
if ($value['end_time']< $endtime& & ($value['end_time']!=0)) {
$spend = $this->getSpendData($value['begin_time'],$value['end_time'],$company_id,$relation_game_id,$admin_id,$promote_id);
$amount += $this->setSpendData($spend,$turnover_ratio,$value['ratio'],$pay_amount);
} else {
$spend = $this->getSpendData($value['begin_time'],$endtime,$company_id,$relation_game_id,$admin_id,$promote_id);
$amount += $this->setSpendData($spend,$turnover_ratio,$value['ratio'],$pay_amount);
break;
}
}
//缺片段的情况下用模板比例补充
if ($company_game_ratio[$key+1]['begin_time'] != $value['end_time']+1 & & $company_game_ratio[$key+1]) {
$spend = $this->getSpendData($value['end_time']+1,$company_game_ratio[$key+1]['begin_time']-1,$company_id,$relation_game_id,$admin_id,$promote_id);
$amount += $this->setSpendData($spend,$mould,$game_ratio_mould['ratio'],$pay_amount);
}
//如果最后还有缺失的片段再用模板补齐
if(!$company_game_ratio[$key+1]& & $endtime>$value['end_time']) {
$spend = $this->getSpendData($value['end_time']+1,$endtime,$company_id,$relation_game_id,$admin_id,$promote_id);
$amount += $this->setSpendData($spend,$mould,$game_ratio_mould['ratio'],$pay_amount);
}
}
return $amount?$amount:0;
}
public function setSpendData($spend=[],$turnover_ratio=[],$radio=0,$pay_amount=0) {
$turnover = 0;
$amount= 0;
foreach ($spend as $k => $v) {
//获取的金额
$amount = ($v['pay_amount'] * ($radio?$radio:0))/100;
foreach ( $turnover_ratio as $tk => $tv ) {
//判断大于的流水是否比前一个要小
if ($turnover >= $tv['turnover']) {
continue;
}
if ($tv['instanceof']==1) {
if ($pay_amount >= $tv['turnover']) {
$amount = ($v['pay_amount'] * ($tv['ratio']?$tv['ratio']:0))/100;
$turnover = $tv['turnover'];
}
} else {
if ($pay_amount > $tv['turnover']) {
$amount = ($v['pay_amount'] * ($tv['ratio']?$tv['ratio']:0))/100;
$turnover = $tv['turnover'];
}
}
}
}
return $amount;
}
public function getSpendData($begintime = 0,$endtime = 0,$company_id=0,$relation_game_id=0,$admin_id=0,$promote_id=0) {
$map['tab_spend.pay_status'] = 1;
$map['pay_way'] = ['egt',0];
$spend = M("spend","tab_")
->field("sum(pay_amount) pay_amount,
CASE WHEN substring_index(substring_index(`chain`,'/',2),'/',-1) !='' THEN substring_index(substring_index(`chain`,'/',2),'/',-1) ELSE promote_id END promote_id,game.relation_game_id,company_id")
->join("left join tab_game game on tab_spend.game_id = game.id")
->join("left join tab_promote promote on tab_spend.promote_id = promote.id")
->where($map)
->where(['payed_time'=>['between',[$begintime,$endtime]],'company_id'=>$company_id,'relation_game_id'=>$relation_game_id,'market_admin_id'=>$admin_id])
->group("tab_spend.id")
->select(false);
$spend = M()->table("({$spend})a")
->field("*,sum(pay_amount) pay_amount")
->where(['promote_id'=>$promote_id])
->group("promote_id")
->select();
return $spend;
}
//计算市场专员业绩公式 参数 time 2020-10
public function caculateMarketStream () {
echo date("Y-m-d H:i:s")."----------------------市场结算管理计算----------------------\n";
@ -886,10 +1067,10 @@ class TimingController extends AdminController {
->join("left join ({$data}) auth on auth.uid=market_admin_id")
->where(['develop_type'=>['egt',1],'group_id'=>['in',['11','12','21']]])
->select();
// dump($spend);die();
//
// dump($tax_radio);die();
$specialPayAmount = [];
foreach ($promote_data as $key => $value) {
@ -900,12 +1081,26 @@ class TimingController extends AdminController {
//上游流水
$promote_data[$key]['cp_amount'] = $value['pay_amount'] * ($cp_radio * 0.01);
//下游
$promote_radio = getGamePromoteCompanyRadio($value['company_id'],$value['relation_game_id'],time(),$value['pay_amount'],true,$value['company_belong']);
if (!$promote_radio) {
$promote_radio = getGamePromoteCompanyRadio($value['company_id'],$value['relation_game_id'],time(),$value['pay_amount'],false,$value['company_belong']);
}
// $promote_radio = getGamePromoteCompanyRadio($value['company_id'],$value['relation_game_id'],time(),$value['pay_amount'],true,$value['company_belong']);
// if (!$promote_radio) {
// $promote_radio = getGamePromoteCompanyRadio($value['company_id'],$value['relation_game_id'],time(),$value['pay_amount'],false,$value['company_belong']);
// }
//下游流水
$promote_data[$key]['promote_amount'] = $value['pay_amount'] * ($promote_radio * 0.01);
$promote_data[$key]['promote_amount'] = $this->getCompanyGameRadio($value['admin_id'],$value['promote_id'],$value['company_id'],$value['relation_game_id'],$value['pay_time'],$value['pay_amount']);
//下游流水
// $promote_data[$key]['promote_amount'] = $value['pay_amount'] * ($promote_radio * 0.01);
//特殊补点
if (!$specialPayAmount[$value['pay_time']]) {
$specialPayAmount[$value['pay_time']] = $this->getSpecialComplement(strtotime(date("Y-m-1",strtotime($value['pay_time']))),strtotime(date("Y-m-t",strtotime($value['pay_time'])))+86399);
}
$specialAmount = $specialPayAmount[$value['pay_time']][$value['company_id']."-".$value['promote_id']."-".$value['relation_game_id']]?$specialPayAmount[$value['pay_time']][$value['promote_id']."-".$value['relation_game_id']]:0;
//渠道费用
$promote_data[$key]['channel_amount'] = $channel_fee[$value['pay_time'].$value['promote_id'].$value['game_name'].$value['admin_id']];
$tax = 0;
@ -919,7 +1114,7 @@ class TimingController extends AdminController {
$promote_data[$key]['company_tax'] = ($value['pay_amount']-$promote_data[$key]['cp_amount']- $promote_data[$key]['promote_amount'])*($tax/100);
//毛利
$promote_data[$key]['company_profit'] = $value['pay_amount']-$promote_data[$key]['cp_amount']-$promote_data[$key]['promote_amount']-$promote_data[$key]['channel_amount']-$promote_data[$key]['company_tax'];
$promote_data[$key]['company_profit'] = $value['pay_amount']-$promote_data[$key]['cp_amount']-$promote_data[$key]['promote_amount']-$promote_data[$key]['channel_amount']-$promote_data[$key]['company_tax']-$specialAmount ;
if ($value['promote_id'] == 0) {
// $promote_data[$key]['promote_account']='官方渠道';
@ -935,6 +1130,7 @@ class TimingController extends AdminController {
->where(['pay_time'=>$value['pay_time'],'promote_id'=>$value['promote_id'],'game_name'=>$value['game_name'],'admin_id'=>$value['admin_id']])
->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_")