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

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

/*-------------------------------------------------------*/
/*-------------------------------------------------------*/
/*-------------------------------------------------------*/
if ($tabla=='valconex'){
	echo "GCTS";
}
/*-------------------------------------------------------*/
/*-------------------------------------------------------*/
function lista_alm(){
	$SqlTmp = "SELECT NUM_ITEM, DES_ITEM, SWT_ITEM
		FROM d_tablas
		WHERE CDG_TAB = 'ARE' 
		ORDER BY NUM_ITEM ";
	$XqTabla = pg_query($SqlTmp) or die('La consulta fallo (Almacen): ' . pg_last_error());
	$XvCantFila = pg_num_rows($XqTabla);
	if ($XvCantFila > 0){
		$XvConteo = 1;
		$XvGrupoF = 0;		
		echo "DELETE FROM almacen" . "\n";
		$XvCabecera = "INSERT INTO almacen (cod_alm, des_alm, activo, cod_loc) VALUES";
		echo $XvCabecera;
		while ($data = pg_fetch_array($XqTabla)){
			$XvCadena = "('".rtrim($data['num_item']) . "','" .
				rtrim($data['des_item']) . "'," .
				$data['swt_item'] .", '')";
				
			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++;
		}
	}	
}
/*-------------------------------------------------------*/
function lista_resp(){
	$SqlTmp = "SELECT cdg_resp, des_resp, swt_resp
		FROM m_respon
		ORDER BY des_resp ";
	$XqTabla = pg_query($SqlTmp) or die('La consulta fallo (m_respon): ' . pg_last_error());
	$XvCantFila = pg_num_rows($XqTabla);
	if ($XvCantFila > 0){
		$XvConteo = 1;
		$XvGrupoF = 0;		
		echo "DELETE FROM m_respon" . "\n";
		$XvCabecera = "INSERT INTO m_respon (cod_res, des_res, activo) VALUES";
		echo $XvCabecera;
		while ($data = pg_fetch_array($XqTabla)){
			$XvCadena = "('".rtrim($data['cdg_resp']) . "','" .
				rtrim($data['des_resp']) . "'," .
				$data['swt_resp'] .")";
				
			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=='resp'){
	lista_resp();
}
/*-------------------------------------------------------*/
if ($tabla=='alm'){
	lista_alm();
}
/*-------------------------------------------------------*/
if ($tabla=='stockart'){
	lista_lin();
	lista_art();
	lista_pre();
	
	$SqlTmp = "SELECT art.CDG_PROD, ingeso.ALMA, ingeso.DES_ITEM, SUM(ingeso.Cantidad) AS Cantidad
		FROM M_PRODUC AS art LEFT OUTER JOIN
                          (SELECT     A.CDG_AREA AS ALMA, ALMA.DES_ITEM, B.CDG_PROD, SUM(B.CAN_DGUI) AS Cantidad
                            FROM          M_GUIA AS A INNER JOIN
                                                   D_GUIA AS B ON A.CDG_TPG = B.CDG_TPG AND A.NUM_GUIA = B.NUM_GUIA INNER JOIN
                                                   D_TABLAS AS ALMA ON A.CDG_AREA = ALMA.NUM_ITEM AND ALMA.CDG_TAB = 'ARE'
                            WHERE      (A.SWT_EST = '') AND (A.CDG_TPG = '001')
                            GROUP BY A.CDG_AREA, B.CDG_PROD, ALMA.DES_ITEM
                            UNION ALL
                            SELECT     A.ORI_AREA AS ALMA, ALMA.DES_ITEM, B.CDG_PROD, SUM(B.CAN_DGUI * - 1) AS Cantidad
                            FROM         M_GUIA AS A INNER JOIN
                                                  D_GUIA AS B ON A.CDG_TPG = B.CDG_TPG AND A.NUM_GUIA = B.NUM_GUIA INNER JOIN
                                                  D_TABLAS AS ALMA ON A.ORI_AREA = ALMA.NUM_ITEM AND ALMA.CDG_TAB = 'ARE'
                            WHERE     (A.SWT_EST = '') AND (A.CDG_TPG <> '001')
                            GROUP BY A.ORI_AREA, B.CDG_PROD, ALMA.DES_ITEM) AS ingeso ON art.CDG_PROD = ingeso.CDG_PROD
		WHERE     (art.SWT_PROD = 1) AND (art.SWT_VTA = 1) AND (NOT (ingeso.ALMA IS NULL))
		GROUP BY art.CDG_PROD, ingeso.ALMA, ingeso.DES_ITEM
		HAVING      (SUM(ingeso.Cantidad) <> 0)
		ORDER BY art.CDG_PROD";

	$XqTabla = pg_query( $SqlTmp) or die('La consulta fallo: ' . pg_last_error());
	$XvCantFila = pg_num_rows($XqTabla);
		
	if ($XvCantFila > 0){
		$XvConteo = 1;
		$XvGrupoF = 0;		
		echo "DELETE FROM stock_art" . "\n";
		$XvCabecera = "INSERT INTO stock_art (co_art, co_alma, des_alma, stock_act) VALUES";
		echo $XvCabecera;
		while ($data = pg_fetch_array($XqTabla)){
			$XvCadena = "('". rtrim(str_replace("'","",$data['cdg_prod'])) . "','" .
				rtrim(str_replace("'","",$data['alma'])) ."','".
				rtrim(str_replace("'","",$data['des_item'])) ."',".
				$data['cantidad'] . ")";		

			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++;
		}
	}
}
//***********************************************************************************
function lista_ven(){
	$SqlTmp = "SELECT cdg_vend, des_vend, swt_vend, swt_usr, cdg_usr 
		FROM m_vended
		ORDER BY cdg_vend ";
	$XqTabla = pg_query($SqlTmp) or die('La consulta fallo: ' . pg_last_error());
	$XvCantFila = pg_num_rows($XqTabla);
	if ($XvCantFila > 0){
		$XvConteo = 1;
		$XvGrupoF = 0;		
		echo "DELETE FROM vendedor" . "\n";
		$XvCabecera = "INSERT INTO vendedor (cod_ven, des_ven, activo, swt_usr, cod_usr) VALUES";
		echo $XvCabecera;
		while ($data = pg_fetch_array($XqTabla)){
			$XvCadena = "('".rtrim($data['cdg_vend']) . "','" .
				rtrim($data['des_vend']) . "'," .
				$data['swt_vend'] ."," .
				$data['swt_usr'] .",'" .
				ltrim($data['cdg_usr']) ."')";
			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++;
		}
	}
}
//***********************************************************************************
function lista_emp(){
	$SqlTmp = "SELECT CDG_EMP, DES_EMP, 
		CASE WHEN ele_emp <> '' THEN ele_emp ELSE PAS_EMP END AS pas_emp ,swt_emp
		FROM m_emple
		ORDER BY CDG_EMP ";
	$XqTabla = pg_query( $SqlTmp) or die('La consulta fallo: ' . pg_last_error());
	$XvCantFila = pg_num_rows($XqTabla);
	if ($XvCantFila > 0){
		$XvConteo = 1;
		$XvGrupoF = 0;		
		echo "DELETE FROM m_emple" . "\n";
		$XvCabecera = "INSERT INTO m_emple (cdg_emp, des_emp, pas_emp, swt_emp) ";
		echo $XvCabecera;
		while ($data = pg_fetch_array($XqTabla)){
			$XvCadena = "SELECT '".rtrim($data['cdg_emp']) . "','" .
				rtrim($data['des_emp']) . "','" .
				rtrim($data['pas_emp']) ."'," .
				$data['swt_emp'] ." ";
				
			if ($XvGrupoF >= 499 || $XvConteo>=$XvCantFila){
				$XvCadena = $XvCadena . "\n";
				$XvGrupoF = -1;
				echo $XvCadena;
				if (($XvConteo+2) < $XvCantFila)
					echo $XvCabecera;
			}else{
				$XvCadena = $XvCadena . " UNION ALL ";
				echo $XvCadena;
			}
			$XvConteo++;
			$XvGrupoF++;
		}
	}
}
//***********************************************************************************
function lista_act(){
	$SqlTmp = "SELECT NUM_ITEM, DES_ITEM, SWT_ITEM
		FROM d_tablas
		WHERE CDG_TAB = 'APR' 
		AND SWT_ITEM = 1 
		ORDER BY NUM_ITEM ";
	$XqTabla = pg_query( $SqlTmp) or die('La consulta fallo: ' . pg_last_error());
	$XvCantFila = pg_num_rows($XqTabla);
	if ($XvCantFila > 0){
		$XvConteo = 1;
		$XvGrupoF = 0;		
		echo "DELETE FROM m_actividad" . "\n";
		$XvCabecera = "INSERT INTO m_actividad (cdg_act, des_act, swt_act) VALUES";
		echo $XvCabecera;
		while ($data = pg_fetch_array($XqTabla)){
			$XvCadena = "('".rtrim($data['num_item']) . "','" .
				rtrim($data['des_item']) . "'," .
				$data['swt_item'] .")";
				
			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++;
		}
	}
}
//***********************************************************************************
function lista_partidas(){
	$SqlTmp = "SELECT p.cdg_part, l.des_lin, p.cdg_cct
		FROM t_lincgt l 
		INNER JOIN d_presup p ON p.cdg_part = l.cdg_lin
		WHERE l.swt_lin=1 
		AND l.tar_lin=1 
		ORDER BY l.des_lin";
	$XqTabla = pg_query( $SqlTmp) or die('La consulta fallo: ' . pg_last_error());
	$XvCantFila = pg_num_rows($XqTabla);
	if ($XvCantFila > 0){
		$XvConteo = 1;
		$XvGrupoF = 0;		
		echo "DELETE FROM m_partida" . "\n";
		$XvCabecera = "INSERT INTO m_partida (cdg_part, des_part, cdg_pres) VALUES";
		echo $XvCabecera;
		while ($data = pg_fetch_array($XqTabla)){
			$XvCadena = "('".rtrim($data['cdg_part']) . "','" .
				rtrim($data['des_lin']) . "','" .
				$data['cdg_cct'] ."')";
				
			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++;
		}
	}
}	
//***********************************************************************************
function lista_ccosto(){
	$SqlTmp = "SELECT CDG_CCT, DES_CCT, CDG_TCCT, SWT_CCT
		FROM m_ccosto
		ORDER BY CDG_CCT ";
	$XqTabla = pg_query( $SqlTmp) or die('La consulta fallo: ' . pg_last_error());
	$XvCantFila = pg_num_rows($XqTabla);
	if ($XvCantFila > 0){
		$XvConteo = 1;
		$XvGrupoF = 0;		
		echo "DELETE FROM m_ccosto" . "\n";
		$XvCabecera = "INSERT INTO m_ccosto (CDG_CCT, DES_CCT, CDG_TCCT, SWT_CCT) VALUES";
		echo $XvCabecera;
		while ($data = pg_fetch_array($XqTabla)){
			$XvCadena = "('".rtrim($data['cdg_cct']) . "','" .
				rtrim($data['des_cct']) . "','" .
				rtrim($data['cdg_tcct']) . "'," .
				$data['swt_cct'] .")";
				
			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++;
		}
	}
}
//***********************************************************************************
function lista_user(){
	$XvConteo = 1;
	$XvGrupoF = 0;		
	$SqlTmp = "SELECT cdg_usr, des_usr, suc_usr, ema_usr, psw_web, swt_usr, vnd_usr FROM m_usuari ";
	$XqTabla = pg_query( $SqlTmp)  or die('La consulta fallo: ' . pg_last_error());
	$XvCantFila = pg_num_rows($XqTabla);
	if ($XvCantFila > 0){
		echo "DELETE FROM usuario" . "\n";
		$XvCabecera = "INSERT INTO usuario (usuario, nombre, co_sucu, email, clave, activo, codasociado) ";
		echo $XvCabecera;
		while ($data = pg_fetch_array($XqTabla)){
			$XvCadena = "SELECT '".rtrim($data['cdg_usr']) . "','" .
				rtrim($data['des_usr']) . "','" .
				rtrim($data['suc_usr']) . "','" .
				rtrim($data['ema_usr']) . "','" .
				rtrim($data['psw_web']) . "', " .
				$data['swt_usr'] . ",'" .
				rtrim($data['vnd_usr']) . "'";

			if ($XvGrupoF >= 499 || $XvConteo>=$XvCantFila){
				$XvCadena = $XvCadena . "\n";
				$XvGrupoF = -1;
				echo $XvCadena;
				if (($XvConteo+2) < $XvCantFila)
					echo $XvCabecera;
			}else{
				$XvCadena = $XvCadena . " UNION ALL ";
				echo $XvCadena;
			}
			$XvConteo++;
			$XvGrupoF++;
		}
	}
}
//***********************************************************************************
function lista_mon(){
	$SqlTmp = "SELECT A.NUM_ITEM, A.DES_ITEM, A.abr_item, A.SWT_ITEM 
		FROM D_TABLAS AS A
		WHERE (A.CDG_TAB = 'MON') 
		AND A.SWT_ITEM = 1 
		GROUP BY A.NUM_ITEM, A.DES_ITEM, A.abr_item, A.SWT_ITEM 
		ORDER BY A.DES_ITEM";

	$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 moneda"."\n";
		$XvCabecera = "INSERT INTO moneda (cod_moneda, des_moneda, abr_moneda, activo) VALUES";
		echo $XvCabecera;
		while ($data = pg_fetch_array($XqTabla)){
			$clides = str_replace(';','',str_replace('"','',str_replace("'","",$data['des_item'])));
			$clides = str_replace('\R', ' ', $clides);
			$clides = str_replace('\R\N', ' ', $clides);
			$clides = str_replace('\N', ' ', $clides);
			$clides = str_replace('\r', ' ', $clides);
			$clides = str_replace('\r\n', ' ', $clides);
			$clides = str_replace('\n', ' ', $clides);
			$clides = str_replace(chr(13), ' ', $clides);
			$clides = str_replace(chr(10), ' ', $clides);
				
			$XvCadena = "('".str_replace("'","",rtrim($data['num_item'])) . "','" .
				rtrim($clides) . "','" .
				rtrim($data['abr_item']) . "'," .
				$data['swt_item'] . ")";

			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++;
		}
	}
}
//***********************************************************************************
function lista_art(){
	global $linea, $codart;
	$SqlTmp = "SELECT A.cdg_prod, A.des_prod, A.cdg_linp, A.swt_prod, A.swt_igv, A.cdg_prv, 
			F.fot_prod 
			FROM M_PRODUC AS A 
			LEFT JOIN h_produc AS F ON A.cdg_prod = F.cdg_prod 
			WHERE A.SWT_VTA = 1 ";

	if ($linea != '')
		$SqlTmp = $SqlTmp . " AND A.cdg_linp = '$linea' ";
		
	if ($codart != '')
		$SqlTmp = $SqlTmp . " AND A.cdg_prod = '$codart' ";
		
	$SqlTmp = $SqlTmp . " ORDER BY A.cdg_prod ";

	$XqTabla = pg_query($SqlTmp) or die('La consulta fallo: ' . pg_last_error());
	$XvCantFila = pg_num_rows($XqTabla);
		
	if ($XvCantFila > 0){
		$XvConteo = 1;
		$XvGrupoF = 0;

		echo "DELETE FROM art" . "\n";
		$XvCabecera = "INSERT INTO art (co_art,art_des,co_lin,activo,stock_act,stock_com,swt_igv,ref,foto) VALUES";
		echo $XvCabecera;
		while ($data = pg_fetch_array($XqTabla)){
			$XvDesArt = rtrim(str_replace("'","",$data['des_prod']));
				
			$imdata = "";
			$FotoBase = "";
			if (!is_null($data['fot_prod']))
				$FotoBase = rtrim(ltrim($data['fot_prod']));
			
			if (!empty($FotoBase)){
				$im = file_get_contents($FotoBase);
				$imdata = ltrim(rtrim(base64_encode($im)));
			}
				
			$XvCadena = "('".rtrim($data['cdg_prod']) . "','" .
				$XvDesArt . "','" .
				rtrim($data['cdg_linp']) ."'," .
				$data['swt_prod'] . "," .
				"10000,0," .
				$data['swt_igv'] . ",'" .
				rtrim($data['cdg_prv']) . "','" .
				$imdata."')";

			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++;
		}
	}
}

//***********************************************************************************
//***********************************************************************************
function lista_fpag(){
	$SqlTmp = "SELECT A.NUM_ITEM, A.DES_ITEM, A.cam_item, A.SWT_ITEM 
		FROM D_TABLAS AS A
		WHERE (A.CDG_TAB = 'TDC') 
		AND A.SWT_ITEM = 1 
		GROUP BY A.NUM_ITEM, A.DES_ITEM, A.cam_item, A.SWT_ITEM 
		ORDER BY A.DES_ITEM";
	$XqTabla = pg_query( $SqlTmp) or die('La consulta fallo: ' . pg_last_error());
	$XvCantFila = pg_num_rows($XqTabla);
		
	if ($XvCantFila > 0){
		$XvConteo = 1;
		$XvGrupoF = 0;		
		echo "DELETE FROM tipo_doc" . "\n";
		$XvCabecera = "INSERT INTO tipo_doc (cod_tipo, des_tipo, abr_tipo, activo) VALUES";
		echo $XvCabecera;
		while ($data = pg_fetch_array($XqTabla)){
			$clides = str_replace(';','',str_replace('"','',str_replace("'","",$data['des_item'])));
			$clides = str_replace('\R', ' ', $clides);
			$clides = str_replace('\R\N', ' ', $clides);
			$clides = str_replace('\N', ' ', $clides);
			$clides = str_replace('\r', ' ', $clides);
			$clides = str_replace('\r\n', ' ', $clides);
			$clides = str_replace('\n', ' ', $clides);
			$clides = str_replace(chr(13), ' ', $clides);
			$clides = str_replace(chr(10), ' ', $clides);
				
			$XvCadena = "('".str_replace("'","",rtrim($data['num_item'])) . "','" .
				rtrim($clides) . "','" .
				rtrim($data['cam_item']) . "'," .
				$data['swt_item'] . ")";

			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++;
		}
	}
	//***********************************************************
	$SqlTmp = "SELECT D_TABLAS.NUM_ITEM, D_TABLAS.DES_ITEM, D_TABLAS.abr_item, D_TABLAS.SWT_ITEM, D_TABLAS.val_ITEM 
		FROM D_TABLAS 
		WHERE (D_TABLAS.CDG_TAB = 'CPG') 
		AND D_TABLAS.SWT_ITEM = 1 
		GROUP BY D_TABLAS.NUM_ITEM, D_TABLAS.DES_ITEM, D_TABLAS.abr_item, D_TABLAS.SWT_ITEM, D_TABLAS.val_ITEM 
		ORDER BY D_TABLAS.DES_ITEM";
	$XqTabla = pg_query( $SqlTmp) or die('La consulta fallo: ' . pg_last_error());
	$XvCantFila = pg_num_rows($XqTabla);
	if ($XvCantFila > 0){
		$XvConteo = 1;
		$XvGrupoF = 0;		
		echo "DELETE FROM forma_pago" . "\n";
		$XvCabecera = "INSERT INTO forma_pago (cod_fpa, des_fpa, abr_fpa, activo, dias) VALUES";
		echo $XvCabecera;
		while ($data = pg_fetch_array($XqTabla)){
			$clides = str_replace(';','',str_replace('"','',str_replace("'","",$data['des_item'])));
			$clides = str_replace('\R', ' ', $clides);
			$clides = str_replace('\R\N', ' ', $clides);
			$clides = str_replace('\N', ' ', $clides);
			$clides = str_replace('\r', ' ', $clides);
			$clides = str_replace('\r\n', ' ', $clides);
			$clides = str_replace('\n', ' ', $clides);
			$clides = str_replace(chr(13), ' ', $clides);
			$clides = str_replace(chr(10), ' ', $clides);
				
			$XvCadena = "('".str_replace("'","",rtrim($data['num_item'])) . "','" .
				rtrim($clides) . "','" .
				rtrim($data['abr_item']) . "'," .
				$data['swt_item'] . "," .
				$data['val_item'] .")";
			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++;
		}
	}
}
//***********************************************************************************
function lista_lin(){
	$SqlTmp = "SELECT D_TABLAS.NUM_ITEM, D_TABLAS.DES_ITEM, D_TABLAS.SWT_ITEM
		FROM D_TABLAS 
		WHERE (D_TABLAS.CDG_TAB = 'LIN') 
		AND D_TABLAS.SWT_ITEM = 1 
		GROUP BY D_TABLAS.NUM_ITEM, D_TABLAS.DES_ITEM, D_TABLAS.SWT_ITEM 
		ORDER BY D_TABLAS.DES_ITEM";

	$XqTabla = pg_query($SqlTmp) or die('La consulta fallo: ' . pg_last_error());
	$XvCantFila = pg_num_rows($XqTabla);
		
	if ($XvCantFila > 0){
		$XvConteo = 1;
		$XvGrupoF = 0;

		echo "DELETE FROM lin_art" . "\n";
		$XvCabecera = "INSERT INTO lin_art (co_lin, lin_des, activo)  VALUES";
		echo $XvCabecera;
		while ($data = pg_fetch_array($XqTabla)){
			$XvDesArt = rtrim(str_replace("'","",$data['des_item']));
				
			$XvCadena = "('".rtrim($data['num_item']) . "','" .
				$XvDesArt . "'," .
				$data['swt_item'] . ")";

			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=='art'){
	$SqlTmp = "SELECT A.cdg_prod, A.des_prod, A.cdg_linp, A.swt_prod, swt_igv 
			FROM M_PRODUC AS A 
			WHERE A.SWT_VTA = 1 ";

	if ($linea != '')
		$SqlTmp = $SqlTmp . " AND A.cdg_linp = '$linea' ";
		
	if ($codart != '')
		$SqlTmp = $SqlTmp . " AND A.cdg_prod = '$codart' ";
		
	$SqlTmp = $SqlTmp . " ORDER BY A.cdg_prod ";

	$XqTabla = pg_query($SqlTmp) or die('La consulta fallo: ' . pg_last_error());
	$XvCantFila = pg_num_rows($XqTabla);
		
	if ($XvCantFila > 0){
		$XvCantFilaFinal = $XvCantFila/500;
		$decimales = explode(".",$XvCantFilaFinal);
		
		if ($decimales[1]>0)
			$XvCantFilaFinal = $decimales[0] + 1;
		else
			$XvCantFilaFinal = $decimales[0];		

		$XvConteo = 1;
		$XvGrupoF = 0;
		$XvCantFilaFinal ++;
		echo $XvCantFila . "\n";
		while ($data = pg_fetch_array($XqTabla)){
			$XvDesArt = rtrim(str_replace("'","",$data['des_prod']));
				
			$XvCadena = "SELECT '".rtrim($data['cdg_prod']) . "','" .
				$XvDesArt . "','" .
				rtrim($data['cdg_linp']) ."'," .
				$data['swt_prod'] . "," .
				"100,0,".
				$data['swt_igv'];

			if ($XvGrupoF >= 499 || $XvConteo>=$XvCantFila){
				$XvCadena = $XvCadena . "\n";
				$XvGrupoF = -1;
			}else{
				$XvCadena = $XvCadena . " UNION ALL ";
			}
					
			$XvConteo++;
			$XvGrupoF++;
			echo $XvCadena;
		}
	}
}
/*-------------------------------------------------------*/
/*-------------------------------------------------------*/
/*-------------------------------------------------------*/
if ($tabla=='ccosto'){
	lista_ccosto();
}
/*-------------------------------------------------------*/
/*-------------------------------------------------------*/
/*-------------------------------------------------------*/
if ($tabla=='activi'){
	lista_act();
}
/*-------------------------------------------------------*/
/*-------------------------------------------------------*/
/*-------------------------------------------------------*/
if ($tabla=='partidas'){
	lista_partidas();
}
/*-------------------------------------------------------*/
/*-------------------------------------------------------*/
/*-------------------------------------------------------*/
if ($tabla=='empleados'){
	lista_emp();
	lista_ccosto();
	lista_act();
	lista_partidas();
}
/*-------------------------------------------------------*/
/*-------------------------------------------------------*/
/*-------------------------------------------------------*/
if ($tabla=='clientes'){
	$SqlTmp = "SELECT A.ruc_cli, A.des_cli, A.cdg_alt, A.ema_cli, A.crd_sol, A.cdg_tcli, A.swt_cli, A.cdg_vend, A.cdg_lprc 
		FROM m_client AS A 
		WHERE A.swt_cli = 1 ";
			
	if ($codven != ''){
		$SqlTmp = $SqlTmp . " AND A.cdg_vend = '$codven' ";
	}
		
	$XqTabla = pg_query( $SqlTmp) or die('La consulta fallo: ' . pg_last_error());
	$XvCantFila = pg_num_rows($XqTabla);
		
	if ($XvCantFila > 0){
		$XvConteo = 1;
		$XvGrupoF = 0;		
		echo "DELETE FROM clientes" . "\n";
		$XvCabecera = "INSERT INTO clientes (co_cli,cli_des,rif,email,tipo,co_ven,mont_cre,activo,cdg_lprc) VALUES";
		echo $XvCabecera;
		while ($data = pg_fetch_array($XqTabla)){
			$clides = str_replace(';','',str_replace('"','',str_replace("'","",$data['des_cli'])));
			$clides = str_replace('\R', ' ', $clides);
			$clides = str_replace('\R\N', ' ', $clides);
			$clides = str_replace('\N', ' ', $clides);
			$clides = str_replace('\r', ' ', $clides);
			$clides = str_replace('\r\n', ' ', $clides);
			$clides = str_replace('\n', ' ', $clides);
			$clides = str_replace(chr(13), ' ', $clides);
			$clides = str_replace(chr(10), ' ', $clides);
				
			$XvCadena = "('".str_replace("'","",rtrim($data['ruc_cli'])) . "','" .
				rtrim($clides) . "','" .
				rtrim(str_replace("'","",$data['cdg_alt'])) ."','".
				rtrim(str_replace("'","",$data['ema_cli'])) ."','".
				rtrim(str_replace("'","",$data['cdg_tcli'])) ."','".
				rtrim(str_replace("'","",$data['cdg_vend'])) ."',".
				$data['crd_sol'] . "," .
				$data['swt_cli'] . ",'" .
				$data['cdg_lprc'] . "')";		

			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++;
		}
	}
}
	/*-------------------------------------------------------*/
	/*--------------------- DOCUM_CC ------------------------*/
	/*-------------------------------------------------------*/
	if ($tabla=='documcc'){
		$SqlTmp = "SELECT B.tipo_doc, B.nro_doc, B.co_cli, B.co_ven, B.fec_emis, B.anulado, 
			B.monto_bru, B.monto_imp, B.monto_net, B.saldo
			FROM clientes AS A 
			INNER JOIN docum_cc AS B ON A.co_cli = B.co_cli 
			WHERE B.tipo_doc IN('FACT', 'N/CR', 'N/DB') 
			AND B.anulado = 0 
			AND B.saldo > 0 ";
			
		if ($linea != ''){
			$SqlTmp = $SqlTmp . " AND A.co_ven = '$linea' ";
		}
		
		$XvConteo = 1;
		$XvGrupoF = 0;		
		$XqTabla = pg_query( $SqlTmp) or die('La consulta fallo: ' . pg_last_error());
		$XvCantFila = pg_num_rows($XqTabla);
		
		if ($XvCantFila > 0){
			while ($data = pg_fetch_array($XqTabla)){
				
				$XvCadena = "SELECT '" . $data['tipo_doc'] . "'," .
					$data['nro_doc'] .",'".
					rtrim(str_replace("'","",$data['co_cli'])) ."','".
					rtrim(str_replace("'","",$data['co_ven'])) ."','".
					substr($data['fec_emis'],0,10) ."',".
					$data['monto_bru'] . "," .
					$data['monto_imp'] . "," .
					$data['monto_net'] . "," .
					$data['saldo'] . "," .
					$data['anulado'] ."";			

				if ($XvGrupoF >= 499 || $XvConteo>=$XvCantFila){
					$XvCadena = $XvCadena . "\n";
					$XvGrupoF = -1;
				}else{
					$XvCadena = $XvCadena . " UNION ALL ";
				}
				
				$XvConteo++;
				$XvGrupoF++;
				echo $XvCadena;
			}
		}
	}
	/*-------------------------------------------------------*/
	/*-------------------------------------------------------*/
	/*-------------------------------------------------------*/
	if ($tabla=='vendedor'){
		lista_ven();
	}
	/*-------------------------------------------------------*/
	/*-------------------------------------------------------*/
	/*-------------------------------------------------------*/
	if ($tabla=='paremp'){
		$SqlTmp = "SELECT clc_tot, por_tigv, mon_ped FROM t_params ";
		$XqTabla = pg_query( $SqlTmp) or die('La consulta fallo: ' . pg_last_error());
		$XvCantFila = pg_num_rows($XqTabla);
		if ($XvCantFila > 0){
			while ($data = pg_fetch_array($XqTabla)){
				$XvCadena = "UPDATE par_emp SET ".
					"clc_tot = " . $data['clc_tot'] .
					", por_tigv = " . $data['por_tigv'] .
					", mon_ped = '".$data['mon_ped'] ."'" . "\n";

				echo $XvCadena;
			}
		}
		lista_mon();
		lista_user();
		lista_ven();
		lista_fpag();
		lista_alm();
		lista_resp();
	}	
	
	
	//M_DEFRST
	//cdg_tipo, des_tipo, abr_tipo, pos_tipo, swt_tipo   order by pos_tipo
	
	
	/*-------------------------------------------------------*/
	/*-------------------------------------------------------*/
	/*-------------------------------------------------------*/
	if ($tabla=='usuario'){
		lista_user();
		lista_ven();
	}
	/*-------------------------------------------------------*/
	/*-------------------------------------------------------*/
	/*-------------------------------------------------------*/
	if ($tabla=='lineas'){
		$SqlTmp = "SELECT D_TABLAS.NUM_ITEM, D_TABLAS.DES_ITEM, D_TABLAS.SWT_ITEM
			FROM D_TABLAS 
			WHERE (D_TABLAS.CDG_TAB = 'LIN') 
			ORDER BY D_TABLAS.DES_ITEM";

		$XvConteo = 1;
		$XvGrupoF = 0;		
		$XqTabla = pg_query( $SqlTmp) or die('La consulta fallo: ' . pg_last_error());
		$XvCantFila = pg_num_rows($XqTabla);
		
		if ($XvCantFila > 0){
			while ($data = pg_fetch_array($XqTabla)){
				$clides = str_replace(';','',str_replace('"','',str_replace("'","",$data['des_item'])));
				$clides = str_replace('\R', ' ', $clides);
				$clides = str_replace('\R\N', ' ', $clides);
				$clides = str_replace('\N', ' ', $clides);
				$clides = str_replace('\r', ' ', $clides);
				$clides = str_replace('\r\n', ' ', $clides);
				$clides = str_replace('\n', ' ', $clides);
				$clides = str_replace(chr(13), ' ', $clides);
				$clides = str_replace(chr(10), ' ', $clides);
				
				$XvCadena = "SELECT '".str_replace("'","",rtrim($data['num_item'])) . "','" .
					rtrim($clides) . "'," .
					$data['swt_item'] . " ";

				if ($XvGrupoF >= 499 || $XvConteo>=$XvCantFila){
					$XvCadena = $XvCadena . "\n";
					$XvGrupoF = -1;
				}else{
					$XvCadena = $XvCadena . " UNION ALL ";
				}
				
				$XvConteo++;
				$XvGrupoF++;
				echo $XvCadena;
			}
		}
	}
	/*-------------------------------------------------------*/
	/*-------------------------------------------------------*/
	/*-------------------------------------------------------*/
	if ($tabla=='moneda'){
		lista_mon();
	}
	/*-------------------------------------------------------*/
	/*-------------------------------------------------------*/
	/*-------------------------------------------------------*/
	if ($tabla=='tipodoc'){
		lista_fpag();
	}	
	/*-------------------------------------------------------*/
	/*-------------------------------------------------------*/
	/*-------------------------------------------------------*/
	if ($tabla=='fpago'){
		$SqlTmp = "SELECT D_TABLAS.NUM_ITEM, D_TABLAS.DES_ITEM, D_TABLAS.abr_item, D_TABLAS.SWT_ITEM, D_TABLAS.val_ITEM 
			FROM D_TABLAS 
			WHERE (D_TABLAS.CDG_TAB = 'CPG') 
			ORDER BY D_TABLAS.DES_ITEM";

		$XvConteo = 1;
		$XvGrupoF = 0;		
		$XqTabla = pg_query( $SqlTmp) or die('La consulta fallo: ' . pg_last_error());
		$XvCantFila = pg_num_rows($XqTabla);
		
		if ($XvCantFila > 0){
			while ($data = pg_fetch_array($XqTabla)){
				$clides = str_replace(';','',str_replace('"','',str_replace("'","",$data['des_item'])));
				$clides = str_replace('\R', ' ', $clides);
				$clides = str_replace('\R\N', ' ', $clides);
				$clides = str_replace('\N', ' ', $clides);
				$clides = str_replace('\r', ' ', $clides);
				$clides = str_replace('\r\n', ' ', $clides);
				$clides = str_replace('\n', ' ', $clides);
				$clides = str_replace(chr(13), ' ', $clides);
				$clides = str_replace(chr(10), ' ', $clides);
				
				$XvCadena = "SELECT '".str_replace("'","",rtrim($data['num_item'])) . "','" .
					rtrim($clides) . "','" .
					rtrim($data['abr_item']) . "'," .
					$data['swt_item'] . "," . 
					rtrim($data['val_item']) ;

				if ($XvGrupoF >= 499 || $XvConteo>=$XvCantFila){
					$XvCadena = $XvCadena . "\n";
					$XvGrupoF = -1;
				}else{
					$XvCadena = $XvCadena . " UNION ALL ";
				}
				
				$XvConteo++;
				$XvGrupoF++;
				echo $XvCadena;
			}
		}
	}
	
	/*-------------------------------------------------------*/
	/*-------------------------------------------------------*/
	/*-------------------------------------------------------*/
if ($tabla=='listprecios'){
	lista_pre();
}
	
function lista_pre(){
	$SqlTmp = "SELECT D_TABLAS.NUM_ITEM, D_TABLAS.DES_ITEM, D_TABLAS.abr_item, D_TABLAS.SWT_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.DES_ITEM";

	$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 list_prec" . "\n";
		$XvCabecera = "INSERT INTO list_prec (cod_list,des_list,activo) VALUES";
		echo $XvCabecera;
		while ($data = pg_fetch_array($XqTabla)){
			$clides = str_replace(';','',str_replace('"','',str_replace("'","",$data['des_item'])));
			$clides = str_replace('\R', ' ', $clides);
			$clides = str_replace('\R\N', ' ', $clides);
			$clides = str_replace('\N', ' ', $clides);
			$clides = str_replace('\r', ' ', $clides);
			$clides = str_replace('\r\n', ' ', $clides);
			$clides = str_replace('\n', ' ', $clides);
			$clides = str_replace(chr(13), ' ', $clides);
			$clides = str_replace(chr(10), ' ', $clides);
				
			$XvCadena = "('".str_replace("'","",rtrim($data['num_item'])) . "','" .
				rtrim($clides) . "'," .
				$data['swt_item'] . ")";

			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++;
		}
	}
	//*********************************************************************************
	$SqlTmp = "SELECT A.cdg_lprc, A.cdg_prod, 
		A.val_sol, A.pre_sol, A.min_sol, A.max_sol,
		A.val_dol, A.pre_dol, A.min_dol, A.max_dol  
		FROM m_precio AS A
		ORDER BY A.cdg_lprc, A.cdg_prod";

	$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 art_prec" . "\n";
		$XvCabecera = "INSERT INTO art_prec (cod_list,co_art,val_sol,pre_sol,min_sol,max_sol,val_dol,pre_dol,min_dol,max_dol) VALUES";
		echo $XvCabecera;
		while ($data = pg_fetch_array($XqTabla)){
			$XvCadena = "('" . rtrim($data['cdg_lprc']) . "','" .
				rtrim($data['cdg_prod']) . "'," .
				$data['val_sol'] . "," .
				$data['pre_sol'] . "," .
				$data['min_sol'] . "," .
				$data['max_sol'] . "," .
				$data['val_dol'] . "," .
				$data['pre_dol'] . "," .
				$data['min_dol'] . "," .
				$data['max_dol'] . ")";

			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=='artprecios'){
		$SqlTmp = "SELECT A.cdg_lprc, A.cdg_prod, 
			A.val_sol, A.pre_sol, A.min_sol, A.max_sol,
			A.val_dol, A.pre_dol, A.min_dol, A.max_dol  
			FROM m_precio AS A
			ORDER BY A.cdg_lprc, A.cdg_prod";

		$XvConteo = 1;
		$XvGrupoF = 0;		
		$XqTabla = pg_query( $SqlTmp) or die('La consulta fallo: ' . pg_last_error());
		$XvCantFila = pg_num_rows($XqTabla);
		
		if ($XvCantFila > 0){
			while ($data = pg_fetch_array($XqTabla)){
				$XvCadena = "SELECT '" . rtrim($data['cdg_lprc']) . "','" .
					rtrim($data['cdg_prod']) . "'," .
					$data['val_sol'] . "," .
					$data['pre_sol'] . "," .
					$data['min_sol'] . "," .
					$data['max_sol'] . "," .
					$data['val_dol'] . "," .
					$data['pre_dol'] . "," .
					$data['min_dol'] . "," .
					$data['max_dol'] . " ";

				if ($XvGrupoF >= 599 || $XvConteo>=$XvCantFila){
					$XvCadena = $XvCadena . "\n";
					$XvGrupoF = -1;
				}else{
					$XvCadena = $XvCadena . " UNION ALL ";
				}
				
				$XvConteo++;
				$XvGrupoF++;
				echo $XvCadena;
			}
		}
	}		
	/*-------------------------------------------------------*/
	/*-------------------------------------------------------*/
	/*-------------------------------------------------------*/
	if ($tabla=='sub_lin'){
		$SqlTmp = "SELECT A.co_subl, A.subl_des, A.co_lin
			FROM sub_lin AS A 
			INNER JOIN art AS B ON A.co_lin = B.co_lin AND A.co_subl= B.co_subl
			WHERE B.tipo = 'V'
			GROUP BY A.co_subl, A.subl_des, A.co_lin
			ORDER BY A.co_subl";

		$XvConteo = 1;
		$XvGrupoF = 0;		
		$XqTabla = pg_query( $SqlTmp)  or die('La consulta fallo: ' . pg_last_error());
		$XvCantFila = pg_num_rows($XqTabla);
		
		if ($XvCantFila > 0){
			$XvCantFilaFinal = $XvCantFila/500;
			$decimales = explode(".",$XvCantFilaFinal);
		
			if ($decimales[1]>0)
				$XvCantFilaFinal = $decimales[0] + 1;
			else
				$XvCantFilaFinal = $decimales[0];		
			
			$XvCantFilaFinal ++;
			
			echo $XvCantFilaFinal . "\n";
			while ($data = pg_fetch_array($XqTabla)){
				$XvCadena = "SELECT '".rtrim($data['co_subl']) . "','" .
					rtrim(str_replace("'","",$data['subl_des'])) . "'";			

				if ($XvGrupoF >= 499 || $XvConteo>=$XvCantFila){
					$XvCadena = $XvCadena . "\n";
					$XvGrupoF = -1;
				}else{
					$XvCadena = $XvCadena . " UNION ALL ";
				}
				$XvConteo++;
				$XvGrupoF++;
				echo $XvCadena;
			}
		}
	}

	/*-------------------------------------------------------*/
	/*-------------------------------------------------------*/
	/*-------------------------------------------------------*/
	if ($tabla=='bancos'){
		$SqlTmp = "SELECT co_ban, des_ban
			FROM bancos
			ORDER BY co_ban";

		$XvConteo = 1;
		$XvGrupoF = 0;		
		$XqTabla = pg_query( $SqlTmp) or die('La consulta fallo: ' . pg_last_error());
		$XvCantFila = pg_num_rows($XqTabla);
		
		if ($XvCantFila > 0){
			$XvCantFilaFinal = $XvCantFila/500;
			$decimales = explode(".",$XvCantFilaFinal);
		
			if ($decimales[1]>0)
				$XvCantFilaFinal = $decimales[0] + 1;
			else
				$XvCantFilaFinal = $decimales[0];		
			
			$XvCantFilaFinal ++;
			
			echo $XvCantFilaFinal . "\n";
			while ($data = pg_fetch_array($XqTabla)){
				$XvCadena = "SELECT '".rtrim($data['co_ban']) . "','" .
					rtrim(str_replace("'","",$data['des_ban'])) . "'";			

				if ($XvGrupoF >= 499 || $XvConteo>=$XvCantFila){
					$XvCadena = $XvCadena . "\n";
					$XvGrupoF = -1;
				}else{
					$XvCadena = $XvCadena . " UNION ALL ";
				}
				$XvConteo++;
				$XvGrupoF++;
				echo $XvCadena;
			}
		}
	}
	/*-------------------------------------------------------*/
	/*-------------------------------------------------------*/
	/*-------------------------------------------------------*/
	if ($tabla=='cuentas'){
		$SqlTmp = "SELECT cod_cta, co_banco, num_cta
			FROM cuentas
			ORDER BY cod_cta";

		$XvConteo = 1;
		$XvGrupoF = 0;		
		$XqTabla = pg_query( $SqlTmp) or die('La consulta fallo: ' . pg_last_error());
		$XvCantFila = pg_num_rows($XqTabla);
		
		if ($XvCantFila > 0){
			$XvCantFilaFinal = $XvCantFila/500;
			$decimales = explode(".",$XvCantFilaFinal);
		
			if ($decimales[1]>0)
				$XvCantFilaFinal = $decimales[0] + 1;
			else
				$XvCantFilaFinal = $decimales[0];		
			
			$XvCantFilaFinal ++;
			
			echo $XvCantFilaFinal . "\n";
			while ($data = pg_fetch_array($XqTabla)){
				$XvCadena = "SELECT '".rtrim($data['cod_cta']) . "','" .
					rtrim(str_replace("'","",$data['co_banco'])) . "','".
					rtrim(str_replace("'","",$data['num_cta'])) . "'";			

				if ($XvGrupoF >= 499 || $XvConteo>=$XvCantFila){
					$XvCadena = $XvCadena . "\n";
					$XvGrupoF = -1;
				}else{
					$XvCadena = $XvCadena . " UNION ALL ";
				}
				$XvConteo++;
				$XvGrupoF++;
				echo $XvCadena;
			}
		}
	}

	/*-------------------------------------------------------*/
	/*-------------------------------------------------------*/
	/*
	$SqlTmp = "SELECT idserial,nombre,claveweb,tipo,codasociado,email,anulado from usuario";
	$XqTabla = mysqli_query($con, $SqlTmp) or die(mysqli_error($con));
	$output = array();
	while($data = mysqli_fetch_array($XqTabla)){
		$valor = array(
                        'idserial' => $data['idserial'],
                        'claveweb' => $data['claveweb'],
                        'nombre' => htmlentities($data['nombre']),
						'codasociado' => htmlentities($data['codasociado']),
						'email' => htmlentities($data['email']),
						'anulado' => htmlentities($data['anulado']),
                        'tipo' => $data['tipo']
                    );
		$output[] = $valor;
	}
	$mastert['usuario'] = $output;
	*/
	/*-------------------------------------------------------*/
	/*-------------------------------------------------------*/
	/*-------------------------------------------------------*/
	/*
	$SqlTmp = "SELECT id_art,co_art,art_des,modelo,ref,co_lin,prec_vta1,prec_vta2,prec_vta3,prec_vta4,prec_vta5,stock_act,foto1,comentario from art";
	$XqTabla = mysqli_query($con, $SqlTmp) or die(mysqli_error($con));
	$output = array();
	while($data = mysqli_fetch_array($XqTabla)){
		$valor = array(
                        'id_art' => $data['id_art'],
                        'co_art' => $data['co_art'],
                        'art_des' => htmlentities($data['art_des']),
						'modelo' => htmlentities($data['modelo']),
						'ref' => htmlentities($data['ref']),
						'co_lin' => htmlentities($data['co_lin']),
                        'prec_vta1' => $data['prec_vta1'],
						'prec_vta2' => $data['prec_vta2'],
						'prec_vta3' => $data['prec_vta3'],
						'prec_vta4' => $data['prec_vta4'],
						'prec_vta5' => $data['prec_vta5'],
						'stock_act' => $data['stock_act'],
						'foto1' => $data['foto1']
                    );
		$output[] = $valor;
	}
	$mastert['art'] = $output;
	*/
	/*-------------------------------------------------------*/
	/*-------------------------------------------------------*/
	/*-------------------------------------------------------*/
	if ($tabla=='cxc'){
		//*********************************** Clientes ***************************************************
		$SqlTmp = "SELECT A.ruc_cli, A.des_cli, A.cdg_alt, A.ema_cli, A.crd_sol, A.cdg_tcli, A.swt_cli, A.cdg_vend, A.cdg_lprc 
			FROM m_client AS A 
			INNER JOIN M_CTECLI AS B ON A.ruc_cli = B.ruc_cli 
			WHERE  (B.IMP_DOCU - B.PAG_DOCU > 0) 
			AND (B.SWT_DOCU NOT IN ('T', 'A'))
			AND A.swt_cli = 1 ";
			
		if ($codven != ''){
			$SqlTmp = $SqlTmp . " AND B.cdg_vend = '$codven' ";
		}
		
		$XqTabla = pg_query( $SqlTmp) or die('La consulta fallo: ' . pg_last_error());
		$XvCantFila = pg_num_rows($XqTabla);
		
		if ($XvCantFila > 0){
			$XvConteo = 1;
			$XvGrupoF = 0;		
			$XvCabecera = "INSERT INTO clientes (co_cli,cli_des,rif,email,tipo,co_ven,mont_cre,activo,cdg_lprc) ";
			echo $XvCabecera;
			while ($data = pg_fetch_array($XqTabla)){
				$clides = str_replace(';','',str_replace('"','',str_replace("'","",$data['des_cli'])));
				$clides = str_replace('\R', ' ', $clides);
				$clides = str_replace('\R\N', ' ', $clides);
				$clides = str_replace('\N', ' ', $clides);
				$clides = str_replace('\r', ' ', $clides);
				$clides = str_replace('\r\n', ' ', $clides);
				$clides = str_replace('\n', ' ', $clides);
				$clides = str_replace(chr(13), ' ', $clides);
				$clides = str_replace(chr(10), ' ', $clides);
				
				$codcli = str_replace("'","",rtrim($data['ruc_cli']));
				
				$XvCadena = "SELECT '". $codcli . "','" .
					rtrim($clides) . "','" .
					rtrim(str_replace("'","",$data['cdg_alt'])) ."','".
					rtrim(str_replace("'","",$data['ema_cli'])) ."','".
					rtrim(str_replace("'","",$data['cdg_tcli'])) ."','".
					rtrim(str_replace("'","",$data['cdg_vend'])) ."',".
					$data['crd_sol'] . "," .
					$data['swt_cli'] . ",'" .
					$data['cdg_lprc'] . "' " .
					"WHERE NOT EXISTS(SELECT co_cli FROM clientes WHERE co_cli='". $codcli . "')";		

				if ($XvGrupoF >= 499 || $XvConteo>=$XvCantFila){
					$XvCadena = $XvCadena . "\n";
					$XvGrupoF = -1;
					echo $XvCadena;
					if (($XvConteo+2) < $XvCantFila){
						echo $XvCabecera;
					}
				}else{
					$XvCadena = $XvCadena . " UNION ";
					echo $XvCadena;
				}
				$XvConteo++;
				$XvGrupoF++;
			}
		}
		//*********************************** Cuentas por Cobrar ***************************************************
		$SqlTmp = "SELECT M_CTECLI.CDG_TDOC, M_CTECLI.NUM_DOCU, M_CTECLI.RUC_CLI, 
			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, B.SaldoTotal,
			T_DOCCLI.NUM_SER, (M_CTECLI.IMP_DOCU - M_CTECLI.PAG_DOCU) AS Saldo  
			FROM M_CTECLI 
			INNER JOIN T_DOCCLI ON M_CTECLI.CDG_TDOC = T_DOCCLI.CDG_TDOC
			INNER JOIN (SELECT RUC_CLI, SUM(IMP_DOCU - PAG_DOCU) AS SaldoTotal
							FROM M_CTECLI AS X
                            WHERE (IMP_DOCU - PAG_DOCU > 0) 
							AND (SWT_DOCU NOT IN ('T', 'A'))
                            GROUP BY RUC_CLI) AS B ON M_CTECLI.RUC_CLI = B.RUC_CLI
			WHERE (M_CTECLI.IMP_DOCU - M_CTECLI.PAG_DOCU > 0) 
			AND (M_CTECLI.SWT_DOCU NOT IN ('T', 'A'))
			ORDER BY M_CTECLI.RUC_CLI, M_CTECLI.FEC_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, fec_emis, fec_venc, co_ven, neto, saldo, saldoTotal) ";
			echo $XvCabecera;
			while ($data = pg_fetch_array($XqTabla)){
				$XvCadena = "SELECT '" . rtrim($data['num_ser']) . "','" .
					rtrim($data['num_docu']) . "','" .
					$data['ruc_cli']  . "','" .
					$data['fec_docu'] . "','" .
					$data['vct_docu'] . "',''," .
					$data['imp_docu'] . "," .
					$data['saldo'] . "," .
					$data['saldototal'];

				if ($XvGrupoF >= 499 || $XvConteo>=$XvCantFila){
					$XvCadena = $XvCadena . "\n";
					$XvGrupoF = -1;
					echo $XvCadena;
					if (($XvConteo+2) < $XvCantFila){
						echo $XvCabecera;
					}
				}else{
					$XvCadena = $XvCadena . " UNION ";
					echo $XvCadena;
				}
				$XvConteo++;
				$XvGrupoF++;
			}
		}
	}
/*-------------------------------------------------------*/
/*-------------------------------------------------------*/
/*-------------------------------------------------------*/
if ($tabla=='pedidos'){
	global $codven;
	lista_lin();
		
	$SqlTmp = "SELECT A.cdg_prod, A.des_prod, A.cdg_linp, A.swt_prod 
			FROM M_PRODUC AS A 
			INNER JOIN d_pedido AS B ON A.cdg_prod = B.cdg_prod 
			INNER JOIN m_pedido AS C ON B.num_ped = C.num_ped 
			WHERE C.swt_ped = '' 
			AND C.cdg_vend = '$codven' 
			GROUP BY A.cdg_prod, A.des_prod, A.cdg_linp, A.swt_prod 
			ORDER BY A.cdg_prod ";

	$XqTabla = pg_query($SqlTmp) or die('Error - SQL (art): ' . pg_last_error());
	$XvCantFila = pg_num_rows($XqTabla);
			
	if ($XvCantFila > 0){
		$XvConteo = 1;
		$XvGrupoF = 0;
		$XvCabecera = "INSERT INTO art (co_art, art_des, co_lin, activo, stock_act, stock_com) ";
		echo $XvCabecera;
		while ($data = pg_fetch_array($XqTabla)){
			$XvDesArt = rtrim(str_replace("'","",$data['des_prod']));
					
			$XvCadena = "SELECT '" . rtrim($data['cdg_prod']) . "','" .
				$XvDesArt . "','" .
				rtrim($data['cdg_linp']) ."'," .
				$data['swt_prod'] . "," .
				"100,0 WHERE NOT EXISTS(SELECT co_art FROM art WHERE co_art='".rtrim($data['cdg_prod'])."') ";

			if ($XvGrupoF >= 499 || $XvConteo>=$XvCantFila){
				$XvCadena = $XvCadena . "\n";
				$XvGrupoF = -1;
				echo $XvCadena;
				if (($XvConteo+2) < $XvCantFila){
					echo $XvCabecera;
				}
			}else{
				$XvCadena = $XvCadena . " UNION ";
				echo $XvCadena;
			}
			$XvConteo++;
			$XvGrupoF++;
		}
	}

	//*********************************** Clientes ***************************************************
	$SqlTmp = "SELECT A.ruc_cli, A.des_cli, A.cdg_alt, A.ema_cli, A.crd_sol, A.cdg_tcli, A.swt_cli, A.cdg_vend, A.cdg_lprc 
		FROM m_client AS A 
		INNER JOIN m_pedido AS B ON A.ruc_cli = B.ruc_cli 
		WHERE B.swt_ped = ''
		AND A.swt_cli = 1 ";
			
	if ($codven != ''){
		$SqlTmp = $SqlTmp . " AND B.cdg_vend = '$codven' ";
	}
		
	$XqTabla = pg_query( $SqlTmp) or die('La consulta fallo: ' . pg_last_error());
	$XvCantFila = pg_num_rows($XqTabla);
		
	if ($XvCantFila > 0){
		$XvConteo = 1;
		$XvGrupoF = 0;		
		$XvCabecera = "INSERT INTO clientes (co_cli,cli_des,rif,email,tipo,co_ven,mont_cre,activo,cdg_lprc) ";
		echo $XvCabecera;
		while ($data = pg_fetch_array($XqTabla)){
			$clides = str_replace(';','',str_replace('"','',str_replace("'","",$data['des_cli'])));
			$clides = str_replace('\R', ' ', $clides);
			$clides = str_replace('\R\N', ' ', $clides);
			$clides = str_replace('\N', ' ', $clides);
			$clides = str_replace('\r', ' ', $clides);
			$clides = str_replace('\r\n', ' ', $clides);
			$clides = str_replace('\n', ' ', $clides);
			$clides = str_replace(chr(13), ' ', $clides);
			$clides = str_replace(chr(10), ' ', $clides);
				
			$codcli = str_replace("'","",rtrim($data['ruc_cli']));
				
			$XvCadena = "SELECT '". $codcli . "','" .
				rtrim($clides) . "','" .
				rtrim(str_replace("'","",$data['cdg_alt'])) ."','".
				rtrim(str_replace("'","",$data['ema_cli'])) ."','".
				rtrim(str_replace("'","",$data['cdg_tcli'])) ."','".
				rtrim(str_replace("'","",$data['cdg_vend'])) ."',".
				$data['crd_sol'] . "," .
				$data['swt_cli'] . ",'" .
				$data['cdg_lprc'] . "' " .
				"WHERE NOT EXISTS(SELECT co_cli FROM clientes WHERE co_cli='". $codcli . "')";		

			if ($XvGrupoF >= 499 || $XvConteo>=$XvCantFila){
				$XvCadena = $XvCadena . "\n";
				$XvGrupoF = -1;
				echo $XvCadena;
				if (($XvConteo+2) < $XvCantFila){
					echo $XvCabecera;
				}
			}else{
				$XvCadena = $XvCadena . " UNION ";
				echo $XvCadena;
			}
			$XvConteo++;
			$XvGrupoF++;
		}
	}
	//************************************ Pedidos ******************************************
	$SqlTmp = "SELECT A.num_ped, A.ruc_cli, A.cdg_vend, A.cdg_cpag, A.cdg_mon, A.fec_ped, A.imp_ttot, A.imp_stot, A.imp_tigv 
		FROM m_pedido AS A
		WHERE A.cdg_vend = '$codven' 
		AND A.swt_ped = ''
		ORDER BY A.num_ped";

	$XvGrupoF = 0;		
	$XqTabla = pg_query($SqlTmp) or die('La consulta fallo: ' . pg_last_error());
	$XvCantFila = pg_num_rows($XqTabla);
		
	if ($XvCantFila > 0){
		$XvConteo = 1;
		echo "DELETE FROM reng_ped WHERE id_ped IN (SELECT id_ped FROM pedidos WHERE nro_fac=0 AND ped_serv = 1) " . "\n";
		$XvCabecera = "INSERT INTO pedidos (ped_num, co_cli, co_ven, cdg_cpag, cdg_mon, fec_emis, tot_neto, tot_bruto, iva, tip_doc, ped_serv, destino) ";
		echo $XvCabecera;
		while ($data = pg_fetch_array($XqTabla)){
			$XvCadena = "SELECT '". rtrim($data['num_ped']) . "','" .
				rtrim($data['ruc_cli']) . "','" .
				rtrim($data['cdg_vend']) . "','" .
				rtrim($data['cdg_cpag']) . "','" .
				rtrim($data['cdg_mon']) . "','" .
				rtrim($data['fec_ped']) . "'," .
				$data['imp_ttot'] . "," .
				$data['imp_stot'] . "," .
				$data['imp_tigv'] . "," .
				"'PED', 1, 'PED' " .
				"WHERE NOT EXISTS(SELECT ped_num FROM pedidos WHERE ped_num='". rtrim($data['num_ped']) . "')";

			if ($XvGrupoF >= 499 || $XvConteo>=$XvCantFila){
				$XvCadena = $XvCadena . "\n";
				$XvGrupoF = -1;
				echo $XvCadena;

				if (($XvConteo+2) < $XvCantFila){
					echo $XvCabecera;
				}
			}else{
				$XvCadena = $XvCadena . " UNION ";
				echo $XvCadena;
			}
			$XvConteo++;
			$XvGrupoF++;
		}
	}
	//**************** Renglones Pedidos ***********************
	$SqlTmp = "SELECT A.num_ped, A.cdg_prod, A.can_pprd, A.pre_pprd, A.imp_tprd, A.pre_igv, A.imp_igv 
		FROM d_pedido AS A 
		INNER JOIN m_pedido AS B ON A.num_ped = B.num_ped 
		WHERE B.cdg_vend = '$codven' 
		AND B.swt_ped = ''
		ORDER BY A.num_ped";

	$XvGrupoF = 0;		
	$XqTabla = pg_query($SqlTmp) or die('La consulta fallo: ' . pg_last_error());
	$XvCantFila = pg_num_rows($XqTabla);
		
	if ($XvCantFila > 0){
		$XvConteo = 1;
		$XvCabecera = "INSERT INTO reng_ped (id_ped, co_art, total_art, prec_vta, reng_neto, prec_vta_igv, reng_neto_igv) ";
		echo $XvCabecera;
		while ($data = pg_fetch_array($XqTabla)){
			$XvCadena = "SELECT A.id_ped,'" . rtrim($data['cdg_prod']) . "'," .
				rtrim($data['can_pprd']) . "," .
				rtrim($data['pre_pprd']) . "," .
				rtrim($data['imp_tprd']) . ",".
				rtrim($data['pre_igv']) . ",".
				rtrim($data['imp_igv']) . " ".
				"FROM pedidos AS A WHERE A.ped_num='".rtrim($data['num_ped'])."' AND A.nro_fac=0 AND A.ped_serv=1 ";

			if ($XvGrupoF >= 499 || $XvConteo>=$XvCantFila){
				$XvCadena = $XvCadena . "\n";
				$XvGrupoF = -1;
				echo $XvCadena;

				if (($XvConteo+2) < $XvCantFila){
					echo $XvCabecera;
				}
			}else{
				$XvCadena = $XvCadena . " UNION ";
				echo $XvCadena;
			}
			$XvConteo++;
			$XvGrupoF++;
		}
	}
}

pg_close($con);


?>