checkout.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. require_once __DIR__ . '/config.php';
  3. require_once __DIR__ . '/includes/functions.php';
  4. $pageTitle = 'Reservierung';
  5. $cart = $_SESSION['cart'] ?? [];
  6. if (empty($cart)) {
  7. header('Location: cart.php');
  8. exit;
  9. }
  10. // Validate cart items and stock
  11. $cartItems = [];
  12. $regularItems = [];
  13. $backorderItems = [];
  14. $errors = [];
  15. $total = 0;
  16. $regularTotal = 0;
  17. $backorderTotal = 0;
  18. foreach ($cart as $item) {
  19. $product = getProductById($item['product_id']);
  20. if (!$product) {
  21. $errors[] = 'Ein Produkt wurde nicht gefunden.';
  22. continue;
  23. }
  24. $size = isset($item['size']) ? $item['size'] : null;
  25. $itemTotal = $product['price'] * $item['quantity'];
  26. $total += $itemTotal;
  27. $isInStock = checkStock($item['product_id'], $item['quantity'], $size);
  28. if ($isInStock) {
  29. $regularTotal += $itemTotal;
  30. } else {
  31. $backorderTotal += $itemTotal;
  32. }
  33. $cartItems[] = [
  34. 'product' => $product,
  35. 'quantity' => $item['quantity'],
  36. 'total' => $itemTotal,
  37. 'size' => isset($item['size']) ? $item['size'] : null,
  38. 'in_stock' => $isInStock
  39. ];
  40. }
  41. // Handle form submission
  42. if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['create_reservation'])) {
  43. $customerName = sanitize($_POST['customer_name'] ?? '');
  44. $customerEmail = sanitize($_POST['customer_email'] ?? '');
  45. if (empty($customerName)) {
  46. $errors[] = 'Bitte geben Sie Ihren Namen ein.';
  47. }
  48. if (empty($customerEmail) || !filter_var($customerEmail, FILTER_VALIDATE_EMAIL)) {
  49. $errors[] = 'Bitte geben Sie eine gültige E-Mail-Adresse ein.';
  50. }
  51. if (empty($errors)) {
  52. // Create reservation
  53. $regularItems = [];
  54. $backorderItems = [];
  55. foreach ($cart as $cartItem) {
  56. $item = [
  57. 'product_id' => $cartItem['product_id'],
  58. 'quantity' => $cartItem['quantity']
  59. ];
  60. if (isset($cartItem['size']) && !empty($cartItem['size'])) {
  61. $item['size'] = $cartItem['size'];
  62. }
  63. $size = isset($cartItem['size']) ? $cartItem['size'] : null;
  64. if (checkStock($cartItem['product_id'], $cartItem['quantity'], $size)) {
  65. $regularItems[] = $item;
  66. } else {
  67. $backorderItems[] = $item;
  68. }
  69. }
  70. $regularResult = null;
  71. $backorderResult = null;
  72. if (!empty($regularItems)) {
  73. $regularResult = createReservation($customerName, $customerEmail, $regularItems);
  74. if (!$regularResult['success']) {
  75. $errors[] = $regularResult['message'];
  76. }
  77. }
  78. if (empty($errors) && !empty($backorderItems)) {
  79. $backorderResult = createBackorderReservation($customerName, $customerEmail, $backorderItems);
  80. }
  81. if (empty($errors)) {
  82. $_SESSION['cart'] = [];
  83. $query = [];
  84. if ($regularResult && $regularResult['success']) {
  85. $query[] = 'order_number=' . urlencode($regularResult['reservation']['id']);
  86. }
  87. if ($backorderResult && $backorderResult['success']) {
  88. $query[] = 'backorder_number=' . urlencode($backorderResult['reservation']['id']);
  89. }
  90. header('Location: reservation.php?' . implode('&', $query));
  91. exit;
  92. }
  93. }
  94. }
  95. include __DIR__ . '/includes/header.php';
  96. ?>
  97. <h2>Reservierung abschließen</h2>
  98. <?php if (!empty($errors)): ?>
  99. <div class="alert alert-error">
  100. <ul style="margin-left: 1.5rem;">
  101. <?php foreach ($errors as $error): ?>
  102. <li><?php echo htmlspecialchars($error); ?></li>
  103. <?php endforeach; ?>
  104. </ul>
  105. </div>
  106. <?php endif; ?>
  107. <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 2rem; margin-top: 2rem;">
  108. <div>
  109. <h3>Ihre Bestellung</h3>
  110. <?php if ($regularTotal > 0): ?>
  111. <h4 style="margin-top: 1rem;">Sofort verfügbar</h4>
  112. <?php foreach ($cartItems as $cartItem): ?>
  113. <?php if ($cartItem['in_stock']): ?>
  114. <div class="panel" style="padding: 1rem; margin-bottom: 1rem;">
  115. <strong><?php echo htmlspecialchars($cartItem['product']['name']); ?></strong><br>
  116. <?php if (isset($cartItem['size']) && !empty($cartItem['size'])): ?>
  117. Größe: <?php echo htmlspecialchars($cartItem['size']); ?><br>
  118. <?php endif; ?>
  119. Menge: <?php echo $cartItem['quantity']; ?><br>
  120. Preis: <?php echo formatPrice($cartItem['total']); ?>
  121. </div>
  122. <?php endif; ?>
  123. <?php endforeach; ?>
  124. <?php endif; ?>
  125. <?php if ($backorderTotal > 0): ?>
  126. <h4 style="margin-top: 1.5rem;">Nachbestellung</h4>
  127. <div class="alert alert-warning" style="margin-bottom: 1rem;">
  128. <strong>Hinweis:</strong> Lieferzeiten sind nicht bekannt, da die Bestellung in Chargen erfolgt.
  129. </div>
  130. <?php foreach ($cartItems as $cartItem): ?>
  131. <?php if (!$cartItem['in_stock']): ?>
  132. <div class="panel" style="padding: 1rem; margin-bottom: 1rem;">
  133. <strong><?php echo htmlspecialchars($cartItem['product']['name']); ?></strong><br>
  134. <?php if (isset($cartItem['size']) && !empty($cartItem['size'])): ?>
  135. Größe: <?php echo htmlspecialchars($cartItem['size']); ?><br>
  136. <?php endif; ?>
  137. Menge: <?php echo $cartItem['quantity']; ?><br>
  138. Preis: <?php echo formatPrice($cartItem['total']); ?>
  139. </div>
  140. <?php endif; ?>
  141. <?php endforeach; ?>
  142. <?php endif; ?>
  143. <div class="panel" style="padding: 1rem; margin-top: 1rem;">
  144. <?php if ($regularTotal > 0): ?>
  145. <div>Summe sofort verfügbar: <strong><?php echo formatPrice($regularTotal); ?></strong></div>
  146. <?php endif; ?>
  147. <?php if ($backorderTotal > 0): ?>
  148. <div>Summe Nachbestellung: <strong><?php echo formatPrice($backorderTotal); ?></strong></div>
  149. <?php endif; ?>
  150. <strong style="font-size: 1.2rem;">Gesamtsumme: <?php echo formatPrice($total); ?></strong>
  151. </div>
  152. </div>
  153. <div>
  154. <h3>Ihre Daten</h3>
  155. <form method="POST">
  156. <div class="form-group">
  157. <label for="customer_name">Name *</label>
  158. <input type="text" id="customer_name" name="customer_name" required
  159. value="<?php echo isset($_POST['customer_name']) ? htmlspecialchars($_POST['customer_name']) : ''; ?>">
  160. </div>
  161. <div class="form-group">
  162. <label for="customer_email">E-Mail-Adresse *</label>
  163. <input type="email" id="customer_email" name="customer_email" required
  164. value="<?php echo isset($_POST['customer_email']) ? htmlspecialchars($_POST['customer_email']) : ''; ?>">
  165. </div>
  166. <div class="alert alert-info">
  167. <strong>Hinweis:</strong> Nach der Reservierung erhalten Sie eine Bestellnummer. Für Nachbestellungen informieren wir Sie, sobald die komplette Bestellung zur Abholung bereit ist.
  168. </div>
  169. <button type="submit" name="create_reservation" class="btn" style="width: 100%;">Reservierung abschließen</button>
  170. </form>
  171. <div style="margin-top: 1rem;">
  172. <a href="cart.php" class="btn btn-secondary">Zurück zum Warenkorb</a>
  173. </div>
  174. </div>
  175. </div>
  176. <?php include __DIR__ . '/includes/footer.php'; ?>