<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('memory_limit', '512M');
date_default_timezone_set('Europe/Madrid');
//include("conexion.php");
if(isset($_REQUEST["code"])) 
	$code=$_REQUEST["code"]; 
else $code=0;

if(isset($_REQUEST["store"])) 
	$store=$_REQUEST["store"]; 
else $store=0;

if(isset($_REQUEST["qty"])) 
	$qty=$_REQUEST["qty"]; 
else $qty=0;

if(isset($_REQUEST["type"])) 
	$type=$_REQUEST["type"]; 
else $type=-1;


$quantitys=[];

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 "Conexion establecida con Navision.\r\n";
	}else{
		 //echo "Conexion no se pudo establecer con Navision.\r\n";
		 die( print_r( sqlsrv_errors(), true));
	}


if($code){
	
	$sql="INSERT INTO dbo.[Item Ledger Entry]
	('Item No.','Entry Type','Document No.','Location Code'.Quantity,Positive,'Document Type','Document Line No.',
	 
	ISNULL(SUM(itl.Quantity),0) stock
	FROM dbo.[Item Ledger Entry] itl
	WHERE itl.[Item No.]='".$code."' AND itl.[Location Code]='".$store."'";

	$objects=[];
	$stmt = sqlsrv_query( $connNavision, $sql );
		if( $stmt !== false) {
			
			$row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC);
		}
		
	
	$result[]=$row;
	header('Content-Type: application/json');
	echo json_encode($result, JSON_PRETTY_PRINT);
}

else echo json_encode("-1", JSON_PRETTY_PRINT);
?>

