backorders.php 12 KB

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