<?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']);
	//-------------------------
	$StrONE=trim(V($_GET['CmdB']));	
	//-------------------------
	$StrEmp=V($_POST['CboEmp']);		
	$StrAlm=V($_POST['CboAlm']);		
	$StrCod=V($_POST['TxtCod']);		
	$StrDes=V($_POST['TxtDes']);
	$StrMar=V($_POST['CboMar']);
	$StrSto=V($_POST['CboSto']);
	//
	if(strlen($StrCmd)==0)$StrCmd=V($_GET['TxtAc']);
	if(strlen($StrMar)==0)$StrMar=GetSO("lin_nombre","lineas","	id_linea>0","lin_nombre");	
	if(strlen($StrSto)==0)$StrSto="TODOS";	
	//	
	$StrCodT=$StrCod."%";
	$StrDesT=$StrDes."%";
	$StrMarT=$StrMar;
	if($StrMarT=="TODOS") $StrMarT="%";
	//	
	if($StrSto=="TODOS"){
		$FF="ID>0 AND COD LIKE '$StrCodT' AND DESCRIPCION LIKE '$StrDesT' AND LINEA LIKE '$StrMarT' AND ALMC LIKE '$StrAlm' ";
	}
	if($StrSto=="MAYOR A CERO"){
		$FF="ID>0 AND COD LIKE '$StrCodT' AND DESCRIPCION LIKE '$StrDesT' AND LINEA LIKE '$StrMarT' AND ALMC LIKE '$StrAlm' AND C_CAJA>0 ";
	}
	if($StrSto=="IGUAL A CERO"){
		$FF="ID>0 AND COD LIKE '$StrCodT' AND DESCRIPCION LIKE '$StrDesT' AND LINEA LIKE '$StrMarT' AND ALMC LIKE '$StrAlm' AND C_CAJA=0 ";
	}
	if($StrSto=="MENOR A CERO"){
		$FF="ID>0 AND COD LIKE '$StrCodT'  AND DESCRIPCION LIKE '$StrDesT' AND LINEA LIKE '$StrMarT' AND ALMC LIKE '$StrAlm' AND C_CAJA<0 ";
	}
	//-------------------------
	if($StrCmd=="G"){
		//BIT
		$_BIT_MOD_ID="0";
		$_BIT_SQL_CAB="SQL";
		$_BIT_SQL_DET="";
		$_BIT_PHP="rep_stock_actualizar.php";
		$_BIT_ID=BIT_ADD("INVETARIO","REPORTE DE STOCK PRESENTACION","GUARDAR",$_BIT_MOD_ID,$_BIT_SQL_CAB,$_BIT_SQL_DET,$_BIT_PHP);
		BIT_DET_ADD($_BIT_ID,"");
		//BIT	
		$FEC=date("Y-m-d");
		$IdAlm=gn("id_almacen","almacenes","alm_obs='$StrAlm'");		  	
		$s="SELECT ID,COD,LINEA,DESCRIPCION,MARCA,C_CAJA,C_UNI
			FROM vis_stock_presentaciones 
			WHERE ".$FF." ORDER BY COD ";		
		$result = grs($s);
		while ($row = $result->fetch_row()){
			//
			$_ID=vbr("0000".$row[0],5);	
			$_PI=vbr("0000".$row[6],5);	
			$TXT="Txt".$_ID.$_PI;
			//
			$IDP=$row[0];
			$PIE=$row[6];
			$NPre=V($_POST[$TXT]);
			if(strlen($NPre)>0){			
				//--
				$ALM=$IdAlm;
				$PRO=$IDP;
				$PIE=$PIE;
				$STO=$NPre;
				$STA=gn("sp_cantidad",
					    "stocks_presentaciones",
					    "sp_id_almacen=$ALM AND 
					     sp_id_producto=$PRO AND sp_presentacion=$PIE");			
				$DIF=$STA - $STO;
				if($DIF!=0){
					$ACC="A";
					if($DIF>0)$ACC="Q";
					$STO=abs($DIF);	
					if($ACC=="A"){
						ZTOCK_ADD($ALM,$PRO,$STO,$PIE);
						STOCK_ADD($ALM,$PRO,$PIE * $STO);
						$s="UPDATE t_productos SET pro_sto=pro_sto + $STO WHERE pro_id=$PRO AND pro_pie=$PIE";
						ejt($s);
						BIT_DET_ADD($_BIT_ID,$s);
					}else{
						ZTOCK_SUB($ALM,$PRO,$STO,$PIE);
						STOCK_SUB($ALM,$PRO,$PIE * $STO);
						$s="UPDATE t_productos SET pro_sto=pro_sto - $STO WHERE pro_id=$PRO AND pro_pie=$PIE";
						ejt($s);
						BIT_DET_ADD($_BIT_ID,$s);
					}
					$GII=GenGI();	
					$ID=GenId("id_ajuste","ajustex");
					$s="INSERT INTO ajustex ".
					   "VALUES ($ID,'$FEC',$ALM,$PRO,'$ACC',$STO,$PIE,$GII,'INVENTARIO',0)";
					ejt($s);
					BIT_DET_ADD($_BIT_ID,$s);					
				}				
			}
		}				
	}
	include("rep_stock_actualizar_fun.php");
	//-------------------------
	$LStrTR=gn("count(*)","vis_stock_presentaciones",$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>Actualizar stock por presentacion</title>
<?php AddJs();?>
<script type="text/javascript" src="fe/funcionez.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();
}
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>

<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;
}
.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;}
.z4 {color: #000099}
.Estilo5 {color: #000000}
#Frm center #DivFRM #layer3 .f2 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_stock_actualizar.php">

<div class="TITULO BLANCO" style=" left: 100px; top: 30px; width: 412px;">
REPORTE DE STOCK POR PRESENTACION</div>

<div class="ps" style=" left: 41px; top: 25px; width: 64px;">
<img src="img/buscar.png" width="57" height="60" /></div>

<div id="DivH">
<input name="TxtId" type="hidden" id="TxtId" value="<?php echo $StrId;?>"/>
<input name="TxtAc" type="hidden" id="TxtAc" value="" />
<input name="TxtPag" type="hidden" id="TxtPag" value="<?php echo $LStrPag; ?>" />
</div>

<div class="s1" style="left: 34px; top: 95px; width: 931px;">SE RECOMIENDA APLICAR LOS FILTROS NECESARIOS, EL SISTEMA ACEPTA UN MAXIMO DE 800 REGISTROS POR ACTUALIZACION EN UN SOLO MOMENTO</div>

<div class="s1" style="left:30px; top: 119px; width: 103px;">EMPRESA</div>
<select name="CboEmp" id="CboEmp" 
style="position:absolute; left:30px; top:138px; width: 124px;" 
onchange="EM_OC();"
onkeydown="return SoloTextoEx(event,'CboAlm');" tabindex="1">
<?php 
	echo LE($StrEmp,"N","S"); 
?>
</select>

<div class="s1" style="left: 169px; top: 119px; width: 103px;">ALMACEN</div>
<select name="CboAlm" id="CboAlm" 
class="lt" style="position:absolute; left: 166px; top: 138px; width: 108px;" 
onkeydown="return SoloTextoEx(event,'TxtCod');"
tabindex="<?php echo ++$TI;?>">
<?php echo LA($StrEmp,$StrAlm,"N","N"); ?>
</select>

<div class="s1" style="left: 282px; top: 119px; width: 91px;">CODIGO</div>
<input name="TxtCod" type="text" id="TxtCod" style="position:absolute; left: 279px; top: 138px; width: 90px;" 
class="lt" value="<?php echo $StrCod;?>"  
tabindex="<?php echo ++$TI;?>" />

<div class="s1" style="left: 378px; top: 119px; width: 163px;">DESCRIPCION</div>
<input name="TxtDes" type="text" id="TxtDes" 
class="lt" style="position:absolute; left: 377px; top: 138px; width: 163px;" 
value="<?php echo $StrDes;?>"  
tabindex="<?php echo ++$TI;?>" />

<div class="s1" style="left: 548px; top: 119px; width: 102px;">LINEA</div>
<select name="CboMar" id="CboMar" 
class="lt" style="position:absolute; left: 546px; top: 137px; width: 104px;" 
tabindex="<?php echo ++$TI;?>">
<?php	
echo FCBO_("lin_nombre","lineas","id_linea>0",$StrMar,"S","N");
?>
</select>

<div class="s1" style="left:659px; top: 119px; width: 102px;">RANGO</div>
<select name="CboSto" id="CboSto" 
class="lt" style="position:absolute; left:659px; top: 137px; width: 149px;" 
tabindex="<?php echo ++$TI;?>">
<?php	
echo FCBOO_("au_item","auxiliar","au_tab='RANGO'",$StrSto,"S","N","au_orden");
?>
</select>


<div id="DivBtn" class="ps" style="left: 100px; top: 50px; width: 704px;">


<input name="CmdB" type="button" id="CmdB" 
style="position:absolute; left: 720px; top: 87px; width: 54px;"
value="Filtrar" onclick="fFiltrar();" 
tabindex="<?php echo ++$TI;?>" />

<input name="CmdPDF" type="button" id="CmdPDF" 
value="Generar PDF" onclick="fGenPDF();"  
tabindex="<?php echo ++$TI;?>" />

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

<input name="CmdV" type="button" id="CmdV" 
class="bt" value="Volver" onclick="fVolver('menu.php');" />
</div>



<div  class="datagrid" style="position:absolute; left: 30px; top: 169px; width: 918px; height: 391px; overflow:auto">
<table id="Exportar_a_Excel" width="817" height="19" border="1" cellpadding="0" cellspacing="0">
<tr>
<td height="18" colspan="7" bgcolor="#666666" ><?php echo PagBtn($LStrTR,$VP_RegXPag,$LStrPag);?>
</td>
</tr>

<tr>
<td width="34" bgcolor="#999999"><span class="z4">Nº</span></td>
<td width="171" bgcolor="#999999"><span class="z4">CODIGO</span></td>
<td width="289" bgcolor="#999999"><span class="z4">DESCRIPCION</span></td>
<td width="184" bgcolor="#999999"><span class="z4">LINEA</span></td>
<td width="63" bgcolor="#999999"><span class="z4">CANT</span></td>
<td width="76" bgcolor="#999999"><span class="z4">PIEZAS</span></td>
<td width="85" bgcolor="#999999"><span class="z4">INVENTARIO</span></td>
</tr>

<?php
	$CX=0;
    $IdAlm=gn("id_almacen","almacenes","alm_obs='$StrAlm'");		  	
    $s="SELECT ID,COD,LINEA,DESCRIPCION,MARCA,C_CAJA,C_UNI
        FROM vis_stock_presentaciones WHERE ".$FF."	ORDER BY COD,C_UNI ";	
	//echo $s."<br>";				
    $s=PagSql($s,$LStrPag,$VP_RegXPag,$LStrTR);				
    $result = grs($s);
    while ($row = $result->fetch_row()){
		$_ID=vbr("0000".$row[0],5);	
		$_PI=vbr("0000".$row[6],5);	
		$TXT="Txt".$_ID.$_PI;
		//$S_STOCK=$row[5]*$row[6];
		$IdP=$row[0];
		$CX++;
	?>
    <tr>
    <td bgcolor="#FFFFFF"><?php echo $CX;?></td>
    <td bgcolor="#FFFFFF"><?php echo $row[1];?></td>
    <td bgcolor="#FFFFFF"><?php echo $row[3];?></td>
    <td bgcolor="#FFFFFF"><?php echo $row[2];?></td>
	<td bgcolor="#FFFFFF"  style="text-align:right"><?php echo $row[5];?></td>
	<td bgcolor="#FFFFFF"  style="text-align:right"><?php echo $row[6];?></td>
    <td bgcolor="#FFFFFF"  style="text-align:right">
    <input name="<?php echo $TXT;?>" type="text" id="<?php echo $TXT;?>" 
    class="ne" style=" position:inherit; width:75px" 
    value=""   onfocus="this.select();"    tabindex="<?php echo ++$TI;?>" />
	</td>
    </tr>
	<?php
    }
?>
</table>
</div>
</form>

<?php
	if(strlen($StrDESCARGAR)>0){
		echo "<div id=\"layer51\" 
			  style=\"position:absolute; left: 101px; top: 83px; 
			  width: 90px; background-color: #FFCC00; layer-background-color: #FFCC00; 
			  border: 1px none #000000; height: 17px;\">";
		echo "<div align=\"center\" class=\"f2\"> ";
		echo " <a href=\"$StrDESCARGAR\" download>Descarga.PDF</a> ";
		echo "</div>";
		echo "</div>";
	}
?>


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