<?php
ini_set("session.cookie_lifetime",60*60*10);
ini_set("session.gc_maxlifetime", 60*60*10);
session_start();
include("cls_conexion.php");$CNN=new ClsMySQL();include("conexion23.php");
if(strlen($_SESSION['SYS_USER_ID'])==0){JsDir("login.php");	return;}
//
if(vbval($_SESSION['SYS_USER_GI'])!=USU_GET_GI($_SESSION['SYS_USER_ID'])){
	JsDir("cerrar.php");	
	return;
}
//
$StrId=V($_POST['TxtId']);
$StrCmd=V($_POST['TxtAc']);
//
$StrCod=V($_POST['TxtCod']);		
$StrDes=V($_POST['TxtDes']);
$StrLin=V($_POST['CboLin']);
//
$STR_GENERAR="";
if(strlen($StrLin)==0)$StrLin="TODOS";
//
$StrCodT=$StrCod."%";
$StrDesT=$StrDes."%";
$StrLinT=$StrLin;
if($StrLinT=="TODOS") $StrLinT="%";
$FF="ID>0 AND COD LIKE '$StrCodT'  AND 
	 DESCRIPCION LIKE '$StrDesT' AND 
	 LINEA LIKE '$StrLinT' AND USO='SI'";
	 
$LStrTR=gn("count(*)","vproductos",$FF);
$LStrPag=V($_POST['TxtPag']);	
if($StrCmd=="F"){$LStrPag="1";}
$VP_RegXPag=50000;	
$LStrPag=PagGet($LStrPag,$StrCmd,$LStrTR,$VP_RegXPag);
//-------------------------
$LStrFoc="document.getElementById('TxtCod').focus();";
$LStrMsg="onload=\"$LStrFoc;\"";
?>
<!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=utf-8" />
<title>Generar plantilla para actualizacion de precios de venta</title>
<script type="text/javascript" src="fe/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");
	jFocus("TxtCod");
});

function fGenPDF(){	
	document.getElementById("TxtAc").value="GEN_PDF";
	document.getElementById("Frm").submit();
}
function fGENERAR(){	
	document.getElementById("TxtAc").value="GEN_PLA";
	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:1000px;
	height:600px;
	position:absolute;
	left:<?php echo "50%";?>;
	top:<?php echo "50%";?>;
	background-color:#006699;
	margin:<?php echo "-300px";?> 0 0 <?php echo "-500px";?>;	
}
#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;
}
.f2 {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;}
.s4 {color: #000099}
.Estilo5 {color: #000000}
#Frm center #DivFRM #layer3 .f2 table tr td {
	color: #000;
	text-align: left;
}

-->
</style>

</head>
<body  >
<div Id="DivFRM"  class="centrarFrm" >
<form id="Frm" name="Frm" method="post" action="pre_plantilla.php">
<center>
<div class="s1" style="left: 42px; top: 561px; width: 744px;">Para separar los decimales utilice el punto (.)</div>

<div class="s1" style="left: 43px; top: 114px; width: 61px;">CODIGO</div>
<input name="TxtCod" type="text" id="TxtCod" style="left: 43px; top: 129px; width:115px" 
value="<?php echo $StrCod;?>"  
onkeydown="return SoloTextoEx(event,'TxtDes');"
tabindex="2" />

<div class="s1" style="left: 173px; top: 114px; width: 236px;">DESCRIPCION</div>
<input name="TxtDes" type="text" id="TxtDes" style="left: 170px; top: 129px; width: 314px;"
value="<?php echo $StrDes;?>"  
onkeydown="return SoloTextoEx(event,'CboLin');"
tabindex="3" />

<div class="s1" style="left: 492px; top: 113px; width: 94px;">LINEA</div>
<select name="CboLin" id="CboLin" style="position:absolute; left: 492px; top: 129px; width: 209px;"
onkeydown="return SoloTextoEx(event,'CmdB');" 
tabindex="4"><?php	echo FCBO_("lin_nombre","lineas","id_linea>=0",$StrLin,"S","")?>  
</select>

<div class="s1" style="left: 100px; top: 50px; width: 183px; height: 28px;">
<input name="CmdV" type="button" id="CmdV" 
value="Volver" onclick="fVolver('menu.php');" />
<input type="hidden" name="TxtAc" id="TxtAc" />
<input type="hidden" name="TxtPag" id="TxtPag" value="<?php echo $LStrPag; ?>" />
<input name="CmdGEN" type="button" id="CmdGEN" 
value="Generar plantilla" onclick="fGENERAR();"  
tabindex="38"/>

</div>


<div id="layer3" style="position:absolute; left: 43px; top: 158px; width: 919px; height: 396px; overflow:auto">
<div align="left" class="f2">
<table id="Exportar_a_Excel" width="903" height="5" border="2" cellpadding="0" cellspacing="0" style="color:#000">
<tr>
<td height="18" colspan="6" bgcolor="#666666" > 
<p align="center"> <?php //echo PagBtn($LStrTR,$VP_RegXPag,$LStrPag);?></p>
</td>
</tr>
<tr>
<td width="130" height="17" bgcolor="#999999">CODIGO</td>
<td width="160" bgcolor="#999999">LINEA</td>
<td width="478" bgcolor="#999999">DESCRIPCION</td>
<td width="123" bgcolor="#999999">PRE.VEN.MIN</td>
<td width="123" bgcolor="#999999">PRE.VEN.MAX</td>
</tr>
<?php		  		
    $s="SELECT ID,COD,LINEA,DESCRIPCION as DES,PV_MIN,PV_MAX 
	    FROM vproductos WHERE $FF ORDER BY COD,DESCRIPCION ";	  					
    //$s=PagSql($s,$LStrPag,$VP_RegXPag,$LStrTR);				
    $result = grs($s);
    while ($row = $result->fetch_assoc()){
		echo "<tr>";
		echo "<td bgcolor=\"#FFFFFF\">".$row['COD']."</td>";
		echo "<td bgcolor=\"#FFFFFF\">".$row['LINEA']."</td>";
		echo "<td bgcolor=\"#FFFFFF\">".$row['DES']."</td>";
		echo "<td bgcolor=\"#FFFFFF\"  style=\"text-align:right\">".NF5($row['PV_MIN'])."</td>";
		echo "<td bgcolor=\"#FFFFFF\"  style=\"text-align:right\">".NF5($row['PV_MAX'])."</td>";
		echo "</tr>";
    }
?>
</table>

</div>
</div>

<div id="layer" style="position:absolute; left: 100px; top: 30px; width: 747px; height: 24px;">
<div align="left" class="Estilo1">GENERAR PLANTILLA PARA ACTUALIZACION DE PRECIOS DE VENTA</div>
</div>

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



<input name="CmdB" type="button" id="CmdB" 
style="position:absolute; left: 716px; top: 128px; width: 83px;"
value="Filtrar" onclick="fFiltrar();" />
<div align="left" class="f2"></div>
    


    
  
</center>	
</form>

<form action="ficheroExcel.php" 
method="post" target="_blank" 
id="FormularioExportacion" >
</form>
<?php
if($StrCmd=="GEN_PLA"){

	define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
	/** Include PHPExcel */
	require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
	// Create new PHPExcel object
	$objPHPExcel = new PHPExcel();
	// Set document properties
	$objPHPExcel->getProperties()->setCreator("SistemaOnLine")
								 ->setLastModifiedBy("SistemaOnLine")
								 ->setTitle("SistemaOnLine")
								 ->setSubject("SistemaOnLine")
								 ->setDescription("SistemaOnLine")
								 ->setKeywords("SistemaOnLine")
								 ->setCategory("SistemaOnLine");
	//
	$objPHPExcel->setActiveSheetIndex(0)
				->setCellValue('A1', 'CODIGO')
				->setCellValue('B1', 'LINEA')
				->setCellValue('C1', 'DESCRIPCION')
				->setCellValue('D1', 'PRE.MIN.ACT')
				->setCellValue('E1', 'PRE.MAX.ACT')
				->setCellValue('F1', 'PRE.MIN.NUE')
				->setCellValue('G1', 'PRE.MAX.NUE');
	//
	$F=1;						 
	$s="SELECT ID,COD,LINEA as LIN,DESCRIPCION as DES,PV_MIN ,PV_MAX
        FROM vproductos WHERE ".$FF."	ORDER BY COD,DESCRIPCION ";

    $result = grs($s);
    while ($row = $result->fetch_assoc()){		
		$F++;
		$COD=$row['COD'];
		$LIN=$row['LIN'];
		$DES=$row['DES'];
		$PM=$row['PV_MIN'];
		$PW=$row['PV_MAX'];
		$objPHPExcel->setActiveSheetIndex(0)
				->setCellValue("A".$F, $COD)
				->setCellValue("B".$F, $LIN)
				->setCellValue("C".$F, $DES)
				->setCellValue("D".$F, $PM)
				->setCellValue("E".$F, $PW);

	}
	
	// Rename worksheet
	$objPHPExcel->getActiveSheet()->setTitle('Plantilla');
	// Set active sheet index to the first sheet, so Excel opens this as the first sheet
	$objPHPExcel->setActiveSheetIndex(0);
	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
	$objWriter->save("plantilla_precios.xls");
	$STR_GENERAR="S";

}
?>
<?php
if($StrCmd=="GEN_PDF"){
	$_PDF=GEN_PDF($FF,$StrAlm,$StrCod,$StrDes,$StrMar,$CboSto);
	$StrDESCARGAR=$_PDF;
}

function GEN_PDF($FF,$StrAlm,$StrCod,$StrDes,$StrMar,$CboSto){
$StrAlm_=gn("id_almacen","almacenes","alm_obs='$StrAlm'");
$_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,"PLANTILLA PARA ACTUALIZACION DE PRECIOS DE VENTA",0,1);	
$pdf->Cell(0,4,"",0,1);	
//------------------------------------------

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

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

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

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

if(strlen($CboSto)>0){
$_ENC=vbl("STOCK".vbfill(" ",15),15)." ".vbl(":".vbfill(" ",1),1);
$_ENC=$_ENC." ".vbl($CboSto.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("CODIGO".vbfill(" ",12),12);
$_ENC=$_ENC." ".vbl("DESCRIPCION".vbfill(" ",40),40);
$_ENC=$_ENC." ".vbl("MARCA".vbfill(" ",25),24);
$_ENC=$_ENC." ".vbr(vbfill(" ",5)."P.V.B",5);
$_ENC=$_ENC." ".vbr(vbfill(" ",5)."P.V.S",5);

$pdf->Cell(0,4,$_ENC,0,1);	
$pdf->Cell(0,4,vbfill("-",$_CxL),0,1);	
//
$s="SELECT ID,COD,LINEA,DESCRIPCION,MARCA,PV_MAX 
    FROM v_stocks WHERE ".$FF."	ORDER BY DESCRIPCION "; 

$result = grs($s);
while ($row = $result->fetch_row()){
	$PRE=gn("pxs_precio","precios",
			"pxs_id_almacen=$StrAlm_ AND pxs_id_producto=$row[0]");
	$_ENC=vbl($row[1].vbfill(" ",12),12);
	$_ENC=$_ENC." ".vbl($row[3].vbfill(" ",40),40);
	$_ENC=$_ENC." ".vbl($row[4].vbfill(" ",25),24);
	$_ENC=$_ENC." ".vbr(vbfill(" ",5).NF($row[5],1),5);
	$_ENC=$_ENC." ".vbr(vbfill(" ",5).NF($PRE,1),5);
	$pdf->Cell(0,4,$_ENC,0,1);	
}

$pdf->Cell(0,4,vbfill("-",$_CxL),0,1);	
$pdf->Cell(0,4,$_TI." ".$_TD,0,1);	
$_S=20;
$pdf->Cell(0,4,"",0,1);	
$pdf->Cell(0,4,"",0,1);	
//
$PDF_NAME="PlanActPrecios.pdf";
$pdf->Output($PDF_NAME);		
return $PDF_NAME;
}
?>

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