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.
290 lines
8.7 KiB
PHTML
290 lines
8.7 KiB
PHTML
3 years ago
|
<?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'=>'上游'
|
||
|
];
|
||
|
|
||
|
private $statementWarningModel;
|
||
|
private $statementWarningInfoModel;
|
||
|
private $spendModel;
|
||
|
private $isInit = false;
|
||
|
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 更新某月毛利数据
|
||
|
*/
|
||
|
public function setFreeMonth($count_date)
|
||
|
{
|
||
|
$this->configInit($count_date);
|
||
|
$this->setStatementWarning();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 设置
|
||
|
*/
|
||
|
private function setStatementWarning()
|
||
|
{
|
||
|
$this->getInsideCompanyid();
|
||
|
$this->setCompanyIsWm();
|
||
|
|
||
|
$this->getAllWmAmount();
|
||
|
$this->cpStatement();
|
||
|
|
||
|
|
||
|
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()
|
||
|
{
|
||
|
|
||
|
$year = date("Y",$this->beginThismonth-1);
|
||
|
$month = date("m",$this->beginThismonth-1);
|
||
|
//cp
|
||
|
$preCpMonth = $this->statementWarningInfoModel->where([
|
||
|
'year'=>$year,
|
||
|
'month'=>$month,
|
||
|
'company_belong'=>9
|
||
|
])->getField('company_id,is_wm',true);
|
||
|
|
||
|
$selfCpMonth = $this->statementWarningInfoModel->where([
|
||
|
'year'=>$this->year,
|
||
|
'month'=>$this->month,
|
||
|
'company_belong'=>9
|
||
|
])->getField('company_id,is_wm',true);
|
||
|
|
||
|
if(!$preCpMonth) $preCpMonth = [];
|
||
|
if(!$selfCpMonth) $selfCpMonth = [];
|
||
|
|
||
|
$this->cpCompanyIsWm = array_merge($preCpMonth,$selfCpMonth);
|
||
|
|
||
|
//下游
|
||
|
$preMonth = $this->statementWarningInfoModel->where([
|
||
|
'year'=>$year,
|
||
|
'month'=>$month,
|
||
|
'company_belong'=>['NEQ',9]
|
||
|
])->getField('company_id,is_wm',true);
|
||
|
|
||
|
$selfMonth = $this->statementWarningInfoModel->where([
|
||
|
'year'=>$this->year,
|
||
|
'month'=>$this->month,
|
||
|
'company_belong'=>['NEQ',9]
|
||
|
])->getField('company_id,is_wm',true);
|
||
|
|
||
|
if(!$preMonth) $preMonth = [];
|
||
|
if(!$selfMonth) $selfMonth = [];
|
||
|
|
||
|
$this->promoteCompanyIsWm = array_merge($preMonth,$selfMonth);
|
||
|
|
||
|
}
|
||
|
/**
|
||
|
* 获取当前万盟结算额
|
||
|
*/
|
||
|
private function getAllWmAmount()
|
||
|
{
|
||
|
$where = [
|
||
|
'partner_type'=>['in',[0,1]],
|
||
|
'pay_status'=>1,
|
||
|
'payed_time'=>['between',[$this->beginThismonth,$this->endThismonth]],
|
||
|
];
|
||
|
$amout = $this->spendModel->where($where)->getField("sum(pay_amount) amount");
|
||
|
$sWWhere = [
|
||
|
"count_month"=>$this->month,
|
||
|
"count_year"=>$this->year,
|
||
|
"name"=>'wm_platm',
|
||
|
"type"=>1
|
||
|
];
|
||
|
$hasDb = $this->statementWarningModel->where($sWWhere)->find();
|
||
|
$saveData = array_merge(['money'=>$amout,'create_time'=>time()],$sWWhere);
|
||
|
if($hasDb){
|
||
|
$saveData['id'] = $hasDb['id'];
|
||
|
$this->statementWarningModel->save($saveData);
|
||
|
}else{
|
||
|
$this->statementWarningModel->add($saveData);
|
||
|
}
|
||
|
}
|
||
|
/**
|
||
|
* 获取上游的结算金额
|
||
|
*/
|
||
|
private function cpStatement()
|
||
|
{
|
||
|
$gamedata = $this->getCpGameSpend();
|
||
|
$cpStatementAmount = 0;
|
||
|
if($gamedata){
|
||
|
$company = $this->getCpCompanyInfo(array_keys($gamedata));
|
||
|
$cpStatementAmount = $this->addCpStatementWarningInfo($company,$gamedata);
|
||
|
}
|
||
|
$savedata = [
|
||
|
'type'=>2,
|
||
|
'name'=>'up_statement',
|
||
|
'count_month'=>$this->month,
|
||
|
'count_year'=>$this->year
|
||
|
];
|
||
|
$hasDb = $this->statementWarningModel->where($savedata)->find();
|
||
|
$savedata = array_merge(['money'=>$cpStatementAmount,'create_time'=>time()],$savedata);
|
||
|
if($hasDb){
|
||
|
$savedata['id'] = $hasDb['id'];
|
||
|
$this->statementWarningModel->save($savedata);
|
||
|
}else{
|
||
|
$this->statementWarningModel->add($savedata);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
/**
|
||
|
* 获取月份游戏结算额
|
||
|
*/
|
||
|
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',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);
|
||
|
}
|
||
|
$cpStatementAmount += $statementMoney;
|
||
|
unset($company[$key]['game_ids']);
|
||
|
$company[$key]['statement_money'] = $statementMoney;
|
||
|
$company[$key]['is_wm'] = $this->getCompanyIsWm($key,9);
|
||
|
$company[$key]['company_belong'] = 9;
|
||
|
$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;
|
||
|
}
|
||
|
$id = $this->statementWarningInfoModel->where([
|
||
|
'count_year'=>$this->year,
|
||
|
'count_month'=>$this->month,
|
||
|
'company_belong'=>9,
|
||
|
'company_id'=>$key
|
||
|
])->getField("id");
|
||
|
if($id){
|
||
|
$company[$key]['id'] = $id;
|
||
|
$this->statementWarningInfoModel->save($company[$key]);
|
||
|
}else{
|
||
|
$this->statementWarningInfoModel->add($company[$key]);
|
||
|
}
|
||
|
}
|
||
|
return $cpStatementAmount;
|
||
|
}
|
||
|
|
||
|
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;
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
}
|