bookings(fn (Builder $query) => $query ->where('status', BookingStatus::Confirmed) ->whereNull('deposit_reminded_at') ->whereDate('deposit_due_on', '<', $today)) ->filter(fn (Booking $booking): bool => $booking->openDeposit() > 0); $depositTasks = $this->createTasks($depositOverdue, BookingTaskKind::DepositReminder, fn (Booking $booking) => $booking->deposit_due_on); $balanceRequest = $this->bookings(fn (Builder $query) => $query ->whereIn('status', $unpaid) ->whereNull('balance_requested_at') ->whereDate('balance_due_on', '>=', $today) ->whereDate('balance_due_on', '<=', $today->copy()->addDays(self::BALANCE_REQUEST_DAYS_BEFORE))) ->filter(fn (Booking $booking): bool => ! $booking->requiresFullPayment() && $booking->openAmount() > 0 && $booking->status === BookingStatus::DepositPaid); $requestTasks = $this->createTasks($balanceRequest, BookingTaskKind::BalanceRequest, fn (Booking $booking) => $booking->balance_due_on); $balanceOverdue = $this->bookings(fn (Builder $query) => $query ->whereIn('status', $unpaid) ->whereNull('balance_reminded_at') ->whereDate('balance_due_on', '<', $today)) ->filter(fn (Booking $booking): bool => $booking->openAmount() > 0 && ($booking->status === BookingStatus::DepositPaid || $booking->requiresFullPayment())); $overdueTasks = $this->createTasks($balanceOverdue, BookingTaskKind::BalanceReminder, fn (Booking $booking) => $booking->balance_due_on); $arrivals = $this->bookings(fn (Builder $query) => $query ->whereIn('status', BookingStatus::occupying()) ->whereNull('arrival_info_sent_at') ->whereDate('arrival', '>=', $today) ->whereDate('arrival', '<=', $today->copy()->addDays(self::ARRIVAL_INFO_MAX_DAYS_BEFORE))) ->filter(fn (Booking $booking): bool => $booking->process()->arrivalInfoSender() === Responsibility::Landlord && $booking->arrival->lessThanOrEqualTo($today->copy()->addDays($booking->process()->arrivalInfoDaysBefore())) // Decision O4: an open portal report of a cancellation or change holds the message back until clarified. && OutwardSuspension::reason($booking) === null); foreach ($arrivals as $booking) { Mail::to($booking->guest->email, $booking->guest->full_name)->send(new ArrivalInformation($booking)); $booking->update(['arrival_info_sent_at' => now()]); } // Package 9: prepare the coming stays, close the stays whose departure has passed (open money stays as follow-up). $prepared = app(PrepareStays::class)->handle(); $completed = app(CloseStay::class)->due(); return [ 'deposit_overdue' => $depositTasks, 'balance_request' => $requestTasks, 'balance_overdue' => $overdueTasks, 'arrival_info' => $arrivals->count(), 'completed' => $completed, 'prepared' => $prepared['arrival_details'] + $prepared['stay_tasks'], ]; } /** * @param callable(Builder): Builder $constraints * @return Collection */ private function bookings(callable $constraints): Collection { return $constraints(Booking::query()->with(['guest', 'property', 'payments', 'importChannel', 'originChannel'])) ->whereHas('guest', fn (Builder $query) => $query->whereNull('anonymized_at')->whereNotNull('email')->where('email', '!=', '')) ->get() ->toBase(); } /** * @param Collection $bookings * @param callable(Booking): mixed $dueOn * @return int the number of tasks created now */ private function createTasks(Collection $bookings, BookingTaskKind $kind, callable $dueOn): int { $created = 0; foreach ($bookings->filter(fn (Booking $booking): bool => $booking->wantsPaymentTasks()) as $booking) { $task = BookingTask::query()->firstOrCreate( ['booking_id' => $booking->id, 'kind' => $kind], ['status' => BookingTaskStatus::Open, 'due_on' => $dueOn($booking)], ); $created += $task->wasRecentlyCreated ? 1 : 0; } return $created; } }