-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command sending a reminder for bookings which are not paid until …
…the payment deadline, refactor booking confirmation (#76) * Add command sending a reminder for bookings which are not paid until the payment deadline * Add Unit tests
- Loading branch information
1 parent
3063a9d
commit 94d4db9
Showing
11 changed files
with
297 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Models\Booking; | ||
use App\Models\BookingOption; | ||
use App\Notifications\PaymentReminderNotification; | ||
use Carbon\Carbon; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Support\Facades\Log; | ||
use Illuminate\Support\Facades\Notification; | ||
|
||
class SendPaymentRemindersCommand extends Command | ||
{ | ||
protected $signature = 'app:send-payment-reminders | ||
{--dry-run : If set the reminders are only listed, but not actually sent.}'; | ||
protected $description = 'Send payment reminders for overdue bookings'; | ||
|
||
private bool $isDryRun = false; | ||
private string $messageBuffer = ''; | ||
|
||
public function handle(): int | ||
{ | ||
/** @var Collection<BookingOption> $bookingOptions */ | ||
$bookingOptions = BookingOption::query() | ||
->whereNotNull('payment_due_days') | ||
->whereHas( | ||
'bookings', | ||
fn (Builder $bookings) => $bookings | ||
->whereNotNull('price') | ||
->whereNull('paid_at') | ||
->whereNull('deleted_at') | ||
) | ||
->with([ | ||
'event', | ||
]) | ||
->orderBy('name') | ||
->get(); | ||
|
||
if ($bookingOptions->isEmpty()) { | ||
$this->info('There are no booking options with unpaid bookings to check.'); | ||
return self::SUCCESS; | ||
} | ||
|
||
$this->isDryRun = $this->option('dry-run'); | ||
|
||
$bookingOptions = $bookingOptions->sortBy([ | ||
'events.name', | ||
'name', | ||
]); | ||
foreach ($bookingOptions as $bookingOption) { | ||
$this->components->task( | ||
$bookingOption->event->name . ', ' . $bookingOption->name, | ||
fn () => $this->processBookingsForOption($bookingOption) | ||
); | ||
} | ||
$this->info($this->messageBuffer); | ||
|
||
return self::SUCCESS; | ||
} | ||
|
||
private function processBookingsForOption(BookingOption $bookingOption): bool | ||
{ | ||
// Calculate which bookings have reached the due date already. | ||
$bookedBefore = Carbon::today()->endOfDay()->subWeekdays($bookingOption->payment_due_days); | ||
|
||
/** @var Collection<Booking> $bookingsReadyForReminder */ | ||
$bookingsReadyForReminder = $bookingOption->bookings() | ||
->whereNotNull('price') | ||
->whereNull('paid_at') | ||
->where('booked_at', '<=', $bookedBefore) | ||
->orderBy('booked_at') | ||
->get(); | ||
if ($bookingsReadyForReminder->isEmpty()) { | ||
$this->info('No reminders to send.'); | ||
return true; | ||
} | ||
|
||
$this->output->progressStart($bookingsReadyForReminder->count()); | ||
|
||
foreach ($bookingsReadyForReminder as $booking) { | ||
if ($this->isDryRun) { | ||
$message = "Reminder to {$booking->email} for booking {$booking->id} not sent because it's a dry run."; | ||
} else { | ||
Notification::route('mail', $booking->email) | ||
->notify(new PaymentReminderNotification($booking)); | ||
$message = "Sent reminder to {$booking->email} for booking {$booking->id}."; | ||
} | ||
Log::info($message); | ||
$this->messageBuffer .= $message . PHP_EOL; | ||
$this->output->progressAdvance(); | ||
} | ||
|
||
$this->output->progressFinish(); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace App\Models\Traits; | ||
|
||
use Illuminate\Database\Eloquent\Casts\Attribute; | ||
|
||
/** | ||
* @property string $first_name | ||
* @property string $last_name | ||
* | ||
* @property-read string greeting {@see self::greeting()} | ||
* @property-read string $name {@see self::name()} | ||
*/ | ||
trait HasFullName | ||
{ | ||
public function greeting(): Attribute | ||
{ | ||
return new Attribute(fn () => __('Hello :name,', ['name' => $this->name])); | ||
} | ||
|
||
public function name(): Attribute | ||
{ | ||
return new Attribute(fn () => $this->first_name . ' ' . $this->last_name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace App\Notifications; | ||
|
||
use App\Models\Booking; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Notifications\Notification; | ||
|
||
class PaymentReminderNotification extends Notification implements ShouldQueue | ||
{ | ||
use Queueable; | ||
|
||
public function __construct(private readonly Booking $booking) | ||
{ | ||
} | ||
|
||
public function toMail($notifiable) | ||
{ | ||
return $this->booking->prepareMailMessage() | ||
->subject( | ||
config('app.name') | ||
. ': ' | ||
. __('Pending payment for booking no. :id', [ | ||
'id' => $this->booking->id, | ||
]) | ||
) | ||
->line(__('we have received your booking for :name dated :date, but have not yet been able to confirm receipt of payment.', [ | ||
'name' => $this->booking->name, | ||
'date' => formatDate($this->booking->booked_at), | ||
])) | ||
->line(__('Please transfer :price to the following bank account as soon as possible:', [ | ||
'price' => formatDecimal($this->booking->price) . ' €', | ||
])) | ||
->lines($this->booking->bookingOption->event->organization->bank_account_lines); | ||
} | ||
|
||
public function via($notifiable) | ||
{ | ||
return ['mail']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.