<?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']);
	//-------------------------
	$StrONE=trim(V($_GET['CmdB']));	
	//-------------------------
	$StrAlm=V($_POST['CboAlm']);		
	$StrCod=V($_POST['TxtCod']);		
	$StrDes=V($_POST['TxtDes']);
	$StrMar=V($_POST['CboMar']);
	$CboSto=V($_POST['CboSto']);
	$STR_GENERAR="";
	if(strlen($StrMar)==0)$StrMar="TODOS";
	
	if(strlen($CboSto)==0){$CboSto="TODOS";}
	if($CboSto=="TODOS"){$StrSto="STOCK LIKE '%'";}
	if($CboSto=="MAYOR A CERO"){$StrSto="STOCK>0";}
	if($CboSto=="IGUAL A CERO"){$StrSto="STOCK=0";}
	if($CboSto=="MENOR A CERO"){$StrSto="STOCK<0";}

	
	
	if($_SESSION['SYS_USER_ALM']=="%"){
		if(strlen($StrAlm)==0)$StrAlm=GetSO("alm_nombre","almacenes","","alm_nombre");
	}else{
		$StrAlm=$_SESSION['SYS_USER_ALM'];
	}
	
	$StrCodT=$StrCod."%";
	$StrDesT=$StrDes."%";
	$StrMarT=$StrMar;
	if($StrMarT=="TODOS") $StrMarT="%";

	$FF="ID>0 
	AND ALM LIKE '$StrAlm'
	AND COD LIKE '$StrCodT'  
	AND DESCRIPCION LIKE '$StrDesT' 
	AND MARCA LIKE '$StrMarT'			  
	AND $StrSto ";

	$LStrTR=gn("count(*)","v_stocks",$FF);
	$LStrPag=V($_POST['TxtPag']);	
	if($StrCmd=="F"){$LStrPag="1";}
	$VP_RegXPag=50000;	
	$LStrPag=PagGet($LStrPag,$StrCmd,$LStrTR,$VP_RegXPag);
	//-------------------------
	$LStrFoc="document.getElementById('CboAlm').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=iso-8859-1" />
<title>Generar plantilla para actualizacion de precios de venta</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();
}
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;
}
.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;}
.s4 {color: #000099}
.Estilo5 {color: #000000}
#Frm center #DivFRM #layer3 .Estilo2 table tr td {
	color: #000;
	text-align: left;
}

-->
</style>

</head>
<body  <?php echo $StrMsg;?>>
<div Id="DivFRM"  class="centrarFrm" >
<form id="Frm" name="Frm" method="post" action="pre_plantilla.php">
<center> 
  
<div class="s1" style="left: 45px; top: 114px; width: 102px;">ALMACEN</div>
<div class="s1" style="left: 163px; top: 114px; width: 52px;">CODIGO</div>
<div class="s1" style="left: 273px; top: 114px; width: 136px;">DESCRIPCION</div>
<div class="s1" style="left: 492px; top: 113px; width: 94px;">MARCA</div>
<div class="s1" style="left: 628px; top: 113px; width: 94px;">ESTADO.STOCK</div>
<div class="s1" style="left: 43px; top: 561px; width: 331px;">*P.V.B : Precio venta base</div>
<div class="s1" style="left: 43px; top: 578px; width: 334px;">*P.V.S : Precio venta sucursal</div>
<div class="s1" style="left: 309px; top: 561px; width: 477px;">Para separar los decimales utilice el punto (.)</div>

<select name="CboAlm" id="CboAlm" style="left: 43px; top: 129px; width:110px" 
onkeydown="return SoloTextoEx(event,'TxtCod');" 
tabindex="1"><?php echo ListALM($StrAlm); ?>
</select>

<input name="TxtCod" type="text" id="TxtCod" style="left: 163px; top: 129px; width:95px" 
value="<?php echo $StrCod;?>"  
onkeydown="return SoloTextoEx(event,'TxtDes');"
tabindex="2" />

<input name="TxtDes" type="text" id="TxtDes" style="left: 271px; top: 129px; width: 213px;"
value="<?php echo $StrDes;?>"  
onkeydown="return SoloTextoEx(event,'CboMar');"
tabindex="3" />

<select name="CboMar" id="CboMar" style="position:absolute; left: 492px; top: 129px; width: 123px;"
onkeydown="return SoloTextoEx(event,'CmdB');" 
tabindex="4"><?php	echo FCBO_("mar_nombre","marcas","id_marca>=0",$StrMar,"S","")?>  
</select>

<select name="CboSto" id="CboSto" style="position:absolute; left: 625px; top: 128px; width: 162px;"
onkeydown="return SoloTextoEx(event,'CmdB');" 
tabindex="5"><?php echo FCBO_("aux_nom","auxiliar","aux_des='ST'",$CboSto,"S","");?>
</select>


<div class="s1" style="left: 100px; top: 50px; width: 291px; 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="CmdPDF" type="button" id="CmdPDF" 
value="Generar PDF" onclick="fGenPDF();"  
tabindex="38"/>
<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="Estilo2">
<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="140" height="17" bgcolor="#999999">CODIGO</td>
<td width="172" bgcolor="#999999">LINEA</td>
<td width="356" bgcolor="#999999">DESCRIPCION</td>
<td width="106" bgcolor="#999999">MARCA</td>
<td width="55" bgcolor="#999999">*P.V.B</td>
<td width="58" bgcolor="#999999">*P.V.S</td>
</tr>
<?php		  		
    $IdAlm=gn("id_almacen","almacenes","alm_nombre='$StrAlm'");		  	
    $s="SELECT ID,COD,LINEA,DESCRIPCION,MARCA,PV_MAX 
        FROM v_stocks WHERE ".$FF."	ORDER BY DESCRIPCION,MARCA ";	  					
    //$s=PagSql($s,$LStrPag,$VP_RegXPag,$LStrTR);				
    $result = grs($s);
    while ($row = $result->fetch_row()){
	$PRE=gn("pxs_precio","precios","pxs_id_almacen=$IdAlm AND pxs_id_producto=$row[0]");

		echo "<tr>";
		echo "<td bgcolor=\"#FFFFFF\">'$row[1]</td>";
		echo "<td bgcolor=\"#FFFFFF\">$row[2]</td>";
		echo "<td bgcolor=\"#FFFFFF\">$row[3]</td>";
		echo "<td bgcolor=\"#FFFFFF\" >$row[4]</td>";
		echo "<td bgcolor=\"#FFFFFF\"  style=\"text-align:right\"> $row[5]</td>";
		echo "<td bgcolor=\"#FFFFFF\"  style=\"text-align:right\"> $PRE</td>";
		echo "</tr>";

    }
?>
</table>

</div>
</div>

<div id="layer" style="position:absolute; left: 100px; top: 21px; 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: 32px; 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: 792px; top: 128px; width: 92px;"
value="Filtrar" onclick="fFiltrar();" />
<div align="left" class="Estilo2"></div>
    


    
  
</center>	
</form>

<form action="ficheroExcel.php" 
method="post" target="_blank" 
id="FormularioExportacion" >
<div 
style="position:absolute; left: 421px; 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_PLA"){

	$Alm_=gn("id_almacen","almacenes","alm_nombre='$StrAlm'");
	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', 'MARCA')
				->setCellValue('E1', 'P.V.B')
				->setCellValue('F1', 'P.V.S')
				->setCellValue('G1', 'NUEVO PRECIO');;
	//
	$F=1;						 
	$s="SELECT ID,COD,LINEA,DESCRIPCION,MARCA,PV_MAX 
        FROM v_stocks WHERE ".$FF."	ORDER BY DESCRIPCION,MARCA ";

    $result = grs($s);
    while ($row = $result->fetch_row()){

		$PRE=gn("pxs_precio","precios","pxs_id_almacen=$Alm_ AND pxs_id_producto=$row[0]");
		$F++;
		$CODIGO=$row[1];
		$CODIGO=str_replace("+","*",$CODIGO);	
		$objPHPExcel->setActiveSheetIndex(0)
				->setCellValue("A".$F, $CODIGO)
				->setCellValue("B".$F, $row[2])
				->setCellValue("C".$F, $row[3])
				->setCellValue("D".$F, $row[4])
				->setCellValue("E".$F, $row[5])
				->setCellValue("F".$F, $PRE)
				->setCellValue("G".$F, '');

	}
	
	// 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_nombre='$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,MARCA "; 

$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=\"Estilo2\"> ";
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=\"Estilo2\"> ";
echo " <a href=\"plantilla_precios.xls\" download>Descargar Plantilla</a> ";
echo "</div>";
echo "</div>";
}
?>
</div>
</body>
</html>
