36782-vm/checkout.php
Flatlogic Bot 11f95cfd4b ENG v.1
2025-12-12 19:32:49 +00:00

171 lines
7.5 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 FROM products p WHERE p.id IN ($placeholders)";
$stmt = $pdo->prepare($sql);
$stmt->execute($product_ids);
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);
$is_supplier_delivery = false;
foreach ($products as $product) {
$quantity = $cart[$product['id']];
$price_info = getEffectivePrice($pdo, $product['id'], $client_id);
$price_net = $price_info['net'];
$price_gross = $price_info['gross'];
$line_total = $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,
];
$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>
<?php require_once 'includes/header.php'; ?>
<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 netto</th>
<th>Cena brutto</th>
<th>Suma (brutto)</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_net'], 2, ',', ' '); ?> zł</td>
<td><?php echo number_format($item['price_gross'], 2, ',', ' '); ?> zł</td>
<td><?php echo number_format($item['line_total'], 2, ',', ' '); ?> zł</td>
</tr>
<?php endforeach; ?>
</tbody>
<tfoot>
<tr>
<td colspan="4" class="text-end"><strong>Suma (brutto):</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>