| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- require_once __DIR__ . '/config.php';
- require_once __DIR__ . '/includes/functions.php';
- $pageTitle = 'Reservierung';
- $cart = $_SESSION['cart'] ?? [];
- if (empty($cart)) {
- header('Location: cart.php');
- exit;
- }
- // Validate cart items and stock
- $cartItems = [];
- $regularItems = [];
- $backorderItems = [];
- $errors = [];
- $total = 0;
- $regularTotal = 0;
- $backorderTotal = 0;
- foreach ($cart as $item) {
- $product = getProductById($item['product_id']);
- if (!$product) {
- $errors[] = 'Ein Produkt wurde nicht gefunden.';
- continue;
- }
-
- $size = isset($item['size']) ? $item['size'] : null;
- $itemTotal = $product['price'] * $item['quantity'];
- $total += $itemTotal;
- $isInStock = checkStock($item['product_id'], $item['quantity'], $size);
- if ($isInStock) {
- $regularTotal += $itemTotal;
- } else {
- $backorderTotal += $itemTotal;
- }
- $cartItems[] = [
- 'product' => $product,
- 'quantity' => $item['quantity'],
- 'total' => $itemTotal,
- 'size' => isset($item['size']) ? $item['size'] : null,
- 'in_stock' => $isInStock
- ];
- }
- // Handle form submission
- if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['create_reservation'])) {
- $customerName = sanitize($_POST['customer_name'] ?? '');
- $customerEmail = sanitize($_POST['customer_email'] ?? '');
-
- if (empty($customerName)) {
- $errors[] = 'Bitte geben Sie Ihren Namen ein.';
- }
-
- if (empty($customerEmail) || !filter_var($customerEmail, FILTER_VALIDATE_EMAIL)) {
- $errors[] = 'Bitte geben Sie eine gültige E-Mail-Adresse ein.';
- }
-
- if (empty($errors)) {
- // Create reservation
- $regularItems = [];
- $backorderItems = [];
- foreach ($cart as $cartItem) {
- $item = [
- 'product_id' => $cartItem['product_id'],
- 'quantity' => $cartItem['quantity']
- ];
- if (isset($cartItem['size']) && !empty($cartItem['size'])) {
- $item['size'] = $cartItem['size'];
- }
-
- $size = isset($cartItem['size']) ? $cartItem['size'] : null;
- if (checkStock($cartItem['product_id'], $cartItem['quantity'], $size)) {
- $regularItems[] = $item;
- } else {
- $backorderItems[] = $item;
- }
- }
-
- $regularResult = null;
- $backorderResult = null;
-
- if (!empty($regularItems)) {
- $regularResult = createReservation($customerName, $customerEmail, $regularItems);
- if (!$regularResult['success']) {
- $errors[] = $regularResult['message'];
- }
- }
-
- if (empty($errors) && !empty($backorderItems)) {
- $backorderResult = createBackorderReservation($customerName, $customerEmail, $backorderItems);
- }
-
- if (empty($errors)) {
- $_SESSION['cart'] = [];
- $query = [];
- if ($regularResult && $regularResult['success']) {
- $query[] = 'order_number=' . urlencode($regularResult['reservation']['id']);
- }
- if ($backorderResult && $backorderResult['success']) {
- $query[] = 'backorder_number=' . urlencode($backorderResult['reservation']['id']);
- }
- header('Location: reservation.php?' . implode('&', $query));
- exit;
- }
- }
- }
- include __DIR__ . '/includes/header.php';
- ?>
- <h2>Reservierung abschließen</h2>
- <?php if (!empty($errors)): ?>
- <div class="alert alert-error">
- <ul style="margin-left: 1.5rem;">
- <?php foreach ($errors as $error): ?>
- <li><?php echo htmlspecialchars($error); ?></li>
- <?php endforeach; ?>
- </ul>
- </div>
- <?php endif; ?>
- <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 2rem; margin-top: 2rem;">
- <div>
- <h3>Ihre Bestellung</h3>
-
- <?php if ($regularTotal > 0): ?>
- <h4 style="margin-top: 1rem;">Sofort verfügbar</h4>
- <?php foreach ($cartItems as $cartItem): ?>
- <?php if ($cartItem['in_stock']): ?>
- <div class="panel" style="padding: 1rem; margin-bottom: 1rem;">
- <strong><?php echo htmlspecialchars($cartItem['product']['name']); ?></strong><br>
- <?php if (isset($cartItem['size']) && !empty($cartItem['size'])): ?>
- Größe: <?php echo htmlspecialchars($cartItem['size']); ?><br>
- <?php endif; ?>
- Menge: <?php echo $cartItem['quantity']; ?><br>
- Preis: <?php echo formatPrice($cartItem['total']); ?>
- </div>
- <?php endif; ?>
- <?php endforeach; ?>
- <?php endif; ?>
-
- <?php if ($backorderTotal > 0): ?>
- <h4 style="margin-top: 1.5rem;">Nachbestellung</h4>
- <div class="alert alert-warning" style="margin-bottom: 1rem;">
- <strong>Hinweis:</strong> Lieferzeiten sind nicht bekannt, da die Bestellung in Chargen erfolgt.
- </div>
- <?php foreach ($cartItems as $cartItem): ?>
- <?php if (!$cartItem['in_stock']): ?>
- <div class="panel" style="padding: 1rem; margin-bottom: 1rem;">
- <strong><?php echo htmlspecialchars($cartItem['product']['name']); ?></strong><br>
- <?php if (isset($cartItem['size']) && !empty($cartItem['size'])): ?>
- Größe: <?php echo htmlspecialchars($cartItem['size']); ?><br>
- <?php endif; ?>
- Menge: <?php echo $cartItem['quantity']; ?><br>
- Preis: <?php echo formatPrice($cartItem['total']); ?>
- </div>
- <?php endif; ?>
- <?php endforeach; ?>
- <?php endif; ?>
-
- <div class="panel" style="padding: 1rem; margin-top: 1rem;">
- <?php if ($regularTotal > 0): ?>
- <div>Summe sofort verfügbar: <strong><?php echo formatPrice($regularTotal); ?></strong></div>
- <?php endif; ?>
- <?php if ($backorderTotal > 0): ?>
- <div>Summe Nachbestellung: <strong><?php echo formatPrice($backorderTotal); ?></strong></div>
- <?php endif; ?>
- <strong style="font-size: 1.2rem;">Gesamtsumme: <?php echo formatPrice($total); ?></strong>
- </div>
- </div>
-
- <div>
- <h3>Ihre Daten</h3>
- <form method="POST">
- <div class="form-group">
- <label for="customer_name">Name *</label>
- <input type="text" id="customer_name" name="customer_name" required
- value="<?php echo isset($_POST['customer_name']) ? htmlspecialchars($_POST['customer_name']) : ''; ?>">
- </div>
-
- <div class="form-group">
- <label for="customer_email">E-Mail-Adresse *</label>
- <input type="email" id="customer_email" name="customer_email" required
- value="<?php echo isset($_POST['customer_email']) ? htmlspecialchars($_POST['customer_email']) : ''; ?>">
- </div>
-
- <div class="alert alert-info">
- <strong>Hinweis:</strong> Nach der Reservierung erhalten Sie eine Bestellnummer. Für Nachbestellungen informieren wir Sie, sobald die komplette Bestellung zur Abholung bereit ist.
- </div>
-
- <button type="submit" name="create_reservation" class="btn" style="width: 100%;">Reservierung abschließen</button>
- </form>
-
- <div style="margin-top: 1rem;">
- <a href="cart.php" class="btn btn-secondary">Zurück zum Warenkorb</a>
- </div>
- </div>
- </div>
- <?php include __DIR__ . '/includes/footer.php'; ?>
|