<?php
 @session_start();
  $vg_login=$_SESSION["usuario"];
  if($vg_login<>""){}
 else
  {echo "<script>";
   echo "window.location='../index.php';";
   echo "</script>";
  }	
include "../valida_permisos.php";
$vg_programa = F_programa();
include "../seguridad.php";
$ip=getenv("REMOTE_ADDR");
?>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title>SIVI</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<LINK REL="SHORTCUT ICON" HREF="../sivi.ico">
<LINK href="../general.css" type=text/css rel=stylesheet>
<link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet">
<script src="../bootstrap/js/bootstrap.min.js"></script>

 <script language="javascript">
function format(input)
{
var num = input.value.replace(/\./g,'');
if(!isNaN(num)){
num = num.toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1.');
num = num.split('').reverse().join('').replace(/^[\.]/,'');
input.value = num;
}

else{ //alert('Solo se permiten numeros');
input.value = input.value.replace(/[^\d\.]*/g,'');
}
}
</script>

</head>
<BODY>
<?php
include "../parametros.php";
include "../conexion.php";
$empresa=$_REQUEST["empresa"]; 
$id=$_REQUEST["id"];	
$cantidad="";	$precio=""; 	$proveedor=""; 	$observacion=""; 	$fecha="";			  

$rs_empresa=mysqli_query($conexion,"SELECT id,nombre FROM ubicacion
									 union 
									 select 0,'' from  ubicacion
									 order by nombre asc");
if(isset($_REQUEST["oculto"]))
{ 
	$id=$_REQUEST["id"]; 
	$cantidad=$_REQUEST["cantidad"]; 
	$empresa=$_REQUEST["empresa"]; 
	//$proveedor=$_REQUEST["proveedor"]; 
	//$observacion=$_REQUEST["observacion"]; 
	//$fecha=$_REQUEST["fecha"];   
	//$codpro=$_REQUEST["codpro"];   		

 if($_REQUEST["ocargainventario"]==1)
  {
		
		if($empresa>0)
		{
			$sql_datosrs=mysqli_query($conexion,"SELECT count(*) as contar from producto where codigobarra='".$id."'");
			 while($datos= mysqli_fetch_row($sql_datosrs)){ $contar=$datos[0];}
			 if($contar>=1)
				{
				 $sql_datosrs=mysqli_query($conexion,"SELECT id from producto where codigobarra='".$id."'");
				 while($datos= mysqli_fetch_row($sql_datosrs)){ $idpro=$datos[0];}
				}
	
			 $observacion="Carga Inicial Inventario"; 
			 $fecha=date("Y-m-d");
			 $sql_insert=mysqli_query($conexion,"insert into inventario  (producto,cantidad,observacion,fechaingreso,usuario,fecsis,empresa)
													 values ('".$id."','".$cantidad."','".$observacion."','".$fecsis."','".$vg_login."','".$fecsis."','".$empresa."')");
			 if ($sql_insert) 
			    { 
			        $sql_datosrs=mysqli_query($conexion,"SELECT max(id) from inventario where producto='".$id."'");
				    while($datos= mysqli_fetch_row($sql_datosrs)){ $idbodega=$datos[0];}
				 
                    $factura="";
						$sql_insert=mysqli_query($conexion,"insert into inventario_kardex(producto,fecha,observacion,origen,cantidad,tipoes,idtipoes,fecsis,horasis,usuario)
						  values('".$id."','".$fecsis."','".$observacion."','".$factura."','".$cantidad."','E','".$idbodega."',
						  '".$fecsis."','".$horasis."','".$vg_login."')"); 
				}
			 else { echo "<script>";   echo "alert('Error al guardar el registro')"; echo "</script>"; }
		}
		else
		{
		 echo "<script>";   echo "alert('Debe de seleccioanr la empresa')"; echo "</script>";
		}
		 
  }
 
 
if($_REQUEST["oquita"]==1)
	{   
	    $c=1; $canti=$_REQUEST['canti'];
		  while($c<=$canti)
		   {  if(isset($_REQUEST['ch'.$c])) {$val=1; }	  else {$val=0;} 
			   $idi=$_REQUEST['idi'.$c];
			   if($val==1)
				  {   $sql_delete=mysqli_query($conexion,"delete from  inventario  where id=".$idi."");
					  if($sql_delete) 
					   {
					       $sql_delete=mysqli_query($conexion,"delete from  inventario_kardex  where idtipoes=".$idi." and producto='".$id."'");
					   }
					  else {}
				   }//fin valida chequeo
			  $c=$c+1;
		   }//fin cliclo
  }//fin
 
  if($_REQUEST["Olimpiar"]==1)
  { $id=""; } 
}
$fecha=date("m/d/Y");

?>
<form name="parametros" method="post" action="" >
<div id="informes" class="container text-center" style="width:98%;">
    <div class="panel panel-primary">
      <div class="panel-heading">
        <table width="100%" border="0" cellpadding="0" cellspacing="0">
          <tbody>
            <tr>
              <td width="14%">INVENTARIO EMPRESA.</td> 
              <td width="86%"><select name="empresa" id="empresa"  
                      class="form-control" data-placement="right"  style="width: 100%" 
                      onFocus="this.style.background='#ccffff'" onBlur="this.style.background='#ffffff'" onChange="submit();">
                <?php while ($row=mysqli_fetch_array($rs_empresa))
									{
									 if ($empresa==$row['id'])
										{echo "<option value=".$row['id']." selected>".substr($row['nombre'],0,50)."</option>";}
									 else
										{echo "<option value=".$row['id'].">".substr($row['nombre'],0,50)."</option>";}		
									}
							 ?>
              </select></td>
            </tr>
          </tbody>
        </table>
      </div>
      <div align="center">
      
  <table width="100%" border="0" cellspacing="0" cellpadding="0" class="texto" >
                    <tr> 
                      <td height="0"> Codigo                       </td>
                      <td align="left" valign="middle"><div align="left">
                        <input name="id" type="text"   id="id" value="<?php echo $id; ?>" 
                      class="form-control" data-placement="right"  style="width: 150px" 
                       		onKeyPress="if(event.keyCode==13){Obuscar.value=1;submit();} 
                                if(event.keyCode>=48 && event.keyCode<=58){} else {event.keyCode=8;}
                                " 
                                 
								onFocus="this.style.background='#ccffff'" onBlur="this.style.background='#ffffff'"  autocomplete="off">
                      </div></td>
                      <td><?php

					 
                    if($id<>"")
					  {
					 /* $sql_datosrs=mysqli_query($conexion,"SELECT count(*) as contar from producto where codigobarra='".$id."'");
					  while($datos= mysqli_fetch_row($sql_datosrs)){ $contar=$datos[0];}
					  if($contar>=1)
					  {
					    $sql_datosrs=mysqli_query($conexion,"SELECT id from producto where codigobarra='".$id."'");
					    while($datos= mysqli_fetch_row($sql_datosrs)){ $id=$datos[0];}
					  }
					  */
					  
					  
				      $sql_datosrs=mysqli_query($conexion,"SELECT count(*) as contar from producto where id='".$id."'");
					  while($datos= mysqli_fetch_row($sql_datosrs)){ $contar=$datos[0];}
					  
					  ?>
Cantidad </td>
                      <td><input name="cantidad" type="text"  id="cantidad"   value="<?php echo $cantidad; ?>" 
                      class="form-control" data-placement="right"  style="width: 150px"  
				    autocomplete="off"></td>
                                
                      <td>
                      <?php 
					  if($contar>=1)
					  {
					  ?>
                      <input name='guardar' type='submit'  id='guardar' value='Guardar'   class="btn btn-primary" title="Clic para ver kardex"
						  onClick="ocargainventario.value=1;Obuscar.value=1;" >
                        <input name='cancelar' type='submit'  id='cancelar' value='Cancelar'   class="btn btn-primary" title="Clic para cancelar el proceso"
						  onClick="Olimpiar.value=1;" >
                        <?php
					     }
					   ?></td>
          </tr>
                    <tr> 
                      <td height="0" colspan="5"> 
                        <?php
					
				      $sql_datosrs=mysqli_query($conexion,"SELECT count(*) as contar from producto where id='".$_REQUEST["id"]."'");
					  while($datos= mysqli_fetch_row($sql_datosrs)){ $contar=$datos[0];}
					  if($contar>=1)
					    { $sqlquery=" id='".$_REQUEST["id"]."'";}
					  else
					   {
					     $sql_datosrs=mysqli_query($conexion,"SELECT count(*) as contar from producto where codigobarra='".$_REQUEST["id"]."'");
					     while($datos= mysqli_fetch_row($sql_datosrs)){ $contar=$datos[0];}
						 if($contar>=1)
						    {
							 $sqlquery=" codigobarra='".$_REQUEST["id"]."'";
							 $sql_datosrs=mysqli_query($conexion,"SELECT id from producto where codigobarra='".$_REQUEST["id"]."'");
					         while($datos= mysqli_fetch_row($sql_datosrs)){echo $id=$datos[0];}
							}
					   }
					}	 
					  ?>                      </td>
                    </tr>
                    <tr>
                      <td height="0" colspan="5"> 
                        <div align="left"></div></td></tr>
                    <tr> 
                      <td height="3" colspan="5"> 
                 <?php
				if($id<>"")
				{
				  	  $sql_datosrs=mysqli_query($conexion,"SELECT count(*) as contar from producto where id='".$id."'");
					  while($datos= mysqli_fetch_row($sql_datosrs)){ $contar=$datos[0];}
					  if($contar>=1)
					   {
					   
					   ?><br>
                        <table width="100%" border="1" cellspacing="0" cellpadding="0" class='table table-hover' style="font-size:12px"> 
                          <tr > 
                            <td width="10%" class="btn-primary" > </td>
                            <td width="10%" class="btn-primary">Codigo</td>
                            <td width="10%" class="btn-primary">Cantidad</td>
                            <td width="10%" class="btn-primary">Precio</td>
                            <td class="btn-primary">Observacion</td>
                            <td width="10%" class="btn-primary"> Fecha</td>
                            <td width="15%" class="btn-primary">Registro</td>
                          </tr>
                          <?php

						$sql_datosrs=mysqli_query($conexion,"SELECT count(*) from inventario where producto='".$id."'");
						while($datos= mysqli_fetch_row($sql_datosrs)){ $contar=$datos[0];}
					   if($contar>=1)
					    {					 
									  $sql_datos=mysqli_query($conexion,"SELECT id,cantidad,precio,proveedor,observacion,fechaingreso,
									 						 fecsis,usuario
									 					     from inventario
															 where producto='".$id."' order by id desc");
										$c=1;					 
									   while ($datos=mysqli_fetch_row($sql_datos))//04
										  { 
											$idi=$datos[0];
											$cantidad=$datos[1];
											$precio=$datos[2];
											$proveedor=$datos[3];
											$observacion=$datos[4];
											$fechaingreso=$datos[5];
											$fecsis=$datos[6];
											$usuario=$datos[7];
											
											 echo "<tr>
													  <td>
													   <div align='center'><input type='checkbox' name='ch$c' >
													    <input name='idi$c' type='hidden' class='texto'  value='$idi' size='5' readonly>
													   ";
													   //if($vl_valida==0)
														//{
															?>
                             <input name='eliminar' type='submit'  id='eliminar' value='-'   class="btn btn-primary" title="Clic para Eliminar"
						  onClick="oquita.value=1;submit();" >
														  <?php
                                                         
														 														
														 // }
														echo "</div>
													   </td>
													   <td>$id</td>																					
													  <td><div align='right'>$cantidad</div></td>
													  <td><div align='right'>$precio</div></td>													  
													  <td>$observacion</td>
													  <td>$fechaingreso</td>
													  <td>$usuario - $fecsis</td>
													</tr>";
													$c=$c+1;
										  }	//04
										$canti=$c-1;  
		 					  			echo "<input type='hidden' name='canti' value='$canti' readonly size='10'>";
							
						}
							 ?>
                        </table>
                      <?php
							 }
					}		 
							?>                      </td>
                    </tr>
                    <tr> 
                      <td colspan="5"><div align="center"> 
                          <input name="buscar2" type="hidden" id="buscar2" value="Busca" title="Clic para Buscar" class="btn" onClick="jq_busca_datos_parametros(document.parametros.id.value,'producto');" >
                          <input name="oguardar" type="hidden" id="oguardar">
                          <input name="Obuscar" type="hidden" id="Obuscar">
                          <input name="oquita" type="hidden" id="oquita">
                          <input name="obuscadato" type="hidden" id="obuscadato">
                          <input name="oculto" type="hidden" id="oculto">
                          <input name="ocargainventario" type="hidden" id="ocargainventario">
						  <input name="obuscarcodigo" type="hidden" id="obuscarcodigo">
						  <input name="Olimpiar" type="hidden" id="Olimpiar">	
                          					  						  
                          <br>
                        </div></td>
                    </tr>
                  </table>
</div>
</div>
</div>                  
   <?php
  $sql_datosrs=mysqli_query($conexion,"SELECT count(*) as contar from producto where codigobarra='".$_REQUEST["id"]."'");
  while($datos= mysqli_fetch_row($sql_datosrs)){ $contar=$datos[0];}
  if($contar>=1){}
  else
    { $sql_datosrs=mysqli_query($conexion,"SELECT count(*) as contar from producto where id='".$_REQUEST["id"]."'");
      while($datos= mysqli_fetch_row($sql_datosrs)){ $contar=$datos[0];}
    }
	
 
?>
</form>
<div id="resultado"></div>
</body>
</html>
