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

	for($i=0; $i<count($dataJSON);$i++){ 
		$ruc_cli = $dataJSON[$i]["ruc_cli"];
		$cdg_alt = $dataJSON[$i]["cdg_alt"];
		$des_cli = $dataJSON[$i]["des_cli"];
		$dir_cli = $dataJSON[$i]["dir_cli"];
		$tel_cli = $dataJSON[$i]["tel_cli"];
		$rep_cli = $dataJSON[$i]["rep_cli"];
		$ema_cli = $dataJSON[$i]["ema_cli"];
		$gps_cli = $dataJSON[$i]["gps_cli"];
		$cdg_vend = $dataJSON[$i]["cdg_vend"];
		$swt_cli = $dataJSON[$i]["swt_cli"];
		$swt_alt = 0;
		$CdgLprc = "";
		if (!empty(ltrim(rtrim($cdg_alt))))
			$swt_alt = 1;	

		$ing_cli = date("Ymd");
		$opc = "";
		if (empty($ruc_cli)){
			$opc = "I";
			$SqlTmp = "SELECT MAX(ruc_cli) AS ruc_cli FROM m_client";
			$XqParEmp2 = pg_query($SqlTmp) or die('La consulta fallo: ' . pg_last_error());
			$data2 = pg_fetch_array($XqParEmp2);
			$RucCliId = $data2['ruc_cli'];
			//************************************************************************
			//************************ Correlativo Clientes **************************
			//************************************************************************
			$llave = 0;
			while ($llave < 1){
				$SqlTmp = "SELECT ruc_cli FROM m_client WHERE ruc_cli = '$RucCliId' ";
				$XqPedidos = pg_query($SqlTmp)  or die('La consulta fallo: ' . pg_last_error());
				$CantReg = pg_num_rows($XqPedidos);
				if ($CantReg > 0){
					$CorrInt = (int)$RucCliId;
					$CorrInt++;
					$RucCliId = substr("00000000".ltrim(rtrim((String)$CorrInt)), -8);
				}else{
					$llave = 1;
				}
			}
			$ruc_cli = $RucCliId;
			if (!empty($ruc_cli)){
				$XvSQL = "SELECT D_TABLAS.NUM_ITEM 
					FROM D_TABLAS 
					WHERE (D_TABLAS.CDG_TAB = 'PRC') 
					AND D_TABLAS.SWT_ITEM = 1 
					GROUP BY D_TABLAS.NUM_ITEM, D_TABLAS.DES_ITEM, D_TABLAS.abr_item, D_TABLAS.SWT_ITEM 
					ORDER BY D_TABLAS.NUM_ITEM 
					LIMIT 1";
				$XqListPrec = pg_query($XvSQL) or die('La consulta fallo: ' . pg_last_error());
				$data3 = pg_fetch_array($XqListPrec);
				$CdgLprc = $data3['num_item'];
				
				$XvSQL = "INSERT INTO m_client (ruc_cli, cdg_alt, des_cli, dir_cli, tel_cli, rep_cli, ema_cli,
				cdg_vend, swt_cli, ing_cli, cdg_cpag, cdg_lprc, cdg_dst, cdg_udis, swt_alt, cdg_tcli, cdg_pais, 
				crd_sol, fax_cli, web_cli, crd_dol, swt_ext, com_cli, swt_ret, cdg_dpt, cdg_prv) VALUES(
				'$RucCliId', '$cdg_alt', '$des_cli', '$dir_cli', '$tel_cli', '$rep_cli', '$ema_cli', '$cdg_vend',
				1, '$ing_cli', '000', '$CdgLprc', '000', '000', $swt_alt, '000', '000', 0, '', '', 0, 0, 
				'',0,'000', '000')";
			}
		}else{
			$opc = "U";
			if (!empty($ruc_cli)){
				$XvSQL = "UPDATE m_client SET 
				cdg_alt = '$cdg_alt', 
				des_cli = '$des_cli', 
				dir_cli = '$dir_cli', 
				tel_cli = '$tel_cli', 
				rep_cli = '$rep_cli', 
				ema_cli = '$ema_cli',
				swt_alt = $swt_alt,
				cdg_vend = '$cdg_vend'
				WHERE ruc_cli = '$ruc_cli'";
			}
		}
		$XqResult = pg_query($XvSQL)  or die('SQL fallo: ' . pg_last_error());
	}
	$XvResultado = $XvResultado . '"opc":"'.$opc.'","ruc_cli":"'.$ruc_cli.'","cdg_lprc":"'.$CdgLprc.'"}]}';
	echo $XvResultado;
}else{
	echo "xxxxxxx";
}
?>