backorders.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. require_once __DIR__ . '/../config.php';
  3. require_once __DIR__ . '/../includes/functions.php';
  4. // Check admin login
  5. if (!isset($_SESSION['admin_logged_in']) || !$_SESSION['admin_logged_in']) {
  6. header('Location: login.php');
  7. exit;
  8. }
  9. $pageTitle = 'Vorbestellungen verwalten';
  10. // Handle availability notification
  11. if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['notify_available'])) {
  12. $reservationId = sanitize($_POST['reservation_id']);
  13. $result = markBackorderAvailable($reservationId);
  14. $message = $result['success'] ? 'Kunde wurde informiert, dass die Vorbestellung zur Abholung bereit ist.' : $result['message'];
  15. $messageType = $result['success'] ? 'success' : 'error';
  16. }
  17. $reservations = getReservations();
  18. $filter = isset($_GET['filter']) ? sanitize($_GET['filter']) : 'pending';
  19. $searchOrderNumber = isset($_GET['order_number']) ? sanitize($_GET['order_number']) : '';
  20. // Filter backorders
  21. $reservations = array_filter($reservations, function($r) {
  22. return isset($r['type']) && $r['type'] === 'backorder';
  23. });
  24. if ($searchOrderNumber) {
  25. $reservations = array_filter($reservations, function($r) use ($searchOrderNumber) {
  26. return stripos($r['id'], $searchOrderNumber) !== false;
  27. });
  28. } else {
  29. switch ($filter) {
  30. case 'pending':
  31. $reservations = array_filter($reservations, function($r) {
  32. return !isset($r['backorder_status']) || $r['backorder_status'] !== 'notified';
  33. });
  34. break;
  35. }
  36. }
  37. $reservations = array_reverse($reservations); // Newest first
  38. $canNotifyMap = [];
  39. foreach ($reservations as $reservation) {
  40. $isNotified = isset($reservation['backorder_status']) && $reservation['backorder_status'] === 'notified';
  41. $canNotifyMap[$reservation['id']] = !$isNotified && canFulfillReservationItems($reservation['items']);
  42. }
  43. $productsById = [];
  44. foreach (getProducts() as $product) {
  45. $productsById[$product['id']] = $product;
  46. }
  47. $backorderSummary = [];
  48. foreach ($reservations as $reservation) {
  49. foreach ($reservation['items'] as $item) {
  50. $productId = $item['product_id'];
  51. if (!isset($productsById[$productId])) {
  52. continue;
  53. }
  54. $sizeLabel = isset($item['size']) && $item['size'] !== '' ? $item['size'] : '';
  55. $key = $productId . '|' . $sizeLabel;
  56. if (!isset($backorderSummary[$key])) {
  57. $backorderSummary[$key] = [
  58. 'name' => $productsById[$productId]['name'],
  59. 'size' => $sizeLabel,
  60. 'quantity' => 0
  61. ];
  62. }
  63. $backorderSummary[$key]['quantity'] += (int)$item['quantity'];
  64. }
  65. }
  66. $summaryRows = array_values($backorderSummary);
  67. usort($summaryRows, function($a, $b) {
  68. $nameCompare = strcasecmp($a['name'], $b['name']);
  69. if ($nameCompare !== 0) {
  70. return $nameCompare;
  71. }
  72. return strcasecmp($a['size'], $b['size']);
  73. });
  74. $summaryTotal = 0;
  75. foreach ($summaryRows as $row) {
  76. $summaryTotal += $row['quantity'];
  77. }
  78. include __DIR__ . '/../includes/header.php';
  79. ?>
  80. <div class="admin-header">
  81. <h2>Vorbestellungen verwalten</h2>
  82. <div>
  83. <a href="index.php" class="btn btn-secondary">Zurück zum Dashboard</a>
  84. </div>
  85. </div>
  86. <?php if (isset($message)): ?>
  87. <div class="alert alert-<?php echo $messageType; ?>">
  88. <?php echo htmlspecialchars($message); ?>
  89. </div>
  90. <?php endif; ?>
  91. <div class="panel">
  92. <form method="GET" style="display: flex; gap: 1rem; align-items: end; flex-wrap: wrap;">
  93. <div style="flex: 1; min-width: 200px;">
  94. <label for="order_number">Bestellnummer suchen:</label>
  95. <input type="text" id="order_number" name="order_number" value="<?php echo htmlspecialchars($searchOrderNumber); ?>" placeholder="Bestellnummer">
  96. </div>
  97. <div>
  98. <label for="filter">Filter:</label>
  99. <select id="filter" name="filter">
  100. <option value="pending" <?php echo $filter === 'pending' ? 'selected' : ''; ?>>Offen</option>
  101. <option value="all" <?php echo $filter === 'all' ? 'selected' : ''; ?>>Alle</option>
  102. </select>
  103. </div>
  104. <div>
  105. <button type="submit" class="btn">Filtern</button>
  106. <a href="backorders.php" class="btn btn-secondary">Zurücksetzen</a>
  107. </div>
  108. </form>
  109. </div>
  110. <?php if (!empty($summaryRows)): ?>
  111. <div class="panel">
  112. <h3>Übersicht Vorbestellte Artikel</h3>
  113. <table class="table-compact">
  114. <thead>
  115. <tr>
  116. <th>Produkt</th>
  117. <th>Größe</th>
  118. <th>Menge</th>
  119. </tr>
  120. </thead>
  121. <tbody>
  122. <?php foreach ($summaryRows as $row): ?>
  123. <tr>
  124. <td><?php echo htmlspecialchars($row['name']); ?></td>
  125. <td><?php echo $row['size'] !== '' ? htmlspecialchars($row['size']) : '—'; ?></td>
  126. <td><?php echo (int)$row['quantity']; ?></td>
  127. </tr>
  128. <?php endforeach; ?>
  129. </tbody>
  130. </table>
  131. <p class="mt-2"><strong>Gesamt:</strong> <?php echo (int)$summaryTotal; ?> Artikel</p>
  132. </div>
  133. <?php endif; ?>
  134. <?php if (empty($reservations)): ?>
  135. <div class="alert alert-info">
  136. <p>Keine Vorbestellungen gefunden.</p>
  137. </div>
  138. <?php else: ?>
  139. <table>
  140. <thead>
  141. <tr>
  142. <th>Bestellnummer</th>
  143. <th>Kunde</th>
  144. <th>E-Mail</th>
  145. <th>Artikel</th>
  146. <th>Erstellt</th>
  147. <th>Status</th>
  148. <th>Aktionen</th>
  149. </tr>
  150. </thead>
  151. <tbody>
  152. <?php foreach ($reservations as $reservation): ?>
  153. <tr>
  154. <td><strong><?php echo htmlspecialchars($reservation['id']); ?></strong></td>
  155. <td><?php echo htmlspecialchars($reservation['customer_name']); ?></td>
  156. <td><?php echo htmlspecialchars($reservation['customer_email']); ?></td>
  157. <td>
  158. <?php
  159. $itemCount = 0;
  160. foreach ($reservation['items'] as $item) {
  161. $itemCount += $item['quantity'];
  162. }
  163. echo $itemCount . ' Artikel';
  164. ?>
  165. </td>
  166. <td><?php echo formatDate($reservation['created']); ?></td>
  167. <td>
  168. <?php
  169. if (isset($reservation['backorder_status']) && $reservation['backorder_status'] === 'notified') {
  170. echo '<span class="status status-notified">Informiert</span>';
  171. } else {
  172. echo '<span class="status status-open">Offen</span>';
  173. }
  174. ?>
  175. </td>
  176. <td>
  177. <?php if ((!isset($reservation['backorder_status']) || $reservation['backorder_status'] !== 'notified') && canFulfillReservationItems($reservation['items'])): ?>
  178. <form method="POST" style="display: inline;" onsubmit="return confirm('Kunden informieren, dass die Vorbestellung zur Abholung bereit ist?');">
  179. <input type="hidden" name="reservation_id" value="<?php echo htmlspecialchars($reservation['id']); ?>">
  180. <button type="submit" name="notify_available" class="btn btn-small">Abholung bereit</button>
  181. </form>
  182. <?php endif; ?>
  183. <button onclick="showDetails('<?php echo htmlspecialchars($reservation['id']); ?>')" class="btn btn-secondary btn-small">Details</button>
  184. </td>
  185. </tr>
  186. <?php endforeach; ?>
  187. </tbody>
  188. </table>
  189. <?php endif; ?>
  190. <!-- Details Modal -->
  191. <div id="detailsModal" class="modal">
  192. <div class="modal-content">
  193. <button onclick="closeDetails()" class="btn btn-small modal-close">Schließen</button>
  194. <div id="detailsContent"></div>
  195. <form method="POST" id="notifyForm" style="margin-top: 1.5rem; display: none;" onsubmit="return confirm('Kunden informieren, dass die Vorbestellung zur Abholung bereit ist?');">
  196. <input type="hidden" name="reservation_id" id="notifyReservationId" value="">
  197. <button type="submit" name="notify_available" class="btn">Abholung bereit</button>
  198. </form>
  199. </div>
  200. </div>
  201. <script>
  202. function showDetails(reservationId) {
  203. const reservations = <?php echo json_encode(getReservations()); ?>;
  204. const reservation = reservations.find(r => r.id === reservationId);
  205. const canNotifyMap = <?php echo json_encode($canNotifyMap); ?>;
  206. if (!reservation) return;
  207. let itemsHtml = '<h3>Artikel:</h3><ul>';
  208. reservation.items.forEach(item => {
  209. const product = <?php echo json_encode(getProducts()); ?>.find(p => p.id == item.product_id);
  210. if (product) {
  211. let sizeInfo = '';
  212. if (item.size && item.size !== '') {
  213. sizeInfo = ` - Größe: ${item.size}`;
  214. }
  215. itemsHtml += `<li>${product.name}${sizeInfo} - Menge: ${item.quantity}</li>`;
  216. }
  217. });
  218. itemsHtml += '</ul>';
  219. const statusText = reservation.backorder_status === 'notified' ? 'Informiert' : 'Offen';
  220. const statusClass = reservation.backorder_status === 'notified' ? 'status-notified' : 'status-open';
  221. const html = `
  222. <h2>Vorbestellungsdetails</h2>
  223. <p><strong>Bestellnummer:</strong> <strong class="order-highlight">${reservation.id}</strong></p>
  224. <p><strong>Kunde:</strong> ${reservation.customer_name}</p>
  225. <p><strong>E-Mail:</strong> ${reservation.customer_email}</p>
  226. <p><strong>Erstellt:</strong> ${reservation.created}</p>
  227. <p><strong>Status:</strong> <span class="status ${statusClass}">${statusText}</span></p>
  228. ${itemsHtml}
  229. `;
  230. document.getElementById('detailsContent').innerHTML = html;
  231. const notifyForm = document.getElementById('notifyForm');
  232. const notifyReservationId = document.getElementById('notifyReservationId');
  233. if (canNotifyMap[reservation.id]) {
  234. notifyReservationId.value = reservation.id;
  235. notifyForm.style.display = 'block';
  236. } else {
  237. notifyReservationId.value = '';
  238. notifyForm.style.display = 'none';
  239. }
  240. document.getElementById('detailsModal').style.display = 'flex';
  241. }
  242. function closeDetails() {
  243. document.getElementById('detailsModal').style.display = 'none';
  244. }
  245. </script>
  246. <?php include __DIR__ . '/../includes/footer.php'; ?>