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

$fini1= date('2000-01-01');
$sql_fini1='select * from empr0';
$res_fini1=mysql_query($sql_fini1,$link_vcrx);
$fini1=mysql_result($res_fini1,0,'empr_fcont');


$fecha1 = date('Y-m-j');
$nuevafecha = strtotime ( '-120 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);

	if(isset($_GET['fecha_ini']) && !empty($_GET['fecha_ini']))
	{
		$fini=$_GET['fecha_ini'];
	}
	else
	{
		$fini=date('2000-01-01');
		if (mb_stristr('gnya',$_SESSION['user_vcrx_empr']))
		{$fini=date('2010-11-01');}
	}
	
	if(isset($_GET['fecha_fin']) && !empty($_GET['fecha_fin']))
	{
		$ffin=$_GET['fecha_fin'];
	}
	else
	{
		$ffin=date('Y-m-d');
	}


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

$xfecha1=str_replace('/','-',$xfecha1);
$xfecha2=str_replace('/','-',$xfecha2);


$sql_icontt='select cont12.*,
			sum(case when fecha<"'.$xfecha1.'" then tcant end) as saldoi, 
			sum(case when fecha>="'.$xfecha1.'" and fecha<="'.$xfecha2.'" then tdebito end) as tdeb, 
			sum(case when fecha>="'.$xfecha1.'" and fecha<="'.$xfecha2.'" then tcredito end) as tcre, 
			sum(tcant) as saldo
			from cont12
			where ';


$sql_icontt.='fecha >= "'.$fini1.'" AND fecha <= "'.$xfecha2.'"';


if(isset($_GET['idnum']) && !empty($_GET['idnum']))
{
	$sql_icontt.=' and  (ctas like "'.$_GET['idnum'].'%" )';
}

$xfgrou='C';

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


$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='docu,ptoe,numero,idnum1 ';break;
	case 'C':$xgroup='ctas ';break;

	case 'P':$xgroup='ptoe ';break;
	case 'V':$xgroup='vndd ';break;
	case 'Z':$xgroup='zsub ';break;
	case 'L':$xgroup='ctas_lsub ';break;
	case 'R':$xgroup='ctas_at10 ';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 'T':$xorder='ttotal desc, '.str_replace('_descri','',$xgroup);break;

	default :'fecha,signo desc, bode, docu, ptonumero';break;
}

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

//$sql_icont.=' group by '.$xgroup;
//$sql_icont.=' order by '.$xorder;
//$sql_icontt.=' order by ctas group by ctas';
$sql_icontt.=' group by ctas order by ctas';
//$sql_icont=str_replace('sum','',$sql_icont);

//$res_icontt=mysql_query($sql_icontt,$link_vcrx);
//$res_icont=mysql_query($sql_icont,$link_vcrx);

//echo $sql_icont;
?>


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

<title>CUENTAS (Detalle)</title>
<style type="text/css">

<style type="text/css">
BODY{font-family: courier new;}
	
body{ font-family:"Courier New", Courier, monospace;}

 H1.SaltoDePagina
 {
     PAGE-BREAK-BEFORE: always
 }
	
	
</style>

</head>
<body>

<?php	
//echo $sql_icontt;
$res_icontt=mysql_query($sql_icontt,$link_vcrx);
while($r_icontt=mysql_fetch_assoc($res_icontt))
{

$xcta1=$r_icontt['ctas'];
$sql_icont='select * from cont12 where ';
$sql_icont.='fecha >= "'.$fini1.'" AND fecha <= "'.$xfecha2.'"';
$sql_icont.=' and  (ctas like "'.$xcta1.'" )';
// $sql_icont.=' order by ctas,fecha,idnum1';
$sql_icont.=' order by ctas,fecha,(tdebito/tcredito),docu,numero';

	
?>	
	
<div align="center">
<div align="center" style="width:1150px; font-size:12px;">
<div align="center" style="font-size:18px; border:#000 solid 1px; background: #F7F7F7; margin:2px; font-weight: bold;">CUENTAS MOVIMIENTO</div>

<table width="90%" 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($fini),'d/M/Y').' - '.date_format(date_create($ffin),'d/M/Y');?></td>
</tr>



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

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


<?php	
	break;
	case 'E':
	$sql_clie='select * from enti00 where enti_codigo like "'.$_GET['idnum'].'%" ';
	$res_clie=mysql_query($sql_clie,$link_vcrx);
?>

<tr>
	<td><b>CLIENTE</td>
	<td><?php echo mysql_result($res_clie,0,'enti_codigo').' - '.mysql_result($res_clie,0,'enti_descri');?></td>
<tr>


<?php	
	break;
	case 'C':
	$sql_ctas='select * from ctas00 where ctas_codigo like "'.$_GET['idnum'].'%" ';
	$res_ctas=mysql_query($sql_ctas,$link_vcrx);
?>

<tr>
	<td><b>CUENTA</td>
	<td><?php echo $r_icontt['ctas'].' - '.$r_icontt['ctas_descri'];?></td>
<tr>
<?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 } ?>

</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:i:s');?></td>
</tr>
</table>
<!----++++++++++------>
</td>




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

<hr/>

<table border="1" width="90%" cellspacing="0" cellpadding="2" style="border-collapse:collapse">
<tr style="background: #F7F7F7;">
	<th align="center"><b>INICIAL</td>
	<th align="center"><b>DEBITOS</td>
	<th align="center"><b>CREDITOS</td>
	<th align="center"><b>SALDO</td>
</tr>
<tr>
   	<td align="center">
		<?php if(mb_stristr('1.5.6',substr($r_icontt['ctas'],0,1) ) and  $r_icontt['saldoi']<0){echo '<font color="red">';} ?>
		<?php if(mb_stristr('2.3.4',substr($r_icontt['ctas'],0,1) ) and  $r_icontt['saldoi']>0){echo '<font color="red">';} ?>
		<?php echo ''.number_format($r_icontt['saldoi'],2);?></td>
   	<td align="center"><?php echo ''.number_format($r_icontt['tdeb'],2);?></td>
   	<td align="center"><?php echo ''.number_format($r_icontt['tcre'],2);?></td>
   	<td align="center" >
		<?php if(mb_stristr('1.5.6',substr($r_icontt['ctas'],0,1) ) and  $r_icontt['saldo']<0){echo '<font color="red">';} ?>
		<?php if(mb_stristr('2.3.4',substr($r_icontt['ctas'],0,1) ) and  $r_icontt['saldo']>0){echo '<font color="red">';} ?>
		<?php echo ''.number_format($r_icontt['saldo'],2);?></td>
</tr>	
</table>

<hr/>

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

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


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

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


<?php	
	break;
	case 'C':
?>
	<th>DOCU</th>
	<th>PTO/NUMERO</th>
	<th>FECHA</th>
	<th>DEBITO</th>
	<th>CREDITO</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>SALDO</th>
	<th>.</th>

<?php
switch ($xfgrou)
{
	case 'D':
?>
	<th>PROVINCIA /CIUDAD</th>
	<th>VENDEDOR</th>

<?php	
	break;
	case 'E':
?>
	<th>PROVINCIA /CIUDAD</th>
	<th>VENDEDOR</th>

<?php	
	break;
	case 'C':
?>
	<th>CSUB</th>
	<th>DETALLE</th>
	<th>ST</th>
	<th>ENTIDAD</th>
	<th>.</th>
<?php } ?>



</tr>

</div>


</tr>

<?php 
$filax=0;
$xtdeb=0;
$xtcre=0;
$xsaldo= 0;
$xuctas='';
$xumes=0;	
//echo $sql_icont;
//$urcdd='';
$res_icont=mysql_query($sql_icont,$link_vcrx);

while($r_icont=mysql_fetch_assoc($res_icont))
{
$duni=2;
$xsaldo=$xsaldo+$r_icont['tdebito']-$r_icont['tcredito'];

?>

	
<?php
//echo '-'.$r_icont['fecha'];
//echo '-'.$xfecha1;
if($r_icont['fecha']>=$xfecha1 )
{
?>
<tr valign="top" 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 if($xumes!=$r_icont['fmes']){echo 'border-top:solid 1px #000000;'; $xumes=$r_icont['fmes'];}?>" >
<?php
switch ($xfgrou)
{
	case 'D':
?>

  <td valign="top" align="center"><?php echo'<span title="'.$r_icont['docu'].'">'.$r_icont['docu'].'</span>';?></td>
	<td valign="top" align="center"><?php echo '<span title="'.$r_icont['ptonumero'].'">'.$r_icont['numero'].'</span>';?></a> </td>
	<td align="center"><?php echo'<span title="'.$r_icont['fechadmy'].'">'.$r_icont['fechadmy'].'</span>';?></td>
	<td align="left"><?php echo'<span title="'.$r_icont['enti'].'">'.$r_icont['enti_descri'].'</span>';?></td>
	<td></td>


<?php	
	break;
	case 'E':
?>
	<td align="center"><?php echo '<span title="'.$r_icont['docu_descri'].'">'.$r_icont['docu'].'</span>';?></a> </td>
	<td align="center"><?php echo '<span title="'.$r_icont['ptonumero'].'">'.$r_icont['ptonumero'].'</span>';?></a> </td>
	<td align="center"><?php echo'<span title="'.$r_icont['fechadmy'].'">'.$r_icont['fechadmy'].'</span>';?></td>


<?php	
	break;
	case 'C':
?>
	<td align="center"><?php echo '<span title="'.$r_icont['docu_descri'].'">'.$r_icont['docu'].'</span>';?></a> </td>
	<td align="center"><?php echo '<span title="'.$r_icont['ptonumero'].'">'.$r_icont['numero'].'</span>';?></a> </td>
	<td align="center"><?php echo'<span title="'.$r_icont['fechadmy'].'">'.$r_icont['fechadmy'].'</span>';?></td>


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

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

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

<?php	
	break;
	case 'P':
?>
	<td></td>
	<td align="left"><?php echo '<span title="'.$r_icont['ptoe'].'">'.$r_icont['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_icont['vndd'].'">'.$r_icont['vndd'].'</span>';?></td>
	<td align="left"><?php echo '<span title="'.$r_icont['vndd_descri'].'">'.$r_icont['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_icont['zsub'].'">'.$r_icont['zsub'].'</span>';?></td>
	<td align="left"><?php echo '<span title="'.$r_icont['zsub_descri'].'">'.$r_icont['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_icont['ctas_lsub'].'">'.$r_icont['ctas_lsub'].'</span>';?></td>
	<td align="left"><?php echo '<span title="'.$r_icont['lsub_descri'].'">'.$r_icont['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_icont['ctas_at10'].'">'.$r_icont['ctas_at10'].'</span>';?></td>
	<td align="left"><?php echo '<span title="'.$r_icont['at10_descri'].'">'.$r_icont['at10_descri'].'</span>';?></a> </td>
	<td align="left"></td>
	<td></td>



<?php }?>


   	<td align="right">&nbsp;<?php echo ''.number_format($r_icont['tdebito'],2);?></td>
   	<td align="right">&nbsp;<?php echo '<i>'.number_format($r_icont['tcredito'],2);?></td>

<?php
if(mb_stristr('1.5.6',substr($_GET['idnum'],0,1)))
{
?>
   	<td align="right">&nbsp;<?php if($xsaldo<0){echo '<font color="red">';} ?><?php echo ''.number_format($xsaldo,2);?></td>
<?php
}
else
{
?>
   	<td align="right">&nbsp;<?php if($xsaldo>0){echo '<font color="red">';} ?><?php echo ''.number_format($xsaldo,2);?></td>
<?php }?>






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

        <td align="center"><?php echo'<span title="'.$r_icont['cdad_descri'].'">'.$r_icont['cdad_descri'].'</span>';?></td>
	<td align="center"><?php echo'<span title="'.$r_icont['vndd'].'">'.$r_icont['vndd'].'</span>';?></td>

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

	<td align="center"><?php echo'<span title="'.$r_icont['enti_telefono'].'">'.$r_icont['prvc_descri'].' /'.$r_icont['cdad_descri'].'</span>';?></td>
	<td align="center"><?php echo'<span title="'.$r_icont['vndd'].'">'.$r_icont['vndd'].'</span>';?></td>

<?php	
	break;
	case 'C':
?>
	<td align="left"></td>
	<td align="left"><?php echo'<span >'.$r_icont['csub'].'</span>';?></td>
	<td align="left"><?php echo'<span >'.trim($r_icont['detalle']).' /'.$r_icont['concepto'].'</span>';?></td>
	<td align="center"><?php echo'<span >'.$r_icont['status'].'</span>';?></td>
	<td align="left"><?php echo'<span >'.$r_icont['enti_descri'].'</span>';?></td>
	<td align="left"><?php echo'<span >'.'_'.'</span>';?></td>
<?php }?>



</tr>
<?php }?>

<?php }?>


<tr align="center" style="background: #F7F7F7; border-top:solid 1px#000;border-bottom:solid 1px#000">
	<th></th>
	<th>TOTALES</th>
	<th></th>
	<th align="right">&nbsp;<?php echo number_format($r_icontt['tdeb'],2)?></th>
	<th align="right">&nbsp;<?php echo number_format($r_icontt['tcre'],2)?></th>
	<th align="right">&nbsp;<?php echo number_format($r_icontt['saldo'],2)?></th>


<tr>

</table>
</div>
<br>
<H1 class=SaltoDePagina> </H1>
<?php
}	
?>	


