topnotch_website/app/Observers/InvoiceObserver.php

43 lines
966 B
PHP
Raw Normal View History

2024-12-10 15:28:14 -08:00
<?php
namespace App\Observers;
use App\Models\Invoice;
2025-01-16 17:37:32 -05:00
use App\Models\TaxRate;
2024-12-10 15:28:14 -08:00
class InvoiceObserver
{
/**
2025-01-16 17:37:32 -05:00
* Handle the Invoice "creating" event.
2024-12-10 15:28:14 -08:00
*/
2025-01-16 17:37:32 -05:00
public function creating(Invoice $invoice): void
2024-12-10 15:28:14 -08:00
{
2025-01-16 17:37:32 -05:00
$invoice->pst_rate = TaxRate::where('name', 'PST')->value('value') ?? 0;
$invoice->gst_rate = TaxRate::where('name', 'GST')->value('value') ?? 0;
2025-01-21 21:28:41 -05:00
$invoice->hst_rate = TaxRate::where('name', 'HST')->value('value') ?? 0;
2024-12-10 15:28:14 -08:00
}
/**
2025-01-16 17:37:32 -05:00
* Handle the Invoice "created" event.
2024-12-10 15:28:14 -08:00
*/
2025-01-16 17:37:32 -05:00
public function created(Invoice $invoice): void
2024-12-10 15:28:14 -08:00
{
2025-01-24 21:37:05 -05:00
2025-01-16 17:37:32 -05:00
$invoice->internal_id = 'INV4'.str_pad($invoice->id, 5, '0', STR_PAD_LEFT);
$invoice->saveQuietly();
2024-12-10 15:28:14 -08:00
2025-01-16 17:37:32 -05:00
$invoice->calculateTotals();
2025-01-24 21:37:05 -05:00
2024-12-10 15:28:14 -08:00
}
2025-01-24 21:37:05 -05:00
public function saved(Invoice $invoice) {}
2024-12-10 15:28:14 -08:00
/**
2025-01-16 17:37:32 -05:00
* Handle the Invoice "updated" event.
2024-12-10 15:28:14 -08:00
*/
2025-01-16 17:37:32 -05:00
public function updated(Invoice $invoice): void
2024-12-10 15:28:14 -08:00
{
2025-01-16 17:37:32 -05:00
$invoice->calculateTotals();
2024-12-10 15:28:14 -08:00
}
}