123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- <?php
- defined('BASEPATH') OR exit('No direct script access allowed');
- /**
- * Class 统计报表类
- */
- class Report extends MY_Controller
- {
- function __construct()
- {
- parent::__construct();
- $this->load->helper(array('form', 'date'));
- $this->load->model("workorder_model");
- }
- /**
- * 统计报表首页
- */
- public function index()
- {
- $sign = $this->input->get("sign", true);
- $begin_date = $this->input->post("begin_date", true);
- $end_date = $this->input->post("end_date", true);
- $begin_time = " 0:0:0";
- $end_time = " 23:59:59";
- $now = new DateTime();
- $end = $now->format("Y-m-d");
- switch ($sign) {
- case '1':
- $title = "近7天";
- $end_date_time = new DateTime($end . $end_time);
- $end_datetime = new MongoDB\BSON\UTCDateTime($end_date_time->getTimestamp()*1000);
- $now = new DateTime();
- $now->modify("-6 day");
- $begin = $now->format("Y-m-d");
- $begin_date_time = new DateTime($begin . $begin_time);
- $begin_datetime = new \MongoDB\BSON\UTCDateTime($begin_date_time->getTimestamp()*1000);
- break;
- case '2':
- $title = "近30天";
- $end_date_time = new DateTime($end . $end_time);
- $end_datetime = new MongoDB\BSON\UTCDateTime($end_date_time->getTimestamp()*1000);
- $now = new DateTime();
- $now->modify("-30 day");
- $begin = $now->format("Y-m-d");
- $begin_date_time = new DateTime($begin . $begin_time);
- $begin_datetime = new \MongoDB\BSON\UTCDateTime($begin_date_time->getTimestamp()*1000);
- break;
- case '3':
- $title = "近三个月";
- $end_date_time = new DateTime($end . $end_time);
- $end_datetime = new MongoDB\BSON\UTCDateTime($end_date_time->getTimestamp()*1000);
- $now = new DateTime();
- $now->modify("-3 month");
- $begin = $now->format("Y-m-d");
- $begin_date_time = new DateTime($begin . $begin_time);
- $begin_datetime = new \MongoDB\BSON\UTCDateTime($begin_date_time->getTimestamp()*1000);
- break;
- case '4':
- $title = "近半年";
- $end_date_time = new DateTime($end . $end_time);
- $end_datetime = new MongoDB\BSON\UTCDateTime($end_date_time->getTimestamp()*1000);
- $now = new DateTime();
- $now->modify("-6 month");
- $begin = $now->format("Y-m-d");
- $begin_date_time = new DateTime($begin . $begin_time);
- $begin_datetime = new \MongoDB\BSON\UTCDateTime($begin_date_time->getTimestamp()*1000);
- break;
- case '5':
- $title = "近一年";
- $end_date_time = new DateTime($end . $end_time);
- $end_datetime = new MongoDB\BSON\UTCDateTime($end_date_time->getTimestamp()*1000);
- $now = new DateTime();
- $now->modify("-1 year");
- $begin = $now->format("Y-m-d");
- $begin_date_time = new DateTime($begin . $begin_time);
- $begin_datetime = new \MongoDB\BSON\UTCDateTime($begin_date_time->getTimestamp()*1000);
- break;
- default:
- $title = "近7天";
- $end_date_time = new DateTime($end . $end_time);
- $end_datetime = new MongoDB\BSON\UTCDateTime($end_date_time->getTimestamp()*1000);
- $now = new DateTime();
- $now->modify("-6 day");
- $begin = $now->format("Y-m-d");
- $begin_date_time = new DateTime($begin . $begin_time);
- $begin_datetime = new \MongoDB\BSON\UTCDateTime($begin_date_time->getTimestamp()*1000);
- }
- if ($begin_date && $end_date) {
- $title = "自" . $begin_date . "至" . $end_date;
- $end_date_time = new DateTime($end_date . $end_time);
- $begin_date_time = new DateTime($begin_date . $begin_time);
- $end_datetime = new MongoDB\BSON\UTCDateTime($end_date_time->getTimestamp()*1000);
- $begin_datetime = new \MongoDB\BSON\UTCDateTime($begin_date_time->getTimestamp()*1000);
- }
- /*图表1数据封装 各个状态的工单分布*/
- $group1 = array(
- "_id" => "\$status",
- "value" => array("\$sum" => 1)
- );
- $result1 = $this->workorder_model->aggregate($begin_datetime, $end_datetime, $group1,NULL,array("status"=>array("\$ne"=>"60")));
- $data1 = array();
- if (is_array($result1) && count($result1) >= 1) {
- foreach ($result1 as $key => $val) {
- $data1[] = array(
- "name" => $this->workorder_status[$val['_id']],
- "value" => $val['value']
- );
- }
- }
- /*图表2数据封装 各分支工单总数*/
- $group2 = array(
- "_id" => "\$branch.branch_id",
- "value" => array("\$sum" => 1)
- );
- $result2 = $this->workorder_model->aggregate($begin_datetime, $end_datetime, $group2,NULL,array("status"=>array("\$ne"=>"60")));
- $data2x = array();
- $data2y = array();
- if (is_array($result2) && count($result2) >= 1) {
- foreach ($this->branch_array as $key => $branch) {
- $data2x[] = $branch['branch_name'];
- $have_value = 0;
- foreach ($result2 as $key => $val) {
- if ($val['_id'] == $branch['branch_id']) {
- $data2y[] = $val['value'];
- $have_value = 1;
- }
- }
- if (!$have_value) {
- $data2y[] = 0;
- }
- }
- }
- /*图表3数据封装 各分支工单状态数量*/
- $group3 = array(
- "_id" => array("branchs" => "\$branch.branch_id", "status" => "\$status"),
- "value" => array("\$sum" => 1)
- );
- $result3 = $this->workorder_model->aggregate($begin_datetime, $end_datetime, $group3,NULL,array("status"=>array("\$ne"=>"60")));
- $data3x = array();
- $data3y = array();
- $data3legend = array();
- $i = 0;
- foreach ($this->branch_array as $key => $branch) {
- $data3x[] = $branch['branch_name'];
- }
- foreach ($this->workorder_status as $k => $status) {
- if($k == "60") continue;
- $data = array();
- $data['name'] = $status;
- $data['type'] = "bar";
- $data['barGap'] = 0;
- $data3y[] = $data;
- $data3legend[] = $status;
- }
- $i = 0;
- foreach ($this->workorder_status as $k => $status) {
- if($k == "60") continue;
- $value = array();
- foreach ($this->branch_array as $key => $branch) {
- $have_value = 0;
- foreach ($result3 as $result) {
- if ($result['_id']['branchs'] == $branch['branch_id'] && $result['_id']['status'] == $k) {
- $value[] = $result['value'];
- $have_value = 1;
- }
- }
- if (!$have_value) {
- $value[] = 0;
- }
- }
- $data3y[$i]['data'] = $value;
- if ($i < count($data3y)) {
- $i++;
- }
- }
- $datediff = $end_date_time->diff($begin_date_time);
- /*图表4数据封装 工单总数趋势图*/
- $data4x = array();
- $data4y = array();
- if($sign==1 || $sign ==2 || ($datediff->m<3 && $datediff->y==0)) {
- $start_date_time = new DateTime($begin_date_time->format("Y-m-d"));
- for($i = 0;$i<=$datediff->d;$i++){
- $data4x[] = $start_date_time->format("Y-m-d");
- $start_date_time->modify("+1 day");
- }
- $group4 = array(
- "_id" => array("\$dateToString" => array("format" => "%Y-%m-%d", "date" => array("\$add" => ["\$create_time", 28800000]))),
- "value" => array("\$sum" => 1),
- );
- }else{
- $start_date_time = new DateTime($begin_date_time->format("Y-m-d"));
- if($datediff->y > 0){
- $count = $datediff->y*12 + $datediff->m;
- }else{
- $count = $datediff->m;
- }
- for($i = 0;$i<$count;$i++){
- $start_date_time->modify("+1 month");
- $data4x[] = $start_date_time->format("Y-m");
- }
- $group4 = array(
- "_id" => array("\$dateToString" => array("format" => "%Y-%m", "date" => array("\$add" => ["\$create_time", 28800000]))),
- "value" => array("\$sum" => 1),
- );
- }
- $result4 = $this->workorder_model->aggregate($begin_datetime, $end_datetime, $group4,NULL,array("status"=>array("\$ne"=>"60")),NULL,NULL,array("_id"=>"ASC"));
- for ($i=0 ; $i<count($data4x) ; $i++){
- $have_value = 0;
- foreach ($result4 as $result) {
- if($data4x[$i] == $result['_id']){
- $data4y[] = $result['value'];
- $have_value =1;
- }
- }
- if(!$have_value){
- $data4y[] = 0;
- }
- }
- /*图表5数据封装 已处理工单总数趋势图*/
- $data5x = array();
- $data5y = array();
- if($sign==1 || $sign ==2 || ($datediff->m<3 && $datediff->y==0)) {
- $start_date_time = new DateTime($begin_date_time->format("Y-m-d"));
- for($i = 0;$i<=$datediff->d;$i++){
- $data5x[] = $start_date_time->format("Y-m-d");
- $start_date_time->modify("+1 day");
- }
- $group5 = array(
- "_id" => array("\$dateToString" => array("format" => "%Y-%m-%d", "date" => array("\$add" => ["\$end_time", 28800000]))),
- "value" => array("\$sum" => 1),
- );
- }else{
- $start_date_time = new DateTime($begin_date_time->format("Y-m-d"));
- if($datediff->y > 0){
- $count = $datediff->y*12 + $datediff->m;
- }else{
- $count = $datediff->m;
- }
- for($i = 0;$i<$count;$i++){
- $start_date_time->modify("+1 month");
- $data5x[] = $start_date_time->format("Y-m");
- }
- $group5 = array(
- "_id" => array("\$dateToString" => array("format" => "%Y-%m", "date" => array("\$add" => ["\$end_time", 28800000]))),
- "value" => array("\$sum" => 1),
- );
- }
- $result5 = $this->workorder_model->aggregate($begin_datetime, $end_datetime, $group5,NULL,array("status"=>"50"),NULL,NULL,array("_id"=>"ASC"));
- for ($i=0;$i<count($data5x);$i++){
- $have_value = 0;
- foreach ($result5 as $result) {
- if($data5x[$i] == $result['_id']){
- $data5y[] = $result['value'];
- $have_value =1;
- }
- }
- if(!$have_value){
- $data5y[] = 0;
- }
- }
- $this->assign("data1", json_encode($data1));
- $this->assign("data2x", json_encode($data2x));
- $this->assign("data2y", json_encode($data2y));
- $this->assign("data3x", json_encode($data3x));
- $this->assign("data3y", json_encode($data3y));
- $this->assign("data3legend", json_encode($data3legend));
- $this->assign("data4x", json_encode($data4x));
- $this->assign("data4y", json_encode($data4y));
- $this->assign("data5x", json_encode($data5x));
- $this->assign("data5y", json_encode($data5y));
- $this->assign("begin_date", $begin_date);
- $this->assign("end_date", $end_date);
- $this->assign("title", $title);
- $this->assign("sign", $sign);
- $this->assign("now", now());
- $this->display("report/index.html");
- }
- /**
- * 导出统计报表
- */
- public function export(){
- $title = $this->input->get('title',true);
- $chart1 = '<div id="chart1" style="text-align: center;"></div>' .
- '<script type="text/javascript">' .
- 'var chart1 = parent.echarts.getInstanceByDom(parent.document.getElementById("main1"));' .
- 'var imgdata = chart1.getDataURL({' .
- 'type:"jpeg",' .
- 'pixelRatio: 1,' .
- 'backgroundColor: "#fff"' .
- '});' .
- 'document.getElementById("chart1").innerHTML = \'<img src="\'+imgdata+\'">\';' .
- '</script>';
- $chart2 = '<div id="chart2" style="text-align: center;"></div>' .
- '<script type="text/javascript">' .
- 'var chart2 = parent.echarts.getInstanceByDom(parent.document.getElementById("main2"));' .
- 'var imgdata = chart2.getDataURL({' .
- 'type:"jpeg",' .
- 'pixelRatio: 1,' .
- 'backgroundColor: "#fff"' .
- '});' .
- 'document.getElementById("chart2").innerHTML = \'<img src="\'+imgdata+\'">\';' .
- '</script>';
- $chart3 = '<div id="chart3" style="text-align: center;"></div>' .
- '<script type="text/javascript">' .
- 'var chart3 = parent.echarts.getInstanceByDom(parent.document.getElementById("main3"));' .
- 'var imgdata = chart3.getDataURL({' .
- 'type:"jpeg",' .
- 'pixelRatio: 1,' .
- 'backgroundColor: "#fff"' .
- '});' .
- 'document.getElementById("chart3").innerHTML = \'<img src="\'+imgdata+\'">\';' .
- '</script>';
- $chart4 = '<div id="chart4" style="text-align: center;"></div>' .
- '<script type="text/javascript">' .
- 'var chart4 = parent.echarts.getInstanceByDom(parent.document.getElementById("main4"));' .
- 'var imgdata = chart4.getDataURL({' .
- 'type:"jpeg",' .
- 'pixelRatio: 1,' .
- 'backgroundColor: "#fff"' .
- '});' .
- 'document.getElementById("chart4").innerHTML = \'<img src="\'+imgdata+\'">\';' .
- '</script>';
- $chart5 = '<div id="chart5" style="text-align: center;"></div>' .
- '<script type="text/javascript">' .
- 'var chart5 = parent.echarts.getInstanceByDom(parent.document.getElementById("main5"));' .
- 'var imgdata = chart5.getDataURL({' .
- 'type:"jpeg",' .
- 'pixelRatio: 1,' .
- 'backgroundColor: "#fff"' .
- '});' .
- 'document.getElementById("chart5").innerHTML = \'<img src="\'+imgdata+\'">\';' .
- '</script>';
- $this->assign("title", $title);
- $this->assign("chart1", $chart1);
- $this->assign("chart2", $chart2);
- $this->assign("chart3", $chart3);
- $this->assign("chart4", $chart4);
- $this->assign("chart5", $chart5);
- $this->display("export/report.html");
- }
- }
|