<?php
function vrretet1d($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');
	}

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

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



?>


<?php
switch ($grupo)
{
	case 'C':
	    $sql_enti ='select * from rete12 where cair='.$idnum.' and fecha>="'.$fini.'" and fecha<="'.$ffin.'" order by enti' ;
		$sql_entit='select *,sum(tcant) as ttcant, sum(tcant) as ttcant, sum(tcant) as ttcant, sum(tcant) as ttcant from rete11 where cair='.$idnum.' ' ;
		break;

	case 'D':
	  $sql_enti ='select * from rete12 where idnum1='.$idnum.' and fecha>="'.$fini.'" and fecha<="'.$ffin.'" order by cair' ;
		$sql_entit='select *,sum(tcant) as ttcant, sum(tcant) as ttcant, sum(tcant) as ttcant, sum(tcant) as ttcant from rete11 where idnum1='.$idnum.' ' ;
		break;

	case 'E':
	    $sql_enti ='select * from rete11 where enti like "'.$idnum.'%" and fecha>="'.$fini.'" and fecha<="'.$ffin.'" order by fecha desc, docu, ptonumero' ;
		$sql_entit='select *, sum(tcant) as ttcant, sum(tcant) as ttcant, sum(tcant) as ttcant, sum(tcant) as ttcant  from rete11 where enti like "'.$idnum.'%" group by enti' ;
		break;

	case 'I':
		$xgroup='item_lsub,item_descri ';
	    $sql_enti ='select * from rete12 where item like "'.$idnum.'" and fecha>="'.$fini.'" and fecha<="'.$ffin.'" order by fecha desc, docu, ptonumero' ;
		$sql_entit='select *, sum(tcant) as ttcant, sum(tcant) as ttcant, sum(tcant) as ttcant, sum(tcant) as ttcant  from rete12 where item like "'.$idnum.'%" group by enti' ;
		break;

	case 'P':
		$xgroup='ptoe ';
	  $sql_enti ='select * from rete11 where ptoe like "'.$idnum.'%" and fecha>="'.$fini.'" and fecha<="'.$ffin.'" order by fecha desc, docu, ptonumero' ;
		$sql_entit='select *, sum(tcant) as ttcant, sum(tcant) as ttcant, sum(tcant) as ttcant, sum(tcant) as ttcant  from rete11 where ptoe like "'.$idnum.'%" group by enti' ;
		break;
	case 'V':
		$xgroup='vndd ';
	    $sql_enti ='select * from rete11 where vndd like "'.$idnum.'%" and fecha>="'.$fini.'" and fecha<="'.$ffin.'" order by fecha desc, docu, ptonumero' ;
		$sql_entit='select *, sum(tcant) as ttcant, sum(tcant) as ttcant, sum(tcant) as ttcant, sum(tcant) as ttcant  from rete11 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 rete11 where concat(fanio,fmes) = "'.$idnum.'"  order by fecha desc, docu, ptonumero desc' ;
		$sql_entit='select *, sum(tcant) as ttcant, sum(tcant) as ttcant, sum(tcant) as ttcant, sum(tcant) as ttcant  from rete11 where concat(fanio,fmes)= "'.$idnum.'" group by enti' ;
		break;
	case 'S':
		$xgroup='fanio,fsema ';
	    $sql_enti ='select * from rete11 where concat(fanio,fsema) = "'.$idnum.'"  order by fecha desc, docu, ptonumero desc' ;
		$sql_entit='select *, sum(tcant) as ttcant, sum(tcant) as ttcant, sum(tcant) as ttcant, sum(tcant) as ttcant  from rete11 where  concat(fanio,fsema) = '.$idnum.' group by enti' ;
		break;

	case 'F':
		$xgroup='fecha ';
	    $sql_enti ='select * from rete11 where fecha = "'.$idnum.'" and fecha>="'.$fini.'" and fecha<="'.$ffin.'" order by fecha desc, docu, ptonumero' ;
		$sql_entit='select *, sum(tcant) as ttcant, sum(tcant) as ttcant, sum(tcant) as ttcant, sum(tcant) as ttcant  from rete11 where fecha = "'.$idnum.'" group by enti' ;
		break;

	default :
	$xgroup='';
	break;
}

if($xfrepo=='P'){$tabla='retp';}
else {$tabla='retc';}
	
$sql_enti=str_replace('rete1',$tabla.'1',$sql_enti);
$sql_entit=str_replace('rete1',$tabla.'1',$sql_entit);
	
$res_entit=mysql_query($sql_entit,$link_vcrx);
$res_enti=mysql_query($sql_enti,$link_vcrx);

// echo $sql_enti;
	
?>




<?php

if(mysql_num_rows($res_entit)!=0)
{
$xtcant=mysql_result($res_entit,0,'ttcant');
$xtcant =mysql_result($res_entit,0,'ttcant');
$xtcant =mysql_result($res_entit,0,'ttcant');
$xtcant=mysql_result($res_entit,0,'ttcant');
}
// 	echo $sql_entit;
?>



<div style="overflow-y:scroll; height:250px; font-size:12px;">



<table id="tabla_retet1"  border="0" width="90%" cellpadding="2" cellspacing="0" style="border-collapse:collapse">

<tr align="center" style="background: #F7F7F7; border-top:solid 1px#000;border-bottom:solid 1px#000">

<?php
switch ($grupo)
{
	case 'D':
?>
	<th>CAIR</th>
	<th>DESCRI</th>
<!-- 	<th>CANT.</th>   -->
<!-- 	<th>P.UNIT.</th> -->

<?php	
	break;
	case 'C':
?>
	<th></th>
	<th>DOCU</th>
	<th>PTO/NUMERO</th>
	<th>FECHA</th>
<th>ENTIDAD</th>


<?php	
	break;
	case 'E':
?>
	<th></th>
	<th>DOCU</th>
	<th>PTO/NUMERO</th>
	<th>FECHA</th>
	<th>CAIR</th>


<?php	
	break;
	case 'I':
?>
	<th></th>
	<th>NOMBRE</th>
	<th>DOCU</th>
	<th>PTO/NUMERO</th>
	<th>FECHA</th>
	<th>CANT</th>
	<th>P.UNIT</th>


<?php	
	break;
	case 'M':
?>
	<th></th>
	<th>NOMBRE</th>
	<th>DOCU</th>
	<th>PTO/NUMERO</th>
	<th>FECHA</th>
<?php	
	break;
	case 'S':
?>
	<th></th>
	<th>NOMBRE</th>
	<th>DOCU</th>
	<th>PTO/NUMERO</th>
	<th>FECHA</th>
<?php	
	break;
	case 'F':
?>
	<th></th>
	<th>ANIO</th>
	<th>MES</th>
	<th>FECHA</th>
	<th></th>

<?php	
	break;
	case 'P':
?>
	<th></th>
	<th>DOCU</th>
	<th>PTO/NUMERO</th>
	<th>FECHA</th>
	<th>CLIENTE</th>


<?php	
	break;
	case 'V':
?>
	<th></th>
	<th>DOCU</th>
	<th>PTO/NUMERO</th>
	<th>FECHA</th>
	<th>CLIENTE</th>


<?php	
	break;
	case 'Z':
?>
	<th></th>
	<th>ZONA</th>
	<th>NOMBRE</th>
	<th></th>
	<th></th>

<?php	
	break;
	case 'L':
?>
	<th></th>
	<th>LINEA</th>
	<th>NOMBRE</th>
	<th></th>
	<th></th>

<?php	
	break;
	case 'R':
?>
	<th></th>
	<th>COD.RET.</th>
	<th>NOMBRE</th>
	<th></th>
	<th></th>

<?php	
	break;
	default:
?>
	<th></th>
	<th>DOCU</th>
	<th>PTO/NUMERO</th>
	<th>FECHA</th>
	
<?php }; ?>


  <th>BASE</th>
  <th>%</th>


  <th>TOTAL</th>
  <th>.</th>
  <th>APLICA</th>
  <th>DETALLE</th>
</tr>
<?php 
$filax=0; 

  
  
while($r_enti=mysql_fetch_assoc($res_enti))
{ ?>

<tr valign="top" style=" <?php if($filax!=3){$filax++;}else{echo 'border-bottom:dotted 1px #000000;'; $filax=0;}?> ">



<?php
switch ($grupo)
{
	case 'D':
?>
  <td align="center"><?php echo $r_enti['cair']; ?></td>
  <td align="left"><?php echo $r_enti['cair_descri']; ?></td>
<!--   <td align="right"><?php echo number_format($r_enti['cant'],$r_enti['duni']) ?></td> -->
<!--   <td align="right"><?php echo number_format($r_enti['puni'],2) ?></td> -->

<?php	
	break;
	case 'C':
?>
  <td>
    <a href="../lib/celectop1.php?docu=<?php echo $r_enti['docu']; ?>&idnum=<?php echo $r_enti['idnum1']; ?>&formato=1" target="_blank">
		<img src="../img/print2.png" width="20"  title="Imprimir  <?php echo $r_enti['docu'];?>-<?php echo $r_enti['numero'];?>"  /></a>
	</td>
  <td align="center"><?php echo $r_enti['docu']; ?></td>
  <td align="center"><?php echo $r_enti['ptonumero']; ?></td>
  <td align="center"><?php echo $r_enti['fechadmy']; ?></td>
  <td align="left"><?php echo $r_enti['enti_descri']; ?></td>

<?php	
	break;
	case 'E':
?>
  <td>
    <a href="../lib/celectop1.php?docu=<?php echo $r_enti['docu']; ?>&idnum=<?php echo $r_enti['idnum1']; ?>&formato=1" target="_blank">
		<img src="../img/print2.png" width="20"  title="Imprimir  <?php echo $r_enti['docu'];?>-<?php echo $r_enti['numero'];?>"  /></a>
	</td>
  <td align="center"><?php echo $r_enti['docu']; ?></td>
  <td align="center"><?php echo $r_enti['ptonumero']; ?></td>
  <td align="center"><?php echo $r_enti['fechadmy']; ?></td>
  <td align="center"><?php echo $r_enti['cair']; ?></td>

<?php	
	break;
	case 'P':
?>
  <td>
    <a href="../lib/celectop1.php?docu=<?php echo $r_enti['docu']; ?>&idnum=<?php echo $r_enti['idnum1']; ?>&formato=1" target="_blank">
		<img src="../img/print2.png" width="20"  title="Imprimir  <?php echo $r_enti['docu'];?>-<?php echo $r_enti['numero'];?>"  /></a>
	</td>
  <td align="center"><?php echo $r_enti['docu']; ?></td>
  <td align="center"><?php echo $r_enti['ptonumero']; ?></td>
  <td align="center"><?php echo $r_enti['fechadmy']; ?></td>
  <td align="left"><?php echo $r_enti['enti_descri']; ?></td>

<?php	
	break;
	case 'V':
?>
  <td>
    <a href="../lib/celectop1.php?docu=<?php echo $r_enti['docu']; ?>&idnum=<?php echo $r_enti['idnum1']; ?>&formato=1" target="_blank">
		<img src="../img/print2.png" width="20"  title="Imprimir  <?php echo $r_enti['docu'];?>-<?php echo $r_enti['numero'];?>"  /></a>
	</td>
  <td align="center"><?php echo $r_enti['docu']; ?></td>
  <td align="center"><?php echo $r_enti['ptonumero']; ?></td>
  <td align="center"><?php echo $r_enti['fechadmy']; ?></td>
  <td align="left"><?php echo $r_enti['enti_descri']; ?></td>

<?php	
	break;
	case 'I':
?>
	<td>
    <a href="../lib/celectop1.php?docu=<?php echo $r_enti['docu']; ?>&idnum=<?php echo $r_enti['idnum1']; ?>&formato=1" target="_blank">
		<img src="../img/print2.png" width="20"  title="Imprimir  <?php echo $r_enti['docu'];?>-<?php echo $r_enti['numero'];?>"  /></a>
	</td>
  <td align="left"><?php echo $r_enti['enti_descri']; ?></td>
  <td align="center"><?php echo $r_enti['docu']; ?></td>
  <td align="center"><?php echo $r_enti['ptonumero']; ?></td>
  <td align="center"><?php echo $r_enti['fechadmy']; ?></td>
  <td align="right"><?php echo $r_enti['cant']; ?></td>
  <td align="right"><?php echo $r_enti['puni']; ?></td>

<?php	
	break;
	case 'M':
?>
	<td>
    <a href="../lib/celectop1.php?docu=<?php echo $r_enti['docu']; ?>&idnum=<?php echo $r_enti['idnum1']; ?>&formato=1" target="_blank">
		<img src="../img/print2.png" width="20"  title="Imprimir  <?php echo $r_enti['docu'];?>-<?php echo $r_enti['numero'];?>"  /></a>
	</td>
  <td align="left"><?php echo $r_enti['enti_descri']; ?></td>
  <td align="center"><?php echo $r_enti['docu']; ?></td>
  <td align="center"><?php echo $r_enti['ptonumero']; ?></td>
  <td align="center"><?php echo $r_enti['fechadmy']; ?></td>

<?php	
	break;
	case 'S':
?>
	<td>
    <a href="../lib/celectop1.php?docu=<?php echo $r_enti['docu']; ?>&idnum=<?php echo $r_enti['idnum1']; ?>&formato=1" target="_blank">
		<img src="../img/print2.png" width="20"  title="Imprimir  <?php echo $r_enti['docu'];?>-<?php echo $r_enti['numero'];?>"  /></a>
	</td>
  <td align="left"><?php echo $r_enti['enti_descri']; ?></td>
  <td align="center"><?php echo $r_enti['docu']; ?></td>
  <td align="center"><?php echo $r_enti['ptonumero']; ?></td>
  <td align="center"><?php echo $r_enti['fechadmy']; ?></td>

<?php	
	break;
	default:
?>
  <td></td>
  <td align="center"><?php echo $r_enti['docu']; ?></td>
  <td align="center"><?php echo $r_enti['ptonumero']; ?></td>
  <td align="center"><?php echo $r_enti['fechadmy']; ?></td>

<?php } ?>






  <td align="right"><?php echo number_format($r_enti['cant'],2) ?></td>
  <td align="center"><?php echo number_format($r_enti['puni'],2) ?></td>


  <td align="right"><?php echo '<b>'.number_format(abs($r_enti['total']),2) ?></td>
  <td></td>

  <td align="left"><?php echo $r_enti['docum'].' '.$r_enti['ptonumer']; ?></td>
  <td align="left"><?php echo $r_enti['detalle']; ?></td>
</tr>

<?php }?>

</table>
<hr/>

<div>

<?php }?>