Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

AccountObserver.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. $account->save();
  38. }
  39. /**
  40. * Handle the Account "created" event.
  41. */
  42. public function created(Account $account): void
  43. {
  44. // $bankAccount = $account->accountable;
  45. info('Observer triggered for Account: ', $account->toArray());
  46. if (($account->accountable_type === BankAccount::class) && $account->code === null) {
  47. info('Setting fields for Bank Account');
  48. $this->setFieldsForBankAccount($account);
  49. }
  50. //$account->histories()->create([
  51. // 'company_id' => $account->company_id,
  52. // 'account_id' => $account->id,
  53. // 'type' => $account->type,
  54. // 'name' => $account->name,
  55. // 'number' => $account->number,
  56. // 'currency_code' => $account->currency_code,
  57. // 'opening_balance' => $account->opening_balance,
  58. // 'balance' => $account->balance,
  59. // 'exchange_rate' => $account->currency->rate,
  60. // 'status' => AccountStatus::Open,
  61. // 'actions' => ['account_created'],
  62. // 'enabled' => $account->enabled,
  63. // 'changed_by' => $account->created_by,
  64. //]);
  65. }
  66. /**
  67. * Handle the Account "updated" event.
  68. */
  69. public function updated(Account $account): void
  70. {
  71. //$actionsTaken = [];
  72. //foreach ($this->actions as $action => $attribute) {
  73. //if ($account->isDirty($attribute)) {
  74. //$actionsTaken[] = $action;
  75. // }
  76. //}
  77. //if (count($actionsTaken) > 0) {
  78. //$account->histories()->create([
  79. //'company_id' => $account->company_id,
  80. // 'account_id' => $account->id,
  81. // 'type' => $account->getOriginal('type'),
  82. // 'name' => $account->getOriginal('name'),
  83. // 'number' => $account->getOriginal('number'),
  84. // 'currency_code' => $account->getOriginal('currency_code'),
  85. // 'opening_balance' => $account->getRawOriginal('opening_balance'),
  86. // 'balance' => $account->getRawOriginal('balance'),
  87. // 'exchange_rate' => $account->currency->getRawOriginal('rate'),
  88. // 'status' => $account->getOriginal('status'),
  89. // 'actions' => $actionsTaken,
  90. // 'enabled' => $account->getOriginal('enabled'),
  91. // 'changed_by' => $account->updated_by,
  92. //]);
  93. //}
  94. }
  95. /**
  96. * Handle the Account "deleted" event.
  97. */
  98. public function deleted(Account $account): void
  99. {
  100. //
  101. }
  102. /**
  103. * Handle the Account "restored" event.
  104. */
  105. public function restored(Account $account): void
  106. {
  107. //
  108. }
  109. /**
  110. * Handle the Account "force deleted" event.
  111. */
  112. public function forceDeleted(Account $account): void
  113. {
  114. //
  115. }
  116. private function getDefaultChartForBankAccount(Account $account): Account
  117. {
  118. $defaultChartCategory = AccountCategory::Asset;
  119. $defaultChartType = AccountType::CurrentAsset;
  120. //if ($account->type->isCreditCard()) {
  121. //$defaultChartCategory = ChartCategory::Liability;
  122. //$defaultChartType = ChartType::CurrentLiability;
  123. //}
  124. $subTypeId = $this->getSubTypeId($account->company_id, $defaultChartType);
  125. $latestChartCode = Account::where('company_id', $account->company_id)
  126. ->where('category', $defaultChartCategory)
  127. ->where('type', $defaultChartType)
  128. ->max('code');
  129. $newChartCode = $latestChartCode ? ++$latestChartCode : '1000';
  130. return Account::create([
  131. 'company_id' => $account->company_id,
  132. 'category' => $defaultChartCategory,
  133. 'type' => $defaultChartType,
  134. 'subtype_id' => $subTypeId,
  135. 'code' => $newChartCode,
  136. 'name' => $account->name,
  137. 'currency_code' => $account->currency_code,
  138. 'description' => $account->description ?? $account->name,
  139. 'balance' => 0,
  140. 'active' => true,
  141. 'default' => false,
  142. 'created_by' => $account->created_by,
  143. 'updated_by' => $account->updated_by,
  144. ]);
  145. }
  146. private function getSubTypeId(int $companyId, AccountType $type): ?int
  147. {
  148. $subType = AccountSubtype::where('company_id', $companyId)
  149. ->where('name', 'Cash and Cash Equivalents')
  150. ->where('type', $type)
  151. ->first();
  152. if (! $subType) {
  153. $subType = AccountSubtype::where('company_id', $companyId)
  154. ->where('type', $type)
  155. ->first();
  156. }
  157. return $subType?->id;
  158. }
  159. private function updateChartBalance(Account $chart, mixed $amount): void
  160. {
  161. //$chart->balance += $amount;
  162. //$chart->save();
  163. }
  164. }