优化批量导出

master
chenzhi 4 years ago
parent 3b725f9c1d
commit f211fe6fa7

@ -60,6 +60,10 @@ class GameMarginController extends AdminController
"id"=>["in",$ids] "id"=>["in",$ids]
]; ];
$info = $this->DBModel->where($where)->order("count_year asc,count_month asc")->select(); $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"=>[]]; $data = ["list"=>[]];
if($info){ if($info){
foreach ($info as $k => $v) { foreach ($info as $k => $v) {
@ -69,13 +73,12 @@ class GameMarginController extends AdminController
$tmp["count"] = $count; $tmp["count"] = $count;
$data["list"][] = $tmp; $data["list"][] = $tmp;
} }
//全部统计
$data["count"] = $this->marginMoreCount(array_column($data["list"],"count")); $data["count"] = $this->marginMoreCount(array_column($data["list"],"count"));
} }
$this->assign("data",$data); $this->assign("data",$data);
$this->display("marginView"); $this->display("marginView");
} }
}
private function marginCount($info) private function marginCount($info)
{ {
@ -165,6 +168,221 @@ class GameMarginController extends AdminController
$this->sprintfCell($count); $this->sprintfCell($count);
return $count; return $count;
} }
private function moreMarginExport($info)
{
$line = 3;
$excelData = [];
$countLineArr = [];
foreach ($info as $index => $date) {
$marginInfo = json_decode($date['margin_info'],true);
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)";
$v["pc_margin_amount"] = "=ROUND((K{$line}*(1-E{$line})*(1-F{$line}-H{$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}+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(D3:D{$line})",
"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 += 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;
$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) private function marginExport($info)
{ {
@ -278,6 +496,7 @@ class GameMarginController extends AdminController
$objPHPExcel->getActiveSheet() $objPHPExcel->getActiveSheet()
->setCellValue('A'.$line, $info['count_year']) ->setCellValue('A'.$line, $info['count_year'])
->setCellValue('B'.$line, $info['count_month']) ->setCellValue('B'.$line, $info['count_month'])
->setCellValue('C'.$line, "小计")
->setCellValue('D'.$line, $count['pay_amount']) ->setCellValue('D'.$line, $count['pay_amount'])
->setCellValue('G'.$line, $count['cp_statement_amount']) ->setCellValue('G'.$line, $count['cp_statement_amount'])
->setCellValue('I'.$line, $count['cp_rebate_amount']) ->setCellValue('I'.$line, $count['cp_rebate_amount'])

@ -86,6 +86,7 @@
</if> </if>
<a class='butn' id='export'>导出</a> <a class='butn' id='export'>导出</a>
<a class='butn' id='moreMarginView'>汇总查看</a> <a class='butn' id='moreMarginView'>汇总查看</a>
<a class='butn' id='moreMarginExport'>汇总导出</a>
</div> </div>
</div> </div>
@ -297,6 +298,21 @@
}); });
}) })
$("#moreMarginExport").click(function () {
var text = $("input:checkbox[name='ids[]']:checked").map(function(index,elem) {
return $(elem).val();
}).get();
if(text.length < 2){
layer.msg("<font style='color:white'>" + '请选择大于两条(含)以上的数据' + "</font>");
return;
}
var ids = text.join(",");
var url = "{:U('moreMarginView')}"+"&export=1&ids="+ids
var iframeExcel = "<iframe src='"+url+"' width='0px' height='0px' style='z-index:-1;position: absolute;top: -999999px;'></iframe>"//添加下载的地址到iframe这里是公司信息我用**表示了。
$("body").append(iframeExcel)
})
$("#search").click(function(){ $("#search").click(function(){
var url = $(this).attr('url'); var url = $(this).attr('url');

Loading…
Cancel
Save