backorders.php 11 KB

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