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

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

<?php

	$enti=$_POST['enti'];


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

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

$sql_cxpa='select cxpa12.*,
	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
	from cxpa12 where ';
$sql_cxpa.='fecha >= "'.$fini1.'" AND fecha <= "'.$xfecha2.'"';

if ($xfrepo=='R' )
{	
$sql_cxpa='select cxpa12.*,
	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 cxpa12 where ';
$sql_cxpa.='fecha >= "'.$xfecha1.'" AND fecha <= "'.$xfecha2.'"';
}

if ($xfrepo=='A')
{	
$sql_cxpa='select cxpa12.*,
	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 cxpa12 where ';
$sql_cxpa.='fecha >= "'.$fini1.'" AND fecha <= "'.$xfecha2.'"';
}

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($xfposi=='false' && $xfnega=='false' && $xfcero=='false')
{$xfposi='true';}

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

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

if(isset($_POST['ftpro']) && !empty($_POST['ftpro']))
{
	$sql_cxpa.=' and  (enti_tpro like "'.$_POST['ftpro'].'%" or tpro_descri like "%'.$_POST['ftpro'].'%" ) ';
	$xftpro=$_POST['ftpro'];
}

$xgroup = 'enti ';

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

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


$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_cxpa.=' group by '.$xgroup;

$sql_cxpa.=' order by '.$xorder. 'enti_tpro,enti,docuenti2';

$res_cxpa=mysql_query($sql_cxpa,$link_vcrx);
// echo str_repeat('-',date('s')*2).'';
?>

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

<table id="tabla_cxpro" 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>T.PROV</th>
	<th>PROVEEDOR</th>
	<th>NOMBRE</th>
	<th>INICIAL</th>
	<th>DEBITOS</th>
	<th>CREDITOS</th>
	<th>SALDO</th>
	<th>T.PROV</th>
<?php } ?>
<?php if ($xfrepo=='R'){ ?>
	<th></th>
	<th>DOCU</th>
	<th>FECHA</th>
	<th style="max-width:100px;">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;
// 	echo $sql_cxpa;
while($r_cxpa=mysql_fetch_assoc($res_cxpa))
{
$duni=$r_cxpa['enti_duni'];
?>

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

	
	<?php
	$idx++;
//   echo $idx;
	$tsaldoi+=$r_cxpa['tsaldoi'];
	$tdebito+=abs($r_cxpa['tdebito']);
	$tcredito+=abs($r_cxpa['tcredito']);
	$tsaldo+=$r_cxpa['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_cxppro1d.php?enti=<?php echo $r_cxpa['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_cxpa['enti_descri'];?>"  /></a>
</td>

	<td align="center"><?php echo'<span title="'.$r_cxpa['tpro'].'">'.$r_cxpa['tpro'].'</span>';?></td>
	<td align="left"><a href="#" onClick="cxppro1d('<?php echo $r_cxpa['enti']."','".$xfrepo;?>',this);"><?php echo '<span title="'.$r_cxpa['enti'].'">'.$r_cxpa['enti'].'</span>';?></a> </td>

	<td align="left"><?php echo'<span title="'.$r_cxpa['enti_direccion'].'">'.$r_cxpa['enti_descri'].'</span>';?></td>
   	<td align="right"><?php if($r_cxpa['tsaldoi']<0){echo '<font color="red">';} ?> <?php echo number_format($r_cxpa['tsaldoi'],2);?></td>
   	<td align="right"><?php echo number_format($r_cxpa['tdebito'],2);?></td>
   	<td align="right"><?php echo '<i>'.number_format(abs($r_cxpa['tcredito']),2);?></td>
   	<td align="right"><?php if($r_cxpa['tsaldo']<0){echo '<font color="red">';} ?> <?php echo number_format($r_cxpa['tsaldo'],2);?></td>
	<td align="center"><?php echo'<span title="'.$r_cxpa['tpro'].'">'.$r_cxpa['tpro_descri'].'</span>';?></td>
</tr>
<?php }?>
	
<?php if ($xfrepo=='R'){ ?>
<tr class="filax" id="<?php echo $r_ped['idnum1']; ?>" align="center" valign="" style=" <?php if($filax>=4){echo 'border-top:dotted 1px #000;';$filax=1;}else{$filax++;} ?>" >
	<?php if ($r_cxpa['enti']!=$lenti){ ?>
	<tr class="filax" valign="bottom" height="30px" style="font-weight:bold;">
<td align="center">
<a href="#" onClick="imprimirlista('<?php echo $r_cxpa['enti']; ?>')" target="_blank"><img src="../img/print2.png" width="20"  title="Imprimir  <?php echo $r_cxpa['enti_descri'];?>"  />
</a>
</td>
 <td align="center"><?php echo'<span title="'.$r_cxpa['enti_zsub'].'">'.$r_cxpa['zsub_descri'].'</span>';?></td>
	<td align="center">
		<a href="#" onClick="cxppro1d('<?php echo $r_cxpa['enti']."','".$xfrepo;?>',this);"><?php echo '<span title="'.$r_cxpa['enti'].'">'.$r_cxpa['enti'].'</span>';?></a>
		</td>
	<td align="left"><?php echo'<span title="'.$r_cxpa['enti_direccion'].'">'.$r_cxpa['enti_descri'].'</span>';?></td>
	</tr>
	<?php $lenti=$r_cxpa['enti']; ?>
	<?php } ?>
	
  <td align="center">
    <a href="../lib/celectop1.php?docu=<?php echo $r_cxpa['docum']; ?>&idnum=<?php echo $r_cxpa['idnum0']; ?>&formato=1" target="_blank">
		<img src="../img/print2.png" width="20"  title="Imprimir  <?php echo $r_cxpa['docum'];?>-<?php echo $r_cxpa['numer'];?>"  /></a>
	</td>
	<td align="center" valign="" >
		<a href="#" onClick="cxppro1d('<?php echo $r_cxpa['enti']."','".$r_cxpa['docuenti2'];?>',this);"><?php echo '<span title="'.$r_cxpa['enti'].'">'.$r_cxpa['docum'].'-'.$r_cxpa['ptonumer'].'</span>';?></a>
		</td>
		<td align="center" valign="" > <?php echo $r_cxpa['fechadmy'];?></td>
		<td align="left" valign="" style="max-width:100px; white-space:pre-line;">
			<span title="<?php echo str_replace(' /','&#13',$r_cxpa['concepto']);?>"><?php echo str_replace('|',' |',$r_cxpa['concepto']);?></span>
		</td>
   	<td align="right" valign="" ><?php echo number_format($r_cxpa['tdebito'],2);?></td>
   	<td align="right" valign="" ><?php echo '<i>'.number_format(abs($r_cxpa['tcredito']),2);?></td>
   	<td align="right" valign="" ><?php if($r_cxpa['tsaldo']<0){echo '<font color="red">';} ?> <?php echo number_format($r_cxpa['tsaldo'],2);?></td>
		<td align="center" valign="" > <?php if($r_cxpa['tsaldo']>0 && $r_cxpa['diasv']>0){echo '<font color="red">';} ?><?php echo $r_cxpa['vencedmy'];?></td>
		<td align="center" valign="" > <?php echo $r_cxpa['diasv'];?></td>
		<td align="center" valign="" >
			<?php if($r_cxpa['tsaldo']>0){ ?>
<!-- 			<a target="_blank" href="../lib/vrpago0.php?idnum=<?php echo $r_cxpa['idnum0'];?>&numero=0" title="Cobrar <?php echo $r_cxo['docum'];?>-<?php echo $r_cxpa['numer'];?>"> -->
			<a target="_blank" href="../lib/vrpago0.php?docu=<?php echo $r_cxpa['docu'];?>&idnum=<?php echo $r_cxpa['idnum0'];?>&numero=0" title="Pagar <?php echo $r_cxo['docum'];?>-<?php echo $r_cxpa['numer'];?>">
			<img src="http://vcrxol.com/_img/ico_cash.png" /></a>
			<?php } ?>
		</td>
	</tr>
<?php } ?>
	
<?php if ($xfrepo=='A'){ ?>
<?php if ($r_cxpa['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_cxpa['enti']; ?>')" target="_blank"><img src="../img/print2.png" width="20"  title="Imprimir  <?php echo  $r_cxpa['enti_descri'];?>"  /></a>
</td>
<td align="center"><?php echo'<span title="'.$r_cxpa['enti_zsub'].'">'.$r_cxpa['zsub_descri'].'</span>';?></td>
<td align="center">
	<a href="#" onClick="cxppro1d('<?php echo $r_cxpa['enti']."','".$xfrepo;?>',this);"><?php echo '<span title="'.$r_cxpa['enti'].'">'.$r_cxpa['enti'].'</span>';?></a>
</td>
<td align="left"><?php echo'<span title="'.$r_cxpa['enti_direccion'].'">'.substr($r_cxpa['enti_descri'],0,30).'</span>';?></td>
<?php $lenti=$r_cxpa['enti']; ?>

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

	if($r_cxpa['anti']==999)
	{
		$sal999+=$r_cxpa['tsaldo'];
		$tsal999+=$r_cxpa['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>
