backorders.php 13 KB

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