tablesList; if (empty($tables)) { return $template_name; } // Get the shift/event service $service = alexr_get_service($this->shift_event_id); if (!$service || $service->availability_type !== 'tables_schedule') { return $template_name; } // Get date and extract day of week $date = $this->date; $dayOfWeek = strtolower(date('D', strtotime($date))); // For simplicity, check only the first table if multiple are assigned $tableId = $tables[0]; $time = $this->time; // Check if table has custom schedule for this day $tableSchedules = $service->list_of_tables_with_schedule['tableSchedules'] ?? []; if (!isset($tableSchedules[$tableId]) || !isset($tableSchedules[$tableId][$dayOfWeek]) || $tableSchedules[$tableId][$dayOfWeek]['type'] !== 'custom') { return $template_name; } // Get the schedule for this table on this day $schedule = $tableSchedules[$tableId][$dayOfWeek]['schedule'] ?? []; // Find current slot and check if it's not the last one $hasNextSlot = false; $currentSlotEndTime = null; foreach ($schedule as $slot) { if (in_array($time, $slot['reserve'])) { $currentSlotEndTime = $slot['to']; // Check if there's another slot after this one on the same day foreach ($schedule as $nextSlot) { if ($nextSlot['from'] == $currentSlotEndTime) { $hasNextSlot = true; break; } } break; } } // If this is not the last slot, use the time-constrained template if ($hasNextSlot && $currentSlotEndTime) { // Determine which custom template to use based on the booking status $templateType = ''; if ($template_name === 'booking_pending') { $templateType = 'pending'; } else if ($template_name === 'booking_booked') { $templateType = 'confirmed'; } // Get the custom email template $result = $this->getCustomEmailTemplate($templateType, $lang); return $result ? $result : $template_name; } return $template_name; } /** * Get the custom email template for time-constrained bookings * * @param string $templateType Template type ('pending' or 'confirmed') * @param string|null $lang Language code * @return array Array with subject and content */ protected function getCustomEmailTemplate($templateType, $lang = null) { if (!$lang) { $lang = $this->restaurant->language; } // Get service configuration to find the custom email template IDs $service = alexr_get_service($this->shift_event_id); if (!$service || !isset($service->list_of_tables_with_schedule['customEmails'])) { return false; } $customEmails = $service->list_of_tables_with_schedule['customEmails'] ?? []; $templateId = $customEmails[$templateType] ?? null; if (!$templateId) { return false; } // Find the EmailCustom by ID $emailTemplate = \Alexr\Settings\EmailCustom::where('id', $templateId) ->where('restaurant_id', $this->restaurant_id) ->first(); if (!$emailTemplate || $emailTemplate->email_active != 1) { return false; } // Get the template content for the specified language, fallback to 'en' $emailCustom = $emailTemplate->email_custom ?? []; // Get subject and content for the specified language or fallback to English $subject_b64 = $emailCustom['subject_'.$lang] ?? $emailCustom['subject_en'] ?? ''; $content_b64 = $emailCustom['content_'.$lang] ?? $emailCustom['content_en'] ?? ''; if (empty($subject_b64) || empty($content_b64)) { return false; } return [ 'subject' => base64_decode($subject_b64), 'content' => base64_decode($content_b64) ]; } }