backorders.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. $bodyClass = 'admin-page';
  79. include __DIR__ . '/../includes/header.php';
  80. ?>
  81. <div class="admin-header">
  82. <h2>Vorbestellungen verwalten</h2>
  83. <div>
  84. <a href="index.php" class="btn btn-secondary">Zurück zum Dashboard</a>
  85. </div>
  86. </div>
  87. <?php if (isset($message)): ?>
  88. <div class="alert alert-<?php echo $messageType; ?>">
  89. <?php echo htmlspecialchars($message); ?>
  90. </div>
  91. <?php endif; ?>
  92. <div class="panel">
  93. <form method="GET" style="display: flex; gap: 1rem; align-items: end; flex-wrap: wrap;">
  94. <div style="flex: 1; min-width: 200px;">
  95. <label for="order_number">Bestellnummer suchen:</label>
  96. <input type="text" id="order_number" name="order_number" value="<?php echo htmlspecialchars($searchOrderNumber); ?>" placeholder="Bestellnummer">
  97. </div>
  98. <div>
  99. <label for="filter">Filter:</label>
  100. <select id="filter" name="filter">
  101. <option value="pending" <?php echo $filter === 'pending' ? 'selected' : ''; ?>>Offen</option>
  102. <option value="all" <?php echo $filter === 'all' ? 'selected' : ''; ?>>Alle</option>
  103. </select>
  104. </div>
  105. <div>
  106. <button type="submit" class="btn">Filtern</button>
  107. <a href="backorders.php" class="btn btn-secondary">Zurücksetzen</a>
  108. </div>
  109. </form>
  110. </div>
  111. <?php if (!empty($summaryRows)): ?>
  112. <div class="panel">
  113. <h3>Übersicht Vorbestellte Artikel</h3>
  114. <div class="table-responsive">
  115. <table class="table-compact">
  116. <thead>
  117. <tr>
  118. <th>Produkt</th>
  119. <th>Größe</th>
  120. <th>Menge</th>
  121. </tr>
  122. </thead>
  123. <tbody>
  124. <?php foreach ($summaryRows as $row): ?>
  125. <tr>
  126. <td><?php echo htmlspecialchars($row['name']); ?></td>
  127. <td><?php echo $row['size'] !== '' ? htmlspecialchars($row['size']) : '—'; ?></td>
  128. <td><?php echo (int)$row['quantity']; ?></td>
  129. </tr>
  130. <?php endforeach; ?>
  131. </tbody>
  132. </table>
  133. </div>
  134. <p class="mt-2"><strong>Gesamt:</strong> <?php echo (int)$summaryTotal; ?> Artikel</p>
  135. </div>
  136. <?php endif; ?>
  137. <?php if (empty($reservations)): ?>
  138. <div class="alert alert-info">
  139. <p>Keine Vorbestellungen gefunden.</p>
  140. </div>
  141. <?php else: ?>
  142. <div class="table-responsive">
  143. <table>
  144. <thead>
  145. <tr>
  146. <th>Bestellnummer</th>
  147. <th>Kunde</th>
  148. <th>E-Mail</th>
  149. <th>Artikel</th>
  150. <th>Erstellt</th>
  151. <th>Status</th>
  152. <th>Aktionen</th>
  153. </tr>
  154. </thead>
  155. <tbody>
  156. <?php foreach ($reservations as $reservation): ?>
  157. <tr>
  158. <td><strong><?php echo htmlspecialchars($reservation['id']); ?></strong></td>
  159. <td><?php echo htmlspecialchars($reservation['customer_name']); ?></td>
  160. <td><?php echo htmlspecialchars($reservation['customer_email']); ?></td>
  161. <td>
  162. <?php
  163. $itemCount = 0;
  164. foreach ($reservation['items'] as $item) {
  165. $itemCount += $item['quantity'];
  166. }
  167. echo $itemCount . ' Artikel';
  168. ?>
  169. </td>
  170. <td><?php echo formatDate($reservation['created']); ?></td>
  171. <td>
  172. <?php
  173. if (isset($reservation['backorder_status']) && $reservation['backorder_status'] === 'notified') {
  174. echo '<span class="status status-notified">Informiert</span>';
  175. } else {
  176. echo '<span class="status status-open">Offen</span>';
  177. }
  178. ?>
  179. </td>
  180. <td>
  181. <?php if ((!isset($reservation['backorder_status']) || $reservation['backorder_status'] !== 'notified') && canFulfillReservationItems($reservation['items'])): ?>
  182. <form method="POST" style="display: inline;" onsubmit="return confirm('Kunden informieren, dass die Vorbestellung zur Abholung bereit ist?');">
  183. <input type="hidden" name="reservation_id" value="<?php echo htmlspecialchars($reservation['id']); ?>">
  184. <button type="submit" name="notify_available" class="btn btn-small">Abholung bereit</button>
  185. </form>
  186. <?php endif; ?>
  187. <button onclick="showDetails('<?php echo htmlspecialchars($reservation['id']); ?>')" class="btn btn-secondary btn-small">Details</button>
  188. </td>
  189. </tr>
  190. <?php endforeach; ?>
  191. </tbody>
  192. </table>
  193. </div>
  194. <?php endif; ?>
  195. <!-- Details Modal -->
  196. <div id="detailsModal" class="modal">
  197. <div class="modal-content">
  198. <button onclick="closeDetails()" class="btn btn-small modal-close">Schließen</button>
  199. <div id="detailsContent"></div>
  200. <form method="POST" id="notifyForm" style="margin-top: 1.5rem; display: none;" onsubmit="return confirm('Kunden informieren, dass die Vorbestellung zur Abholung bereit ist?');">
  201. <input type="hidden" name="reservation_id" id="notifyReservationId" value="">
  202. <button type="submit" name="notify_available" class="btn">Abholung bereit</button>
  203. </form>
  204. </div>
  205. </div>
  206. <script>
  207. function showDetails(reservationId) {
  208. const reservations = <?php echo json_encode(getReservations()); ?>;
  209. const reservation = reservations.find(r => r.id === reservationId);
  210. const canNotifyMap = <?php echo json_encode($canNotifyMap); ?>;
  211. if (!reservation) return;
  212. let itemsHtml = '<h3>Artikel:</h3><ul>';
  213. reservation.items.forEach(item => {
  214. const product = <?php echo json_encode(getProducts()); ?>.find(p => p.id == item.product_id);
  215. if (product) {
  216. let sizeInfo = '';
  217. if (item.size && item.size !== '') {
  218. sizeInfo = ` - Größe: ${item.size}`;
  219. }
  220. itemsHtml += `<li>${product.name}${sizeInfo} - Menge: ${item.quantity}</li>`;
  221. }
  222. });
  223. itemsHtml += '</ul>';
  224. const statusText = reservation.backorder_status === 'notified' ? 'Informiert' : 'Offen';
  225. const statusClass = reservation.backorder_status === 'notified' ? 'status-notified' : 'status-open';
  226. const html = `
  227. <h2>Vorbestellungsdetails</h2>
  228. <p><strong>Bestellnummer:</strong> <strong class="order-highlight">${reservation.id}</strong></p>
  229. <p><strong>Kunde:</strong> ${reservation.customer_name}</p>
  230. <p><strong>E-Mail:</strong> ${reservation.customer_email}</p>
  231. <p><strong>Erstellt:</strong> ${reservation.created}</p>
  232. <p><strong>Status:</strong> <span class="status ${statusClass}">${statusText}</span></p>
  233. ${itemsHtml}
  234. `;
  235. document.getElementById('detailsContent').innerHTML = html;
  236. const notifyForm = document.getElementById('notifyForm');
  237. const notifyReservationId = document.getElementById('notifyReservationId');
  238. if (canNotifyMap[reservation.id]) {
  239. notifyReservationId.value = reservation.id;
  240. notifyForm.style.display = 'block';
  241. } else {
  242. notifyReservationId.value = '';
  243. notifyForm.style.display = 'none';
  244. }
  245. document.getElementById('detailsModal').style.display = 'flex';
  246. }
  247. function closeDetails() {
  248. document.getElementById('detailsModal').style.display = 'none';
  249. }
  250. </script>
  251. <?php include __DIR__ . '/../includes/footer.php'; ?>