36782-vm/cart.php
2025-12-12 18:01:13 +00:00

180 lines
8.2 KiB
PHP

<?php
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
require_once 'includes/auth.php';
require_login();
require_once 'includes/helpers.php';
$cart = $_SESSION['cart'] ?? [];
$cart_products = [];
$total_price = 0;
if (!empty($cart)) {
$product_ids = array_keys($cart);
$placeholders = implode(',', array_fill(0, count($product_ids), '?'));
try {
$pdo = db();
$client_id = $_SESSION['client_id'] ?? null;
$stmt = $pdo->prepare("SELECT * FROM products WHERE id IN ($placeholders)");
$stmt->execute($product_ids);
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($products as $product) {
$quantity = $cart[$product['id']];
// Use the new centralized price function
$price_info = getEffectivePrice($pdo, $product['id'], $client_id);
$price_net = $price_info['net'];
$price_gross = $price_info['gross'];
$line_total_gross = $price_gross * $quantity;
$cart_products[] = [
'id' => $product['id'],
'name' => $product['name'],
'price_net' => $price_net,
'price_gross' => $price_gross,
'quantity' => $quantity,
'line_total' => $line_total_gross, // Use gross for calculations
];
$total_price += $line_total_gross; // Sum up the gross total
}
} catch (PDOException $e) {
die("Błąd połączenia z bazą danych: " . $e->getMessage());
}
}
$page_title = 'Koszyk';
$user_role = get_user_role();
?>
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo htmlspecialchars($page_title); ?> - B2B Commerce</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm">
<div class="container-fluid">
<a class="navbar-brand" href="index.php">
<img src="assets/pasted-20251209-065617-6bf1b4e6.png" alt="Logo" style="height: 40px;">
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ms-auto mb-2 mb-lg-0 align-items-center">
<li class="nav-item">
<a class="nav-link" href="index.php">Katalog</a>
</li>
<li class="nav-item">
<a class="nav-link" href="cart.php">
<i class="bi bi-cart"></i> Koszyk
<span class="badge bg-primary rounded-pill"><?= count($_SESSION['cart'] ?? []) ?></span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="orders.php">Zamówienia</a>
</li>
<?php if ($user_role === 'admin'): ?>
<li class="nav-item">
<a class="nav-link" href="/admin/products.php">Admin</a>
</li>
<?php endif; ?>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
<i class="bi bi-person-circle"></i> Witaj, <?= isset($_SESSION['username']) ? htmlspecialchars($_SESSION['username']) : '' ?>
</a>
<ul class="dropdown-menu dropdown-menu-end">
<li><a class="dropdown-item p-2" href="profile.php">Profil</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item p-2" href="logout.php">Wyloguj</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<main class="container my-5">
<h1 class="mb-4">Koszyk</h1>
<?php if (empty($cart_products)): ?>
<div class="alert alert-info" role="alert">
Twój koszyk jest pusty.
</div>
<a href="index.php" class="btn btn-primary">Wróć do sklepu</a>
<?php else: ?>
<table class="table">
<thead>
<tr>
<th>Produkt</th>
<th>Cena netto</th>
<th>Cena brutto</th>
<th>Ilość</th>
<th>Razem</th>
<th></th>
</tr>
</thead>
<tbody>
<?php foreach ($cart_products as $item): ?>
<tr>
<td><?php echo htmlspecialchars($item['name']); ?></td>
<td><?php echo number_format($item['price_net'], 2, ',', ' '); ?> zł</td>
<td><?php echo number_format($item['price_gross'], 2, ',', ' '); ?> zł</td>
<td>
<form action="cart_actions.php" method="POST" class="d-inline-flex align-items-center">
<input type="hidden" name="action" value="update">
<input type="hidden" name="product_id" value="<?php echo $item['id']; ?>">
<input type="hidden" name="redirect_to" value="cart.php">
<label for="quantity-<?php echo $item['id']; ?>" class="visually-hidden">Ilość</label>
<input type="number" id="quantity-<?php echo $item['id']; ?>" name="quantity" value="<?php echo $item['quantity']; ?>" min="1" class="form-control form-control-sm" style="width: 70px;" aria-label="Ilość">
<button type="submit" class="btn btn-secondary ms-2">Zaktualizuj</button>
</form>
</td>
<td><?php echo number_format($item['line_total'], 2, ',', ' '); ?> zł</td>
<td>
<form action="cart_actions.php" method="POST" class="d-inline">
<input type="hidden" name="action" value="remove">
<input type="hidden" name="product_id" value="<?php echo $item['id']; ?>">
<input type="hidden" name="redirect_to" value="cart.php">
<button type="submit" class="btn btn-sm btn-danger" aria-label="Usuń"><i class="bi bi-trash"></i></button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
<tfoot>
<tr>
<td colspan="4" class="text-end"><strong>Razem (brutto):</strong></td>
<td colspan="2"><strong><?php echo number_format($total_price, 2, ',', ' '); ?> zł</strong></td>
</tr>
</tfoot>
</table>
<div class="d-flex justify-content-between mt-4">
<a href="index.php" class="btn btn-outline-secondary">Wróć do sklepu</a>
<a href="checkout.php" class="btn btn-primary">Przejdź do zamówienia</a>
</div>
<?php endif; ?>
</main>
<footer class="text-center py-4 mt-auto text-muted bg-light">
<div class="container">
<p class="mb-0">&copy; <?php echo date("Y"); ?> powered by LEA24. All Rights Reserved.</p>
</div>
</footer>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>