<?php
if (!isset($_POST["total"])):
   exit;
endif;
session_start();
include_once ("lib/fpdf/funci.php");
$coalm = $_SESSION["user_al"];
$conex = conectar();
date_default_timezone_set('America/La_Paz');
?>
<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">
<?php
$graba = 0;
$xusua = strtoupper($_SESSION["user_id"]);
$xtota = $_POST["total"];
$cocli = trim($_POST["codcli"]);
$nocli = trim($_POST["nomcli"]);
$paefe = $_POST["pagoef"];
$ahora = date("Y-m-d");
$xhora = date("H:i:s");
if ($cocli == ""):
   $cocli = 1;
   $bucli = mysql_query("select * from vtcli where vtcliccli = '$cocli'");
   $micli = mysql_fetch_array($bucli);
   $nocli = $micli["vtclincli"];
else:
   $bucli = mysql_query("select * from vtcli where vtcliccli = '$cocli'");
   if (mysql_num_rows($bucli) > 0):
      $micli = mysql_fetch_array($bucli);
      $nocli = $micli["vtclincli"];
   else:
      if ($nocli == ""):
         $graba = 1;
      else:
         $upnom = "insert into vtcli (vtcliccli,vtclincli,vtclidire,vtclitelf,
                                      vtclimail,vtclifpro,vtcliusua,vtclistat)
                               values('$cocli','$nocli','','','','$ahora','$xusua',0)";
         mysql_query("lock tables vtcli write");
         mysql_query($upnom);
         mysql_query("unlock tables");
      endif;
   endif;
endif;
if ($xtota > 0 and $paefe >= $xtota and $graba == 0):
   $nufac = 0;
   $sqlcb = "insert into vtcab (vtcabntra,vtcabnfac,vtcabccli,vtcabncli,vtcabimpt,
                                vtcabfech,vtcabhora,vtcabusua,vtcabstat)
                        values (0,'$nufac','$cocli','$nocli','$xtota',
                                '$ahora','$xhora','$xusua',0)";
   mysql_query("lock tables vtcab write");
   mysql_query($sqlcb);
   mysql_query("unlock tables");
   $trsql = mysql_query("select max(vtcabntra) as mitra from vtcab");
   $sqtra = mysql_fetch_array($trsql);
   $trans = $sqtra["mitra"];
   $cambi = ($paefe - $xtota);
   $cambi = number_format($cambi,2);
   for ($i=0; $i < count($_SESSION["carrito"]); $i++): 
      $xcart = $_SESSION["carrito"][$i][vtartcart];
      $xnart = $_SESSION["carrito"][$i][vtartnart];
      $xcant = $_SESSION["carrito"][$i][mican];
      $xpven = $_SESSION["carrito"][$i][vtartpven];
      $ximpt = $_SESSION["carrito"][$i][total];
      $sqldt = "insert into vtdet (vtdetiden,vtdetntra,vtdetcart,vtdetnart,vtdetcant,
                                   vtdetpven,vtdetimpt,vtdetfech,vtdetstat)
                           values (0,'$trans','$xcart','$xnart','$xcant',
                                   '$xpven','$ximpt','$ahora',0)";
      mysql_query("lock tables vtdet write");
      mysql_query($sqldt);
      mysql_query("unlock tables");
      $upart = "update vtart set vtartcant = (vtartcant - '$xcant')
                where  vtartcart = '$xcart'
                and    vtartcalm = '$coalm'
                and    vtartstat = 0";
      mysql_query("lock tables vtart write");
      mysql_query($upart);
      mysql_query("unlock tables");
      $miart = mysql_query("select * from vtart
                            where  vtartcalm = '$coalm'
                            and    vtartcart = '$xcart'
                            and    vtartstat = 0");
      $linea = mysql_fetch_array($miart);
      $verif = $linea["vtartestf"];
      if ($verif == 0):
         $porfe = mysql_query("select * from vtalm
                               where  vtalmcalm = '$coalm'
                               and    vtalmcart = '$xcart'
                               and    vtalmcant > 0
                               and    vtalmstat = 0
                               order  by vtalmfven,vtalmiden");
         while ($rowid = mysql_fetch_array($porfe)):
            $miide = $rowid["vtalmiden"];
            if ($rowid[vtalmcant] > $xcant):
               $squpd = "update vtalm set vtalmcant = (vtalmcant - '$xcant')
                         where  vtalmiden = '$miide'";
               mysql_query("lock tables vtalm write");
               mysql_query($squpd);
               mysql_query("unlock tables");
               break;
            else:
               $xcant = ($xcant - $rowid[vtalmcant]);
               $miupd = "update vtalm set vtalmcant = 0, vtalmstat = 9
                         where  vtalmiden = '$miide'";
               mysql_query("lock tables vtalm write");
               mysql_query($miupd);
               mysql_query("unlock tables");
               if ($xcant == 0):
                  break;
               endif;
            endif;
         endwhile;
      else:
         $upalm = "update vtalm set vtalmcant = (vtalmcant - '$xcant')
                   where  vtalmcalm = '$coalm'
                   and    vtalmcart = '$xcart'
                   and    vtalmstat = 0";
         mysql_query("lock tables vtalm write");
         mysql_query($upalm);
         mysql_query("unlock tables");
      endif;
   endfor;
   unset($_SESSION["carrito"]);
   $_SESSION["carrito"] = [];
   print "<script>alert(\"Su Cambio es Bs.- $cambi\");
   window.location='vt111.php?status=1';</script>";
else:
   if ($graba == 1):
      header("Location: vt111.php?status=0");
   else:
      header("Location: vt111.php?status=2");
   endif;
endif;
?>