diff --git a/app/Helpers/TenantCodeGenerator.php b/app/Helpers/TenantCodeGenerator.php new file mode 100644 index 0000000..e5d04eb --- /dev/null +++ b/app/Helpers/TenantCodeGenerator.php @@ -0,0 +1,114 @@ +code) >= 4) { + $sequenceString = substr($tenant->code, -4); + + // 마지막 4자리가 36진수 문자열인지 확인 + if (strspn($sequenceString, self::BASE36_CHARS) === 4) { + $sequence = self::fromBase36($sequenceString); + if ($sequence > $lastNumber) { + $lastNumber = $sequence; + } + } + } + } + + // 3. 다음 순번 계산 (36^4 = 1,679,616로 순환) + $nextSequence = ($lastNumber + 1) % 1679616; + + // 4. 36진수 4자리로 포맷 + $formattedSequence = self::toBase36($nextSequence); + + // 5. 초성 + 순번 조합하여 최종 코드 생성 + return $initials.$formattedSequence; + } + + /** + * 한글 텍스트에서 초성 추출 및 영문 변환 + * + * @param string $text 한글 텍스트 + * @return string 영문 초성 (예: "테크컴퍼니" → "TKP") + */ + private static function extractKoreanInitials(string $text): string + { + $initials = ''; + $charLength = mb_strlen($text, 'UTF-8'); + + for ($i = 0; $i < $charLength; $i++) { + $char = mb_substr($text, $i, 1, 'UTF-8'); + $code = mb_ord($char, 'UTF-8'); + + // 한글 유니코드 범위 (가-힣: 0xAC00-0xD7A3) + if ($code >= 0xAC00 && $code <= 0xD7A3) { + $index = floor(($code - 0xAC00) / 588); + $initials .= self::INITIALS[$index]; + } + // 한글이 아닌 문자는 무시 + } + + // 초성을 영문으로 변환 + $koreanInitials = ['ㄱ', 'ㄲ', 'ㄴ', 'ㄷ', 'ㄸ', 'ㄹ', 'ㅁ', 'ㅂ', 'ㅃ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅉ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ']; + $englishInitials = ['G', 'KK', 'N', 'D', 'TT', 'R', 'M', 'B', 'BB', 'S', 'SS', 'O', 'J', 'JJ', 'CH', 'K', 'T', 'P', 'H']; + $initials = strtr($initials, array_combine($koreanInitials, $englishInitials)); + + // 공백 제거 및 대문자 변환 + return strtoupper(str_replace(' ', '', $initials)); + } + + /** + * 36진수 문자열을 10진수로 변환 + * + * @param string $str 36진수 문자열 (예: "0001") + * @return int 10진수 값 + */ + private static function fromBase36(string $str): int + { + return intval($str, 36); + } + + /** + * 10진수를 4자리 36진수 문자열로 변환 + * + * @param int $num 10진수 값 + * @return string 4자리 36진수 문자열 (예: "0001") + */ + private static function toBase36(int $num): string + { + return str_pad(strtoupper(base_convert((string) $num, 10, 36)), 4, '0', STR_PAD_LEFT); + } +} \ No newline at end of file diff --git a/app/Services/RegisterService.php b/app/Services/RegisterService.php index b72c4ed..480d625 100644 --- a/app/Services/RegisterService.php +++ b/app/Services/RegisterService.php @@ -2,6 +2,7 @@ namespace App\Services; +use App\Helpers\TenantCodeGenerator; use App\Models\Commons\Menu; use App\Models\Members\User; use App\Models\Tenants\Tenant; @@ -35,9 +36,13 @@ class RegisterService public static function register(array $params): array { return DB::transaction(function () use ($params) { - // 1. Create Tenant with trial status and options + // 1. Generate unique tenant code from company name + $code = TenantCodeGenerator::generate($params['company_name']); + + // 2. Create Tenant with trial status and options $tenant = Tenant::create([ 'company_name' => $params['company_name'], + 'code' => $code, 'business_num' => $params['business_num'] ?? null, 'tenant_st_code' => 'trial', // 트라이얼 상태 'options' => [