@ -6,6 +6,7 @@ use Think\Controller;
/**
* 聚合渠道结算接口对接
* @author cz
* TODO: 罚款的游戏没有流水不统计, 补点为0 无法重算
*/
class CompanyStatementSetController extends Controller {
public $cpapihost;
@ -91,7 +92,7 @@ class CompanyStatementSetController extends Controller {
echo "∟----推广公司月结&补点统计ok".PHP_EOL;
$this->promoteUserPool(1,$stime);
// $this->promoteUserPool(2,$stime);
$this->promoteUserPool(2,$stime);
echo "∟----推广个人月结&补点统计ok".PHP_EOL;
}
}else{
@ -143,7 +144,7 @@ class CompanyStatementSetController extends Controller {
$cp_id = implode(",",array_flip(array_flip(array_keys($cpList))));
$js_id = implode(",",array_unique($js_id));
//获取cp公司
$tmpp = M("Partner","tab_")->field("id,partner,link_man,link_phone,address,company_tax_no,payee_name,bank_account,opening_bank,channel_rate,invoice_rate,company_type,ali_user,ali_account")->where(["id"=>['in',$cp_id]])->select();
$tmpp = M("Partner","tab_")->field("id,partner,link_man,link_phone,address,company_tax_no,payee_name,bank_account,opening_bank,channel_rate,invoice_rate,taxation_rate, company_type,ali_user,ali_account,has_advance_charge,has_server_fee,server_fee ")->where(["id"=>['in',$cp_id]])->select();
$cp =[];
foreach ($tmpp as $v) {
$cp[$v['id']]=$v;
@ -242,9 +243,9 @@ class CompanyStatementSetController extends Controller {
$add_data['company_belong']=9;//上游
$add_data['company_type']=$cp[$add_data['company_id']]['company_type'];
$add_data['is_payment']=$v['is_payment'];
//获取渠道及发票 税率
//获取渠道及税费费 率
$promote_ratio = $cp[$add_data['company_id']]['channel_rate'] ?? 0;
$fax_ratio = $cp[$add_data['company_id']]['invoice _rate'] ?? 0;
$fax_ratio = $cp[$add_data['company_id']]['taxat io n_rate'] ?? 0;
//游戏统计
if(!isset($v['list'])){continue;}
foreach($v['list'] as $ke=>$va){
@ -298,14 +299,15 @@ class CompanyStatementSetController extends Controller {
if($type < 2 ) {
//非补点奖罚
$rrmap = array(
"reward _time" => ['between', [$begintime,$endtime]],
"confirm _time" => ['between', [$begintime,$endtime]],
"company_type"=>1,
"company_id"=>$add_data['company_id']
"company_id"=>$add_data['company_id'],
"settlement_type"=>2,
);
$rfres = M("RewardRecord ","tab_")
$rfres = M("RewardDetail ","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
IFNULL(SUM(CASE WHEN type = 1 THEN amount ELSE 0 END),0) as reward_count,
IFNULL(SUM(CASE WHEN type = 2 THEN amount ELSE 0 END),0) as fine_count
")
->where($rrmap)->find();
if($rfres['reward_count'] > 0){
@ -333,7 +335,22 @@ class CompanyStatementSetController extends Controller {
);
$add_data['statement_money'] -= $fine_count;
$add_data['pay_amount'] -= $fine_count;;
}
}
//获取服务器费用
$tmp_company_info= $cp[$add_data['company_id']];
if($tmp_company_info['has_server_fee'] == 1 & & $tmp_company_info['server_fee'] >0){
$server_count = $tmp_company_info['server_fee'];
$add_data['statement_info'][] = array(
"statement_begin_time"=>$statement_begin_time,
"statement_end_time"=>$statement_end_time,
'statement_type'=>1,
"game_name"=>"服务器费用",
'pay_amount'=>$server_count,
'sum_money'=>$server_count,
);
$add_data['statement_money'] -= $server_count;
$add_data['pay_amount'] -= $server_count;;
}
}
$add_data['statement_info'] = json_encode($add_data['statement_info'],JSON_UNESCAPED_UNICODE);
@ -361,7 +378,7 @@ class CompanyStatementSetController extends Controller {
{
$game_where = "partner_id in ({$cp_id})";
$game = M('game','tab_');
$gameDbRes = $game->field("id,relation_game_name as game_name,partner_id as cp_id,original_package_name")->where($game_where)->select();
$gameDbRes = $game->field("id,relation_game_name as game_name,partner_id as cp_id,original_package_name,select_package_name ")->where($game_where)->select();
$gameList=[];
foreach ($gameDbRes as $k => $v) {
$gameList[$v['id']] =$v;
@ -392,7 +409,11 @@ class CompanyStatementSetController extends Controller {
if(empty($v['original_package_name'])){
$game_name = $v['game_name'];
}else{
$game_name = $v['original_package_name'];
if($v['select_package_name'] == 1){
$game_name = $v['original_package_name'];
}else{
$game_name = $v['game_name'];
}
}
$cp_id = $v['cp_id'];
unset($v['cp_id']);
@ -430,7 +451,7 @@ class CompanyStatementSetController extends Controller {
}else{
$where['settlement_type']=1;
$pcDbRes = M("CompanyRelation","tab_")->where($where)->select();
}
}
$pcList=[];
$js_id = [];//己方公司
foreach ($pcDbRes as $k => $v) {
@ -450,11 +471,25 @@ class CompanyStatementSetController extends Controller {
return ;
}
//获取pc公司
$tmpp = M("PromoteCompany","tab_")->field("id,uid,company_name partner,settlement_contact link_man,contact_phone link_phone,bank_name opening_bank,bank_address payee_name,bank_card bank_account,fax_ratio,company_belong,company_type,ali_user,ali_account")
$tmpp = M("PromoteCompany","tab_")->field("id,uid,company_name partner,settlement_contact link_man,contact_phone link_phone,bank_name opening_bank,bank_address payee_name,bank_card bank_account,fax_ratio,taxation_rate, company_belong,company_type,ali_user,ali_account,has_server_fee,server_fee,has_ratio,turnover_ratio ")
->where(["id"=>['in',$pc_id],"company_type"=>1])
->select();
$pc =[];
$pc =[];
$Promote = M("Promote","tab_");
foreach ($tmpp as $v) {
//获取会长的支付宝账号
$p_map = [
"company_id"=>$v['id'],
"withdraw_done"=>1
];
$pres = $Promote->field("alipay_account,alipay_real_name")->where($p_map)->find();
if(!empty($pres) & & !empty($pres['alipay_account']) & & !empty($pres['alipay_real_name'])){
$v['ali_user'] = $pres['alipay_real_name'];
$v['ali_account'] = $pres['alipay_account'];
}
if($v['has_ratio']){
$v['turnover_ratio'] = json_decode($v['turnover_ratio'],true);
}
$pc[$v['id']]=$v;
}
@ -466,20 +501,24 @@ class CompanyStatementSetController extends Controller {
$our[$v['id']]=$v;
}
unset($tmpp);
unset($js_id);
$this->getPromoteCompanySpend($pcList,array_unique(array_keys($pcList)),$begintime,$endtime);
unset($js_id);
$this->getPromoteCompanySpend($pcList,array_unique(array_keys($pcList)),$begintime,$endtime);
// dd($pcList);
//数据整合
$verify_log=json_encode(["create_user"=>"system","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);
$RewardRecord = M("RewardRecord ","tab_");
$RewardRecord = M("RewardDetail ","tab_");
foreach($pcList as $k=>$v){
if(!isset($pc[$k])){continue;}//不存在的公司不结算
if(!isset($pc[$k])){continue;}//不存在的公司不结算
if($pc[$k]['has_ratio'] == 1){
$company_ratio = $this->getCompanyRatio($v['pay_amount'],$pc[$k]['turnover_ratio']);
}else{
$company_ratio = 0;
}
$add_data=[];
//1.获取甲乙方信息
if($v['first_company_type'] > 0){
@ -513,7 +552,7 @@ class CompanyStatementSetController extends Controller {
$add_data['statement_info'] =[];
//获取渠道及发票税率
$fax_ratio = $pc[$add_data['company_id']]['fax_ratio'] ?? 0;
$fax_ratio = $pc[$add_data['company_id']]['taxation_rate'] ?? 0; //税费费率
$company_belong = $pc[$add_data['company_id']]['company_belong'];
$add_data['company_belong']=$company_belong;
$add_data['company_type']=$pc[$add_data['company_id']]['company_type'];
@ -526,14 +565,15 @@ class CompanyStatementSetController extends Controller {
//获取比例
$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 = 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;
$tratio = $tratio1-$tratio2+$company_ratio ;
if($tratio < = 0){ continue;}
}
@ -544,7 +584,7 @@ class CompanyStatementSetController extends Controller {
}
$add_data['pay_amount'] += $va['pay_amount'];
$game['sum_money']=round($va['pay_amount']*$tratio*(100-$fax_ratio)/(100*100),2);//结算金额=平台总额*( 1-渠道费)* 分成比例*(1-税费费率)
$game['sum_money']=round($va['pay_amount']*$tratio*(100-$fax_ratio)/(100*100),2);//结算金额=平台总额*(分成比例+补点比例) *(1-税费费率)
$add_data['statement_money'] += $game['sum_money'];
$game['fax_ratio']=$fax_ratio;
@ -558,14 +598,15 @@ class CompanyStatementSetController extends Controller {
if($type < 2 ) {
//非补点计算奖罚
$rrmap = array(
"reward _time" => ['between', [$begintime,$endtime]],
"confirm _time" => ['between', [$begintime,$endtime]],
"company_type"=>2,
"company_id"=>$add_data['company_id']
"company_id"=>$add_data['company_id'],
"settlement_type"=>2,
);
$rfres = M("RewardRecord ","tab_")
$rfres = M("RewardDetail ","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
IFNULL(SUM(CASE WHEN type = 1 THEN amount ELSE 0 END),0) as reward_count,
IFNULL(SUM(CASE WHEN type = 2 THEN amount ELSE 0 END),0) as fine_count
")
->where($rrmap)->find();
if($rfres['reward_count'] > 0){
@ -593,7 +634,22 @@ class CompanyStatementSetController extends Controller {
);
$add_data['statement_money'] -= $fine_count;
$add_data['pay_amount'] -= $fine_count;;
}
}
//获取服务器费用
$tmp_company_info= $pc[$add_data['company_id']];
if($tmp_company_info['has_server_fee'] == 1 & & $tmp_company_info['server_fee'] >0){
$server_count = $tmp_company_info['server_fee'];
$add_data['statement_info'][] = array(
"statement_begin_time"=>$statement_begin_time,
"statement_end_time"=>$statement_end_time,
'statement_type'=>1,
"game_name"=>"服务器费用",
'pay_amount'=>$server_count,
'sum_money'=>$server_count,
);
$add_data['statement_money'] -= $server_count;
$add_data['pay_amount'] -= $server_count;;
}
}
$add_data['statement_info'] = json_encode($add_data['statement_info'],JSON_UNESCAPED_UNICODE);
@ -647,7 +703,8 @@ class CompanyStatementSetController extends Controller {
"s.pay_time"=>['between', [$begintime,$endtime]],
];
for ($i=0; $i < count ( $ idarr ) ; $ i + + ) {
$company_id = $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']];
@ -657,6 +714,11 @@ class CompanyStatementSetController extends Controller {
->select();
if(empty($list)){continue;}
foreach($list as $k=>$v){
try {
$res[$company_id]["pay_amount"] += $v['pay_amount'];
} catch (\Throwable $th) {
$res[$company_id]["pay_amount"] = $v['pay_amount'];
}
if(isset($res[$company_id][$v['relation_game_id']])){
//
$res[$company_id][$v['relation_game_id']]['pay_amount'] += $v['pay_amount'];
@ -665,9 +727,12 @@ class CompanyStatementSetController extends Controller {
$res[$company_id][$v['relation_game_id']]= $v;
}
}
}
}
foreach($res as $k=>$v){
$pcList[$k]["pay_amount"] = $v['pay_amount'];
unset($v['pay_amount']);
$pcList[$k]['list'] = $v;
}
}
public function promoteUserPool($type,$stime,$recount = false){
@ -703,13 +768,18 @@ class CompanyStatementSetController extends Controller {
if($type == 0){//周结
$this->doPromoteUserPool($user,$users,$type,$stime,$recount);
}else{
}else if($type == 1) {
$users['withdraw_type'] = 1;
$user['withdraw_type'] = 1;
//月结& 补点
$this->doPromoteUserPool($user,$users,1,$stime,$recount);
}else{
$users['withdraw_type'] = 2;
$user['withdraw_type'] = 2;
$this->doPromoteUserPool($user,$users,2,$stime,$recount);
}
$this->savePromotateUserPool($users);
$this->savePromotateUserPool($user);
}
@ -748,13 +818,28 @@ class CompanyStatementSetController extends Controller {
}
//获取pc公司
$pc_id = implode(",",array_flip(array_flip(array_keys($pcList))));
$tmpp = M("PromoteCompany","tab_")->field("id,uid,company_name partner,settlement_contact link_man,contact_phone link_phone,bank_name opening_bank,bank_address payee_name,bank_card bank_account,fax_ratio,company_belong,company_type,ali_user,ali_account")
$tmpp = M("PromoteCompany","tab_")
->field("id,uid,company_name partner,settlement_contact link_man,contact_phone link_phone,bank_name opening_bank,bank_address payee_name,bank_card bank_account,fax_ratio,taxation_rate,company_belong,company_type,ali_user,ali_account,has_server_fee,server_fee,has_ratio,turnover_ratio")
->where(["id"=>['in',$pc_id],"company_type"=>2])
->select();
if(count($tmpp)< 1 ) { return ; }
$pc =[];
$Promote = M("Promote","tab_");
foreach ($tmpp as $v) {
$p_map = [
"company_id"=>$v['id'],
"withdraw_done"=>1
];
$pres = $Promote->field("alipay_account,alipay_real_name")->where($p_map)->find();
if(!empty($pres) & & !empty($pres['alipay_account']) & & !empty($pres['alipay_real_name'])){
$v['ali_user'] = $pres['alipay_real_name'];
$v['ali_account'] = $pres['alipay_account'];
}
if($v['has_ratio']){
$v['turnover_ratio'] = json_decode($v['turnover_ratio'],true);
}
$pc[$v['id']]=$v;
$pc[$v['id']]['is_payment']=$pcList[$v['id']]['is_payment'];
}
@ -764,11 +849,18 @@ class CompanyStatementSetController extends Controller {
$StatementDb = M("CompanyStatement","tab_");
$statement_begin_time = date("Y-m-d",$begintime);
$statement_end_time = date("Y-m-d",$endtime);
$RewardRecord = M("RewardRecord","tab_");
// $RewardRecord = M("RewardRecord","tab_");
$LackStatement = M("company_lack_statement_info","tab_");
$StatementInfo = M("company_statement_info","tab_");
foreach($pc as $k=>& $v){
$fax_ratio = $v['taxation_rate'];
$RewardRecordRes = $this->getPromoteUserRewardRecord($v['id'],$begintime,$endtime);
if($v['has_ratio'] == 1){
$company_ratio = $this->getCompanyRatio($v['pay_amount'],$pc[$k]['turnover_ratio']);
}else{
$company_ratio = 0;
}
//获取其他信息
$v = $this->getCompanyOtherInfo($v,$k);
if($v['is_payment'] == 1){
@ -782,6 +874,21 @@ class CompanyStatementSetController extends Controller {
$v['reward'] = 0;
$v['platform_amount'] = 0;
if(!isset($v['list'])){continue;}
//服务器费用
if ($type < 2 ) {
if ($v['has_server_fee'] == 1 & & $v['server_fee'] > 0) {
$v['statement_info'][] = [
"game_name"=>"服务器费用",
"sum_money"=>-$v['server_fee'],
"pay_amount"=>-$v['server_fee'],
'statement_type'=>1,
"statement_begin_time"=>$statement_begin_time,
"statement_end_time"=>$statement_end_time
];
$v['pay_amount'] -= $v['server_fee'];
$v['statement_money'] -= $v['server_fee'];
}
}
foreach($v['list'] as $ke=>$va){
$game =[];
$game['pay_amount'] =$va['pay_amount'];
@ -792,13 +899,14 @@ class CompanyStatementSetController extends Controller {
if($type == 1){
$tratio = getGamePromoteCompanyRadio($v['id'],$va['relation_game_id'],$endtime,$va['pay_amount'],true,$company_belong);
$tratio += $company_ratio;
}elseif($type == 0){
$tratio = getGamePromoteCompanyRadio($v['id'],$va['relation_game_id'],$endtime,$va['pay_amount'],false,$company_belong);
}else{
//补点
$tratio1 = getGamePromoteCompanyRadio($v['id'],$va['relation_game_id'],$endtime,$va['pay_amount'],true,$company_belong);;
$tratio2 = getGamePromoteCompanyRadio($v['id'],$va['relation_game_id'],$endtime,$va['pay_amount'],false,$company_belong);
$tratio = $tratio1-$tratio2;
$tratio = $tratio1-$tratio2+$company_ratio ;
if($tratio < = 0){
continue;
}
@ -813,23 +921,18 @@ class CompanyStatementSetController extends Controller {
//判断是否是个人
if($type < 2 ) {
//非补点
$rrmap = array(
"reward_time" => ['between', [$begintime,$endtime]],
"company_type"=>2,
"company_id"=>$k,
"relation_game_id"=>$va['relation_game_id']
);
$rrfres =$RewardRecord->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();
$v['fine'] += $rrfres['fine_count'];
$v['reward'] += $rrfres['reward_count'];
if($RewardRecordRes){
$rrfres = $RewardRecordRes[$va['relation_game_id']];
}else{
$rrfres = [
'reward_count'=>0,
'fine_count'=>0,
];
}
$game['reward'] = $rrfres['reward_count'];
$game['fine'] = $rrfres['fine_count'];
$game['sum_money']=round($va['pay_amount']*$tratio/100,2)+$game['reward']-$game['fine'];
}else{
$game['sum_money']=round($va['pay_amount']*$tratio*(100-$fax_ratio)/(100*100),2);//结算金额=平台总额*( 1-渠道费)*分成比例*(1-税费费率)
}
$game['sum_money']=round($va['pay_amount']*$tratio/100,2)+$game['reward']-$game['fine']; //个人等于 结算金额*比例-罚款+奖励
$v['statement_money'] += $game['sum_money'];
$game['fax_ratio']=$fax_ratio;
@ -944,7 +1047,9 @@ class CompanyStatementSetController extends Controller {
* 保存信息汇总信息
*/
public function savePromotateUserPool($data){
if(empty($data['create_lack_ids']) & & empty($data['del_lack_ids']) & & empty($data['info_ids'])){ return;}
if(empty($data['create_lack_ids']) & & empty($data['del_lack_ids']) & & empty($data['info_ids'])){ //补点为空不重算
return ;
}
if($data['statement_money'] == 0){
$data['verify_status'] = 5;
$data['verify_log'] = json_encode(["create_user"=>"system","create_time"=>date("Y-m-d H:i:s"),"verify_user"=>"system","verify_time"=>date("Y-m-d H:i:s"),"payment_user"=>"system","payment_time"=>date("Y-m-d H:i:s")]);
@ -988,6 +1093,63 @@ class CompanyStatementSetController extends Controller {
public function getGameTypeName($relation_game_id){
return M("Game","tab_")->field("game_type_name")->where("relation_game_id = '{$relation_game_id}'")->find()['game_type_name'];
}
/**
* 获取公司比例加成
* @param [type] $paymount 支付金额
* @param [type] $turnover_ratio 梯度
*/
protected function getCompanyRatio($paymount,$turnover_ratio)
{
$ratio = 0;
foreach($turnover_ratio as $k=>$v){
if($v['instanceof'] == 1){
if($paymount >= $v['turnover']){
$ratio = $v['ratio'];
}
}else{
if($paymount > $v['turnover']){
$ratio = $v['ratio'];
}
}
}
return $ratio;
}
/**
* 获取公司比例加成
* @param [type] $paymount 支付金额
* @param [type] $turnover_ratio 梯度
*/
protected function getPromoteUserRewardRecord($cpmpamy_id,$begintime,$endtime)
{
//获取所有公司的奖罚
$rrmap = array(
"d.confirm_time" => ['between', [$begintime,$endtime]],
"d.company_type"=>2,
"d.company_id"=>$cpmpamy_id,
"d.settlement_type"=>2,
);
$rfres = M("RewardDetail","tab_")
->alias("d")
->field("
IFNULL(SUM(CASE WHEN type = 1 THEN amount ELSE 0 END),0) as reward_count,
IFNULL(SUM(CASE WHEN type = 2 THEN amount ELSE 0 END),0) as fine_count,
r.relation_game_id
")
->where($rrmap)
->join("tab_reward_record as r on d.record_id = r.id")
->group("record_id,company_id")
->select();
if(empty($rfres)){
return false;
}else{
$s = [];
foreach($rfres as $k=>$v){
$s[$v['relation_game_id']] = $v;
}
return $s;
}
}
/**
* 重算接口