You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InvoiceObserver.php 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Observers;
  3. use App\Enums\Accounting\InvoiceStatus;
  4. use App\Models\Accounting\DocumentLineItem;
  5. use App\Models\Accounting\Invoice;
  6. use App\Models\Accounting\Transaction;
  7. use Illuminate\Support\Facades\DB;
  8. class InvoiceObserver
  9. {
  10. /**
  11. * Handle the Invoice "created" event.
  12. */
  13. public function created(Invoice $invoice): void
  14. {
  15. //
  16. }
  17. /**
  18. * Handle the Invoice "updated" event.
  19. */
  20. public function updated(Invoice $invoice): void
  21. {
  22. //
  23. }
  24. public function deleting(Invoice $invoice): void
  25. {
  26. //
  27. }
  28. public function saving(Invoice $invoice): void
  29. {
  30. if ($invoice->is_currently_overdue) {
  31. $invoice->status = InvoiceStatus::Overdue;
  32. }
  33. }
  34. /**
  35. * Handle the Invoice "deleted" event.
  36. */
  37. public function deleted(Invoice $invoice): void
  38. {
  39. DB::transaction(function () use ($invoice) {
  40. $invoice->lineItems()->each(function (DocumentLineItem $lineItem) {
  41. $lineItem->delete();
  42. });
  43. $invoice->transactions()->each(function (Transaction $transaction) {
  44. $transaction->delete();
  45. });
  46. });
  47. }
  48. /**
  49. * Handle the Invoice "restored" event.
  50. */
  51. public function restored(Invoice $invoice): void
  52. {
  53. //
  54. }
  55. /**
  56. * Handle the Invoice "force deleted" event.
  57. */
  58. public function forceDeleted(Invoice $invoice): void
  59. {
  60. //
  61. }
  62. }