<?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_av"] == 0 || $_SESSION["user_av"] == null):
    print "<script>alert(\"Sin Asignar Almacen de Venta!\");
    window.location='vt000.php';</script>";
endif;
include_once ("lib/fpdf/funci.php");
$conex = conectar();
date_default_timezone_set('America/La_Paz');
if (!isset($_SESSION["carrito"])):
   $_SESSION["carrito"] = [];
endif;
$granTotal = 0;
//	  <label>Precio :</label>
//	  <input autocomplete="off" class="redondo" name="precio" size="11" style="text-align:center; font-weight:bold" type="number" step="0.01" placeholder="Precio" onkeypress="return validanum(event)">
//        &emsp;&emsp;&emsp;&emsp;&ensp;&nbsp;&nbsp;&nbsp;&nbsp;
?>
<div id="resultadoBusqueda"></div>
<script src="js/jquery-1.9.1.min.js"></script>
<script>
   $(document).ready(function() {
       $("#resultadoBusqueda").html('<p><center><strong>Cliente</strong></center></p>');
   });
   function buscar() {
      var textoBusqueda = $("input#codcli").val();
      if (textoBusqueda != "") {
         $.post("vt116.php", {valorBusqueda: textoBusqueda}, function(mensaje) {
             $("#resultadoBusqueda").html(mensaje);
         });
      } else { 
         $("#resultadoBusqueda").html('<p><center><strong>Cliente</strong></center></p>');
      };
   };
</script>

<script>
function restar() {
  try {
    var a = parseFloat(document.f.total.value),
        b = parseFloat(document.f.desto.value);
    document.f.tpago.value = a - b;
  } catch (e) {
  }
}
</script>

<script>
function cambio() {
  try {
    var a = parseFloat(document.f.efectivo.value),
        b = parseFloat(document.f.tpago.value);
    document.f.difer.value = a - b;
  } catch (e) {
  }
}
</script>

<script>
   function validanum(e) {
      tecla = (document.all) ? e.keyCode : e.which;
      //Tecla de retroceso para borrar y Enter 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){
         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>

<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>
<link rel="stylesheet" href="/resources/demos/style.css">

<style type="text/css">   
   .boton{
      font-size:12px;
      font-family:Verdana,Helvetica;
      font-weight:bold;
      color:#000000;
      background:#A4C1FF;
      border:5px;
      width:155px;
      height:25px;
      border-radius: 5px;
      border: 1px solid #39c;
   }

   .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>Venta de Productos</h1></center>
     <?php
     if (isset($_GET["status"])):
        if ($_GET["status"] === "1"): /*
	   ?>
	   <div class="alert alert-success">
	        <strong>¡Perfecto!</strong> Venta Realizada Correctamente
	   </div>
	   <?php */
	elseif($_GET["status"] === "2"):
	   ?>
	   <div class="alert alert-info">
		<strong>El Pago Efectivo debe se Mayor a la Venta</strong>
	   </div>
	   <?php
	elseif($_GET["status"] === "3"):
	   ?>
	   <div class="alert alert-info">
		<strong>OK</strong> El Producto a sido quitado de la lista
	   </div>
	   <?php
	elseif($_GET["status"] === "4"):
	   ?>
	   <div class="alert alert-warning">
		<strong>Error:</strong> El producto que buscas no existe
	   </div>
	   <?php
	elseif($_GET["status"] === "5"):
	   ?>
	   <div class="alert alert-danger">
		<strong>Error: </strong> El producto está sin Stock
	   </div>
	   <?php
	elseif($_GET["status"] === "6"):
	   ?>
	   <div class="alert alert-danger">
		<strong>Error: </strong> No Existe Venta Actual
	   </div>
	   <?php
	else:
	   ?>
	   <div class="alert alert-warning">
		<strong>Error:</strong> Falta el Nombre del Cliente
	   </div>
	   <?php
	endif;
     endif;
     ?>
     <form method="post" action="vt112.php">
	  <center><label>Código :</label>
	  <input autocomplete="off" autofocus class="redondo" name="codigo" size="11" style="text-align:center; font-weight:bold" required type="text" placeholder="Código">
          &emsp;&emsp;&emsp;&ensp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	  <label>Cantidad :</label> 
	  <input autocomplete="off" class="redondo" name="canti" size="11" style="text-align:center; font-weight:bold" required type="text" placeholder="Cantidad" onkeypress="return validanum(event)">
          &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
          &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
	  <button type="submit" class="btn btn-success btn-sm">Agregar</button></center>
     </form>
     <center><table class="tabla">
	  <thead>
	  <tr>
	  <th><center>Código</center></th>
	  <th>Nombre Producto</th>
	  <th><center>Cantidad</center></th>
	  <th><center>Precio Venta</center></th>
	  <th><center>Total Item</center></th>
	  <th><center>Opcion</center></th>
	  </tr>
	  </thead>
	  <tbody>
	  <?php foreach($_SESSION["carrito"] as $indice => $producto){ 
			$granTotal += $producto[total];
	  ?>
	  <tr class="modo1">
	  <td><center><?php echo $producto[inalmcart]?></center></td>
	  <td><?php echo $producto[inartnart]?></td>
	  <td><center><?php echo number_format($producto[mican],2)?></center></td>
	  <td><center><?php echo $producto[prven]?></center></td>
	  <td><center><?php echo number_format($producto[total],2)?></center></td>
	  <td><center><a class="btn btn-danger btn-xs" href="<?php echo "vt113.php?indice=" . $indice?>">Quitar<i class="fa fa-trash"></i></a></center></td>
	  </tr>
	  <?php } ?>
	  </tbody>
     </table></center>
     <h3>
     <center>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
     &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
     Total Venta : <?php echo number_format($granTotal,2); ?></h3></center>
     <form name="f" action="vt115.php" method="POST">
	  <input name="total" type="hidden" value="<?php echo $granTotal;?>">
          <tr><center>
	  <label>Cliente :</label>
	  <input autocomplete="off" class="redondo" name="codcli" id="codcli" size="11" style="text-align:center; font-weight:bold" type="text" placeholder="Código" onKeyUp="buscar()" onkeypress="return validanum(event)">
          &emsp;&emsp;&emsp;&nbsp;
	  <label>Nombre :</label>
	  <input autocomplete="off" class="redondo" name="nomcli" size="35" style="text-align:left; font-weight:bold" type="text" placeholder="Nombre" onkeypress="return validalet(event)">
          &emsp;&emsp;&emsp;&nbsp
	  <label>Descuento :</label>&ensp;
	  <input autocomplete="off" class="redondo" name="desto" style="text-align:center; font-weight:bold" size="11" type="number" step="0.01" onchange="restar()" onkeyup="restar()">
          </center></tr>
          <tr><center>
          </center></tr>
          <center><tr>
	  <label>Forma de Pago :</label>
          <select " name="fopag" style="width:90px; font-weight:bold" class="campos" onchange="if(this.value==='3')
          {document.getElementById('cheque').disabled = false;
          document.getElementById('banco').disabled = false;
          document.getElementById('efectivo').disabled = true}

          else if(this.value==='2')   
          {document.getElementById('cheque').disabled = true;
          document.getElementById('banco').disabled = true;
          document.getElementById('efectivo').disabled = true}

          else {document.getElementById('cheque').disabled = true;
          document.getElementById('banco').disabled = true;
          document.getElementById('efectivo').disabled = false} ">

          <OPTION selected>Efectivo</OPTION>
          <OPTION value="2">Credito</OPTION>
          <OPTION value="3">Cheque</OPTION>
          </select>
          &emsp;&nbsp;
	  <input autocomplete="off" class="redondo" disabled id="cheque" name="cheque" size="9" style="text-align:center; font-weight:bold" required type="text" placeholder="Cheque" onkeypress="return validanum(event)">
          &emsp;&nbsp;
	  <input autocomplete="off" class="redondo" disabled id="banco" name="banco" size="25" style="text-align:left; font-weight:bold" required type="text" placeholder="Banco" onkeypress="return validalet(event)">
          &emsp;&emsp;&emsp;
	  <label>Total a Pagar :</label>
	  <input class="redondo" name="tpago" size="11" style="text-align:center; font-weight:bold; font-weight:bold" type="number" step="0.01" value="<?php echo $granTotal;?>" readonly="readonly">
          </center></tr>
          <tr><center>
          &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
          &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
          &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
          &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
          &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
          &emsp;&emsp;&emsp;&emsp;&emsp;
	  <label>Pago Efectivo :</label>
	  <input autocomplete="off" class="redondo" name="efectivo" id="efectivo" size="11" style="text-align:center; font-weight:bold" type="number" step"0.01" onchange="cambio()" onkeyup="cambio()">
          </center></tr>
          <tr><center>
          &emsp;
 	  <button type="submit" class="btn btn-success">Grabar Venta</button>
          &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
          &emsp;&emsp;&emsp;&emsp;&emsp;&ensp;&ensp;
	  <a href="vt114.php" class="btn btn-danger">Cancelar Venta</a>
          &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
          &emsp;&emsp;&emsp;&emsp;&emsp;
	  <label>Cambio :</label>
	  <input class="redondo" name="difer" size="11" style="text-align:center; font-weight:bold" type="number" step="0.01" readonly="readonly">
          </center></tr>
     </form>
</div>
