<?php 
session_start();

require('../cfg/res.php');
require('../cfg/cfg.php');
?>
<?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');
$xfecha2p= $xfecha2;
?>

<?php

	$enti=$_POST['enti'];
// echo $_POST['fecha_fin'];

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

  $xfecha2p= $xfecha2;


if($xfpost=='true')
{
	$xfecha2 ='2999-01-01';
	$xfecha2p='2999-01-01';
}

// echo $xfecha2;

$xfrepo='S';
if(isset($_POST['frepo']) && !empty($_POST['frepo']))
{$xfrepo=$_POST['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<="'.$xfecha2p.'" then abs(credito) end) as tcredito,
	sum(case when fecha<="'.$xfecha2p.'" then debito-abs(credito) end) as tsaldo
	from cxco12 where ';
// 	sum(case when fecha>="'.$xfecha1.'" and fecha<="'.$xfecha2p.'" then debito-abs(credito) end) as tsaldo
// $sql_cxco.='fecha >= "'.$fini1.'" AND fecha <= "'.$xfecha2.'"';
$sql_cxco.='fecha >= "'.$fini1.'" AND fecha <= "'.$xfecha2p.'"';


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.'"';
$sql_cxco.='and fecha>="'.$fini1.'"';
}

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.'"';
}

// echo $sql_cxco;

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

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

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

if(isset($_POST['fpost']) && ($_POST['fpost']=='true'))
{$xfpost=$_POST['fpost'];}
else
{$xfpost='false';}



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

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

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

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

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

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

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

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

$xgroup = 'enti ';

$xforde='N';
if(isset($_POST['forde']) && !empty($_POST['forde']))
{$xforde=$_POST['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;
}

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

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

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

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

// echo $sql_cxco;
$res_cxco=mysql_query($sql_cxco,$link_vcrx);
// echo str_repeat('-',date('s')*2).'';

?>

<div style="overflow-y:scroll; height:300px;">

<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 if ($xfrepo=='S'){ ?>
	<th></th>
  <th>ZONA</th>
	<th>CLIENTE</th>
	<th>NOMBRE</th>
	<th>INICIAL</th>
	<th>DEBITOS</th>
	<th>CREDITOS</th>
	<th>SALDO</th>
	<th>PROVINCIA /CIUDAD</th>
	<th>VENDEDOR</th>
<?php } ?>
<?php if ($xfrepo=='R'){ ?>
	<th></th>
	<th>DOCU</th>
	<th>FECHA</th>
	<th>CONCEPTO</th>
	<th>VALOR</th>
	<th>ABONO</th>
	<th>SALDO</th>
	<th>VENCE</th>
	<th>DIAS</th>
	<th></th>
<?php } ?>
<?php if ($xfrepo=='A'){ ?>
	<th></th>
  <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;

$tsaldoi=0;
$tdebito=0;
$tcredito=0;
$tsaldo=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))
{
$duni=$r_cxco['enti_duni'];
?>

<?php
// if(($xfposi=='true' && ($r_cxco['tsaldo'])>$xsaldo0)
//    || ($xfcero=='true' && abs($r_cxco['tsaldo'])<=$xsaldo0) 
//    || ($xfnega=='true' && ($r_cxco['tsaldo'])<$xsaldo0*-1))
if(($xfposi=='true' and ($r_cxco['tsaldo'])>$xsaldo0)
   or ($xfcero=='true' and abs($r_cxco['tsaldo'])<=$xsaldo0) 
   or ($xfnega=='true' and ($r_cxco['tsaldo'])<$xsaldo0*(-1) ))
{
?>

  <?php
	$idx++;
	$tsaldoi+=$r_cxco['tsaldoi'];
	$tdebito+=abs($r_cxco['tdebito']);
	$tcredito+=abs($r_cxco['tcredito']);
	$tsaldo+=$r_cxco['tsaldo'];
	?>

<?php if ($xfrepo=='S'){ ?>
<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>
<a href="../lib/impr_cxccli1d.php?enti=<?php echo $r_cxco['enti']; ?>&fecha_ini=<?php echo $xfecha1; ?>&fecha_fin=<?php echo $xfecha2; ?>&frepo=<?php echo $xfrepo;?>" target="_blank"><img src="../img/print2.png" width="20"  title="Imprimir  <?php echo $r_cxco['enti_descri'];?>"  />
</a>
</td>
 <td align="center"><?php echo'<span title="'.$r_cxco['enti_zsub'].'">'.$r_cxco['zsub_descri'].'</span>';?></td>
	<td align="left"><a href="#" onClick="cxccli1d('<?php echo $r_cxco['enti']."','".$xfrepo;?>',this);"><?php echo '<span title="'.$r_cxco['enti'].'">'.$r_cxco['enti'].'</span>';?></a> </td>
	<td align="left"><?php echo'<span title="'.$r_cxco['enti_direccion'].'">'.$r_cxco['enti_descri'].'</span>';?></td>
		<td align="right"><?php if($r_cxco['tsaldoi']<0){echo '<font color="red">';} ?> <?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 '<i>'.number_format(abs($r_cxco['tcredito']),2);?></td>
	<td align="right"><b><?php if($r_cxco['tsaldo']<0){echo '<font color="red">';} ?> <?php echo number_format($r_cxco['tsaldo'],2);?></b></td>
	<td align="center"><?php echo'<span title="'.$r_cxco['enti_telefono'].'">'.$r_cxco['prvc_descri'].' /'.$r_cxco['cdad_descri'].'</span>';?></td>
	<td align="center"><?php echo'<span title="'.$r_cxco['vndd'].'">'.$r_cxco['vndd'].'</span>';?></td>
</tr>
<?php } ?>

<?php if ($xfrepo=='R'){ ?>
<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 if ($r_cxco['enti']!=$lenti){ ?>
	<tr class="filax" valign="bottom" height="30px" style="font-weight:bold;">
<td align="center">
<a href="#" onClick="imprimirlista('<?php echo $r_cxco['enti']; ?>')" target="_blank"><img src="../img/print2.png" width="20"  title="Imprimir  <?php echo $r_cxco['enti_descri'];?>"  />
</a>
</td>
 <td align="center"><?php echo'<span title="'.$r_cxco['enti_zsub'].'">'.$r_cxco['zsub_descri'].'</span>';?></td>
	<td align="center">
		<a href="#" onClick="cxccli1d('<?php echo $r_cxco['enti']."','".$xfrepo;?>',this);"><?php echo '<span title="'.$r_cxco['enti'].'">'.$r_cxco['enti'].'</span>';?></a>
		</td>
	<td align="left"><?php echo'<span title="'.$r_cxco['enti_direccion'].'">'.$r_cxco['enti_descri'].'</span>';?></td>
	<td></td>	
	<td></td>	
	<td></td>	
	<td></td>	
	<td></td>	
	<td align="center">
<!-- 		<a href="#" onClick="cxccli1d('<?php echo $r_cxco['enti']."','".$xfrepo;?>',this);"><?php echo '<span title="'.'Aplicar Pagos a Facturas'.'">'.'Aplicar'.'</span>';?></a> -->
		<?php //$pcaption='APLICAR';  $ponclick="cxccli1d('".$r_cxco['enti']."','R',this);" ;include('vrbutton.php'); ?>
	</td>	
	</tr>
	<?php $lenti=$r_cxco['enti']; ?>
	<?php } ?>
	
  <td align="center">
    <a href="../lib/celectop1.php?docu=<?php echo $r_cxco['docum']; ?>&idnum=<?php echo $r_cxco['idnum0']; ?>&formato=1" target="_blank">
		<img src="../img/print2.png" width="20"  title="Imprimir  <?php echo $r_cxco['docum'];?>-<?php echo $r_cxco['numer'];?>"  /></a>
	</td>
	<td align="center">
		<a href="#" onClick="cxccli1d('<?php echo $r_cxco['enti']."','".$r_cxco['docuenti2'];?>',this);"><?php echo '<span title="'.$r_cxco['enti'].'">'.$r_cxco['docum'].'-'.$r_cxco['ptonumer'].'</span>';?></a>
		</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 '<font color="red">';} ?> <?php echo number_format($r_cxco['tsaldo'],2);?></td>
		<td align="center"> <?php if($r_cxco['tsaldo']>0 && $r_cxco['diasv']>0){echo '<font color="red">';} ?><?php echo $r_cxco['vencedmy'];?></td>
		<td align="center"> <?php echo $r_cxco['diasv'];?></td>
		<td align="center">
			<?php if($r_cxco['tsaldo']>0){ ?>
			<a target="_blank" href="../lib/vrcobro0.php?docu=<?php echo $r_cxco['docu'];?>&idnum=<?php echo $r_cxco['idnum0'];?>&numero=0" title="Cobrar <?php echo $r_cxo['docum'];?>-<?php echo $r_cxco['numer'];?>">
			<img src="http://vcrxol.com/_img/ico_cash.png" /></a>
			<?php } ?>
		</td>
	</tr>
<?php } ?>
	
<?php if ($xfrepo=='A'){ ?>
<?php if ($r_cxco['enti']!=$lenti){ ?>
<?php if ($idx>1){ ?>
   	<td align="right" style="border-right:solid 1px;"><?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" style="border-right:solid 1px;"><?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" style="border-right:solid 1px;"><?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"><b><?php echo number_format($saltot,2);?></b></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">
  <a href="#" onClick="imprimirlista('<?php echo $r_cxco['enti']; ?>')" target="_blank"><img src="../img/print2.png" width="20"  title="Imprimir  <?php echo  $r_cxco['enti_descri'];?>"  /></a>
</td>
<td align="center"><?php echo'<span title="'.$r_cxco['enti_zsub'].'">'.$r_cxco['zsub_descri'].'</span>';?></td>
<td align="center">
	<a href="#" onClick="cxccli1d('<?php echo $r_cxco['enti']."','".$xfrepo;?>',this);"><?php echo '<span title="'.$r_cxco['enti'].'">'.$r_cxco['enti'].'</span>';?></a>
</td>
<td align="left"><?php echo'<span title="'.$r_cxco['enti_direccion'].'">'.substr($r_cxco['enti_descri'],0,30).'</span>';?></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=='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"><b><?php echo number_format($saltot,2);?></b></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></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>
<?php } ?>
<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>
</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></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"><b><?php echo number_format($tsaltot,2);?></b></th>
</tr>
<?php } ?>


</table>
</div>
