<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
date_default_timezone_set('Europe/Madrid');

// Parámetros
$serverName 	= "localhost";
$connectionInfo = array( "Database"=>"ICF", "UID"=>"VAIO", "PWD"=>"VAIO");
$aDeleteTransfers = [];
if(isset($_REQUEST["deleteTransfers"])) 
	$aDeleteTransfers=json_decode($_REQUEST["deleteTransfers"],true); 

// Conexión con Navision
$connNavision=false;
$retries=0;
while(!$connNavision && $retries<3){
	$connNavision = sqlsrv_connect( $serverName, $connectionInfo);
	$retries++;
}

if( $connNavision ) {
	 //echo "Conexion establecida con Navision.\r\n";
}else{
	  echo json_encode(["result"=>-11 , "text"=>"No se pudo conectar con Navision"]);
	  die;
}

$result		= [];

foreach($aDeleteTransfers as $sTransfer){
	// Borrado del cambio
	$sql = "DELETE FROM [navisionChanges] WHERE entidad='TRANSFERBASE' AND accion='U' AND codigo_antiguo='".$sTransfer."' AND codigo_nuevo='".$sTransfer."'";
	$stmt_delete_changes = sqlsrv_query( $connNavision, $sql);
	if(sqlsrv_errors(SQLSRV_ERR_ERRORS)!==NULL) {
		echo json_encode(["result"=>-12, "text"=>"1 Ocurrio un error en la consulta SQL de Navision"]);
		die;
	}
}

header('Content-Type: application/json');
echo json_encode($result, JSON_PRETTY_PRINT);
?>
