<?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_fcxpa');
//echo '-'.$fini1.'-';

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

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


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

$sql_cxpat='select cxpa12.*,
	sum(case when fecha<"'.$xfecha1.'" then debito-abs(credito) end) as tsaldoi,
	sum(case when fecha>="'.$xfecha1.'" and fecha<="'.$xfecha2.'" then 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_cxpat.='fecha >= "'.$fini1.'" AND fecha <= "'.$xfecha2.'"';
$sql_cxpat.=' and  (enti like "'.$_GET['enti'].'%" ) ';
$sql_cxpat.=' group by enti order by enti_descri ';



$sql_cxpa='select *,sum(debito) as tdebito, sum(credito) as tcredito	from cxpa12 where ';

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

if(isset($_GET['enti']) && !empty($_GET['enti']))
{
	$sql_cxpa.=' and  (enti like "'.$_GET['enti'].'" ) ';
	
	//$xfc=$_GET['fcdad'];
}




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_cxpa.=' and  (prvc_descri like "%'.$_GET['fcdad'].'%" or enti_cdad like "'.$_GET['fcdad'].'%" or cdad_descri like "%'.$_GET['fcdad'].'%"  ) ';
	$xfcdad=$_GET['fcdad'];
}

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

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


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

if(isset($_GET['frcdd']) && !empty($_GET['frcdd']))
{
	//$sql_cxpa.=' and  (enti like "%'.$_GET['frcdd'].'%" ) ';
	$xfrcdd=$_GET['frcdd'];
}




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



$xgroup = 'enti ';

$xforde='N';
//if (mb_stristr('gnya',$_SESSION['user_vcrx_empr']))
//{$xforde='C';}

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.='tvalor desc,enti_descri,';break;
	case 'V': $xorder.='vndd,enti_descri,';break;
	default : $xorder.='';break;
}

//$res_cxpat=mysql_query($sql_cxpa,$link_vcrx);


//$sql_cxpa.=' group by '.$xgroup;
//$sql_cxpa.=' order by '.$xorder. 'enti_zsub,enti';

?>


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

 H1.SaltoDePagina
 {
     PAGE-BREAK-BEFORE: always
 }

</style>

</head>
<body>

<?php
//echo $sql_invest;

$res_cxpat=mysql_query($sql_cxpat,$link_vcrx);
//echo $sql_cxpat.'-';

while($r_cxpat=mysql_fetch_assoc($res_cxpat))
{
	
$xenti=$r_cxpat['enti'];	

$sql_clie='select * from enti00 where enti_codigo like "'.$xenti.'" ';
$res_clie=mysql_query($sql_clie,$link_vcrx);
	
	
$sql_cxpa='select *,sum(debito) as tdebito,sum(credito) as tcredito	from cxpa12 where ';

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

	$sql_cxpa.=' and  (enti like "'.$xenti.'" ) ';
   $sql_cxpa.='group by idnum1,docu,numero,fecha ';

$sql_cxpa.=' order by fecha, signo desc, docu, ptonumero ';

//echo $sql_cxpa.'-';
//echo $sql_cxpat;

$res_cxpa=mysql_query($sql_cxpa,$link_vcrx);
?>
	
	
	
<div align="center">
<div align="center" style="width:1000px; 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($xfecha1),'d/M/Y').' - '.date_format(date_create($xfecha2),'d/M/Y');?></td>
</tr>


<tr>
	<td><b>RUC</td>
	<td><?php echo mysql_result($res_clie,0,'enti_codigo');?></td>
</tr>
<tr>
	<td><b>RAZON SOCIAL</td>
	<td><?php echo mysql_result($res_clie,0,'enti_razsoc');?></td>
</tr>
<tr>
	<td><b>NOMBRE COMERCIAL</td>
	<td><?php echo mysql_result($res_clie,0,'enti_nomcom');?></td>
</tr>
<tr>
	<td><b>MATRIZ</td>
	<td><?php echo mysql_result($res_clie,0,'enti_calle1').' /'.mysql_result($res_clie,0,'enti_callen').' /'.mysql_result($res_clie,0,'enti_calle2');?></td>
</tr>
<tr>
	<td><b>TELEFONO</td>
	<td><?php echo mysql_result($res_clie,0,'enti_telefono');?></td>
</tr>
<tr>
	<td><b>E-MAIL</td>
	<td><a href="mailto:<?php echo mysql_result($res_clie,0,'enti_email');?>?subject=<?php echo mysql_result($res_tran1,0,'docu').'-'.mysql_result($res_tran1,0,'ptonumero');?>">
	<?php echo mysql_result($res_clie,0,'enti_email');?></a>
    </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="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($r_cxpat['tsaldoi']<0){echo '<font color="red">';} ?><?php echo ''.number_format($r_cxpat['tsaldoi'],2);?></td>
   	<td align="center"><?php echo ''.number_format($r_cxpat['tdebito'],2);?></td>
   	<td align="center"><?php echo ''.number_format($r_cxpat['tcredito'],2);?></td>
   	<td align="center"><?php if($r_cxpat['tsaldo']<0){echo '<font color="red">';} ?><?php echo ''.number_format($r_cxpat['tsaldo'],2);?></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">
  <th>DOCU</th>
  <th>NUMERO</th>
  <th>FECHA</th>
  <th>DEBITO</th>
  <th>CREDITO</th>
  <th>SALDO</th>
  <th>.</th>  
  <th width="40%">CONCEPTO</th>  
  <th>ST</th>  
  
</tr>


<?php 
$aux_f=0; 
$xsaldo = 0;
$xumes=0;
	
while($r_cxpa=mysql_fetch_assoc($res_cxpa)) 
{
?>


<?php

$xsaldo0=1/100;
//  	$xsaldo=$xsaldo+$r_cxpa['debito']-abs($r_cxpa['credito']);
 	$xsaldo=$xsaldo+$r_cxpa['tdebito']-abs($r_cxpa['tcredito']);

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


  ?>

<tr style=" <?php if($aux_f!=3){$aux_f++;}else{echo 'border-bottom:dotted 1px #000000;'; $aux_f=0;}?><?php if($xumes!=$r_cxpa['fmes']){echo 'border-top:solid 1px #000000;'; $xumes=$r_inves['fmes'];}?> ">
  <td align="center"><?php echo $r_cxpa['docu']; ?></td>
  <td align="center"><?php echo $r_cxpa['ptonumero']; ?></td>
  <td align="center"><?php echo $r_cxpa['fechadmy']; ?></td>
  <td align="right">&nbsp;<?php if($r_cxpa['tdebito']!=0){echo number_format($r_cxpa['tdebito'],2);} ?></td>
  <td align="right">&nbsp;<?php if($r_cxpa['tcredito']!=0){echo '<i>'.number_format(abs($r_cxpa['tcredito']),2);} ?></td>
  <td align="right">&nbsp;<?php if($xsaldo<0){echo '<font color="red">';} ?><?php echo number_format($xsaldo,2); ?></td>
  <td></td>
  <td align="left"><?php echo $r_cxpa['concepto'].' /'.$r_cxpa['detalle']; ?></td>
  <td align="center"><?php echo $r_cxpa['status']; ?></td>


</tr>
<?php
}
?>

<?php }?>


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

</table>




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

