<?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']);		
	$StrLin=V($_POST['CboLin']);
	$Fab=V($_POST['Fab']);
	$Mod=V($_POST['Mod']);	
	$StrDes=V($_POST['TxtDes']);
	$StrMar=V($_POST['CboMar']);	
	$Img=V($_POST['CboImg']);
	
	$IdUsu=$_SESSION['SYS_USER_ID'];
	//
	if(strlen($Img)==0)$Img="NO";
	//
	if(strlen($StrLin)==0)$StrLin=GetSO("lin_nombre","lineas","id_linea>0","lin_nombre");
	//
	if($_SESSION['SYS_USER_ALM']=="%"){
		if(strlen($StrAlm)==0)$StrAlm=GetSO("alm_nombre","almacenes","","alm_nombre");		
	}else{
		$StrAlm=$_SESSION['SYS_USER_ALM'];
	}
	
	$StrAlm=T($StrAlm);

	//
	$Fab_=LIKE($Fab);
	$Mod_=LIKE($Mod);	
	$StrLinT=VCbo($StrLin);
	$StrMarT=VCbo($StrMar);
	$StrCodT=$StrCod."%";
	$StrDesT=$StrDes."%";

	$FF="ID>0 
	AND ALM LIKE '$StrAlm'
	AND COD LIKE '$StrCodT'  
	AND DESCRIPCION LIKE '$StrDesT' 
	AND MARCA LIKE '$StrMarT'
	AND LINEA LIKE '$StrLinT'
	AND $StrSto AND FAB LIKE '$Fab_' AND MODELO LIKE '$Mod_' ";

	$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;\"";
	$TIN=1;


	$AT=gs("at","usuarios","id_usuario=$IdUsu");
?>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Listado de productos</title>
<!----------------------------------------->
<?php AddJs(); ?>
<script type="text/javascript" src="ja/funciones.js"></script>

<!----------------------------------------->
<link rel="stylesheet" type="text/css" href="css/estiloss.css"  />
<link rel="stylesheet" type="text/css" href="css/basic.css"  />
<link rel="stylesheet" type="text/css" href="css/mnu.css"  />
<!----------------------------------------->
<style type="text/css">
.cF {height:600px;width:1250px;position:relative;top:0px}
.mM {height:34px;width:1250px;position:relative;top:0px;background-color:#09C; z-index:500;}

</style>

<!----------------------------------------->
<script>
$(document).ready(function(){  
   Inicializar();
   jF("TxtNom");
});

$(document).keydown(function(e){
	var AT=gv("TxtAT");

	if(e.which==66 && e.altKey){
		if(AT=="1"){	
		document.getElementById("CmdB").click();
		return;
		}
	}

	if(e.which==86 && e.altKey){
		if(AT=="1"){
		document.getElementById("CmdZ").click();
		return;
		}
	}
  
});
</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>
<!----------------------------------------->

<body  <?php echo $LStrMsg;?>  >
<div align="center">

<div class="mM" align="left" style="font-size:12px">
<?php
$HTM=file_get_contents("http://inversionessounduniversal.com/ventas/phpmnu.php?USER_ID=$IdUsu");
echo $HTM;
?>
</div>

<div Id="DivFRM"  class="cF" style="background-color:#FFFFFF;" >
<form id="Frm" name="Frm" method="post" action="rep_lista.php" style="margin:0px; padding:0px; display:inline;" onSubmit="return false;">

<!----------------------------------------->
<div id="DivT" class="dT"  style="width:1250px;">
<img src="b.png" class="iT" />
<div class="tT">LISTADO DE PRODUCTOS</div>
</div>
<!----------------------------------------->


<div class="s1" style="left:35px; top: 114px; width: 102px;">ALMACEN</div>
<select name="CboAlm" id="CboAlm" 
class="lt" style="left:35px; top: 129px; width:116px" 
tabindex="<?php echo ++$TI;?>">
<?php 
	if($IdUsu>=1  && V($_SESSION['SYS_USER_ALM'])!="%"){
		echo ListALM($StrAlm); 
	}else{
    	echo FCBO("alm_nombre","almacenes","id_almacen>0",$StrAlm,"N","N");
    }
?>
</select>

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

<div class="s1" style="left:265px; top: 113px; width: 83px;">LINEA</div>
<select name="CboLin" id="CboLin" 
class="lt" style="position:absolute; left:263px; top: 129px; width: 117px;"
tabindex="<?php echo ++$TI;?>">
<?php	echo FCBO_("lin_nombre","lineas","id_linea>0",$StrLin,"S","N")?>  
</select>

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

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

<div class="s1" style="left: 685px; top: 114px; width: 157px;">DESCRIPCION</div>
<input name="TxtDes" type="text" id="TxtDes" 
class="lt" style="left: 684px; top: 129px; width: 176px;"
value="<?php echo $StrDes;?>"  
tabindex="<?php echo ++$TI;?>"/>

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

<div class="s1" style="left: 1013px; top: 113px; width: 97px;">IMAGEN</div>
<select name="CboImg" id="CboImg" 
class="lt" style="position:absolute; left:1009px; top: 129px; width: 103px;"
tabindex="<?php echo ++$TI;?>">
<?php	
	echo FCBO("aux_nom","auxiliar","aux_des='SN'",$Img,"N","N");
?>  
</select>





<div id="DivP" style="visibility:hidden;position:absolute; text-align:center; background:#FF0; left: 869px; top: 82px; width: 90px; left: 253px;">
Descarga</div>



<!----------------------------------------->
<div id="DivBtn" align="left" style="position:absolute; left: 33px; top: 71px; width: 551px; height: 26px;">
<input name="CmdB" type="button" id="CmdB" class="btm"
value="Buscar" onClick="fFiltrar();"  
tabindex="<?php echo ++$TI;?>" />

</div>

<!-----------------------------------------> 
<div class="datagrid" style="position: absolute; left:35px; top: 159px; width: 1180px; height: 403px; overflow: auto">
<table id="Exportar_a_Excel"  height="5" border="2" cellpadding="0" cellspacing="0" style="color:#000">
<?php
	$ALM_=gn("id_almacen","almacenes","alm_nombre='$StrAlm'");
	$s="SELECT LINEA FROM vproductos 
		WHERE ID>0 AND LINEA LIKE '$StrLinT' GROUP BY LINEA ORDER BY LINEA";
	$rs = grs($s);
	while ($r= $rs->fetch_assoc()){
		$LIN=$r['LINEA'];
		ejt("DELETE FROM matriz");
		//
		$s="INSERT INTO matriz (cx,hw,m) VALUES (0,'','')";
		ejt($s);
		//
		$d_cx=0;
		$d_s="SELECT DESCRIPCION FROM vproductos 
			  WHERE LINEA='$LIN' AND 
			  COD LIKE '$StrCodT' AND FAB LIKE '$Fab_' AND 
			  MODELO LIKE '$Mod_' AND DESCRIPCION LIKE '$StrDesT' AND 
			  MARCA LIKE '$StrMarT' GROUP BY DESCRIPCION 
			  ORDER BY DESCRIPCION";
		$d_rs = grs($d_s);
		while ($d_r= $d_rs->fetch_assoc()){
			$DES=$d_r['DESCRIPCION'];
			$d_cx=$d_cx+1;
			$CAMPO="d".vbr("00".$d_cx,2);
			$s="UPDATE matriz SET $CAMPO='$DES' WHERE cx=0";
			ejt($s);			
			if($d_cx==99){
				break;
			}
		}
		//
		$m_cx=0;
		$m_s="SELECT MODELO FROM vproductos 
			  WHERE LINEA='$LIN' AND 
			  COD LIKE '$StrCodT' AND FAB LIKE '$Fab_' AND 
			  MODELO LIKE '$Mod_' AND DESCRIPCION LIKE '$StrDesT' AND 
			  MARCA LIKE '$StrMarT' 
			  GROUP BY MODELO 
			  ORDER BY MODELO";
		$m_rs = grs($m_s);
		while ($m_r= $m_rs->fetch_assoc()){
			$MOD=$m_r['MODELO'];
			$m_cx=$m_cx+1;
			$s="INSERT INTO matriz (cx,hw,m) VALUES ($m_cx,'','$MOD')";
			ejt($s);			
		}
		/*
		for($c=1;$c<=$d_cx;$c++){
			$CAM="d".vbr("00".$c,2);
			$DES=gs($CAM,"matriz","cx=0");
			for($f=1;$f<=$m_cx;$f++){				
				$MOD=gs("m","matriz","cx=$f");
				$STOCK=gn("STOCK","vstocks","ID_ALM=$ALM_ AND LIN='$LIN' AND MODELO LIKE '$MOD'");
				ejt("UPDATE matriz SET $CAM='$STOCK' WHERE cx=$f");
			}
		}
		*/
		$TOT_COL=3+$d_cx;
		//		
		?>
		<tr>
		<td width="52" height="17" bgcolor="#999999"><span class="x4">LINEA </span></td>
		<td colspan="<?php echo $TOT_COL;?>" bgcolor="#999999"><b><span class="x4" style="font-size:25px"><?php echo $LIN;?></span></b></td>
		</tr>                
        <?php
			if($Img=="SI"){
		?>
            <tr>        
            <td width="52" height="17" bgcolor="#999999"><span class="x4">#</span></td>
            <td width="234" bgcolor="#999999"><span class="x4">IMAGENES</span></td>
            <?php
                for($c=1;$c<=$d_cx;$c++){
                    $CAM="d".vbr("00".$c,2);
                    $DES=gs($CAM,"matriz","cx=0");
					$COD=gs("COD","vproductos","LINEA='$LIN' AND DESCRIPCION='$DES'");
					if(strlen($COD)==0){
						$IMG="vacio.png";
					}else{
						$IMG=GET_IMG($COD);
					}
            ?>
            	<td  bgcolor="#999999" align="center"><img src="<?php echo $IMG;?>" width="80" height="120" style="margin-left:3px; margin-right:3px; margin-top:3px; margin-bottom:3px" /></td>   
            <?php
                }
            ?>        
            <td width="55" bgcolor="#999999" align="right"><span class="x4">STOCK</span></td>
            </tr>
        <?php			
			}
		?>
		<tr>        
		<td width="52" height="17" bgcolor="#999999"><span class="x4">#</span></td>
		<td width="234" bgcolor="#999999"><span class="x4">MODELO</span></td>
        <?php
			for($c=1;$c<=$d_cx;$c++){
				$CAM="d".vbr("00".$c,2);
				$DES=gs($CAM,"matriz","cx=0");
		?>
    	<td  bgcolor="#999999" align="center"><span class="x4"><?php echo $DES;?></span></td>   
        <?php
			}
		?>        
    	<td width="55" bgcolor="#999999" align="right"><span class="x4">STOCK</span></td>
    	</tr>
        <?php
		for($f=1;$f<=$m_cx;$f++){			
			$MOD=gs("m","matriz","cx=$f");		
			$MOD_TOT=0;				
		?>
            <tr>
	        <td bgcolor="#FFFFFF"><?php echo $f;?></td>
            <td bgcolor="#FFFFFF">'<?php echo $MOD;?></td>
        <?php
			for($c=1;$c<=$d_cx;$c++){				
				$CAM="d".vbr("00".$c,2);
                $DEZ=gs($CAM,"matriz","cx=0");
				$STOCK=0;
				if($ALM_==0){
					$STOCK=gn("SUM(STOCK)","vstocks","LIN='$LIN' AND MODELO LIKE '$MOD' AND DES='$DEZ'");
				}else{
					$STOCK=gn("STOCK","vstocks","ID_ALM=$ALM_ AND LIN='$LIN' AND MODELO LIKE '$MOD' AND DES='$DEZ'");
				}
				$MOD_TOT=$MOD_TOT+$STOCK;
				?>
                <td bgcolor="#FFFFFF" align="center"><?php echo $STOCK;?></td>
                <?php
			}
			?>
        	<td bgcolor="#FFFFFF" align="center"><?php echo $MOD_TOT;?></td>       
            </tr>
		<?php
		}
		?>
<?php    
	}
?>

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

<!----------------------------------------->    
<?php include("ocultos.php");?>

</form>

<form action="ficheroExcel.php" 
    method="post" target="_blank" id="FormularioExportacion" >
    
    <div style="position: absolute; left: 126px; top: 71px; width: 45px; height: 15px;">  
    <input name="" type="button" class="botonExcel btm" value="Exportar"  />
    <input type="hidden" id="datos_a_enviar" name="datos_a_enviar" />
    </div>
</form>
</div>
</div>
</body>
</html>
