<?php 
session_start();

require('../cfg/res.php');
require('../cfg/cfg.php');
?>
<?php

$fecha1 = date('Y-m-j');
$nuevafecha = strtotime ('-1 month',strtotime($fecha1));
$xfecha1= date('Y/m/j',$nuevafecha );
$xfecha1= date('Y/m/'.'1');
$xfecha2= date('Y/m/d');

$sql_datesp='SET lc_time_names = "es_UY"';
$res_datesp=mysql_query($sql_datesp,$link_vcrx);

$xtipo='';
if(isset($_POST['xtipo']) && !empty($_POST['xtipo']))
{$xtipo=$_POST['xtipo'];}

$xfgrou='';
if(isset($_POST['fgrou']) && !empty($_POST['fgrou']))
{$xfgrou=$_POST['fgrou'];}


$xfentt='4';
if(isset($_POST['fentt']) && !empty($_POST['fentt']))
{$xfentt=$_POST['fentt'];}


if(isset($_POST['fcodi']) && !empty($_POST['fcodi']))
{$xfcodi=$_POST['fcodi'];}
switch ($xfcodi)
{
	case '1': $item_codi='enti';break;
	case '2': $item_codi='enti_codigoc';break;
	case '3': $item_codi='enti_codigop';break;
	default : $item_codi='enti';break;
}

// mysql_query('update enti0 set enti_direccion=upper(enti_direccion), enti_calle1=upper(enti_calle1), enti_calle2=upper(enti_calle2),enti_callen=upper(enti_callen), enti_calle3=upper(enti_calle3)',$link_vcrx);

$sql_entil='select * from enti00 left join adrs0 on enti_adrs=adrs_codigo where ';
/*
$fecha1 = date('Y-m-j');
$nuevafecha = strtotime ( '-1 month' , strtotime ( $fecha1 ) ) ;
$xfecha1 = date ( 'Y/m/j' , $nuevafecha );
$xfecha1= date('Y/m/'.'1');
$xfecha2= date('Y/m/d');
*/


if(isset($_POST['fecha_ini']) && !empty($_POST['fecha_ini']) && isset($_POST['fecha_fin']) && !empty($_POST['fecha_fin']))
{
	$xfecha1=$_POST['fecha_ini'];
	$xfecha2=$_POST['fecha_fin'];
}

	$xanios=date('Y',strtotime($xfecha2b))-date('Y',strtotime($xfecha1b))+1;
	$xanio1=date('Y',strtotime($xfecha1b));
	$xanio2=date('Y',strtotime($xfecha2b));

	$dias=intval(strtotime($xfecha2b) - strtotime($xfecha1b))/60/60/24;
  $anios=date('Y',strtotime($xfecha2))-date('Y',strtotime($xfecha1));

$sql_entil.='(';
// $sql_entil.='(date_format(enti_idate,"%Y-%m-%d") BETWEEN "'.$xfecha1.'" and "'.$xfecha2.'") or enti_idate is null';
$sql_entil.='1=1';
$sql_entil.=')';

$sql_entil=str_replace(')(',')||(',$sql_entil);


if(isset($_POST['enti']) && !empty($_POST['enti']))
{
	$sql_entil.=' and  (enti_codigo like "'.$_POST['enti'].'%" or enti_descri like "%'.$_POST['enti'].'%" ) ';
	$xfenti=$_POST['enti'];
}

// console.log($xfentt);
// if ($xfentt=='1'){$sql_entil.=' and  (enti_codigo in (select enti_codigo from clie0))';}
// if ($xfentt=='2'){$sql_entil.=' and  (enti_codigo in (select enti_codigo from prov0))';}
// if ($xfentt=='3'){$sql_entil.=' and  (enti_codigo in (select enti_codigo from empl0))';}

if ($xfentt=='1'){$sql_entil.=' and(enti_codigo in (select distinct enti from vtas1 where (date_format(fecha,"%Y-%m-%d") BETWEEN "'.$xfecha1.'" and "'.$xfecha2.'")))';}
if ($xfentt=='2'){$sql_entil.=' and(enti_codigo in (select distinct enti from comp1 where (date_format(fecha,"%Y-%m-%d") BETWEEN "'.$xfecha1.'" and "'.$xfecha2.'")))';}
if ($xfentt=='3'){$sql_entil.=' and(enti_codigo in (select distinct enti from rolp1 where (date_format(fecha,"%Y-%m-%d") BETWEEN "'.$xfecha1.'" and "'.$xfecha2.'")))';}

// console.log($sql_entil);


if(isset($_POST['item']) && !empty($_POST['item']))
{
// 	$sql_entil.=' and  (item_codigo like "'.$_POST['item'].'%" or item_descri like "%'.$_POST['item'].'%" ) ';
	$xfitem=$_POST['item'];
}


if(isset($_POST['fcdad']) && !empty($_POST['fcdad']))
{
	$sql_entil.=' and  (prvc_descri like "%'.$_POST['fcdad'].'%" or enti_cdad like "'.$_POST['fcdad'].'%" or cdad_descri like "%'.$_POST['fcdad'].'%"  ) ';
	$xfcdad=$_POST['fcdad'];
}

if(isset($_POST['flsub']) && !empty($_POST['flsub']))
{
// 	$sql_entil.=' and  (item_lsub like "'.$_POST['flsub'].'%" or lsub_descri like "%'.$_POST['flsub'].'%" ) ';
	$xflsub=$_POST['flsub'];
}

if(isset($_POST['fzsub']) && !empty($_POST['fzsub']))
{
	$sql_entil.=' and  (enti_zsub like "'.$_POST['fzsub'].'%" or zsub_descri like "%'.$_POST['fzsub'].'%" ) ';
	$xfzsub=$_POST['fzsub'];
}

if(isset($_POST['fvndd']) && !empty($_POST['fvndd']))
{
	$sql_entil.=' and  (enti_vndd like "'.$_POST['fvndd'].'%" or vndd_descri like "%'.$_POST['fvndd'].'%" ) ';
	$xfvndd=$_POST['fvndd'];
}


if(isset($_POST['frcdd']) && !empty($_POST['frcdd']))
{
	$sql_entil.=' and  (enti_rcdd like "'.$_POST['frcdd'].'%" or rcdd_descri like "%'.$_POST['frcdd'].'%" ) ';
	$xfrcdd=$_POST['frcdd'];
}


if(isset($_POST['fctlg']) && !empty($_POST['fctlg']))
{
	$xfctlg=$_POST['fctlg'];
}


	$sql_entil.=' and(enti_status!="X") ';
	$sql_entil.=' and(enti_status!="O") ';


$xforde='D';
if(isset($_POST['forde']) && !empty($_POST['forde']))
{$xforde=$_POST['forde'];}

switch ($xfgrou)
{

	case 'T':$xgroup='tpro_descri,enti_tpro ';break;
	case 'V':$xgroup='vndd_descri,enti_vndd ';break;
	case 'R':$xgroup='rcdd_descri,enti_rcdd ';break;
	case 'Z':$xgroup='zsub_descri,enti_zsub ';break;
	case 'L':$xgroup='lsub_descri,item_lsub ';break;
// 	case 'R':$xgroup='item_at10 ';break;
	case 'C':$xgroup='cdad_descri, enti_cdad ';break;
	default :$xgroup='enti_descri, enti_codigo';break;
}

// $xgroup='';
switch ($xforde)
{
	case 'D':$xorder=$xgroup.',enti_descri,enti_codigo';break;
	case 'C':$xorder=$xgroup.',enti_codigo';break;
}

//$sql_entil.=' group by '.$xgroup;
$sql_entil.=' order by '.$xorder;
// include ('php_gene1.php');
// echo explain_sql($sql_entil);
$res_entil=mysql_query($sql_entil,$link_vcrx);
$totreg = mysql_num_rows($res_entil);
// echo str_repeat('-',date('s')*2).'';

?>
<table id="tabla_cxccli" width="100%" border="0" cellspacing="0" cellpadding="2" style="border-collapse:collapse;font-size:12px; border:#000000 solid 1px;">
<tr style="border-bottom:#000000 solid 2px; ">
	<th align="center">M</th>
  <th align="center">I</th>
	<th>ZONA</th>
	<th>T.PRO</th>
	<th>CODIGO</th>
	<th>ID/TIPO/RUC</th>
	<th>NOMBRE</th>
	<th>e-mail</th>
	<th>TELEFONO</th>
	<th>DIRECCION</th>
	<?php if ($xtipo==1){?>
	<th>PROV/CIUDAD</th>
	<th>VENDEDOR</th>
	<th>RECAUDADOR</th>
	<th>FEC.REG.</th>
	<?php } ?>

	<?php if ($xtipo==4){?>
	<th>CTA X COBRAR</th>
	<th>CTA X PAGAR</th>
	<th></th>
	<?php } ?>
</tr>

<?php 
$filax=0;
while($r_entil=mysql_fetch_assoc($res_entil))
{
// $duni=$r_entil['item_duni'];
?>
<tr class="filax" valign="top" id="<?php echo $r_entil['idnum1']; ?>" align="center" style=" <?php if($filax>=4){echo 'border-top:dotted 1px #000;';$filax=1;}else{$filax++;} ?>" >
<td align="center">
	<a href="nenti.php?idx=<?php echo $r_entil['enti_codigo']; ?>" target="_blank">
		<img src="http://vcrxol.com/_img/ico_edit.png" width="20" title="Modificar  <?php echo $r_entil['enti_codigo'];?>"  /></a></td>
<td align="center">
		<a href="lblenti0.php?ci=<?php echo $r_entil['enti_codigo']; ?>" target="_blank">
		<img src="../img/print2.png" width="20"  title="Imprimir  <?php echo $r_entil['enti_codigo'];?>"  /></a>
    </td>

	<td align="center"><?php echo'<span title="'.$r_entil['enti_zsub'].'">'.$r_entil['zsub_descri'].'</span>';?></td>
	<td align="center"><?php echo'<span title="'.$r_entil['tpro_descri'].'">'.$r_entil['enti_tpro'].'</span>';?></td>
  <td align="left">
		<a href="#" onClick="entil1d('<?php echo $xfgrou; ?>','<?php echo $r_entil['enti_codigo']; ?>',this);">
		<?php echo '<div><span title="'.$r_entil['enti_codigo'].'">'.$r_entil['enti_codigo'].'</span></div>';?>
		</a> 
		<?php echo'<div><span title="'.$r_entil['enti_pasaporte'].'">'.$r_entil['enti_pasaporte'].'</span></div>';?>
		<?php echo'<div><span title="'.$r_entil['enti_codigoe'].'">'.$r_entil['enti_codigoe'].'</span></div>';?>
		</td>
	<td align="center"><?php echo'<span title="'.$r_entil['enti_tipoid'].'">'.$r_entil['enti_tipoid'].' / '.$r_entil['enti_tiporuc'].' / '.$r_entil['enti_struc'].'</span>';?></td>
	<td align="left">
		<?php 
		$xdescri=$r_entil['enti_descri'];
		if(trim($r_entil['enti_ape1'])!="")
		{$xdescri=trim($r_entil['enti_ape1']).' '.trim($r_entil['enti_ape2']).' '.trim($r_entil['enti_nom1']).' '.trim($r_entil['enti_nom2']);}
		if ($xfentt=='1')
		{
		if(mb_stristr('PJ',$r_entil['enti_tiporuc'])){echo'<div><span title="'.$r_entil['enti_codigo'].'">'.'RS: '.$r_entil['enti_razsoc'].'</span></div>';}
		if(mb_stristr('N-',$r_entil['enti_tiporuc'])){echo'<div><span title="'.$r_entil['enti_codigo'].'">'.'NA: '.$xdescri.'</span></div>';}
		if(mb_stristr('P',$r_entil['enti_tipoid'])){echo'<div><span title="'.$r_entil['enti_codigo'].'">'.'N: '.$xdescri.'</span></div>';}
		}
		if ($xfentt=='2')
		{
		if(mb_stristr('PJ',$r_entil['enti_tiporuc'])){echo'<div><span title="'.$r_entil['enti_codigo'].'">'.'RS: '.$r_entil['enti_razsoc'].'</span></div>';}
		if(mb_stristr('N-',$r_entil['enti_tiporuc'])){echo'<div><span title="'.$r_entil['enti_codigo'].'">'.'NA: '.$xdescri.'</span></div>';}
		if(mb_stristr('P',$r_entil['enti_tipoid'])){echo'<div><span title="'.$r_entil['enti_codigo'].'">'.'N: '.$xdescri.'</span></div>';}
		}
		if ($xfentt=='3')
		{
		if(mb_stristr('N-',$r_entil['enti_tiporuc'])){echo'<div><span title="'.$r_entil['enti_codigo'].'">'.'NA: '.$xdescri.'</span></div>';}
		}
		if ($xfentt=='4')
		{
		if(mb_stristr('PJ',$r_entil['enti_tiporuc'])){echo'<div><span title="'.$r_entil['enti_codigo'].'">'.'RS: '.$r_entil['enti_razsoc'].'</span></div>';}
		if(mb_stristr('NF-',$r_entil['enti_tiporuc'])){echo'<div><span title="'.$r_entil['enti_codigo'].'">'.'NA: '.$xdescri.'</span></div>';}
		if(mb_stristr('E',$r_entil['enti_tiporuc'])){echo'<div><span title="'.$r_entil['enti_codigo'].'">'.': '.$xdescri.'</span></div>';}
		}
		?>
	</td>
	<td align="left"><?php echo'<span title="'.$r_entil['adrs_email'].'">'.$r_entil['adrs_email'].'</span>';?></td>
	<td align="left"><?php echo'<span title="'.$r_entil['adrs_tlfn'].'">'.$r_entil['adrs_tlfn'].'</span>';?></td>
	<td align="left">
 		<?php echo'<div><span title="'.$r_entil['adrs_direccion'].'">'.$r_entil['adrs_direccion'].'</span></div>';?>
<!-- 
		<?php echo'<div><span title="'.$r_entil['adrs_direccion'].'">'.$r_entil['adrs_calle1'].'</span></div>';?>
		<?php echo'<div><span title="'.$r_entil['adrs_direccion'].'">'.$r_entil['adrs_calle2'].'</span></div>';?>
		<?php echo'<div><span title="'.$r_entil['adrs_direccion'].'">'.$r_entil['adrs_callen'].'</span></div>';?>
		<?php echo'<div><span title="'.$r_entil['adrs_direccion'].'">'.$r_entil['adrs_referencia'].'</span></div>';?>
 -->
	</td>
	<?php if ($xtipo==1){?>
	<td align="center"><?php echo'<span title="'.$r_entil['enti_cdad'].'">'.$r_entil['prvc_descri'].'/'.$r_entil['cdad_descri'].'</span>';?></td>
	<td align="center"><?php echo'<span title="'.$r_entil['enti_vndd'].'">'.$r_entil['vndd_descri'].'</span>';?></td>
	<td align="center"><?php echo'<span title="'.$r_entil['enti_rcdd'].'">'.$r_entil['rcdd_descri'].'</span>';?></td>
	<td align="center" style="font-family:courier;"><?php echo'<span title="'.$r_entil['enti_iuser'].'"><font size="1">'.date_format(date_create($r_entil['enti_idate']),"d-m-y H:i:s").'</font></span>';?></td>
	<?php } ?>

	<?php if ($xtipo==4){?>
	<td>
		<?php $pcaption=$r_entil['enti_ctaxcob'];  $ptabla='ctas'; $pidtxt='cxc_'.$r_entil['enti_codigo']; $pdefa=$r_entil['enti_ctaxcob']; $pobj=1; include('vrgetcod.php'); ?>
	</td>
	<td>
		<?php $pcaption=$r_entil['enti_ctaxpag'];  $ptabla='ctas'; $pidtxt='cxp_'.$r_entil['enti_codigo']; $pdefa=$r_entil['enti_ctaxpag']; $pobj=1; include('vrgetcod.php'); ?>
	</td>
	<td>
		<?php $pcaption='Modif';  $ponclick='_modi('.$r_entil['enti_codigo'].');'; include('vrbutton.php'); ?>
	</td>
	<td style="font-size:10px;"><div id="error_<?php echo $r_entil['enti_codigo'];?>"></div></td>
	<?php } ?>
	
</tr>
<?php }?>


</table>
</div>