<?php
	session_start();
	include("cls_conexion.php");$CNN=new ClsMySQL();include("conexion23.php");
	if(strlen($_SESSION['SYS_USER_ID'])==0){JsDir("login.php");	return;}
	$TI=0;
	$StrId=V($_POST['TxtId']);
	$StrCmd=V($_POST['TxtAc']);
	//-------------------------
	$StrONE=trim(V($_GET['CmdB']));	
	//-------------------------
	$StrAlm=V($_POST['CboAlm']);		
	$StrCod=V($_POST['TxtCod']);		
	$Fab=V($_POST['Fab']);	
	$Mod=V($_POST['Mod']);		
	$StrDes=V($_POST['TxtDes']);
	$StrMar=V($_POST['CboMar']);
	$CboSto=V($_POST['CboSto']);
	$STR_GENERAR="";
	//
	if(strlen($Fab)==0)$Fab="TODOS";
	if(strlen($Mod)==0)$Mod="TODOS";
	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."%";
	$Fab_=LIKE($Fab);
	$Mod_=LIKE($Mod);
	$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 AND FAB LIKE '$Fab_' AND MODELO LIKE '$Mod_' ";
	//-------------------------
	if($StrCmd=="G"){
		$s="SELECT ID,COD,LINEA,DESCRIPCION,MARCA,PV_MAX 
			FROM v_stocks WHERE ".$FF."	ORDER BY DESCRIPCION,MARCA ";	 					
		//$C=0;	
		$IdAlm=gn("id_almacen","almacenes","alm_nombre='$StrAlm'");
	
		$result = grs($s);
		while ($row = $result->fetch_row()){
			$IDP=$row[0];
			$NPre=V($_POST[Txt.$IDP]);
			if(strlen($NPre)>0){
				if(GetC("precios","pxs_id_almacen=$IdAlm AND pxs_id_producto=$row[0]")==0){
					$Id=GenId("id_pxs","precios");
					$Sql="INSERT INTO precios VALUES 
					($Id,$IdAlm,$row[0],$NPre)";
					ejt($Sql);
				}else{
					$Sql="UPDATE precios SET pxs_precio=$NPre 
					WHERE pxs_id_almacen=$IdAlm AND pxs_id_producto=$row[0]";
					ejt($Sql);
				}
			}
		}				
	}
	//-------------------------
	$LStrTR=gn("count(*)","v_stocks",$FF);
	$LStrPag=V($_POST['TxtPag']);	
	if($StrCmd=="F"){$LStrPag="1";}
	$VP_RegXPag=15;	
	$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>
<?php AddJs();?>
<script type="text/javascript" src="funciones.js"></script>
<script>
$(document).ready(function(){  
   Inicializar();
});
</script>

<script>
function fGenPDF(){	
	document.getElementById("TxtAc").value="GEN_PDF";
	document.getElementById("Frm").submit();
}
function fGENERAR(){	
	document.getElementById("TxtAc").value="GEN_PLA";
	document.getElementById("Frm").submit();
}


function fGuardar(){
	var Alm = document.getElementById("CboAlm").value;	
	if(confirm("Esta seguro que desea Guardar los Cambios "+Alm)==true){
		document.getElementById("TxtAc").value="G";
	}else{
		return;
	}
	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:1200px;
	height:600px;
	position:absolute;
	left:<?php echo "50%";?>;
	top:<?php echo "50%";?>;
	background-color:#006699;
	margin:<?php echo "-300px";?> 0 0 <?php echo "-600px";?>;	
}
#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;}
.sX {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_plantilla2.php">
<center> 
  





<div class="s1" style="left: 29px; top: 561px; width: 166px;">*P.V.B : Precio venta base</div>
<div class="s1" style="left: 29px; 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>

<div class="s1" style="left: 31px; top: 114px; width: 102px;">ALMACEN</div>
<select name="CboAlm" id="CboAlm" 
class="lt" style="position:absolute; left: 29px; top: 129px; width:110px" 
tabindex="<?php echo $TI++;?>">
<?php echo ListALM($StrAlm); ?>
</select>

<div class="s1" style="left: 149px; top: 114px; width: 52px;">CODIGO</div>
<input name="TxtCod" type="text" id="TxtCod" 
class="lt" style="position:absolute; left: 149px; top: 129px; width:95px" 
value="<?php echo $StrCod;?>"
tabindex="<?php echo $TI++;?>" />

<div class="s1" style="left: 253px; top: 113px; width: 99px;">
FABRICANTE</div>
<select name="Fab" id="Fab" 
class="lt" style="position:absolute; left: 253px; top: 129px; width: 144px;"
tabindex="<?php echo $TI++;?>">
<?php	echo FCBO_("fab_nom","fabricantes","id_fab>0",$Fab,"S","N")?>  
</select>

<div class="s1" style="left:403px; top: 113px; width: 99px;">
MODELO</div>
<select name="Mod" id="Mod" 
class="lt" style="position:absolute; left:403px; top: 129px; width: 144px;"
tabindex="<?php echo $TI++;?>">
<?php	echo FCBO_("mod_nom","modelos","id_mod>0",$Mod,"S","N")?>  
</select>



<div class="s1" style="left: 557px; top: 114px; width: 211px;">DESCRIPCION</div>
<input name="TxtDes" type="text" id="TxtDes" 
class="lt" style="position:absolute; left: 553px; top: 129px; width: 246px;"
value="<?php echo $StrDes;?>"  
tabindex="<?php echo $TI++;?>" />

<div class="s1" style="left: 811px; top: 113px; width: 99px;">MARCA</div>
<select name="CboMar" id="CboMar" 
class="lt" style="position:absolute; left: 807px; top: 129px; width: 106px;"
tabindex="<?php echo $TI++;?>">
<?php	echo FCBO_("mar_nombre","marcas","id_marca>0",$StrMar,"S","")?>  
</select>

<div class="s1" style="left: 920px; top: 113px; width: 116px;">ESTADO.STOCK</div>
<select name="CboSto" id="CboSto" 
class="lt" style="position:absolute; left: 917px; top: 128px; width: 106px;"
tabindex="<?php echo $TI++;?>">
<?php echo FCBO_("aux_nom","auxiliar","aux_des='ST'",$CboSto,"S","");?>
</select>


<div class="s1" style="left: 100px; top: 50px; width: 188px; 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();" />

</div>


<div id="layer3" style="position:absolute; left: 29px; top: 158px; width: 1144px; height: 396px; overflow:auto">
<div align="left" class="Estilo2">
<table id="Exportar_a_Excel" width="1120" height="5" border="2" cellpadding="0" cellspacing="0" style="color:#000">
<tr>
<td height="18" colspan="9" bgcolor="#666666" align="center">
<?php echo PagBtn($LStrTR,$VP_RegXPag,$LStrPag);?>
</td>
</tr>
<tr>
<td width="74" height="17" bgcolor="#999999">CODIGO</td>
<td width="158" bgcolor="#999999">LINEA</td>
<td width="175" bgcolor="#999999">FABRICANTE</td>
<td width="166" bgcolor="#999999">MODELO</td>
<td width="261" bgcolor="#999999">DESCRIPCION</td>
<td width="100" bgcolor="#999999">MARCA</td>
<td width="53" bgcolor="#999999">*P.V.B</td>
<td width="56" bgcolor="#999999">*P.V.S</td>
<td width="55" bgcolor="#999999">N.Pre</td>
</tr>
<?php		  		
    $IdAlm=gn("id_almacen","almacenes","alm_nombre='$StrAlm'");
    $s="SELECT ID,COD,LINEA,DESCRIPCION,MARCA,PV_MAX,FAB,MODELO 
        FROM v_stocks WHERE ".$FF."	ORDER BY DESCRIPCION,MARCA ";	  					
    $s=PagSql($s,$LStrPag,$VP_RegXPag,$LStrTR);
	$C=0;	
    $result = grs($s);
    while ($row = $result->fetch_assoc()){
		$PRE=gn("pxs_precio","precios","pxs_id_almacen=$IdAlm AND pxs_id_producto=".$row['ID']);
		$C++;
		$IdP=$row['ID'];
		?>
		<tr>
		<td bgcolor="#FFFFFF">'<?php echo $row['COD']?></td>
		<td bgcolor="#FFFFFF"><?php echo $row['LINEA']?></td>
		<td bgcolor="#FFFFFF"><?php echo $row['FAB']?></td>
		<td bgcolor="#FFFFFF"><?php echo $row['MODELO']?></td>
        <td bgcolor="#FFFFFF"><?php echo $row['DESCRIPCION']?></td>
        <td bgcolor="#FFFFFF"><?php echo $row['MARCA']?></td>
		<td bgcolor="#FFFFFF" style="text-align:right"><?php echo $row['PV_MAX']?></td>
		<td bgcolor="#FFFFFF" style="text-align:right"><?php echo $PRE?></td>
		<td bgcolor="#FFFFFF">
        <input name="Txt<?php echo $IdP;?>" type="text" id="Txt<?php echo $IdP;?>" 
        class="nd" style=" position:inherit; width:55px" 
        value="<?php echo $StrD[$IdP];?>"   onfocus="this.select();"
        tabindex="<?php echo $TI++;?>" />
		</td>
		</tr>
		<?php
		
    }

	
?>
</table>

</div>
</div>

<div class="s1" style="left: 794px; top: 561px; width: 152px;">Total Registros: <?php echo $C; ?></div>

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

<div id="LblTit" style="position:absolute; left: 34px; 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: 1031px; top: 128px; width: 59px;"
value="Filtrar" onclick="fFiltrar();" />

<input name="CmdG" type="button" id="CmdG" 
style="position:absolute; left: 1098px; top: 128px; width: 67px;"
value="Guardar" onclick="fGuardar();" />


  
</center>	
</form>

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