ReporteController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace app\controllers;
  3. use yii\db\Query;
  4. use yii\web\Controller;
  5. class ReporteController extends Controller {
  6. public function actionDescarga() {
  7. $req = \Yii::$app->getRequest();
  8. $pc = trim($req->get("pc", ""));
  9. $st = trim($req->get("st", ""));
  10. $fi = trim($req->get("fi", "2021-12-01"));
  11. $ff = trim($req->get("ff", ""));
  12. $sql = intval($req->get("sql", "")) === 1;
  13. $json = intval($req->get("json", "")) === 1;
  14. $query = (new Query())
  15. ->select([
  16. "{{Estacion}}.id",
  17. "{{Estacion}}.clave",
  18. "{{Estacion}}.siglas",
  19. "{{Estacion}}.frecuencia",
  20. "ciudad",
  21. "pc",
  22. "to_char(fecha, 'YYYY-MM-DD') as fecha",
  23. "extract(epoch from to_char(fecha, 'YYYY-MM-DD')::DATE) as epoch",
  24. "count(estacion) filter (where descargado = true) as descargados",
  25. "count(estacion) filter (where descargado = false) as pendientes",
  26. "count(estacion) as total"
  27. ])
  28. ->from("Descarga")
  29. ->innerJoin("Estacion", "{{Estacion}}.clave = {{Descarga}}.estacion")
  30. ->andWhere(['not', ['pc' => null]])
  31. ->groupBy(["{{Estacion}}.id", "to_char(fecha, 'YYYY-MM-DD')", "ciudad", "pc"])
  32. ->orderBy([
  33. "pc" => SORT_ASC,
  34. "clave" => SORT_ASC,
  35. "fecha" => SORT_ASC,
  36. ]);
  37. if($fi !== "") {
  38. $query->andWhere([">=", "fecha", "{$fi} 00:00:00"]);
  39. }
  40. if($ff !== "") {
  41. $query->andWhere(["<=", "fecha", "{$ff} 23:59:59"]);
  42. }
  43. if($pc !== "") {
  44. $aux = explode(",", $pc);
  45. $pcs = [];
  46. foreach($aux as $p) {
  47. $v = trim($p);
  48. if(!empty($v)) {
  49. $pcs[] = $v;
  50. }
  51. }
  52. $query->andWhere(["pc" => $pcs]);
  53. }
  54. if($st !== "") {
  55. $aux = explode(",", $st);
  56. $sts = [];
  57. foreach($aux as $p) {
  58. $v = trim($p);
  59. if(!empty($v)) {
  60. $sts[] = $v;
  61. }
  62. }
  63. $query->andWhere(["estacion" => $sts]);
  64. }
  65. if($sql) {
  66. \Yii::$app->getResponse()->format = \yii\web\Response::FORMAT_RAW;
  67. return $query->createCommand()->getRawSql();
  68. }
  69. $aux = $query->all();
  70. $primer = null;
  71. $ultimo = null;
  72. if(!empty($aux)) {
  73. $tz = new \DateTimeZone("America/Mexico_City");
  74. $primer = \DateTime::createFromFormat("Y-m-d", $aux[0]['fecha'], $tz);
  75. $ultimo = \DateTime::createFromFormat("Y-m-d", $aux[count($aux) - 1]['fecha'], $tz);
  76. $primer->setTime(0, 0, 0);
  77. $ultimo->setTime(0, 0, 0);
  78. }
  79. $descargas = [];
  80. $estaciones = [];
  81. foreach($aux as $d) {
  82. if(!isset($estaciones[$d['pc']]) || (isset($estaciones[$d['pc']]) && !isset($estaciones[$d['pc']][$d['clave']]))) {
  83. $estaciones[$d['pc']][$d['clave']] = [
  84. "clave" => $d['clave'],
  85. "siglas" => $d['siglas'],
  86. "ciudad" => $d['ciudad'],
  87. "frecuencia" => $d['frecuencia']
  88. ];
  89. }
  90. $color = "darkgreen";
  91. if($d["descargados"] >= 70 && $d["descargados"] < 90) {
  92. $color = "darkgray";
  93. } elseif($d["descargados"] < 70) {
  94. $color = "red";
  95. }
  96. $descargas[$d['pc']][$d['clave']][$d['fecha']] = [
  97. "descargados" => $d['descargados'],
  98. "pendientes" => $d['pendientes'],
  99. "total" => $d['total'],
  100. "color" => $color
  101. ];
  102. }
  103. if($json) {
  104. \Yii::$app->getResponse()->format = \yii\web\Response::FORMAT_JSON;
  105. return [
  106. "primer" => $primer,
  107. "ultimo" => $ultimo,
  108. "estaciones" => $estaciones,
  109. "descargas" => $descargas
  110. ];
  111. }
  112. return $this->render('index', [
  113. "estaciones" => $estaciones,
  114. "descargas" => $descargas,
  115. "primer" => $primer,
  116. "ultimo" => $ultimo
  117. ]);
  118. }
  119. }