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.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. if ($invoice->wasChanged('status')) {
  23. $invoice->statusHistories()->create([
  24. 'company_id' => $invoice->company_id,
  25. 'old_status' => $invoice->getOriginal('status'),
  26. 'new_status' => $invoice->status,
  27. 'changed_by' => $invoice->updated_by,
  28. ]);
  29. }
  30. }
  31. public function deleting(Invoice $invoice): void
  32. {
  33. //
  34. }
  35. public function saving(Invoice $invoice): void
  36. {
  37. if ($invoice->is_currently_overdue) {
  38. $invoice->status = InvoiceStatus::Overdue;
  39. }
  40. }
  41. /**
  42. * Handle the Invoice "deleted" event.
  43. */
  44. public function deleted(Invoice $invoice): void
  45. {
  46. DB::transaction(function () use ($invoice) {
  47. $invoice->lineItems()->each(function (DocumentLineItem $lineItem) {
  48. $lineItem->delete();
  49. });
  50. $invoice->transactions()->each(function (Transaction $transaction) {
  51. $transaction->delete();
  52. });
  53. });
  54. }
  55. /**
  56. * Handle the Invoice "restored" event.
  57. */
  58. public function restored(Invoice $invoice): void
  59. {
  60. //
  61. }
  62. /**
  63. * Handle the Invoice "force deleted" event.
  64. */
  65. public function forceDeleted(Invoice $invoice): void
  66. {
  67. //
  68. }
  69. }