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 912B

1234567891011121314151617181920212223242526272829303132333435
  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. public function saving(Invoice $invoice): void
  11. {
  12. if ($invoice->approved_at && $invoice->is_currently_overdue) {
  13. $invoice->status = InvoiceStatus::Overdue;
  14. }
  15. }
  16. /**
  17. * Handle the Invoice "deleted" event.
  18. */
  19. public function deleted(Invoice $invoice): void
  20. {
  21. DB::transaction(function () use ($invoice) {
  22. $invoice->lineItems()->each(function (DocumentLineItem $lineItem) {
  23. $lineItem->delete();
  24. });
  25. $invoice->transactions()->each(function (Transaction $transaction) {
  26. $transaction->delete();
  27. });
  28. });
  29. }
  30. }