<?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');
	if (mb_stristr('gnya',$_SESSION['user_vcrx_empr']))
	{$fini1=date('2014-11-01');}


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


$sql_clie='select * from enti00 where enti_codigo like "%'.$_GET['enti'].'%" ';
$res_clie=mysql_query($sql_clie,$link_vcrx);



$sql_cxpa='select *
	from cxpa12 where ';

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

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['fenti']) && !empty($_GET['fenti']))
{
	$sql_cxco.=' and  (enti like "'.$_GET['fenti'].'%" or enti_descri like "%'.$_GET['fenti'].'%" ) ';
	$xfenti=$_GET['fenti'];
}


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['ftpro']) && !empty($_GET['ftpro']))
{
	$sql_cxpa.=' and  (enti_tpro like "'.$_GET['ftpro'].'%" or tpro_descri like "%'.$_GET['ftpro'].'%" ) ';
	$xftpro=$_GET['ftpro'];
}

/*
$xfcodi='1';
if (mb_stristr('elco',substr($_SESSION['user_vcrx_empr'],0,4)))
{
$xfcodi='2';
}

if(isset($_GET['fcodi']) && !empty($_GET['fcodi']))
{
	$xfcodi=$_GET['fcodi'];
}
switch ($xfcodi)
{
	case '1': $enti_codi='enti';break;
	case '2': $enti_codi='enti_codigov';break;
	case '3': $enti_codi='enti';break;
	default : $enti_codi='enti';break;
}
*/

$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_tpro,enti';

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

//echo $sql_cxpa;

$res_cxpa=mysql_query($sql_cxpa,$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:1150px; 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="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>


<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:m:s');?></td>
</tr>
</table>
<!----++++++++++------>
</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">
  <th>DOCU</th>
  <th>NUMERO</th>
  <th>FECHA</th>
  <th>DEBITO</th>
  <th>CREDITO</th>
  <th>SALDO</th>
  <th>.</th>  
  <th>CONCEPTO</th>  
  <th>ST</th>  
  
</tr>


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

while($r_cxpa=mysql_fetch_assoc($res_cxpa)) 
{
?>






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

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

  ?>


<?php

$xsaldo0=1/100;

if($r_cxpa['fecha']>=$fini)
{
?>


<tr style=" <?php if($aux_f!=3){$aux_f++;}else{echo 'border-bottom:dotted 1px #000000;'; $aux_f=0;}?> ">
  <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"><?php if($r_cxpa['debito']!=0){echo number_format($r_cxpa['debito'],2);} ?></td>
  <td align="right"><?php if($r_cxpa['credito']!=0){echo '<i>'.number_format($r_cxpa['credito'],2);} ?></td>
  <td align="right"><?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"><?php echo number_format($tdebito,2); ?></th>
<th align="right"><?php echo number_format($tcredito,2); ?></th>
<th align="right"><?php echo number_format($tdebito-$tcredito,2); ?></th>
</tr>

</table>




</div>
</div>

</body>
</html>

<?php //}?>