'system']) { if ($lang === null) { $lang = $this->language; } $booking = $this; // 1. Check reminder really exists $restaurant_id = $booking->restaurant_id; $reminder = WhatsappReminder::where('restaurant_id', $restaurant_id) ->where('id', $reminder_id) ->first(); if (!$reminder) { //ray('WhatsApp Reminder not found: ' . $reminder_id); return false; } // 2. Check reminder is active if (!$reminder->isEnabled()) { //ray('WhatsApp Reminder is not active'); return false; } // 3. Check reminder is for shifts or events $for_shifts = $reminder->isForShifts(); $for_events = $reminder->isForEvents(); // 4. Check service exists and is used by this reminder $service_id = $booking->shift_event_id; $service = Shift::where('restaurant_id', $restaurant_id) ->where('id', $service_id) ->first(); if ($service) { if (!$for_shifts) { //ray('Reminder not configured for shifts'); return false; } } else { $service = Event::where('restaurant_id', $restaurant_id) ->where('id', $service_id) ->first(); if ($service) { if (!$for_events) { //ray('Reminder not configured for events'); return false; } } else { //ray('Service not found'); return false; // Service not exists } } // 5. Get template configuration for the language $templateConfig = $reminder->getTemplateConfig($lang); if (!$templateConfig || empty($templateConfig['template_sid'])) { //ray('No WhatsApp template configured for language: ' . $lang); return false; } $templateSid = $templateConfig['template_sid']; $templateName = $templateConfig['template_name'] ?? ''; $fieldMapping = $templateConfig['field_mapping'] ?? []; // 6. Map booking fields to template variables $variables = $this->mapBookingFieldsToVariables($booking, $fieldMapping); // 7. Get customer phone $customerPhone = $this->getCustomerPhone($booking); if (empty($customerPhone)) { //ray('Customer phone is empty, cannot send WhatsApp reminder'); return false; } // 7b. Check if customer has WhatsApp if ($booking->customer_id) { $customer = Customer::find($booking->customer_id); if ($customer && !$customer->has_whatsapp) { return false; } } // 8. Send the WhatsApp template using WhatsappManager $manager = new WhatsappManager($booking->restaurant_id); $result = $manager->sendTemplate( $customerPhone, $templateSid, $variables, $booking->id, $booking->customer_id, $templateName, $senderInfo ); // 9. Store the notification in BookingNotification $this->storeWhatsappSent( $customerPhone, $templateName, $templateSid, $variables, $result, 'whatsapp_reminder', $reminder->id ); if ($result['success']) { //ray('WhatsApp Reminder sent successfully', $result); } else { //ray('Failed to send WhatsApp Reminder', $result); } return $result; } }