123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
-
- namespace App\Observers;
-
- use App\Enums\Accounting\AccountCategory;
- use App\Enums\Accounting\AccountType;
- use App\Enums\AccountStatus;
- use App\Models\Accounting\Account;
- use App\Models\Accounting\AccountSubtype;
- use App\Models\Banking\BankAccount;
- use App\Utilities\Accounting\AccountCode;
-
- class AccountObserver
- {
- public function creating(Account $account): void
- {
- $this->setCategoryAndType($account, true);
- }
-
- public function updating(Account $account): void
- {
- if ($account->isDirty('subtype_id')) {
- $this->setCategoryAndType($account, false);
- }
- }
-
- private function setCategoryAndType(Account $account, bool $isCreating): void
- {
- $subtype = $account->subtype_id ? AccountSubtype::find($account->subtype_id) : null;
-
- if ($subtype) {
- $account->category = $subtype->category;
- $account->type = $subtype->type;
- } elseif ($isCreating) {
- $account->category = AccountCategory::Asset;
- $account->type = AccountType::CurrentAsset;
- }
- }
-
- private function setFieldsForBankAccount(Account $account): void
- {
- $generatedAccountCode = AccountCode::generate($account->company_id, $account->subtype_id);
-
- $account->code = $generatedAccountCode;
- }
-
- /**
- * Handle the Account "created" event.
- */
- public function created(Account $account): void
- {
- // $bankAccount = $account->accountable;
- if (($account->accountable_type === BankAccount::class) && $account->code === null) {
- $this->setFieldsForBankAccount($account);
- }
-
- //$account->histories()->create([
- // 'company_id' => $account->company_id,
- // 'account_id' => $account->id,
- // 'type' => $account->type,
- // 'name' => $account->name,
- // 'number' => $account->number,
- // 'currency_code' => $account->currency_code,
- // 'opening_balance' => $account->opening_balance,
- // 'balance' => $account->balance,
- // 'exchange_rate' => $account->currency->rate,
- // 'status' => AccountStatus::Open,
- // 'actions' => ['account_created'],
- // 'enabled' => $account->enabled,
- // 'changed_by' => $account->created_by,
- //]);
- }
-
- /**
- * Handle the Account "updated" event.
- */
- public function updated(Account $account): void
- {
- //$actionsTaken = [];
-
- //foreach ($this->actions as $action => $attribute) {
- //if ($account->isDirty($attribute)) {
- //$actionsTaken[] = $action;
- // }
- //}
-
- //if (count($actionsTaken) > 0) {
- //$account->histories()->create([
- //'company_id' => $account->company_id,
- // 'account_id' => $account->id,
- // 'type' => $account->getOriginal('type'),
- // 'name' => $account->getOriginal('name'),
- // 'number' => $account->getOriginal('number'),
- // 'currency_code' => $account->getOriginal('currency_code'),
- // 'opening_balance' => $account->getRawOriginal('opening_balance'),
- // 'balance' => $account->getRawOriginal('balance'),
- // 'exchange_rate' => $account->currency->getRawOriginal('rate'),
- // 'status' => $account->getOriginal('status'),
- // 'actions' => $actionsTaken,
- // 'enabled' => $account->getOriginal('enabled'),
- // 'changed_by' => $account->updated_by,
- //]);
- //}
- }
-
- /**
- * Handle the Account "deleted" event.
- */
- public function deleted(Account $account): void
- {
- //
- }
-
- /**
- * Handle the Account "restored" event.
- */
- public function restored(Account $account): void
- {
- //
- }
-
- /**
- * Handle the Account "force deleted" event.
- */
- public function forceDeleted(Account $account): void
- {
- //
- }
-
- private function getDefaultChartForBankAccount(Account $account): Account
- {
- $defaultChartCategory = AccountCategory::Asset;
- $defaultChartType = AccountType::CurrentAsset;
-
- //if ($account->type->isCreditCard()) {
- //$defaultChartCategory = ChartCategory::Liability;
- //$defaultChartType = ChartType::CurrentLiability;
- //}
-
- $subTypeId = $this->getSubTypeId($account->company_id, $defaultChartType);
-
- $latestChartCode = Account::where('company_id', $account->company_id)
- ->where('category', $defaultChartCategory)
- ->where('type', $defaultChartType)
- ->max('code');
-
- $newChartCode = $latestChartCode ? ++$latestChartCode : '1000';
-
- return Account::create([
- 'company_id' => $account->company_id,
- 'category' => $defaultChartCategory,
- 'type' => $defaultChartType,
- 'subtype_id' => $subTypeId,
- 'code' => $newChartCode,
- 'name' => $account->name,
- 'currency_code' => $account->currency_code,
- 'description' => $account->description ?? $account->name,
- 'balance' => 0,
- 'active' => true,
- 'default' => false,
- 'created_by' => $account->created_by,
- 'updated_by' => $account->updated_by,
- ]);
- }
-
- private function getSubTypeId(int $companyId, AccountType $type): ?int
- {
- $subType = AccountSubtype::where('company_id', $companyId)
- ->where('name', 'Cash and Cash Equivalents')
- ->where('type', $type)
- ->first();
-
- if (! $subType) {
- $subType = AccountSubtype::where('company_id', $companyId)
- ->where('type', $type)
- ->first();
- }
-
- return $subType?->id;
- }
-
- private function updateChartBalance(Account $chart, mixed $amount): void
- {
- //$chart->balance += $amount;
- //$chart->save();
- }
- }
|