Advertisement
Guest User

Untitled

a guest
Oct 14th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.59 KB | None | 0 0
  1. <?php
  2.   $db = pg_connect("host=10.50.114.201 port=5432 dbname=sube user=subeweb password=WGCO6sfygF");
  3.  
  4.   $linea = addslashes($_GET['linea']);
  5.   $fecha = addslashes($_GET['fecha']);
  6.   if ($linea != 'mitre') {
  7.     $sql = "
  8.        SELECT
  9.          e.estacion as id_est,
  10.          t2.hora,
  11.          e.est_desc as descripcion,
  12.          coalesce(t2.cant_uso_checkin+t2.cant_checkout_sin_checkin,0) checkin,
  13.          coalesce(t2.cant_checkout,0) checkout
  14.        FROM
  15.          ".$linea.".estaciones e
  16.        LEFT JOIN
  17.        (
  18.          SELECT
  19.            case when codigo_operacion in (114,124,125) then check_out else check_in end as estacion,
  20.            extract(HOUR from fecha) hora,
  21.            sum(case when (codigo_operacion=124) then 1 else 0 end) as cant_checkout_sin_checkin,
  22.            sum(case when (codigo_operacion=113) then 1 else 0 end) as cant_uso_checkin,
  23.            sum(case when (codigo_operacion in (114,125)) then 1 else 0 end) as cant_checkout
  24.          FROM
  25.            ".$linea.".mov_monetario M
  26.          WHERE
  27.            M .fecha >= '".$fecha." 00:00:00' :: TIMESTAMP
  28.            AND M .fecha < '".$fecha." 23:59:59' :: TIMESTAMP
  29.            AND codigo_operacion <> 123
  30.          GROUP BY
  31.            estacion, extract(HOUR from fecha)
  32.        ) t2 ON e.estacion = t2.estacion
  33.        WHERE e.estacion>0
  34.        ORDER BY
  35.          e.estacion,
  36.          t2.hora";
  37.   }
  38.   else {
  39.     $sql = "
  40.        SELECT
  41.          e.estacion as id_est,
  42.          t2.hora,
  43.          e.est_desc as descripcion,
  44.          coalesce(t2.cant_uso_checkin+t2.cant_checkout_sin_checkin,0) checkin,
  45.          coalesce(t2.cant_checkout,0) checkout
  46.        FROM
  47.          ".$linea.".estaciones e
  48.        LEFT JOIN
  49.        (
  50.          SELECT
  51.            mol.estacion,
  52.            extract(HOUR from fecha) hora,
  53.            sum(case when (codigo_operacion=124) then 1 else 0 end) as cant_checkout_sin_checkin,
  54.            sum(case when (codigo_operacion=113) then 1 else 0 end) as cant_uso_checkin,
  55.            sum(case when (codigo_operacion in (114,125)) then 1 else 0 end) as cant_checkout
  56.          FROM
  57.            ".$linea.".mov_monetario M
  58.          INNER JOIN
  59.            ".$linea.".reporte rep ON rep.id_reporte = M.id_reporte
  60.          INNER JOIN
  61.            ".$linea.".molinetes mol ON mol.id_molinete = rep.nro_interno
  62.          WHERE
  63.            M .fecha >= '".$fecha." 00:00:00' :: TIMESTAMP
  64.            AND M .fecha < '".$fecha." 23:59:59' :: TIMESTAMP
  65.            AND codigo_operacion <> 123
  66.          GROUP BY
  67.            mol.estacion, extract(HOUR from fecha)
  68.        ) t2 ON e.estacion = t2.estacion
  69.        WHERE e.estacion>0
  70.        ORDER BY
  71.          e.estacion,
  72.          t2.hora";
  73.   }
  74.  
  75.   $serie1 = [];
  76.   $serie2 = [];
  77.   $nombres = [];
  78.   $result = pg_query($sql);
  79.   while ($rs = pg_fetch_array($result)) {
  80.     if (!array_key_exists($rs['id_est'], $serie1)) {
  81.       $serie1[$rs['id_est']] = [];
  82.       $serie2[$rs['id_est']] = [];
  83.     }
  84.     $nombres[$rs['id_est']] = $rs['descripcion'];
  85.     $h = str_pad($rs['hora'],2,'0', STR_PAD_LEFT);
  86.     $serie1[$rs['id_est']][$h] = $rs['checkin'];
  87.     $serie2[$rs['id_est']][$h] = $rs['checkout'];
  88.   }
  89.   foreach ($serie1 as $idest => $d1) {
  90.     for ($i=0; $i < 24; $i++) {
  91.       $h = str_pad($i,2,'0', STR_PAD_LEFT);
  92.       if (!array_key_exists($h, $serie1[$idest])) {
  93.         $serie1[$idest][$h] = 0;
  94.         $serie2[$idest][$h] = 0;
  95.       }
  96.     }
  97.     ksort($serie1[$idest]);
  98.     ksort($serie2[$idest]);
  99.   }
  100.   echo json_encode(['transacciones_pagas' => $serie1, 'check_out' => $serie2, 'estaciones' => $nombres]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement