120 lines
5.7 KiB
PHP
120 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace App\Actions;
|
|
|
|
use App\Enums\BookingStatus;
|
|
use App\Enums\BookingTaskKind;
|
|
use App\Enums\BookingTaskStatus;
|
|
use App\Enums\Responsibility;
|
|
use App\Mail\ArrivalInformation;
|
|
use App\Models\Booking;
|
|
use App\Models\BookingTask;
|
|
use App\Process\OutwardSuspension;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
/**
|
|
* Daily booking workflow. Payments are never chased automatically: due payments become tasks for the landlord
|
|
* (BookingTask, once per booking and kind) who checks the payment and sends the reminder on a click
|
|
* ({@see SendPaymentReminder}). Only the arrival information goes out by itself, once per booking, the configured
|
|
* number of days before arrival. Preparation of the stays and their closing after departure ({@see PrepareStays},
|
|
* {@see CloseStay}).
|
|
*/
|
|
class SendBookingReminders
|
|
{
|
|
private const BALANCE_REQUEST_DAYS_BEFORE = 7;
|
|
|
|
/** Upper bound of the per-property setting; the property decides the actual day. */
|
|
private const ARRIVAL_INFO_MAX_DAYS_BEFORE = 14;
|
|
|
|
/**
|
|
* @return array{deposit_overdue: int, balance_request: int, balance_overdue: int, arrival_info: int, completed: int, prepared: int}
|
|
*/
|
|
public function handle(): array
|
|
{
|
|
$today = today();
|
|
$unpaid = [BookingStatus::Confirmed, BookingStatus::DepositPaid];
|
|
|
|
$depositOverdue = $this->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()->guestMessages() === 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<Booking>): Builder<Booking> $constraints
|
|
* @return Collection<int, Booking>
|
|
*/
|
|
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<int, Booking> $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;
|
|
}
|
|
}
|