| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?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
- ]);
- }
- }
|