<?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;
	}
	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;
		
	}
?>