|
@@ -263,7 +263,8 @@ function createReservation($customerName, $customerEmail, $items) {
|
|
|
'created' => $now->format('Y-m-d H:i:s'),
|
|
'created' => $now->format('Y-m-d H:i:s'),
|
|
|
'expires' => $expires->format('Y-m-d H:i:s'),
|
|
'expires' => $expires->format('Y-m-d H:i:s'),
|
|
|
'status' => 'open',
|
|
'status' => 'open',
|
|
|
- 'picked_up' => false
|
|
|
|
|
|
|
+ 'picked_up' => false,
|
|
|
|
|
+ 'type' => 'regular'
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
$reservations[] = $reservation;
|
|
$reservations[] = $reservation;
|
|
@@ -275,6 +276,37 @@ function createReservation($customerName, $customerEmail, $items) {
|
|
|
return ['success' => true, 'reservation' => $reservation];
|
|
return ['success' => true, 'reservation' => $reservation];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * Create new backorder reservation
|
|
|
|
|
+ */
|
|
|
|
|
+function createBackorderReservation($customerName, $customerEmail, $items) {
|
|
|
|
|
+ $reservations = getReservations();
|
|
|
|
|
+
|
|
|
|
|
+ $now = new DateTime();
|
|
|
|
|
+
|
|
|
|
|
+ $reservation = [
|
|
|
|
|
+ 'id' => generateReservationId(),
|
|
|
|
|
+ 'code' => generateReservationCode(),
|
|
|
|
|
+ 'customer_name' => $customerName,
|
|
|
|
|
+ 'customer_email' => $customerEmail,
|
|
|
|
|
+ 'items' => $items,
|
|
|
|
|
+ 'created' => $now->format('Y-m-d H:i:s'),
|
|
|
|
|
+ 'expires' => '',
|
|
|
|
|
+ 'status' => 'open',
|
|
|
|
|
+ 'picked_up' => false,
|
|
|
|
|
+ 'type' => 'backorder',
|
|
|
|
|
+ 'backorder_status' => 'pending'
|
|
|
|
|
+ ];
|
|
|
|
|
+
|
|
|
|
|
+ $reservations[] = $reservation;
|
|
|
|
|
+ saveReservations($reservations);
|
|
|
|
|
+
|
|
|
|
|
+ // Send confirmation emails
|
|
|
|
|
+ sendBackorderEmails($reservation);
|
|
|
|
|
+
|
|
|
|
|
+ return ['success' => true, 'reservation' => $reservation];
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Mark reservation as picked up
|
|
* Mark reservation as picked up
|
|
|
*/
|
|
*/
|
|
@@ -300,6 +332,12 @@ function expireOldReservations() {
|
|
|
|
|
|
|
|
foreach ($reservations as &$reservation) {
|
|
foreach ($reservations as &$reservation) {
|
|
|
if ($reservation['status'] === 'open' && !$reservation['picked_up']) {
|
|
if ($reservation['status'] === 'open' && !$reservation['picked_up']) {
|
|
|
|
|
+ if (isset($reservation['type']) && $reservation['type'] === 'backorder') {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (empty($reservation['expires'])) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
$expires = new DateTime($reservation['expires']);
|
|
$expires = new DateTime($reservation['expires']);
|
|
|
if ($now > $expires) {
|
|
if ($now > $expires) {
|
|
|
$reservation['status'] = 'expired';
|
|
$reservation['status'] = 'expired';
|
|
@@ -318,6 +356,52 @@ function expireOldReservations() {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * Check if all items are in stock
|
|
|
|
|
+ */
|
|
|
|
|
+function canFulfillReservationItems($items) {
|
|
|
|
|
+ foreach ($items as $item) {
|
|
|
|
|
+ $size = isset($item['size']) ? $item['size'] : null;
|
|
|
|
|
+ if (!checkStock($item['product_id'], $item['quantity'], $size)) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return true;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * Mark backorder as available
|
|
|
|
|
+ */
|
|
|
|
|
+function markBackorderAvailable($reservationId) {
|
|
|
|
|
+ $reservations = getReservations();
|
|
|
|
|
+ foreach ($reservations as &$reservation) {
|
|
|
|
|
+ if ($reservation['id'] === $reservationId) {
|
|
|
|
|
+ if (!isset($reservation['type']) || $reservation['type'] !== 'backorder') {
|
|
|
|
|
+ return ['success' => false, 'message' => 'Diese Reservierung ist keine Nachbestellung.'];
|
|
|
|
|
+ }
|
|
|
|
|
+ if (isset($reservation['backorder_status']) && $reservation['backorder_status'] === 'notified') {
|
|
|
|
|
+ return ['success' => false, 'message' => 'Diese Nachbestellung wurde bereits informiert.'];
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!canFulfillReservationItems($reservation['items'])) {
|
|
|
|
|
+ return ['success' => false, 'message' => 'Nicht alle Artikel sind verfügbar.'];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ foreach ($reservation['items'] as $item) {
|
|
|
|
|
+ $size = isset($item['size']) ? $item['size'] : null;
|
|
|
|
|
+ allocateStock($item['product_id'], $item['quantity'], $size);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $reservation['backorder_status'] = 'notified';
|
|
|
|
|
+ saveReservations($reservations);
|
|
|
|
|
+
|
|
|
|
|
+ sendBackorderAvailableEmail($reservation);
|
|
|
|
|
+
|
|
|
|
|
+ return ['success' => true, 'reservation' => $reservation];
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return ['success' => false, 'message' => 'Nachbestellung nicht gefunden.'];
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Sanitize input
|
|
* Sanitize input
|
|
|
*/
|
|
*/
|
|
@@ -442,3 +526,136 @@ function sendReservationEmails($reservation) {
|
|
|
|
|
|
|
|
sendEmail(ADMIN_EMAIL, $adminSubject, $adminMessage);
|
|
sendEmail(ADMIN_EMAIL, $adminSubject, $adminMessage);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * Send backorder confirmation emails
|
|
|
|
|
+ */
|
|
|
|
|
+function sendBackorderEmails($reservation) {
|
|
|
|
|
+ // Build items list
|
|
|
|
|
+ $itemsHtml = '<ul>';
|
|
|
|
|
+ foreach ($reservation['items'] as $item) {
|
|
|
|
|
+ $product = getProductById($item['product_id']);
|
|
|
|
|
+ if ($product) {
|
|
|
|
|
+ $sizeInfo = '';
|
|
|
|
|
+ if (isset($item['size']) && !empty($item['size'])) {
|
|
|
|
|
+ $sizeInfo = ' - Größe: ' . htmlspecialchars($item['size']);
|
|
|
|
|
+ }
|
|
|
|
|
+ $itemsHtml .= '<li>' . htmlspecialchars($product['name']) . $sizeInfo . ' - Menge: ' . $item['quantity'] . '</li>';
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ $itemsHtml .= '</ul>';
|
|
|
|
|
+
|
|
|
|
|
+ // Customer email
|
|
|
|
|
+ $customerSubject = 'Nachbestellung bei ' . SITE_NAME;
|
|
|
|
|
+ $customerMessage = '
|
|
|
|
|
+ <html>
|
|
|
|
|
+ <head>
|
|
|
|
|
+ <meta charset="UTF-8">
|
|
|
|
|
+ </head>
|
|
|
|
|
+ <body style="font-family: Arial, sans-serif; line-height: 1.6; color: #333;">
|
|
|
|
|
+ <h2 style="color: #c41e3a;">Nachbestellung bestätigt</h2>
|
|
|
|
|
+ <p>Sehr geehrte/r ' . htmlspecialchars($reservation['customer_name']) . ',</p>
|
|
|
|
|
+ <p>vielen Dank für Ihre Nachbestellung bei ' . SITE_NAME . '.</p>
|
|
|
|
|
+
|
|
|
|
|
+ <div style="background: #fff3cd; border: 2px solid #ffc107; padding: 1.5rem; margin: 1.5rem 0; border-radius: 8px; text-align: center;">
|
|
|
|
|
+ <h3 style="margin-top: 0;">Ihr Abholcode:</h3>
|
|
|
|
|
+ <h2 style="font-size: 2rem; letter-spacing: 0.2rem; color: #856404; font-family: monospace;">' . htmlspecialchars($reservation['code']) . '</h2>
|
|
|
|
|
+ </div>
|
|
|
|
|
+
|
|
|
|
|
+ <h3>Nachbestellungsdetails:</h3>
|
|
|
|
|
+ <p><strong>Nachbestellungsnummer:</strong> ' . htmlspecialchars($reservation['id']) . '</p>
|
|
|
|
|
+ <p><strong>Erstellt am:</strong> ' . formatDate($reservation['created']) . '</p>
|
|
|
|
|
+
|
|
|
|
|
+ <h3>Nachbestellte Artikel:</h3>
|
|
|
|
|
+ ' . $itemsHtml . '
|
|
|
|
|
+
|
|
|
|
|
+ <div style="background: #f8d7da; border: 2px solid #f5c6cb; padding: 1.5rem; margin: 1.5rem 0; border-radius: 8px;">
|
|
|
|
|
+ <strong>Hinweis:</strong> Die Lieferzeiten sind nicht bekannt, da die Bestellung in Chargen erfolgt.
|
|
|
|
|
+ </div>
|
|
|
|
|
+
|
|
|
|
|
+ <p>Wir informieren Sie, sobald die komplette Nachbestellung verfügbar ist.</p>
|
|
|
|
|
+
|
|
|
|
|
+ <p>Mit freundlichen Grüßen<br>' . SITE_NAME . '</p>
|
|
|
|
|
+ </body>
|
|
|
|
|
+ </html>';
|
|
|
|
|
+
|
|
|
|
|
+ sendEmail($reservation['customer_email'], $customerSubject, $customerMessage);
|
|
|
|
|
+
|
|
|
|
|
+ // Admin email
|
|
|
|
|
+ $adminSubject = 'Neue Nachbestellung: ' . $reservation['code'];
|
|
|
|
|
+ $adminMessage = '
|
|
|
|
|
+ <html>
|
|
|
|
|
+ <head>
|
|
|
|
|
+ <meta charset="UTF-8">
|
|
|
|
|
+ </head>
|
|
|
|
|
+ <body style="font-family: Arial, sans-serif; line-height: 1.6; color: #333;">
|
|
|
|
|
+ <h2 style="color: #c41e3a;">Neue Nachbestellung</h2>
|
|
|
|
|
+ <p>Eine neue Nachbestellung wurde erstellt:</p>
|
|
|
|
|
+
|
|
|
|
|
+ <div style="background: #d1ecf1; border: 2px solid #bee5eb; padding: 1.5rem; margin: 1.5rem 0; border-radius: 8px;">
|
|
|
|
|
+ <h3 style="margin-top: 0;">Abholcode:</h3>
|
|
|
|
|
+ <h2 style="font-size: 2rem; letter-spacing: 0.2rem; color: #0c5460; font-family: monospace;">' . htmlspecialchars($reservation['code']) . '</h2>
|
|
|
|
|
+ </div>
|
|
|
|
|
+
|
|
|
|
|
+ <h3>Kundendaten:</h3>
|
|
|
|
|
+ <p><strong>Name:</strong> ' . htmlspecialchars($reservation['customer_name']) . '</p>
|
|
|
|
|
+ <p><strong>E-Mail:</strong> ' . htmlspecialchars($reservation['customer_email']) . '</p>
|
|
|
|
|
+
|
|
|
|
|
+ <h3>Nachbestellungsdetails:</h3>
|
|
|
|
|
+ <p><strong>Nachbestellungsnummer:</strong> ' . htmlspecialchars($reservation['id']) . '</p>
|
|
|
|
|
+ <p><strong>Erstellt am:</strong> ' . formatDate($reservation['created']) . '</p>
|
|
|
|
|
+
|
|
|
|
|
+ <h3>Nachbestellte Artikel:</h3>
|
|
|
|
|
+ ' . $itemsHtml . '
|
|
|
|
|
+
|
|
|
|
|
+ <p><strong>Hinweis:</strong> Lieferzeiten sind nicht bekannt, Bestellung in Chargen.</p>
|
|
|
|
|
+ </body>
|
|
|
|
|
+ </html>';
|
|
|
|
|
+
|
|
|
|
|
+ sendEmail(ADMIN_EMAIL, $adminSubject, $adminMessage);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * Send backorder availability email
|
|
|
|
|
+ */
|
|
|
|
|
+function sendBackorderAvailableEmail($reservation) {
|
|
|
|
|
+ $itemsHtml = '<ul>';
|
|
|
|
|
+ foreach ($reservation['items'] as $item) {
|
|
|
|
|
+ $product = getProductById($item['product_id']);
|
|
|
|
|
+ if ($product) {
|
|
|
|
|
+ $sizeInfo = '';
|
|
|
|
|
+ if (isset($item['size']) && !empty($item['size'])) {
|
|
|
|
|
+ $sizeInfo = ' - Größe: ' . htmlspecialchars($item['size']);
|
|
|
|
|
+ }
|
|
|
|
|
+ $itemsHtml .= '<li>' . htmlspecialchars($product['name']) . $sizeInfo . ' - Menge: ' . $item['quantity'] . '</li>';
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ $itemsHtml .= '</ul>';
|
|
|
|
|
+
|
|
|
|
|
+ $subject = 'Ihre Nachbestellung ist verfügbar';
|
|
|
|
|
+ $message = '
|
|
|
|
|
+ <html>
|
|
|
|
|
+ <head>
|
|
|
|
|
+ <meta charset="UTF-8">
|
|
|
|
|
+ </head>
|
|
|
|
|
+ <body style="font-family: Arial, sans-serif; line-height: 1.6; color: #333;">
|
|
|
|
|
+ <h2 style="color: #28a745;">Nachbestellung verfügbar</h2>
|
|
|
|
|
+ <p>Sehr geehrte/r ' . htmlspecialchars($reservation['customer_name']) . ',</p>
|
|
|
|
|
+ <p>Ihre komplette Nachbestellung ist jetzt verfügbar.</p>
|
|
|
|
|
+
|
|
|
|
|
+ <div style="background: #d4edda; border: 2px solid #c3e6cb; padding: 1.5rem; margin: 1.5rem 0; border-radius: 8px; text-align: center;">
|
|
|
|
|
+ <h3 style="margin-top: 0;">Ihr Abholcode:</h3>
|
|
|
|
|
+ <h2 style="font-size: 2rem; letter-spacing: 0.2rem; color: #155724; font-family: monospace;">' . htmlspecialchars($reservation['code']) . '</h2>
|
|
|
|
|
+ </div>
|
|
|
|
|
+
|
|
|
|
|
+ <h3>Verfügbare Artikel:</h3>
|
|
|
|
|
+ ' . $itemsHtml . '
|
|
|
|
|
+
|
|
|
|
|
+ <p>Bitte bringen Sie den Abholcode zur Abholung mit.</p>
|
|
|
|
|
+
|
|
|
|
|
+ <p>Mit freundlichen Grüßen<br>' . SITE_NAME . '</p>
|
|
|
|
|
+ </body>
|
|
|
|
|
+ </html>';
|
|
|
|
|
+
|
|
|
|
|
+ sendEmail($reservation['customer_email'], $subject, $message);
|
|
|
|
|
+}
|