优化下游公司多比例结算

master
chenzhi 4 years ago
parent 492d7a8f73
commit ff19f3259f

@ -562,7 +562,9 @@ class CompanyStatementSetController extends Controller {
} }
unset($tmpp); unset($tmpp);
unset($js_id); unset($js_id);
$this->getPromoteCompanySpend($pcList,array_unique(array_keys($pcList)),$begintime,$endtime); $this->getPromoteCompanySpend($pcList,array_unique(array_keys($pcList)),$begintime,$endtime);
//数据整合 //数据整合
$verify_log=json_encode(["create_user"=>"system","create_time"=>date("Y-m-d H:i:s")]); $verify_log=json_encode(["create_user"=>"system","create_time"=>date("Y-m-d H:i:s")]);
$StatementDb = M("CompanyStatement","tab_"); $StatementDb = M("CompanyStatement","tab_");
@ -614,43 +616,85 @@ class CompanyStatementSetController extends Controller {
$company_belong = $pc[$add_data['company_id']]['company_belong']; $company_belong = $pc[$add_data['company_id']]['company_belong'];
$add_data['company_belong']=$company_belong; $add_data['company_belong']=$company_belong;
$add_data['company_type']=$pc[$add_data['company_id']]['company_type']; $add_data['company_type']=$pc[$add_data['company_id']]['company_type'];
if(!isset($v['list'])){continue;}
//游戏统计
foreach($v['list'] as $ke=>$va){
$game =[];
$game['pay_amount'] =$va['pay_amount'];
$game['game_name'] =$va['game_name'];
//获取比例
$game['relation_game_id']=$va['relation_game_id'];
if($type == 1){
$tratio = getGamePromoteCompanyRadio($add_data['company_id'],$va['relation_game_id'],$endtime,$va['pay_amount'],true,$company_belong);
$tratio += $company_ratio;
}elseif($type == 0){
$tratio = getGamePromoteCompanyRadio($add_data['company_id'],$va['relation_game_id'],$endtime,$va['pay_amount'],false,$company_belong);
}else{
//补点
$tratio1 = getGamePromoteCompanyRadio($add_data['company_id'],$va['relation_game_id'],$endtime,$va['pay_amount'],true,$company_belong);;
$tratio2 = getGamePromoteCompanyRadio($add_data['company_id'],$va['relation_game_id'],$endtime,$va['pay_amount'],false,$company_belong);
$tratio = $tratio1-$tratio2+$company_ratio;
if($tratio <= 0){ continue;}
}
$tratio = $tratio-0;
if($v['type'] == 2){
$game['increment_ratio']=$tratio;
}else{
$game['ratio']=$tratio;
}
$add_data['pay_amount'] += $va['pay_amount'];
$game['sum_money']=round($va['pay_amount']*$tratio*(100-$fax_ratio)/(100*100),2);//结算金额=平台总额*(分成比例+补点比例)*(1-税费费率) if(!isset($v['game_list'])){continue;}
$add_data['statement_money'] += $game['sum_money']; //获取多段比例
$game_ratio_list = D("CompanyGameRatio")->getPromoteCompanyGameRatio($add_data['company_id'],$v['game_ids'],$begintime,$endtime,$company_belong);
foreach ($game_ratio_list as $t_game_id => $t_game_ratio) {
$game =[];
$game['game_name'] =$v['game_list'][$t_game_id]['game_name'];
$game['relation_game_id'] =$v['game_list'][$t_game_id]['relation_game_id'];
$game_all_payment = $v['game_list'][$t_game_id]['pay_amount'];
$game['fax_ratio']=$fax_ratio; // dump($tratio);
$game['fax_ratio']=$fax_ratio-0;
$game['statement_begin_time']=$statement_begin_time;
$game['statement_end_time']=$statement_end_time;
$game['statement_type']=0; $game['statement_type']=0;
$add_data['statement_info'][] = $game;
if(count($t_game_ratio) == 1){
$tmp_game_ratio = $t_game_ratio[0];
$game['pay_amount'] =$game_all_payment;
$game['pay_amount'] -=0;
$game['statement_begin_time']=$statement_begin_time;
$game['statement_end_time']=$statement_end_time;
$add_data['pay_amount'] += $game['pay_amount'];
if($type == 1){
$tratio =$this->getTurnoverRatio($game_all_payment,$tmp_game_ratio['ratio'],$tmp_game_ratio['turnover_ratio']);
$tratio += $company_ratio;
}elseif($type == 0){
$tratio = $t_game_ratio['ratio'];
}else{
//补点
$tratio1 =$this->getTurnoverRatio($game_all_payment,$tmp_game_ratio['ratio'],$tmp_game_ratio['turnover_ratio']);
$tratio2 =$tmp_game_ratio['ratio'];
$tratio = $tratio1-$tratio2+$company_ratio;
if($tratio <= 0){ continue;}
}
if($v['type'] == 2){
$game['increment_ratio']=$tratio;
}else{
$game['ratio']=$tratio;
}
$game['sum_money']=round($game['pay_amount']*$tratio*(100-$fax_ratio)/(100*100),2);//结算金额=平台总额*(分成比例+补点比例)*(1-税费费率)
$add_data['statement_money'] += $game['sum_money'];
$add_data['statement_info'][] = $game;
}else{
// dd($t_game_ratio);
for ($i=0; $i < count($t_game_ratio); $i++) {
$tmp_game_ratio = $t_game_ratio[$i];
$temp_game = $game;
$temp_game['statement_begin_time']=date("Y.m.d",$tmp_game_ratio['begintime']);
$temp_game['statement_end_time']=date("Y.m.d",$tmp_game_ratio['endtime']);
// dd($t_game_id);
$temp_game['pay_amount'] =$this->getRelationGameIdSpend($add_data['company_id'],$t_game_id,$tmp_game_ratio['begintime'],$tmp_game_ratio['endtime']);
$temp_game['pay_amount'] -=0;
$add_data['pay_amount'] += $temp_game['pay_amount'];
if($type == 1){
$tratio =$this->getTurnoverRatio($game_all_payment,$tmp_game_ratio['ratio'],$tmp_game_ratio['turnover_ratio']);
$tratio += $company_ratio;
}elseif($type == 0){
$tratio = $t_game_ratio['ratio'];
}else{
//补点
$tratio1 =$this->getTurnoverRatio($game_all_payment,$tmp_game_ratio['ratio'],$tmp_game_ratio['turnover_ratio']);
$tratio2 =$tmp_game_ratio['ratio'];
$tratio = $tratio1-$tratio2+$company_ratio;
if($tratio <= 0){ continue;}
}
if($v['type'] == 2){
$temp_game['increment_ratio']=$tratio;
}else{
$temp_game['ratio']=$tratio;
}
$temp_game['sum_money']=round($temp_game['pay_amount']*$tratio*(100-$fax_ratio)/(100*100),2);//结算金额=平台总额*(分成比例+补点比例)*(1-税费费率)
$add_data['statement_money'] += $temp_game['sum_money'];
$add_data['statement_info'][] = $temp_game;
}
}
} }
$add_data['platform_amount'] = $add_data['pay_amount']; $add_data['platform_amount'] = $add_data['pay_amount'];
if($type < 2){ if($type < 2){
@ -789,9 +833,10 @@ class CompanyStatementSetController extends Controller {
foreach($res as $k=>$v){ foreach($res as $k=>$v){
$pcList[$k]["pay_amount"] = $v['pay_amount']; $pcList[$k]["pay_amount"] = $v['pay_amount'];
unset($v['pay_amount']); unset($v['pay_amount']);
$pcList[$k]['list'] = $v; $pcList[$k]['game_list'] = $v;
$pcList[$k]['game_ids'] = implode(",",array_keys($pcList[$k]['game_list']));
} }
} }
//获取推广公司推广金额 //获取推广公司推广金额
protected function getPuPromoteSpend(&$pcList,$idarr,$begintime,$endtime){ protected function getPuPromoteSpend(&$pcList,$idarr,$begintime,$endtime){
@ -980,7 +1025,6 @@ class CompanyStatementSetController extends Controller {
$pc[$v['id']]['is_payment']=$pcList[$v['id']]['is_payment']; $pc[$v['id']]['is_payment']=$pcList[$v['id']]['is_payment'];
} }
unset($tmpp); unset($tmpp);
// $this->getPromoteCompanySpend($pc,array_unique(array_keys($pc)),$begintime,$endtime);
$this->getPuPromoteSpend($pc,array_unique(array_keys($pc)),$begintime,$endtime); $this->getPuPromoteSpend($pc,array_unique(array_keys($pc)),$begintime,$endtime);
@ -1286,102 +1330,58 @@ class CompanyStatementSetController extends Controller {
} }
} }
/** /**
* 按公司获取游戏分成比例 * 获取比例
* *
* @param [type] $company_id 公司id * @param [type] $amount 总额
* @param [type] $game_ids 游戏关联id * @param [type] $ratio 默认比例
* @param [type] $begintime 开始时间 * @param [type] $turnover_ratio 比例梯度
* @param [type] $endtime 结束时间
* @param boolean $company_belong 公司内外团
* @return void * @return void
*/ */
protected function getPromoteCompanyGameRatio($company_id,$game_ids,$begintime,$endtime,$company_belong=false){ protected function getTurnoverRatio($amount,$ratio,$turnover_ratio)
//获取默认比例 {
if($company_belong === false){ if(empty($turnover_ratio)){
$company_belong = M("PromoteCompany","tab_")->field("company_belong")->where("id='{$company_id}'")->find()['company_belong']; return $ratio;
}
$mwhere = [
"company_belong"=>$company_belong,
"relation_game_id"=>["in",$game_ids]
];
$m_res = M("GameRatioMould","tab_")->where($mwhere)->select();
$modul_ratio = [];
for ($i=0; $i < count($m_res); $i++) {
$modul_ratio[$m_res[$i]['relation_game_id']] = $m_res[$i];
} }
unset($m_res); if(!is_array($turnover_ratio)){
//设置默认 $turnover_ratio = json_decode($turnover_ratio,true);
$game_ratio = [];
$tmp_g = explode(",",$game_ids);
for ($i=0; $i < count($tmp_g); $i++) {
$game_ratio[$tmp_g[$i]] = [
["begintime"=>$begintime,"endtime"=>$endtime]
];
} }
$map = [ foreach($turnover_ratio as $k=>$v){
"company_id"=>$company_id, if($v['instanceof'] == 1){
"relation_game_id"=>['in',$game_ids], if($amount >= $v['turnover']){
"_string"=>"begin_time <={$endtime} AND ( end_time = 0 OR end_time >= {$begintime})" $ratio = $v['ratio'];
]; }
$res = M("company_game_ratio","tab_")->where($map)->order("begin_time asc")->select(); }else{
foreach($res as $k=>$v){ if($amount > $v['turnover']){
//获取最后一个数据 $ratio = $v['ratio'];
$tgr = &$game_ratio[$v['relation_game_id']];
$last_time_ratio = end($game_ratio[$v['relation_game_id']]);
if($v['end_time'] == 0 || ($v['end_time'] >= $last_time_ratio['endtime'])){
//全段
if($v['begin_time'] <= $last_time_ratio['begintime']){
$tgr[count($tgr)-1]['ratio'] = $v['ratio'];
$tgr[count($tgr)-1]['turnover_ratio'] = $v['turnover_ratio'];
continue;
}else{
//上分段
$tgr[count($tgr)-1]['endtime'] = $v['begin_time']-1;
$tgr[] = ['begintime'=>$v['begin_time'],"endtime"=>$last_time_ratio['endtime'],"ratio"=>$v['ratio'],"turnover_ratio"=>$v['turnover_ratio']];
continue;
} }
} }
if($v['end_time'] < $last_time_ratio['endtime']){ }
if($last_time_ratio['begintime'] < $v['begin_time']){ return $ratio;
//中段 }
$tgr[count($tgr)-1]['endtime'] = $v['begin_time']-1;
$tgr[] = ['begintime'=>$v['begin_time'],"endtime"=>$v['end_time'],"ratio"=>$v['ratio'],"turnover_ratio"=>$v['turnover_ratio']]; protected function getRelationGameIdSpend($company_id,$relation_game_id,$begintime,$endtime)
{
$res = [];
//获取推广员
$Promote = M("Promote","tab_");
$Spend = M("Spend","tab_");
$spenMap = [
"s.pay_status"=>1,
"s.payed_time"=>['between', [$begintime,$endtime]],
];
$tgr[] = ['begintime'=>$v['end_time']-0+1,"endtime"=>$last_time_ratio['endtime']]; $company_id = $company_id;
continue; $pres = $Promote->field("group_concat(id) ids,count(id) count")->where("company_id = '{$company_id}'")->group("company_id")->find();
} if($pres['count'] ==0 ){return 0;}
if($last_time_ratio['begintime'] > $v['begin_time']){ $spenMap['s.promote_id']=["in",$pres['ids']];
//下分段 //游戏
$tgr[count($tgr)-1]['endtime'] = $v['end_time'];
$tgr[] = ['begintime'=>$v['end_time']-0+1,"endtime"=>$last_time_ratio['endtime'],"ratio"=>$v['ratio'],"turnover_ratio"=>$v['turnover_ratio']];
continue;
}
if($last_time_ratio['begintime'] = $v['begin_time']){
$tgr[count($tgr)-1]['endtime'] = $v['end_time'];
$tgr[count($tgr)-1]['ratio'] = $v['ratio'];
$tgr[count($tgr)-1]['turnover_ratio'] = $v['turnover_ratio'];
$tgr[] = ['begintime'=>$v['end_time']-0+1,"endtime"=>$last_time_ratio['endtime']]; $game_id = D("Game")->changeRelationGameidToGameid($relation_game_id);
} // dd($game_id);
} $spenMap['s.game_id'] =['in',$game_id];
} //获取支付记录
//模板填充 $pay_amount = $Spend->alias('s')->field('sum(s.pay_amount) pay_amount')->where($spenMap)->find();
foreach($game_ratio as $k=>&$list){ return $pay_amount['pay_amount'] ?? 0;
foreach($list as $i => &$ratio){
if(!isset($ratio['ratio'])){
if(isset($modul_ratio[$k]['ratio'])){
$ratio['ratio'] = $modul_ratio[$k]['ratio'];
$ratio['turnover_ratio'] = $modul_ratio[$k]['turnover_ratio'];
}else{
$ratio['ratio'] = 0;
$ratio['turnover_ratio'] =null;
}
}
}
}
return $game_ratio;
} }
/** /**

@ -0,0 +1,109 @@
<?php
namespace Admin\Model;
use Think\Model;
class CompanyGameRatioModel extends Model
{
// 数据表前缀
protected $tablePrefix = 'tab_';
/**
* 按公司获取游戏分成比例
*
* @param [type] $company_id 公司id
* @param [type] $game_ids 游戏关联id
* @param [type] $begintime 开始时间
* @param [type] $endtime 结束时间
* @param boolean $company_belong 公司内外团
* @return void
*/
public function getPromoteCompanyGameRatio($company_id,$game_ids,$begintime,$endtime,$company_belong=false){
//获取默认比例
if($company_belong === false){
$company_belong = M("PromoteCompany","tab_")->field("company_belong")->where("id='{$company_id}'")->find()['company_belong'];
}
$mwhere = [
"company_belong"=>$company_belong,
"relation_game_id"=>["in",$game_ids]
];
$m_res = M("GameRatioMould","tab_")->where($mwhere)->select();
$modul_ratio = [];
for ($i=0; $i < count($m_res); $i++) {
$modul_ratio[$m_res[$i]['relation_game_id']] = $m_res[$i];
}
unset($m_res);
//设置默认
$game_ratio = [];
$tmp_g = explode(",",$game_ids);
for ($i=0; $i < count($tmp_g); $i++) {
$game_ratio[$tmp_g[$i]] = [
["begintime"=>$begintime,"endtime"=>$endtime]
];
}
$map = [
"company_id"=>$company_id,
"relation_game_id"=>['in',$game_ids],
"_string"=>"begin_time <={$endtime} AND ( end_time = 0 OR end_time >= {$begintime})"
];
$res = $this->where($map)->order("begin_time asc")->select();
foreach($res as $k=>$v){
//获取最后一个数据
$tgr = &$game_ratio[$v['relation_game_id']];
$last_time_ratio = end($game_ratio[$v['relation_game_id']]);
if($v['end_time'] == 0 || ($v['end_time'] >= $last_time_ratio['endtime'])){
//全段
if($v['begin_time'] <= $last_time_ratio['begintime']){
$tgr[count($tgr)-1]['ratio'] = $v['ratio'];
$tgr[count($tgr)-1]['turnover_ratio'] = $v['turnover_ratio'];
continue;
}else{
//上分段
$tgr[count($tgr)-1]['endtime'] = $v['begin_time']-1;
$tgr[] = ['begintime'=>$v['begin_time'],"endtime"=>$last_time_ratio['endtime'],"ratio"=>$v['ratio'],"turnover_ratio"=>$v['turnover_ratio']];
continue;
}
}
if($v['end_time'] < $last_time_ratio['endtime']){
if($last_time_ratio['begintime'] < $v['begin_time']){
//中段
$tgr[count($tgr)-1]['endtime'] = $v['begin_time']-1;
$tgr[] = ['begintime'=>$v['begin_time'],"endtime"=>$v['end_time'],"ratio"=>$v['ratio'],"turnover_ratio"=>$v['turnover_ratio']];
$tgr[] = ['begintime'=>$v['end_time']-0+1,"endtime"=>$last_time_ratio['endtime']];
continue;
}
if($last_time_ratio['begintime'] > $v['begin_time']){
//下分段
$tgr[count($tgr)-1]['endtime'] = $v['end_time'];
$tgr[] = ['begintime'=>$v['end_time']-0+1,"endtime"=>$last_time_ratio['endtime'],"ratio"=>$v['ratio'],"turnover_ratio"=>$v['turnover_ratio']];
continue;
}
if($last_time_ratio['begintime'] = $v['begin_time']){
$tgr[count($tgr)-1]['endtime'] = $v['end_time'];
$tgr[count($tgr)-1]['ratio'] = $v['ratio'];
$tgr[count($tgr)-1]['turnover_ratio'] = $v['turnover_ratio'];
$tgr[] = ['begintime'=>$v['end_time']-0+1,"endtime"=>$last_time_ratio['endtime']];
}
}
}
//模板填充
foreach($game_ratio as $k=>&$list){
foreach($list as $i => &$ratio){
if(!isset($ratio['ratio'])){
if(isset($modul_ratio[$k]['ratio'])){
$ratio['ratio'] = $modul_ratio[$k]['ratio'];
$ratio['turnover_ratio'] = $modul_ratio[$k]['turnover_ratio'];
}else{
$ratio['ratio'] = 0;
$ratio['turnover_ratio'] =null;
}
}
}
}
return $game_ratio;
}
}

@ -462,7 +462,7 @@ class GameModel extends Model{
*/ */
public function changeRelationGameidToGameid($ids,$type=false) public function changeRelationGameidToGameid($ids,$type=false)
{ {
$rid = $this->field('id')->where(["relation_game_id"=>["in",$ids]])->select(); $rid = $this->field('id')->where("relation_game_id in ({$ids})")->select();
if($type){ if($type){
return array_column($rid,'id'); return array_column($rid,'id');
}else{ }else{
@ -479,7 +479,7 @@ class GameModel extends Model{
*/ */
public function changeGameidToRelationGameid($ids,$type=false) public function changeGameidToRelationGameid($ids,$type=false)
{ {
$rid = $this->field('relation_game_id')->where(["id"=>["in",$ids]])->group("relation_game_id")->select(); $rid = $this->field('relation_game_id')->where("id in ({$ids})")->group("relation_game_id")->select();
if($type){ if($type){
return array_column($rid,'relation_game_id'); return array_column($rid,'relation_game_id');
}else{ }else{

Loading…
Cancel
Save