Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

BankAccountObserver.php 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Observers;
  3. use App\Enums\Accounting\AccountType;
  4. use App\Models\Accounting\AccountSubtype;
  5. use App\Models\Accounting\Transaction;
  6. use App\Models\Banking\BankAccount;
  7. use Illuminate\Support\Facades\DB;
  8. class BankAccountObserver
  9. {
  10. /**
  11. * Handle the BankAccount "created" event.
  12. */
  13. public function created(BankAccount $bankAccount): void
  14. {
  15. //
  16. }
  17. /**
  18. * Handle the BankAccount "creating" event.
  19. */
  20. public function creating(BankAccount $bankAccount): void
  21. {
  22. //
  23. }
  24. /**
  25. * Get the default bank account subtype.
  26. */
  27. protected function getDefaultBankAccountSubtype(int $companyId, AccountType $type)
  28. {
  29. $subType = AccountSubtype::where('company_id', $companyId)
  30. ->where('name', 'Cash and Cash Equivalents')
  31. ->where('type', $type)
  32. ->first();
  33. if (! $subType) {
  34. $subType = AccountSubtype::where('company_id', $companyId)
  35. ->where('type', $type)
  36. ->first();
  37. }
  38. return $subType?->id;
  39. }
  40. /**
  41. * Handle the BankAccount "updated" event.
  42. */
  43. public function updated(BankAccount $bankAccount): void
  44. {
  45. //
  46. }
  47. /**
  48. * Handle the BankAccount "deleting" event.
  49. */
  50. public function deleting(BankAccount $bankAccount): void
  51. {
  52. DB::transaction(function () use ($bankAccount) {
  53. $account = $bankAccount->account;
  54. $connectedBankAccount = $bankAccount->connectedBankAccount;
  55. if ($account) {
  56. $bankAccount->transactions()->each(fn (Transaction $transaction) => $transaction->delete());
  57. $account->delete();
  58. }
  59. if ($connectedBankAccount) {
  60. $connectedBankAccount->delete();
  61. }
  62. });
  63. }
  64. /**
  65. * Handle the BankAccount "deleted" event.
  66. */
  67. public function deleted(BankAccount $bankAccount): void
  68. {
  69. //
  70. }
  71. /**
  72. * Handle the BankAccount "restored" event.
  73. */
  74. public function restored(BankAccount $bankAccount): void
  75. {
  76. //
  77. }
  78. /**
  79. * Handle the BankAccount "force deleted" event.
  80. */
  81. public function forceDeleted(BankAccount $bankAccount): void
  82. {
  83. //
  84. }
  85. }