生成上下游结算单

master
chenzhi 5 years ago
parent 380243dbb8
commit 6162f9f8a1

@ -2115,6 +2115,59 @@ function getGameCpRadio($game_id,$amount,$type) {
return $data['ratio']?$data['ratio']:0; return $data['ratio']?$data['ratio']:0;
} }
/**
* 获取渠道分成比例
* @author chenzhi
* @param [type] $company_id 公司id
* @param [type] $relation_game_id 游戏广联id
* @param [type] $endtime 结算结束的时间戳
* @param [type] $amount 金额
* @param [type] $type $type=false时返回最低分成比例
* @param [type] $company_belong 公司团体类型不传的话用id查询
* @return ratio
*/
function getGamePromoteCompanyRadio($company_id,$relation_game_id,$endtime,$amount,$type,$company_belong=false) {
//判断是否有生效
$rwhere = "(end_time = 0 OR end_time >= '{$endtime}') and company_id='{$company_id}' and relation_game_id='{$relation_game_id}'";
$cgr_res = M("CompanyGameRatio","tab_")->where($rwhere)->find();
if(empty($cgr_res)){
//模板搜索
//获取公司类型
if($company_belong === false){
$company_belong = M("PromoteCompany","tab_")->field("company_belong")->where("id='{$company_id}'")->find()['company_belong'];
}
$mwhere = "company_belong='{$company_belong}' and relation_game_id='{$relation_game_id}'";
$m_res = M("GameRatioMould","tab_")->where($mwhere)->find();
if(empty($m_res)){
$default_r = 0;
$turnover_ratio = false;
}else{
$default_r = $m_res['ratio'];
$turnover_ratio = json_decode($m_res['turnover_ratio'],true);
}
}else{
$default_r = $cgr_res['ratio'];
$turnover_ratio = json_decode($cgr_res['turnover_ratio'],true);
}
//执行比例
if (!$type || !$turnover_ratio) {
return $default_r;
}
//获取比例
$ratio = $default_r;
foreach($turnover_ratio as $k=>$v){
if($v['instanceof'] == 1){
if($amount >= $v['turnover']){
$ratio = $v['ratio'];
}
}else{
if($amount > $v['turnover']){
$ratio = $v['ratio'];
}
}
}
return $ratio;
}
function check_str($str, $substr) function check_str($str, $substr)
{ {

@ -63,17 +63,24 @@ class CompanyStatementSetController extends Controller {
if($w == 1){ if($w == 1){
echo $nowdate.":".PHP_EOL; echo $nowdate.":".PHP_EOL;
echo "--周结begin".PHP_EOL; echo "--周结begin".PHP_EOL;
// $this->cpStatement(0,$stime);
$this->promoteCompanyStatement(0,$stime);
$this->cpStatement(0,$stime);
echo "----CP周结统计ok".PHP_EOL;
$this->promoteCompanyStatement(0,$stime);
echo "----推广公司周结统计ok".PHP_EOL;
} }
if($d == 1){ if($d == 1){
echo $nowdate.":".PHP_EOL; echo $nowdate.":".PHP_EOL;
echo "--月结&补点begin".PHP_EOL; echo "--月结&补点begin".PHP_EOL;
// $this->cpStatement(1,$stime);
// $this->cpStatement(2,$stime); $this->cpStatement(1,$stime);
$this->cpStatement(2,$stime);
echo "----CP月结&补点统计ok".PHP_EOL;
$this->promoteCompanyStatement(1,$stime); $this->promoteCompanyStatement(1,$stime);
$this->promoteCompanyStatement(2,$stime);
echo "----推广公司月结&补点统计ok".PHP_EOL;
} }
}else{ }else{
echo $nowdate."非周一和月初,无需任何处理".PHP_EOL; echo $nowdate."非周一和月初,无需任何处理".PHP_EOL;
@ -86,7 +93,7 @@ class CompanyStatementSetController extends Controller {
*/ */
public function cpStatement($type,$stime) public function cpStatement($type,$stime)
{ {
$t = setBeginAndEndTime($type); $t = $this->setBeginAndEndTime($type,$stime);;
$begintime = $t[0]; $begintime = $t[0];
$endtime = $t[1]; $endtime = $t[1];
@ -110,6 +117,8 @@ class CompanyStatementSetController extends Controller {
} }
} }
if(count($js_id) ==0 || count($cpList) == 0){return ;}
$cp_id = implode(",",array_flip(array_flip(array_keys($cpList)))); $cp_id = implode(",",array_flip(array_flip(array_keys($cpList))));
$js_id = implode(",",array_unique($js_id)); $js_id = implode(",",array_unique($js_id));
//获取cp公司 //获取cp公司
@ -241,7 +250,7 @@ class CompanyStatementSetController extends Controller {
} }
$add_data['pay_amount'] += $va['pay_money']; $add_data['pay_amount'] += $va['pay_money'];
$game['sum_money']=round($va['pay_money']*(100-$promote_ratio)*$tratio*(100-$fax_ratio)/100*100*100,2);//结算金额=平台总额*1-渠道费)*分成比例*(1-税费费率) $game['sum_money']=round($va['pay_money']*(100-$promote_ratio)*$tratio*(100-$fax_ratio)/(100*100*100),2);//结算金额=平台总额*1-渠道费)*分成比例*(1-税费费率)
$add_data['statement_money'] += $game['sum_money']; $add_data['statement_money'] += $game['sum_money'];
@ -360,7 +369,7 @@ class CompanyStatementSetController extends Controller {
public function promoteCompanyStatement($type,$stime) public function promoteCompanyStatement($type,$stime)
{ {
$t = setBeginAndEndTime($type); $t = $this->setBeginAndEndTime($type,$stime);
$begintime = $t[0]; $begintime = $t[0];
$endtime = $t[1]; $endtime = $t[1];
//获取哪些要结算 //获取哪些要结算
@ -388,8 +397,8 @@ class CompanyStatementSetController extends Controller {
//无需处理 //无需处理
return ; return ;
} }
//获取cp公司 //获取pc公司
$tmpp = M("PromoteCompany","tab_")->field("id,company_name partner,settlement_contact link_man,contact_phone link_phone,address,bank_address opening_bank,bank_card bank_account")->where(["id"=>['in',$pc_id]])->select(); $tmpp = M("PromoteCompany","tab_")->field("id,company_name partner,settlement_contact link_man,contact_phone link_phone,address,bank_address opening_bank,bank_card bank_account,fax_ratio,company_belong")->where(["id"=>['in',$pc_id]])->select();
$pc =[]; $pc =[];
foreach ($tmpp as $v) { foreach ($tmpp as $v) {
$pc[$v['id']]=$v; $pc[$v['id']]=$v;
@ -403,13 +412,135 @@ class CompanyStatementSetController extends Controller {
} }
unset($tmpp); unset($tmpp);
unset($js_id); unset($js_id);
dump($pc); $this->getPromoteCompanySpend($pcList,array_unique(array_keys($pcList)),$begintime,$endtime);
dd($our); //数据整合
$verify_log=json_encode(["create_user"=>$_SESSION['onethink_admin']['user_auth']["username"],"create_time"=>date("Y-m-d H:i:s")]);
$StatementDb = M("CompanyStatement","tab_");
$statement_begin_time = date("Y-m-d",$begintime);
$statement_end_time = date("Y-m-d",$endtime);
foreach($pcList as $k=>$v){
$add_data=[];
//1.获取甲乙方信息
if($v['first_company_type'] == 1){
//甲方上游
$add_data['first_party_info'] = json_encode($pc[$v['first_company_id']],JSON_UNESCAPED_UNICODE);
$add_data['second_party_info'] = json_encode($our[$v['second_company_id']],JSON_UNESCAPED_UNICODE);
$add_data['company_id'] = $v['first_company_id'];
$add_data['company_name'] = $v['first_company_name'];
}else{
//乙方上游
$add_data['first_party_info'] = json_encode($our[$v['first_company_id']],JSON_UNESCAPED_UNICODE);
$add_data['second_party_info'] = json_encode($pc[$v['second_company_id']],JSON_UNESCAPED_UNICODE);
$add_data['company_id'] = $v['second_company_id'];
$add_data['company_name'] = $v['second_company_name'];
}
$add_data['pay_type'] = $v['collection'];
$add_data['withdraw_type'] = $type;
$add_data['statement_begin_time'] = $begintime;
$add_data['statement_end_time'] = $endtime;
$add_data['statement_money'] = 0;
$add_data['pay_amount'] = 0;
$add_data['verify_log'] = $verify_log;
$add_data['op_time'] =time();
$add_data['statement_info'] =[];
//获取渠道及发票税率
$fax_ratio = $pc[$add_data['company_id']]['fax_ratio'] ?? 0;
$company_belong = $pc[$add_data['company_id']]['company_belong'];
//游戏统计
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);
}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;
}
if($v['first_company_type'] == 1){
$game['first_ratio']=$tratio;
$game['second_ratio']=100-$tratio;
}else{
$game['first_ratio']=100-$tratio;
$game['second_ratio']=$tratio;
}
$add_data['pay_amount'] += $va['pay_amount'];
$game['sum_money']=round($va['pay_amount']*$tratio*(100-$fax_ratio)/(100*100),2);//结算金额=平台总额*1-渠道费)*分成比例*(1-税费费率)
$add_data['statement_money'] += $game['sum_money'];
$game['fax_ratio']=$fax_ratio;
$game['statement_begin_time']=$statement_begin_time;
$game['statement_end_time']=$statement_end_time;
$game['statement_type']=0;
$add_data['statement_info'][] = $game;
}
$add_data['platform_amount'] = $add_data['pay_amount'];
if($type < 2){
//非补点奖罚
$rrmap = array(
"reward_time" => ['between', [$begintime,$endtime]],
"company_type"=>2,
"company_id"=>$add_data['company_id']
);
$rfres = M("RewardRecord","tab_")
->field("
IFNULL(SUM(CASE WHEN reward_type = 1 THEN money ELSE 0 END),0) as reward_count,
IFNULL(SUM(CASE WHEN reward_type = 2 THEN money ELSE 0 END),0) as fine_count
")
->where($rrmap)->find();
if($rfres['reward_count'] > 0){
$reward_count = $rfres['reward_count'];
$add_data['statement_info'][] = array(
"statement_begin_time"=>$statement_begin_time,
"statement_end_time"=>$statement_end_time,
'statement_type'=>2,
"game_name"=>"奖励",
'pay_amount'=>$reward_count,
'sum_money'=>$reward_count,
);
$add_data['statement_money']+=$reward_count;
$add_data['pay_amount']+=$reward_count;
}
if($rfres['fine_count'] > 0){
$fine_count = $rfres['fine_count'];
$add_data['statement_info'][] = array(
"statement_begin_time"=>$statement_begin_time,
"statement_end_time"=>$statement_end_time,
'statement_type'=>1,
"game_name"=>"罚款",
'pay_amount'=>$fine_count,
'sum_money'=>$fine_count,
);
$add_data['statement_money'] -= $fine_count;
$add_data['pay_amount'] -= $fine_count;;
}
}
$add_data['statement_info'] = json_encode($add_data['statement_info'],JSON_UNESCAPED_UNICODE);
if($type == 2 && $add_data['statement_money'] == 0){
//金额为0不补点
continue;
}
//添加
$StatementDb->add($add_data);
}
} }
//设定开始结束时间 //设定开始结束时间
protected function setBeginAndEndTime($type){ protected function setBeginAndEndTime($type,$stime){
if($type == 0){ if($type == 0){
$begintime = mktime(0,0,0,date('m',$stime),date('d',$stime)-7,date('Y',$stime)); $begintime = mktime(0,0,0,date('m',$stime),date('d',$stime)-7,date('Y',$stime));
$endtime = mktime(0,0,0,date('m',$stime),date('d',$stime),date('Y',$stime))-1; $endtime = mktime(0,0,0,date('m',$stime),date('d',$stime),date('Y',$stime))-1;
@ -430,6 +561,39 @@ class CompanyStatementSetController extends Controller {
} }
return [$begintime,$endtime]; return [$begintime,$endtime];
} }
//获取推广公司推广金额
protected function getPromoteCompanySpend(&$pcList,$idarr,$begintime,$endtime){
$res = [];
//获取推广员
if(count($idarr) == 0){return $res;}
$Promote = M("Promote","tab_");
$Spend = M("Spend","tab_");
$spenMap = [
"s.pay_status"=>1,
"s.pay_time"=>['between', [$begintime,$endtime]],
];
for ($i=0; $i <count($idarr); $i++) {
$company_id = $idarr[$i];
$pres = $Promote->field("group_concat(id) ids,count(id) count")->where("company_id = '{$company_id}'")->group("company_id")->find();
if($pres['count'] ==0 ){continue;}
$spenMap['s.promote_id']=["in",$pres['ids']];
//获取支付记录
$list = $Spend->alias('s')->field('sum(s.pay_amount) pay_amount,s.game_id,g.relation_game_id,g.relation_game_name game_name')->where($spenMap)->group('game_id')
->join("tab_game g on s.game_id = g.id")
->select();
if(empty($list)){continue;}
foreach($list as $k=>$v){
if(isset($res[$company_id][$v['relation_game_id']])){
//
$res[$company_id][$v['relation_game_id']]['pay_amount'] += $v['pay_amount'];
}else{
unset($v['game_id']);
$res[$company_id][$v['relation_game_id']]= $v;
}
}
}
foreach($res as $k=>$v){
$pcList[$k]['list'] = $v;
}
}
} }

Loading…
Cancel
Save