ReporteController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace app\commands;
  3. use yii\base\View;
  4. use yii\db\Query;
  5. class ReporteController extends \yii\console\Controller {
  6. public $pc = null;
  7. public $st = null;
  8. public $fi = null;
  9. public $ff = null;
  10. public function options($actionID) {
  11. return [
  12. 'pc',
  13. 'st',
  14. 'fi',
  15. 'ff'
  16. ];
  17. }
  18. public function actionIndex() {
  19. $query = (new Query())
  20. ->select([
  21. "{{Estacion}}.id",
  22. "{{Estacion}}.clave",
  23. "{{Estacion}}.siglas",
  24. "{{Estacion}}.frecuencia",
  25. "ciudad",
  26. "pc",
  27. "to_char(fecha, 'YYYY-MM-DD') as fecha",
  28. "extract(epoch from to_char(fecha, 'YYYY-MM-DD')::DATE) as epoch",
  29. "count(estacion) filter (where descargado = true) as descargados",
  30. "count(estacion) filter (where descargado = false) as pendientes",
  31. "count(estacion) as total"
  32. ])
  33. ->from("Descarga")
  34. ->innerJoin("Estacion", "{{Estacion}}.clave = {{Descarga}}.estacion")
  35. ->andWhere(['not', ['pc' => null]])
  36. ->groupBy(["{{Estacion}}.id", "to_char(fecha, 'YYYY-MM-DD')", "ciudad", "pc"])
  37. ->orderBy([
  38. "pc" => SORT_ASC,
  39. "clave" => SORT_ASC,
  40. "fecha" => SORT_ASC,
  41. ]);
  42. if($this->fi !== null) {
  43. $query->andWhere([">=", "fecha", "{$this->fi} 00:00:00"]);
  44. }
  45. if($this->ff !== null) {
  46. $query->andWhere(["<=", "fecha", "{$this->ff} 23:59:59"]);
  47. }
  48. if($this->pc !== null) {
  49. $aux = explode(",", $this->pc);
  50. $pcs = [];
  51. foreach($aux as $p) {
  52. $v = trim($p);
  53. if(!empty($v)) {
  54. $pcs[] = $v;
  55. }
  56. }
  57. $query->andWhere(["pc" => $pcs]);
  58. }
  59. if($this->st !== null) {
  60. $aux = explode(",", $this->st);
  61. $sts = [];
  62. foreach($aux as $p) {
  63. $v = trim($p);
  64. if(!empty($v)) {
  65. $sts[] = $v;
  66. }
  67. }
  68. $query->andWhere(["estacion" => $sts]);
  69. }
  70. $aux = $query->all();
  71. $primer = null;
  72. $ultimo = null;
  73. if(!empty($aux)) {
  74. $tz = new \DateTimeZone("America/Mexico_City");
  75. $primer = \DateTime::createFromFormat("Y-m-d", $aux[0]['fecha'], $tz);
  76. $ultimo = \DateTime::createFromFormat("Y-m-d", $aux[count($aux) - 1]['fecha'], $tz);
  77. $primer->setTime(0, 0, 0);
  78. $ultimo->setTime(0, 0, 0);
  79. }
  80. $descargas = [];
  81. $estaciones = [];
  82. foreach($aux as $d) {
  83. if(!isset($estaciones[$d['pc']]) || (isset($estaciones[$d['pc']]) && !isset($estaciones[$d['pc']][$d['clave']]))) {
  84. $estaciones[$d['pc']][$d['clave']] = [
  85. "clave" => $d['clave'],
  86. "siglas" => $d['siglas'],
  87. "ciudad" => $d['ciudad'],
  88. "frecuencia" => $d['frecuencia']
  89. ];
  90. }
  91. $color = "darkgreen";
  92. if($d["descargados"] >= 70 && $d["descargados"] < 90) {
  93. $color = "darkgray";
  94. } elseif($d["descargados"] < 70) {
  95. $color = "red";
  96. }
  97. $descargas[$d['pc']][$d['clave']][$d['fecha']] = [
  98. "descargados" => $d['descargados'],
  99. "pendientes" => $d['pendientes'],
  100. "total" => $d['total'],
  101. "color" => $color
  102. ];
  103. }
  104. $view = new View();
  105. $content = $view->render('/reporte/index', [
  106. "estaciones" => $estaciones,
  107. "descargas" => $descargas,
  108. "primer" => $primer,
  109. "ultimo" => $ultimo
  110. ]);
  111. /*
  112. $content = '<table border="1" width="100%" id="tabla">
  113. <tr>
  114. <th>pc</th>
  115. <th>siglas</th>
  116. <th>clave</th>
  117. <th>frecuencia</th>
  118. <th colspan="30">
  119. <!--table>
  120. <tr id="dias"></tr>
  121. </table -->
  122. </th>
  123. </tr>
  124. ';
  125. foreach ($estaciones as $pc => $est) {
  126. foreach($est as $clave => $info) {
  127. $content .= '<tr>
  128. <td>' . $pc . '</td>
  129. <td>' . $info['siglas'] . '</td>
  130. <td>' . $info['clave'] . '</td>
  131. <td>' . $info['frecuencia'] . '</td>';
  132. $dias = [];
  133. $fecha = $primer;
  134. while($fecha <= $ultimo) {
  135. $dias[] = $fecha->format("Y-m-d");
  136. $fecha->add(new \DateInterval("P1D"));
  137. $this->stdout("Fecha: {$fecha->format("Y-m-d")}\n");
  138. }
  139. $content .= '<td>';
  140. foreach($dias as $dia) {
  141. if(isset($descargas[$pc][$clave][$dia])) {
  142. $content .= '<table>
  143. <tr>
  144. <td>' . $dia . '</td>
  145. <td>' . $descargas[$pc][$clave][$dia]['descargados'] . '</td>
  146. <td>' . $descargas[$pc][$clave][$dia]['pendientes'] . '</td>
  147. <td>' . $descargas[$pc][$clave][$dia]['total'] . '</td>
  148. </tr>
  149. </table>';
  150. } else {
  151. $content .= '<table>
  152. <tr>
  153. <td>' . $dia . '</td>
  154. <td>0</td>
  155. <td>0</td>
  156. <td>0</td>
  157. </tr>
  158. </table>';
  159. }
  160. }
  161. $content .= '</td>';
  162. $content .= '</tr>';
  163. }
  164. } //*/
  165. $file = "reporte.html"; // \Yii::$app->getSecurity()->generateRandomString(10) . ".html";
  166. $base = \Yii::getAlias("@app") . "/web/assets";
  167. file_put_contents("{$base}/{$file}", $content);
  168. $this->stdout("/assets/{$file}\n");
  169. }
  170. }