ReporteController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace app\controllers;
  3. use yii\db\Query;
  4. use yii\web\Controller;
  5. class ReporteController extends Controller {
  6. public $layout = 'bulma';
  7. public function actionDescarga() {
  8. $req = \Yii::$app->getRequest();
  9. $pc = trim($req->get("pc", ""));
  10. $st = trim($req->get("st", ""));
  11. $fi = trim($req->get("fi", "2021-12-01"));
  12. $ff = trim($req->get("ff", ""));
  13. $sql = intval($req->get("sql", "")) === 1;
  14. $json = intval($req->get("json", "")) === 1;
  15. $query = (new Query())
  16. ->select([
  17. "{{Estacion}}.id",
  18. "{{Estacion}}.clave",
  19. "{{Estacion}}.siglas",
  20. "{{Estacion}}.frecuencia",
  21. "ciudad",
  22. "pc",
  23. "to_char(fecha, 'YYYY-MM-DD') as fecha",
  24. "extract(epoch from to_char(fecha, 'YYYY-MM-DD')::DATE) as epoch",
  25. "count(estacion) filter (where descargado = true) as descargados",
  26. "count(estacion) filter (where descargado = false) as pendientes",
  27. "count(estacion) as total"
  28. ])
  29. ->from("Descarga")
  30. ->innerJoin("Estacion", "{{Estacion}}.clave = {{Descarga}}.estacion")
  31. ->andWhere(['not', ['pc' => null]])
  32. ->groupBy(["{{Estacion}}.id", "to_char(fecha, 'YYYY-MM-DD')", "ciudad", "pc"])
  33. ->orderBy([
  34. "pc" => SORT_ASC,
  35. "clave" => SORT_ASC,
  36. "fecha" => SORT_ASC,
  37. ]);
  38. if($fi !== "") {
  39. $query->andWhere([">=", "fecha", "{$fi} 00:00:00"]);
  40. }
  41. if($ff !== "") {
  42. $query->andWhere(["<=", "fecha", "{$ff} 23:59:59"]);
  43. }
  44. if($pc !== "") {
  45. $aux = explode(",", $pc);
  46. $pcs = [];
  47. foreach($aux as $p) {
  48. $v = trim($p);
  49. if(!empty($v)) {
  50. $pcs[] = $v;
  51. }
  52. }
  53. $query->andWhere(["pc" => $pcs]);
  54. }
  55. if($st !== "") {
  56. $aux = explode(",", $st);
  57. $sts = [];
  58. foreach($aux as $p) {
  59. $v = trim($p);
  60. if(!empty($v)) {
  61. $sts[] = $v;
  62. }
  63. }
  64. $query->andWhere(["estacion" => $sts]);
  65. }
  66. if($sql) {
  67. \Yii::$app->getResponse()->format = \yii\web\Response::FORMAT_RAW;
  68. return $query->createCommand()->getRawSql();
  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. $porcentaje = $d["descargados"] * 100 / $d["total"];
  93. if($d["total"] < 240) {
  94. $color = "#ff5733";
  95. } elseif($porcentaje >= 70 && $porcentaje < 90) {
  96. $color = "darkblue";
  97. } elseif($porcentaje < 70) {
  98. $color = "blue";
  99. }
  100. $descargas[$d['pc']][$d['clave']][$d['fecha']] = [
  101. "descargados" => $d['descargados'],
  102. "pendientes" => $d['pendientes'],
  103. "total" => $d['total'],
  104. "color" => $color
  105. ];
  106. }
  107. if($json) {
  108. \Yii::$app->getResponse()->format = \yii\web\Response::FORMAT_JSON;
  109. return [
  110. "primer" => $primer,
  111. "ultimo" => $ultimo,
  112. "estaciones" => $estaciones,
  113. "descargas" => $descargas
  114. ];
  115. }
  116. return $this->render('index', [
  117. "estaciones" => $estaciones,
  118. "descargas" => $descargas,
  119. "primer" => $primer,
  120. "ultimo" => $ultimo
  121. ]);
  122. }
  123. public function actionDescargaSinPc() {
  124. $req = \Yii::$app->getRequest();
  125. $pc = trim($req->get("pc", ""));
  126. $st = trim($req->get("st", ""));
  127. $fi = trim($req->get("fi", "2021-12-01"));
  128. $ff = trim($req->get("ff", ""));
  129. $sql = intval($req->get("sql", "")) === 1;
  130. $json = intval($req->get("json", "")) === 1;
  131. $query = (new Query())
  132. ->select([
  133. "{{Estacion}}.id",
  134. "{{Estacion}}.clave",
  135. "{{Estacion}}.siglas",
  136. "{{Estacion}}.frecuencia",
  137. "ciudad",
  138. "to_char(fecha, 'YYYY-MM-DD') as fecha",
  139. "extract(epoch from to_char(fecha, 'YYYY-MM-DD')::DATE) as epoch",
  140. "count(estacion) filter (where descargado = true) as descargados",
  141. "count(estacion) filter (where descargado = false) as pendientes",
  142. "count(estacion) as total"
  143. ])
  144. ->from("Descarga")
  145. ->innerJoin("Estacion", "{{Estacion}}.clave = {{Descarga}}.estacion")
  146. ->andWhere(['not', ['pc' => null]])
  147. ->groupBy(["{{Estacion}}.id", "to_char(fecha, 'YYYY-MM-DD')", "ciudad"])
  148. ->orderBy([
  149. "ciudad" => SORT_ASC,
  150. "clave" => SORT_ASC,
  151. "fecha" => SORT_ASC,
  152. ]);
  153. if($fi !== "") {
  154. $query->andWhere([">=", "fecha", "{$fi} 00:00:00"]);
  155. }
  156. if($ff !== "") {
  157. $query->andWhere(["<=", "fecha", "{$ff} 23:59:59"]);
  158. }
  159. if($pc !== "") {
  160. $aux = explode(",", $pc);
  161. $pcs = [];
  162. foreach($aux as $p) {
  163. $v = trim($p);
  164. if(!empty($v)) {
  165. $pcs[] = $v;
  166. }
  167. }
  168. $query->andWhere(["pc" => $pcs]);
  169. }
  170. if($st !== "") {
  171. $aux = explode(",", $st);
  172. $sts = [];
  173. foreach($aux as $p) {
  174. $v = trim($p);
  175. if(!empty($v)) {
  176. $sts[] = $v;
  177. }
  178. }
  179. $query->andWhere(["estacion" => $sts]);
  180. }
  181. if($sql) {
  182. \Yii::$app->getResponse()->format = \yii\web\Response::FORMAT_RAW;
  183. return $query->createCommand()->getRawSql();
  184. }
  185. $aux = $query->all();
  186. $primer = null;
  187. $ultimo = null;
  188. if(!empty($aux)) {
  189. $tz = new \DateTimeZone("America/Mexico_City");
  190. $primer = \DateTime::createFromFormat("Y-m-d", $aux[0]['fecha'], $tz);
  191. $ultimo = \DateTime::createFromFormat("Y-m-d", $aux[count($aux) - 1]['fecha'], $tz);
  192. $primer->setTime(0, 0, 0);
  193. $ultimo->setTime(0, 0, 0);
  194. }
  195. $descargas = [];
  196. $estaciones = [];
  197. foreach($aux as $d) {
  198. if(!isset($estaciones[$d['clave']])) {
  199. $estaciones[$d['clave']] = [
  200. "clave" => $d['clave'],
  201. "siglas" => $d['siglas'],
  202. "ciudad" => $d['ciudad'],
  203. "frecuencia" => $d['frecuencia']
  204. ];
  205. }
  206. $color = "darkgreen";
  207. $porcentaje = $d["descargados"] * 100 / $d["total"];
  208. if($d["total"] < 240) {
  209. $color = "#ff5733";
  210. } elseif($porcentaje >= 70 && $porcentaje < 90) {
  211. $color = "darkblue";
  212. } elseif($porcentaje < 70) {
  213. $color = "blue";
  214. }
  215. $descargas[$d['clave']][$d['fecha']] = [
  216. "descargados" => $d['descargados'],
  217. "pendientes" => $d['pendientes'],
  218. "total" => $d['total'],
  219. "color" => $color
  220. ];
  221. }
  222. if($json) {
  223. \Yii::$app->getResponse()->format = \yii\web\Response::FORMAT_JSON;
  224. return [
  225. "primer" => $primer,
  226. "ultimo" => $ultimo,
  227. "estaciones" => $estaciones,
  228. "descargas" => $descargas
  229. ];
  230. }
  231. return $this->render('sin-pc', [
  232. "estaciones" => $estaciones,
  233. "descargas" => $descargas,
  234. "primer" => $primer,
  235. "ultimo" => $ultimo
  236. ]);
  237. }
  238. }