} */ public function match(InboundMessage $message): array { $sender = mb_strtolower(trim((string) $message->from_address)); $text = $message->subject."\n".$message->body; $references = array_values(array_filter([$message->in_reply_to, ...((array) $message->references)])); if ($references !== []) { $replied = BookingMessage::query()->whereIn('message_id', $references)->whereNotNull('booking_id')->latest('id')->first()?->booking; if ($replied !== null) { return ['booking' => $replied, 'reason' => 'reply', 'hints' => []]; } } $hints = []; if (preg_match_all('/\b(20\d{2}-\d{4})\b/', $text, $numbers)) { foreach (Booking::query()->whereIn('reference', array_unique($numbers[1]))->with('guest')->get() as $booking) { if ($sender !== '' && mb_strtolower((string) $booking->guest?->email) === $sender) { return ['booking' => $booking, 'reason' => 'reference_and_sender', 'hints' => []]; } $hints[] = ['booking_id' => $booking->id, 'reference' => $booking->reference, 'why' => 'Vorgangsnummer genannt, aber anderer Absender']; } } if ($sender === '') { return ['booking' => null, 'reason' => null, 'hints' => $hints]; } $open = Booking::query() ->whereHas('guest', fn ($guest) => $guest->where('email', $sender)) ->whereNotIn('status', [BookingStatus::Cancelled, BookingStatus::Declined, BookingStatus::Expired, BookingStatus::Completed]) ->whereDate('departure', '>=', CarbonImmutable::today()) ->with('property') ->get(); $dates = $this->dates($text); $singleProperty = Property::query()->count() === 1; $candidates = $open->filter(fn (Booking $booking): bool => in_array($booking->arrival->toDateString(), $dates, true) && in_array($booking->departure->toDateString(), $dates, true) && ($singleProperty || mb_stripos($text, (string) $booking->property?->name) !== false)); if ($candidates->count() === 1) { return ['booking' => $candidates->first(), 'reason' => 'sender_property_dates', 'hints' => []]; } foreach ($open as $booking) { $hints[] = ['booking_id' => $booking->id, 'reference' => $booking->reference, 'why' => 'offener Vorgang dieses Absenders ('.$booking->arrival->format('d.m.').'–'.$booking->departure->format('d.m.Y').')']; } return ['booking' => null, 'reason' => null, 'hints' => array_values(array_unique($hints, SORT_REGULAR))]; } /** * Dates written in the message (d.m.yyyy, d.m.yy, yyyy-mm-dd). * * @return list Y-m-d */ public function dates(string $text): array { $dates = []; if (preg_match_all('/\b(\d{1,2})\.\s?(\d{1,2})\.\s?(\d{4}|\d{2})\b/', $text, $german, PREG_SET_ORDER)) { foreach ($german as [, $day, $month, $year]) { $year = strlen($year) === 2 ? '20'.$year : $year; if (checkdate((int) $month, (int) $day, (int) $year)) { $dates[] = sprintf('%04d-%02d-%02d', $year, $month, $day); } } } if (preg_match_all('/\b(20\d{2})-(\d{2})-(\d{2})\b/', $text, $iso, PREG_SET_ORDER)) { foreach ($iso as [, $year, $month, $day]) { if (checkdate((int) $month, (int) $day, (int) $year)) { $dates[] = sprintf('%04d-%02d-%02d', $year, $month, $day); } } } return array_values(array_unique($dates)); } }