<?php
//set_time_limit(300);
$json = file_get_contents('php://input');
$dataJSON=json_decode($json, true); 
if(is_array($dataJSON)){
	$XvResultado = '{"inv": [{';
	include_once('conex.php');

	$SqlTmp = "SELECT num_item FROM d_tablas WHERE cdg_tab='COR' AND num_item='INV' ";
	$XqDTabla = pg_query($SqlTmp) or die('La consulta fallo: ' . pg_last_error());
	$CFila = pg_num_rows($XqDTabla);
	if ($CFila==0){
		$SqlTmp = "INSERT INTO d_tablas (cdg_tab, num_item, des_item, swt_item, abr_item, val_item, mod_item) 
			VALUES ('COR', 'INV', 'TOMA DE INVENTARIO', 1, 'T/INV', 10, 'F')";
		$XqIDT = pg_query($SqlTmp)  or die(pg_last_error());
	}
	
	$SqlTmp = "SELECT por_tigv, mon_ped FROM t_params WHERE cdg_par = '000'";
	$XqParEmp = pg_query($SqlTmp)  or die(pg_last_error());
	$data = pg_fetch_array($XqParEmp);
	$cdg_mon = $data['mon_ped'];
	$por_tigv = $data['por_tigv'];
	$cdg_resp = "000";
	$cdg_tmov = "006";

	for($i=0; $i<count($dataJSON);$i++){ 
		$cdg_loc 		= $dataJSON[$i]["cdg_loc"];
		$cdg_area 		= $dataJSON[$i]["cdg_area"];
		$fec_inv 		= $dataJSON[$i]["fec_inv"];
		$num_inv 		= $dataJSON[$i]["num_inv"];
		$id_inv 		= $dataJSON[$i]["id_inv"];
		$renglonJSON	= $dataJSON[$i]["renglon"];
			
		if (empty($num_inv)){
			//************************************************************************
			//************************ Correlativo Inventario ************************
			//************************************************************************
			$llave = 0;
			while ($llave < 1){
				$SqlTmp = "SELECT cor_loc FROM t_correl WHERE (cdg_cor ='INV') AND (cdg_loc='$cdg_loc') ";
				$XqTCorre = pg_query($SqlTmp) or die('La consulta fallo (t_correl): ' . pg_last_error());
				$CCorreF = pg_num_rows($XqTCorre);
				if ($CCorreF>0){
					$data2 = pg_fetch_array($XqTCorre);
					$num_inv = $data2['cor_loc'];
				}else{
					$SqlTmp = "INSERT INTO t_correl (CDG_COR, CDG_LOC, COR_LOC) VALUES ('INV','$cdg_loc','0000000001')";
					$XqInsertTC = pg_query($SqlTmp) or die(pg_last_error());
					$num_inv = "0000000001";
				}
					
				$SqlTmp = "SELECT * FROM m_invent WHERE cdg_loc='$cdg_loc' AND num_inv='$num_inv' ";
				$XqPedidos = pg_query($SqlTmp)  or die('La consulta fallo: ' . pg_last_error());
				$CantReg = pg_num_rows($XqPedidos);
				if ($CantReg > 0){
					$CorrInt = (int)$num_inv;
					$CorrInt++;
					$num_inv = substr("0000000000".ltrim(rtrim((String)$CorrInt)), -10);
					$SqlTmp = "UPDATE t_correl SET cor_loc = '$num_inv' WHERE (cdg_cor ='INV') AND (cdg_loc='$cdg_loc') ";
					$XqUpdatePed = pg_query($con, $SqlTmp)  or die(pg_last_error());
				}else{
					$llave = 1;
				}
			}
			//************************************************************************
			$SqlTmp = "INSERT INTO m_invent 
				( CDG_LOC, NUM_INV, FEC_INV, CDG_AREA,
				CDG_RESP, CDG_TMOV, CDG_MON, REF_DOCU,
				SWT_CIE, FEC_CIE, SWT_INV, CDG_USR,
				INI_LINP, FIN_LINP, INI_TPRD, FIN_TPRD,
				TPG_ING, GUI_ING, TPG_SAL, GUI_SAL) VALUES 
				('$cdg_loc', '$num_inv', '$fec_inv','$cdg_area',
				'$cdg_resp', '$cdg_tmov', '$cdg_mon','',
				0, NULL, '','SUPERVISOR',
				'000','999','000','999','','','','') ";
			$XqInsertPed = pg_query($SqlTmp)  or die("Error - INSERT/UPDATE (m_invent). " . pg_last_error());
		}
		//************************************************************************
		//********************* INSERT Renglones *********************************
		//************************************************************************
		for($iD=0; $iD<count($renglonJSON);$iD++){ 
			$cdg_prod	= $renglonJSON[$iD]["cdg_prod"];
			$inv_act	= $renglonJSON[$iD]["inv_act"];

			$SqlTmp = "INSERT INTO d_invent 
				(CDG_LOC, NUM_INV, CDG_PROD, STK_ACT, INV_ACT) VALUES 
				('$cdg_loc', '$num_inv', '$cdg_prod', 0, $inv_act)";
			$XqInsertRengPed = pg_query($SqlTmp)  or die('Error INSERT d_invent. '.pg_last_error());
		}
		$XvResultado = $XvResultado . '"docum":'.$id_inv.',"sico":"'.$num_inv.'"},{';
	}
	$XvResultado = substr($XvResultado, 0, strlen($XvResultado)-2) . "]}";
	echo $XvResultado;
}else{
	echo "xxxxxxx";
}
?>