<?php
session_start();
if (!isset($_SESSION["user_id"]) || $_SESSION["user_id"] == null):
    print "<script>alert(\"Acceso Invalido!\");
    window.location='login.php';</script>";
endif;
if ($_SESSION["user_at"] < 2):
    print "<script>alert(\"Sin Acceso a Transaccion!\");
    window.location='vt000.php';</script>";
endif;
if ($_SESSION["user_ac"] == 0 || $_SESSION["user_ac"] == null):
    print "<script>alert(\"Sin Asignar Almacen de Ingreso!\");
    window.location='vt000.php';</script>";
endif;
date_default_timezone_set('America/La_Paz');
include_once ("lib/fpdf/funci.php");
$coalm = $_SESSION["user_ac"];
$conex = conectar();
$fereg = date("d-m-Y");
$fesis = date("Y-m-d");
$xusua = $_SESSION["user_id"];
?>
<html>
<head>
   <meta charset="utf-8">
   <div id="resultadoBusqueda"></div>
   <script src="js/jquery-1.9.1.min.js"></script>
   <script>
   $(document).ready(function() {
       $("#resultadoBusqueda").html('<p><center><strong>Digitar Codigo</strong></center></p>');
   });
   function buscar() {
      var textoBusqueda = $("input#xcodi").val();
      if (textoBusqueda != "") {
         $.post("vt122.php", {valorBusqueda: textoBusqueda}, function(mensaje) {
             $("#resultadoBusqueda").html(mensaje);
         });
      } else { 
         $("#resultadoBusqueda").html('<p><center><strong>Digitar Codigo</strong></center></p>');
      };
   };
   </script>
   <div class="main-wrapper">
   <link href="css/tabla.css" rel="stylesheet" type="text/css" />
   <link href="bootstrap/css/bootstrap.css" rel="stylesheet" />
   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
   <script>
   $(function() {
      $("#feven").datepicker({
         dateFormat: 'dd-mm-yy'});
   });
   </script>
   <script>
   function validanum(e) {
      tecla = (document.all) ? e.keyCode : e.which;
      //Tecla de retroceso para borrar, siempre la permite
      if (tecla==8 || tecla==13){
         return true;
      }
      // Patron de entrada, en este caso solo acepta numeros
      patron =/[0-9.]/;
      tecla_final = String.fromCharCode(tecla);
      return patron.test(tecla_final);
   }
   </script>
   <script>
   function validalet(e) {
      tecla = (document.all) ? e.keyCode : e.which;
      //Tecla de retroceso para borrar, siempre la permite
      if (tecla==8 || tecla==13){
         return true;
      }
      // Patron de entrada, en este caso solo acepta letras
      patron =/[A-Za-z/ ]/;
      tecla_final = String.fromCharCode(tecla);
      return patron.test(tecla_final);
   }
   </script>
<style type="text/css">   
   .campos{
      border-radius: 5px;
      border: 1px solid #39c;
      text-align: center;
      font-family: Arial;
      font-size: 10pt;
      background-color: #E9E9E9;
   }

   .redondo{
      border-radius: 5px;
      border: 1px solid #39c;
   }

   .select{
      border-radius: 5px;
      border: 1px solid #39c;
   }
</style>
</head>
<div class="col-xs-12">
<center><h1>Reabastecimiento de Productos</h1></center>
<form id="abastecer" method="post" autocomplete="off">
   <center><table bgcolor= "#E0FFFF" border="1" width="52%" cellpadding="1" cellspacing="1" class="redondo">
   <tr height="50">
      <td>&nbsp;&nbsp;&nbsp;
      <FONT FACE="times new roman"><strong>Fecha de Registro</strong></FONT>
      &nbsp;
      <input name="fereg" type="text" class="campos" value="<?php echo $fereg;?>" id="fereg" size="10" maxlength="10" disabled=true>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <FONT FACE="times new roman"><strong>Codigo</strong></FONT>
      &nbsp;
      <input name="xcodi" required id="xcodi" class="campos" type="text" autofocus size="16" maxlength="15" onKeyUp="buscar();">
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <FONT FACE="times new roman"><strong>Cantidad</strong></FONT>
      &nbsp;
      <input name="canti" required type="text" class="campos" size="12" maxlength="12">
      </td>
   </tr>
   <tr height="50">
      <td>&nbsp;&nbsp;&nbsp;
      <FONT FACE="times new roman"><strong>Precio de Compra</strong></FONT>
      &nbsp;
      <input name="prcom" required type="text" class="campos" size="12" maxlength="12">
      &nbsp;&nbsp;
      <FONT FACE="times new roman"><strong>Lote</strong></FONT>
      &nbsp;
      <input name="xlote" type="text" class="campos" size="12" maxlength="12">
      &nbsp;&nbsp;
      <FONT FACE="times new roman"><strong>Fecha de Vencimiento</strong></FONT>
      &nbsp;
      <input type="text" name="feven" id="feven" class="campos" size="10">
      </td>
   </tr>
   <tr height="50">
      <td>
      <center><button type="submit" name="enviar" class="btn btn-success">Grabar el Formulario</button>
      &emsp;&emsp;&emsp;&emsp;&emsp;
      &emsp;&emsp;&emsp;&ensp;&nbsp;
      <a href="vt000.php" class="btn btn-danger">Salir del Formulario</a></center>
      </td>
   </tr>
</center></table>
</form>
</div>
</html>
<?php
if (isset($_POST["enviar"])):
    if ($_POST["xcodi"] == "" or $_POST["canti"] == "" or $_POST["prcom"] == ""):
        print '<script language="JavaScript">';
        print 'alert("Llenar los Campos Requeridos");';
	print '</script>';
    else:
	$xcodi = trim(strip_tags($_POST["xcodi"]));
        $bupro = mysql_query("select * from inart
                              where inartcart = '$xcodi'
                              and   inartstat = 0");
        if (mysql_num_rows($bupro) > 0):
           $linea = mysql_fetch_array($bupro);
           $vefec = $linea["inartlofe"];
	   $canti = trim(strip_tags($_POST["canti"]));
           $prcom = trim(strip_tags($_POST["prcom"]));
           $xlote = trim(strip_tags($_POST["xlote"]));
           $feven = trim(strip_tags($_POST["feven"]));
           $fereg = date("Y-m-d",strtotime($fereg));
           $feven = trim(strip_tags($_POST["feven"]));
           $feven = date("Y-m-d",strtotime($feven));
           $gesti = date("Y");
           $tocom = ($prcom * $canti);
           $valor = 0;
           if ($vefec == 1):
              if ($_POST["xlote"] == "" or $_POST["feven"] == ""):
                 $valor = 1;
                 print '<script language="JavaScript">';
                 print 'alert("Falta Lote y/o Fecha de Vencimiento");';
	         print '</script>';
              elseif ($fereg >= $feven):
                 $valor = 1;
                 print '<script language="JavaScript">';
                 print 'alert("Fecha de Venc. debe ser Mayor a Fecha Actual");';
                 print '</script>';
              endif;
           else:
              $feven = "";
	      $xlote = "";
           endif;
           if ($valor == 0):
              $upuno = "update inart set inartcant = (inartcant + '$canti'),
                                         inartpcom = '$prcom'
                        where  inartcart = '$xcodi'
                        and    inartstat = 0";
              mysql_query("lock tables inart write");
              mysql_query($upuno);
              mysql_query("unlock tables");
              $busco = mysql_query("select * from inalm
                                    where inalmcart = '$xcodi'
                                    and   inalmcalm = '$coalm'
                                    and   inalmlote = '$xlote'
                                    and   inalmfven = '$feven'
                                    and   inalmstat = 0");
              if (mysql_num_rows($busco) > 0):
                 $milin = mysql_fetch_array($busco);
                 $mirow = $milin[0];
                 $updal = "update inalm set inalmcant = (inalmcant + '$canti')
                           where  inalmcorr = '$mirow'";
                 mysql_query("lock tables inalm write");
                 mysql_query($updal);
                 mysql_query("unlock tables");
              else:
                 $insal = "insert into inalm values (0,'$coalm','$xcodi','$canti','$xlote',
                                                    '$feven','$fereg','$xusua',0)";
                 mysql_query("lock tables inalm write");
                 mysql_query($insal);
                 mysql_query("unlock tables");
              endif;
              $cocab = "insert into cocab values (0,'$coalm','$fereg',1,'$tocom',
                                                 '$gesti','$xusua',0)";
              mysql_query("lock tables cocab write");
              mysql_query($cocab);
              $buide = mysql_query("select max(cocabntra) as mayor from cocab");
              $miide = mysql_fetch_array($buide);
              $nutra = $miide["mayor"];
              mysql_query("unlock tables");
              $codet = "insert into codet values (0,'$nutra','$xcodi','$canti',
                                                 '$prcom','$tocom')";
              mysql_query("lock tables codet write");
              mysql_query($codet);
              mysql_query("unlock tables");
              $incab = "insert into incab values (0,'$nutra','$coalm','$fereg',1,1,
                                                 'INGRESO POR COMPRA','$gesti','$xusua',0)";
              mysql_query("lock tables incab write");
              mysql_query($incab);
              mysql_query("unlock tables");
              $indet = "insert into indet values (0,'$nutra','$coalm','$xcodi','$canti')";
              mysql_query("lock tables indet write");
              mysql_query($indet);
              mysql_query("unlock tables");
              print '<script language="JavaScript">';
              print 'alert("La Informacion de Grabo");';
              print '</script>';
           endif;
        else:
           print '<script language="JavaScript">';
           print 'alert("Producto de Baja");';
	   print '</script>';
        endif;
    endif;
endif;
?>
