<?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']);		
	$StrDes=V($_POST['TxtDes']);
	$StrMar=V($_POST['CboMar']);
	$CboSto=V($_POST['CboSto']);
	//
	$Fab=V($_POST['Fab']);
	$Mod=V($_POST['Mod']);
	$Lin=V($_POST['Lin']);
	$Lin_=LIKE($Lin);
	$Fab_=LIKE($Fab);
	$Mod_=LIKE($Mod);
	
	$IdUsu=$_SESSION['SYS_USER_ID'];
	//
	$STR_GENERAR="";
	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."%";
	$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_' AND LINEA LIKE '$Lin_'";
	//-------------------------
	if($StrCmd=="G"){
		$IdAlm=gn("id_almacen","almacenes","alm_nombre='$StrAlm'");		  	
		$s="SELECT ID,COD,LINEA,DESCRIPCION,MARCA,STOCK 
			FROM v_stocks WHERE ".$FF."	ORDER BY DESCRIPCION,MARCA ";
		$result = grs($s);
		while ($row = $result->fetch_row()){
			$IDP=$row[0];
			$NPre=V($_POST[Txt.$IDP]);
			if(strlen($NPre)>0){
				$NPre=vbval($NPre);
				//
				$AX_FEC=date("Y-m-d");
				$IDP=$row[0];
				$ALM_=$IdAlm;
				$INV=$NPre;
				$STOCK=gn("stock","stocks","id_almacen=$ALM_ AND id_producto=$IDP");
				$AX_ID=GenId("id_ajuste","ajustex");
				$AX_DIF=abs($STOCK - $INV);
				if($AX_DIF!=0){
					if($STOCK>$INV){						
						ejt("INSERT INTO ajustex VALUES ($AX_ID,".
						"'$AX_FEC',$ALM_,$IDP,'Q',$AX_DIF)");
					}else{
						ejt("INSERT INTO ajustex VALUES ($AX_ID,".
						"'$AX_FEC',$ALM_,$IDP,'A',$AX_DIF)");
					}
					ejt("UPDATE stocks SET stock=".$INV." WHERE ".
					"id_producto=$IDP AND id_almacen=".$ALM_);
				}
				//
				if(GetC("stocks","id_almacen=$IdAlm AND id_producto=$row[0]")>0){										
					/*$Sql="INSERT INTO stocks VALUES 
					($IdAlm,$row[0],$NPre)";
					ejt($Sql);	
					echo "A"." ".$Sql;
					
					$Sql="UPDATE stocks SET stock=$NPre";
					ejt($Sql);
					echo "A"." ".$Sql;
								
					}else{*/					
					$Sql="UPDATE stocks SET stock=$NPre 
					WHERE id_almacen=$IdAlm AND 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);
	//-------------------------
	if($StrCmd=="PDF"){	
		//
		$TI="REPORTE DE STOCK - ALMACEN : $StrAlm" ;
		$s="SELECT COD,DESCRIPCION,MARCA,STOCK FROM v_stocks
			WHERE $FF ORDER BY DESCRIPCION,MARCA ";
		$T="CODIGO,DESCRIPCION,MARCA,STOCK";
		$E="12,47,18,6";
		$A=" 0, 0, 0,2";		
		//
		$_ID=GenId("id","reportes");
		PDF_TIT($_ID,$TI);
		PDF_SQL($_ID,$s);
		PDF_CAM($_ID,$T);
		PDF_ESP($_ID,$E);
		PDF_ANC($_ID,$A);		
		$StrFilePDF=PDF_CREATE($_ID);	
		$StrMsg="onload=\"alert('Ahora puede descargar el reporte');\"";
}
	$LStrFoc="document.getElementById('CboAlm').focus();";
	$LStrMsg="onload=\"$LStrFoc;\"";
	

$AT=gs("at","usuarios","id_usuario=$IdUsu");
?>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Actualizacion de stock</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("CboAlm");
});

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(){
	if(confirm("Esta seguro que desea Guardar los Cambios")==true){
		document.getElementById("TxtAc").value="G";
	}else{
		return;
	}
	document.getElementById("Frm").submit();
}


$(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="inv_plantilla2.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">ACTUALIZACION DE STOCK</div>
</div>

<!----------------------------------------->

<div class="s1" style="left: 35px; top: 121px; width: 102px;">
ALMACEN</div>
<select name="CboAlm" id="CboAlm" 
class="lt" style="left: 35px; top: 138px; width: 212px" 
tabindex="<?php echo ++$TI;?>">
<?php 
	echo ListALM($StrAlm); 
?>
</select>

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

<div class="s1" style="left: 369px; top: 121px; width: 160px;">
FABRICANTE</div>
<select name="Fab" id="Fab" 
class="lt" style="position: absolute; left: 367px; top: 138px; width: 146px;"
tabindex="<?php echo ++$TI;?>">
<?php	echo FCBO_("fab_nom","fabricantes","id_fab>0",$Fab,"S","N")?>  
</select>

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

<div class="s1" style="left: 644px; top: 121px; width: 90px;">
LINEA</div>
<select name="Lin" id="Lin" 
class="lt" style="position: absolute; left: 644px; top: 138px; width: 120px;"
tabindex="<?php echo ++$TI;?>">
<?php	echo FCBO_("lin_nombre","lineas","id_linea>0",$Lin,"S","N")?>  
</select>

<div class="s1" style="left: 774px; top: 121px; width: 90px;">
DESCRIPCION</div>
<input name="TxtDes" type="text" id="TxtDes" 
class="lt" style="left: 774px; top: 138px; width: 138px;"
value="<?php echo $StrDes;?>"  
tabindex="<?php echo ++$TI;?>" />

<div class="s1" style="left: 923px; top: 121px; width: 108px;">
MARCA</div>
<select name="CboMar" id="CboMar" 
class="lt" style="position: absolute; left: 921px; top: 138px; width: 115px;"
tabindex="<?php echo ++$TI;?>">
<?php	echo FCBO_("mar_nombre","marcas","id_marca>0",$StrMar,"S","")?>  
</select>

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

<!----------------------------------------->
<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;?>" />

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

<input name="CmdG" type="button" id="CmdG" class="btm" 
value="Guardar" onClick="fGuardar();" 
tabindex="<?php echo ++$TI;?>"/>
</div>
    
<!----------------------------------------->

<div class="datagrid" style="position: absolute; left: 35px; top: 165px; width: 1180px; height: 405px; overflow: auto">
<table id="Exportar_a_Excel" width="1143" height="5" border="2" cellpadding="0" cellspacing="0" style="color:#000">
<thead>
<tr>
<td colspan="8" align="center"><?php echo PagBtn($LStrTR,$VP_RegXPag,$LStrPag);?></td>
</tr>

<tr>
<th width="69" height="17" bgcolor="#999999">CODIGO</th>
<th width="144" bgcolor="#999999">LINEA</th>
<th width="152" bgcolor="#999999">FABRICANTE</th>
<th width="158" bgcolor="#999999">MODELO</th>
<th width="381" bgcolor="#999999">DESCRIPCION</th>
<th width="125" bgcolor="#999999">MARCA</th>
<th width="62" bgcolor="#999999">STOCK</th>
<th width="67" bgcolor="#999999">N.STOCK</th>
</tr>

</thead>
<tbody>
<?php		  		
    $IdAlm=gn("id_almacen","almacenes","alm_nombre='$StrAlm'");	
	//	  	
    $s="SELECT ID,COD,
	SUBSTRING(LINEA,1,20) AS LINEA,
	SUBSTRING(DESCRIPCION,1,50) AS DESCRIPCION,
	SUBSTRING(MARCA,1,16) AS MARCA,STOCK,
	SUBSTRING(FAB,1,25) AS FAB,
	SUBSTRING(MODELO,1,22) AS MODELO 
	FROM v_stocks WHERE ".$FF."	ORDER BY DESCRIPCION,MARCA ";
				
    $s=PagSql($s,$LStrPag,$VP_RegXPag,$LStrTR);				
    $result = grs($s);
    while ($row = $result->fetch_row()){
		$C++;
		$IdP=$row[0];
		?>
		<tr>
		<td bgcolor="#FFFFFF">'<?php echo $row[1]?></td>
        <td bgcolor="#FFFFFF"><?php echo $row[2]?></td>
        <td bgcolor="#FFFFFF"><?php echo $row[6]?></td>
		<td bgcolor="#FFFFFF"><?php echo $row[7]?></td>
		<td bgcolor="#FFFFFF"><?php echo $row[3]?></td>
		<td bgcolor="#FFFFFF" ><?php echo $row[4]?></td>
		<td bgcolor="#FFFFFF"  style="text-align:right"><?php echo $row[5]?></td>
		<td bgcolor="#FFFFFF" >
        <input name="Txt<?php echo $IdP;?>" type="text" id="Txt<?php echo $IdP;?>" 
        class="ndd" style=" position:inherit; width:55px" 
        value="<?php echo $StrD[$IdP];?>"  
        tabindex="<?php echo $TI++;?>" />
		</td>
		</tr>
		<?php	
    }

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

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

<div class="s1" style="left: 1006px; top: 575px; width: 147px;">
Total Registros: <?php echo $C; ?></div>

</form>

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

<?php
if($StrCmd=="GEN_PLA"){
	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', 'STOCK')
				->setCellValue('F1', 'INVENTARIO');
	//
	$F=1;						 
	$s="SELECT ID,COD,LINEA,DESCRIPCION,MARCA,STOCK 
        FROM v_stocks WHERE ".$FF."	ORDER BY DESCRIPCION,MARCA ";
    $result = grs($s);
    while ($row = $result->fetch_row()){
		$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, '');
	}	
	// 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_inventario.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){
$_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,"REPORTE DE STOCK",0,1);	
$pdf->Cell(0,4,"",0,1);	
//------------------------------------------

if(strlen($StrAlm)>0){
if($StrAlm=="%")$StrAlm="TODOS";
$_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),25);
$_ENC=$_ENC." ".vbr(vbfill(" ",10)."STOCK",10);

$pdf->Cell(0,4,$_ENC,0,1);	
$pdf->Cell(0,4,vbfill("-",$_CxL),0,1);	
//
$s="SELECT ID,COD,LINEA,DESCRIPCION,MARCA,STOCK 
    FROM v_stocks WHERE ".$FF."	ORDER BY DESCRIPCION,MARCA "; 

$result = grs($s);
while ($row = $result->fetch_row()){
	$_ENC=vbl($row[1].vbfill(" ",12),12);
	$_ENC=$_ENC." ".vbl($row[3].vbfill(" ",40),40);
	$_ENC=$_ENC." ".vbl($row[4].vbfill(" ",25),25);
	$_ENC=$_ENC." ".vbr(vbfill(" ",10).NF2($row[5],1),10);
	$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="Reporte de Stock.pdf";
$pdf->Output($PDF_NAME);		
return $PDF_NAME;
}
?>

<?php
if(strlen($StrDESCARGAR)>0){
echo "<div id=\"layer51\" 
	  style=\"position:absolute; left: 128px; top: 102px; 
	  width: 88px; background-color: #FFCC00; layer-background-color: #FFCC00; 
	  border: 1px none #000000; height: 17px;\">";
echo "<div align=\"center\" class=\"Estilo2\"> ";
echo " <a href=\"$StrDESCARGAR\" download>Desc.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_inventario.xls\" download>Descargar Plantilla</a> ";
echo "</div>";
echo "</div>";
}
?>

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

