Report.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. /**
  4. * Class 统计报表类
  5. */
  6. class Report extends MY_Controller
  7. {
  8. function __construct()
  9. {
  10. parent::__construct();
  11. $this->load->helper(array('form', 'date'));
  12. $this->load->model("workorder_model");
  13. }
  14. /**
  15. * 统计报表首页
  16. */
  17. public function index()
  18. {
  19. $sign = $this->input->get("sign", true);
  20. $begin_date = $this->input->post("begin_date", true);
  21. $end_date = $this->input->post("end_date", true);
  22. $begin_time = " 0:0:0";
  23. $end_time = " 23:59:59";
  24. $now = new DateTime();
  25. $end = $now->format("Y-m-d");
  26. switch ($sign) {
  27. case '1':
  28. $title = "近7天";
  29. $end_date_time = new DateTime($end . $end_time);
  30. $end_datetime = new MongoDB\BSON\UTCDateTime($end_date_time->getTimestamp()*1000);
  31. $now = new DateTime();
  32. $now->modify("-6 day");
  33. $begin = $now->format("Y-m-d");
  34. $begin_date_time = new DateTime($begin . $begin_time);
  35. $begin_datetime = new \MongoDB\BSON\UTCDateTime($begin_date_time->getTimestamp()*1000);
  36. break;
  37. case '2':
  38. $title = "近30天";
  39. $end_date_time = new DateTime($end . $end_time);
  40. $end_datetime = new MongoDB\BSON\UTCDateTime($end_date_time->getTimestamp()*1000);
  41. $now = new DateTime();
  42. $now->modify("-30 day");
  43. $begin = $now->format("Y-m-d");
  44. $begin_date_time = new DateTime($begin . $begin_time);
  45. $begin_datetime = new \MongoDB\BSON\UTCDateTime($begin_date_time->getTimestamp()*1000);
  46. break;
  47. case '3':
  48. $title = "近三个月";
  49. $end_date_time = new DateTime($end . $end_time);
  50. $end_datetime = new MongoDB\BSON\UTCDateTime($end_date_time->getTimestamp()*1000);
  51. $now = new DateTime();
  52. $now->modify("-3 month");
  53. $begin = $now->format("Y-m-d");
  54. $begin_date_time = new DateTime($begin . $begin_time);
  55. $begin_datetime = new \MongoDB\BSON\UTCDateTime($begin_date_time->getTimestamp()*1000);
  56. break;
  57. case '4':
  58. $title = "近半年";
  59. $end_date_time = new DateTime($end . $end_time);
  60. $end_datetime = new MongoDB\BSON\UTCDateTime($end_date_time->getTimestamp()*1000);
  61. $now = new DateTime();
  62. $now->modify("-6 month");
  63. $begin = $now->format("Y-m-d");
  64. $begin_date_time = new DateTime($begin . $begin_time);
  65. $begin_datetime = new \MongoDB\BSON\UTCDateTime($begin_date_time->getTimestamp()*1000);
  66. break;
  67. case '5':
  68. $title = "近一年";
  69. $end_date_time = new DateTime($end . $end_time);
  70. $end_datetime = new MongoDB\BSON\UTCDateTime($end_date_time->getTimestamp()*1000);
  71. $now = new DateTime();
  72. $now->modify("-1 year");
  73. $begin = $now->format("Y-m-d");
  74. $begin_date_time = new DateTime($begin . $begin_time);
  75. $begin_datetime = new \MongoDB\BSON\UTCDateTime($begin_date_time->getTimestamp()*1000);
  76. break;
  77. default:
  78. $title = "近7天";
  79. $end_date_time = new DateTime($end . $end_time);
  80. $end_datetime = new MongoDB\BSON\UTCDateTime($end_date_time->getTimestamp()*1000);
  81. $now = new DateTime();
  82. $now->modify("-6 day");
  83. $begin = $now->format("Y-m-d");
  84. $begin_date_time = new DateTime($begin . $begin_time);
  85. $begin_datetime = new \MongoDB\BSON\UTCDateTime($begin_date_time->getTimestamp()*1000);
  86. }
  87. if ($begin_date && $end_date) {
  88. $title = "自" . $begin_date . "至" . $end_date;
  89. $end_date_time = new DateTime($end_date . $end_time);
  90. $begin_date_time = new DateTime($begin_date . $begin_time);
  91. $end_datetime = new MongoDB\BSON\UTCDateTime($end_date_time->getTimestamp()*1000);
  92. $begin_datetime = new \MongoDB\BSON\UTCDateTime($begin_date_time->getTimestamp()*1000);
  93. }
  94. /*图表1数据封装 各个状态的工单分布*/
  95. $group1 = array(
  96. "_id" => "\$status",
  97. "value" => array("\$sum" => 1)
  98. );
  99. $result1 = $this->workorder_model->aggregate($begin_datetime, $end_datetime, $group1,NULL,array("status"=>array("\$ne"=>"60")));
  100. $data1 = array();
  101. if (is_array($result1) && count($result1) >= 1) {
  102. foreach ($result1 as $key => $val) {
  103. $data1[] = array(
  104. "name" => $this->workorder_status[$val['_id']],
  105. "value" => $val['value']
  106. );
  107. }
  108. }
  109. /*图表2数据封装 各分支工单总数*/
  110. $group2 = array(
  111. "_id" => "\$branch.branch_id",
  112. "value" => array("\$sum" => 1)
  113. );
  114. $result2 = $this->workorder_model->aggregate($begin_datetime, $end_datetime, $group2,NULL,array("status"=>array("\$ne"=>"60")));
  115. $data2x = array();
  116. $data2y = array();
  117. if (is_array($result2) && count($result2) >= 1) {
  118. foreach ($this->branch_array as $key => $branch) {
  119. $data2x[] = $branch['branch_name'];
  120. $have_value = 0;
  121. foreach ($result2 as $key => $val) {
  122. if ($val['_id'] == $branch['branch_id']) {
  123. $data2y[] = $val['value'];
  124. $have_value = 1;
  125. }
  126. }
  127. if (!$have_value) {
  128. $data2y[] = 0;
  129. }
  130. }
  131. }
  132. /*图表3数据封装 各分支工单状态数量*/
  133. $group3 = array(
  134. "_id" => array("branchs" => "\$branch.branch_id", "status" => "\$status"),
  135. "value" => array("\$sum" => 1)
  136. );
  137. $result3 = $this->workorder_model->aggregate($begin_datetime, $end_datetime, $group3,NULL,array("status"=>array("\$ne"=>"60")));
  138. $data3x = array();
  139. $data3y = array();
  140. $data3legend = array();
  141. $i = 0;
  142. foreach ($this->branch_array as $key => $branch) {
  143. $data3x[] = $branch['branch_name'];
  144. }
  145. foreach ($this->workorder_status as $k => $status) {
  146. if($k == "60") continue;
  147. $data = array();
  148. $data['name'] = $status;
  149. $data['type'] = "bar";
  150. $data['barGap'] = 0;
  151. $data3y[] = $data;
  152. $data3legend[] = $status;
  153. }
  154. $i = 0;
  155. foreach ($this->workorder_status as $k => $status) {
  156. if($k == "60") continue;
  157. $value = array();
  158. foreach ($this->branch_array as $key => $branch) {
  159. $have_value = 0;
  160. foreach ($result3 as $result) {
  161. if ($result['_id']['branchs'] == $branch['branch_id'] && $result['_id']['status'] == $k) {
  162. $value[] = $result['value'];
  163. $have_value = 1;
  164. }
  165. }
  166. if (!$have_value) {
  167. $value[] = 0;
  168. }
  169. }
  170. $data3y[$i]['data'] = $value;
  171. if ($i < count($data3y)) {
  172. $i++;
  173. }
  174. }
  175. $datediff = $end_date_time->diff($begin_date_time);
  176. /*图表4数据封装 工单总数趋势图*/
  177. $data4x = array();
  178. $data4y = array();
  179. if($sign==1 || $sign ==2 || ($datediff->m<3 && $datediff->y==0)) {
  180. $start_date_time = new DateTime($begin_date_time->format("Y-m-d"));
  181. for($i = 0;$i<=$datediff->d;$i++){
  182. $data4x[] = $start_date_time->format("Y-m-d");
  183. $start_date_time->modify("+1 day");
  184. }
  185. $group4 = array(
  186. "_id" => array("\$dateToString" => array("format" => "%Y-%m-%d", "date" => array("\$add" => ["\$create_time", 28800000]))),
  187. "value" => array("\$sum" => 1),
  188. );
  189. }else{
  190. $start_date_time = new DateTime($begin_date_time->format("Y-m-d"));
  191. if($datediff->y > 0){
  192. $count = $datediff->y*12 + $datediff->m;
  193. }else{
  194. $count = $datediff->m;
  195. }
  196. for($i = 0;$i<$count;$i++){
  197. $start_date_time->modify("+1 month");
  198. $data4x[] = $start_date_time->format("Y-m");
  199. }
  200. $group4 = array(
  201. "_id" => array("\$dateToString" => array("format" => "%Y-%m", "date" => array("\$add" => ["\$create_time", 28800000]))),
  202. "value" => array("\$sum" => 1),
  203. );
  204. }
  205. $result4 = $this->workorder_model->aggregate($begin_datetime, $end_datetime, $group4,NULL,array("status"=>array("\$ne"=>"60")),NULL,NULL,array("_id"=>"ASC"));
  206. for ($i=0 ; $i<count($data4x) ; $i++){
  207. $have_value = 0;
  208. foreach ($result4 as $result) {
  209. if($data4x[$i] == $result['_id']){
  210. $data4y[] = $result['value'];
  211. $have_value =1;
  212. }
  213. }
  214. if(!$have_value){
  215. $data4y[] = 0;
  216. }
  217. }
  218. /*图表5数据封装 已处理工单总数趋势图*/
  219. $data5x = array();
  220. $data5y = array();
  221. if($sign==1 || $sign ==2 || ($datediff->m<3 && $datediff->y==0)) {
  222. $start_date_time = new DateTime($begin_date_time->format("Y-m-d"));
  223. for($i = 0;$i<=$datediff->d;$i++){
  224. $data5x[] = $start_date_time->format("Y-m-d");
  225. $start_date_time->modify("+1 day");
  226. }
  227. $group5 = array(
  228. "_id" => array("\$dateToString" => array("format" => "%Y-%m-%d", "date" => array("\$add" => ["\$end_time", 28800000]))),
  229. "value" => array("\$sum" => 1),
  230. );
  231. }else{
  232. $start_date_time = new DateTime($begin_date_time->format("Y-m-d"));
  233. if($datediff->y > 0){
  234. $count = $datediff->y*12 + $datediff->m;
  235. }else{
  236. $count = $datediff->m;
  237. }
  238. for($i = 0;$i<$count;$i++){
  239. $start_date_time->modify("+1 month");
  240. $data5x[] = $start_date_time->format("Y-m");
  241. }
  242. $group5 = array(
  243. "_id" => array("\$dateToString" => array("format" => "%Y-%m", "date" => array("\$add" => ["\$end_time", 28800000]))),
  244. "value" => array("\$sum" => 1),
  245. );
  246. }
  247. $result5 = $this->workorder_model->aggregate($begin_datetime, $end_datetime, $group5,NULL,array("status"=>"50"),NULL,NULL,array("_id"=>"ASC"));
  248. for ($i=0;$i<count($data5x);$i++){
  249. $have_value = 0;
  250. foreach ($result5 as $result) {
  251. if($data5x[$i] == $result['_id']){
  252. $data5y[] = $result['value'];
  253. $have_value =1;
  254. }
  255. }
  256. if(!$have_value){
  257. $data5y[] = 0;
  258. }
  259. }
  260. $this->assign("data1", json_encode($data1));
  261. $this->assign("data2x", json_encode($data2x));
  262. $this->assign("data2y", json_encode($data2y));
  263. $this->assign("data3x", json_encode($data3x));
  264. $this->assign("data3y", json_encode($data3y));
  265. $this->assign("data3legend", json_encode($data3legend));
  266. $this->assign("data4x", json_encode($data4x));
  267. $this->assign("data4y", json_encode($data4y));
  268. $this->assign("data5x", json_encode($data5x));
  269. $this->assign("data5y", json_encode($data5y));
  270. $this->assign("begin_date", $begin_date);
  271. $this->assign("end_date", $end_date);
  272. $this->assign("title", $title);
  273. $this->assign("sign", $sign);
  274. $this->assign("now", now());
  275. $this->display("report/index.html");
  276. }
  277. /**
  278. * 导出统计报表
  279. */
  280. public function export(){
  281. $title = $this->input->get('title',true);
  282. $chart1 = '<div id="chart1" style="text-align: center;"></div>' .
  283. '<script type="text/javascript">' .
  284. 'var chart1 = parent.echarts.getInstanceByDom(parent.document.getElementById("main1"));' .
  285. 'var imgdata = chart1.getDataURL({' .
  286. 'type:"jpeg",' .
  287. 'pixelRatio: 1,' .
  288. 'backgroundColor: "#fff"' .
  289. '});' .
  290. 'document.getElementById("chart1").innerHTML = \'<img src="\'+imgdata+\'">\';' .
  291. '</script>';
  292. $chart2 = '<div id="chart2" style="text-align: center;"></div>' .
  293. '<script type="text/javascript">' .
  294. 'var chart2 = parent.echarts.getInstanceByDom(parent.document.getElementById("main2"));' .
  295. 'var imgdata = chart2.getDataURL({' .
  296. 'type:"jpeg",' .
  297. 'pixelRatio: 1,' .
  298. 'backgroundColor: "#fff"' .
  299. '});' .
  300. 'document.getElementById("chart2").innerHTML = \'<img src="\'+imgdata+\'">\';' .
  301. '</script>';
  302. $chart3 = '<div id="chart3" style="text-align: center;"></div>' .
  303. '<script type="text/javascript">' .
  304. 'var chart3 = parent.echarts.getInstanceByDom(parent.document.getElementById("main3"));' .
  305. 'var imgdata = chart3.getDataURL({' .
  306. 'type:"jpeg",' .
  307. 'pixelRatio: 1,' .
  308. 'backgroundColor: "#fff"' .
  309. '});' .
  310. 'document.getElementById("chart3").innerHTML = \'<img src="\'+imgdata+\'">\';' .
  311. '</script>';
  312. $chart4 = '<div id="chart4" style="text-align: center;"></div>' .
  313. '<script type="text/javascript">' .
  314. 'var chart4 = parent.echarts.getInstanceByDom(parent.document.getElementById("main4"));' .
  315. 'var imgdata = chart4.getDataURL({' .
  316. 'type:"jpeg",' .
  317. 'pixelRatio: 1,' .
  318. 'backgroundColor: "#fff"' .
  319. '});' .
  320. 'document.getElementById("chart4").innerHTML = \'<img src="\'+imgdata+\'">\';' .
  321. '</script>';
  322. $chart5 = '<div id="chart5" style="text-align: center;"></div>' .
  323. '<script type="text/javascript">' .
  324. 'var chart5 = parent.echarts.getInstanceByDom(parent.document.getElementById("main5"));' .
  325. 'var imgdata = chart5.getDataURL({' .
  326. 'type:"jpeg",' .
  327. 'pixelRatio: 1,' .
  328. 'backgroundColor: "#fff"' .
  329. '});' .
  330. 'document.getElementById("chart5").innerHTML = \'<img src="\'+imgdata+\'">\';' .
  331. '</script>';
  332. $this->assign("title", $title);
  333. $this->assign("chart1", $chart1);
  334. $this->assign("chart2", $chart2);
  335. $this->assign("chart3", $chart3);
  336. $this->assign("chart4", $chart4);
  337. $this->assign("chart5", $chart5);
  338. $this->display("export/report.html");
  339. }
  340. }