From 927b3d6cbfad6ce84ec1bff2faaace95e9586efd Mon Sep 17 00:00:00 2001
From: joonhoekim <26rote@gmail.com>
Date: Fri, 28 Nov 2025 16:05:42 +0900
Subject: (김준회) dolce RegisterKind 코드 오류 수정 (APPP --> APPR-P),
 업로드시 file max seq 처리 추가, 상세도면 추가시 RegisterSerialNo 최대값 + 1
 으로 요청
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../add-and-modify-detail-drawing-dialog.tsx       | 32 +++++++++++++++++++---
 1 file changed, 28 insertions(+), 4 deletions(-)

(limited to 'lib/dolce/dialogs/add-and-modify-detail-drawing-dialog.tsx')

diff --git a/lib/dolce/dialogs/add-and-modify-detail-drawing-dialog.tsx b/lib/dolce/dialogs/add-and-modify-detail-drawing-dialog.tsx
index 54ec79a7..d4318b90 100644
--- a/lib/dolce/dialogs/add-and-modify-detail-drawing-dialog.tsx
+++ b/lib/dolce/dialogs/add-and-modify-detail-drawing-dialog.tsx
@@ -23,7 +23,7 @@ import { Textarea } from "@/components/ui/textarea";
 import { Upload, X, FileIcon, Info, Trash2 } from "lucide-react";
 import { toast } from "sonner";
 import { useTranslation } from "@/i18n/client";
-import { UnifiedDwgReceiptItem, DetailDwgReceiptItem, editDetailDwgReceipt } from "../actions";
+import { UnifiedDwgReceiptItem, DetailDwgReceiptItem, editDetailDwgReceipt, fetchDetailDwgReceiptList } from "../actions";
 import { v4 as uuidv4 } from "uuid";
 import { useFileUploadWithProgress } from "../hooks/use-file-upload-with-progress";
 import { uploadFilesWithProgress } from "../utils/upload-with-progress";
@@ -32,7 +32,8 @@ import {
   getB3DrawingUsageOptions, 
   getB3RegisterKindOptions,
   getB4DrawingUsageOptions,
-  getB4RegisterKindOptions
+  getB4RegisterKindOptions,
+  findRegisterKindCode
 } from "../utils/code-translator";
 import {
   AlertDialog,
@@ -92,7 +93,7 @@ export function AddAndModifyDetailDrawingDialog({
   useEffect(() => {
     if (mode === "edit" && detailDrawing && open) {
       setDrawingUsage(detailDrawing.DrawingUsage || "");
-      setRegisterKind(detailDrawing.RegisterKind || "");
+      setRegisterKind(findRegisterKindCode(detailDrawing.RegisterKind || "", drawingKind));
       setRevision(detailDrawing.DrawingRevNo || "");
       setComment(detailDrawing.RegisterDesc || "");
     } else if (mode === "add" && open) {
@@ -227,6 +228,29 @@ export function AddAndModifyDetailDrawingDialog({
       setIsSubmitting(true);
 
       if (mode === "add" && drawing) {
+        // 상세도면 리스트 조회하여 RegisterSerialNo 계산
+        let nextSerialNo = 1;
+        try {
+          const detailList = await fetchDetailDwgReceiptList({
+            project: drawing.ProjectNo,
+            drawingNo: drawing.DrawingNo,
+            discipline: drawing.Discipline,
+            drawingKind: drawing.DrawingKind,
+            userId: userId,
+          });
+
+          if (detailList && detailList.length > 0) {
+            // RegisterSerialNo의 최댓값을 찾아 +1 (기본값 0 처리)
+            const maxSerial = Math.max(...detailList.map(item => item.RegisterSerialNo || 0));
+            nextSerialNo = maxSerial + 1;
+          }
+          console.log(`[AddDetail] RegisterSerialNo Calculated: ${nextSerialNo} (Max: ${nextSerialNo - 1})`);
+        } catch (error) {
+          console.error("상세도면 리스트 조회 실패 (SerialNo 계산 중):", error);
+          toast.error("Failed to calculate serial number");
+          return;
+        }
+
         // 파일 업로드 ID 생성
         const uploadId = uuidv4();
 
@@ -243,7 +267,7 @@ export function AddAndModifyDetailDrawingDialog({
               DrawingNo: drawing.DrawingNo,
               DrawingName: drawing.DrawingName,
               RegisterGroupId: drawing.RegisterGroupId,
-              RegisterSerialNo: 0, // 자동 증가
+              RegisterSerialNo: nextSerialNo, // 자동 증가값 사용
               RegisterKind: registerKind,
               DrawingRevNo: drawingUsage === "CMT" ? null : revision,
               Category: "TS", // To SHI (벤더가 SHI에게 제출)
-- 
cgit v1.2.3

