<?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;
	}
	*/
	//
	$HW=$_SESSION['SYS_HW'];
	$HW=$HW."_SP";
	//-------------------------
	$StrId=V($_POST['TxtId']);
	$StrCmd=V($_POST['TxtAc']);	
	//-------------------------
	$StrONE=trim(V($_GET['CmdB']));	
	//-------------------------
	$StrCod=V($_POST['TxtCod']);		
	$StrDes=V($_POST['TxtDes']);
	$StrMar=V($_POST['CboMar']);
	
	if(strlen($StrCmd)==0)$StrCmd=V($_GET['TxtAc']);
	if($StrCmd=="X" || strlen($StrCmd)==0){
		$StrLin="TODOS";
		$StrMar="TODOS";	
	}
	//-------------------------
	$StrCodT=$StrCod."%";
	$StrDesT=$StrDes."%";
	$StrMarT=$StrMar;
	if($StrMarT=="TODOS") $StrMarT="%";
	$FF="ID>0 AND COD LIKE '$StrCodT' AND 
		 DESCRIPCION LIKE '$StrDesT' AND LINEA LIKE '$StrMarT'";
	$LStrTR=gn("count(*)","vproductos",$FF);
	$VP_RegXPag=20000;	
	$LStrPag=V($_POST['TxtPag']);
	if($StrCmd=="F"){$LStrPag="1";}
	$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>Reporte de Stock</title>
<?php AddJs();?>
<script type="text/javascript" src="fe/funciones.js"></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>

<style type="text/css">
<!--
body {
	background-color: #CCC;
}


#Layer1 {
	position:absolute;
	left:345px;
	top:115px;
	width:123px;
	height:35px;
	z-index:1;
}
.estilo888 {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
color: #003366;
font-weight: bold;
}
.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;
}
.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;}
.eS4 {color: #000099}
.Estilo5 {color: #000000}
#Frm center #DivFRM #layer3 .f2 table tr td {
	color: #000;
	text-align: left;
}
#Frm center #DivFRM #layer3 .f2 table {
	font-weight: normal;
}

-->
</style>
<style type="text/css">
  table th { text-align: right; }
  table td { text-align: left; }
</style>
</head>
<body  <?php echo $StrMsg;?>>
<div Id="DivFRM"  class="centrarFrm" >
<form id="Frm" name="Frm" method="post" action="stockpersonalizado.php">
<center> 

    <div id="layer6" style="position:absolute; left: 120px; top: 89px; width: 64px;">
      <div align="left" class="f2" >CODIGO</div>
    </div>
    <div id="layer5" style="position:absolute; left: 69px; top: 50px; width: 205px; height: 28px;">
      <div align="left" class="f2">
        <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; ?>" />
      </div>
    </div>
<div align="left" 
style="height:382px; width:1155px; overflow-x:hidden; position:absolute; left: 21px; top:145px; overflow-y:scroll; color: #0FF; background-color: #FFFFFF; layer-background-color: #FFFFFF; border: 1px none #000000;">
<?php
		$COLOR=0;
		$Mar=$P3."%";
		$Cod=$P4."%";
		//			  
	    $ry = grs("SELECT count(*) FROM almacenes WHERE id_almacen>0");
		$roy = $ry->fetch_row();
		$XYZ=$roy[0];
		//
		$T=110+400+120+180+($XYZ*60)+120;
		$s="<table id=\"Exportar_a_Excel\" style=\"width:$T"."px; top:0px\" border=\"1\" cellpadding=\"0\" ".
		   "cellspacing=\"0\" bgcolor=\"#FFFFFF\" class=\"estilo888\" >";
		$t=$s;
		$s="";
		$cx=0;
		$F="";
		//
		$F="<tr  height=\"10 px\">";
		$F=$F."<td style=\"width:110px\" bgcolor=\"#CCCCCC\">CODIGO</td> ";
		$F=$F."<td style=\"width:400px\" bgcolor=\"#CCCCCC\">DESCRIPCION</td>";
		$F=$F."<td style=\"width:120px\" bgcolor=\"#CCCCCC\">LINEA</td>";
		$F=$F."<th style=\"width:60px\" bgcolor=\"#CCCCCC\">PV MIN</th>";
		$F=$F."<th style=\"width:60px\" bgcolor=\"#CCCCCC\">PV MAX</th>";
		$F=$F."<th style=\"width:60px\" bgcolor=\"#CCCCCC\">STOCK</th>";
		
		$z="SELECT alm_obs FROM almacenes ".
		   "WHERE id_almacen>0 ORDER BY alm_obs";
		$result = grs($z);
		while ($roww = $result->fetch_row()){		 
			$F=$F."<td  align=\"right\" style=\"width:60px\" bgcolor=\"#CCCCCC\">$roww[0]</td>";
		}
		echo $s;
		
		$F=$F."<td style=\"width:60px\" align=\"right\" bgcolor=\"#CCCCCC\">SEP.</td>";
		$F=$F."<td style=\"width:60px\" align=\"right\" bgcolor=\"#CCCCCC\">E.T.</td>";
		$F=$F."</tr>";
		$s=$s.$F;

		$sql="SELECT ID,COD,LEFT(DESCRIPCION,45),ROUND(PV_MIN,3),ROUND(PV_MAX,3),
			  LEFT(LINEA,35),ROJ,AMA FROM vproductos
			  WHERE COD LIKE '$StrCodT' AND DESCRIPCION LIKE '$StrDesT' AND 
			  LINEA LIKE '$StrMarT'  ORDER BY COD";
		$result = grs($sql);
		while ($row = $result->fetch_row()){
		 	  
	      $sql_p="SELECT PIE ".
		         "FROM vis_sp ".
			     "WHERE ID_PRO=$row[0] AND (STOCK>0 OR PROVICIONAL=0) ORDER BY PIE";			   			 		  
		  $result_p = grs($sql_p);
		  //
		  ejt("DELETE FROM hw_pie WHERE hw='$HW'");
		  while ($row_p = $result_p->fetch_assoc()){
			  $_PIE=$row_p['PIE'];
			  $z="INSERT INTO hw_pie VALUES ('$HW',$_PIE)";
			  //ejt($z);
			  $rz = grs($z);
			  
		  }		   
		  $z="SELECT PIE FROM vcot_control WHERE IDP=$row[0] AND (CA_CONTROL - DEJ)>0 AND ANU='N' AND PRI='1' GROUP BY PIE ";
		  $result_p = grs($z);
		  while ($row_p = $result_p->fetch_assoc()){
			  $_PIE=$row_p['PIE'];
			  //			  
			  $ry = grs("SELECT count(*) FROM hw_pie WHERE pie=$_PIE AND hw='$HW'");
			  $roy = $ry->fetch_row();
			  $XYZ=$roy[0];
			  //
			  //if(gnw("count(*)","hw_pie","pie=$_PIE AND hw='$HW'")==0){
			  if($XYZ==0){	
				  $z="INSERT INTO hw_pie VALUES ('$HW',$_PIE)";
				  //ejt("INSERT INTO hw_pie VALUES ('$HW',$_PIE)");
				  $rz = grs($z);
			  }
		  }
		  $z="SELECT PIE FROM vven_control WHERE IDP=$row[0] AND (CA_CONTROL - DEJ)>0 AND ANU='N' AND PRI=1 GROUP BY PIE ";
		  $result_p = grs($z);
		  while ($row_p = $result_p->fetch_assoc()){
			  $_PIE=$row_p['PIE'];
			  //			  
			  $ry = grs("SELECT count(*) FROM hw_pie WHERE pie=$_PIE AND hw='$HW'");
			  $roy = $ry->fetch_row();
			  $XYZ=$roy[0];
			  //
			  //if(gnw("count(*)","hw_pie","pie=$_PIE AND hw='$HW'")==0){
			  if($XYZ==0){				  
				  $z="INSERT INTO hw_pie VALUES ('$HW',$_PIE)";
				  //ejt("INSERT INTO hw_pie VALUES ('$HW',$_PIE)");
				  $rz = grs($z);
			  }
		  }
		  
		  $sql_p="SELECT pie ".
		         "FROM hw_pie ".
			     "WHERE hw='$HW' GROUP BY pie ORDER BY pie";			   			 
		  $result_p = grs($sql_p);
		  //
		  	
		  while ($row_p = $result_p->fetch_row()){
			  //
			  $_IDP=$row[0];
			  $ROJ=$row[6];
			  $AMA=$row[7];
			  //
			  $_PIE=$row_p[0];			 
			  //			  
			  $ry = grs("SELECT SUM(CA_CONTROL - DEJ) FROM vcot_control WHERE IDP=$_IDP AND PIE=$_PIE AND ANU='N' AND PRI='1'");
			  $roy = $ry->fetch_row();
			  $_SEP=$roy[0];			 
			  //			  
			  $ry = grs("SELECT SUM(CA_CONTROL - DEJ) FROM vven_control WHERE IDP=$_IDP AND PIE=$_PIE AND ANU='N' AND PRI=1");
			  $roy = $ry->fetch_row();
			  $_SEP=$_SEP +$roy[0];			 
			  //			  
			  $ry = grs("SELECT SUM(CA) FROM vcom_control WHERE IDP=$_IDP AND PIE=$_PIE AND ANU='N' AND ET='SI'");
			  $roy = $ry->fetch_row();
			  $_TRA= $roy[0];
			  //
			  $KEY=$row[0].".".$row_p[0];
			  $s=$s."<tr>";
			  $s=$s."<td>".$row[1]."x".$row_p[0]."</td>";
			  $s=$s."<td>".rtrim($row[2])."</td>";
			  if(strlen(trim($row[5]))==0){
				$s=$s."<td>&nbsp;</td>";  
			  }else{
			  	$s=$s."<td>".trim($row[5])."</td>";
			  }
			  $s=$s."<th>".$row[3]."</th>";
			  $s=$s."<th>".$row[4]."</th>";
			  /*
			  $STO=gnw("SUM(sp_cantidad)",
			  		  "stocks_presentaciones",
					  "sp_id_producto=$row[0] AND sp_presentacion=$row_p[0]");
			  */
			  //
			  $ry = grs("SELECT SUM(sp_cantidad) FROM stocks_presentaciones WHERE sp_id_producto=$row[0] AND sp_presentacion=$row_p[0]");
			  $roy = $ry->fetch_row();
			  $STO= $roy[0];
			  //	
			  if($STO<=$ROJ){
				  $COLOR="#FC8686";	
			  }else{
				  if($STO<=$AMA){
					  $COLOR="#FAEF9F";	
				  }else{
					  $COLOR="#CDDDEF";
				  }				  
			  }			  
			  $s=$s."<th  bgcolor=\"$COLOR\" > ".$STO."</th>";
			  
			  $z="SELECT id_almacen FROM almacenes ".
				 "WHERE id_almacen>0 ORDER BY alm_obs";
				 $resultt = grs($z);
				 while ($roww = $resultt->fetch_row()){		 
				 	/*
				    $STO=gnw("SUM(sp_cantidad)",
			  		        "stocks_presentaciones",
					        "sp_id_producto=$row[0] AND sp_presentacion=$row_p[0] AND sp_id_almacen=$roww[0]");
					*/
					//
					$ry = grs("SELECT SUM(sp_cantidad) 
									  FROM stocks_presentaciones 
									  WHERE sp_id_producto=$row[0] AND sp_presentacion=$row_p[0] AND sp_id_almacen=$roww[0]");
					$roy = $ry->fetch_row();
					$STO= $roy[0];
					//
					$s=$s."<th>$STO</th>";
				 }
			  $s=$s."<th>$_SEP</th>"; 
			  $s=$s."<th>$_TRA</th>"; 
			  $s=$s."</tr>";
			  
		  }
		}
		if($s!=""){
        $s=$t.$s."</table>";		
		}		
		echo $s;	
?>
</div>
    <div id="layer" style="position:absolute; left: 69px; top: 30px; width: 412px; height: 24px;">
      <div align="left" class="Estilo1">REPORTE DE STOCK PERSONALIZADO</div>
    </div>
          <input name="TxtCod" type="text" id="TxtCod" 
          style="position:absolute; left: 118px; top: 109px; width: 111px;" value="<?php echo $StrCod;?>"  
          onkeydown="return SoloTextoEx(event,'TxtDes');"
          tabindex="1" />

    <div id="LblTit" style="position:absolute; left: 10px; 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: 701px; top: 109px; width: 66px;"
value="Filtrar" onclick="fFiltrar();" />

<div  align="left" style="height:19px; width:1155px; position:absolute; left: 20px; top: 143px; color: #0FF; background-color: #FFFFFF; layer-background-color: #FFFFFF; border: 1px none #000000;">
<?php
$T=110+400+120+180+(gn("count(*)","almacenes","id_almacen>0")*60)+120;
?>
<table style="width:<?php echo $T;?>px" border="1" cellpadding="0" cellspacing="0" bgcolor="#d4e3e5" 
class="estilo888">

<tbody>
<tr  height="10 px">
<td style="width:110px" bgcolor="#CCCCCC" >CODIGO</td>
<td style="width:400px" bgcolor="#CCCCCC">DESCRIPCION</td>
<td style="width:120px" bgcolor="#CCCCCC">LINEA</td>
<td style="width:60px" align="right" bgcolor="#CCCCCC">PV MIN</td>
<td style="width:60px" align="right" bgcolor="#CCCCCC">PV MAX</td>
<td style="width:60px" align="right" bgcolor="#CCCCCC">STOCK</td>

<?php
$s="";
$z="SELECT alm_obs FROM almacenes ".
   "WHERE id_almacen>0 ORDER BY alm_obs";
$result = grs($z);
while ($roww = $result->fetch_row()){		 
	$s=$s."<td  align=\"right\" style=\"width:60px\" bgcolor=\"#CCCCCC\">$roww[0]</td>";
}
echo $s;
?>
<td style="width:60px" align="right" bgcolor="#CCCCCC">SEP.</td>
<td style="width:60px" align="right" bgcolor="#CCCCCC">E.T.</td>
</tr>
</tbody>
</table>

</div>
    <div id="layer8" style="position:absolute; left: 245px; top: 89px; width: 136px;">
      <div align="left" class="f2">DESCRIPCION</div>
</div>
<input name="TxtDes" type="text" id="TxtDes" style="position:absolute; left: 242px; top: 109px; width: 311px;" 
value="<?php echo $StrDes;?>"  
onkeydown="return SoloTextoEx(event,'CboMar');"
tabindex="1" />

<select name="CboMar" id="CboMar" style="position:absolute; left: 574px; top: 109px; width: 120px;" 
onkeydown="return SoloTextoEx(event,'CmdB');">
<?php	
echo FCBO_("lin_nombre","lineas","id_linea>0",$StrMar,"S","S");
?>
</select>

<div id="layer12" style="position:absolute; left: 576px; top: 89px; width: 136px;">
<div align="left" class="f2">LINEA</div>
</div>
</center>	
</form>

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

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