Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

AccountCode.php 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. $type = $subtype->type;
  46. $range = self::getRangeForType($type);
  47. $lastAccount = Account::where('subtype_id', $subtypeId)
  48. ->where('company_id', $companyId)
  49. ->orderBy('code', 'desc')
  50. ->first(); // maybe handle subaccounts (parent-child) in the future (not using max() because of subaccounts)
  51. if ($lastAccount) {
  52. $lastCode = $lastAccount->code;
  53. $lastAccountPart = explode('-', $lastCode)[0]; // possibly handle subaccounts (parent-child) in the future
  54. $numericValue = (int) $lastAccountPart;
  55. $numericValue++;
  56. } else {
  57. $numericValue = $range[0];
  58. }
  59. while (Account::where('company_id', $companyId)->where('code', '=', (string) $numericValue)->exists() || $numericValue > $range[1]) {
  60. if ($numericValue > $range[1]) {
  61. throw new RuntimeException('No more account codes available for this type.');
  62. }
  63. $numericValue++;
  64. }
  65. return (string) $numericValue;
  66. }
  67. }