您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ChartOfAccountsService.php 3.3KB

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