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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. }
  43. private function createDefaultAccounts(Company $company, AccountSubtype $subtype, array $subtypeConfig): void
  44. {
  45. if (isset($subtypeConfig['accounts']) && is_array($subtypeConfig['accounts'])) {
  46. $baseCode = $subtypeConfig['base_code'];
  47. foreach ($subtypeConfig['accounts'] as $accountName => $accountDetails) {
  48. Account::create([
  49. 'company_id' => $company->id,
  50. 'category' => $subtype->type->getCategory()->value,
  51. 'type' => $subtype->type->value,
  52. 'subtype_id' => $subtype->id,
  53. 'code' => $baseCode++,
  54. 'name' => $accountName,
  55. 'description' => $accountDetails['description'] ?? 'No description available.',
  56. 'ending_balance' => 0,
  57. 'active' => true,
  58. 'default' => true,
  59. 'currency_code' => CurrencyAccessor::getDefaultCurrency(),
  60. 'created_by' => $company->owner->id,
  61. 'updated_by' => $company->owner->id,
  62. ]);
  63. }
  64. }
  65. }
  66. }