diff --git a/Application/Admin/Controller/StatementWarningSetController.class.php b/Application/Admin/Controller/StatementWarningSetController.class.php new file mode 100644 index 000000000..d928a00e4 --- /dev/null +++ b/Application/Admin/Controller/StatementWarningSetController.class.php @@ -0,0 +1,428 @@ +'万盟平台', + '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(); + $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() + { + + $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->getPromoteCompanyGameSpend($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 getPromoteCompanyGameSpend($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(); + 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,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 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); + if(!$company) { + $this->addStatementWarning(2,'pu_statement',0); + return; + }; + $company = $this->getPuSpecialStatement( $this->getPromoteCompanyGameSpend($company) ); + foreach ($company as $key => $value) { + if(!array_key_exists('statement_money',$value) ) { + unset($company[$key]); + }; + } + + $puStatementAmount = $this->addStatementWarningInfo($company,0); + $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) + { + $savedata = [ + 'type'=>$type, + 'name'=>$name, + 'count_month'=>$this->month, + 'count_year'=>$this->year + ]; + $hasDb = $this->statementWarningModel->where($savedata)->find(); + $savedata = array_merge(['money'=>$money,'create_time'=>time()],$savedata); + if($hasDb){ + $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; + $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; + + } + + + + + + + +} diff --git a/Application/Admin/Model/CompanyGameRatioModel.class.php b/Application/Admin/Model/CompanyGameRatioModel.class.php index f77e8789d..b0dda5044 100644 --- a/Application/Admin/Model/CompanyGameRatioModel.class.php +++ b/Application/Admin/Model/CompanyGameRatioModel.class.php @@ -112,7 +112,7 @@ class CompanyGameRatioModel extends Model } - protected function getGameRadio($company_id,$game_id,$company_belong,$begintime,$endtime) + public function getGameRadio($company_id,$game_id,$company_belong,$begintime,$endtime) { $map = [ "company_id"=>$company_id, @@ -285,4 +285,26 @@ class CompanyGameRatioModel extends Model ]; } } + + public function getTurnoverRatio($amount,$ratio,$turnover_ratio) + { + if(empty($turnover_ratio)){ + return $ratio; + } + if(!is_array($turnover_ratio)){ + $turnover_ratio = json_decode($turnover_ratio,true); + } + foreach($turnover_ratio as $k=>$v){ + if($v['instanceof'] == 1){ + if($amount >= $v['turnover']){ + $ratio = $v['ratio']; + } + }else{ + if($amount > $v['turnover']){ + $ratio = $v['ratio']; + } + } + } + return $ratio; + } } \ No newline at end of file