From b252e86ceda009a2ae9c2a8445bb9e9d20fbea99 Mon Sep 17 00:00:00 2001 From: Andrea Fiorencis Date: Wed, 15 Apr 2026 10:29:31 +0200 Subject: [PATCH] Aggiunto controllo stock in checkout per prevenire stock negativi --- src/checkout.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/checkout.php b/src/checkout.php index 6794db7..c49cc38 100644 --- a/src/checkout.php +++ b/src/checkout.php @@ -52,6 +52,22 @@ try { $quantity = $item['quantity']; $price = $item['price']; + // Controlla lo stock disponibile + $sql_check_stock = "SELECT StockQuantity, ProductName FROM Products WHERE ProductID = ?"; + $stmt_check = $conn->prepare($sql_check_stock); + if (!$stmt_check) { + throw new Exception("Errore nella preparazione della query di controllo stock: " . $conn->error); + } + $stmt_check->bind_param("i", $product_id); + $stmt_check->execute(); + $result_check = $stmt_check->get_result(); + $product = $result_check->fetch_assoc(); + $stmt_check->close(); + + if (!$product || $product['StockQuantity'] < $quantity) { + throw new Exception("Quantità insufficiente per il prodotto '" . $product['ProductName'] . "'. Disponibile: " . ($product ? $product['StockQuantity'] : 0)); + } + // Aggiungi item all'ordine $sql_item = "INSERT INTO OrderItems (OrderID, ProductID, Quantity, Price) VALUES (?, ?, ?, ?)"; $stmt_item = $conn->prepare($sql_item);