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

	$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'];

	for($i=0; $i<count($dataJSON);$i++){ 
		$fec_tar 		= $dataJSON[$i]["fec_tar"];
		$num_tar 		= $dataJSON[$i]["num_tar"];
		$tot_hora		= $dataJSON[$i]["tot_hora"];
		$obs_tar		= $dataJSON[$i]["obs_tar"];
		$renglonJSON	= $dataJSON[$i]["renglon"];

		if (empty($num_tar)){
			//************************************************************************
			//************************* Correlativo Tareos ***************************
			//************************************************************************
			$llave = 0;
			while ($llave < 1){		
				$SqlTmp = "SELECT MAX(num_tar) AS num_tar FROM m_tarpry ";
				$XqCorrela = pg_query($SqlTmp) or die('La consulta fallo: ' . pg_last_error());
				$dataCorrela = pg_fetch_array($XqCorrela);
				$num_tar = $dataCorrela['num_tar'];

				$CorrInt = (int)$num_tar;
				$CorrInt++;
				$num_tar = substr("0000000000".ltrim(rtrim((String)$CorrInt)), -10);
					
				$SqlTmp = "SELECT num_tar FROM m_tarpry WHERE num_tar = '$num_tar' ";
				$XqPedidos = pg_query($SqlTmp)  or die('La consulta fallo: ' . pg_last_error());
				$CantReg = pg_num_rows($XqPedidos);
				if ($CantReg = 0){
					$llave = 1;
				}
			}

			$SqlTmp = "INSERT INTO m_tarpry 
				( NUM_TAR, FEC_TAR, OBS_TAR, SWT_TAR, SWT_TERM, TOT_HORA, SWT_FER ) VALUES 
				('$num_tar', '$fec_tar', '$obs_tar', '', 0, $tot_hora, 0) ";
			$XqInsertPed = pg_query($SqlTmp)  or die("Error - INSERT (m_tarpry). " . pg_last_error());
		}else{
			$SqlTmp = "UPDATE m_tarpry SET TOT_HORA = $tot_hora WHERE NUM_TAR = '$num_tar'";
			$XqInsertPed = pg_query($SqlTmp)  or die("Error - UPDATE (m_tarpry). " . pg_last_error());
		}
			
		//************************************************************************
		//********************* INSERT Renglones *********************************
		//************************************************************************
		$RengNum = 0;
		$SqlTmp = "SELECT MAX(COR_TAR) AS COR_TAR FROM d_tarpry WHERE num_tar = '$num_tar' ";
		$XqArtTmp = pg_query($SqlTmp) or die("Error SELECT d_tarpry. " . pg_last_error());
		$DataArt = pg_fetch_array($XqArtTmp);
		$cor_tar = $DataArt['cor_tar'];
		$RengNum = (int)$cor_tar;
		$RengNum++;
		for($iD=0; $iD<count($renglonJSON);$iD++){ 
			$cdg_emp	= $renglonJSON[$iD]["cdg_emp"];
			$cdg_cct 	= $renglonJSON[$iD]["cdg_cct"];
			$cdg_part 	= $renglonJSON[$iD]["cdg_part"];
			$hra_tar 	= $renglonJSON[$iD]["hra_tar"];
			$cdg_act 	= $renglonJSON[$iD]["cdg_act"];

			$SqlTmp = "SELECT cth_emp FROM m_emple WHERE cdg_emp = '$cdg_emp' ";
			$XqArtTmp = pg_query($SqlTmp) or die("Error SELECT m_emple. " . pg_last_error());
			$DataArt = pg_fetch_array($XqArtTmp);
			$cth_emp = $DataArt['cth_emp'];

			$num_item = substr("00000" . rtrim(ltrim((String)$RengNum)), -5);
			if (empty($PedNumOrige)){
				$SqlTmp = "INSERT INTO d_tarpry (NUM_TAR, COR_TAR, CDG_EMP, CDG_CCT, CDG_PART, HRA_TAR, CTO_TAR, CDG_ACT) 
					VALUES ('$num_tar','$num_item','$cdg_emp','$cdg_cct','$cdg_part', $hra_tar, $cth_emp, '$cdg_act') ";
				$XqInsertRengPed = pg_query($SqlTmp)  or die('Error INSERT d_tarpry. '.pg_last_error());
				$RengNum ++;
			}
		}
		$XvResultado = $XvResultado . '"docum":'.$id_ped.',"num_tar":"'.$num_tar.'"}]}';
	}
	echo $XvResultado;
}else{
	echo "xxxxxxx";
}
?>