diff --git a/app/Services/BendingItemService.php b/app/Services/BendingItemService.php index 7b290cfe..5bb6f147 100644 --- a/app/Services/BendingItemService.php +++ b/app/Services/BendingItemService.php @@ -146,40 +146,25 @@ private function buildOptions(array $data): ?array /** * 기초자료 코드 자동 채번 * - * BD-XX : 표준 형상 (재공품 코드 앞 5자리와 동일) - * BD-XX.nnn : 표준 대비 변형 (주문 수정 형상, 최대 999종) + * BD-XX.001 : 대표(표준) 형상 — 재공품 코드(BD-XX-길이)의 기준 + * BD-XX.002~: 표준 대비 변형 (주문 수정 형상, 최대 999종) * - * 첫 등록 → BD-XX (표준), 이후 → BD-XX.001~ (변형) + * 항상 .001부터 시작, .001 = 대표 번호 */ private function generateCode(string $prefix): string { $prefix = strtoupper($prefix); - // BD-XX.nnn 형태 중 가장 큰 순번 조회 - $lastDotCode = BendingItem::withoutGlobalScopes() + $lastCode = BendingItem::withoutGlobalScopes() ->where('code', 'like', "BD-{$prefix}.%") ->orderByRaw('CAST(SUBSTRING(code, ?) AS UNSIGNED) DESC', [strlen("BD-{$prefix}.") + 1]) ->value('code'); - // BD-XX (표준 형상) 존재 여부 - $plainExists = BendingItem::withoutGlobalScopes() - ->where('code', "BD-{$prefix}") - ->exists(); - - if (! $lastDotCode && ! $plainExists) { - // 해당 분류 첫 등록 → BD-XX (표준 형상) - return "BD-{$prefix}"; + $nextSeq = 1; + if ($lastCode && preg_match('/\.(\d+)$/', $lastCode, $m)) { + $nextSeq = (int) $m[1] + 1; } - if ($plainExists && ! $lastDotCode) { - // 표준만 있고 변형 없음 → 변형 001번 부여 - return "BD-{$prefix}.001"; - } - - // 변형이 이미 있음 → 마지막 +1 - preg_match('/\.(\d+)$/', $lastDotCode, $m); - $nextSeq = (int) ($m[1] ?? 0) + 1; - return "BD-{$prefix}.".str_pad($nextSeq, 3, '0', STR_PAD_LEFT); }