<?php
session_start();
include("cls_conexion.php");$CNN=new ClsMySQL();include("conexion23.php");
if(strlen($_SESSION['SYS_USER_ID'])==0){JsDir("login.php");	return;}
//-------------------------	
$VP_RegXPag=50000;
//-------------------------
$StrHW=$_SESSION['SYS_HW'];
$StrPag=V($_POST['TxtPag']);	
//-------------------------
$StrId=V($_POST['TxtId']);
$StrCmd=V($_POST['TxtAc']);
//-------------------------
$StrPro=V($_POST['TxtPro']);		
$StrMon=V($_POST['CboMon']);	
//-------------------------
if($StrCmd=="F"){
	$StrPag="0";
}
//-------------------------
if(strlen($StrMon)==0) $StrMon="SOLES";	
//-------------------------
$StrMon_=substr($StrMon,0,1);
$StrPro_=$StrPro."%";
//
ejt("DELETE FROM temp_edc WHERE hw='$StrHW'");
$s="select id_cliente FROM ventas ".
   "WHERE id_tip_pag='CR' AND ".
   "mon='$StrMon_' AND anu='N' GROUP BY id_cliente";
$result = grs($s);				
while ($row = $result->fetch_row()){
	$IC=$row[0];
	$TOT=gn("SUM(tot)","ventas",
			"id_cliente=$IC AND ".
			"anu='N' AND id_tip_pag='CR' AND mon='$StrMon_'");
	$PAE=gn("SUM(total)","cobros","id_cliente=$IC AND anu='N' AND mon='$StrMon_'");			
	$TOT=round($TOT - $PAE,2);
	$s="INSERT INTO temp_edc VALUES ('$StrHW',$IC,$TOT)";
	ejt($s);
}
//-------------------------
$VP_RegXPag=50000;	
$LStrTR=gn("count(*)","vcobros",$w);
$LStrPag=PagGet($LStrPag,$StrCmd,$LStrTR,$VP_RegXPag);	
$TI=0;

$IdUsu=$_SESSION['SYS_USER_ID'];
$AT=gs("at","usuarios","id_usuario=$IdUsu");
?>

<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Resumen de saldo pendiente por cobrar</title>
<!----------------------------------------->
<?php AddJs(); ?>
<script type="text/javascript" src="ja/funciones.js"></script>

<!----------------------------------------->
<link rel="stylesheet" type="text/css" href="css/estiloss.css"  />
<link rel="stylesheet" type="text/css" href="css/basic.css"  />
<link rel="stylesheet" type="text/css" href="css/mnu.css"  />
<!----------------------------------------->
<style type="text/css">
.cF {height:600px;width:1250px;position:relative;top:0px}
.mM {height:34px;width:1250px;position:relative;top:0px;background-color:#09C; z-index:500;}

</style>

<!----------------------------------------->
<script>
$(document).ready(function(){  
   Inicializar();
   jF("TxtPro");
});

function fGenPDF(){	
	document.getElementById("TxtAc").value="GEN_PDF";
	document.getElementById("Frm").submit();
}


$(document).keydown(function(e){
	var AT=gv("TxtAT");

	if(e.which==66 && e.altKey){
		if(AT=="1"){	
		document.getElementById("CmdB").click();
		return;
		}
	}

	if(e.which==86 && e.altKey){
		if(AT=="1"){
		document.getElementById("CmdZ").click();
		return;
		}
	}
  
});
</script>

<script language="javascript">
$(document).ready(function() {
	$(".botonExcel").click(function(event) {
		$("#datos_a_enviar").val( $("<div>").append( $("#Exportar_a_Excel").eq(0).clone()).html());
		$("#FormularioExportacion").submit();
});
});
</script>
<!----------------------------------------->

<body  <?php echo $LStrMsg;?>  >
<div align="center">

<div class="mM" align="left" style="font-size:12px">
<?php
$HTM=file_get_contents("http://inversionessounduniversal.com/ventas/phpmnu.php?USER_ID=$IdUsu");
echo $HTM;
?>
</div>

<div Id="DivFRM"  class="cF" style="background-color:#FFFFFF;" >
<form id="Frm" name="Frm" method="post" action="rpt_saldo.php" style="margin:0px; padding:0px; display:inline;" onSubmit="return false;">

<!----------------------------------------->
<div id="DivT" class="dT"  style="width:1250px;">
<img src="b.png" class="iT" />
<div class="tT">SALDO PENDIENTE POR COBRAR</div>
</div>

<!----------------------------------------->

<div class="s1" style="left: 35px; top: 121px; width: 100px;">CLIENTE</div>
<input name="TxtPro" type="text" id="TxtPro" 
class="lt" style="position: absolute; left: 35px; top: 138px; width: 476px;"
value="<?php echo $StrPro;?>"  	
tabindex="<?php echo ++$TI;?>"/>


<div class="s1" style="left: 529px; top: 121px; width: 81px;">MONEDA</div>
<select name="CboMon" id="CboMon" 
class="lt" style="position:absolute; left: 525px; top: 138px; width: 109px;"
tabindex="<?php echo ++$TI;?>">
<?php echo FCBO_("mon_nombre","monedas","id_moneda<>'X'",$StrMon,"","");?>
</select>


<!----------------------------------------->
<div id="DivBtn" align="left" style="position:absolute; left: 33px; top: 71px; width: 551px; height: 26px;">
<input name="CmdB" type="button" id="CmdB" class="btm"
value="Buscar" onClick="fFiltrar();"  
tabindex="<?php echo ++$TI;?>" />

<input name="CmdPDF" type="button" id="CmdPDF" class="btm" 
value="Generar PDF" onClick="fGenPDF();"
tabindex="<?php echo ++$TI;?>" />
</div>
    
<!----------------------------------------->

<div class="datagrid" style="position: absolute; left: 35px; top: 165px; width: 1180px; height: 416px; overflow: auto">
<table id="Exportar_a_Excel" width="100%">
<thead>
<tr>
<td colspan="2" align="center"><?php echo PagBtn($LStrTR,$VP_RegXPag,$LStrPag);?></td>
</tr>

<tr>
<th width="1028" style="text-align:left; background:#999">CLIENTE</th>
<th width="100" style="text-align:right; background:#999">TOTAL</th>

</tr>
</thead>
<tbody>

<?php
	$W="HW='$StrHW' AND CL LIKE '$StrPro_' AND SA>0 ";
		
	$TOT=0;	  		
	
	$ss="SELECT CL,SA FROM vis_temp_edc 
	WHERE $W ORDER BY SA DESC";
	
	
	$result = grs($ss);				
	while ($row = $result->fetch_row()){
		?>	
		<tr>
		<!----------------------------------------------->
		<td><?php echo $row[0];?></td>  
		<td style="text-align:right"><?php echo NF2($row[1],2);?></td>
		<!----------------------------------------------->
		</tr>
		<?php
		$TOT=$TOT + $row[1];
	}

?>		  
<tr>
    <td height="17" align="right"  bgcolor="#FFFFFF">TOTAL</td>
    <td bgcolor="#FFFFFF" align="right" ><?php echo NF2($TOT,2);?></td>
</tr>

</tbody>
</table>
</div>

<!----------------------------------------->    
<?php include("ocultos.php");?>

</form>

<form action="ficheroExcel.php" 
    method="post" target="_blank" id="FormularioExportacion" >
    
    <div style="position: absolute; left: 220px; top: 71px; width: 45px; height: 15px;">  
    <input name="" type="button" class="botonExcel btm" value="Exportar"  />
    <input type="hidden" id="datos_a_enviar" name="datos_a_enviar" />
    </div>
</form>

<?php
if($StrCmd=="GEN_PDF"){
	$_PDF=GEN_PDF($W,$StrPro,$StrMon);
	$StrDESCARGAR=$_PDF;
}

function GEN_PDF($W,$StrPro,$StrMon){
$_CxL=90;
$_S=10;	

$pdf = new PDF('P','mm',A4);
$pdf->AddPage();$pdf->SetFont('courier','',10);
$pdf->Cell(0,4,"",0,1);	
$pdf->Cell(0,4,"REPORTE SALDO PENDIENTE POR COBRAR",0,1);	
$pdf->Cell(0,4,"",0,1);	
//------------------------------------------

if(strlen($StrPro)>0){
$_ENC=vbl("CLIENTE".vbfill(" ",15),15)." ".vbl(":".vbfill(" ",1),1);
$_ENC=$_ENC." ".vbl($StrPro.vbfill(" ",30),30);
$pdf->Cell(0,4,$_ENC,0,1);	
}

if(strlen($StrMon)>0){
$_ENC=vbl("MONEDA".vbfill(" ",15),15)." ".vbl(":".vbfill(" ",1),1);
$_ENC=$_ENC." ".vbl($StrMon.vbfill(" ",30),30);
$pdf->Cell(0,4,$_ENC,0,1);	
}


$pdf->Cell(0,4,"",0,1);	

//------------------------------------------
$pdf->Cell(0,4,vbfill("-",$_CxL),0,1);
$_ENC=vbl("CLIENTE".vbfill(" ",79),79);
$_ENC=$_ENC." ".vbr(vbfill(" ",10)."TOTAL",10);

$pdf->Cell(0,4,$_ENC,0,1);	
$pdf->Cell(0,4,vbfill("-",$_CxL),0,1);	
//
$s="SELECT CL,SA FROM vis_temp_edc 
WHERE $W ORDER BY SA DESC";

$result = grs($s);
while ($row = $result->fetch_row()){
	$_ENC=vbl($row[0].vbfill(" ",79),79);
	$_ENC=$_ENC." ".vbr(vbfill(" ",10).NF2($row[1],1),10);
	$pdf->Cell(0,4,$_ENC,0,1);	
	$TOT=$TOT+$row[1];
}
$pdf->Cell(0,4,vbfill("-",$_CxL),0,1);	

$_TI=vbr(vbfill(" ",75)."TOTAL :",75);
$_TD=vbr(vbfill(" ",14).NF2($TOT,1),14);
$pdf->Cell(0,4,$_TI." ".$_TD,0,1);	

$_S=20;
$pdf->Cell(0,4,"",0,1);	
$pdf->Cell(0,4,"",0,1);	
//
$PDF_NAME="Saldo pendiente por cobrar.pdf";
$pdf->Output($PDF_NAME);		
return $PDF_NAME;
}
?>

<?php
if(strlen($StrDESCARGAR)>0){
echo "<div id=\"layer51\" 
	  style=\"position:absolute; left: 223px; top: 102px; 
	  width: 88px; background-color: #FFCC00; layer-background-color: #FFCC00; 
	  border: 1px none #000000; height: 17px;\">";
echo "<div align=\"center\" class=\"Estilo2\"> ";
echo " <a href=\"$StrDESCARGAR\" download>Desc.PDF</a> ";
echo "</div>";
echo "</div>";
}
?>

</div>
</div>
</body>
</html>
