<?php 
session_start();

require('../cfg/res.php');
require('../cfg/cfg.php');

include('php_gene1.php');


$xacceso=0;
$sql_acce='select * from usrdoc1 where ((docu like "RP%" and info="1")or(docu like "RC%" and info="1")) and user="'.$_SESSION['user_vcrx_id'].'" ';
$res_acce=mysql_query($sql_acce,$link_vcrx);
if (mysql_num_rows($res_acce)!=0){$xacceso=1;}
if (mb_stristr('sist.supe',$_SESSION['user_vcrx_tipo'])){$xacceso=1;}
if ($xacceso==0){echo '<font size="8" color="red"><b>';die('No tiene acceso a Informe de Retenciones ' );}	


$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);



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

if($xfrepo=='P'){$tabla='retp';}
else {$tabla='retc';}

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


if(isset($_POST['fcodi']) && !empty($_POST['fcodi']))
{
	$xfcodi=$_POST['fcodi'];
}
switch ($xfcodi)
{
	case '1': $item_codi='item';break;
	case '2': $item_codi='item_codigov';break;
	case '3': $item_codi='item_ean';break;
	default : $item_codi='item';break;
}
// echo $item_codi;


$sql_retet='select *,
	sum(tcant) as ttcant,
	sum(brta) as tbrta,
	sum(biva) as tbiva,
	sum(rrta) as trrta,
	sum(riva) as triva,
	max(piva) as tpiva,
	max(prta) as tprta,
	sum(rrta+riva) as ttotal
	from '.$tabla.'12 where ';

$sql_retet.='(';

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

$sql_retef='select min(fecha) as fecha from '.$tabla.'1 where status!="X" and status!="O" and fecha>="'.$xfecha1.'"';
$res_retef=mysql_query($sql_retef,$link_vcrx);
$xfecha1b=mysql_result($res_retef,0,'fecha');

$sql_retef='select max(fecha) as fecha from '.$tabla.'1 where status!="X" and status!="O" and fecha<="'.$xfecha2.'"';
$res_retef=mysql_query($sql_retef,$link_vcrx);
$xfecha2b=mysql_result($res_retef,0,'fecha');

	$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));

	switch ($xfgrou)
	{
	case 'M':
		$xfecha1=date('Y/m/'.'1',strtotime($xfecha1));

		if($dias<=31)
		{$xfecha1=date('Y/m/'.'1',strtotime('-2 month',strtotime($xfecha1)));}
		$xfecha2=date('Y/m/t',strtotime($xfecha2));
		break;

	case 'S':
		if($dias<=7)
		{$xfecha1=date('Y/m/d',strtotime('-1 month',strtotime($xfecha1)));}
		
		$xfecha1=date('Y/m/d',strtotime('-'.date('w',strtotime($xfecha1)).' day',strtotime($xfecha1)));
		$xfecha2=date('Y/m/d',strtotime('+'.(6-date('w',strtotime($xfecha2))).' day',strtotime($xfecha2)));
		break;
	}

	$sql_retet.='fecha BETWEEN "'.$xfecha1.'" and "'.$xfecha2.'"';
	$sql_retet.=' and docu not like "RBCO%" ';

// echo $sql_retet;
//}
//else
//{
//	$sql_retet.=' fecha BETWEEN "'.$xfecha1.'" and "'.$xfecha2.'"' ;
//}

$sql_retet.=')';
$sql_retet=str_replace(')(',')||(',$sql_retet);

if(isset($_POST['docu']) && !empty($_POST['docu']))
{
	//$sql_retet.=' and  (docu like "'.$_POST['docu'].'%" or docu_descri like "%'.$_POST['docu'].'%" ) ';
	$sql_retet.=' and  (docu like "'.$_POST['docu'].'%"  ) ';
	$xfdocu=$_POST['docu'];
}

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



if(isset($_POST['frta']) && !empty($_POST['frta'])){$xfrta=$_POST['frta'];}
if(isset($_POST['fiva']) && !empty($_POST['fiva'])){$xfiva=$_POST['fiva'];}

if ($xfrta=='true' and $xfiva=='false'){$sql_retet.=' and  (brta>0) ';}	
if ($xfrta=='false' and $xfiva=='true'){$sql_retet.=' and  (biva>0) ';}	

$sql_retet.=' and(status!="X") ';
// $sql_retet.=' and(status!="O") ';


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

switch ($xfgrou)
{
	case 'C':$xgroup='cair,puni ';$xforde='C';break;
	case 'D':$xgroup='ptoe,numero ';break;
	case 'E':$xgroup='enti_descri,enti ';break;
	default :$xgroup='';break;
}

switch ($xforde)
{
	case 'N':$xorder='ptoe,numero';break;
	case 'F':$xorder='fecha,ptoe,numero';break;
	case 'E':$xorder=$xgroup.',ptoe,numero';break;
	case 'C':$xorder='cair,puni ';break;

	default :$xorder=$xgroup;break;
}

if($xfrepo=='C')
{upl_claves2('07',$xfecha1,$xfecha2);}

//$xorder=str_replace('_codigo','',$xorder)
// explain_sql($sql_retet);
// echo '****'.$link_vcrx;
$res_retett=mysql_query($sql_retet,$link_vcrx);
if(mysql_errno($link_vcrx)!=0)
{
require('../cfg/res.php');
require('../cfg/cfg.php');
}

// echo 
if (mysql_result($res_retett,0,'ttcant')==0)
{
// 	$sql_retet=str_replace($xfecha1,'2000-01-01',$sql_retet);
// 	$res_retett=mysql_query($sql_retet,$link_vcrx);
// 	$xfecha1='2000-01-01';
}

$xttcant=mysql_result($res_retett,0,'ttcant');
$xttbrta=mysql_result($res_retett,0,'tbrta');
$xttbiva=mysql_result($res_retett,0,'tbiva');
$xttrrta=mysql_result($res_retett,0,'trrta');
$xttriva=mysql_result($res_retett,0,'triva');
$xtttotal=mysql_result($res_retett,0,'ttotal');


$sql_retet.=' group by '.$xgroup;

$sql_retet.=' order by '.$xorder;

// echo $sql_retet;	
// explain_sql($sql_retet);

$res_retet=mysql_query($sql_retet,$link_vcrx);
$totreg = mysql_num_rows($res_retet);
// 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; ">

<?php switch ($xfgrou) { ?>
<?php	case 'D': ?>
	<th></th>
	<th>PTO/NUMERO</th>
	<th>FECHA</th>
	<th>NOMBRE</th>
	<th align="right"></th>
<?php	break; ?>

<?php	case 'E': ?>
	<th></th>
	<th></th>
	<th>ENTIDAD</th>
	<th>NOMBRE</th>
	<th></th>
<?php	break; ?>

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

<?php	case 'X': ?>
	<th></th>
	<th>COD.RET.</th>
	<th>NOMBRE</th>
	<th></th>
	<th></th>
<?php } ?>

<?php if($xfrta=='true'){ ?>	
	<th align="right">B.RTA</th>
	<th align="right">%</th>
	<th align="right">R.RTA</th>
<?php } ?>

<?php if($xfiva=='true'){ ?>	
	<th align="right">B.IVA</th>
	<th align="right">%</th>
	<th align="right">R.IVA</th>
<?php } ?>

<th>TOTAL</th>

</tr>

<?php 
$filax=0;
$cregi=0;
while($r_retet=mysql_fetch_assoc($res_retet))
{
$cregi++;
$duni=2;
?>
<?php
    $enti_descri=$r_retet['enti_descri'];
    $enti_razsoc=$r_retet['enti_razsoc'];
    if($enti_descri=='' and $enti_razsoc=='')    
    {
      get_ruc0($r_retet['enti']);
      get_ced0($r_retet['enti']);
    }
?>
	
<tr class="filax" id="<?php echo $r_ped['idnum1']; ?>" valign="top" align="center" style=" <?php if($filax>=4){echo 'border-top:dotted 1px #000;';$filax=1;}else{$filax++;} ?>" >

<?php switch ($xfgrou) { ?>
<?php case 'D': ?>
<td>
    <a href="../lib/celectop1.php?docu=<?php echo $r_retet['docu']; ?>&idnum=<?php echo $r_retet['idnum1']; ?>&formato=1" target="_blank"><img src="http://vcrxol.com/_img/print2.png" width="20"  title="Imprimir  <?php echo $r_retet['docu'];?>-<?php echo $r_retet['numero'];?>"  /></a>
</td>
<!--   <td align="center"><?php //echo'<span title="'.$r_retet['docu'].'">'.$r_retet['docu'].'</span>';?></td> -->
	<td align="center"><a href="#" onClick="retet1d('<?php echo $xfgrou; ?>','<?php echo $r_retet['idnum1']; ?>',this);"><?php echo '<span title="'.$r_retet['ptonumero'].'">'.$r_retet['ptonumero'].'</span>';?></a> </td>
	<td align="center"><?php echo'<span title="'.$r_retet['fechadmy'].'">'.$r_retet['fechadmy'].'</span>';?></td>
	<td align="left"><?php echo'<span title="'.$r_retet['enti'].'">'.$r_retet['enti_descri'].'</span>';?></td>
	<td align="right"><?php //echo'<span title="'.$r_retet['cair'].'">'. number_format($r_retet['puni'],2).'</span>';?></td>
<?php	break; ?>

<?php	case 'E': ?>
	<td>
    	<a href="../lib/impr_retet1d.php?grupo=<?php echo $xfgrou; ?>&idnum=<?php echo $r_retet['enti']; ?>&fecha_ini=<?php echo $xfecha1; ?>&fecha_fin=<?php echo $xfecha2; ?>" target="_blank"><img src="http://vcrxol.com/_img/print2.png" width="20"  title="Imprimir  <?php echo $r_retet['enti'];?>"  /></a>
	</td>
  <td align="center"><?php echo'<span title="'.$r_retet['enti_zsub'].'">'.$r_retet['zsub_descri'].'</span>';?></td>
	<td align="left"><a href="#" onClick="retet1d('<?php echo $xfgrou; ?>','<?php echo $r_retet['enti']; ?>',this);"><?php echo '<span title="'.$r_retet['enti'].'">'.$r_retet['enti'].'</span>';?></a> </td>
	<td align="left"><?php echo'<span title="'.$r_retet['enti_direccion'].'">'.$r_retet['enti_descri'].'</span>';?></td>
	<td></td>

<?php	break; ?>

<?php	case 'C': ?>
	<td>
    	<a href="../lib/impr_retet1d.php?grupo=<?php echo $xfgrou; ?>&idnum=<?php echo $r_retet['item']; ?>&fecha_ini=<?php echo $xfecha1; ?>&fecha_fin=<?php echo $xfecha2; ?>" target="_blank"><img src="http://vcrxol.com/_img/print2.png" width="20"  title="Imprimir  <?php echo $r_retet['item'];?>"  /></a>
	</td>
	<td align="center"><a href="#" onClick="retet1d('<?php echo $xfgrou; ?>','<?php echo $r_retet['cair']; ?>',this);"><?php echo '<span title="'.$r_retet['item'].'">'.$r_retet['cair'].'</span>';?></a> </td>
  <td align="left"><?php echo'<span title="'.$r_retet['cair_descri'].'">'.$r_retet['cair_descri'].'</span>';?></td>
	<td align="left"><?php echo'<span title="'.$r_retet['item_mrca'].'">'.$r_retet['item_descri'].'</span>';?></td>
	<td></td>


<?php	break; ?>

<?php	case 'X': ?>
	<td><input src="http://vcrxol.com/_img/print2.png" title="Imprimir" onclick="imprimirlista('<?php echo $r_retet['item'];?>','<?php echo $r_retet['item_descri'];?>');" type="image" width="20"></td>
        <td align="center"><?php echo'<span title="'.$r_retet['item_at10'].'">'.$r_retet['item_at10'].'</span>';?></td>
	<td align="left"><a href="#" onClick="retet1d('<?php echo $xfgrou; ?>','<?php echo $r_retet['item_at10']; ?>',this);"><?php echo '<span title="'.$r_retet['at10_descri'].'">'.$r_retet['at10_descri'].'</span>';?></a> </td>
	<td align="left"></td>
	<td></td>


<?php }?>



<?php if($xfrta=='true'){ ?>	
   	<td align="right"><?php echo number_format($r_retet['tbrta'],2);?></td>
   	<td align="right"><i><?php echo number_format($r_retet['tprta'],2);?></td>
   	<td align="right"><?php echo number_format($r_retet['trrta'],2);?></td>
<?php } ?>

<?php if($xfiva=='true'){ ?>	
   	<td align="right"><?php echo number_format($r_retet['tbiva'],2);?></td>
   	<td align="right"><i><?php echo number_format($r_retet['tpiva'],2);?></td>
   	<td align="right"><?php echo number_format($r_retet['triva'],2);?></td>
<?php } ?>

  <td align="right"><?php echo '<b>'.number_format($r_retet['ttotal'],2);?></td>

  <td>
  <?php if($xfrepo=='C'){ ?>  
  <?php $pidtxt='hshe_'.$r_retet['idnum1']; $pcaption=''; $pdefa=$r_retet['hshe']; $ptabla='retc1'; $pline=$r_retet['idnum1']; $pcolu='hshe'; include('vrchkbox.php'); ?>	
  <?php } ?>  
  </td>

</tr>
<?php }?>


<tr align="center" style="background: #F7F7F7; border-top:solid 1px#000;border-bottom:solid 1px#000">
	<th></th>
	<th></th>
	<th><?php echo $xfgrou.' : '.$cregi; ?></th>
	<th>TOTALES</th>
	<th></th>
<?php if($xfrta=='true'){ ?>	
   	<td align="right"><?php echo number_format($xttbrta,2);?></td>
   	<td align="right"></td>
   	<td align="right"><?php echo number_format($xttrrta,2);?></td>
<?php } ?>

<?php if($xfiva=='true'){ ?>	
   	<td align="right"><?php echo number_format($xttbiva,2);?></td>
   	<td align="right"></td>
   	<td align="right"><?php echo number_format($xttriva,2);?></td>
<?php } ?>

<th align="right"><?php echo number_format($xtttotal,2);?></th>



	</tr>

<tr align="center" style="display:none;background: #F7F7F7; border-top:solid 1px#000;border-bottom:solid 1px#000">
	<th></th>
	<th></th>
	<th></th>
	<th>PROMEDIOS</th>
	<th></th>
	<th align="right"><?php echo number_format(mysql_result($res_retett,0,'ttsubt')/$totreg,2);?></th>

<?php if($xttdcto!=0){ ?>	
	<th align="right"><?php echo '<i>'.number_format(mysql_result($res_retett,0,'ttdcto')/$totreg,2);?></th>
<?php } ?>

<?php if ($xttice!=0){ ?>	
	<th align="right"><?php echo number_format(mysql_result($res_retett,0,'ttice')/$totreg,2);?></th>
<?php } ?>

	<th align="right"><?php echo number_format(mysql_result($res_retett,0,'ttsubtiva0')/$totreg,2);?></th>

<?php if ($xttiva!=0){ ?>	
	<th align="right"><?php echo number_format(mysql_result($res_retett,0,'ttsubtiva')/$totreg,2);?></th>
	<th align="right"><?php echo number_format(mysql_result($res_retett,0,'ttiva')/$totreg,2);?></th>
<?php } ?>

<?php if ($xttirbp!=0){ ?>	
	<th align="right"><?php echo number_format(mysql_result($res_retett,0,'ttirbp')/$totreg,2);?></th>
<?php } ?>

	<th align="right"><?php echo number_format(mysql_result($res_retett,0,'ttotal')/$totreg,2);?></th>


	</tr>


</table>
</div>