您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

InvoiceObserver.php 1.7KB

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