36782-vm/checkout.php
2025-12-12 14:13:03 +00:00

204 lines
9.4 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)) {
header('Location: index.php');
exit;
}
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;
$credit_info = null;
if ($client_id) {
$stmt = $pdo->prepare('SELECT credit_limit, credit_balance, credit_enabled FROM clients WHERE id = ?');
$stmt->execute([$client_id]);
$credit_info = $stmt->fetch(PDO::FETCH_ASSOC);
}
$sql = "SELECT p.id, p.name, p.units_per_pallet, COALESCE(cp.price, p.price_gross) as price FROM products p LEFT JOIN client_prices cp ON p.id = cp.product_id AND cp.client_id = ? WHERE p.id IN ($placeholders)";
$stmt = $pdo->prepare($sql);
$params = array_merge([$client_id], $product_ids);
$stmt->execute($params);
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);
$is_supplier_delivery = false;
foreach ($products as $product) {
$quantity = $cart[$product['id']];
$line_total = $product['price'] * $quantity;
$cart_products[] = [
'id' => $product['id'],
'name' => $product['name'],
'price' => $product['price'],
'quantity' => $quantity,
'line_total' => $line_total,
];
$total_price += $line_total;
// Check for full pallets only if units_per_pallet is set and positive
if (isset($product['units_per_pallet']) && $product['units_per_pallet'] > 0) {
if ($quantity >= $product['units_per_pallet']) {
$is_supplier_delivery = true;
}
}
}
$delivery_source = $is_supplier_delivery ? 'supplier' : 'cs';
} catch (PDOException $e) {
die('Błąd połączenia z bazą danych: ' . $e->getMessage());
}
}
$page_title = 'Podsumowanie zamówienia';
$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); ?> - ExtraB2B</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">Podsumowanie zamówienia</h1>
<div class="row">
<div class="col-md-8">
<div class="card">
<div class="card-header">Twoje zamówienie</div>
<div class="card-body">
<table class="table">
<thead>
<tr>
<th>Produkt</th>
<th>Ilość</th>
<th>Cena jedn.</th>
<th>Suma</th>
</tr>
</thead>
<tbody>
<?php foreach ($cart_products as $item): ?>
<tr>
<td><?php echo htmlspecialchars($item['name']); ?></td>
<td><?php echo $item['quantity']; ?></td>
<td><?php echo number_format($item['price'], 2, ',', ' '); ?> zł</td>
<td><?php echo number_format($item['line_total'], 2, ',', ' '); ?> zł</td>
</tr>
<?php endforeach; ?>
</tbody>
<tfoot>
<tr>
<td colspan="3" class="text-end"><strong>Suma:</strong></td>
<td><strong><?php echo number_format($total_price, 2, ',', ' '); ?> zł</strong></td>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<div class="card-header">Opcje dostawy i płatności</div>
<div class="card-body">
<div class="alert alert-secondary">
Źródło dostawy: <strong><?php echo ($delivery_source === 'cs') ? 'Magazyn Centralny' : 'Dostawca zewnętrzny'; ?></strong>
</div>
<?php if ($credit_info && $credit_info['credit_enabled']): ?>
<div class="alert alert-info">
Dostępny kredyt kupiecki: <strong><?php echo number_format($credit_info['credit_balance'], 2, ',', ' '); ?> zł</strong>
</div>
<?php endif; ?>
<form action="order_process.php" method="POST">
<div class="mb-3">
<label for="payment_method" class="form-label">Metoda płatności</label>
<select class="form-select" id="payment_method" name="payment_method" required>
<option value="bank_transfer">Przelew bankowy</option>
<option value="online">Płatność online</option>
<?php if ($credit_info && $credit_info['credit_enabled'] && $credit_info['credit_balance'] >= $total_price): ?>
<option value="credit">Kredyt kupiecki</option>
<?php endif; ?>
</select>
</div>
<div class="mb-3">
<label for="notes" class="form-label">Uwagi do zamówienia</label>
<textarea class="form-control" id="notes" name="notes" rows="3"></textarea>
</div>
<input type="hidden" name="total_amount" value="<?php echo $total_price; ?>">
<button type="submit" class="btn btn-primary w-100">Potwierdź zamówienie</button>
</form>
</div>
</div>
</div>
</div>
</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"); ?> ExtraB2B. 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>