backorders.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 über die Verfügbarkeit informiert.' : $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. include __DIR__ . '/../includes/header.php';
  49. ?>
  50. <div class="admin-header">
  51. <h2>Nachbestellungen verwalten</h2>
  52. <div>
  53. <a href="index.php" class="btn btn-secondary">Zurück zum Dashboard</a>
  54. </div>
  55. </div>
  56. <?php if (isset($message)): ?>
  57. <div class="alert alert-<?php echo $messageType; ?>">
  58. <?php echo htmlspecialchars($message); ?>
  59. </div>
  60. <?php endif; ?>
  61. <div style="background: white; padding: 1.5rem; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); margin-bottom: 2rem;">
  62. <form method="GET" style="display: flex; gap: 1rem; align-items: end; flex-wrap: wrap;">
  63. <div style="flex: 1; min-width: 200px;">
  64. <label for="code">Code/ID suchen:</label>
  65. <input type="text" id="code" name="code" value="<?php echo htmlspecialchars($searchCode); ?>" placeholder="Abholcode oder Nachbestellungsnummer">
  66. </div>
  67. <div>
  68. <label for="filter">Filter:</label>
  69. <select id="filter" name="filter">
  70. <option value="pending" <?php echo $filter === 'pending' ? 'selected' : ''; ?>>Offen</option>
  71. <option value="notified" <?php echo $filter === 'notified' ? 'selected' : ''; ?>>Informiert</option>
  72. <option value="all" <?php echo $filter === 'all' ? 'selected' : ''; ?>>Alle</option>
  73. </select>
  74. </div>
  75. <div>
  76. <button type="submit" class="btn">Filtern</button>
  77. <a href="backorders.php" class="btn btn-secondary">Zurücksetzen</a>
  78. </div>
  79. </form>
  80. </div>
  81. <?php if (empty($reservations)): ?>
  82. <div class="alert alert-info">
  83. <p>Keine Nachbestellungen gefunden.</p>
  84. </div>
  85. <?php else: ?>
  86. <table>
  87. <thead>
  88. <tr>
  89. <th>Code</th>
  90. <th>Kunde</th>
  91. <th>E-Mail</th>
  92. <th>Artikel</th>
  93. <th>Erstellt</th>
  94. <th>Status</th>
  95. <th>Aktionen</th>
  96. </tr>
  97. </thead>
  98. <tbody>
  99. <?php foreach ($reservations as $reservation): ?>
  100. <tr>
  101. <td><strong><?php echo htmlspecialchars($reservation['code']); ?></strong></td>
  102. <td><?php echo htmlspecialchars($reservation['customer_name']); ?></td>
  103. <td><?php echo htmlspecialchars($reservation['customer_email']); ?></td>
  104. <td>
  105. <?php
  106. $itemCount = 0;
  107. foreach ($reservation['items'] as $item) {
  108. $itemCount += $item['quantity'];
  109. }
  110. echo $itemCount . ' Artikel';
  111. ?>
  112. </td>
  113. <td><?php echo formatDate($reservation['created']); ?></td>
  114. <td>
  115. <?php
  116. if (isset($reservation['backorder_status']) && $reservation['backorder_status'] === 'notified') {
  117. echo '<span style="color: #28a745;">Informiert</span>';
  118. } else {
  119. echo '<span style="color: #ffc107;">Offen</span>';
  120. }
  121. ?>
  122. </td>
  123. <td>
  124. <?php if ((!isset($reservation['backorder_status']) || $reservation['backorder_status'] !== 'notified') && canFulfillReservationItems($reservation['items'])): ?>
  125. <form method="POST" style="display: inline;" onsubmit="return confirm('Kunde über Verfügbarkeit informieren?');">
  126. <input type="hidden" name="reservation_id" value="<?php echo htmlspecialchars($reservation['id']); ?>">
  127. <button type="submit" name="notify_available" class="btn btn-small">Abholung freigeben</button>
  128. </form>
  129. <?php endif; ?>
  130. <button onclick="showDetails('<?php echo htmlspecialchars($reservation['id']); ?>')" class="btn btn-secondary btn-small">Details</button>
  131. </td>
  132. </tr>
  133. <?php endforeach; ?>
  134. </tbody>
  135. </table>
  136. <?php endif; ?>
  137. <!-- Details Modal -->
  138. <div id="detailsModal" style="display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); z-index: 1000; align-items: center; justify-content: center;">
  139. <div style="background: white; padding: 2rem; border-radius: 8px; max-width: 900px; max-height: 90vh; overflow-y: auto; position: relative;">
  140. <button onclick="closeDetails()" style="position: absolute; top: 1rem; right: 1rem; background: #dc3545; color: white; border: none; border-radius: 4px; padding: 0.5rem 1rem; cursor: pointer;">Schließen</button>
  141. <div id="detailsContent"></div>
  142. <form method="POST" id="notifyForm" style="margin-top: 1.5rem; display: none;" onsubmit="return confirm('Kunde über Verfügbarkeit informieren?');">
  143. <input type="hidden" name="reservation_id" id="notifyReservationId" value="">
  144. <button type="submit" name="notify_available" class="btn">Abholung freigeben</button>
  145. </form>
  146. </div>
  147. </div>
  148. <script>
  149. function showDetails(reservationId) {
  150. const reservations = <?php echo json_encode(getReservations()); ?>;
  151. const reservation = reservations.find(r => r.id === reservationId);
  152. const canNotifyMap = <?php echo json_encode($canNotifyMap); ?>;
  153. if (!reservation) return;
  154. let itemsHtml = '<h3>Artikel:</h3><ul>';
  155. reservation.items.forEach(item => {
  156. const product = <?php echo json_encode(getProducts()); ?>.find(p => p.id == item.product_id);
  157. if (product) {
  158. let sizeInfo = '';
  159. if (item.size && item.size !== '') {
  160. sizeInfo = ` - Größe: ${item.size}`;
  161. }
  162. itemsHtml += `<li>${product.name}${sizeInfo} - Menge: ${item.quantity}</li>`;
  163. }
  164. });
  165. itemsHtml += '</ul>';
  166. const statusText = reservation.backorder_status === 'notified' ? 'Informiert' : 'Offen';
  167. const html = `
  168. <h2>Nachbestellungsdetails</h2>
  169. <p><strong>Nachbestellungsnummer:</strong> ${reservation.id}</p>
  170. <p><strong>Abholcode:</strong> <strong style="font-size: 1.5rem; color: #c41e3a;">${reservation.code}</strong></p>
  171. <p><strong>Kunde:</strong> ${reservation.customer_name}</p>
  172. <p><strong>E-Mail:</strong> ${reservation.customer_email}</p>
  173. <p><strong>Erstellt:</strong> ${reservation.created}</p>
  174. <p><strong>Status:</strong> ${statusText}</p>
  175. ${itemsHtml}
  176. `;
  177. document.getElementById('detailsContent').innerHTML = html;
  178. const notifyForm = document.getElementById('notifyForm');
  179. const notifyReservationId = document.getElementById('notifyReservationId');
  180. if (canNotifyMap[reservation.id]) {
  181. notifyReservationId.value = reservation.id;
  182. notifyForm.style.display = 'block';
  183. } else {
  184. notifyReservationId.value = '';
  185. notifyForm.style.display = 'none';
  186. }
  187. document.getElementById('detailsModal').style.display = 'flex';
  188. }
  189. function closeDetails() {
  190. document.getElementById('detailsModal').style.display = 'none';
  191. }
  192. </script>
  193. <?php include __DIR__ . '/../includes/footer.php'; ?>