'system']) { if ($lang === null) { $lang = $this->language; } $booking = $this; // 1. Check reconfirmation really exists $restaurant_id = $booking->restaurant_id; $reconfirmation = WhatsappReconfirmation::where('restaurant_id', $restaurant_id) ->where('id', $reconfirmation_id) ->first(); if (!$reconfirmation) { //ray('WhatsApp Reconfirmation not found: ' . $reconfirmation_id); return false; } // 2. Check reconfirmation is active if (!$reconfirmation->isEnabled()) { //ray('WhatsApp Reconfirmation is not active'); return false; } // 3. Check reconfirmation is for shifts or events $for_shifts = $reconfirmation->isForShifts(); $for_events = $reconfirmation->isForEvents(); // 4. Check service exists and is used by this reconfirmation $service_id = $booking->shift_event_id; $service = Shift::where('restaurant_id', $restaurant_id) ->where('id', $service_id) ->first(); if ($service) { if (!$for_shifts) { //ray('Reconfirmation 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('Reconfirmation not configured for events'); return false; } } else { //ray('Service not found'); return false; // Service not exists } } // 5. Get template configuration for the language $templateConfig = $reconfirmation->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 reconfirmation'); 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_reconfirmation', $reconfirmation->id ); if ($result['success']) { //ray('WhatsApp Reconfirmation sent successfully', $result); } else { //ray('Failed to send WhatsApp Reconfirmation', $result); } return $result; } }