Ver Fonte

Vista de matriz en reporte

Hugo há 4 anos atrás
pai
commit
5b9d05190f
2 ficheiros alterados com 203 adições e 0 exclusões
  1. 133 0
      controllers/ReporteController.php
  2. 70 0
      views/reporte/index.php

+ 133 - 0
controllers/ReporteController.php

@@ -0,0 +1,133 @@
+<?php
+
+namespace app\controllers;
+
+use yii\db\Query;
+use yii\web\Controller;
+
+class ReporteController extends Controller {
+
+  public function actionDescarga() {
+    $req = \Yii::$app->getRequest();
+    $pc = trim($req->get("pc", ""));
+    $st = trim($req->get("st", ""));
+    $fi = trim($req->get("fi", "2021-12-01"));
+    $ff = trim($req->get("ff", ""));
+    $sql = intval($req->get("sql", "")) === 1;
+    $json = intval($req->get("json", "")) === 1;
+
+    $query = (new Query())
+      ->select([
+        "{{Estacion}}.id",
+        "{{Estacion}}.clave",
+        "{{Estacion}}.siglas",
+        "{{Estacion}}.frecuencia",
+        "ciudad",
+        "pc",
+        "to_char(fecha, 'YYYY-MM-DD') as fecha",
+        "extract(epoch from to_char(fecha, 'YYYY-MM-DD')::DATE) as epoch",
+        "count(estacion) filter (where descargado = true) as descargados",
+        "count(estacion) filter (where descargado = false) as pendientes",
+        "count(estacion) as total"
+      ])
+      ->from("Descarga")
+      ->innerJoin("Estacion", "{{Estacion}}.clave = {{Descarga}}.estacion")
+      ->andWhere(['not', ['pc' => null]])
+      ->groupBy(["{{Estacion}}.id", "to_char(fecha, 'YYYY-MM-DD')", "ciudad", "pc"])
+      ->orderBy([
+        "pc" => SORT_ASC,
+        "clave" => SORT_ASC,
+        "fecha" => SORT_ASC,
+      ]);
+    
+    if($fi !== "") {
+      $query->andWhere([">=", "fecha", "{$fi} 00:00:00"]);
+    }
+  
+    if($ff !== "") {
+      $query->andWhere(["<=", "fecha", "{$ff} 23:59:59"]);
+    }
+
+    if($pc !== "") {
+      $aux = explode(",", $pc);
+      $pcs = [];
+      foreach($aux as $p) {
+        $v = trim($p);
+        if(!empty($v)) {
+          $pcs[] = $v;
+        }
+      }
+      $query->andWhere(["pc" => $pcs]);
+    }
+
+    if($st !== "") {
+      $aux = explode(",", $st);
+      $sts = [];
+      foreach($aux as $p) {
+        $v = trim($p);
+        if(!empty($v)) {
+          $sts[] = $v;
+        }
+      }
+      $query->andWhere(["estacion" => $sts]);
+    }
+
+    if($sql) {
+      \Yii::$app->getResponse()->format = \yii\web\Response::FORMAT_RAW;
+      return $query->createCommand()->getRawSql();
+    }
+
+    $aux = $query->all();
+    $primer = null;
+    $ultimo = null;
+    if(!empty($aux)) {
+      $tz = new \DateTimeZone("America/Mexico_City");
+      $primer = \DateTime::createFromFormat("Y-m-d", $aux[0]['fecha'], $tz);
+      $ultimo = \DateTime::createFromFormat("Y-m-d", $aux[count($aux) - 1]['fecha'], $tz);
+      $primer->setTime(0, 0, 0);
+      $ultimo->setTime(0, 0, 0);
+    }
+    $descargas = [];
+    $estaciones = [];
+    foreach($aux as $d) {
+      if(!isset($estaciones[$d['pc']]) || (isset($estaciones[$d['pc']]) && !isset($estaciones[$d['pc']][$d['clave']]))) {
+        $estaciones[$d['pc']][$d['clave']] = [
+          "clave" => $d['clave'],
+          "siglas" => $d['siglas'],
+          "ciudad" => $d['ciudad'],
+          "frecuencia" => $d['frecuencia']
+        ];
+      }
+      $color = "darkgreen";
+      if($d["total"] >= 70 && $d["total"] < 90) {
+        $color = "darkgray";
+      } elseif($d["total"] < 70) {
+        $color = "red";
+      }
+      $descargas[$d['pc']][$d['clave']][$d['fecha']] = [
+        "descargados" => $d['descargados'],
+        "pendientes" => $d['pendientes'],
+        "total" => $d['total'],
+        "color" => $color
+      ];
+    }
+
+    if($json) {
+      \Yii::$app->getResponse()->format = \yii\web\Response::FORMAT_JSON;
+      return [
+        "primer" => $primer,
+        "ultimo" => $ultimo,
+        "estaciones" => $estaciones,
+        "descargas" => $descargas
+      ];
+    }
+
+    return $this->render('index', [
+      "estaciones" => $estaciones,
+      "descargas" => $descargas,
+      "primer" => $primer,
+      "ultimo" => $ultimo
+    ]);
+  }
+
+}

+ 70 - 0
views/reporte/index.php

@@ -0,0 +1,70 @@
+<?php
+
+/* @var $descargas app\models\Descarga[] */
+/* @var $estaciones array */
+/* @var $primer \DateTime */
+/* @var $ultimo \DateTime */
+
+?>
+<style>
+  .site-card {
+    min-height: 100vh;
+    width: 100vw;
+    padding: 20px;
+    background: #ececec;
+  }
+
+  .content {
+    width: 100%;
+    height: 100%;
+  }
+
+  .full-width {
+    width: 100%;
+  }
+</style>
+<div>
+  <table border="1" width="100%" id="tabla">
+    <tr>
+      <th>pc</th>
+      <th>ciudad</th>
+      <th>clave</th>
+      <th>siglas</th>
+      <th>frecuencia</th>
+      <?php $f = (clone $primer); ?>
+      <?php while($f <= $ultimo): ?>
+        <th>
+        <?= $f->format("d/m/Y") ?>
+        </th>
+        <?php $f->add(new \DateInterval("P1D")); ?>
+      <?php endwhile; ?>
+    </tr>
+    <?php $estacion = ""; ?>
+    <?php foreach ($estaciones as $pc => $est): ?>
+      <?php foreach($est as $clave => $info): ?>
+        <tr>
+          <td><?= $pc ?></td>
+          <td><?= $info['ciudad'] ?></td>
+          <td><?= $clave ?></td>
+          <td><?= $info['siglas'] ?></td>
+          <td><?= $info['frecuencia'] ?></td>
+          <?php $fecha = (clone $primer); ?>
+          <?php while($fecha <= $ultimo): ?>
+            <?php $d = null; ?>
+            <?php if(isset($descargas[$pc][$clave][$fecha->format("Y-m-d")])): ?>
+              <?php $d = $descargas[$pc][$clave][$fecha->format("Y-m-d")]; ?>
+            <?php endif; ?>
+            <?php if($d !== null): ?>
+              <td style="background-color: <?= $d["color"] ?>; text-align: center ">
+              <?= $d["descargados"] ?> / <?= $d["total"] ?>
+              </td>
+            <?php else: ?>
+              <td></td>
+            <?php endif; ?>
+            <?php $fecha->add(new \DateInterval("P1D")); ?>
+          <?php endwhile ?>
+        </tr>
+      <?php endforeach ?>
+    <?php endforeach ?>
+  </table>
+</div>