<?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']);
	//-------------------------
	$StrEmp=V($_POST['CboEmp']);
	$StrAlm=V($_POST['CboAlm']);		
	$StrCod=V($_POST['TxtCod']);		
	$StrDes=V($_POST['TxtDes']);
	$StrMar=V($_POST['CboMar']);
	if(strlen($StrMar)==0)$StrMar="TODOS";	
	//	
	
	//
	$StrCodT=$StrCod."%";
	$StrDesT=$StrDes."%";
	$StrMarT=$StrMar;
	if($StrMarT=="TODOS") $StrMarT="%";
	$FF="COD LIKE '$StrCodT'  AND 
		 DES LIKE '$StrDesT' AND 
		 LIN LIKE '$StrMarT' AND ALM LIKE '$StrAlm' AND PIE>0";
	//
	if($StrCmd=="KILL_TODO"){
		//BIT
		$_BIT_MOD_ID="0";
		$_BIT_SQL_CAB="SQL";
		$_BIT_SQL_DET="";
		$_BIT_PHP="rep_stock.php";
		$_BIT_ID=BIT_ADD("INVETARIO","CONTROL DE STOCK PRESENTACION","GUARDAR",$_BIT_MOD_ID,$_BIT_SQL_CAB,$_BIT_SQL_DET,$_BIT_PHP);
		BIT_DET_ADD($_BIT_ID,"");
		//BIT		
		$IdAlm=gn("id_almacen","almacenes","alm_obs='$StrAlm'");		  	
		$s="SELECT COD,DES,LIN,CAN,PIE,ID_PRO 
			FROM vix_stock_pie WHERE ".$FF." ORDER BY COD,PIE ";		
		$result = grs($s);
		while ($row = $result->fetch_assoc()){		
			$EXI=0;
			$IDP=$row['ID_PRO'];
			$PIE=$row['PIE'];
			$CAN=$row['CAN'];
			$XXX=$PIE * $CAN;		
			//++++++++++++++++++++++++
			$EXI=XERIFICAR($IDP,$PIE);
			if($EXI==0){
				$qqq=ejt("UPDATE stocks SET stock=stock - $XXX WHERE id_producto=$IDP AND id_almacen=$IdAlm");
				BIT_DET_ADD($_BIT_ID,$qqq);
				//
				$qqq=ejt("DELETE FROM stocks_presentaciones WHERE sp_id_producto=$IDP AND sp_id_almacen=$IdAlm AND sp_presentacion=$PIE");
				BIT_DET_ADD($_BIT_ID,$qqq);
				//
				$s="DELETE FROM t_productos WHERE pro_id=$IDP AND pro_pie=$PIE";
				ejt($s);
				BIT_DET_ADD($_BIT_ID,$s);
				//
				$IDK=GenId("id_ajuste","ajustex");
				$GII=GenGI();
				$FEC=date("Y-m-d");
				$s="INSERT INTO ajustex VALUES ($IDK,'$FEC',$IdAlm,$IDP,'Q',$CAN,$PIE,$GII,'CONTROL STOCK',0)";
				ejt($s);
				BIT_DET_ADD($_BIT_ID,$s);
				//
				T_P_ACT($IDP);
			}
		}
	}
	//
	$LStrTR=gn("count(*)","vix_stock_pie",$FF);
	$LStrPag=V($_POST['TxtPag']);	
	if($StrCmd=="F"){$LStrPag="1";}
	$VP_RegXPag=5000;	
	$LStrPag=PagGet($LStrPag,$StrCmd,$LStrTR,$VP_RegXPag);
	//
	$LStrFoc="document.getElementById('CboEmp').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>Control de Stock por presentacion</title>
<?php AddJs();?>
<script type="text/javascript" src="fe/funcionez.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();
});
});

function ELI_TODO(){
	var Rpta = confirm("Esta seguro que desea eliminar todo?")		
	if (Rpta==true){
		document.getElementById("TxtAc").value="KILL_TODO";
		document.getElementById("Frm").submit();
	}
}
function jsEli(A){
	if(document.getElementById("ChP").checked==true){
		var Rpta = confirm("Esta seguro que desea eliminar?")		
		if (Rpta==false){
			return;
		}
	}
	var Cmd="K_"+A;
	var Fil="F_"+A;
	A=A.split("_");
	var AL=A[0];
	var PR=A[1];
	var PI=A[2];	
	var KA=A[3];	
	var RR=AJ("ax.php?C=K&P1="+AL+"&P2="+PR+"&P3="+PI+"&P4="+KA);
	if(RR=="OK"){
		//document.getElementById(Cmd).disabled=true;
		//document.getElementById(Cmd).style.visibility="hidden";
		document.getElementById(Fil).remove();
	}else{
		alert("Proceso no completado");
	}
}
function EM_OC(){
	var Emp=gv("CboEmp");	
	var r=AX("02",Emp);
	if(r=="X"){
		CBO_CLS("CboAlm",1);
	}else{
		CBO_FILL("CboAlm",r,0);
	}
}
</script>
<style type="text/css">
<!--
body {
	background-color: #CCC;
}
#Layer1 {
	position:absolute;
	left:345px;
	top:115px;
	width:123px;
	height:35px;
	z-index:1;
}
.centrarFrm{
	width:900px;
	height:600px;
	position:absolute;
	left:<?php echo "50%";?>;
	top:<?php echo "50%";?>;
	background-color:#006699;
	margin:<?php echo "-300px";?> 0 0 <?php echo "-450px";?>;
}
#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;
}
.zz2 {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;}
.x4 {color: #000099}
.Estilo5 {color: #000000}
#Frm center #DivFRM #layer3 .zz2 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="rep_xtock.php">  

<div id="layer5" style="position:absolute; left: 100px; top: 50px; width: 205px; height: 28px;">
<div align="left" class="zz2">
<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 id="layer3" style="position:absolute; left: 43px; top: 168px; width: 828px; height: 387px;">
<div align="left" class="zz2">
<table id="Exportar_a_Excel" width="832" height="19" border="1" cellpadding="0" cellspacing="0">
<tr  >
<td height="18" colspan="6" bgcolor="#666666" > 
<p align="center"> <?php echo PagBtn($LStrTR,$VP_RegXPag,$LStrPag);?></p>
</td>
</tr>
<tr>
<td width="107" height="17" bgcolor="#999999"><span class="x4">CODIGO</span></td>
<td width="62" bgcolor="#999999"><span class="x4">PIEZAS</span></td>
<td width="63" bgcolor="#999999"><span class="x4">ACCION</span></td>
<td width="66" bgcolor="#999999"><span class="x4">CANTIDAD</span></td>
<td width="438" bgcolor="#999999"><span class="x4">DESCRIPCION</span></td>

<td width="67" bgcolor="#999999">&nbsp;</td>
</tr>
<?php
	//	  		
    $IdAlm=gn("id_almacen","almacenes","alm_obs='$StrAlm'");		  	
    $s="SELECT COD,DES,LIN,CAN,PIE,ID_PRO,ID_ALM,ID_PRO 
        FROM vix_stock_pie WHERE ".$FF." ORDER BY COD,PIE ";
	//echo $s."<br>";		
    $result = grs($s);
    while ($row = $result->fetch_assoc()){		
		$EXI=0;
		$IDP=$row['ID_PRO'];
		$PIE=$row['PIE'];
		$CAN=$row['CAN'];
		$III=$row['ID_ALM']."_".$row['ID_PRO']."_".$row['PIE']."_".$row['CAN'];		
		//
		$EXI=XERIFICAR($IDP,$PIE);
		if($EXI==0){
			echo "<tr id=\"F_".$III."\"  class=\"Estilo5\">";
			echo "<td bgcolor=\"#FFFFFF\">".$row['COD']."</td>";
			echo "<td bgcolor=\"#FFFFFF\">".$row['PIE']."</td>";
			echo "<td bgcolor=\"#FFFFFF\"  > 
			<input name=\"Kmd_".$III."\" type=\"button\" value=\"Eliminar\" id=\"K_".$III."\" onclick=\"jsEli('$III');\" />
			</td>";
			echo "<td bgcolor=\"#FFFFFF\"  >".$row['CAN']." </td>";
			echo "<td bgcolor=\"#FFFFFF\" >".$row['DES']."</td>";
			echo "<td bgcolor=\"#FFFFFF\" ></td>";
			echo "</tr>";
		}
    }
	//
?>
</table>
      </div>
    </div>
    <div id="layer" style="position:absolute; left: 100px; top: 30px; width: 621px; height: 24px;">
      <div align="left" class="Estilo1">CONTROL DE STOCK POR PRESENTACION (Descuento de stock)</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: 708px; top: 109px; width: 54px;"
value="Filtrar" onclick="fFiltrar();" />

<input name="CmdB" type="button" id="CmdB"
style="position:absolute; left: 769px; top: 109px; width: 105px;"
value="Eliminar todos" onclick="ELI_TODO();" />


<div align="left" class="zz2"></div>


<div class="zz2" style="position:absolute; left: 40px; top: 138px; width: 503px; height: 25px;">
<input name="ChP" type="checkbox" id="ChP"   value="1" checked="checked"
<?php
if($Str_DINERO=="1"){
	echo "checked=\"checked\"";
}
?>
/> 
PREGUNTAR ANTES DE ELIMINAR
</div>


<div style="position:absolute; left:43px; top: 89px; width: 112px;">
<div align="left" class="zz2">EMPRESA</div>
</div>
<select name="CboEmp" id="CboEmp" 
style="position:absolute; left:43px; top:109px; width: 124px;" 
onchange="EM_OC();"
onkeydown="return SoloTextoEx(event,'CboAlm');" tabindex="1">
<?php 
	echo LE($StrEmp,"N","S"); 
?>
</select>

<div style="position:absolute; left: 179px; top: 89px; width: 112px;">
<div align="left" class="zz2">ALMACEN</div>
</div>
<select name="CboAlm" id="CboAlm" 
style="position:absolute; left: 176px; top: 109px; width: 118px;" 
onkeydown="return SoloTextoEx(event,'TxtCod');">
<?php echo LA($StrEmp,$StrAlm,"N","N"); ?>
</select>

<div id="layer6" style="position:absolute; left: 304px; top: 89px; width: 93px;">
<div align="left" class="zz2">CODIGO</div>
</div>
<input name="TxtCod" type="text" id="TxtCod" 
style="position:absolute; left: 302px; top: 109px; width: 99px;" value="<?php echo $StrCod;?>"  
onkeydown="return SoloTextoEx(event,'TxtDes');"
tabindex="1" />

<div style="position:absolute; left: 407px; top: 89px; width: 131px;">
<div align="left" class="zz2">DESCRIPCION</div>
</div>

<div style="position:absolute; left: 587px; top: 89px; width: 136px;">
<div align="left" class="zz2">LINEA</div>
</div>
<input name="TxtDes" type="text" id="TxtDes" style="position:absolute; left: 409px; top: 109px; width: 167px;" 
value="<?php echo $StrDes;?>"  
onkeydown="return SoloTextoEx(event,'CboMar');"
tabindex="1" />

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



</form>

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