mirror of
https://codeberg.org/ThisIsMiseryy/techstore
synced 2026-05-14 12:42:04 +00:00
156 lines
6.1 KiB
PHP
156 lines
6.1 KiB
PHP
<?php
|
|
session_start();
|
|
include 'db_conf.php';
|
|
|
|
// Controlla se l'utente è loggato
|
|
if (!isset($_SESSION['id'])) {
|
|
header('Location: login.php');
|
|
exit();
|
|
}
|
|
|
|
// Leggi il carrello dal cookie
|
|
$cart = [];
|
|
$cartCookie = isset($_COOKIE['cart']) ? $_COOKIE['cart'] : '';
|
|
|
|
if ($cartCookie) {
|
|
$cart = json_decode(urldecode($cartCookie), true);
|
|
}
|
|
|
|
// Se il carrello è vuoto, reindirizza al carrello
|
|
if (empty($cart)) {
|
|
header('Location: cart.php');
|
|
exit();
|
|
}
|
|
|
|
$user_id = $_SESSION['id'];
|
|
$order_total = 0;
|
|
$error = '';
|
|
|
|
// Inzia la transazione
|
|
$conn->begin_transaction();
|
|
|
|
try {
|
|
// Calcola il totale dell'ordine
|
|
foreach ($cart as $item) {
|
|
$order_total += $item['price'] * $item['quantity'];
|
|
}
|
|
|
|
// Crea un nuovo ordine
|
|
$sql_order = "INSERT INTO Orders (UserID, Total, Status) VALUES (?, ?, 'pending')";
|
|
$stmt_order = $conn->prepare($sql_order);
|
|
if (!$stmt_order) {
|
|
throw new Exception("Errore nella preparazione della query: " . $conn->error);
|
|
}
|
|
|
|
$stmt_order->bind_param("id", $user_id, $order_total);
|
|
$stmt_order->execute();
|
|
$order_id = $conn->insert_id;
|
|
|
|
// Per ogni prodotto nel carrello
|
|
foreach ($cart as $item) {
|
|
$product_id = $item['id'];
|
|
$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);
|
|
if (!$stmt_item) {
|
|
throw new Exception("Errore nella preparazione della query: " . $conn->error);
|
|
}
|
|
|
|
$stmt_item->bind_param("iiii", $order_id, $product_id, $quantity, $price);
|
|
$stmt_item->execute();
|
|
|
|
// Riduci lo stock del prodotto
|
|
$sql_stock = "UPDATE Products SET StockQuantity = StockQuantity - ? WHERE ProductID = ?";
|
|
$stmt_stock = $conn->prepare($sql_stock);
|
|
if (!$stmt_stock) {
|
|
throw new Exception("Errore nella preparazione della query: " . $conn->error);
|
|
}
|
|
|
|
$stmt_stock->bind_param("ii", $quantity, $product_id);
|
|
$stmt_stock->execute();
|
|
|
|
$stmt_item->close();
|
|
$stmt_stock->close();
|
|
}
|
|
|
|
// Conferma la transazione
|
|
$conn->commit();
|
|
|
|
// Svuota il carrello
|
|
setcookie('cart', '', time() - 3600, '/');
|
|
|
|
// Mostra pagina di successo
|
|
$success = true;
|
|
|
|
} catch (Exception $e) {
|
|
// Annulla la transazione in caso di errore
|
|
$conn->rollback();
|
|
$error = "Errore nell'elaborazione dell'ordine: " . $e->getMessage();
|
|
$success = false;
|
|
}
|
|
|
|
$stmt_order->close();
|
|
$conn->close();
|
|
?>
|
|
|
|
<!DOCTYPE html>
|
|
<html lang="it">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title><?php echo $success ? 'Ordine confermato' : 'Errore ordine'; ?> - TechStore</title>
|
|
<link rel="stylesheet" href="assets/style.css">
|
|
<?php
|
|
if(getenv('ITSTRACKER_KEY')){
|
|
echo "<script src=\"https://projectits.altervista.org/tracker.js?key=" . getenv('ITSTRACKER_KEY') . "\"></script>";
|
|
}
|
|
?>
|
|
</head>
|
|
<body>
|
|
<header style="display: flex; justify-content: space-between; align-items: center; padding: 10px; background-color: #f2f2f2; border-bottom: 1px solid #ddd;">
|
|
<div id="logo" style="font-size: 24px; font-weight: bold;"><a href="index.php"><img src="img/logo.png" alt="Logo TechStore" height="32px"></a></div>
|
|
<div>
|
|
<a href="cart.php"><button style="margin-left: 10px; padding: 8px 16px; background-color: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer;">Carrello</button></a>
|
|
<?php if (isset($_SESSION['name'])): ?>
|
|
<a href="logout.php"><button style="margin-left: 10px; padding: 8px 16px; background-color: #dc3545; color: white; border: none; border-radius: 4px; cursor: pointer;">Logout</button></a>
|
|
<span style="margin-left: 10px;">Benvenuto, <a href="account.php"><?php echo htmlspecialchars($_SESSION['name']); ?></a></span>
|
|
<?php else: ?>
|
|
<a href="login.php"><button style="margin-left: 10px; padding: 8px 16px; background-color: #1d8033; color: white; border: none; border-radius: 4px; cursor: pointer;">Accesso</button></a>
|
|
<?php endif; ?>
|
|
</div>
|
|
</header>
|
|
|
|
<div style="max-width: 600px; margin: 50px auto; padding: 20px; text-align: center; border: 1px solid #ddd; border-radius: 8px; background-color: #fff;">
|
|
<?php if ($success): ?>
|
|
<h1 style="color: #1d8033;">Ordine confermato!</h1>
|
|
<p>Il tuo ordine è stato elaborato con successo.</p>
|
|
<p><strong>Numero ordine:</strong> #<?php echo $order_id; ?></p>
|
|
<p><strong>Totale:</strong> €<?php echo number_format($order_total, 2, ',', '.'); ?></p>
|
|
<a href="index.php"><button style="padding: 10px 20px; background-color: #1c71d8; color: white; border: none; border-radius: 4px; cursor: pointer; margin-top: 20px;">Torna alla home</button></a>
|
|
<?php else: ?>
|
|
<h1 style="color: #dc3545;">Errore nell'ordine</h1>
|
|
<p><?php echo htmlspecialchars($error); ?></p>
|
|
<a href="cart.php"><button style="padding: 10px 20px; background-color: #1c71d8; color: white; border: none; border-radius: 4px; cursor: pointer; margin-top: 20px;">Torna al carrello</button></a>
|
|
<?php endif; ?>
|
|
</div>
|
|
</body>
|
|
</html>
|