<?php
set_time_limit(300);
include_once('conex.php');

$tabla = "";
if (isset($_REQUEST['t']))
	$tabla = $_REQUEST['t'];

$linea = '';
if (isset($_REQUEST['l']))
	$linea = $_REQUEST['l'];

$codart = '';
if (isset($_REQUEST['c']))
	$codart = $_REQUEST['c'];

$codven = '';
if (isset($_REQUEST['v']))
	$codven = $_REQUEST['v'];

$ip_server = "//192.168.3.2";

if (ini_get('default_charset')=='UTF-8')
	ini_set('default_charset', 'Windows-1251');

/*-------------------------------------------------------*/
/*-------------------------------------------------------*/
/*-------------------------------------------------------*/
if ($tabla=='valconex'){
	echo "GCTS";
}
//***********************************************************************************
function limpiarcadena($pCadena){
	$vCadena = rtrim($pCadena);
	$vCadena = str_replace(';','',str_replace('"','',str_replace("'","",$vCadena)));
	$vCadena = str_replace("<", "", $vCadena);
	$vCadena = str_replace(">", "", $vCadena);
	$vCadena = str_replace('\R', ' ', $vCadena);
	$vCadena = str_replace('\R\N', ' ', $vCadena);
	$vCadena = str_replace('\N', ' ', $vCadena);
	$vCadena = str_replace('\r', ' ', $vCadena);
	$vCadena = str_replace('\r\n', ' ', $vCadena);
	$vCadena = str_replace('\n', ' ', $vCadena);
	$vCadena = str_replace(chr(13), ' ', $vCadena);
	$vCadena = str_replace(chr(10), ' ', $vCadena);

	return $vCadena;
}
function lista_cxc(){
	global $codven;
	//*********************************** Cuentas por Cobrar ***************************************************
	$SqlTmp = "SELECT m_ctecli.CDG_TDOC, m_ctecli.NUM_DOCU, m_ctecli.RUC_CLI, tipoDocum.tip_item,
		m_ctecli.CDG_MON, m_ctecli.FEC_DOCU, m_ctecli.VCT_DOCU, m_ctecli.IMP_DOCU, m_ctecli.PAG_DOCU, 
		m_ctecli.CDG_LOC, m_ctecli.TOT_RET, d_tablas.abr_item AS moneda, tipoDocum.abr_item AS tipodocu,
		T_DOCCLI.NUM_SER, (m_ctecli.IMP_DOCU - m_ctecli.PAG_DOCU) AS Saldo, m_client.cdg_vend, m_client.tel_cli, 
		m_client.fax_cli, m_client.ema_cli, m_client.des_cli, m_client.cdg_alt, extract(DAYS FROM now()- m_ctecli.VCT_DOCU) AS dias 
		FROM m_ctecli 
		INNER JOIN m_client ON m_client.ruc_cli = m_ctecli.ruc_cli 
		INNER JOIN T_DOCCLI ON m_ctecli.CDG_TDOC = T_DOCCLI.CDG_TDOC 
		INNER JOIN d_tablas ON m_ctecli.cdg_mon = d_tablas.num_item AND d_tablas.cdg_tab = 'MON'
		INNER JOIN d_tablas AS tipoDocum ON m_ctecli.cdg_tdoc = tipoDocum.num_item AND tipoDocum.cdg_tab = 'TDC'
		WHERE (m_ctecli.IMP_DOCU - m_ctecli.PAG_DOCU > 0) 
		AND (m_ctecli.SWT_DOCU NOT IN ('T', 'A')) 
		AND tipoDocum.tip_item = '+' 
		AND m_client.tel_cli <> '' 
		AND extract(DAYS FROM now()- m_ctecli.VCT_DOCU) > -5 
		ORDER BY m_ctecli.VCT_DOCU ";
		
		
	$XvConteo = 1;
	$XvGrupoF = 0;		
	$XqTabla = pg_query( $SqlTmp) or die('La consulta fallo: ' . pg_last_error());
	$XvCantFila = pg_num_rows($XqTabla);
		
	if ($XvCantFila > 0){
		echo "DELETE FROM docum_cc" . "\n";
		$XvCabecera = "INSERT INTO docum_cc (tipo_doc,nro_doc,co_cli,cdg_mon,tipodocu,tip_item,fec_emis,".
			"fec_venc,co_ven,neto,saldo,tel_cli,fax_cli,ema_cli,des_cli,cdg_alt,dias) VALUES";
		echo $XvCabecera;
		while ($data = pg_fetch_array($XqTabla)){
			$XvCadena = "('" . rtrim($data['num_ser']) . "','" .
				rtrim($data['num_docu']) . "','" .
				$data['ruc_cli']  . "','" .
				ltrim(rtrim($data['moneda']))  . "','" .
				ltrim(rtrim($data['tipodocu']))  . "','" .
				ltrim(rtrim($data['tip_item']))  . "','" .
				$data['fec_docu'] . "'," .
				"'" .$data['vct_docu'] . "'," .
				"'" . $data['cdg_vend'] . "'," .
				$data['imp_docu'] . "," .
				$data['saldo'] . ",".
				"'". ltrim(rtrim($data['tel_cli']))."',".
				"'".ltrim(rtrim($data['fax_cli']))."',".
				"'".ltrim(rtrim($data['ema_cli']))."',".
				"'".ltrim(rtrim($data['des_cli']))."',".
				"'".ltrim(rtrim($data['cdg_alt']))."',".
				"" . ltrim(rtrim($data['dias'])) . ")";

			if ($XvGrupoF >= 499 || $XvConteo>=$XvCantFila){
				$XvCadena = $XvCadena . "\n";
				$XvGrupoF = -1;
				echo $XvCadena;
				if (($XvConteo+2) < $XvCantFila){
					echo $XvCabecera;
				}
			}else{
				$XvCadena = $XvCadena . ",";
				echo $XvCadena;
			}
			$XvConteo++;
			$XvGrupoF++;
		}
	}
}	
/*-------------------------------------------------------*/
/*-------------------------------------------------------*/
/*-------------------------------------------------------*/
if ($tabla=='cxc'){
	lista_cxc();
}
pg_close($con);

?>