選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ConfigureChartOfAccounts.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace App\Listeners;
  3. use App\Enums\Accounting\AccountType;
  4. use App\Enums\Banking\BankAccountType;
  5. use App\Events\CompanyGenerated;
  6. use App\Models\Accounting\Account;
  7. use App\Models\Accounting\AccountSubtype;
  8. use App\Models\Banking\BankAccount;
  9. use App\Models\Company;
  10. use App\Utilities\Currency\CurrencyAccessor;
  11. class ConfigureChartOfAccounts
  12. {
  13. /**
  14. * Create the event listener.
  15. */
  16. public function __construct()
  17. {
  18. //
  19. }
  20. /**
  21. * Handle the event.
  22. */
  23. public function handle(CompanyGenerated $event): void
  24. {
  25. $company = $event->company;
  26. $this->createChartOfAccounts($company);
  27. }
  28. public function createChartOfAccounts(Company $company): void
  29. {
  30. $chartOfAccounts = config('chart-of-accounts.default');
  31. foreach ($chartOfAccounts as $type => $subtypes) {
  32. foreach ($subtypes as $subtypeName => $subtypeConfig) {
  33. $subtype = AccountSubtype::create([
  34. 'company_id' => $company->id,
  35. 'multi_currency' => $subtypeConfig['multi_currency'] ?? false,
  36. 'category' => AccountType::from($type)->getCategory()->value,
  37. 'type' => $type,
  38. 'name' => $subtypeName,
  39. 'description' => $subtypeConfig['description'] ?? 'No description available.',
  40. ]);
  41. $this->createDefaultAccounts($company, $subtype, $subtypeConfig);
  42. }
  43. }
  44. }
  45. private function createDefaultAccounts(Company $company, AccountSubtype $subtype, array $subtypeConfig): void
  46. {
  47. if (isset($subtypeConfig['accounts']) && is_array($subtypeConfig['accounts'])) {
  48. $baseCode = $subtypeConfig['base_code'];
  49. foreach ($subtypeConfig['accounts'] as $accountName => $accountDetails) {
  50. $bankAccount = null;
  51. if ($subtypeConfig['multi_currency'] && isset($subtypeConfig['bank_account_type'])) {
  52. $bankAccount = $this->createBankAccountForMultiCurrency($company, $subtypeConfig['bank_account_type']);
  53. }
  54. $account = Account::create([
  55. 'company_id' => $company->id,
  56. 'subtype_id' => $subtype->id,
  57. 'category' => $subtype->type->getCategory()->value,
  58. 'type' => $subtype->type->value,
  59. 'code' => $baseCode++,
  60. 'name' => $accountName,
  61. 'currency_code' => CurrencyAccessor::getDefaultCurrency(),
  62. 'description' => $accountDetails['description'] ?? 'No description available.',
  63. 'default' => true,
  64. 'created_by' => $company->owner->id,
  65. 'updated_by' => $company->owner->id,
  66. ]);
  67. if ($bankAccount) {
  68. $account->bankAccount()->associate($bankAccount);
  69. }
  70. $account->save();
  71. }
  72. }
  73. }
  74. private function createBankAccountForMultiCurrency(Company $company, string $bankAccountType): BankAccount
  75. {
  76. $bankAccountType = BankAccountType::from($bankAccountType) ?? BankAccountType::Other;
  77. return BankAccount::create([
  78. 'company_id' => $company->id,
  79. 'institution_id' => null,
  80. 'type' => $bankAccountType,
  81. 'number' => null,
  82. 'enabled' => BankAccount::where('company_id', $company->id)->where('enabled', true)->doesntExist(),
  83. 'created_by' => $company->owner->id,
  84. 'updated_by' => $company->owner->id,
  85. ]);
  86. }
  87. }