<?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'][0]['DateTime'];
$fechaini = $data['Packets'][0]['Data'][0]['DateTimeStart'];
$surtidor = $data['Packets'][0]['Data'][0]['Pump'];
$manguera = $data['Packets'][0]['Data'][0]['Nozzle'];
$combusid = $data['Packets'][0]['Data'][0]['FuelGradeId'];
$combunom = $data['Packets'][0]['Data'][0]['FuelGradeName'];
$tranid   = $data['Packets'][0]['Data'][0]['Transaction'];
$userid   = $data['Packets'][0]['Data'][0]['UserId'];
$volumen  = $data['Packets'][0]['Data'][0]['Volume'];
$precio   = $data['Packets'][0]['Data'][0]['Price'];
$totalvta = $data['Packets'][0]['Data'][0]['Amount'];
$totsoles = $data['Packets'][0]['Data'][0]['TotalAmount'];
$totgalon = $data['Packets'][0]['Data'][0]['TotalVolume'];
$tcvolume = $data['Packets'][0]['Data'][0]['TCVolume'];
$tag      = $data['Packets'][0]['Data'][0]['Tag'];

if ($tipotran=="UploadPumpTransaction"){
	$servername = "redmovildenegocios.com";
	$database = "oil_20608885855";
	$username = "O1L20608885855";
	$password = "2O060888585IL75";
	// Crear connexion
	$conn = mysqli_connect($servername, $username, $password, $database);
	// Verificar conexion
	if (!$conn) {
	      die("Conexion fallida: " . mysqli_connect_error());
	}
	$sql = "INSERT INTO oil_venta VALUES ('$ptsid',1,'$fechaini','$surtidor','$manguera',$tranid,'$combusid','$combunom','$userid',$volumen,$totalvta,$precio,$totgalon,$totsoles,'$fechfin',$tcvolume,$tag,CURRENT_TIMESTAMP, NULL, NULL, NULL, NULL,1);"; 
	if (mysqli_query($conn, $sql)) {
		$resp1 ="HTTP/1.1 200 OK\r\nContent-Type: application/json; charset=utf-8\r\n";
		$long ="Content-Length:";
		$resp2='{
 			"Protocol":"jsonPTS",
			 "Packets":[{
				 "Id":'.$dataid.',
				 "Type":"UploadPumpTransaction",
				 "Message":"OK"
				}
 		]
	}';
		$ncar=strlen($resp1.$long.$resp2);
		$resp=$resp1.$long.$ncar."\r\n".$resp2;
		echo $resp;

	} else {
	      echo "Error: " . $sql . "<br>" . mysqli_error($conn);
		}
	mysqli_close($conn);
}
?>
