<?php
date_default_timezone_set('America/La_Paz');
require __DIR__ . '/ticket/autoload.php';
use Mike42\Escpos\Printer;
use Mike42\Escpos\EscposImage;
use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
if (!isset($_POST["total"])):
   exit;
endif;
session_start();
include_once ("lib/fpdf/funci.php");
date_default_timezone_set('America/La_Paz');
$conex = conectar();
?>
<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 = $_SESSION["user_id"];
$coalm = $_SESSION["user_av"];
$toven = $_POST["total"];
$cocli = trim($_POST["codcli"]);
$nocli = trim($_POST["nomcli"]);
$fopag = $_POST["fopag"];
$topag = $_POST["tpago"];
$paefe = $_POST["efectivo"];
$decto = ($toven - $topag);
$ahora = date("Y-m-d");
$gesti = date("Y");
$xhora = date("H:i:s");
if ($fopag === "1"):
   if ($topag > $paefe):
      $graba = 2;
   endif;
endif;
if ($toven > 0 and $graba == 0):
   $mifac = mysql_query("select * from vedfa where vedfastat = 0 and vedfafech >= '$ahora'");
   $mirow = mysql_fetch_array($mifac);
   $nufac = $mirow[1] + 1;
   $milin = $mirow[0];
   $actua = "update vedfa set vedfanfac = '$nufac'
             where  vedfacorr = '$milin'";
   mysql_query("lock tables vedfa write");
   mysql_query($actua);
   mysql_query("unlock tables");
   $sqlcb = "insert into vecab values (0,'$coalm','$ahora','$nufac','$cocli','$nocli','$toven',
                                      '$decto','$topag','$gesti','ABCDEF','$xusua',0)";
   mysql_query("lock tables vecab write");
   mysql_query($sqlcb);
   $trsql = mysql_query("select max(vecabntra) as mitra from vecab");
   $sqtra = mysql_fetch_array($trsql);
   $trans = $sqtra["mitra"];
   mysql_query("unlock tables");
   $incab = "insert into incab values (0,'$trans','$coalm','$ahora',2,1,
                                      'SALIDA POR VENTA','$gesti','$xusua',0)";
   mysql_query("lock tables incab write");
   mysql_query($incab);
   mysql_query("unlock tables");
   for ($i=0; $i < count($_SESSION["carrito"]); $i++): 
      $xcart = $_SESSION["carrito"][$i][inalmcart];
      $xnart = $_SESSION["carrito"][$i][inartnart];
      $xcant = $_SESSION["carrito"][$i][mican];
      $xpven = $_SESSION["carrito"][$i][prven];
      $ximpt = $_SESSION["carrito"][$i][total];
      $sqldt = "insert into vedet values (0,'$trans','$xcart','$xcant','$xpven','$ximpt')";
      mysql_query("lock tables vedet write");
      mysql_query($sqldt);
      mysql_query("unlock tables");
      $indet = "insert into indet values (0,'$trans','$coalm','$xcart','$xcant')";
      mysql_query("lock tables indet write");
      mysql_query($indet);
      mysql_query("unlock tables");
      $inart = "update inart set inartcant = (inartcant - '$xcant')
                where  inartcart = '$xcart'
                and    inartstat = 0";
      mysql_query("lock tables inart write");
      mysql_query($inart);
      mysql_query("unlock tables");
      $miart = mysql_query("select * from inart
                            where  inartcart = '$xcart'
                            and    inartstat = 0");
      $linea = mysql_fetch_array($miart);
      $verif = $linea["inartlofe"];
      if ($verif == 1):
         $porfe = mysql_query("select * from inalm
                               where  inalmcalm = '$coalm'
                               and    inalmcart = '$xcart'
                               and    inalmcant > 0
                               and    inalmstat = 0
                               order  by inalmfven,inalmcorr");
         while ($rowid = mysql_fetch_array($porfe)):
            $mirow = $rowid[0];
            if ($rowid[3] > $xcant):
               $squpd = "update inalm set inalmcant = (inalmcant - '$xcant')
                         where  inalmcorr = '$mirow'";
               mysql_query("lock tables inalm write");
               mysql_query($squpd);
               mysql_query("unlock tables");
               break;
            else:
               $xcant = ($xcant - $rowid[inalmcant]);
               $miupd = "update inalm set inalmcant = 0, inalmstat = 9
                         where  inalmcorr = '$mirow'";
               mysql_query("lock tables inalm write");
               mysql_query($miupd);
               mysql_query("unlock tables");
               if ($xcant == 0):
                  break;
               endif;
            endif;
         endwhile;
      else:
         $upalm = "update inalm set inalmcant = (inalmcant - '$xcant')
                   where  inalmcalm = '$coalm'
                   and    inalmcart = '$xcart'
                   and    inalmstat = 0";
         mysql_query("lock tables inalm write");
         mysql_query($upalm);
         mysql_query("unlock tables");
      endif;
   endfor;
/*
   $nombre_impresora = "EPSON TM-U330"; 
   $connector = new WindowsPrintConnector($nombre_impresora);
   $printer = new Printer($connector);
   # Vamos a alinear al centro lo próximo que imprimamos
   $printer->setJustification(Printer::JUSTIFY_CENTER);
   $printer->text("FARMACIA PRUEBA" . "\n");
   $printer->text("CALLE ANTOFAGASTA DIAGONAL" . "\n");
   #La fecha también
   $printer->setJustification(Printer::JUSTIFY_LEFT);
   $printer->text(date("d-m-Y H:i:s") . "\n");

   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];
   endfor;
*/
   unset($_SESSION["carrito"]);
   $_SESSION["carrito"] = [];
   print "<script>alert(\"Venta Correcta\");
   window.location='vt111.php?status=1';</script>";
else:
   if ($graba == 1):
      header("Location: vt111.php?status=0");
   elseif ($graba == 2):
      header("Location: vt111.php?status=2");
   else:
      header("Location: vt111.php?status=6");
   endif;
endif;
?>