You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

455 lines
15 KiB
PHTML

<?php
namespace Admin\Controller;
use Think\Controller;
/**
* 财务结算预警
* TODO:当前业务下不用计算奖罚
* @author cz
*
*/
class StatementWarningSetController extends Controller {
private $beginThismonth;
private $endThismonth;
private $date;
private $nowdata;
private $month;//月份
private $year;//年份
private $adddata;//要添加的数据
private $inSideCompanyIds;
private $cpCompanyIsWm;
private $promoteCompanyIsWm;
private $statementWarningKey=[
'wm_platm'=>'万盟平台',
'up_statement'=>'上游',
'pc_statement'=>'下游内团',
'pu_statement'=>'下游外团',
'margin_ratio'=>'毛利率',
'profit_ratio'=>'利润率',
'cost'=>'费用',
'other_income'=>'其他收入'
];
private $statementWarningModel;
private $statementWarningInfoModel;
private $spendModel;
private $isInit = false;
public function getStatementWarningKey(){
return $this->statementWarningKey;
}
private function configInit($count_date){
if($this->isInit) return; //避免重复生成配置
ini_set('serialize_precision',14);
if(empty($count_date)) die("参数错误");
$this->adddata = [];//置空否者会脚本进来会重复计算
$this->date = $count_date;
$tarry = explode('-',$count_date);
$this->year= $tarry[0];
if(strlen($tarry[1]) < 2) $tarry[1]="0".$tarry[1];
$this->month= $tarry[1];
$this->beginThismonth=mktime(0,0,0,$tarry[1],1,$tarry[0]);
$this->endThismonth=mktime(0,0,0,$tarry[1]-0+1,1,$tarry[0])-1;
$this->statementWarningModel=M("statement_warning","tab_");
$this->statementWarningInfoModel=M("statement_warning_info","tab_");
$this->spendModel=M("spend","tab_");
$this->isInit = true;
}
3 years ago
public function run()
{
$count_date = date('Y',time()).'-'.(date('m',time()));
$this->configInit($count_date);
3 years ago
$nextMonth = $this->getNextMonth();
3 years ago
$hasdb = $this->statementWarningModel->where([
3 years ago
'count_year'=>$nextMonth[0],
'count_month'=>$nextMonth[1],
3 years ago
"name"=>'wm_platm',
"type"=>1
])->find();
if(!$hasdb){
//第一次生成,同时上一次结扎
3 years ago
$preMonth = $this->getPreMonth();
$preCountMonth = $preMonth[0]."-".$preMonth[1];
$params = "php ".SUBSITE_INDEX." StatementWarningSet/setFreeMonth/count_date/{$preCountMonth}";
3 years ago
$time = time()+2*3600;
D("CmdTasks")->addScheduleTask("StatementWarningSet",$params,$time);
}
$this->setStatementWarning();
$this->setDefaultStatementWarning();
3 years ago
}
/**
* 更新某月毛利数据
*/
public function setFreeMonth($count_date)
{
$this->configInit($count_date);
$this->setStatementWarning();
$this->setDefaultStatementWarning();
}
/**
* 设置
*/
private function setStatementWarning()
{
$this->getInsideCompanyid();
$this->setCompanyIsWm();
3 years ago
$this->getAllWmAmount();
$this->cpStatement();
3 years ago
$this->pcStatement();
$this->puStatement();
echo "{$this->year}-{$this->month}生成成功".PHP_EOL;
}
/**
* 获取公司内部公司id
*/
private function getInsideCompanyid()
{
$res = M("PromoteCompany","tab_")->where("is_inside = 1")->field("id")->select();
if($res){
$res = array_column($res,"id");
$res[] = 0;
}else{
$res = [0];
}
$this->inSideCompanyIds = implode(",",$res);
}
/**
* 获取CP是否万盟结算数据
*/
private function setCompanyIsWm()
{
$preMonth = $this->getPreMonth();
//cp
$preCpMonth = $this->statementWarningInfoModel->where([
'count_year'=>$preMonth[0],
'count_month'=>$preMonth[1],
'company_belong'=>9
])->getField('company_id,is_wm',true);
$selfCpMonth = $this->statementWarningInfoModel->where([
3 years ago
'count_year'=>$this->year,
'count_month'=>$this->month,
'company_belong'=>9
])->getField('company_id,is_wm',true);
if(!$preCpMonth) $preCpMonth = [];
if(!$selfCpMonth) $selfCpMonth = [];
3 years ago
3 years ago
$this->cpCompanyIsWm = $preCpMonth;
foreach ($selfCpMonth as $key => $value) {
$this->cpCompanyIsWm[$key] = $value;
}
//下游
$preMonth = $this->statementWarningInfoModel->where([
'count_year'=>$preMonth[0],
'count_month'=>$preMonth[1],
'company_belong'=>['NEQ',9]
])->getField('company_id,is_wm',true);
$selfMonth = $this->statementWarningInfoModel->where([
3 years ago
'count_year'=>$this->year,
'count_month'=>$this->month,
'company_belong'=>['NEQ',9]
])->getField('company_id,is_wm',true);
if(!$preMonth) $preMonth = [];
if(!$selfMonth) $selfMonth = [];
3 years ago
$this->promoteCompanyIsWm = $preMonth;
foreach ($selfMonth as $key => $value) {
$this->promoteCompanyIsWm[$key] = $value;
}
}
/**
* 获取当前万盟结算额
*/
private function getAllWmAmount()
{
//获取官方渠道
$merchantIds = M("payment_merchant","tab_")->group('main_id')->where("main_id = 1")->getField("GROUP_CONCAT(id) ids");
$where = [
'merchant_id'=>['in',$merchantIds],
'pay_status'=>1,
'payed_time'=>['between',[$this->beginThismonth,$this->endThismonth]],
];
$amout = $this->spendModel->where($where)->where(['pay_way'=>['GT',0],])->getField("sum(pay_amount) amount");
$despost = M("deposit","tab_")->where($where)->getField("sum(pay_amount) amount");
$this->addStatementWarning(1,'wm_platm',$amout-0+$despost);
}
/**
* 添加默认信息
*/
private function setDefaultStatementWarning()
{
$this->addStatementWarning(3,'cost',700000,false);
$this->addStatementWarning(3,'other_income',26000,false);
}
/**
* 获取上游的结算金额
*/
private function cpStatement()
{
$gamedata = $this->getCpGameSpend();
$cpStatementAmount = 0;
if($gamedata){
$company = $this->getCpCompanyInfo(array_keys($gamedata));
$cpStatementAmount = $this->addCpStatementWarningInfo($company,$gamedata);
}
$this->addStatementWarning(2,'up_statement',$cpStatementAmount);
}
/**
* 获取月份游戏结算额
*/
private function getCpGameSpend()
{
$where = [
'pay_status'=> 1,
'payed_time'=>['between',[$this->beginThismonth,$this->endThismonth]],
"pay_game_status"=>1
];
$dbres = $this->spendModel->where($where)->group("game_id")->getField("game_id,sum(pay_amount) amount",true);
if($dbres){
foreach ($dbres as $key => $val) {
$tratio = getGameCpRadio($key, $val, true);
$dbres[$key] = round($val*$tratio/100,2);
}
}
return $dbres;
}
/**
* 按游戏id获取公司信息 partner_id
*/
private function getCpCompanyInfo($game_ids)
{
return M('game','tab_')
->alias("g")
->join("tab_partner as p on g.partner_id = p.id")
->where(['g.id'=>['in',$game_ids]])
->group("g.partner_id")
->getField('p.id company_id,GROUP_CONCAT(g.id) game_ids,p.partner company_name,channel_rate,taxation_rate',true);
}
/**
* 添加上游详情
*/
private function addCpStatementWarningInfo($company,$gamedata)
{
// $cpStatementAmount = 0;
foreach ($company as $key => $value) {
$statementMoney = 0;
$gameids = explode(",",$value['game_ids']);
foreach ($gameids as $gameid) {
$statementMoney += ($gamedata[$gameid]-0);
}
//上游需要计算祱费及渠道费用
if($value['channel_rate'] > 0 || $value['taxation_rate'] > 0){
$statementMoney=round($statementMoney*(100-$value['channel_rate'])*(100-$value['taxation_rate'])/(100*100), 2);//结算金额=平台总额*1-渠道费)*分成比例*(1-税费费率)
}
$company[$key]['statement_money'] = $statementMoney;
unset($company[$key]['game_ids']);
}
return $this->addStatementWarningInfo($company,9);
}
/**
* 获取内团的结算金额
*/
private function pcStatement()
{
//获取内团公司
$company = M('promote_company','tab_')->where([
'id'=>["NOT IN",$this->inSideCompanyIds],
'company_belong'=>0
])->getField("id company_id,company_name,develop_type",true);
$company = $this->getPromoteCompanyGameSpend($company,0);
$pcStatementAmount = $this->addStatementWarningInfo($company,0);
$this->addStatementWarning(2,'pc_statement',$pcStatementAmount);
}
private function getPromoteCompanyGameSpend($company,$companyBelong)
{
$where = [
'tab_spend.is_check'=>1,
'tab_spend.pay_status'=>1,
'tab_spend.is_refund'=>0,
'tab_spend.payed_time'=>['between', [$this->beginThismonth,$this->endThismonth]],
"tab_promote.company_id"=>["IN",array_keys($company)],
];
$items = M('spend', 'tab_')
->field(["substring_index(game_name, '(', 1) relation_game_name",'tab_promote.company_id', 'sum(pay_amount) amount'])
->join('left join tab_promote on tab_spend.promote_id=tab_promote.id')
->where($where)
->group('tab_promote.company_id,relation_game_name')
->select();
if(!$items) return $company;
$CompanyGameRatio = D("CompanyGameRatio");
$games = M("Game",'tab_')->where(['relation_game_name' => ['in',array_column($items,'relation_game_name')]])->getField("relation_game_name,relation_game_id");
foreach ($items as $key => $val) {
$relationGameId = $games[$val['relation_game_name']];
$radio = end($CompanyGameRatio->getGameRadio($val['company_id'],$relationGameId,$companyBelong,$this->beginThismonth,$this->endThismonth));
$radio = $CompanyGameRatio->getTurnoverRatio($val['amount'],$radio['ratio'],$radio['turnover_ratio']);
$statementMoney =round($val['amount']*$radio/100,2);
if(isset($company[$val['company_id']]['statement_money'])){
$company[$val['company_id']]['statement_money'] += ( $statementMoney - 0);
}else{
$company[$val['company_id']]['statement_money'] = ( $statementMoney - 0);
}
}
return $company;
}
private function puStatement()
{
//获取公司
$company = M('promote_company','tab_')->where([
'id'=>["NOT IN",$this->inSideCompanyIds],
'company_type'=>1,
'company_belong'=>['in',[1,2]]
])->getField("id company_id,company_name,develop_type",true);
$puStatementAmount = 0;
if($company) {
$company = $this->getPuSpecialStatement( $this->getPromoteCompanyGameSpend($company,1) );
foreach ($company as $key => $value) {
if(!array_key_exists('statement_money',$value) ) {
unset($company[$key]);
};
}
3 years ago
$puStatementAmount = $this->addStatementWarningInfo($company,1);
};
$this->addStatementWarning(2,'pu_statement',$puStatementAmount);
}
private function getPuSpecialStatement($company)
{
$where = [
"company_belong"=>["NOT IN",[0,9]],
"company_id"=>["IN",array_keys($company)],
"withdraw_type"=>3,
"_string"=>"statement_begin_time >= {$this->beginThismonth} and statement_end_time <= {$this->endThismonth}"
];
$res = M("company_statement", "tab_")->where($where)->group('company_id')->getField("company_id,sum(statement_money) statement_money");
if($res){
foreach ($res as $key => $value) {
if(array_key_exists('statement_money',$company[$key])){
$company[$key]['statement_money'] += ($value-0);
}else{
$company[$key]['statement_money'] = ($value-0);
}
}
}
return $company;
}
private function addStatementWarning($type,$name,$money,$issave = true)
{
//汇总表差一个月
3 years ago
$nextMonth = $this->getNextMonth();
$savedata = [
'type'=>$type,
'name'=>$name,
3 years ago
'count_year'=>$nextMonth[0],
'count_month'=>$nextMonth[1]
];
$hasDb = $this->statementWarningModel->where($savedata)->find();
$savedata = array_merge(['money'=>$money,'create_time'=>time()],$savedata);
if($hasDb && $issave){
$savedata['id'] = $hasDb['id'];
$this->statementWarningModel->save($savedata);
}else{
$this->statementWarningModel->add($savedata);
}
}
private function addStatementWarningInfo($company,$companyBelong)
{
$statementAmount = 0;
foreach ($company as $key => $value) {
$statementMoney = array_key_exists('statement_money',$value) ? ($value['statement_money']-0) : 0;
$company[$key]['is_wm'] = $this->getCompanyIsWm($key,$companyBelong);
$company[$key]['company_belong'] = $companyBelong;
$company[$key]['count_month'] = $this->month;
$company[$key]['count_year'] = $this->year;
$company[$key]['create_time'] = time();
if($company[$key]['is_wm'] == 1){
$company[$key]['wm_amount'] = $statementMoney;
$company[$key]['other_amount'] = 0;
}else{
$company[$key]['wm_amount'] = 0;
$company[$key]['other_amount'] = $statementMoney;
}
$statementAmount += $company[$key]['wm_amount'];
$id = $this->statementWarningInfoModel->where([
'count_year'=>$this->year,
'count_month'=>$this->month,
'company_belong'=>$companyBelong,
'company_id'=>$key
])->getField("id");
if($id){
$company[$key]['id'] = $id;
$this->statementWarningInfoModel->save($company[$key]);
}else{
$this->statementWarningInfoModel->add($company[$key]);
}
}
return $statementAmount;
}
private function getCompanyIsWm($companyid,$company_belong)
{
if($company_belong == 9){
if(isset($this->cpCompanyIsWm[$companyid])){
return $this->cpCompanyIsWm[$companyid];
}
}else{
if(isset($this->promoteCompanyIsWm[$companyid])){
return $this->promoteCompanyIsWm[$companyid];
}
}
return 1;
}
private function getPreMonth()
{
return explode('-',date("Y-m",$this->beginThismonth-1));
}
3 years ago
private function getNextMonth()
{
return explode('-',date("Y-m",$this->endThismonth+1));
}
}