@ -406,323 +406,61 @@ class SubCompanyStatementSetController extends Controller {
/**
* 下游结算
*/
public function promoteCompanyStatement($type,$stime,$company_id=false,$recount=true)
public function promoteCompanyStatement($type,$stime,$recount_ company_id=false,$recount=true)
{
$t = $this->setBeginAndEndTime($type,$stime);
$begintime = $t[0];
$endtime = $t[1];
if($company_id === false){
//非重算
$where = [
"_string"=>"first_company_type = '2' OR second_company_type = '2'"
];
}else{
$where = [
"_string"=>"(first_company_type = '2' AND first_company_id in ({$company_id}) ) OR (second_company_type = '2' AND second_company_id in ({$company_id}) )"
];
}
//获取哪些要结算
if($type == 1){
$where['settlement_type']=2;
$pcDbRes = M("CompanyRelation","tab_")->where($where)->select();
}else{
$where['settlement_type']=1;
$pcDbRes = M("CompanyRelation","tab_")->where($where)->select();
}
$pcList=[];
$js_id = [];//己方公司
foreach ($pcDbRes as $k => $v) {
if($v['first_company_type'] == 2){
$pcList[$v['first_company_id']] =$v;
$js_id[] =$v['second_company_id'];
}else{
$pcList[$v['second_company_id']] =$v;
$js_id[] =$v['first_company_id'];
}
}
$pc_id = implode(",",array_flip(array_flip(array_keys($pcList))));
$js_id = implode(",",array_unique($js_id));
if(count($js_id) ==0 || count($pcList) == 0){
//无需处理
return ;
}
$pc_map = [
"id"=>["IN",$pc_id],
"_string"=>"(validity_start_time > 0 AND validity_end_time = 0 ) OR validity_end_time >= {$endtime}",
"company_type"=>1
];
//获取pc公司
$tmpp = M("PromoteCompany","tab_")->field("id,uid,company_name partner,settlement_contact link_man,address,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($pc_map)
->select();
// dump($tmpp);
if(empty($tmpp)){
return true;
}
$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;
}
unset($tmpp);
//获取己方公司
$our = [];
$tmpp = M("CompanyInfo","tab_")->field("id,partner,link_man,link_phone,address,company_tax_no,payee_name,bank_account,opening_bank")->where(["id"=>['in',$js_id]])->select();
foreach ($tmpp as $v) {
$our[$v['id']]=$v;
}
unset($tmpp);
unset($js_id);
$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")]);
$StatementDb = SM("CompanyStatement","tab_");
$statement_begin_time = date("Y.m.d",$begintime);
$statement_end_time = date("Y.m.d",$endtime);
$RewardRecord = M("RewardDetail","tab_");
foreach($pcList as $k=>$v){
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){
//甲方合作
$pc[$v['first_company_id']]['invoice_type'] = $v['invoice_type'];
$pc[$v['first_company_id']]['invoice_content'] = $v['invoice_content'];
$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{
//乙方合作
$pc[$v['second_company_id']]['invoice_type'] = $v['invoice_type'];
$pc[$v['second_company_id']]['invoice_content'] = $v['invoice_content'];
$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['is_payment']=$v['is_payment'];
$add_data['statement_info'] =[];
//获取渠道及发票税率
$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'];
if(!isset($v['game_list'])){continue;}
//获取多段比例
$game_ratio_list = D("CompanyGameRatio")->getPromoteCompanyGameRatio($add_data['company_id'],$v['game_ids'],$begintime,$endtime,$company_belong);
$handleed_data = [];
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'];
if(isset($v['game_list'][$t_game_id]['all_pay_amount'])){
$game_all_payment = $v['game_list'][$t_game_id]['all_pay_amount']-0;
}else{
// $game_all_payment = $v['game_list'][$t_game_id]['pay_amount']-0;
$game_all_payment = $v['game_list'][$t_game_id]['ratio_pay_amount']-0;
}
// dump($tratio);
$game['fax_ratio']=$fax_ratio-0;
$game['statement_type']=0;
if(count($t_game_ratio) == 1){
$tmp_game_ratio = $t_game_ratio[0];
$game['pay_amount'] =$this->getRelationGameIdSpend($add_data['company_id'],$t_game_id,$tmp_game_ratio['begintime'],$tmp_game_ratio['endtime']);
// $game['pay_amount'] = $v['game_list'][$t_game_id]['pay_amount'];
$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;
// if ($add_data['company_id'] == 5) {
// dump($game_all_payment);
// dump($this->getTurnoverType($t_game_id,$k,$begintime,$endtime));
// dump($tmp_game_ratio);
// }
}elseif($type == 0){
// $tratio = $tmp_game_ratio['ratio'];
$turnover_type = $this->getTurnoverType($t_game_id,$k,$begintime,$endtime);
$verify_log=json_encode(["create_user"=>"system","create_time"=>date("Y-m-d H:i:s")]);
if ($turnover_type == 2 || $turnover_type == 4) {
$tratio =$this->getTurnoverRatio($game_all_payment,$tmp_game_ratio['ratio'],$tmp_game_ratio['turnover_ratio']);
} else {
$tratio = $tmp_game_ratio['ratio'];
//获取主站数据begin
$MainStatementDb = M("CompanyStatement","tab_"); //主站结算单
$mainWhere = [
"company_belong"=>["neq",9],
"withdraw_type"=>["neq",3],
"statement_begin_time"=>$begintime,
"statement_end_time"=>$endtime
];
if($recount_company_id !== false){
$mainWhere['company_id'] = $recount_company_id;
}
}else{
//补点
$turnover_type = $this->getTurnoverType($t_game_id,$k,$begintime,$endtime);
if ($turnover_type == 2 || $turnover_type == 4) {
$statementList = $MainStatementDb->where($mainWhere)->select();
$StatementDb = SM("CompanyStatement","tab_");
$RewardRecord = SM("RewardDetail","tab_");
if(empty($statementList)) return false;
foreach ($statementList as $k => $v) {
$v['statement_info'] = json_decode($v['statement_info'],true);
$v['statement_money'] = 0;
$v['pay_amount'] = 0;
$v['verify_status'] = 0;
$v['verify_log'] = $verify_log;
$company_id = $v['company_id'];
unset($v['id'],$v['remark'],$v['confirm_status'],$v['confirm_log']);
foreach ($v['statement_info'] as $gameindex => & $game) {
if($game['statement_type'] > 0){
unset($v['statement_info'][$gameindex]);
continue;
}
$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;
}
$RewardRecordRes = $this->getPromoteUserGameRewardRecord($add_data['company_id'],$statement_begin_time,$statement_end_time);
if (
($handleed_data[$v['id'].$statement_begin_time.$statement_end_time]['fine'] || $handleed_data[$v['id'].$statement_begin_time.$statement_end_time]['fine'] == '0')
|| ($handleed_data[$v['id'].$statement_begin_time.$statement_end_time]['reward'] || $handleed_data[$v['id'].$statement_begin_time.$statement_end_time]['reward'] == '0')
) {
$game['fine'] = 0;
$game['reward'] = 0;
} else {
$game['fine'] = $RewardRecordRes['fine']?$RewardRecordRes['fine']:0;
$game['reward'] = $RewardRecordRes['reward']?$RewardRecordRes['reward']:0;
$handleed_data[$v['id'].$statement_begin_time.$statement_end_time]['fine'] = (string)$RewardRecordRes['fine'];
$handleed_data[$v['id'].$statement_begin_time.$statement_end_time]['reward'] = (string)$RewardRecordRes['reward'];
}
$game['sum_money']=round($game['pay_amount']*$tratio*(100-$fax_ratio)/(100*100),2);//结算金额=平台总额*(分成比例+补点比例)*(1-税费费率)
$add_data['statement_money'] += $game['sum_money'];
if ($game['pay_amount'] != '0') {
$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){
// if ($add_data['company_id'] == 5) {
// dump($game_all_payment);
// dump($this->getTurnoverType($t_game_id,$k,$begintime,$endtime));
// dump($tmp_game_ratio);
// }
$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'];
$turnover_type = $this->getTurnoverType($t_game_id,$k,$begintime,$endtime);
if ($turnover_type == 2 || $turnover_type == 4) {
$tratio =$this->getTurnoverRatio($game_all_payment,$tmp_game_ratio['ratio'],$tmp_game_ratio['turnover_ratio']);
} else {
$tratio = $tmp_game_ratio['ratio'];
}
}else{
$turnover_type = $this->getTurnoverType($t_game_id,$k,$begintime,$endtime);
if ($turnover_type == 2 || $turnover_type == 4) {
$relation_game_id = $game['relation_game_id'];
$game['pay_amount'] = $this->getRelationGameIdSpend($company_id,$relation_game_id,$begintime,$endtime);
if($game['pay_amount'] < = 0){
unset($v['statement_info'][$gameindex]);
continue;
}
//补点
$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;
}
$RewardRecordRes = $this->getPromoteUserGameRewardRecord($add_data['company_id'],$temp_game['statement_begin_time'],$temp_game['statement_end_time']);
if (
($handleed_data[$v['id'].$statement_begin_time.$statement_end_time]['fine'] || $handleed_data[$v['id'].$statement_begin_time.$statement_end_time]['fine'] == '0')
|| ($handleed_data[$v['id'].$statement_begin_time.$statement_end_time]['reward'] || $handleed_data[$v['id'].$statement_begin_time.$statement_end_time]['reward'] == '0')
) {
$game['fine'] = 0;
$game['reward'] = 0;
} else {
$game['fine'] = $RewardRecordRes['fine']?$RewardRecordRes['fine']:0;
$game['reward'] = $RewardRecordRes['reward']?$RewardRecordRes['reward']:0;
$handleed_data[$v['id'].$statement_begin_time.$statement_end_time]['fine'] = (string)$RewardRecordRes['fine'];
$handleed_data[$v['id'].$statement_begin_time.$statement_end_time]['reward'] = (string)$RewardRecordRes['reward'];
$v['pay_amount'] += $game['pay_amount'];
$game['sum_money'] = round($game['pay_amount']*$game['ratio']*(100-$game['fax_ratio'])/100/100,2);
$v['statement_money'] += $game['sum_money'];
}
$v['platform_amount'] = $v['pay_amount'];
//奖罚
$rrmap = ["confirm_time" => ['between', [$begintime,$endtime]],"company_type"=>2,"company_id"=>$company_id,"settlement_type"=>2];
$rfres = $RewardRecord->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")->where($rrmap)->find();
$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'];
if ($temp_game['pay_amount'] != '0') {
$add_data['statement_info'][] = $temp_game;
}
}
}
}
$add_data['platform_amount'] = $add_data['pay_amount'];
if($type < 2 ) {
//非补点计算奖罚
$rrmap = array(
"confirm_time" => ['between', [$begintime,$endtime]],
"company_type"=>2,
"company_id"=>$add_data['company_id'],
"settlement_type"=>2,
);
$rfres = M("RewardDetail","tab_")
->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
")
->where($rrmap)->find();
if($rfres['reward_count'] > 0){
$reward_count = $rfres['reward_count'];
$add_data ['statement_info'][] = array(
$v['statement_info'][] = array(
"statement_begin_time"=>$statement_begin_time,
"statement_end_time"=>$statement_end_time,
'statement_type'=>2,
@ -730,12 +468,12 @@ class SubCompanyStatementSetController extends Controller {
'pay_amount'=>$reward_count,
'sum_money'=>$reward_count,
);
$add_data['statement_money']+= $reward_count;
$add_data['pay_amount']+= $reward_count;
$v['statement_money'] += $reward_count;
$v['pay_amount'] += $reward_count;
}
if($rfres['fine_count'] > 0){
$fine_count = $rfres['fine_count'];
$add_data ['statement_info'][] = array(
$v ['statement_info'][] = array(
"statement_begin_time"=>$statement_begin_time,
"statement_end_time"=>$statement_end_time,
'statement_type'=>1,
@ -743,53 +481,24 @@ class SubCompanyStatementSetController extends Controller {
'pay_amount'=>$fine_count,
'sum_money'=>$fine_count,
);
$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;;
}
}
$is_continue = false;
$statement_data = $add_data['statement_info'];
foreach ($statement_data as $skey => $sval) {
if ($sval) {
$is_continue = true;
}
$v['statement_money'] -= $fine_count;
$v['pay_amount'] -= $fine_count;;
}
if (!$is_continue) {
continue;
}
$add_data['statement_info'] = json_encode($add_data['statement_info'],JSON_UNESCAPED_UNICODE);
if($type == 2 & & $add_data['statement_money'] == 0){
$v['statement_info'] = json_encode($v['statement_info'],JSON_UNESCAPED_UNICODE);
if($type == 2 & & $v['statement_money'] == 0){
//金额为0不补点
continue;
}
if($add_data ['platform_amount'] == 0){
if($v['platform_amount'] == 0){
continue;
}
//添加
if($recount === true & & $company_id !== false){
if($recount === true & & $recount_company_id !== false){
//非重算
return $add_data ;
return $v;
}
$StatementDb->add($add_data);
$StatementDb->add($v);
}
}
//设定开始结束时间
@ -1152,7 +861,6 @@ class SubCompanyStatementSetController extends Controller {
"s.payed_time"=>['elt',$endtime]
];
D("Spend")->addSubsiteWhere($cpAllMap,"s");
// unset($cpAllMap["s.payed_time"]);
for ($i=0; $i < count ( $ idarr ) ; $ i + + ) {
$company_id = $idarr[$i];
@ -1187,10 +895,6 @@ class SubCompanyStatementSetController extends Controller {
->join("left join tab_game g on s.game_id = g.id")
->select();
// if ($company_id == 64) {
// dump($all_list);
// }
if(empty($list)){continue;}
$game = [];
foreach($list as $k=>$v){
@ -1228,9 +932,6 @@ class SubCompanyStatementSetController extends Controller {
$cpSpendList = $Spend->alias('s')->field('sum(s.pay_amount) pay_amount,s.game_id,g.relation_game_id,g.relation_game_name game_name')->where($cpMap)->group('game_id')
->join("left join tab_game g on s.game_id = g.id")
->select();
// if ($company_id == 43) {
// dump($cpMap);
// }
foreach($cpSpendList as $ck=>$cv){
if(isset($cpgame[$cv['relation_game_id']])){
$cpgame[$cv['relation_game_id']]['pay_amount'] += $cv['pay_amount'];
@ -1259,10 +960,8 @@ class SubCompanyStatementSetController extends Controller {
$temp += $cpallgame[$v[$j]]['pay_amount'];
} else {
// $res[$company_id][$v[$j]]['ratio_pay_amount'] = $res[$company_id][$v[$j]]['pay_amount'];
$temp += $game[$v[$j]]['pay_amount'];
}
// $temp += $game[$v[$j]]['pay_amount'];
}
$sameGame[$k] = $temp;
}
@ -1290,7 +989,6 @@ class SubCompanyStatementSetController extends Controller {
$turnover_type = $this->getTurnoverType($v['relation_game_id'],$company_id,$begintime,$endtime);
if ($turnover_type == 2) {
// $temp += $allgame[$v[$j]]['pay_amount'];
$res[$company_id][$p_account][$v['relation_game_id']]['all_pay_amount']= $allgame[$v['relation_game_id']]['pay_amount'];
} elseif($turnover_type == 3) {
@ -1298,10 +996,6 @@ class SubCompanyStatementSetController extends Controller {
$cpSpendList = $Spend->alias('s')->field('sum(s.pay_amount) pay_amount,s.game_id,g.relation_game_id,g.relation_game_name game_name')->where($cpMap)->group('game_id')
->join("left join tab_game g on s.game_id = g.id")
->select();
// if ($company_id == 43) {
// dump($cpMap);
// }
foreach($cpSpendList as $ck=>$cv){
if(isset($cpgame[$cv['relation_game_id']])){
$cpgame[$cv['relation_game_id']]['pay_amount'] += $cv['pay_amount'];
@ -1330,11 +1024,8 @@ class SubCompanyStatementSetController extends Controller {
$res[$company_id][$p_account][$v['relation_game_id']]['all_pay_amount']= $cpallgame[$v['relation_game_id']]['pay_amount'];
} else {
// $res[$company_id][$v[$j]]['ratio_pay_amount'] = $res[$company_id][$v[$j]]['pay_amount'];
$res[$company_id][$p_account][$v['relation_game_id']]['all_pay_amount']= $game[$v['relation_game_id']]['pay_amount'];
}
// $res[$company_id][$p_account][$v['relation_game_id']]['all_pay_amount']= $game[$v['relation_game_id']]['pay_amount'];
$res[$company_id][$p_account][$v['relation_game_id']]['game_type_name'] = $this->getGameTypeName($v['relation_game_id']);
}
}
@ -1413,14 +1104,8 @@ class SubCompanyStatementSetController extends Controller {
$where = [
"_string"=>"first_company_type = '2' OR second_company_type = '2'"
];
//获取哪些要结算
if($type == 1){//月结
$where['settlement_type']=2;
$pcDbRes = M("CompanyRelation","tab_")->where($where)->select();
}else{
$where['settlement_type']=1;
$pcDbRes = M("CompanyRelation","tab_")->where($where)->select();
}
$pcList=[];
$js_id = [];//己方公司
@ -1453,11 +1138,6 @@ class SubCompanyStatementSetController extends Controller {
"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);
}
@ -1468,7 +1148,7 @@ class SubCompanyStatementSetController extends Controller {
unset($tmpp);
$this->getPuPromoteSpend($pc,array_unique(array_keys($pc)),$begintime,$endtime);
// dump($pc);die();
dump($pc);die();
$StatementDb = SM("CompanyStatement","tab_");
$statement_begin_time = date("Y.m.d",$begintime);