'万盟平台', 'up_statement'=>'上游', 'pc_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(); $this->pcStatement(); 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); } $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->getPcGameSpend($company); $pcStatementAmount = $this->addStatementWarningInfo($company,0); $savedata = [ 'type'=>2, 'name'=>'pc_statement', 'count_month'=>$this->month, 'count_year'=>$this->year ]; $hasDb = $this->statementWarningModel->where($savedata)->find(); $savedata = array_merge(['money'=>$pcStatementAmount,'create_time'=>time()],$savedata); if($hasDb){ $savedata['id'] = $hasDb['id']; $this->statementWarningModel->save($savedata); }else{ $this->statementWarningModel->add($savedata); } } private function getPcGameSpend($company) { $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(); $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,0,$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 addStatementWarningInfo($company,$companyBelong) { $statementAmount = 0; foreach ($company as $key => $value) { $statementMoney = $value['statement_money']-0; $statementAmount += $statementMoney; $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; } $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; } }