Report.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 = $begin_date_time->diff($end_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. if($sign == 1) {
  182. for ($i = 0; $i <= $datediff->d; $i++) {
  183. $data4x[] = $start_date_time->format("Y-m-d");
  184. $start_date_time->modify("+1 day");
  185. }
  186. }elseif($sign==2){
  187. for ($i = 0; $i < 30; $i++) {
  188. $data4x[] = $start_date_time->format("Y-m-d");
  189. $start_date_time->modify("+1 day");
  190. }
  191. }else{
  192. $diff = $this->diffBetweenTwoDays($begin_date_time,$end_date_time);
  193. for ($i = 0; $i < $diff; $i++) {
  194. $data4x[] = $start_date_time->format("Y-m-d");
  195. $start_date_time->modify("+1 day");
  196. }
  197. }
  198. $group4 = array(
  199. "_id" => array("\$dateToString" => array("format" => "%Y-%m-%d", "date" => array("\$add" => ["\$create_time", 28800000]))),
  200. "value" => array("\$sum" => 1),
  201. );
  202. }else{
  203. $start_date_time = new DateTime($begin_date_time->format("Y-m-d"));
  204. if($datediff->y > 0){
  205. $count = $datediff->y*12 + $datediff->m;
  206. }else{
  207. $count = $datediff->m;
  208. }
  209. for($i = 0;$i<$count;$i++){
  210. $start_date_time->modify("+1 month");
  211. $data4x[] = $start_date_time->format("Y-m");
  212. }
  213. $group4 = array(
  214. "_id" => array("\$dateToString" => array("format" => "%Y-%m", "date" => array("\$add" => ["\$create_time", 28800000]))),
  215. "value" => array("\$sum" => 1),
  216. );
  217. }
  218. $result4 = $this->workorder_model->aggregate($begin_datetime, $end_datetime, $group4,NULL,array("status"=>array("\$ne"=>"60")),NULL,NULL,array("_id"=>"ASC"));
  219. for ($i=0 ; $i<count($data4x) ; $i++){
  220. $have_value = 0;
  221. foreach ($result4 as $result) {
  222. if($data4x[$i] == $result['_id']){
  223. $data4y[] = $result['value'];
  224. $have_value =1;
  225. }
  226. }
  227. if(!$have_value){
  228. $data4y[] = 0;
  229. }
  230. }
  231. /*图表5数据封装 已处理工单总数趋势图*/
  232. $data5x = array();
  233. $data5y = array();
  234. if($sign==1 || $sign ==2 || ($datediff->m<3 && $datediff->y==0)) {
  235. $start_date_time = new DateTime($begin_date_time->format("Y-m-d"));
  236. if($sign == 1) {
  237. for ($i = 0; $i <= $datediff->d; $i++) {
  238. $data5x[] = $start_date_time->format("Y-m-d");
  239. $start_date_time->modify("+1 day");
  240. }
  241. }elseif($sign==2){
  242. for ($i = 0; $i < 30; $i++) {
  243. $data5x[] = $start_date_time->format("Y-m-d");
  244. $start_date_time->modify("+1 day");
  245. }
  246. }else{
  247. $diff = $this->diffBetweenTwoDays($begin_date_time,$end_date_time);
  248. for ($i = 0; $i <= $diff; $i++) {
  249. $data5x[] = $start_date_time->format("Y-m-d");
  250. $start_date_time->modify("+1 day");
  251. }
  252. }
  253. $group5 = array(
  254. "_id" => array("\$dateToString" => array("format" => "%Y-%m-%d", "date" => array("\$add" => ["\$end_time", 28800000]))),
  255. "value" => array("\$sum" => 1),
  256. );
  257. }else{
  258. $start_date_time = new DateTime($begin_date_time->format("Y-m-d"));
  259. if($datediff->y > 0){
  260. $count = $datediff->y*12 + $datediff->m;
  261. }else{
  262. $count = $datediff->m;
  263. }
  264. for($i = 0;$i<$count;$i++){
  265. $start_date_time->modify("+1 month");
  266. $data5x[] = $start_date_time->format("Y-m");
  267. }
  268. $group5 = array(
  269. "_id" => array("\$dateToString" => array("format" => "%Y-%m", "date" => array("\$add" => ["\$end_time", 28800000]))),
  270. "value" => array("\$sum" => 1),
  271. );
  272. }
  273. $result5 = $this->workorder_model->aggregate($begin_datetime, $end_datetime, $group5,NULL,array("status"=>"50"),NULL,NULL,array("_id"=>"ASC"));
  274. for ($i=0;$i<count($data5x);$i++){
  275. $have_value = 0;
  276. foreach ($result5 as $result) {
  277. if($data5x[$i] == $result['_id']){
  278. $data5y[] = $result['value'];
  279. $have_value =1;
  280. }
  281. }
  282. if(!$have_value){
  283. $data5y[] = 0;
  284. }
  285. }
  286. $this->assign("data1", json_encode($data1));
  287. $this->assign("data2x", json_encode($data2x));
  288. $this->assign("data2y", json_encode($data2y));
  289. $this->assign("data3x", json_encode($data3x));
  290. $this->assign("data3y", json_encode($data3y));
  291. $this->assign("data3legend", json_encode($data3legend));
  292. $this->assign("data4x", json_encode($data4x));
  293. $this->assign("data4y", json_encode($data4y));
  294. $this->assign("data5x", json_encode($data5x));
  295. $this->assign("data5y", json_encode($data5y));
  296. $this->assign("begin_date", $begin_date);
  297. $this->assign("end_date", $end_date);
  298. $this->assign("title", $title);
  299. $this->assign("sign", $sign);
  300. $this->assign("now", now());
  301. $this->display("report/index.html");
  302. }
  303. /**
  304. * 求两个日期之间相差的天数
  305. * (针对1970年1月1日之后,求之前可以采用泰勒公式)
  306. * @param DateTime $day1
  307. * @param DateTime $day2
  308. * @return number
  309. */
  310. public function diffBetweenTwoDays ($day1, $day2)
  311. {
  312. $second1 = $day1->getTimestamp();
  313. $second2 = $day2->getTimestamp();
  314. if ($second1 < $second2) {
  315. $tmp = $second2;
  316. $second2 = $second1;
  317. $second1 = $tmp;
  318. }
  319. return ($second1 - $second2) / 86400;
  320. }
  321. /**
  322. * 导出统计报表
  323. */
  324. public function export(){
  325. $title = $this->input->get('title',true);
  326. $chart1 = '<div id="chart1" style="text-align: center;"></div>' .
  327. '<script type="text/javascript">' .
  328. 'var chart1 = parent.echarts.getInstanceByDom(parent.document.getElementById("main1"));' .
  329. 'var imgdata = chart1.getDataURL({' .
  330. 'type:"jpeg",' .
  331. 'pixelRatio: 1,' .
  332. 'backgroundColor: "#fff"' .
  333. '});' .
  334. 'document.getElementById("chart1").innerHTML = \'<img src="\'+imgdata+\'">\';' .
  335. '</script>';
  336. $chart2 = '<div id="chart2" style="text-align: center;"></div>' .
  337. '<script type="text/javascript">' .
  338. 'var chart2 = parent.echarts.getInstanceByDom(parent.document.getElementById("main2"));' .
  339. 'var imgdata = chart2.getDataURL({' .
  340. 'type:"jpeg",' .
  341. 'pixelRatio: 1,' .
  342. 'backgroundColor: "#fff"' .
  343. '});' .
  344. 'document.getElementById("chart2").innerHTML = \'<img src="\'+imgdata+\'">\';' .
  345. '</script>';
  346. $chart3 = '<div id="chart3" style="text-align: center;"></div>' .
  347. '<script type="text/javascript">' .
  348. 'var chart3 = parent.echarts.getInstanceByDom(parent.document.getElementById("main3"));' .
  349. 'var imgdata = chart3.getDataURL({' .
  350. 'type:"jpeg",' .
  351. 'pixelRatio: 1,' .
  352. 'backgroundColor: "#fff"' .
  353. '});' .
  354. 'document.getElementById("chart3").innerHTML = \'<img src="\'+imgdata+\'">\';' .
  355. '</script>';
  356. $chart4 = '<div id="chart4" style="text-align: center;"></div>' .
  357. '<script type="text/javascript">' .
  358. 'var chart4 = parent.echarts.getInstanceByDom(parent.document.getElementById("main4"));' .
  359. 'var imgdata = chart4.getDataURL({' .
  360. 'type:"jpeg",' .
  361. 'pixelRatio: 1,' .
  362. 'backgroundColor: "#fff"' .
  363. '});' .
  364. 'document.getElementById("chart4").innerHTML = \'<img src="\'+imgdata+\'">\';' .
  365. '</script>';
  366. $chart5 = '<div id="chart5" style="text-align: center;"></div>' .
  367. '<script type="text/javascript">' .
  368. 'var chart5 = parent.echarts.getInstanceByDom(parent.document.getElementById("main5"));' .
  369. 'var imgdata = chart5.getDataURL({' .
  370. 'type:"jpeg",' .
  371. 'pixelRatio: 1,' .
  372. 'backgroundColor: "#fff"' .
  373. '});' .
  374. 'document.getElementById("chart5").innerHTML = \'<img src="\'+imgdata+\'">\';' .
  375. '</script>';
  376. $this->assign("title", $title);
  377. $this->assign("chart1", $chart1);
  378. $this->assign("chart2", $chart2);
  379. $this->assign("chart3", $chart3);
  380. $this->assign("chart4", $chart4);
  381. $this->assign("chart5", $chart5);
  382. $this->display("export/report.html");
  383. }
  384. }