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.

ConfigureChartOfAccounts.php 3.1KB

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