<?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;
date_default_timezone_set('America/La_Paz');
include_once ("lib/fpdf/funci.php");
$conex = conectar();
$coalm = $_SESSION["user_av"];
if(!isset($_POST["codigo"])) return;
$codigo = $_POST["codigo"];
$canti  = $_POST["canti"];
//$precio = $_POST["precio"];
$rowid = mysql_query("select inalmcart,inartnart,inartpven,sum(inalmcant) cantidad from inalm
                      inner  join inart on inalmcart = inartcart
                      where  inalmcart = '$codigo'
                      and    inalmcant > 0
                      and    inalmstat = 0
                      and    inalmcalm = '$coalm'
                      group  by 1,2,3");
if (mysql_num_rows($rowid) > 0):
   $producto = mysql_fetch_array($rowid);
   if ($producto[cantidad] < $canti or $canti <= 0):
      header("Location: vt111.php?status=5");
      exit;
   endif;
   $indice = false;
   for ($i=0; $i < count($_SESSION["carrito"]); $i++): 
       if ($_SESSION["carrito"][$i][inalmcart] === $codigo):
          $indice = $i;
	  break;
       endif;
   endfor;
   $preven = $producto[inartpven];
   if ($indice === FALSE):
      $producto[mican] = $canti;
      $producto[prven] = $preven;
      $producto[total] = $preven * $producto[mican];
      array_push($_SESSION["carrito"], $producto);
   else:
      $_SESSION["carrito"][$indice][prven] = $preven;
      $_SESSION["carrito"][$indice][mican] = $_SESSION["carrito"][$indice][mican] + $canti;
      $_SESSION["carrito"][$indice][total] = $_SESSION["carrito"][$indice][mican] * $preven;
      //$_SESSION["carrito"][$indice][total] = $_SESSION["carrito"][$indice][mican] * $_SESSION["carrito"][$indice][vtartpven];
   endif;
   header("Location: vt111.php");
else:
   header("Location: vt111.php?status=4");
endif;
?>
