diff --git a/app/Services/ReceivingService.php b/app/Services/ReceivingService.php index 2b2c78ca..4948f23b 100644 --- a/app/Services/ReceivingService.php +++ b/app/Services/ReceivingService.php @@ -427,16 +427,29 @@ private function generateReceivingNumber(int $tenantId): string /** * LOT번호 자동 생성 + * + * 5130 레거시 차용: YYMMDD-NN (일별 시퀀스, 01부터 시작) + * 개선: 파일 기반 → DB 기반 (테넌트별 격리, 동시성 안전) */ private function generateLotNo(): string { - $now = now(); - $year = $now->format('y'); - $month = $now->format('m'); - $day = $now->format('d'); - $seq = str_pad(rand(1, 99), 2, '0', STR_PAD_LEFT); + $tenantId = $this->tenantId(); + $prefix = now()->format('ymd'); // 예: 260317 - return "{$year}{$month}{$day}-{$seq}"; + $lastReceiving = Receiving::query() + ->where('tenant_id', $tenantId) + ->where('lot_no', 'like', $prefix.'-%') + ->orderBy('lot_no', 'desc') + ->first(['lot_no']); + + if ($lastReceiving) { + $lastSeq = (int) substr($lastReceiving->lot_no, -2); + $newSeq = $lastSeq + 1; + } else { + $newSeq = 1; + } + + return $prefix.'-'.str_pad($newSeq, 2, '0', STR_PAD_LEFT); } /**