<?php
	session_start();
	include("cls_conexion.php");$CNN=new ClsMySQL();include("conexion23.php");
	//if(strlen($_SESSION['SYS_USER_ID'])==0){JsDir("login.php");	return;}
	$StrId=V($_POST['TxtId']);
	$StrCmd=V($_POST['TxtAc']);
	//-------------------------
	//$StrAlm=V($_POST['CboAlm']);		
	$StrCom=V($_POST['CboCom']);		
	$StrSer=V($_POST['TxtSer']);		
	$StrNum=V($_POST['TxtNum']);		
	$StrDex=V($_POST['TxtDex']);		
	$StrHax=V($_POST['TxtHax']);			
	$StrPro=V($_POST['TxtPro']);			
	//-------------------------	
	$StrAlm="%";
	if(strlen($StrCom)==0) $StrCom="TODOS";	
	//-------------------------
	if($StrCom=="TODOS"){
		$StrCom_="%";
	}else{
		$StrCom_=substr($StrCom,0,2);
	}
	$StrNum_=$StrNum."%";
	$StrSer_=$StrSer."%";
	if(strlen($StrDex)==0){
		$StrDex_="2000-01-01";
	}else{
		$StrDex_=ToDate($StrDex);
	}
	if(strlen($StrHax)==0){
		$StrHax_="2020-12-01";
	}else{
		$StrHax_=ToDate($StrHax);
	}
	$StrPro_=$StrPro."%";
	//-------------------------
	$FF="id>0 AND tienda LIKE '$StrAlm' AND doc LIKE '$StrCom_' AND
		 num LIKE '$StrNum_' AND ser LIKE '$StrSer_' AND 
		 fec >= '$StrDex_' AND fec <= '$StrHax_' AND cliente LIKE '$StrPro_' ";
	//-------------------------
	$LStrTR=gn("count(*)","historial",$FF);	
	$LStrPag=V($_POST['TxtPag']);	
	if($StrCmd=="F"){$LStrPag="1";}
	$VP_RegXPag=50000;	
	$LStrPag=PagGet($LStrPag,$StrCmd,$LStrTR,$VP_RegXPag);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Historial</title>
<script type="text/javascript" src="funciones.js"></script>
<script src="jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="jquery.maskedinput.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
$("#TxtDex").mask("99-99-99");
$("#TxtHax").mask("99-99-99");
});


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

</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>
<!--CSS_NEW------------------------------------->
<link rel="stylesheet" type="text/css" href="estiloss.css"  />
<link rel="stylesheet" type="text/css" href="basic.css"  />
<!--CSS_NEW------------------------------------->
<style type="text/css">
<!--
body {
	background-color: #CCC;
}
#Layer1 {
	position:absolute;
	left:345px;
	top:115px;
	width:123px;
	height:35px;
	z-index:1;
}
.centrarFrm{
	width:1024px;
	height:600px;
	position:absolute;
	left:50%;
	top:50%;
	background-color:#006699;
	margin:-300px 0 0 -512px;
}
#Layer2 {
	position:absolute;
	left:409px;
	top:205px;
	width:82px;
	height:24px;
	z-index:1;
}
.Estilo1 {
	font-family: Arial, Helvetica, sans-serif;
	font-weight: bold;
	color: #FFFFFF;
}
.Estilo2 {font-family: Arial, Helvetica, sans-serif; font-weight: bold; color: #FFFFFF; font-size: 12px; }
.Estilo3 {color: #FFFFFF; font-size: 12px; font-family: Arial, Helvetica, sans-serif;}
.Estilo4 {color: #000099}
.Estilo5 {color: #000000}

-->
</style>

</head>
<body >
<div Id="DivFRM"  class="centrarFrm" >
<form id="Frm" name="Frm" method="post" action="historial.php">
<input name="TxtAc" id="TxtAc" type="hidden" value="" />
<input name="TxtPag" id="TxtPag" type="hidden" value="<?php echo $LStrPag; ?>" />
<center> 

<div id="layer" style="position:absolute; left: 79px; top: 30px; width: 412px; height: 24px;">
<div align="left" class="Estilo1">HISTORIAL</div></div>

<div id="LblTit" style="position:absolute; left: 20px; top: 25px; width: 64px;">
<div align="left" class="Estilo1"><img src="img/buscar.png" width="57" height="60" /></div></div>

<div class="s1" style="left: 24px; top: 122px; width: 102px;">COMPROBANTE</div>
<div class="s1" style="left: 279px; top: 122px; width: 46px;">SERIE</div>
<div class="s1" style="left: 333px; top: 122px; width: 74px;">NUMERO</div>
<div class="s1" style="left: 405px; top: 122px; width: 45px;">DESDE</div>
<div class="s1" style="left: 484px; top: 122px; width: 60px;">HASTA</div>
<div class="s1" style="left: 561px; top: 122px; width: 100px;">CLIENTE</div>


<select name="CboCom" id="CboCom" style="left: 24px; top: 140px; width:250px" 
onkeydown="return SoloTextoEx(event,'TxtSer');" 
tabindex="1"><?php echo FCBO_("com_nombre","comprobantes","id_comprobante>0",$StrCom,"S","");?>
</select>

<input name="TxtSer" type="text" id="TxtSer" style="left: 279px; top: 140px; width:45px" 
value="<?php echo $StrSer;?>"  
onkeydown="return SoloTextoEx(event,'TxtNum');"
tabindex="2" />
      
<input name="TxtNum" type="text" id="TxtNum" style="left: 334px; top: 140px;width:60px" 
value="<?php echo $StrNum;?>"  
onkeydown="return SoloTextoEx(event,'TxtDex');"
tabindex="3" />

<input name="TxtDex" type="text" id="TxtDex" style="left: 405px; top: 140px; width:65px" 
value="<?php echo $StrDex;?>"  
onkeydown="return SoloFec(this,event,'TxtHax');"
tabindex="4" />

<input name="TxtHax" type="text" id="TxtHax" style="left: 484px; top: 140px; width:65px" 
value="<?php echo $StrHax;?>"  
onkeydown="return SoloFec(this,event,'TxtPro');"
tabindex="5" />

<input name="TxtPro" type="text" id="TxtPro" style="left: 559px; top: 140px; width:260px" 
value="<?php echo $StrPro;?>"  
onkeydown="return SoloTextoEx(event,'CmdB');" 
tabindex="6" />

<input name="CmdB" type="button" id="CmdB" style="position:absolute; left: 828px; top: 140px; width: 57px;"
value="Filtrar" onclick="fFiltrar();"  
tabindex="7"/>




<div class="s1" style="left: 79px; top: 50px; width: 172px; height: 28px;">

        <input name="CmdV" type="button" id="CmdV" 
        value="Volver" onclick="fVolver('menu.php');" />

<input name="CmdPDF" type="button" id="CmdPDF" 
value="Generar PDF" onclick="fGenPDF();"  
		<?php
		/*if(strlen($StrId)==0){
			echo "disabled=\"disabled\"";
		}*/
		?>
tabindex="38"/>

</div>

<div id="layer3" style="position:absolute; left: 23px; top: 169px; width: 982px; height: 405px; overflow:auto">
      <div align="left" class="Estilo2">
        <table id="Exportar_a_Excel" width="966" height="19" border="1" cellpadding="0" cellspacing="0">
<tr  >
<td height="17" colspan="10" bgcolor="#666666" align="center">
<?php //echo PagBtn($LStrTR,$VP_RegXPag,$LStrPag);?>
</td>
</tr>
<tr>
<td width="46" bgcolor="#999999"><span class="Estilo4">ALM</span></td>
<td width="51" bgcolor="#999999"><span class="Estilo4">COM</span></td>
<td width="88" bgcolor="#999999"><span class="Estilo4">NUMERO</span></td>
<td width="78" bgcolor="#999999"><span class="Estilo4">FECHA</span></td>
<td width="156" bgcolor="#999999"><span class="Estilo4">CLIENTE</span></td>            
<td width="94" bgcolor="#999999"><span class="Estilo4">CODIGO</span></td>            
<td width="238" bgcolor="#999999"><span class="Estilo4">DESCRIPCION</span></td>            
<td width="41" bgcolor="#999999" align="right"><span class="Estilo4">CAN</span></td>
<td width="40" bgcolor="#999999" align="right"><span class="Estilo4">PRE</span></td>
<td width="112" bgcolor="#999999" align="right"><span class="Estilo4">IMPORTE</span></td>

</tr>
<?php		  		

	$s="SELECT id,tienda,doc,CONCAT(ser,'-',num),DATE_FORMAT(fec,'%d-%m-%y'),cliente,
		 prod_cod,prod_des,can,FORMAT(pre,1),FORMAT(importe,1)
		 FROM historial WHERE ".$FF." ORDER BY id ";

	$X1="<td bgcolor=\"#FFFFFF\"><span class=\"Estilo5\">";
	$X1B="<td bgcolor=\"#FFFFFF\" align=\"right\"><span class=\"Estilo5\">";
	$X2="</span></td>";
	//$s=PagSql($s,$LStrPag,$VP_RegXPag,$LStrTR);	
	$result = grs($s);				
	while ($row = $result->fetch_row()){
		echo "<tr >";
		echo $X1.$row[0].$X2;
		echo $X1.$row[2].$X2;
		echo $X1."'".$row[3].$X2;
		echo $X1.$row[4].$X2;
		echo $X1.$row[5].$X2;
		echo $X1.$row[6].$X2;
		echo $X1.$row[7].$X2;
		echo $X1.$row[8].$X2;
		echo $X1B.$row[9].$X2;
		echo $X1B.$row[10].$X2;
		echo "</tr>";
		
		$ST=$ST+$row[10];
	}

?>		  
<tr>
<td colspan="9" bgcolor="#999999" align="right"><span class="Estilo4">TOTAL</span></td>
<td width="112" bgcolor="#999999" align="right"><span class="Estilo4"><?php echo NF2($ST,1);?></span></td>

</tr>

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


  
</center>	
</form>

<form action="ficheroExcel.php" 
method="post" target="_blank" 
id="FormularioExportacion" >

<div 
style="position:absolute; left: 231px; top: 50px; width: 45px; height: 15px;">  
<input name="" type="button" class="botonExcel" value="Exportar" />
<input type="hidden" id="datos_a_enviar" name="datos_a_enviar" />
</div>

</form>
<?php
if($StrCmd=="GEN_PDF"){
	$_PDF=GEN_PDF($FF,$StrCom,$StrSer,$StrNum,$StrDex,$StrHax,$StrPro);
	$StrDESCARGAR=$_PDF;
}
//
function GEN_PDF($FF,$StrCom,$StrSer,$StrNum,$StrDex,$StrHax,$StrPro){
$_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 DE HISTORIAL",0,1);	
$pdf->Cell(0,4,"",0,1);	
//------------------------------------------

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

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

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

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

if(strlen($StrHax)>0){
$_ENC=vbl("HASTA".vbfill(" ",15),15)." ".vbl(":".vbfill(" ",1),1);
$_ENC=$_ENC." ".vbl($StrHax.vbfill(" ",30),30);
$pdf->Cell(0,4,$_ENC,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);	
}
$pdf->Cell(0,4,"",0,1);	

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

$_ENC=vbl("COM".vbfill(" ",3),3);
$_ENC=$_ENC." ".vbl("NUMERO".vbfill(" ",11),11);
$_ENC=$_ENC." ".vbl("FECHA".vbfill(" ",8),8);
$_ENC=$_ENC." ".vbl("CLIENTE".vbfill(" ",13),13);
$_ENC=$_ENC." ".vbl("CODIGO".vbfill(" ",16),16);
$_ENC=$_ENC." ".vbl("DESCRIPCION".vbfill(" ",13),13);
$_ENC=$_ENC." ".vbr(vbfill(" ",4)."CAN",4);
$_ENC=$_ENC." ".vbr(vbfill(" ",4)."PRE",4);
$_ENC=$_ENC." ".vbr(vbfill(" ",10)."IMPORTE",10);

$pdf->Cell(0,4,$_ENC,0,1);	
$pdf->Cell(0,4,vbfill("-",$_CxL),0,1);	
//
$s="SELECT id,tienda,doc,CONCAT(ser,'-',num),DATE_FORMAT(fec,'%d-%m-%y'),cliente,
	prod_cod,prod_des,can,FORMAT(pre,1),FORMAT(importe,1)
	FROM historial WHERE ".$FF." ORDER BY id ";

$result = grs($s);
while ($row = $result->fetch_row()){
	$_ENC=vbl($row[2].vbfill(" ",3),3);
	$_ENC=$_ENC." ".vbl($row[3].vbfill(" ",11),11);
	$_ENC=$_ENC." ".vbl($row[4].vbfill(" ",8),8);
	$_ENC=$_ENC." ".vbl($row[5].vbfill(" ",13),13);
	$_ENC=$_ENC." ".vbl($row[6].vbfill(" ",16),16);
	$_ENC=$_ENC." ".vbl($row[7].vbfill(" ",13),13);
	$_ENC=$_ENC." ".vbr(vbfill(" ",4).$row[8],4);
	$_ENC=$_ENC." ".vbr(vbfill(" ",4).$row[9],4);
	$_ENC=$_ENC." ".vbr(vbfill(" ",10).$row[10],10);

	$pdf->Cell(0,4,$_ENC,0,1);	
	$TOT=$TOT+$row[10];
}
$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="Historial.pdf";
$pdf->Output($PDF_NAME);		
return $PDF_NAME;
}
?>

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

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