<?php
require_once ('../../../Clases/Producto.php');
require_once ('../../../Clases/Venta.php');

$p = new productos ();

$p->cantidad = $_POST["txtCantidad"];


    if ($p->cantidad > 0) {
        $p->id = $_POST["txtId"];
    $p->nombre = $_POST["txtNombre"];
    $p->precio = $_POST["txtPrecio"];
    $p->preciobs = $_POST["txtPrecioBs"];
    $p->stock = $_POST["txtStock"];
   $p->subTotal = $p->precio * $p->cantidad;
$p->subTotalBs = $p->preciobs * $p->cantidad;
  
$d= new ventas();



session_start();
    if(isset($_SESSION["carrito"])){
        $carrito = $_SESSION["carrito"];
    }else{
        $carrito = array();
    }

$sumaStock= 0;
foreach ($carrito as $pro) {
    if ($pro->id == $p->id){
       $sumaStock += $pro->cantidad;
    }
}

$sumaStock += $p->cantidad;

if ($p->stock >= $sumaStock){
    //tengo stock
array_push($carrito, $p);
$_SESSION["carrito"] = $carrito;
 header("location: ../../ventas.php");
}else{
    //No tengo stock
    header("location: ../../ventas.php?m=1");
}


    } else
 {
    header("location: ../../ventas.php?m=2");
 }



   

  
?>