DBModel = M("game_margin","tab_"); parent::_initialize(); } public function lists() { $params = I('get.'); $page = $params['p'] ? intval($params['p']) : 1; $row = $params['row'] ? intval($params['row']) : 10; $where = [ "_string"=>"1=1" ]; if(array_key_exists('count_date',$params)){ $m = explode("-",$params['count_date']); $where['count_year']=$m[0]; $where['count_month']=$m[1]; } $data = $this->DBModel->where($where)->field("margin_info",true)->order("count_year desc,count_month desc")->page($page,$row)->select(); // dd($data); $count = $this->DBModel->where($where)->count(); $page = set_pagination($count,$row); if($page) {$this->assign('_page', $page);} $this->assign("data",$data); $this->assign("isCanRecount",D("CmdTasks")->isCanAddTask("GameMargin")); $this->display(); } //查看毛利表 public function marginView(){ if(!isset($_REQUEST['id'])){ $this->error('参数错误'); } $id = $_REQUEST['id']; $info = $this->DBModel->where("id = '{$id}'")->find(); if (isset($_REQUEST['export']) && $_REQUEST['export']==1){ $this->marginExport($info); }else{ list($marginInfo,$count) = $this->marginCount($info); $data = ["list"=>[["marginInfo"=>$marginInfo,"count"=>$count]]]; $this->assign("data",$data); $this->assign("id", $id); $this->display(); } } public function moreMarginView(){ if(!isset($_REQUEST['ids'])) $this->error('参数错误'); $ids = $_REQUEST['ids']; $where = [ "id"=>["in",$ids] ]; $info = $this->DBModel->where($where)->order("count_year asc,count_month asc")->select(); if (isset($_REQUEST['export']) && $_REQUEST['export']==1){ $this->moreMarginExport($info); }else{ $data = ["list"=>[]]; if($info){ foreach ($info as $k => $v) { $tmp = []; list($marginInfo,$count) = $this->marginCount($v); $tmp["marginInfo"] = $marginInfo; $tmp["count"] = $count; $data["list"][] = $tmp; } $data["count"] = $this->marginMoreCount(array_column($data["list"],"count")); } $this->assign("data",$data); $this->display("marginView"); } } private function marginCount($info) { $marginInfo = json_decode($info['margin_info'],true); $count = [ "pay_amount"=>$info['pay_amount'], "margin_ratio"=>$info['margin_ratio'], "margin_amount"=>$info['margin_amount'], "cp_statement_amount"=>0, "cp_ratio"=>0, "cp_rebate_amount"=>0, "cp_other_amount"=>0, "pu_pay_amount"=>0, "pu_statement_amount"=>0, "pu_ratio"=>0, "pu_margin_amount"=>0, "pu_margin_ratio"=>0, "pc_pay_amount"=>0, "pc_statement_amount"=>0, "pc_ratio"=>0, "pc_margin_amount"=>0, "pc_margin_ratio"=>0, "platform_margin_ratio"=>0, "jh_pay_amount"=>0, "jh_statement_amount"=>0, "jh_ratio"=>0, "jh_margin_amount"=>0, "jh_margin_ratio"=>0, ]; foreach ($marginInfo as &$v) { $count['cp_statement_amount'] += $v['cp_statement_amount']; $count['cp_rebate_amount'] += $v['cp_rebate_amount']; $count['cp_other_amount'] += $v['cp_other_amount']; $count['pu_pay_amount'] += $v['pu_pay_amount']; $count['pu_statement_amount'] += $v['pu_statement_amount']; $count['pu_margin_amount'] += $v['pu_margin_amount']; $count['pc_pay_amount'] += $v['pc_pay_amount']; $count['pc_statement_amount'] += $v['pc_statement_amount']; $count['pc_margin_amount'] += $v['pc_margin_amount']; $count['jh_pay_amount'] += $v['jh_pay_amount']; $count['jh_statement_amount'] += $v['jh_statement_amount']; $count['jh_margin_amount'] += $v['jh_margin_amount']; $v['year'] = $info['count_year']; $v['month'] = $info['count_month']; $this->sprintfCell($v); } $count['year'] = $info['count_year']; $count['month'] = $info['count_month']; $count['pu_ratio'] = round( $count['pu_statement_amount']/$count['pu_pay_amount'] ,4)*100; $count['pu_margin_ratio'] = round( $count['pu_margin_amount']/$count['pu_pay_amount'] ,4)*100; $count['pc_ratio'] = round( $count['pc_statement_amount']/$count['pc_pay_amount'] ,4)*100; $count['pc_margin_ratio'] = round( $count['pc_margin_amount']/$count['pc_pay_amount'] ,4)*100; $count['platform_margin_ratio'] = round(($count['pay_amount']-$count['jh_pay_amount']-$count['cp_statement_amount']-$count['cp_rebate_amount']-$count['cp_other_amount']-$count['pc_statement_amount']-$count['pu_statement_amount'])/($count['pay_amount']-$count['jh_pay_amount']),4)*100; $count['jh_ratio'] = round( $count['jh_statement_amount']/$count['jh_pay_amount'] ,4)*100; $count['jh_margin_ratio'] = round( $count['jh_margin_amount']/$count['jh_pay_amount'] ,4)*100; $this->sprintfCell($count); return [$marginInfo,$count]; } private function marginMoreCount($arr) { $count = []; foreach ($arr as $v) { $count['pay_amount'] += $v['pay_amount']; $count['margin_amount'] += $v['margin_amount']; $count['cp_statement_amount'] += $v['cp_statement_amount']; $count['cp_rebate_amount'] += $v['cp_rebate_amount']; $count['cp_other_amount'] += $v['cp_other_amount']; $count['pu_pay_amount'] += $v['pu_pay_amount']; $count['pu_statement_amount'] += $v['pu_statement_amount']; $count['pu_margin_amount'] += $v['pu_margin_amount']; $count['pc_pay_amount'] += $v['pc_pay_amount']; $count['pc_statement_amount'] += $v['pc_statement_amount']; $count['pc_margin_amount'] += $v['pc_margin_amount']; $count['jh_pay_amount'] += $v['jh_pay_amount']; $count['jh_statement_amount'] += $v['jh_statement_amount']; $count['jh_margin_amount'] += $v['jh_margin_amount']; } $count['date'] = "合计"; $count['pu_ratio'] = round( $count['pu_statement_amount']/$count['pu_pay_amount'] ,4)*100; $count['pu_margin_ratio'] = round( $count['pu_margin_amount']/$count['pu_pay_amount'] ,4)*100; $count['pc_ratio'] = round( $count['pc_statement_amount']/$count['pc_pay_amount'] ,4)*100; $count['pc_margin_ratio'] = round( $count['pc_margin_amount']/$count['pc_pay_amount'] ,4)*100; $count['platform_margin_ratio'] = round(($count['pay_amount']-$count['jh_pay_amount']-$count['cp_statement_amount']-$count['cp_rebate_amount']-$count['cp_other_amount']-$count['pc_statement_amount']-$count['pu_statement_amount'])/($count['pay_amount']-$count['jh_pay_amount']),4)*100; $count['jh_ratio'] = round( $count['jh_statement_amount']/$count['jh_pay_amount'] ,4)*100; $count['jh_margin_ratio'] = round( $count['jh_margin_amount']/$count['jh_pay_amount'] ,4)*100; $count['margin_ratio'] = round( $count['margin_amount']/$count['pay_amount'] ,4)*100; $this->sprintfCell($count); return $count; } private function moreMarginExport($info) { $line = 3; $dateBing = 3; $excelData = []; $countLineArr = []; foreach ($info as $index => $date) { $marginInfo = json_decode($date['margin_info'],true); foreach ($marginInfo as $k=>$v) { // $v["cp_ratio"] = "=ROUND(G{$line}/D{$line},4)"; if($k == 0){ $dateBing = $line; } if($v["pc_pay_amount"] > 0){ $v["pc_ratio"] ="=ROUND(M{$line}/K{$line},2)"; //K3*(1-(1-E3)*F3-L3) $v["pc_margin_amount"] = "=ROUND((K{$line}*(1-(1-E{$line})*F{$line}-L{$line})),2)"; $v["pc_margin_ratio"] ="=ROUND(N{$line}/K{$line},3)"; } if ($v["pu_pay_amount"] > 0) { $v["pu_ratio"] ="=ROUND(R{$line}/P{$line},2)"; $v["pu_margin_amount"] = "=ROUND((P{$line}-P{$line}*(1-E{$line})*(F{$line}+H{$line})-R{$line}),2)"; $v["pu_margin_ratio"] = "=ROUND(S{$line}/P{$line},3)"; } $v['platform_margin_ratio'] = "=ROUND((D{$line}-V{$line}-G{$line}-I{$line}-J{$line}-M{$line}-R{$line})/(D{$line}-V{$line}),3)"; if($v['jh_pay_amount'] > 0){ $v["jh_ratio"] ="=ROUND(X{$line}/V{$line},2)"; $v["jh_margin_amount"] = "=ROUND((X{$line}-V{$line}*(1-E{$line})*(F{$line}+H{$line})),2)"; $v["jh_margin_ratio"] = "=ROUND(Y{$line}/V{$line},3)"; } $v['margin_amount'] = "=ROUND((D{$line}-G{$line}-I{$line}-J{$line}-M{$line}-R{$line}-(V{$line}-X{$line})),2)"; $v['margin_ratio'] = "=ROUND(AB{$line}/D{$line},3)"; $v['count_year'] = $date['count_year']; $v['count_month'] = $date['count_month']; $v['cp_promote_ratio'] = $v['cp_promote_ratio']/100; $v['cp_ratio'] = $v['cp_ratio']/100; $v['cp_rebate_ratio'] = $v['cp_rebate_ratio']/100; $excelData[] = $v; $line ++; } $countLineArr[] = $line; //计算统计 $countLine = $line; $line--; $excelData[] = [ "count_year" => $date['count_year'], 'count_month' => $date['count_month'], 'relation_game_name'=>"小计", "pay_amount"=>"=SUM(D{$dateBing}:D{$line})", "cp_statement_amount"=>"=SUM(G{$dateBing}:G{$line})", "cp_rebate_amount"=>"=SUM(I{$dateBing}:I{$line})", "cp_other_amount"=>"=SUM(J{$dateBing}:J{$line})", "pc_pay_amount"=>"=SUM(K{$dateBing}:K{$line})", "pc_ratio"=>"=ROUND(M{$countLine}/K{$countLine},4)", "pc_statement_amount"=>"=SUM(M{$dateBing}:M{$line})", "pc_margin_amount"=>"=SUM(N{$dateBing}:N{$line})", "pc_margin_ratio"=>"=ROUND(N{$countLine}/K{$countLine},4)", "pu_pay_amount"=>"=SUM(P{$dateBing}:P{$line})", "pu_statement_amount"=>"=SUM(R{$dateBing}:R{$line})", "pu_ratio"=>"=ROUND(R{$countLine}/P{$countLine},4)", "pu_margin_amount"=>"=SUM(S{$dateBing}:S{$line})", "pu_margin_ratio"=>"=ROUND(S{$countLine}/P{$countLine},4)", "platform_margin_ratio"=>"=ROUND((D{$countLine}-V{$countLine}-G{$countLine}-I{$countLine}-J{$countLine}-M{$countLine}-R{$countLine})/(D{$countLine}-V{$countLine}),4)", "jh_pay_amount"=>"=SUM(V{$dateBing}:V{$line})", "jh_statement_amount"=>"=SUM(X{$dateBing}:X{$line})", "jh_ratio"=>"=ROUND(X{$countLine}/V{$countLine},4)", "jh_margin_amount"=>"=SUM(Y{$dateBing}:Y{$line})", "jh_margin_ratio"=>"=ROUND(Y{$countLine}/V{$countLine},4)", "margin_ratio"=>"=ROUND(AB{$countLine}/D{$countLine},4)", "margin_amount"=>"=SUM(AB{$dateBing}:AB{$line})", ]; $line += 2; } $countLine = $line; $line--; //设置总计 $allCountData = [ "count_year" => "总计", "pay_amount"=>"=SUM(", "cp_statement_amount"=>"=SUM(", "cp_rebate_amount"=>"=SUM(", "cp_other_amount"=>"=SUM(", "pc_pay_amount"=>"=SUM(", "pc_ratio"=>"=ROUND(M{$countLine}/K{$countLine},4)", "pc_statement_amount"=>"=SUM(", "pc_margin_amount"=>"=SUM(", "pc_margin_ratio"=>"=ROUND(N{$countLine}/K{$countLine},4)", "pu_pay_amount"=>"=SUM(", "pu_statement_amount"=>"=SUM(", "pu_ratio"=>"=ROUND(R{$countLine}/P{$countLine},4)", "pu_margin_amount"=>"=SUM(", "pu_margin_ratio"=>"=ROUND(S{$countLine}/P{$countLine},4)", "platform_margin_ratio"=>"=ROUND((D{$countLine}-V{$countLine}-G{$countLine}-I{$countLine}-J{$countLine}-M{$countLine}-R{$countLine})/(D{$countLine}-V{$countLine}),4)", "jh_pay_amount"=>"=SUM(", "jh_statement_amount"=>"=SUM(", "jh_ratio"=>"=ROUND(X{$countLine}/V{$countLine},4)", "jh_margin_amount"=>"=SUM(", "jh_margin_ratio"=>"=ROUND(Y{$countLine}/V{$countLine},4)", "margin_ratio"=>"=ROUND(AB{$countLine}/D{$countLine},4)", "margin_amount"=>"=SUM(", ]; for ($i=0; $i < count($countLineArr); $i++) { $allCountData['pay_amount'] .= ("D". $countLineArr[$i].","); $allCountData['cp_statement_amount'] .= ("G". $countLineArr[$i].","); $allCountData['cp_rebate_amount'] .= ("I". $countLineArr[$i].","); $allCountData['cp_other_amount'] .= ("J". $countLineArr[$i].","); $allCountData['pc_pay_amount'] .= ("K". $countLineArr[$i].","); $allCountData['pc_statement_amount'] .= ("M". $countLineArr[$i].","); $allCountData['pc_margin_amount'] .= ("N". $countLineArr[$i].","); $allCountData['pu_pay_amount'] .= ("P". $countLineArr[$i].","); $allCountData['pu_statement_amount'] .= ("R". $countLineArr[$i].","); $allCountData['pu_margin_amount'] .= ("S". $countLineArr[$i].","); $allCountData['jh_pay_amount'] .= ("V". $countLineArr[$i].","); $allCountData['jh_statement_amount'] .= ("X". $countLineArr[$i].","); $allCountData['jh_margin_amount'] .= ("Y". $countLineArr[$i].","); $allCountData['margin_amount'] .= ("AB". $countLineArr[$i].","); } $allCountData['pay_amount'] = rtrim($allCountData['pay_amount'], ",").")"; $allCountData['cp_statement_amount'] = rtrim($allCountData['cp_statement_amount'], ",").")"; $allCountData['cp_rebate_amount'] = rtrim($allCountData['cp_rebate_amount'], ",").")"; $allCountData['cp_other_amount'] = rtrim($allCountData['cp_other_amount'], ",").")"; $allCountData['pc_pay_amount'] = rtrim($allCountData['pc_pay_amount'], ",").")"; $allCountData['pc_statement_amount'] = rtrim($allCountData['pc_statement_amount'], ",").")"; $allCountData['pc_margin_amount'] = rtrim($allCountData['pc_margin_amount'], ",").")"; $allCountData['pu_pay_amount'] = rtrim($allCountData['pu_pay_amount'], ",").")"; $allCountData['pu_statement_amount'] = rtrim($allCountData['pu_statement_amount'], ",").")"; $allCountData['pu_margin_amount'] = rtrim($allCountData['pu_margin_amount'], ",").")"; $allCountData['jh_pay_amount'] = rtrim($allCountData['jh_pay_amount'], ",").")"; $allCountData['jh_statement_amount'] = rtrim($allCountData['jh_statement_amount'], ",").")"; $allCountData['jh_margin_amount'] = rtrim($allCountData['jh_margin_amount'], ",").")"; $allCountData['margin_amount'] = rtrim($allCountData['margin_amount'], ",").")"; $excelData[] = $allCountData; // dd($excelData); $this->exportExcel($excelData,"毛利分析表汇总"); } private function exportExcel($arr,$title) { header("Content-type: text/html; charset=utf-8"); error_reporting(E_ALL); ini_set('display_errors', TRUE); ini_set('display_startup_errors', TRUE); Vendor("PHPExcel.PHPExcel"); $objReader = \PHPExcel_IOFactory::createReader('Excel2007'); $objPHPExcel = $objReader->load("Public/Admin/excel/gamemargin.xlsx"); $line = 3; $objPHPExcel->getActiveSheet()->insertNewRowBefore($line+1,count($arr)-2); foreach ($arr as $v) { //relation_game_name $objPHPExcel->getActiveSheet() ->setCellValue('A'.$line, $v['count_year']) ->setCellValue('B'.$line, $v['count_month'] ?? '') ->setCellValue('C'.$line, $v['relation_game_name'] ?? '') ->setCellValue('D'.$line, $v['pay_amount']) ->setCellValue('E'.$line, $v['cp_promote_ratio'] ?? '') ->setCellValue('F'.$line, $v['cp_ratio'] ?? '') ->setCellValue('G'.$line, $v['cp_statement_amount']) ->setCellValue('H'.$line, $v['cp_rebate_ratio'] ?? '') ->setCellValue('I'.$line, $v['cp_rebate_amount']) ->setCellValue('J'.$line, $v['cp_other_amount']) ->setCellValue('K'.$line, $v['pc_pay_amount']) ->setCellValue('L'.$line, $v['pc_ratio']) ->setCellValue('M'.$line, $v['pc_statement_amount']) ->setCellValue('N'.$line, $v['pc_margin_amount']) ->setCellValue('O'.$line, $v['pc_margin_ratio']) ->setCellValue('P'.$line, $v['pu_pay_amount']) ->setCellValue('Q'.$line, $v['pu_ratio']) ->setCellValue('R'.$line, $v['pu_statement_amount']) ->setCellValue('S'.$line, $v['pu_margin_amount']) ->setCellValue('T'.$line, $v['pu_margin_ratio']) ->setCellValue('U'.$line, $v['platform_margin_ratio']) ->setCellValue('V'.$line, $v['jh_pay_amount']) ->setCellValue('W'.$line, $v['jh_ratio']) ->setCellValue('X'.$line, $v['jh_statement_amount']) ->setCellValue('Y'.$line, $v['jh_margin_amount']) ->setCellValue('Z'.$line, $v['jh_margin_ratio']) ->setCellValue('AA'.$line, $v['margin_ratio']) ->setCellValue('AB'.$line, $v['margin_amount']); if(array_key_exists('relation_game_name',$v)){ if($v['relation_game_name'] == "小计"){ $objPHPExcel->getActiveSheet()->getStyle("A{$line}:AB{$line}")->getFont()->setBold(true); } }else{ //总计 $objPHPExcel->getActiveSheet()->mergeCells("A{$line}:C{$line}")->getStyle("A{$line}")->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); } $line ++; } $fileName = $title; ob_end_clean();//清除缓冲区,避免乱码 header('pragma:public'); header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$fileName.'".xlsx'); header("Content-Disposition:attachment;filename={$fileName}.xlsx");//attachment新窗口打印inline本窗口打印 $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save('php://output'); exit; } private function marginExport($info) { header("Content-type: text/html; charset=utf-8"); error_reporting(E_ALL); ini_set('display_errors', TRUE); ini_set('display_startup_errors', TRUE); define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '
'); Vendor("PHPExcel.PHPExcel"); $objReader = \PHPExcel_IOFactory::createReader('Excel2007'); //设置模板文件 $objPHPExcel = $objReader->load("Public/Admin/excel/gamemargin.xlsx"); $marginInfo = json_decode($info['margin_info'],true); $line = 3; $objPHPExcel->getActiveSheet()->insertNewRowBefore($line+1,count($marginInfo)-1); foreach ($marginInfo as &$v) { // $v["cp_ratio"] = "=ROUND(G{$line}/D{$line},4)"; if($v["pc_pay_amount"] > 0){ $v["pc_ratio"] ="=ROUND(M{$line}/K{$line},2)"; //K3*(1-(1-E3)*F3-L3) $v["pc_margin_amount"] = "=ROUND((K{$line}*(1-(1-E{$line})*F{$line}-L{$line})),2)"; $v["pc_margin_ratio"] ="=ROUND(N{$line}/K{$line},3)"; } if ($v["pu_pay_amount"] > 0) { $v["pu_ratio"] ="=ROUND(R{$line}/P{$line},2)"; $v["pu_margin_amount"] = "=ROUND((P{$line}-P{$line}*(1-E{$line})*(F{$line}+H{$line})-R{$line}),2)"; $v["pu_margin_ratio"] = "=ROUND(S{$line}/P{$line},3)"; } $v['platform_margin_ratio'] = "=ROUND((D{$line}-V{$line}-G{$line}-I{$line}-J{$line}-M{$line}-R{$line})/(D{$line}-V{$line}),3)"; if($v['jh_pay_amount'] > 0){ $v["jh_ratio"] ="=ROUND(X{$line}/V{$line},2)"; $v["jh_margin_amount"] = "=ROUND((X{$line}-V{$line}*(1-E{$line})*(F{$line}+H{$line})),2)"; $v["jh_margin_ratio"] = "=ROUND(Y{$line}/V{$line},3)"; } $v['margin_amount'] = "=ROUND((D{$line}-G{$line}-I{$line}-J{$line}-M{$line}-R{$line}-(V{$line}-X{$line})),2)"; $v['margin_ratio'] = "=ROUND(AB{$line}/D{$line},3)"; $objPHPExcel->getActiveSheet() ->setCellValue('A'.$line, $info['count_year']) ->setCellValue('B'.$line, $info['count_month']) ->setCellValue('C'.$line, $v['relation_game_name']) ->setCellValue('D'.$line, $v['pay_amount']) ->setCellValue('E'.$line, $v['cp_promote_ratio']/100) ->setCellValue('F'.$line, $v['cp_ratio']/100) ->setCellValue('G'.$line, $v['cp_statement_amount']) ->setCellValue('H'.$line, $v['cp_rebate_ratio']/100) ->setCellValue('I'.$line, $v['cp_rebate_amount']) ->setCellValue('J'.$line, $v['cp_other_amount']) ->setCellValue('K'.$line, $v['pc_pay_amount']) ->setCellValue('L'.$line, $v['pc_ratio']) ->setCellValue('M'.$line, $v['pc_statement_amount']) ->setCellValue('N'.$line, $v['pc_margin_amount']) ->setCellValue('O'.$line, $v['pc_margin_ratio']) ->setCellValue('P'.$line, $v['pu_pay_amount']) ->setCellValue('Q'.$line, $v['pu_ratio']) ->setCellValue('R'.$line, $v['pu_statement_amount']) ->setCellValue('S'.$line, $v['pu_margin_amount']) ->setCellValue('T'.$line, $v['pu_margin_ratio']) ->setCellValue('U'.$line, $v['platform_margin_ratio']) ->setCellValue('V'.$line, $v['jh_pay_amount']) ->setCellValue('W'.$line, $v['jh_ratio']) ->setCellValue('X'.$line, $v['jh_statement_amount']) ->setCellValue('Y'.$line, $v['jh_margin_amount']) ->setCellValue('Z'.$line, $v['jh_margin_ratio']) ->setCellValue('AA'.$line, $v['margin_ratio']) ->setCellValue('AB'.$line, $v['margin_amount']); $line ++; } $countLine = $line; $line--; $count = [ "pay_amount"=>"=SUM(D3:D{$line})", // "cp_ratio"=>"=ROUND(G{$countLine}/D{$countLine},4)", "cp_statement_amount"=>"=SUM(G3:G{$line})", "cp_rebate_amount"=>"=SUM(I3:I{$line})", "cp_other_amount"=>"=SUM(J3:J{$line})", "pc_pay_amount"=>"=SUM(K3:K{$line})", "pc_ratio"=>"=ROUND(M{$countLine}/K{$countLine},4)", "pc_statement_amount"=>"=SUM(M3:M{$line})", "pc_margin_amount"=>"=SUM(N3:N{$line})", "pc_margin_ratio"=>"=ROUND(N{$countLine}/K{$countLine},4)", "pu_pay_amount"=>"=SUM(P3:P{$line})", "pu_statement_amount"=>"=SUM(R3:R{$line})", "pu_ratio"=>"=ROUND(R{$countLine}/P{$countLine},4)", "pu_margin_amount"=>"=SUM(S3:S{$line})", "pu_margin_ratio"=>"=ROUND(S{$countLine}/P{$countLine},4)", "platform_margin_ratio"=>"=ROUND((D{$countLine}-V{$countLine}-G{$countLine}-I{$countLine}-J{$countLine}-M{$countLine}-R{$countLine})/(D{$countLine}-V{$countLine}),4)", "jh_pay_amount"=>"=SUM(V3:V{$line})", "jh_statement_amount"=>"=SUM(X3:X{$line})", "jh_ratio"=>"=ROUND(X{$countLine}/V{$countLine},4)", "jh_margin_amount"=>"=SUM(Y3:Y{$line})", "jh_margin_ratio"=>"=ROUND(Y{$countLine}/V{$countLine},4)", "margin_ratio"=>"=ROUND(AB{$countLine}/D{$countLine},4)", "margin_amount"=>"=SUM(AB3:AB{$line})", ]; $line ++; $objPHPExcel->getActiveSheet() ->setCellValue('A'.$line, $info['count_year']) ->setCellValue('B'.$line, $info['count_month']) ->setCellValue('C'.$line, "小计") ->setCellValue('D'.$line, $count['pay_amount']) ->setCellValue('G'.$line, $count['cp_statement_amount']) ->setCellValue('I'.$line, $count['cp_rebate_amount']) ->setCellValue('J'.$line, $count['cp_other_amount']) ->setCellValue('K'.$line, $count['pc_pay_amount']) ->setCellValue('L'.$line, $count['pc_ratio']) ->setCellValue('M'.$line, $count['pc_statement_amount']) ->setCellValue('N'.$line, $count['pc_margin_amount']) ->setCellValue('O'.$line, $count['pc_margin_ratio']) ->setCellValue('P'.$line, $count['pu_pay_amount']) ->setCellValue('Q'.$line, $count['pu_ratio']) ->setCellValue('R'.$line, $count['pu_statement_amount']) ->setCellValue('S'.$line, $count['pu_margin_amount']) ->setCellValue('T'.$line, $count['pu_margin_ratio']) ->setCellValue('U'.$line, $count['platform_margin_ratio']) ->setCellValue('V'.$line, $count['jh_pay_amount']) ->setCellValue('W'.$line, $count['jh_ratio']) ->setCellValue('X'.$line, $count['jh_statement_amount']) ->setCellValue('Y'.$line, $count['jh_margin_amount']) ->setCellValue('Z'.$line, $count['jh_margin_ratio']) ->setCellValue('AA'.$line, $count['margin_ratio']) ->setCellValue('AB'.$line, $count['margin_amount']); $fileName = "{$info['count_year']}-{$info['count_month']}毛利分析表"; ob_end_clean();//清除缓冲区,避免乱码 header('pragma:public'); header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$fileName.'".xlsx'); header("Content-Disposition:attachment;filename={$fileName}.xlsx");//attachment新窗口打印inline本窗口打印 $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save('php://output'); exit; } /** * 编辑 */ public function marginEdit(){ ini_set('serialize_precision',14); if(!isset($_REQUEST['id'])){ $this->error('参数错误'); } $params = I("post."); $data = $this->DBModel->where("id='{$params['id']}'")->find(); $marginInfo = json_decode($data['margin_info'],true); $item = &$marginInfo[$params['key']]; $item['cp_rebate_ratio'] = $params['cp_rebate_ratio']; $item['cp_rebate_amount'] = $params['cp_rebate_amount']; $item['cp_other_amount'] = $params['cp_other_amount']; if($item["pc_pay_amount"] > 0){ $item["pc_margin_amount"] = round($item["pc_pay_amount"]*(1-(100-$item['cp_promote_ratio'])/100*$item['cp_ratio']/100-$item['pc_ratio']/100),2); $item["pc_margin_ratio"] = round($item["pc_margin_amount"]/$item['pc_pay_amount'],4)*100; } if ($item["pu_pay_amount"] > 0) { $item["pu_margin_amount"] = round($item["pu_pay_amount"]-$item["pu_pay_amount"]*(1-$item['cp_promote_ratio']/100)*($item["cp_ratio"]+$item['cp_rebate_ratio'])/100-$item["pu_statement_amount"],2); $item["pu_margin_ratio"] = round($item["pu_margin_amount"]/$item['pu_pay_amount'],4)*100; } $item['platform_margin_ratio'] = round(($item['pay_amount']-$item['jh_pay_amount']-$item['cp_statement_amount']-$item['cp_rebate_amount']-$item['cp_other_amount']-$item['pc_statement_amount']-$item['pu_statement_amount'])/($item['pay_amount']-$item['jh_pay_amount']),4)*100; if($item['jh_pay_amount'] > 0){ $item["jh_margin_amount"] = round( $item["jh_statement_amount"]-$item["jh_pay_amount"]*(1-$item['cp_promote_ratio']/100)*($item["cp_ratio"]+$item['cp_rebate_ratio'])/100 ,2); $item["jh_margin_ratio"] = round($item["jh_margin_amount"]/$item['jh_pay_amount'],4)*100; } $item['margin_amount'] = round( ($item['pay_amount']-$item['cp_statement_amount']-$item['cp_rebate_amount']-$item['cp_other_amount']-$item['pc_statement_amount']-$item['pu_statement_amount']-($item['jh_pay_amount'] - $item['jh_statement_amount'])),2); $item['margin_ratio'] = round($item['margin_amount']/$item['pay_amount'],4)*100; $margin_amount = 0; //总计 foreach ($marginInfo as &$v) { $margin_amount += $v['margin_amount']; } $margin_ratio = round( $margin_amount/$data['pay_amount'] ,4)*100; $save = [ "margin_ratio"=>$margin_ratio, "margin_amount"=>$margin_amount, "margin_info"=>json_encode($marginInfo,JSON_UNESCAPED_UNICODE) ]; $this->DBModel->where("id='{$params['id']}'")->save($save); $this->ajaxReturn(['status'=>1,'msg'=>"修改成功,刷新。。。"]); } public function recount() { if(!isset($_REQUEST['type'])) $this->error("参数错误"); $type = $_REQUEST['type']; if($type == "recount"){ if(!isset($_REQUEST['id'])) $this->error("参数错误"); $id = $_REQUEST['id']; $data = $this->DBModel->field("count_year,count_month")->where("id='{$id}'")->find(); $time = $data['count_year']."-".$data['count_month']; $this->DBModel->where("id='{$id}'")->delete(); }elseif($type == "add"){ $time =$_REQUEST['date']; $m = explode("-",$time); $where = []; $where['count_year']=$m[0]; $where['count_month']=$m[1]; $count = $this->DBModel->where($where)->count(); if($count > 0){ $this->ajaxReturn(["status"=>"0","msg"=>"{$time}已经生成报表,请重算即可"]); } } $params = "php ".SUBSITE_INDEX." GameMarginSet/setFreeMonth/recount/1/count_date/{$time}"; $r = D("CmdTasks")->addTask("GameMargin",$params,true); if($r){ $this->ajaxReturn(["status"=>"1",'msg'=>"任务添加成功,大约需等待5分钟后生成"]); }else{ $this->ajaxReturn(["status"=>"0","msg"=>'任务添加失败']); } } /** * 格式化数据 */ private function sprintfCell(&$item) { $item['pay_amount'] = sprintf("%.2f",$item['pay_amount']); $item['pu_pay_amount'] = sprintf("%.2f",$item['pu_pay_amount']); $item['pc_pay_amount'] = sprintf("%.2f",$item['pc_pay_amount']); $item['jh_pay_amount'] = sprintf("%.2f",$item['jh_pay_amount']); $item['cp_statement_amount'] = sprintf("%.2f",$item['cp_statement_amount']); $item['pu_statement_amount'] = sprintf("%.2f",$item['pu_statement_amount']); $item['pc_statement_amount'] = sprintf("%.2f",$item['pc_statement_amount']); $item['jh_statement_amount'] = sprintf("%.2f",$item['jh_statement_amount']); $item['margin_amount'] = sprintf("%.2f",$item['margin_amount']); $item['pu_margin_amount'] = sprintf("%.2f",$item['pu_margin_amount']); $item['pc_margin_amount'] = sprintf("%.2f",$item['pc_margin_amount']); $item['jh_margin_amount'] = sprintf("%.2f",$item['jh_margin_amount']); $item['cp_other_amount'] = sprintf("%.2f",$item['cp_other_amount']); $item['cp_rebate_amount'] = sprintf("%.2f",$item['cp_rebate_amount']); if(array_key_exists('cp_ratio',$item)){ $item['cp_ratio'] = sprintf("%.0f%%",$item['cp_ratio']); $item['cp_rebate_ratio'] = sprintf("%.0f%%",$item['cp_rebate_ratio']); } $item['pc_ratio'] = sprintf("%.0f%%",round($item['pc_ratio'],0)); $item['pu_ratio'] = sprintf("%.0f%%",round($item['pu_ratio'],0)); $item['jh_ratio'] = sprintf("%.0f%%",round($item['jh_ratio'],0)); $item['margin_ratio'] = sprintf("%.1f%%",round($item['margin_ratio'],1)); $item['pu_margin_ratio'] = sprintf("%.1f%%",round($item['pu_margin_ratio'],1)); $item['pc_margin_ratio'] = sprintf("%.1f%%",round($item['pc_margin_ratio'],1)); $item['jh_margin_ratio'] = sprintf("%.1f%%",round($item['jh_margin_ratio'],1)); $item['platform_margin_ratio'] = sprintf("%.1f%%",round($item['platform_margin_ratio'],1)); } }