<?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');


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

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


	//$enti=$_GET['enti'];
	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('2014-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'];
	}


$xfrepo=$_GET['frepo'];

$sql_cxco='select cxco12.*,
	sum(case when fecha<"'.$xfecha1.'" then debito-credito end) as tsaldoi,
	sum(case when fecha>="'.$xfecha1.'" and fecha<="'.$xfecha2.'" then abs(debito) end) as tdebito,
	sum(case when fecha>="'.$xfecha1.'" and fecha<="'.$xfecha2.'" then abs(credito) end) as tcredito,
	sum(debito-abs(credito)) as tsaldo,
	sum(case when signo=1 then idnum1 end) as idnum0
	from cxco12 where ';
$sql_cxco.='fecha >= "'.$fini1.'" AND fecha <= "'.$xfecha2.'"';


if ($xfrepo=='R')
{	
$sql_cxco='select cxco12.*,
	sum(debito-credito) as tsaldoi,
	sum(abs(debito)) as tdebito,
	sum(abs(credito) ) as tcredito,
	sum(debito-abs(credito)) as tsaldo,
	sum(case when signo=1 then idnum1 end) as idnum0
	from cxco12 where ';
	$sql_cxco.='fecha >= "'.$xfecha1.'" AND fecha <= "'.$xfecha2.'"';
}


if ($xfrepo=='A')
{	
$sql_cxco='select cxco12.*,
	sum(debito-credito) as tsaldoi,
	sum(abs(debito)) as tdebito,
	sum(abs(credito) ) as tcredito,
	sum(debito-abs(credito)) as tsaldo,
	sum(case when signo=1 then idnum1 end) as idnum0
	from cxco12 where ';
$sql_cxco.='fecha >= "'.$fini1.'" AND fecha <= "'.$xfecha2.'"';
}

$sql_cxco.=' and docu not like "RBCO" ';


if(isset($_GET['fposi']) && ($_GET['fposi']=='true'))
{$xfposi=$_GET['fposi'];}
else
{$xfposi='false';}

if(isset($_GET['fcero']) && ($_GET['fcero']=='true'))
{$xfcero=$_GET['fcero'];}
else
{$xfcero='false';}

if(isset($_GET['fnega']) && ($_GET['fnega']=='true'))
{$xfnega=$_GET['fnega'];}
else
{$xfnega='false';}

if($xfposi=='false' && $xfnega=='false' && $xfcero=='false')
{$xfposi='true';}


if(isset($_GET['fcdad']) && !empty($_GET['fcdad']))
{
	$sql_cxco.=' and  (prvc_descri like "%'.$_GET['fcdad'].'%" or enti_cdad like "'.$_GET['fcdad'].'%" or cdad_descri like "%'.$_GET['fcdad'].'%"  ) ';
	$xfcdad=$_GET['fcdad'];
}

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

if(isset($_GET['fzsub']) && !empty($_GET['fzsub']))
{
// 	$sql_cxco.=' and  (enti_zsub like "'.$_GET['fzsub'].'%" or zsub_descri like "%'.$_GET['fzsub'].'%" ) ';
	$sql_cxco.=' and  (zsub like "'.$_GET['fzsub'].'%" or zsub_descri like "%'.$_GET['fzsub'].'%" ) ';
	$xfzsub=$_GET['fzsub'];
}

if(isset($_GET['fvndd']) && !empty($_GET['fvndd']))
{
	$sql_cxco.=' and  (vndd like "'.$_GET['fvndd'].'%" ) ';
	$xfvndd=$_GET['fvndd'];
}

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


// if(isset($_GET['fctlg']) && !empty($_GET['fctlg']))
// {
// 	$xfctlg=$_GET['fctlg'];
// }

$xgroup = 'enti ';
if ($xfrepo=='R'){$xgroup='enti,docuenti2';}
if ($xfrepo=='A'){$xgroup='enti,docuenti2';}

$xforde='N';

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

$xorder='';
if (mb_stristr('gnya',$_SESSION['user_vcrx_empr']))
{$xorder='';}

switch ($xforde)
{
	case 'N': $xorder.='enti_descri,';break;
	case 'C': $xorder.='enti,enti_descri,';break;
	case 'T': $xorder.='tsaldo desc,enti_descri,';break;
	case 'V': $xorder.='vndd,enti_descri,';break;
	default : $xorder.='';break;
}

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

$sql_cxco.=' order by '.$xorder. 'enti_zsub,enti,docuenti2';

$res_cxco=mysql_query($sql_cxco,$link_vcrx);

?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Cuentas X Cobrar (Cliente)</title>
<style type="text/css">
body{ font-family:"Courier New", Courier, monospace;}

</style>

</head>
<body>

<div align="center">
<div align="center" style="width:1100px; font-size:12px;">
<div align="center" style="font-size:18px;font-weight: bold; border:#000 solid 1px; background: #F7F7F7; margin:2px;">CUENTAS X COBRAR (CLIENTE)</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($fini),'d/M/Y').' - '.date_format(date_create($ffin),'d/M/Y');?></td>
</tr>

<tr>
    	<td style="font-weight:bolder;">CLIENTE</td>
	<td><?php if($xfenti==''){echo 'TODOS';}else{echo $xfenti;}?></td>
</tr>

<tr>
    	<td style="font-weight:bolder;">SALDOS</td>
	<td><?php if($xfposi=='true'){echo '/POSITIVOS';};if($xfcero=='true'){echo '/CANCELADOS';};if($xfnega=='true'){echo '/NEGATIVOS';};?></td>
</tr>

<tr>
    	<td style="font-weight:bolder;">ORDENADO X</td>
	<td><?php if($xorder=='N'){echo 'NOMBRE';};if($xorder=='C'){echo 'CODIGO';};if($xorder=='T'){echo 'TOTALES';};if($xorder=='V'){echo 'VENDEDOR';};?></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:i: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 if ($xfrepo=='S'){ ?>
  <th>ZONA</th>
  <th>CLIENTE</th>
  <th>NOMBRE</th>  
  <th>INICIAL</th>
  <th>DEBITO</th>
  <th>CREDITO</th>
  <th>SALDO</th>
	
<?php } ?>
<?php if ($xfrepo=='R'){ ?>
	<th>DOCU</th>
	<th>FECHA</th>
	<th>CONCEPTO</th>
	<th>VALOR</th>
	<th>ABONO</th>
	<th>SALDO</th>
	<th>VENCE</th>
	<th>DIAS</th>
<?php } ?>
	
<?php if ($xfrepo=='A'){ ?>
  <th>ZONA</th>
	<th>CLIENTE</th>
	<th>NOMBRE</th>

	<th>0 DIAS</th>
	<th>30 DIAS</th>
	<th>60 DIAS</th>
	<th>90 DIAS</th>
	<th>120 DIAS</th>
	<th>150 DIAS</th>
	<th>180 DIAS</th>
	<th>360 DIAS</th>
	<th>+360 DIAS</th>
	<th>TOTAL</th>
<?php } ?>

	
</tr>


<?php 
$filax=0; 
$xsaldo = 0;

$tsaldoi=0;
$tdebito=0;
$tcredito=0;
$tsaldo=0;

$tvalor=0;
$tabono=0;
$tsalde=0;
$lenti='';	
	
$saltot=0;	
$sal000=0;	
$sal030=0;	
$sal060=0;	
$sal090=0;	
$sal120=0;	
$sal150=0;	
$sal180=0;	
$sal360=0;	
$sal999=0;	

$tsaltot=0;	
$tsal000=0;	
$tsal030=0;	
$tsal060=0;	
$tsal090=0;	
$tsal120=0;	
$tsal150=0;	
$tsal180=0;	
$tsal360=0;	
$tsal999=0;	

$xsaldo0=1/100;
$xsaldo0=10/100;

$idx=0;
	
while($r_cxco=mysql_fetch_assoc($res_cxco)) 
{
?>


<?php
if(($xfposi=='true' && ($r_cxco['tsaldo'])>$xsaldo0) || ($xfcero=='true' && abs($r_cxco['tsaldo'])<=$xsaldo0) || ($xfnega=='true' && ($r_cxco['tsaldo'])<$xsaldo0*-1))
{
?>
  <?php
	$idx++;
	$tsaldoi=$tsaldoi+($r_cxco['tsaldoi']);
	$tdebito=$tdebito+abs($r_cxco['tdebito']);
	$tcredito=$tcredito+abs($r_cxco['tcredito']);
	$tsaldo=$tsaldo+($r_cxco['tsaldo']);
  ?>

<?php if ($xfrepo=='S'){ ?>
<tr style=" <?php if($filax!=3){$filax++;}else{echo 'border-bottom:dotted 1px #000000;'; $filax=0;}?> ">
  <td align="center"><?php echo $r_cxco['enti_zsub']; ?></td>
  <td align="left"><?php echo $r_cxco['enti']; ?></td>
  <td align="left"><?php echo $r_cxco['enti_descri']; ?></td>
  <td align="right"><?php if($r_cxco['tsaldoi']<0){echo '<u>';} ?><?php echo number_format($r_cxco['tsaldoi'],2); ?></td>
  <td align="right"><?php echo number_format($r_cxco['tdebito'],2); ?></td>
  <td align="right"><?php echo number_format(abs($r_cxco['tcredito']),2); ?></td>
  <td align="right"><?php if($r_cxco['tsaldo']<0){echo '<u>';} ?><?php echo number_format($r_cxco['tsaldo'],2); ?></td>
	</tr>
<?php } ?>
	
<?php if ($xfrepo=='R' ){ ?>
	<?php if ($r_cxco['enti']!=$lenti && $lenti!=''){ ?>
	<tr  style="font-weight:bold;">
	<th></th>	
	<th></th>	
  <th align="right">TOTALES</td>
  <th align="right"><?php echo number_format($tvalor,2); ?></th>
  <th align="right"><?php echo number_format($tabono,2); ?></th>
  <th align="right"><?php echo number_format($tsalde,2); ?></th>
	</tr>
	<?php 
			$tvalor=0;
			$tabono=0;
			$tsalde=0;
	?>
	<?php } ?>
	<?php if ($r_cxco['enti']!=$lenti){ ?>
	<tr valign="bottom" height="30px" style="font-weight:bold;">
  <td align="center"><?php echo $r_cxco['enti_zsub']; ?></td>
  <td align="center"><?php echo $r_cxco['enti']; ?></td>
  <td align="left" colspan="1"><?php echo $r_cxco['enti_descri']; ?></td>
	<?php 
			$filax=0; 
			$lenti=$r_cxco['enti'];
// 			$sql_cxcot=$sql_cxco;
// 			$sql_cxcot=str_replace('where','where enti="'.$r_cxco['enti'].'" and ',$sql_cxcot);
// 			$sql_cxcot=str_replace(',docuenti2','',$sql_cxcot);
	?>
	</tr>
	<?php } ?>
	<?php 
			$tvalor=$tvalor+$r_cxco['tdebito'];
			$tabono=$tabono+abs($r_cxco['tcredito']);
			$tsalde=$tsalde+$r_cxco['tdebito']-abs($r_cxco['tcredito']);
	?>
	<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++;} ?>" >
		<td align="center"> <?php echo $r_cxco['docum'].'-'.$r_cxco['ptonumer'];?></td>
		<td align="center"> <?php echo $r_cxco['fechadmy'];?></td>
		<td align="left"> <?php echo $r_cxco['concepto'];?></td>
   	<td align="right"><?php echo number_format($r_cxco['tdebito'],2);?></td>
   	<td align="right"><?php echo '<i>'.number_format(abs($r_cxco['tcredito']),2);?></td>
   	<td align="right"><?php if($r_cxco['tsaldo']<0){echo '<u>';} ?> <?php echo number_format($r_cxco['tsaldo'],2);?></td>
		<td align="center"> <?php if($r_cxco['tsaldo']>0 && $r_cxco['dias']>0){echo '<u>';} ?><?php echo $r_cxco['vence'];?></td>
		<td align="center"> <?php echo $r_cxco['dias'];?></td>
	</tr>
<?php } ?>

<?php if ($xfrepo=='A' ){ ?>
<?php if ($r_cxco['enti']!=$lenti){ ?>
<?php if ($idx>1){ ?>
   	<td align="right"><?php echo number_format($sal000,2);?></td>
   	<td align="right"><?php echo number_format($sal030,2);?></td>
   	<td align="right"><?php echo number_format($sal060,2);?></td>
   	<td align="right"><?php echo number_format($sal090,2);?></td>
   	<td align="right"><?php echo number_format($sal120,2);?></td>
   	<td align="right"><?php echo number_format($sal150,2);?></td>
   	<td align="right"><?php echo number_format($sal180,2);?></td>
   	<td align="right"><?php echo number_format($sal360,2);?></td>
   	<td align="right"><?php echo number_format($sal999,2);?></td>
   	<td align="right"><?php echo number_format($saltot,2);?></td>
</tr>
<?php
$saltot=0;	
$sal000=0;	
$sal030=0;	
$sal060=0;	
$sal090=0;	
$sal120=0;	
$sal150=0;	
$sal180=0;	
$sal360=0;	
$sal999=0;	
									
?>
<?php } ?>
	
<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++;} ?>" >
  <td align="center"><?php echo $r_cxco['enti_zsub']; ?></td>
  <td align="left"><?php echo $r_cxco['enti']; ?></td>
  <td align="left"><?php echo $r_cxco['enti_descri']; ?></td>
<?php $lenti=$r_cxco['enti']; ?>

<?php } ?>
  <?php
	$saltot+=$r_cxco['tsaldo'];
	$tsaltot+=$r_cxco['tsaldo'];
	
	if($r_cxco['anti']==0)
	{
		$sal000+=$r_cxco['tsaldo'];
		$tsal000+=$r_cxco['tsaldo'];
	}
	
	if($r_cxco['anti']==30)
	{
		$sal030+=$r_cxco['tsaldo'];
		$tsal030+=$r_cxco['tsaldo'];
	}
	
	if($r_cxco['anti']==60)
	{
		$sal060+=$r_cxco['tsaldo'];
		$tsal060+=$r_cxco['tsaldo'];
	}
	
	if($r_cxco['anti']==90)
	{
		$sal090+=$r_cxco['tsaldo'];
		$tsal090+=$r_cxco['tsaldo'];
	}
	
	if($r_cxco['anti']==120)
	{
		$sal120+=$r_cxco['tsaldo'];
		$tsal120+=$r_cxco['tsaldo'];
	}
	
	if($r_cxco['anti']==150)
	{
		$sal150+=$r_cxco['tsaldo'];
		$tsal150+=$r_cxco['tsaldo'];
	}
	
	if($r_cxco['anti']==180)
	{
		$sal180+=$r_cxco['tsaldo'];
		$tsal180+=$r_cxco['tsaldo'];
	}
	
	if($r_cxco['anti']==360)
	{
		$sal360+=$r_cxco['tsaldo'];
		$tsa360+=$r_cxco['tsaldo'];
	}

	if($r_cxco['anti']==999)
	{
		$sal999+=$r_cxco['tsaldo'];
		$tsal999+=$r_cxco['tsaldo'];
	}
	
  ?>

<?php } ?>
	
	
	
<?php } ?>
<?php }?>


	
<?php if ($xfrepo=='R'){ ?>
	<tr  style="font-weight:bold;">
	<td></td>	
	<td></td>	
  <td align="right">TOTALES</td>
  <td align="right"><?php echo number_format($tvalor,2); ?></td>
  <td align="right"><?php echo number_format($tabono,2); ?></td>
  <td align="right"><?php echo number_format($tsalde,2); ?></td>
</tr>	
<?php } ?>

<?php if ($xfrepo=='A'){ ?>
   	<td align="right"><?php echo number_format($sal000,2);?></td>
   	<td align="right"><?php echo number_format($sal030,2);?></td>
   	<td align="right"><?php echo number_format($sal060,2);?></td>
   	<td align="right"><?php echo number_format($sal090,2);?></td>
   	<td align="right"><?php echo number_format($sal120,2);?></td>
   	<td align="right"><?php echo number_format($sal150,2);?></td>
   	<td align="right"><?php echo number_format($sal180,2);?></td>
   	<td align="right"><?php echo number_format($sal360,2);?></td>
   	<td align="right"><?php echo number_format($sal999,2);?></td>
   	<td align="right"><?php echo number_format($saltot,2);?></td>
</tr>
<?php } ?>
	
<?php if ($xfrepo=='S' || $xfrepo=='R'){ ?>
<tr align="center" style="background: #F7F7F7; border-top:solid 1px#000;border-bottom:solid 1px#000">
<th></th>
<th></th>
<th align="center">T O T A L E S</th>
<?php if ($xfrepo=='S'){ ?>
<th align="right"><?php echo number_format($tsaldoi,2); ?></th>
<th align="right"><?php echo number_format($tdebito,2); ?></th>
<th align="right"><?php echo number_format($tcredito,2); ?></th>
<th align="right"><?php echo number_format($tsaldo,2); ?></th>
<?php } ?>
<?php if ($xfrepo=='R'){ ?>
<th align="right"><?php echo number_format($tdebito,2); ?></th>
<th align="right"><?php echo number_format($tcredito,2); ?></th>
<th align="right"><?php echo number_format($tsaldo,2); ?></th>
<?php } ?>
</tr>
<?php } ?>

<?php if ($xfrepo=='A'){ ?>
<tr align="center" style="background: #F7F7F7; border-top:solid 1px#000;border-bottom:solid 1px#000">
		<th></th>
		<th></th>
		<th align="right">T O T A L E S</th>
   	<th align="right"><?php echo number_format($tsal000,2);?></th>
   	<th align="right"><?php echo number_format($tsal030,2);?></th>
   	<th align="right"><?php echo number_format($tsal060,2);?></th>
   	<th align="right"><?php echo number_format($tsal090,2);?></th>
   	<th align="right"><?php echo number_format($tsal120,2);?></th>
   	<th align="right"><?php echo number_format($tsal150,2);?></th>
   	<th align="right"><?php echo number_format($tsal180,2);?></th>
   	<th align="right"><?php echo number_format($tsal360,2);?></th>
   	<th align="right"><?php echo number_format($tsal999,2);?></th>
   	<th align="right"><?php echo number_format($tsaltot,2);?></th>
</tr>
<?php } ?>
	
	
	
</table>




</div>
</div>

</body>
</html>

<?php //}?>