| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <?php
- require_once __DIR__ . '/../config.php';
- require_once __DIR__ . '/../includes/functions.php';
- // Check admin login
- if (!isset($_SESSION['admin_logged_in']) || !$_SESSION['admin_logged_in']) {
- header('Location: login.php');
- exit;
- }
- $pageTitle = 'Nachbestellungen verwalten';
- // Handle availability notification
- if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['notify_available'])) {
- $reservationId = sanitize($_POST['reservation_id']);
- $result = markBackorderAvailable($reservationId);
- $message = $result['success'] ? 'Kunde wurde informiert, dass die Nachbestellung zur Abholung bereit ist.' : $result['message'];
- $messageType = $result['success'] ? 'success' : 'error';
- }
- $reservations = getReservations();
- $filter = isset($_GET['filter']) ? sanitize($_GET['filter']) : 'pending';
- $searchOrderNumber = isset($_GET['order_number']) ? sanitize($_GET['order_number']) : '';
- // Filter backorders
- $reservations = array_filter($reservations, function($r) {
- return isset($r['type']) && $r['type'] === 'backorder';
- });
- if ($searchOrderNumber) {
- $reservations = array_filter($reservations, function($r) use ($searchOrderNumber) {
- return stripos($r['id'], $searchOrderNumber) !== false;
- });
- } else {
- switch ($filter) {
- case 'pending':
- $reservations = array_filter($reservations, function($r) {
- return !isset($r['backorder_status']) || $r['backorder_status'] !== 'notified';
- });
- break;
- }
- }
- $reservations = array_reverse($reservations); // Newest first
- $canNotifyMap = [];
- foreach ($reservations as $reservation) {
- $isNotified = isset($reservation['backorder_status']) && $reservation['backorder_status'] === 'notified';
- $canNotifyMap[$reservation['id']] = !$isNotified && canFulfillReservationItems($reservation['items']);
- }
- $productsById = [];
- foreach (getProducts() as $product) {
- $productsById[$product['id']] = $product;
- }
- $backorderSummary = [];
- foreach ($reservations as $reservation) {
- foreach ($reservation['items'] as $item) {
- $productId = $item['product_id'];
- if (!isset($productsById[$productId])) {
- continue;
- }
- $sizeLabel = isset($item['size']) && $item['size'] !== '' ? $item['size'] : '';
- $key = $productId . '|' . $sizeLabel;
- if (!isset($backorderSummary[$key])) {
- $backorderSummary[$key] = [
- 'name' => $productsById[$productId]['name'],
- 'size' => $sizeLabel,
- 'quantity' => 0
- ];
- }
- $backorderSummary[$key]['quantity'] += (int)$item['quantity'];
- }
- }
- $summaryRows = array_values($backorderSummary);
- usort($summaryRows, function($a, $b) {
- $nameCompare = strcasecmp($a['name'], $b['name']);
- if ($nameCompare !== 0) {
- return $nameCompare;
- }
- return strcasecmp($a['size'], $b['size']);
- });
- $summaryTotal = 0;
- foreach ($summaryRows as $row) {
- $summaryTotal += $row['quantity'];
- }
- include __DIR__ . '/../includes/header.php';
- ?>
- <div class="admin-header">
- <h2>Nachbestellungen verwalten</h2>
- <div>
- <a href="index.php" class="btn btn-secondary">Zurück zum Dashboard</a>
- </div>
- </div>
- <?php if (isset($message)): ?>
- <div class="alert alert-<?php echo $messageType; ?>">
- <?php echo htmlspecialchars($message); ?>
- </div>
- <?php endif; ?>
- <div class="panel">
- <form method="GET" style="display: flex; gap: 1rem; align-items: end; flex-wrap: wrap;">
- <div style="flex: 1; min-width: 200px;">
- <label for="order_number">Bestellnummer suchen:</label>
- <input type="text" id="order_number" name="order_number" value="<?php echo htmlspecialchars($searchOrderNumber); ?>" placeholder="Bestellnummer">
- </div>
- <div>
- <label for="filter">Filter:</label>
- <select id="filter" name="filter">
- <option value="pending" <?php echo $filter === 'pending' ? 'selected' : ''; ?>>Offen</option>
- <option value="all" <?php echo $filter === 'all' ? 'selected' : ''; ?>>Alle</option>
- </select>
- </div>
- <div>
- <button type="submit" class="btn">Filtern</button>
- <a href="backorders.php" class="btn btn-secondary">Zurücksetzen</a>
- </div>
- </form>
- </div>
- <?php if (!empty($summaryRows)): ?>
- <div class="panel">
- <h3>Übersicht Nachbestellte Artikel</h3>
- <table class="table-compact">
- <thead>
- <tr>
- <th>Produkt</th>
- <th>Größe</th>
- <th>Menge</th>
- </tr>
- </thead>
- <tbody>
- <?php foreach ($summaryRows as $row): ?>
- <tr>
- <td><?php echo htmlspecialchars($row['name']); ?></td>
- <td><?php echo $row['size'] !== '' ? htmlspecialchars($row['size']) : '—'; ?></td>
- <td><?php echo (int)$row['quantity']; ?></td>
- </tr>
- <?php endforeach; ?>
- </tbody>
- </table>
- <p class="mt-2"><strong>Gesamt:</strong> <?php echo (int)$summaryTotal; ?> Artikel</p>
- </div>
- <?php endif; ?>
- <?php if (empty($reservations)): ?>
- <div class="alert alert-info">
- <p>Keine Nachbestellungen gefunden.</p>
- </div>
- <?php else: ?>
- <table>
- <thead>
- <tr>
- <th>Bestellnummer</th>
- <th>Kunde</th>
- <th>E-Mail</th>
- <th>Artikel</th>
- <th>Erstellt</th>
- <th>Status</th>
- <th>Aktionen</th>
- </tr>
- </thead>
- <tbody>
- <?php foreach ($reservations as $reservation): ?>
- <tr>
- <td><strong><?php echo htmlspecialchars($reservation['id']); ?></strong></td>
- <td><?php echo htmlspecialchars($reservation['customer_name']); ?></td>
- <td><?php echo htmlspecialchars($reservation['customer_email']); ?></td>
- <td>
- <?php
- $itemCount = 0;
- foreach ($reservation['items'] as $item) {
- $itemCount += $item['quantity'];
- }
- echo $itemCount . ' Artikel';
- ?>
- </td>
- <td><?php echo formatDate($reservation['created']); ?></td>
- <td>
- <?php
- if (isset($reservation['backorder_status']) && $reservation['backorder_status'] === 'notified') {
- echo '<span class="status status-notified">Informiert</span>';
- } else {
- echo '<span class="status status-open">Offen</span>';
- }
- ?>
- </td>
- <td>
- <?php if ((!isset($reservation['backorder_status']) || $reservation['backorder_status'] !== 'notified') && canFulfillReservationItems($reservation['items'])): ?>
- <form method="POST" style="display: inline;" onsubmit="return confirm('Kunden informieren, dass die Nachbestellung zur Abholung bereit ist?');">
- <input type="hidden" name="reservation_id" value="<?php echo htmlspecialchars($reservation['id']); ?>">
- <button type="submit" name="notify_available" class="btn btn-small">Abholung bereit</button>
- </form>
- <?php endif; ?>
- <button onclick="showDetails('<?php echo htmlspecialchars($reservation['id']); ?>')" class="btn btn-secondary btn-small">Details</button>
- </td>
- </tr>
- <?php endforeach; ?>
- </tbody>
- </table>
- <?php endif; ?>
- <!-- Details Modal -->
- <div id="detailsModal" class="modal">
- <div class="modal-content">
- <button onclick="closeDetails()" class="btn btn-small modal-close">Schließen</button>
- <div id="detailsContent"></div>
- <form method="POST" id="notifyForm" style="margin-top: 1.5rem; display: none;" onsubmit="return confirm('Kunden informieren, dass die Nachbestellung zur Abholung bereit ist?');">
- <input type="hidden" name="reservation_id" id="notifyReservationId" value="">
- <button type="submit" name="notify_available" class="btn">Abholung bereit</button>
- </form>
- </div>
- </div>
- <script>
- function showDetails(reservationId) {
- const reservations = <?php echo json_encode(getReservations()); ?>;
- const reservation = reservations.find(r => r.id === reservationId);
- const canNotifyMap = <?php echo json_encode($canNotifyMap); ?>;
-
- if (!reservation) return;
-
- let itemsHtml = '<h3>Artikel:</h3><ul>';
- reservation.items.forEach(item => {
- const product = <?php echo json_encode(getProducts()); ?>.find(p => p.id == item.product_id);
- if (product) {
- let sizeInfo = '';
- if (item.size && item.size !== '') {
- sizeInfo = ` - Größe: ${item.size}`;
- }
- itemsHtml += `<li>${product.name}${sizeInfo} - Menge: ${item.quantity}</li>`;
- }
- });
- itemsHtml += '</ul>';
-
- const statusText = reservation.backorder_status === 'notified' ? 'Informiert' : 'Offen';
- const statusClass = reservation.backorder_status === 'notified' ? 'status-notified' : 'status-open';
- const html = `
- <h2>Nachbestellungsdetails</h2>
- <p><strong>Bestellnummer:</strong> <strong class="order-highlight">${reservation.id}</strong></p>
- <p><strong>Kunde:</strong> ${reservation.customer_name}</p>
- <p><strong>E-Mail:</strong> ${reservation.customer_email}</p>
- <p><strong>Erstellt:</strong> ${reservation.created}</p>
- <p><strong>Status:</strong> <span class="status ${statusClass}">${statusText}</span></p>
- ${itemsHtml}
- `;
-
- document.getElementById('detailsContent').innerHTML = html;
- const notifyForm = document.getElementById('notifyForm');
- const notifyReservationId = document.getElementById('notifyReservationId');
- if (canNotifyMap[reservation.id]) {
- notifyReservationId.value = reservation.id;
- notifyForm.style.display = 'block';
- } else {
- notifyReservationId.value = '';
- notifyForm.style.display = 'none';
- }
- document.getElementById('detailsModal').style.display = 'flex';
- }
- function closeDetails() {
- document.getElementById('detailsModal').style.display = 'none';
- }
- </script>
- <?php include __DIR__ . '/../includes/footer.php'; ?>
|