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

header('Content-Type: application/json');


function utf8_converter($array)
{
    array_walk_recursive($array, function(&$item, $key){
        if(mb_detect_encoding($item, 'utf-8', true)){
                $item = utf8_decode($item);
				$item = str_replace("'", "''", $item );
				$item = str_replace("/", "//", $item );
        }
    });
 
    return $array;
}



if(isset($_POST["json"])) {
	$json=json_decode($_POST["json"], true); 
	$json=utf8_converter($json);

}else{
	echo json_encode(["result"=>-10, "text"=>"Faltan parametros en la llamada a Navision"]);
	die;
}


/*
if(isset($_REQUEST["json"])) 
	$json=json_decode($_REQUEST["json"], true); 
else{
	echo json_encode(["result"=>-10, "text"=>"Faltan parametros en la llamada a Navision"]);
	die;
}
*/


function mssql_escape($str)
{
    if(get_magic_quotes_gpc()){ 
        $str= stripslashes($str);
    }
    return str_replace("'", "''", $str);
}

$serverName = "localhost";
$connectionInfo = array( "Database"=>"ICF", "UID"=>"VAIO", "PWD"=>"VAIO");

$connNavision=false;
$retries=0;

while(!$connNavision && $retries<3){
	$connNavision = sqlsrv_connect( $serverName, $connectionInfo);
	$retries++;
}
if(!$connNavision ){
	 echo json_encode(["result"=>-11 , "text"=>"No se pudo conectar con Navision"]);
	 die;
}

	
			
	$exist=false;
	$sqlExist='SELECT * FROM dbo.[Ind. Campollano Ferretería S.L$Purchase Header] WHERE [No.]=\''.$json["No."].'\'';
	$smtpExist=sqlsrv_query( $connNavision, $sqlExist );
	
	if($row = sqlsrv_fetch_array( $smtpExist, SQLSRV_FETCH_ASSOC)){
		
		$exist=true;
		
	}
	
	if(!$exist)
	{
		
	$sqlAgent='SELECT [User ID] from dbo.[Ind. Campollano Ferretería S.L$User Setup] where [E-Mail]=\''.$json["Assigned User ID"].'\'';
	$smtpAgent=sqlsrv_query( $connNavision, $sqlAgent );
	
	
	if($row = sqlsrv_fetch_array( $smtpAgent, SQLSRV_FETCH_ASSOC)){
				$result=$row["User ID"];
			}
				
	
		
	$sqlPurchaserCode='SELECT [Code] from dbo.[Ind. Campollano Ferretería S.L$Salesperson_Purchaser] where [E-Mail]=\''.$json["Assigned User ID"].'\'';
	$smtpPurchaserCode=sqlsrv_query( $connNavision, $sqlPurchaserCode );
	
	$purchasercode=0;
	if($row = sqlsrv_fetch_array( $smtpPurchaserCode, SQLSRV_FETCH_ASSOC)){
				$purchasercode=$row["Code"];
			}
			
			$orderDate=new DateTime($json["Order Date"]["date"]);
			$fechaLanzamiento=new DateTime($json["Fecha 1.lanzamiento"]["date"]);
			$documentDate=new DateTime($json["Document Date"]["date"]);
			
			//$address1=str_replace("'","''",$json["Buy-from Address"]);
			//$address2=str_replace("'","''",$json["Buy-from Address 2"]);


			$json_lines=$json["lines"];

				$sql = 'INSERT INTO dbo.[Ind. Campollano Ferretería S.L$Purchase Header]'." (
						[Document Type],
						[No.],
						[Buy-from Vendor No.],
						[Assigned User ID],
						[Purchaser Code],
						[Buy-from Vendor Name],
						[Buy-from Vendor Name 2],
						[Buy-from Address],
						[Buy-from Address 2],
						[Buy-from Post Code],
						[Buy-from City],
						[Buy-from County],
						[No oferta relacionada],
						[Ship-to Post Code],
						[Status],
						[VAT Registration No.],
						[Document Date],
						[Fecha 1.lanzamiento],
						[Payment Method Code], 
						[Pay-to Vendor No.] ,
						[Pay-to Name] ,
						[Pay-to Name 2] ,
						[Pay-to Address] ,
						[Pay-to Address 2] ,
						[Pay-to City] ,
						[Pay-to Contact] ,
						[Your Reference] ,
						[Ship-to Code] ,
						[Ship-to Name] ,
						[Ship-to Name 2] ,
						[Ship-to Address] ,
						[Ship-to Address 2] ,
						[Ship-to City] ,
						[Ship-to Contact] ,
						[Order Date] ,
						[Posting Date] ,
						[Expected Receipt Date] ,
						[Posting Description] ,
						[Payment Terms Code] ,
						[Due Date] ,
						[Payment Discount %] ,
						[Pmt. Discount Date] ,
						[Shipment Method Code] ,
						[Location Code] ,
						[Shortcut Dimension 1 Code] ,
						[Shortcut Dimension 2 Code] ,
						[Vendor Posting Group] ,
						[Currency Code] ,
						[Currency Factor] ,
						[Prices Including VAT] ,
						[Invoice Disc. Code] ,
						[Language Code] ,
						[Order Class] ,
						[No. Printed] ,
						[On Hold],
						[Applies-to Doc. Type] ,
						[Applies-to Doc. No.] ,
						[Bal. Account No.] ,
						[Receive] ,
						[Invoice] ,
						[Receiving No.] ,
						[Posting No.] ,
						[Last Receiving No.] ,
						[Last Posting No.] ,
						[Vendor Order No.] ,
						[Vendor Shipment No.] ,
						[Vendor Invoice No.] ,
						[Vendor Cr. Memo No.] ,
						[Sell-to Customer No.] ,
						[Reason Code] ,
						[Gen. Bus. Posting Group] ,
						[Transaction Type] ,
						[Transport Method] ,
						[VAT Country_Region Code] ,
						[Buy-from Contact] ,
						[Pay-to Post Code] ,
						[Pay-to County] ,
						[Pay-to Country_Region Code] ,
						[Buy-from Country_Region Code] ,
						[Ship-to County] ,
						[Ship-to Country_Region Code] ,
						[Bal. Account Type] ,
						[Order Address Code] ,
						[Entry Point] ,
						[Correction] ,
						[Area] ,
						[Transaction Specification] ,
						[No. Series] ,
						[Posting No. Series] ,
						[Receiving No. Series] ,
						[Tax Area Code] ,
						[Tax Liable] ,
						[VAT Bus. Posting Group] ,
						[Applies-to ID] ,
						[VAT Base Discount %] ,
						[Invoice Discount Calculation] ,
						[Invoice Discount Value] ,
						[Send IC Document] ,
						[IC Status] ,
						[Buy-from IC Partner Code] ,
						[Pay-to IC Partner Code] ,
						[IC Direction] ,
						[Prepayment No.] ,
						[Last Prepayment No.] ,
						[Prepmt. Cr. Memo No.] ,
						[Last Prepmt. Cr. Memo No.] ,
						[Prepayment %] ,
						[Prepayment No. Series] ,
						[Compress Prepayment] ,
						[Prepayment Due Date] ,
						[Prepmt. Cr. Memo No. Series] ,
						[Prepmt. Posting Description] ,
						[Prepmt. Pmt. Discount Date] ,
						[Prepmt. Payment Terms Code] ,
						[Prepmt. Payment Discount %] ,
						[Doc. No. Occurrence] ,
						[Campaign No.] ,
						[Buy-from Contact No.] ,
						[Pay-to Contact No.] ,
						[Responsibility Center] ,
						[Posting from Whse. Ref.] ,
						[Requested Receipt Date] ,
						[Promised Receipt Date] ,
						[Lead Time Calculation] ,
						[Inbound Whse. Handling Time] ,
						[Vendor Authorization No.] ,
						[Return Shipment No.] ,
						[Return Shipment No. Series] ,
						[Ship] ,
						[Last Return Shipment No.] ,
						[Generate Autoinvoices] ,
						[Generate Autocredit Memo] ,
						[Corrected Invoice No.] ,
						[Estado pedido] ,
						[Cod. Banco domic.] ,
						[Fecha cierre] ,
						[Regimen fiscal IRPF] ,
						[Tipo base IRPF] ,
						[% IRPF] ,
						[Doc. Rectificado] ,
						[Tipo Doc. Rectificado] ,
						[Tipo Facturas (349)] ,
						[Applies-to Bill No.] ,
						[Vendor Bank Acc. Code] ,
						[Pay-at Code] ,
						[Historic] ,
						[Contestado por Proveedor] ,
						[incluir recl. cant. pendientes] ,
						[Importe portes] ,
						[Peso total] ,
						[Dto por pesos] ,
						[% seguro] ,
						[No oferta patron] ,
						[Fax Documento] ,
						[Email Documento],
						[Cod. Transportista] ,
						[No Transporte] ,
						[Plazo medio entrega (días) Ped] ,
						[Plazo total entrega (días) Ped] ,
						[Es Devolucion] ,
						[Factura rappel] ,
						[Date Received] ,
						[Time Received] ,
						[BizTalk Purchase Quote] ,
						[BizTalk Purch. Order Cnfmn.] ,
						[BizTalk Purchase Invoice] ,
						[BizTalk Purchase Receipt] ,
						[BizTalk Purchase Credit Memo] ,
						[Date Sent] ,
						[Time Sent] ,
						[BizTalk Request for Purch. Qte] ,
						[BizTalk Purchase Order] ,
						[Vendor Quote No.] ,
						[BizTalk Document Sent] ,
						[Packages] ,
						[FechaReclamacion] ,
						[PorcentajeRecibido] ,
						[Telefono] 
						) VALUES (
										1,
						'".$json["No."]."',
						'".$json["Buy-from Vendor No."]."',
						'$result',
						'$purchasercode',
						'".$json["Buy-from Vendor Name"]."',
						'".$json["Buy-from Vendor Name 2"]."',
						'".$json["Buy-from Address"]."',
						'".$json["Buy-from Address 2"]."',
						'".$json["Buy-from Post Code"]."',
						'".$json["Buy-from City"]."',
						'".$json["Buy-from County"]."',
						'".$json["No oferta relacionada"]."',
						'".$json["Ship-to Post Code"]."',
						'".$json["Status"]."',
						'".$json["VAT Registration No."]."',
						'".$documentDate->format("Y-m-d").'T00:00:00.000'."',
						'".$fechaLanzamiento->format("Y-m-d").'T00:00:00.000'."',
						'".$json["Payment Method Code"]."',
						'',
						'',
						'',
						'',
						'',
						'',
						'',
						'',
						'',
						'',
						'',
						'',
						'',
						'',
						'',
						'".$orderDate->format("Y-m-d").'T00:00:00.000'."',
						'1753-01-01T00:00:00.000',
						'1753-01-01T00:00:00.000',
						'Pedido ".$json["No."]."',
						'',
						'1753-01-01T00:00:00.000',
						0,
						'1753-01-01T00:00:00.000',
						'',
						'ALM01',
						'',
						'',
						'NAC',
						'',
						0,
						0,
						'',
						'',
						'',
						0,
						'',
						0,
						'',
						'',
						0,
						0,
						'',
						'',
						'',
						'',
						'',
						'',
						'',
						'',
						'',
						'',
						'NACIONAL',
						'',
						'',
						'ES',
						'',
						'',
						'',
						'ES',
						'ES',
						'ALBACETE',
						'02007',
						0,
						'',
						'',
						0,
						'',
						'',
						'C-PED',
						'C-FAC+',
						'C-ALB',
						'',
						0,
						'NAC',
						'',
						0,
						0,
						0,
						0,
						0,
						'',
						'',
						0,
						'',
						'',
						'',
						'',
						0,
						'PREP-INT+',
						1,
						'1753-01-01T00:00:00.000',
						'PREP-AB+',
						'',
						'1753-01-01T00:00:00.000',
						'',
						0,
						1,
						'',
						'',
						'',
						'',
						0,
						'1753-01-01T00:00:00.000',
						'1753-01-01T00:00:00.000',
						'',
						'',
						'',
						'',
						'',
						0,
						'',
						0,
						0,
						'',
						0,
						'',
						'1753-01-01T00:00:00.000',
						'',
						0,
						0,
						'',
						0,
						0,
						'',
						'',
						'',
						0,
						0,
						0,
						0,
						0,
						0,
						0,
						'',
						'',
						'',
						'',
						0,
						0,
						0,
						".$json["Es Devolucion"].",
						0,
						'1753-01-01T00:00:00.000',
						'1753-01-01T00:00:00.000',
						0,
						0,
						0,
						0,
						0,
						'1753-01-01T00:00:00.000',
						'1753-01-01T00:00:00.000',
						0,
						0,
						'',
						0,
						0,
						'1753-01-01T00:00:00.000',
						0,
						''
						);";



			//echo $sql;

			$stmt = sqlsrv_query( $connNavision, $sql );
			if(sqlsrv_errors(SQLSRV_ERR_ERRORS)===NULL) echo json_encode(["result"=>1]); else echo json_encode(["result"=>-12, "text"=>"1 Ocurrio un error en la consulta SQL de Navision"]);
			/*
				if( ($errors = sqlsrv_errors() ) != null) {
					foreach( $errors as $error ) {
						echo "SQLSTATE: ".$error[ 'SQLSTATE']."<br />";
						echo "code: ".$error[ 'code']."<br />";
						echo "message: ".$error[ 'message']."<br />";
					}
				}
			*/	
		foreach($json_lines as $line)
		{
			
			$sqlUnidadMedidaPrecio='SELECT [Unidad medida precio] from dbo.[Item] WHERE [No.]=\''.$line["No."].'\'';
			$smtpUnidadMedidaPrecio=sqlsrv_query( $connNavision, $sqlUnidadMedidaPrecio );
			
			$unidadmedidaprecio="UD";
			$codunidadmedidoprecio="UD";
			$descripcionumprecio="Unidades";
			
			if($row = sqlsrv_fetch_array( $smtpUnidadMedidaPrecio, SQLSRV_FETCH_ASSOC)){
						$unidadmedidaprecio=$row["Unidad medida precio"];
						
			}
					
			if($unidadmedidaprecio=="C")
			{
				
				$codunidadmedidoprecio="C";
				$descripcionumprecio="Centenar";
			}
			
			
			$sql = 'INSERT INTO dbo.[Ind. Campollano Ferretería S.L$Purchase Line]'." (
						[Document Type],
						[Document No.],
						[Line No.],
						[Buy-from Vendor No.],
						[Type],
						[No.],
						[Location Code],
						[Posting Group],
						[Expected Receipt Date],
						[Description],
						[Description 2],
						[Unit of Measure],
						[Quantity],
						[Outstanding Quantity],
						[Qty. to Invoice],
						[Qty. to Receive],
						[Direct Unit Cost],
						[Unit Cost (LCY)],
						[VAT %], 
						[Line Discount %] ,
						[Line Discount Amount] ,
						[Amount] ,
						[Amount Including VAT] ,
						[Unit Price (LCY)] ,
						[Allow Invoice Disc.] ,
						[Gross Weight] ,
						[Net Weight] ,
						[Units per Parcel] ,
						[Unit Volume] ,
						[Appl.-to Item Entry] ,
						[Shortcut Dimension 1 Code] ,
						[Shortcut Dimension 2 Code] ,
						[Job No.] ,
						[Indirect Cost %] ,
						[Outstanding Amount] ,
						[Qty. Rcd. Not Invoiced] ,
						[Amt. Rcd. Not Invoiced] ,
						[Quantity Received] ,
						[Quantity Invoiced] ,
						[Receipt No.] ,
						[Receipt Line No.] ,
						[Profit %] ,
						[Pay-to Vendor No.] ,
						[Inv. Discount Amount] ,
						[Vendor Item No.] ,
						[Sales Order No.] ,
						[Sales Order Line No.] ,
						[Drop Shipment] ,
						[Gen. Bus. Posting Group] ,
						[Gen. Prod. Posting Group] ,
						[VAT Calculation Type] ,
						[Transaction Type] ,
						[Transport Method] ,
						[Attached to Line No.] ,
						[Entry Point],
						[Area] ,
						[Transaction Specification] ,
						[Tax Area Code] ,
						[Tax Liable] ,
						[Tax Group Code] ,
						[Use Tax] ,
						[VAT Bus. Posting Group] ,
						[VAT Prod. Posting Group] ,
						[Currency Code] ,
						[Outstanding Amount (LCY)] ,
						[Amt. Rcd. Not Invoiced (LCY)] ,
						[Blanket Order No.] ,
						[Blanket Order Line No.] ,
						[VAT Base Amount] ,
						[Unit Cost] ,
						[System-Created Entry] ,
						[Line Amount] ,
						[VAT Difference] ,
						[Inv. Disc. Amount to Invoice] ,
						[VAT Identifier] ,
						[IC Partner Ref. Type] ,
						[IC Partner Reference] ,
						[Prepayment %] ,
						[Prepmt. Line Amount] ,
						[Prepmt. Amt. Inv.] ,
						[Prepmt. Amt. Incl. VAT] ,
						[Prepayment Amount] ,
						[Prepmt. VAT Base Amt.] ,
						[Prepayment VAT %] ,
						[Prepmt. VAT Calc. Type] ,
						[Prepayment VAT Identifier] ,
						[Prepayment Tax Area Code] ,
						[Prepayment Tax Liable] ,
						[Prepayment Tax Group Code] ,
						[Prepmt Amt to Deduct] ,
						[Prepmt Amt Deducted] ,
						[Prepayment Line] ,
						[Prepmt. Amount Inv. Incl. VAT] ,
						[Prepmt. Amount Inv. (LCY)] ,
						[IC Partner Code] ,
						[Prepayment VAT Difference] ,
						[Prepmt VAT Diff. to Deduct] ,
						[Prepmt VAT Diff. Deducted] ,
						[Job Task No.] ,
						[Job Line Type] ,
						[Job Unit Price] ,
						[Job Total Price] ,
						[Job Line Amount] ,
						[Job Line Discount Amount] ,
						[Job Line Discount %] ,
						[Job Unit Price (LCY)] ,
						[Job Total Price (LCY)] ,
						[Job Line Amount (LCY)] ,
						[Job Line Disc. Amount (LCY)] ,
						[Job Currency Factor] ,
						[Job Currency Code] ,
						[Prod. Order No.] ,
						[Variant Code] ,
						[Bin Code] ,
						[Qty. per Unit of Measure] ,
						[Unit of Measure Code] ,
						[Quantity (Base)] ,
						[Outstanding Qty. (Base)] ,
						[Qty. to Invoice (Base)] ,
						[Qty. to Receive (Base)] ,
						[Qty. Rcd. Not Invoiced (Base)] ,
						[Qty. Received (Base)] ,
						[Qty. Invoiced (Base)] ,
						[FA Posting Date] ,
						[FA Posting Type] ,
						[Depreciation Book Code] ,
						[Salvage Value] ,
						[Depr. until FA Posting Date] ,
						[Depr. Acquisition Cost] ,
						[Maintenance Code] ,
						[Insurance No.] ,
						[Budgeted FA No.] ,
						[Duplicate in Depreciation Book] ,
						[Use Duplication List] ,
						[Responsibility Center] ,
						[Cross-Reference No.] ,
						[Unit of Measure (Cross Ref.)] ,
						[Cross-Reference Type] ,
						[Cross-Reference Type No.] ,
						[Item Category Code] ,
						[Nonstock] ,
						[Purchasing Code] ,
						[Product Group Code] ,
						[Special Order] ,
						[Special Order Sales No.] ,
						[Special Order Sales Line No.] ,
						[Completely Received] ,
						[Requested Receipt Date] ,
						[Promised Receipt Date] ,
						[Lead Time Calculation] ,
						[Inbound Whse. Handling Time] ,
						[Planned Receipt Date] ,
						[Order Date] ,
						[Allow Item Charge Assignment] ,
						[Return Qty. to Ship],
						[Return Qty. to Ship (Base)] ,
						[Return Qty. Shipped Not Invd.] ,
						[Ret. Qty. Shpd Not Invd.(Base)] ,
						[Return Shpd. Not Invd.] ,
						[Return Shpd. Not Invd. (LCY)] ,
						[Return Qty. Shipped] ,
						[Return Qty. Shipped (Base)] ,
						[Return Shipment No.] ,
						[Return Shipment Line No.] ,
						[Return Reason Code] ,
						[Pmt. Disc. Rcd. Amount] ,
						[EC %] ,
						[EC Difference] ,
						[Prepayment EC %] ,
						[% Descuento 1] ,
						[% Descuento 2] ,
						[% Descuento 3] ,
						[% Descuento 4] ,
						[ViejoPrepmt. Amount Inv. (LCY)] ,
						[Estado línea pedido] ,
						[Cantidad inicial] ,
						[Cantidad inicial base] ,
						[Sujeto IRPF] ,
						[% IRPF] ,
						[Base IRPF] ,
						[Base IRPF (DL)] ,
						[Importe IRPF] ,
						[Importe IRPF (DL)] ,
						[Proveedor extranjero] ,
						[Base imponible (DUA)] ,
						[Fecha admisión (DUA)] ,
						[Identificación DUA] ,
						[Cod. Marca] ,
						[Historic] ,
						[Importe pendiente base] ,
						[Importe pendiente base (DL)] ,
						[Estado documento] ,
						[Cod. unidad medida precio] ,
						[Coste unit. directo UM precio] ,
						[Unit Cost UM Precio] ,
						[Cdad por UM Precio] ,
						[Descripcion UM precio] ,
						[Pasar a pedido] ,
						[Procede de No oferta] ,
						[Fecha cierre linea] ,
						[Es devolucion documento] ,
						[Dto por pesos] ,
						[Drop Shipment simul] ,
						[Sales Order No. simul] ,
						[Sales Order Line No. simul] ,
						[Cantidad modif.] ,
						[No albaran dev] ,
						[No linea alb. Dev.] ,
						[No linea alb] ,
						[Routing No.] ,
						[Operation No.] ,
						[Work Center No.] ,
						[Finished] ,
						[Prod. Order Line No.] ,
						[Overhead Rate] ,
						[MPS Order] ,
						[Planning Flexibility] ,
						[Safety Lead Time] ,
						[Routing Reference No.] ,
						[Es Devolucion] ,
						[Nº línea lista materiales] ,
						[BOM Item No.] ,
						[Skip purchase discount] ,
						[Peso] ,
						[Pedido WEB]
						) VALUES (
							1,
				'".$line["Document No."]."',
				".$line["Line No."].",
				'".$json["Buy-from Vendor No."]."',
				2,
				'".$line["No."]."',
				'ALM01',
				'EXISTENCIA',
				'1753-01-01T00:00:00.000',
				'".$line["Description"]."',
				'".$line["Description 2"]."',
				'Unidades',
				".$line["Quantity"].",
				".$line["Outstanding Quantity"].",
				0,
				0,
				".$line["Direct Unit Cost"].",
				".$line["Unit Cost (LCY)"].",
				".$line["VAT %"].",
				".$line["Line Discount %"].",
				".$line["Line Discount Amount"].",
				".$line["Amount"].",
				".$line["Amount including VAT"].",
				".$line["Unit price (LCY)"].",
				1,
				0,
				0,
				0,
				0,
				0,
				'',
				'',
				'',
				0,
				0,
				0,
				0,
				0,
				0,
				'',
				0,
				0,
				'".$json["Buy-from Vendor No."]."',
				0,
				'',
				'',
				0,
				0,
				'NACIONAL',
				'MERCADERIA',
				0,
				'',
				'',
				0,
				'',
				'',
				'',
				'',
				0,
				'',
				0,
				'NAC',
				'IVA21',
				'',
				0,
				0,
				'',
				0,
				".$line["VAT Base Amount"].",
				".$line["Unit Cost"].",
				0,
				".$line["Line Amount"].",
				0,
				0,
				'VAT21',
				0,
				'',
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				'',
				'',
				0,
				'',
				0,
				0,
				0,
				0,
				0,
				'',
				0,
				0,
				0,
				'',
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				'',
				'',
				'',
				'',
				1,
				'UD',
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				'1753-01-01T00:00:00.000',
				0,
				'',
				0,
				0,
				0,
				'',
				'',
				'',
				'',
				0,
				'',
				'',
				'',
				0,
				'',
				'',
				0,
				'',
				'',
				0,
				'',
				0,
				0,
				'1753-01-01T00:00:00.000',
				'1753-01-01T00:00:00.000',
				'',
				'',
				'1753-01-01T00:00:00.000',
				'".$orderDate->format("Y-m-d").'T00:00:00.000'."',
				1,
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				'',
				0,
				'',
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				'',
				0,
				'1753-01-01T00:00:00.000',
				'',
				'',
				0,
				".$line["Importe pendiente base"].",
				".$line["Importe pendiente base (DL)"].",
				1,
				'$codunidadmedidoprecio',
				".$line["Coste unit. directo UM precio"].",
				".$line["Unit Cost UM Precio"].",
				0,
				'$descripcionumprecio',
				0,
				'',
				'1753-01-01T00:00:00.000',
				0,
				0,
				0,
				'',
				0,
				0,
				'',
				0,
				0,
				'',
				'',
				'',
				0,
				0,
				0,
				0,
				0,
				'',
				0,
				0,
				0,
				'',
				0,
				0,
				0
				);";		
			
			//echo $sql;
			$stmt = sqlsrv_query( $connNavision, $sql );
			if(sqlsrv_errors(SQLSRV_ERR_ERRORS)===NULL) echo json_encode(["result"=>1]); else echo json_encode(["result"=>-13, "text"=>"1 Ocurrio un error en la consulta SQL de Navision"]);
		/*
				if( ($errors = sqlsrv_errors() ) != null) {
					foreach( $errors as $error ) {
						echo "SQLSTATE: ".$error[ 'SQLSTATE']."<br />";
						echo "code: ".$error[ 'code']."<br />";
						echo "message: ".$error[ 'message']."<br />";
					}
				}
			*/
				
		}
	}
	
?>
