<?php session_start();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<?php
require('../cfg/res.php');	
include('php_gene1.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);


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

$sql_retet='select *,
	sum(tcant) as ttcant,
	sum(tsubt) as ttsubt,
	sum(brta) as sbrta,
	max(prta) as mprta,
	sum(rrta) as srrta,
	sum(biva) as sbiva,
	max(piva) as mpiva,
	sum(riva) as sriva,
	sum(rrta+riva) as ttotal ';

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

$sql_retet.=' from '.$tabla.'12 where ';	

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

$xfcodi='1';
if (mb_stristr('elco',substr($_SESSION['user_vcrx_empr'],0,4)))
{$xfcodi='2';}


if(isset($_GET['fcodi']) && !empty($_GET['fcodi']))
{
	$xfcodi=$_GET['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;
}


$sql_retet.='(';

if(isset($_GET['fecha_ini']) && !empty($_GET['fecha_ini']) && isset($_GET['fecha_fin']) && !empty($_GET['fecha_fin']))
{
	$xfecha1=$_GET['fecha_ini'];
	$xfecha2=$_GET['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)));}

		//if($dias>360)
		//{$xfecha1=date('Y/'.'1/1',strtotime($xfecha1));}

		//if($anios!=0)
		//{$xfecha1=date('Y/'.'1/1',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 fanio BETWEEN "'.$xanio1.'" and "'.$xanio2.'"';

	$sql_retet.=' and docu not like "RBCO%" ';


$sql_retet.=')';


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

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



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

if(isset($_GET['fitem']) && !empty($_GET['fitem']))
{
	$sql_retet.=' and  (item like "'.$_GET['fitem'].'%" or item_descri like "%'.$_GET['fitem'].'%" ) ';
	$xfitem=$_GET['fitem'];
}



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


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

switch ($xfgrou)
{
	case 'D':$xgroup='docu,ptoe,numero,idnum1 ';break;
	case 'E':$xgroup='enti_zsub,enti_descri,enti ';break;
	case 'I':$xgroup='item_lsub,item_descri,item  ';break;

	case 'P':$xgroup='ptoe ';break;
	case 'V':$xgroup='vndd ';break;
	case 'Z':$xgroup='zsub ';break;
	case 'L':$xgroup='item_lsub ';break;
	case 'R':$xgroup='item_at10 ';break;
	case 'C':$xgroup='cair,puni ';break;

	case 'A':$xgroup='fanio ';break;
	case 'M':$xgroup='fanio,fmes ';break;
	case 'S':$xgroup='fanio,fsema ';break;
	case 'F':$xgroup='fecha ';break;

	default :$xgroup='';break;
}



switch ($xforde)
{
	case 'D':$xorder=$xgroup;break;
	case 'C':$xorder=str_replace('_descri','',$xgroup);
			$xorder=str_replace(',item',','.$item_codi.',item_descri,item',$xorder);break;
	case 'T':$xorder='ttotal desc, '.str_replace('_descri','',$xgroup);break;

	default :$xorder=$xgroup;break;
}

//$xorder=str_replace('_codigo','',$xorder)

// echo $sql_retet;
$res_retett=mysql_query($sql_retet,$link_vcrx);
// $xttdcto=mysql_result($res_retett,0,'ttdcto');
// $xttice=mysql_result($res_retett,0,'ttice');
// $xttiva=mysql_result($res_retett,0,'ttiva');
// $xttirbp=mysql_result($res_retett,0,'ttirbp');

$xtbrta=mysql_result($res_retett,0,'sbrta');




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

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

$res_retet=mysql_query($sql_retet,$link_vcrx);
$totreg = mysql_num_rows($res_retet);

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


switch ($xfgrou)
{
	case 'D':$xgroup='DOCUMENTO ';break;
	case 'E':$xgroup='ZONA / CLIENTE ';break;
	case 'I':$xgroup='LINEA / ITEM';break;

	case 'P':$xgroup='PTO.EMISION ';break;
	case 'V':$xgroup='VENDEDOR ';break;
	case 'Z':$xgroup='ZONAS ';break;
	case 'L':$xgroup='LINEAS ';break;
	case 'R':$xgroup='COD.RET. ';break;
	case 'C':$xgroup='CODIGO RETENCION ';break;

	case 'A':$xgroup='ANIO ';break;
	case 'M':$xgroup='MESES ';break;
	case 'S':$xgroup='SEMANAS';break;
	case 'F':$xgroup='FECHA ';break;

	default :$xgroup='';break;
}



switch ($xforde)
{
	case 'D':$xorder='NOMBRE ';break;
	case 'C':$xorder='CODIGO ';break;
	case 'T':$xorder='TOTALES';break;

	default :$xorder='';break;
}





?>


<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>Retenciones Totales </title>
<style type="text/css">
body{ font-family:"Courier New", Courier, monospace;}
H1.SaltoDePagina{PAGE-BREAK-BEFORE: always}

</style>





</head>
<body>

<div align="center">
<div align="center" style="width:1100px; font-size:12px;">
<div align="center" style="font-size:18px; border:#000 solid 1px; background: #F7F7F7; margin:2px; font-weight: bold;">RETENCIONES TOTALES 
  <?php if($xfrepo=='P'){echo ' (PROVEEDORES)';} else{echo ' (CLIENTES)';} ?></div>

<table width="100%" cellspacing="0" cellpadding="2">
<tr valign="top">
<td width="50%">

<table border="1" width="100%" cellpadding="2" cellspacing="0" style="border-collapse:collapse">
<tr>
	<th width="30%" style="background: #F7F7F7;">PARAMETROS</th>
	<th></th>
</tr>

<tr>
    	<td style="font-weight:bolder;">FECHA</td>
	<td><?php echo date_format(date_create($xfecha1),'d/M/Y').' - '.date_format(date_create($xfecha2),'d/M/Y');?></td>
</tr>

<?php	
if(isset($_GET['fenti']) && !empty($_GET['fenti']))
{
$sql_descri='select * from enti0 where enti_codigo like "'.$_GET['fenti'].'%"';
$res_descri=mysql_query($sql_descri,$link_vcrx);
$enti_descri=mysql_result($res_descri,0,'enti_descri');	
?>
	<tr>
    	<td style="font-weight:bolder;">CLIENTE</td>
    	<td><?php echo $_GET['fenti'].'-'.$enti_descri;?></td>
    </tr>
<?php	
}
?>
	
<?php	
if(isset($_GET['fitem']) && !empty($_GET['fitem']))
{
$sql_descri='select * from item0 where item_codigo like "'.$_GET['fitem'].'%"';
$res_descri=mysql_query($sql_descri,$link_vcrx);
$item_descri=mysql_result($res_descri,0,'item_descri');	
?>
	<tr>
    	<td style="font-weight:bolder;">ITEM</td>
    	<td><?php echo $_GET['fitem'].'-'.$item_descri;?></td>
    </tr>
<?php	
}
?>
	
	<tr>
    	<td style="font-weight:bolder;">AGRUPADO X</td>
    	<td><?php echo $xgroup;?></td>
    </tr>

	<tr>
    	<td style="font-weight:bolder;">ORDENADO X</td>
    	<td><?php echo $xorder;?></td>
    </tr>



</table>




<td>
<!----++++++++++------>
<table border="1" width="100%" cellspacing="0" cellpadding="2" style="border-collapse:collapse">
<tr>
	<th width="30%"  style="background: #F7F7F7;">I N F O</th>
	<th></th>    
</tr>

<?php
$sql_empr='select * from empr0';
$res_empr=mysql_query($sql_empr,$link_vcrx);
?>
<tr>
	<td align="right">EMPRESA:</td>
	<td><?php echo mysql_result($res_empr,0,'empr_descri');?></td>
</tr>

<tr>
	<td align="right">USUARIO:</td>
	<td><?php echo $_SESSION['user_vcrx_id'].' / '.$_SESSION['user_vcrx_descri'];?></td>
</tr>
<tr>
	<td align="right">FECHA / HORA :</td>
	<td><?php echo date('d/M/Y H:m:s');?></td>
</tr>
</table>
<!----++++++++++------>
</td>




</tr>
</table>
<!----++++++++++------>


<hr/>

<table 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">
<?php
switch ($xfgrou)
{
	case 'D':
?>

	<th>DOCU</th>
	<th>PTO/NUMERO</th>
	<th>FECHA</th>
	<th>NOMBRE</th>
	<th></th>


<?php	
	break;
	case 'C':
?>

	<th>CODIGO</th>
	<th>NOMBRE</th>
	<th></th>
	<th></th>
	<th></th>


<?php	
	break;
	case 'E':
?>

	<th></th>
	<th>ZONA</th>
	<th>CLIENTE</th>
	<th>NOMBRE</th>
	<th></th>


<?php	
	break;
	case 'I':
?>
	<th></th>
	<th>LINEA</th>
	<th>ITEM</th>
	<th>NOMBRE</th>
	<th>CANT</th>


<?php	
	break;
	case 'M':
?>
	<th></th>
	<th>ANIO</th>
	<th>MES</th>
	<th>NOMBRE</th>
	<th></th>
<?php	
	break;
	case 'S':
?>
	<th></th>
	<th>ANIO</th>
	<th>SEMA</th>
	<th></th>
	<th></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>PTO/EMI</th>
	<th></th>
	<th></th>
	<th></th>

<?php	
	break;
	case 'V':
?>
	<th></th>
	<th>VNDD</th>
	<th>NOMBRE</th>
	<th></th>
	<th></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 } ?>

	<th>B.RTA</th>
	<th>%</th>
	<th>R.RTA</th>

	<th>B.IVA</th>
	<th>%</th>
	<th>R.IVA</th>


	<th>TOTAL</th>



</tr>




</tr>

<?php 
$filax=0;
//$urcdd='';
//$res_retet=mysql_query($sql_retet,$link_vcrx);

while($r_retet=mysql_fetch_assoc($res_retet))
{
$duni=$r_retet['item_duni'];
?>

<tr class="filax" id="<?php echo $r_ped['idnum1']; ?>" align="center" style=" <?php if($filax>=4){echo 'border-top:dotted 1px #000;';$filax=1;}else{$filax++;} ?>" >

<?php
switch ($xfgrou)
{
	case 'D':
?>

        <td align="center"><?php echo'<span title="'.$r_retet['docu'].'">'.$r_retet['docu'].'</span>';?></td>
	<td align="center"><?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></td>


<?php	
	break;
	case 'C':
?>

        <td align="center"><?php echo'<span title="'.$r_retet['cair'].'">'.$r_retet['cair'].'</span>';?></td>
	<td align="left"><?php echo '<span title="'.$r_retet['cair_descri'].'">'.$r_retet['cair_descri'].'</span>';?></a> </td>
	<td></td>
	<td></td>
	<td></td>


<?php	
	break;
	case 'E':
?>
	<td></td>
        <td align="center"><?php echo'<span title="'.$r_retet['enti_zsub'].'">'.$r_retet['zsub_descri'].'</span>';?></td>
	<td align="left"><?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;
	case 'I':
?>
	<td></td>
        <td align="center"><?php echo'<span title="'.$r_retet['item_lsub'].'">'.$r_retet['lsub_descri'].'</span>';?></td>
	<td align="left"><?php echo '<span title="'.$r_retet['item'].'">'.$r_retet[$item_codi].'</span>';?></a> </td>
	<td align="left"><?php echo'<span title="'.$r_retet['item_mrca'].'">'.$r_retet['item_descri'].'</span>';?></td>
   	<td align="right"><?php echo number_format($r_retet['ttcant'],$duni);?></td>


<?php	
	break;
	case 'M':
?>
	<td></td>
        <td align="center"><?php echo'<span title="'.$r_retet['fanio'].'">'.$r_retet['fanio'].'</span>';?></td>
	<td align="center"><?php echo '<span title="'.$r_retet['fmes'].'">'.$r_retet['fmes'].'</span>';?></a> </td>
	<td align="center"><?php echo'<span title="'.$r_retet['fmesn'].'">'.$r_retet['fmesn'].'</span>';?></td>
	<td></td>

<?php	
	break;
	case 'S':
?>
	<td></td>
        <td align="center"><?php echo'<span title="'.$r_retet['fanio'].'">'.$r_retet['fanio'].'</span>';?></td>
	<td align="center"><?php echo '<span title="'.$r_retet['fsema'].'">'.$r_retet['fsema'].'</span>';?></a> </td>
	<td align="left"></td>
	<td></td>

<?php	
	break;
	case 'F':
?>
	<td></td>
        <td align="center"><?php echo'<span title="'.$r_retet['fanio'].'">'.$r_retet['fanio'].'</span>';?></td>
	<td align="center"><?php echo'<span title="'.$r_retet['fmesn'].'">'.$r_retet['fmesn'].'</span>';?></td>
	<td align="center"><?php echo '<span title="'.$r_retet['fechadmy'].'">'.$r_retet['fdian'].'-'.$r_retet['fdia'].'</span>';?></a> </td>
	<td></td>

<?php	
	break;
	case 'P':
?>
	<td></td>
	<td align="left"><?php echo '<span title="'.$r_retet['ptoe'].'">'.$r_retet['ptoe'].'</span>';?></a> </td>
	<td align="left"></td>
	<td align="left"></td>
	<td></td>

<?php	
	break;
	case 'V':
?>
	<td></td>
        <td align="center"><?php echo'<span title="'.$r_retet['vndd'].'">'.$r_retet['vndd'].'</span>';?></td>
	<td align="left"><?php echo '<span title="'.$r_retet['vndd_descri'].'">'.$r_retet['vndd_descri'].'</span>';?></a> </td>
	<td align="left"></td>
	<td></td>

<?php	
	break;
	case 'Z':
?>
	<td></td>
        <td align="center"><?php echo'<span title="'.$r_retet['zsub'].'">'.$r_retet['zsub'].'</span>';?></td>
	<td align="left"><?php echo '<span title="'.$r_retet['zsub_descri'].'">'.$r_retet['zsub_descri'].'</span>';?></a> </td>
	<td align="left"></td>
	<td></td>

<?php	
	break;
	case 'L':
?>
	<td></td>
        <td align="center"><?php echo'<span title="'.$r_retet['item_lsub'].'">'.$r_retet['item_lsub'].'</span>';?></td>
	<td align="left"><?php echo '<span title="'.$r_retet['lsub_descri'].'">'.$r_retet['lsub_descri'].'</span>';?></a> </td>
	<td align="left"></td>
	<td></td>

<?php	
	break;
	case 'R':
?>
	<td></td>
        <td align="center"><?php echo'<span title="'.$r_retet['item_at10'].'">'.$r_retet['item_at10'].'</span>';?></td>
	<td align="left"><?php echo '<span title="'.$r_retet['at10_descri'].'">'.$r_retet['at10_descri'].'</span>';?></a> </td>
	<td align="left"></td>
	<td></td>



<?php }?>



   	<td align="right"><?php echo number_format($r_retet['sbrta'],2);?></td>
   	<td align="right"><?php echo number_format($r_retet['mprta'],2);?></td>
   	<td align="right"><?php echo number_format($r_retet['srrta'],2);?></td>
   	<td align="right"><?php echo number_format($r_retet['sbiva'],2);?></td>
   	<td align="right"><?php echo number_format($r_retet['mpiva'],2);?></td>
   	<td align="right"><?php echo number_format($r_retet['sriva'],2);?></td>

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








</tr>
<?php }?>

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

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

	<th></th>

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

	<th></th>

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



</table>
<br />


















</div>
</div>
<br>

<div id="retetot2"></div>
</body>
</html>