101 lines
4.2 KiB
PHP
101 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Actions;
|
|
|
|
use App\Enums\BookingStatus;
|
|
use App\Mail\ArrivalInformation;
|
|
use App\Mail\PaymentReminder;
|
|
use App\Models\Booking;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
/**
|
|
* Daily booking workflow: payment requests and reminders, arrival information, completion after departure.
|
|
* Every message is sent at most once per booking.
|
|
*/
|
|
class SendBookingReminders
|
|
{
|
|
private const BALANCE_REQUEST_DAYS_BEFORE = 7;
|
|
|
|
private const ARRIVAL_INFO_DAYS_BEFORE = 7;
|
|
|
|
/**
|
|
* @return array{deposit_overdue: int, balance_request: int, balance_overdue: int, arrival_info: int, completed: 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);
|
|
$this->remind($depositOverdue, PaymentReminder::DEPOSIT_OVERDUE, 'deposit_reminded_at');
|
|
|
|
$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);
|
|
$this->remind($balanceRequest, PaymentReminder::BALANCE_REQUEST, 'balance_requested_at');
|
|
|
|
$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()));
|
|
$this->remind($balanceOverdue, PaymentReminder::BALANCE_OVERDUE, 'balance_reminded_at');
|
|
|
|
$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_DAYS_BEFORE)));
|
|
foreach ($arrivals as $booking) {
|
|
Mail::to($booking->guest->email, $booking->guest->full_name)->send(new ArrivalInformation($booking));
|
|
$booking->update(['arrival_info_sent_at' => now()]);
|
|
}
|
|
|
|
$completed = Booking::query()
|
|
->where('status', BookingStatus::Paid)
|
|
->whereDate('departure', '<', $today)
|
|
->get();
|
|
$completed->each->update(['status' => BookingStatus::Completed]);
|
|
|
|
return [
|
|
'deposit_overdue' => $depositOverdue->count(),
|
|
'balance_request' => $balanceRequest->count(),
|
|
'balance_overdue' => $balanceOverdue->count(),
|
|
'arrival_info' => $arrivals->count(),
|
|
'completed' => $completed->count(),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @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']))
|
|
->whereHas('guest', fn (Builder $query) => $query->whereNull('anonymized_at')->whereNotNull('email')->where('email', '!=', ''))
|
|
->get()
|
|
->toBase();
|
|
}
|
|
|
|
/**
|
|
* @param Collection<int, Booking> $bookings
|
|
*/
|
|
private function remind(Collection $bookings, string $kind, string $sentAtColumn): void
|
|
{
|
|
foreach ($bookings as $booking) {
|
|
Mail::to($booking->guest->email, $booking->guest->full_name)->send(new PaymentReminder($booking, $kind));
|
|
$booking->update([$sentAtColumn => now()]);
|
|
}
|
|
}
|
|
}
|