<?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");
$scriptPath = 'do-NAVISION-sendDiscordMessage.php';
//$sDiscordChannel= "1075385202303586324"; // Antonio, pruebas
$sDiscordChannel= "806434577437556776"; // Dpto. Compras

// 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"=>-1 , "text"=>"No se pudo conectar con Navision"]);
	  die;
}

function mssql_escape($str)
{
    if(get_magic_quotes_gpc())
    {
        $str= stripslashes($str);
    }
    return str_replace("'", "''", $str);
}
$row=[];
$code='24PV32535';
$row['CodFacturaTemp']='24PV32613';

	$sql = 'SELECT * FROM [dbo].[Ind. Campollano Ferretería S.L$Sales Header] WHERE [No.]=\''.$code.'\'';
	$smtpOriginalOrder=sqlsrv_query( $connNavision, $sql );
				if($rowOriginalOrder = sqlsrv_fetch_array( $smtpOriginalOrder, SQLSRV_FETCH_ASSOC)){
					$sql = "UPDATE ".'[Ind. Campollano Ferretería S.L$Sales Header]'." SET 
					[Estado Oferta]=0, 
					[Bill-to Customer No.]='".$rowOriginalOrder["Bill-to Customer No."]."',
					[Bill-to Name]='".$rowOriginalOrder["Bill-to Name"]."',
					[Bill-to Name 2]='".$rowOriginalOrder["Bill-to Name 2"]."',
					[Bill-to Address]='".$rowOriginalOrder["Bill-to Address"]."',
					[Bill-to Address 2]='".$rowOriginalOrder["Bill-to Address 2"]."',
					[Bill-to City]='".$rowOriginalOrder["Bill-to City"]."',
					[Bill-to Contact]='".$rowOriginalOrder["Bill-to Contact"]."',
					[Ship-to Code]='".$rowOriginalOrder["Ship-to Code"]."',
					[Ship-to Name]='".$rowOriginalOrder["Ship-to Name"]."',
					[Ship-to Name 2]='".$rowOriginalOrder["Ship-to Name 2"]."',
					[Ship-to Address]='".$rowOriginalOrder["Ship-to Address"]."',
					[Ship-to Address 2]='".$rowOriginalOrder["Ship-to Address 2"]."',
					[Ship-to City]='".$rowOriginalOrder["Ship-to City"]."',
					[Ship-to Contact]='".$rowOriginalOrder["Ship-to Contact"]."',
					[Sell-to Customer Name]='".$rowOriginalOrder["Sell-to Customer Name"]."',
					[Sell-to Customer Name 2]='".$rowOriginalOrder["Sell-to Customer Name 2"]."',
					[Sell-to Address]='".$rowOriginalOrder["Sell-to Address"]."',
					[Sell-to Address 2]='".$rowOriginalOrder["Sell-to Address 2"]."',
					[Sell-to City]='".$rowOriginalOrder["Sell-to City"]."',
					[Sell-to Contact]='".$rowOriginalOrder["Sell-to Contact"]."',
					[Payment Terms Code]='".$rowOriginalOrder["Payment Terms Code"]."',
					[Shipment Method Code]='".$rowOriginalOrder["Shipment Method Code"]."',
					[Ship]='".$rowOriginalOrder["Ship"]."',
					[Invoice]='".$rowOriginalOrder["Invoice"]."',
					[Salesperson Code]='".$rowOriginalOrder["Salesperson Code"]."',
					[Payment Method Code]='".$rowOriginalOrder["Payment Method Code"]."',
					[Shipping Agent Code]='".$rowOriginalOrder["Shipping Agent Code"]."',
					[Invoice Discount Calculation]='".$rowOriginalOrder["Invoice Discount Calculation"]."',
					[Assigned User ID]='".$rowOriginalOrder["Assigned User ID"]."',
					[Creation user]='".$rowOriginalOrder["Creation user"]."',
					[Pedido WEB]='".$rowOriginalOrder["Pedido WEB"]."',
					[External Document No.]='".$rowOriginalOrder["External Document No."]."',		
					[Package Tracking No.]='".$code."'		
					where [No.]='".$row['CodFacturaTemp']."' and [Document Type]=1";
					$smtpSaveOrder=sqlsrv_query( $connNavision, $sql );
					if( $smtpSaveOrder === false) {
						echo json_encode(["result"=>-4,"text"=>"Error al actualizar la cabecera"]);
						die( print_r( sqlsrv_errors(), true) );
					}
				}
				//Modificado el 26/08/2024: Antes se lanzaban automaticamente los pedidos de restos. Ahora se validan por el agente en cuestion
				//$sql = "UPDATE ".'[Ind. Campollano Ferretería S.L$Sales Header]'." set [Estado Oferta]=0, [Status]=1 where [No.]='".$row['CodFacturaTemp']."' and [Document Type]=1";
