<?php
//include("conexion.php");
$json = file_get_contents('php://input');
$data = json_decode($json,true);
$ptsid  = $data['PtsId'];
$dataid = $data['Packets'][0]['Id'];
$tipotran = $data['Packets'][0]['Type'];

$fechafin = $data['Packets'][0]['Data']['DateTime'];
$fechaini = $data['Packets'][0]['Data']['DateTimeStart'];
$surtidor = $data['Packets'][0]['Data']['Pump'];
$manguera = $data['Packets'][0]['Data']['Nozzle'];
$combusid = $data['Packets'][0]['Data']['FuelGradeId'];
$combunom = $data['Packets'][0]['Data']['FuelGradeName'];
$tranid   = $data['Packets'][0]['Data']['Transaction'];
$userid   = $data['Packets'][0]['Data']['UserId'];
$volumen  = $data['Packets'][0]['Data']['Volume'];
$precio   = $data['Packets'][0]['Data']['Price'];
$totalvta = $data['Packets'][0]['Data']['Amount'];
$totsoles = $data['Packets'][0]['Data']['TotalAmount'];
$totgalon = $data['Packets'][0]['Data']['TotalVolume'];
$tcvolume = $data['Packets'][0]['Data']['TCVolume'];
$tag      = $data['Packets'][0]['Data']['Tag'];

$diferencia=time() - strtotime($fechafin);

/*$vGestor=fopen("logs/headlogpts.txt", "a");
fwrite($vGestor, "$tipotran -- select * from public.interfaz_patransaccion('$fechafin','$surtidor','$manguera','$combusid',$volumen,$precio,$totalvta,$totsoles,$totgalon)\r\n");
fclose($vGestor);*/

if (($totsoles<=0)||($totgalon<=0)){
        if ($diferencia<=50){
                echo "HTTP/1.1 200 OK\r\n";
                echo "Content-Type: application/json; charset=utf-8\r\n";
                echo "Content-Length: 188\r\n";
                echo '{"Protocol":"jsonPTS","Packets":[{"Id":'.$dataid.',"Type":"UploadPumpTransaction","Error":true}]}';
                $vGestor=fopen("logs/logceros".date("Ymd").".txt", "a");
                fwrite($vGestor, "select * from public.interfaz_patransaccion('$fechafin','$surtidor','$manguera','$combusid',$volumen,$precio,$totalvta,$totsoles,$totgalon)--".date("Y-m-d H:i:s")." \r\n");
                fclose($vGestor);
                exit;
        }
}

if ($tipotran=="UploadPumpTransaction"){
        try {
             	$dsn = "pgsql:host=localhost;port=5432;dbname=bdgrifo_pamyplush;";
                $pdo = new PDO($dsn, "postgres", "LX300II**01", [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);

                if ($pdo) {
                        switch ($manguera){
                                case 1:$vmanguera='A';break;
                                case 2:$vmanguera='B';break;
                                case 3:$vmanguera='C';break;
                                case 4:$vmanguera='D';break;
                        }
                        $cadena="select total_soles,total_galones from \"TRAN\" where cara='".$surtidor."' and manguera='".$vmanguera."' order by numero desc limit 1";
                        if ($totsoles<=0){
                                $resultado=$pdo->query($cadena);
                                $row=$resultado->fetch();
                                $totsoles=$row['total_soles']+$totalvta;
                        }
                        if ($totgalon<=0){
                                $resultado=$pdo->query($cadena);
                                $row=$resultado->fetch();
                                $totgalon=$row['total_galones']+$volumen;
                        }

                        if ($pdo->query("select * from public.interfaz_patransaccion('$fechafin','$surtidor','$manguera','$combusid',$volumen,$precio,$totalvta,$totsoles,$totgalon);"))
                        {
                                echo "HTTP/1.1 200 OK\r\n";
                                echo "Content-Type: application/json; charset=utf-8\r\n";
                                echo "Content-Length: 188\r\n";
                                echo '{"Protocol":"jsonPTS","Packets":[{"Id":'.$dataid.',"Type":"UploadPumpTransaction","Message":"OK"}]}';
                                $vGestor=fopen("logs/logpts".date("Ymd").".txt", "a");
                                fwrite($vGestor, "select * from public.interfaz_patransaccion('$fechafin','$surtidor','$manguera','$combusid',$volumen,$precio,$totalvta,$totsoles,$totgalon)\r\n");
                                fclose($vGestor);
                        }
                }
        } catch (PDOException $e) {
                $vGestor=fopen("logs/errorlogpts".date("Ymd").".txt", "a");
                fwrite($vGestor, $e->getMessage()."\r\n");
                fclose($vGestor);
                die($e->getMessage());

        } finally {
                if ($pdo) {
                        $pdo = null;
                }
        }
}
?>
