<?php
session_start();

echo date('h:i:s');
require('../cfg/res.php');
require('../../_lib/vrdocu0.php');
require('../../_lib/vr_contab.php');
include('php_gene1.php');


// require('../../_lib/vr_updsql.php');
// $contab=new vrupdt0('empr',4);
// die();

// $doc=new vrdocu0('../cfg/res.php',$_POST['docu']);

$modu=$_POST['modu'];
$docu=$_POST['docu'];
$enti=$_POST['enti'];
$contab=$_POST['contab'];
// $tabla=r_fiel('docu',$docu,'tabla');
// echo $docu.$contab;

$sql_docu='select * from docu0 where docu_codigo like "'.$docu.'%" order by docu_codigo';
$res_docu=mysql_query($sql_docu,$link_vcrx);
$tabla=mysql_result($res_docu,0,'docu_tabla');

if($modu!="")
{$sql_gene1='select * from '.strtolower($_POST['modu']).'1 where numero>"0" ';}
else
{$sql_gene1='select * from '.$tabla.'1 where numero>"0" ';}

if(!empty($_POST['docu']))
{$sql_gene1.=' and (docu like "'.$_POST['docu'].'%")';}

if(!empty($_POST['enti']))
{$sql_gene1.=' and (enti like "'.$_POST['enti'].'%")';}

if(!empty($_POST['nini']) && !empty($_POST['nfin']))
{$sql_gene1.=' and (numero>="'.$_POST['nini'].'" and numero<="'.$_POST['nfin'].'")';}

if(!empty($_POST['fini']) && !empty($_POST['ffin']))
{$sql_gene1.=' and (fecha>="'.$_POST['fini'].'" and fecha<="'.$_POST['ffin'].'")';}

// para validar los nuevos
// $sql_gene1.=' and idnum1 not in(select idfrom from cont1 where docu like "'.substr($docu,0,2).'%") ';
if($contab==1)
{$sql_gene1.=' and idnum1 not in(select idfrom from cont1 where mdfrom ="'.strtoupper($tabla).'") ';}


// ESPECIAL
// $sql_gene1.=' and enti like "99999%" and docu like "CH%"';
// $sql_gene1.=' and idnum1 in(select idnum2 from bcos2 where docum like "FP%" and (ctas like "5%"))';

// echo $sql_gene1;
// echo '*/*/*/*'.$contab2;

$sql_gene1.=' order by fecha, docu, numero ';

// echo explain_sql($sql_gene1);  


echo '<br>';
$res_gene1=mysql_query($sql_gene1,$link_vcrx);
if(mysql_num_rows($res_gene1)==0)
{echo 'No se encontraron NUEVOS Comprobantes';}
else
{
	while($gene1_r=mysql_fetch_assoc($res_gene1))
	{
    updt_ctascomp($gene1_r['docu'],$gene1_r['idnum1']);
    gen_caba13($gene1_r['docu'],$gene1_r['idnum1']);
//     gen_invent($gene1_r['docu'],$gene1_r['idnum1']);
    
		$idcontab=gen_contab($gene1_r['docu'],$gene1_r['idnum1']);
    
		echo '<br>';
		echo $gene1_r['docu'].'-'.$gene1_r['numero'].'-'.$gene1_r['fecha'];
	}
}	

if($contab=='2' and 1==2)
{
	echo '<br>';
	$sql_gene2=str_replace('idnum1 not in','idnum1 in',$sql_gene1);
	$res_gene1=mysql_query($sql_gene2,$link_vcrx);
	if(mysql_num_rows($res_gene1)==0)
	{echo 'No se encontraron Comprobantes.';}
	else
	{
		while($gene1_r=mysql_fetch_assoc($res_gene1))
		{
			$idcontab=gen_contab($gene1_r['docu'],$gene1_r['idnum1']);
      echo '<br>';
      echo $gene1_r['docu'].'-'.$gene1_r['numero'].'-'.$gene1_r['fecha'];
		}
	}	
}	



?>