<?php
function vrentil1d($grupo,$idnum,$fini,$ffin)
{
	if(isset($_POST['fini']) && !empty($_POST['fini']))
	{
		$fini=$_POST['fini'];
	}
	else
	{
		$fini=date('2000-01-01');
	}
	
	if(isset($_POST['ffin']) && !empty($_POST['ffin']))
	{
		$ffin=$_POST['ffin'];
	}
	else
	{
		$ffin=date('Y-m-d');
	}

	
	
	require('../cfg/res.php');
	mysql_query("SET NAMES 'utf8'");



?>


<?php
switch ($grupo)
{
	case 'D':
	    $sql_enti ='select * from enti00 where idnum1='.$idnum.' and fecha>="'.$fini.'" and fecha<="'.$ffin.'" order by item' ;
		$sql_entit='select *,sum(tdcto) as ttdcto, sum(tice) as ttice, sum(tiva) as ttiva, sum(tirbp) as ttirbp from enti00 where idnum1='.$idnum.' ' ;
		break;

	case 'E':
	    $sql_enti ='select * from enti00 where enti like "'.$idnum.'%" and fecha>="'.$fini.'" and fecha<="'.$ffin.'" order by fecha desc, docu, ptonumero' ;
		$sql_entit='select *, sum(tdcto) as ttdcto, sum(tice) as ttice, sum(tiva) as ttiva, sum(tirbp) as ttirbp  from enti00 where enti like "'.$idnum.'%" group by enti' ;
		break;

	case 'I':
		$xgroup='item_lsub,item_descri ';
	    $sql_enti ='select * from enti00 where item like "'.$idnum.'" and fecha>="'.$fini.'" and fecha<="'.$ffin.'" order by fecha desc, docu, ptonumero' ;
		$sql_entit='select *, sum(tdcto) as ttdcto, sum(tice) as ttice, sum(tiva) as ttiva, sum(tirbp) as ttirbp  from enti00 where item like "'.$idnum.'%" group by enti' ;
		break;

	case 'P':
		$xgroup='ptoe ';
	    $sql_enti ='select * from enti00 where ptoe like "'.$idnum.'%" and fecha>="'.$fini.'" and fecha<="'.$ffin.'" order by fecha desc, docu, ptonumero' ;
		$sql_entit='select *, sum(tdcto) as ttdcto, sum(tice) as ttice, sum(tiva) as ttiva, sum(tirbp) as ttirbp  from enti00 where ptoe like "'.$idnum.'%" group by enti' ;
		break;
	case 'V':
		$xgroup='vndd ';
	    $sql_enti ='select * from enti00 where vndd like "'.$idnum.'%" and fecha>="'.$fini.'" and fecha<="'.$ffin.'" order by fecha desc, docu, ptonumero' ;
		$sql_entit='select *, sum(tdcto) as ttdcto, sum(tice) as ttice, sum(tiva) as ttiva, sum(tirbp) as ttirbp  from enti00 where vndd like "'.$idnum.'%" group by enti' ;
		break;
	case 'Z':$xgroup='zsub ';break;
	case 'L':$xgroup='item_lsub ';break;
	case 'R':$xgroup='item_at10 ';break;
	case 'C':$xgroup='cdad ';break;

	case 'A':$xgroup='fanio ';break;
	case 'M':
		$xgroup='fanio,fmes ';
		$sql_enti ='select * from enti00 where concat(fanio,fmes) = "'.$idnum.'"  order by fecha desc, docu, ptonumero desc' ;
		$sql_entit='select *, sum(tdcto) as ttdcto, sum(tice) as ttice, sum(tiva) as ttiva, sum(tirbp) as ttirbp  from enti00 where concat(fanio,fmes)= "'.$idnum.'" group by enti' ;
		break;
	case 'S':
		$xgroup='fanio,fsema ';
	    $sql_enti ='select * from enti00 where concat(fanio,fsema) = "'.$idnum.'"  order by fecha desc, docu, ptonumero desc' ;
		$sql_entit='select *, sum(tdcto) as ttdcto, sum(tice) as ttice, sum(tiva) as ttiva, sum(tirbp) as ttirbp  from enti00 where  concat(fanio,fsema) = '.$idnum.' group by enti' ;
		break;

	case 'F':
		$xgroup='fecha ';
	    $sql_enti ='select * from enti00 where fecha = "'.$idnum.'" and fecha>="'.$fini.'" and fecha<="'.$ffin.'" order by fecha desc, docu, ptonumero' ;
		$sql_entit='select *, sum(tdcto) as ttdcto, sum(tice) as ttice, sum(tiva) as ttiva, sum(tirbp) as ttirbp  from enti00 where fecha = "'.$idnum.'" group by enti' ;
		break;

	default :
	$xgroup='';
	break;
}
	
// AJUSTA ADRS DEFAULT
$sql_adrs ='select * from adrs00 where adrs_enti = "'.$idnum.'" and adrs_status!="X" order by adrs_defa desc, adrs_codigo' ;
$res_adrs=mysql_query($sql_adrs,$link_vcrx);

// 	echo $sql_adrs;
if (mysql_num_rows($res_adrs)!=0){
$xdescri=mysql_result($res_adrs,0,'enti_descri');
$xcodefa=mysql_result($res_adrs,0,'adrs_codigo');
}
$sql_adrs ='update adrs0 set adrs_defa="1" where adrs_codigo="'.$xcodefa.'"' ;
$res_adrs=mysql_query($sql_adrs,$link_vcrx);
	
$sql_enti ='update enti0 set enti_adrs="'.$xcodefa.'" where enti_codigo="'.$idnum.'"' ;
$res_enti=mysql_query($sql_enti,$link_vcrx);
	
$sql_adrs ='select * from adrs00 where adrs_enti = "'.$idnum.'" and adrs_status!="X" order by adrs_defa desc, adrs_codigo' ;
$res_adrs=mysql_query($sql_adrs,$link_vcrx);
// echo $sql_adrs;	
?>

<div style="overflow-y:scroll; height:250px; font-size:12px;">
<?php 
echo '<b>'.$idnum.' - '.$xdescri.'</b>';
?>
<table id="tabla_entil1"  border="0" width="100%" cellpadding="2" cellspacing="0" style="border-collapse:collapse">
<tr align="center" style="background: #F7F7F7; border-top:solid 1px#000;border-bottom:solid 1px#000">
	<th></th>
 	<th align="left">e-mail</th>
 	<th align="left">CALLE1</th>  
 	<th align="left">CALLE2</th>
 	<th align="center">No</th>
 	<th align="left">Referencia</th>
 	<th align="left">Tel.</th>
 	<th align="center">Ciudad</th>
 	<th align="center"></th>
</tr>
<?php 
$filax=0; 

while($r_adrs=mysql_fetch_assoc($res_adrs))
{ ?>

<tr valign="center" style=" <?php if($filax!=3){$filax++;}else{echo 'border-bottom:dotted 1px #000000;'; $filax=0;}?> ">
  	<td align="center">
		<a href="lbladrs0.php?ci=<?php echo $r_adrs['adrs_codigo']; ?>" target="_blank">
		<img src="../img/print2.png" width="20"  title="Imprimir  <?php echo $r_adrs['adrs_enti'];?>"  /></a>
    </td>

  <td align="left"><?php echo $r_adrs['adrs_email']; ?></td>
  <td align="left"><?php echo $r_adrs['adrs_calle1'].' ***'.$r_adrs['adrs_direccion']; ?></td>
  <td align="left"><?php echo $r_adrs['adrs_calle2']; ?></td>
  <td align="center"><?php echo $r_adrs['adrs_callen']; ?></td>
  <td align="left"><?php echo $r_adrs['adrs_calle3']; ?></td>
  <td align="left"><?php echo $r_adrs['adrs_tlfn']; ?></td>
  <td align="center"><?php echo $r_adrs['cdad_descri']; ?></td>
  <td align="center"><?php echo $r_adrs['adrs_defa']; ?></td>
</tr>

<?php }?>

</table>
<hr/>

<div>

<?php }?>