| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?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 über die Verfügbarkeit informiert.' : $result['message'];
- $messageType = $result['success'] ? 'success' : 'error';
- }
- $reservations = getReservations();
- $filter = isset($_GET['filter']) ? sanitize($_GET['filter']) : 'pending';
- $searchCode = isset($_GET['code']) ? sanitize($_GET['code']) : '';
- // Filter backorders
- $reservations = array_filter($reservations, function($r) {
- return isset($r['type']) && $r['type'] === 'backorder';
- });
- if ($searchCode) {
- $reservations = array_filter($reservations, function($r) use ($searchCode) {
- return stripos($r['code'], $searchCode) !== false || stripos($r['id'], $searchCode) !== false;
- });
- } else {
- switch ($filter) {
- case 'notified':
- $reservations = array_filter($reservations, function($r) {
- return isset($r['backorder_status']) && $r['backorder_status'] === 'notified';
- });
- break;
- 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']);
- }
- 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 style="background: white; padding: 1.5rem; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); margin-bottom: 2rem;">
- <form method="GET" style="display: flex; gap: 1rem; align-items: end; flex-wrap: wrap;">
- <div style="flex: 1; min-width: 200px;">
- <label for="code">Code/ID suchen:</label>
- <input type="text" id="code" name="code" value="<?php echo htmlspecialchars($searchCode); ?>" placeholder="Abholcode oder Nachbestellungsnummer">
- </div>
- <div>
- <label for="filter">Filter:</label>
- <select id="filter" name="filter">
- <option value="pending" <?php echo $filter === 'pending' ? 'selected' : ''; ?>>Offen</option>
- <option value="notified" <?php echo $filter === 'notified' ? 'selected' : ''; ?>>Informiert</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($reservations)): ?>
- <div class="alert alert-info">
- <p>Keine Nachbestellungen gefunden.</p>
- </div>
- <?php else: ?>
- <table>
- <thead>
- <tr>
- <th>Code</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['code']); ?></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 style="color: #28a745;">Informiert</span>';
- } else {
- echo '<span style="color: #ffc107;">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('Kunde über Verfügbarkeit informieren?');">
- <input type="hidden" name="reservation_id" value="<?php echo htmlspecialchars($reservation['id']); ?>">
- <button type="submit" name="notify_available" class="btn btn-small">Abholung freigeben</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" 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;">
- <div style="background: white; padding: 2rem; border-radius: 8px; max-width: 900px; max-height: 90vh; overflow-y: auto; position: relative;">
- <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>
- <div id="detailsContent"></div>
- <form method="POST" id="notifyForm" style="margin-top: 1.5rem; display: none;" onsubmit="return confirm('Kunde über Verfügbarkeit informieren?');">
- <input type="hidden" name="reservation_id" id="notifyReservationId" value="">
- <button type="submit" name="notify_available" class="btn">Abholung freigeben</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 html = `
- <h2>Nachbestellungsdetails</h2>
- <p><strong>Nachbestellungsnummer:</strong> ${reservation.id}</p>
- <p><strong>Abholcode:</strong> <strong style="font-size: 1.5rem; color: #c41e3a;">${reservation.code}</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> ${statusText}</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'; ?>
|