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

AccountCode.php 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Utilities\Accounting;
  3. use App\Enums\Accounting\AccountType;
  4. use App\Models\Accounting\Account;
  5. use App\Models\Accounting\AccountSubtype;
  6. use RuntimeException;
  7. class AccountCode
  8. {
  9. public static function isValidCode($code, AccountType $type): bool
  10. {
  11. $range = self::getRangeForType($type);
  12. $mainAccountPart = explode('-', $code)[0];
  13. $numericValue = (int) $mainAccountPart;
  14. return $numericValue >= $range[0] && $numericValue <= $range[1];
  15. }
  16. public static function getMessage(AccountType $type): string
  17. {
  18. $range = self::getRangeForType($type);
  19. return "The account code must range from {$range[0]} to {$range[1]} for a {$type->getLabel()}.";
  20. }
  21. public static function getRangeForType(AccountType $type): array
  22. {
  23. return match ($type) {
  24. AccountType::CurrentAsset => [1000, 1499],
  25. AccountType::NonCurrentAsset => [1500, 1899],
  26. AccountType::ContraAsset => [1900, 1999],
  27. AccountType::CurrentLiability => [2000, 2499],
  28. AccountType::NonCurrentLiability => [2500, 2899],
  29. AccountType::ContraLiability => [2900, 2999],
  30. AccountType::Equity => [3000, 3899],
  31. AccountType::ContraEquity => [3900, 3999],
  32. AccountType::OperatingRevenue => [4000, 4499],
  33. AccountType::NonOperatingRevenue => [4500, 4899],
  34. AccountType::ContraRevenue => [4900, 4949],
  35. AccountType::UncategorizedRevenue => [4950, 4999],
  36. AccountType::OperatingExpense => [5000, 5499],
  37. AccountType::NonOperatingExpense => [5500, 5899],
  38. AccountType::ContraExpense => [5900, 5949],
  39. AccountType::UncategorizedExpense => [5950, 5999],
  40. };
  41. }
  42. public static function generate(int $companyId, string $subtypeId): string
  43. {
  44. $subtype = AccountSubtype::find($subtypeId);
  45. $subtypeName = $subtype->name;
  46. $typeEnum = $subtype->type;
  47. $typeValue = $typeEnum->value;
  48. $baseCode = config("chart-of-accounts.default.{$typeValue}.{$subtypeName}.base_code");
  49. $range = self::getRangeForType($typeEnum);
  50. $lastAccount = Account::where('subtype_id', $subtypeId)
  51. ->where('company_id', $companyId)
  52. ->whereNotNull('code')
  53. ->orderBy('code', 'desc')
  54. ->first();
  55. $numericValue = $lastAccount ? (int) explode('-', $lastAccount->code)[0] + 1 : (int) $baseCode;
  56. // Ensure the new code does not exist and is within the acceptable range
  57. while (Account::where('company_id', $companyId)->where('code', '=', (string) $numericValue)->exists() || $numericValue > $range[1]) {
  58. if ($numericValue > $range[1]) {
  59. throw new RuntimeException('No more account codes available within the allowed range for this type.');
  60. }
  61. $numericValue++;
  62. }
  63. return (string) $numericValue;
  64. }
  65. }