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 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace App\Observers;
  3. use App\Enums\Accounting\AccountCategory;
  4. use App\Enums\Accounting\AccountType;
  5. use App\Enums\AccountStatus;
  6. use App\Models\Accounting\Account;
  7. use App\Models\Accounting\AccountSubtype;
  8. use App\Models\Banking\BankAccount;
  9. use App\Utilities\Accounting\AccountCode;
  10. class AccountObserver
  11. {
  12. public function creating(Account $account): void
  13. {
  14. $this->setCategoryAndType($account, true);
  15. }
  16. public function updating(Account $account): void
  17. {
  18. if ($account->isDirty('subtype_id')) {
  19. $this->setCategoryAndType($account, false);
  20. }
  21. }
  22. private function setCategoryAndType(Account $account, bool $isCreating): void
  23. {
  24. $subtype = $account->subtype_id ? AccountSubtype::find($account->subtype_id) : null;
  25. if ($subtype) {
  26. $account->category = $subtype->category;
  27. $account->type = $subtype->type;
  28. } elseif ($isCreating) {
  29. $account->category = AccountCategory::Asset;
  30. $account->type = AccountType::CurrentAsset;
  31. }
  32. }
  33. private function setFieldsForBankAccount(Account $account): void
  34. {
  35. $generatedAccountCode = AccountCode::generate($account->company_id, $account->subtype_id);
  36. $account->code = $generatedAccountCode;
  37. }
  38. /**
  39. * Handle the Account "created" event.
  40. */
  41. public function created(Account $account): void
  42. {
  43. // $bankAccount = $account->accountable;
  44. if (($account->accountable_type === BankAccount::class) && $account->code === null) {
  45. $this->setFieldsForBankAccount($account);
  46. }
  47. //$account->histories()->create([
  48. // 'company_id' => $account->company_id,
  49. // 'account_id' => $account->id,
  50. // 'type' => $account->type,
  51. // 'name' => $account->name,
  52. // 'number' => $account->number,
  53. // 'currency_code' => $account->currency_code,
  54. // 'opening_balance' => $account->opening_balance,
  55. // 'balance' => $account->balance,
  56. // 'exchange_rate' => $account->currency->rate,
  57. // 'status' => AccountStatus::Open,
  58. // 'actions' => ['account_created'],
  59. // 'enabled' => $account->enabled,
  60. // 'changed_by' => $account->created_by,
  61. //]);
  62. }
  63. /**
  64. * Handle the Account "updated" event.
  65. */
  66. public function updated(Account $account): void
  67. {
  68. //$actionsTaken = [];
  69. //foreach ($this->actions as $action => $attribute) {
  70. //if ($account->isDirty($attribute)) {
  71. //$actionsTaken[] = $action;
  72. // }
  73. //}
  74. //if (count($actionsTaken) > 0) {
  75. //$account->histories()->create([
  76. //'company_id' => $account->company_id,
  77. // 'account_id' => $account->id,
  78. // 'type' => $account->getOriginal('type'),
  79. // 'name' => $account->getOriginal('name'),
  80. // 'number' => $account->getOriginal('number'),
  81. // 'currency_code' => $account->getOriginal('currency_code'),
  82. // 'opening_balance' => $account->getRawOriginal('opening_balance'),
  83. // 'balance' => $account->getRawOriginal('balance'),
  84. // 'exchange_rate' => $account->currency->getRawOriginal('rate'),
  85. // 'status' => $account->getOriginal('status'),
  86. // 'actions' => $actionsTaken,
  87. // 'enabled' => $account->getOriginal('enabled'),
  88. // 'changed_by' => $account->updated_by,
  89. //]);
  90. //}
  91. }
  92. /**
  93. * Handle the Account "deleted" event.
  94. */
  95. public function deleted(Account $account): void
  96. {
  97. //
  98. }
  99. /**
  100. * Handle the Account "restored" event.
  101. */
  102. public function restored(Account $account): void
  103. {
  104. //
  105. }
  106. /**
  107. * Handle the Account "force deleted" event.
  108. */
  109. public function forceDeleted(Account $account): void
  110. {
  111. //
  112. }
  113. private function getDefaultChartForBankAccount(Account $account): Account
  114. {
  115. $defaultChartCategory = AccountCategory::Asset;
  116. $defaultChartType = AccountType::CurrentAsset;
  117. //if ($account->type->isCreditCard()) {
  118. //$defaultChartCategory = ChartCategory::Liability;
  119. //$defaultChartType = ChartType::CurrentLiability;
  120. //}
  121. $subTypeId = $this->getSubTypeId($account->company_id, $defaultChartType);
  122. $latestChartCode = Account::where('company_id', $account->company_id)
  123. ->where('category', $defaultChartCategory)
  124. ->where('type', $defaultChartType)
  125. ->max('code');
  126. $newChartCode = $latestChartCode ? ++$latestChartCode : '1000';
  127. return Account::create([
  128. 'company_id' => $account->company_id,
  129. 'category' => $defaultChartCategory,
  130. 'type' => $defaultChartType,
  131. 'subtype_id' => $subTypeId,
  132. 'code' => $newChartCode,
  133. 'name' => $account->name,
  134. 'currency_code' => $account->currency_code,
  135. 'description' => $account->description ?? $account->name,
  136. 'balance' => 0,
  137. 'active' => true,
  138. 'default' => false,
  139. 'created_by' => $account->created_by,
  140. 'updated_by' => $account->updated_by,
  141. ]);
  142. }
  143. private function getSubTypeId(int $companyId, AccountType $type): ?int
  144. {
  145. $subType = AccountSubtype::where('company_id', $companyId)
  146. ->where('name', 'Cash and Cash Equivalents')
  147. ->where('type', $type)
  148. ->first();
  149. if (! $subType) {
  150. $subType = AccountSubtype::where('company_id', $companyId)
  151. ->where('type', $type)
  152. ->first();
  153. }
  154. return $subType?->id;
  155. }
  156. private function updateChartBalance(Account $chart, mixed $amount): void
  157. {
  158. //$chart->balance += $amount;
  159. //$chart->save();
  160. }
  161. }