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.

AccountObserver.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Observers;
  3. use App\Enums\AccountStatus;
  4. use App\Models\Banking\Account;
  5. class AccountObserver
  6. {
  7. protected array $actions = [
  8. 'exchange_rate_changed' => 'balance',
  9. 'currency_changed' => 'currency_code',
  10. 'status_changed' => 'status',
  11. 'default_account_changed' => 'enabled',
  12. 'type_changed' => 'type',
  13. 'name_changed' => 'name',
  14. 'number_changed' => 'number',
  15. ];
  16. public function creating(Account $account): void
  17. {
  18. $account->balance = $account->opening_balance;
  19. }
  20. /**
  21. * Handle the Account "created" event.
  22. */
  23. public function created(Account $account): void
  24. {
  25. $account->histories()->create([
  26. 'company_id' => $account->company_id,
  27. 'account_id' => $account->id,
  28. 'type' => $account->type,
  29. 'name' => $account->name,
  30. 'number' => $account->number,
  31. 'currency_code' => $account->currency_code,
  32. 'opening_balance' => $account->opening_balance,
  33. 'balance' => $account->balance,
  34. 'exchange_rate' => $account->currency->rate,
  35. 'status' => AccountStatus::Open,
  36. 'actions' => ['account_created'],
  37. 'enabled' => $account->enabled,
  38. 'changed_by' => $account->created_by,
  39. ]);
  40. }
  41. /**
  42. * Handle the Account "updated" event.
  43. */
  44. public function updated(Account $account): void
  45. {
  46. $actionsTaken = [];
  47. foreach ($this->actions as $action => $attribute) {
  48. if ($account->isDirty($attribute)) {
  49. $actionsTaken[] = $action;
  50. }
  51. }
  52. if (count($actionsTaken) > 0) {
  53. $account->histories()->create([
  54. 'company_id' => $account->company_id,
  55. 'account_id' => $account->id,
  56. 'type' => $account->getOriginal('type'),
  57. 'name' => $account->getOriginal('name'),
  58. 'number' => $account->getOriginal('number'),
  59. 'currency_code' => $account->getOriginal('currency_code'),
  60. 'opening_balance' => $account->getRawOriginal('opening_balance'),
  61. 'balance' => $account->getRawOriginal('balance'),
  62. 'exchange_rate' => $account->currency->getRawOriginal('rate'),
  63. 'status' => $account->getOriginal('status'),
  64. 'actions' => $actionsTaken,
  65. 'enabled' => $account->getOriginal('enabled'),
  66. 'changed_by' => $account->updated_by,
  67. ]);
  68. }
  69. }
  70. /**
  71. * Handle the Account "deleted" event.
  72. */
  73. public function deleted(Account $account): void
  74. {
  75. //
  76. }
  77. /**
  78. * Handle the Account "restored" event.
  79. */
  80. public function restored(Account $account): void
  81. {
  82. //
  83. }
  84. /**
  85. * Handle the Account "force deleted" event.
  86. */
  87. public function forceDeleted(Account $account): void
  88. {
  89. //
  90. }
  91. }