fix: 채번 규칙 할당 로직 개선 - 복사 시 품목코드 자동생성 수정
This commit is contained in:
@@ -801,6 +801,9 @@ export class ButtonActionExecutor {
|
||||
console.log("🎯 채번 규칙 할당 시작 (allocateCode 호출)");
|
||||
const { allocateNumberingCode } = await import("@/lib/api/numberingRule");
|
||||
|
||||
let hasAllocationFailure = false;
|
||||
const failedFields: string[] = [];
|
||||
|
||||
for (const [fieldName, ruleId] of Object.entries(fieldsWithNumbering)) {
|
||||
try {
|
||||
console.log(`🔄 ${fieldName} 필드에 대해 allocateCode 호출: ${ruleId}`);
|
||||
@@ -811,13 +814,31 @@ export class ButtonActionExecutor {
|
||||
console.log(`✅ ${fieldName} 새 코드 할당: ${formData[fieldName]} → ${newCode}`);
|
||||
formData[fieldName] = newCode;
|
||||
} else {
|
||||
console.warn(`⚠️ ${fieldName} 코드 할당 실패, 기존 값 유지:`, allocateResult.error);
|
||||
console.warn(`⚠️ ${fieldName} 코드 할당 실패:`, allocateResult.error);
|
||||
// 🆕 기존 값이 빈 문자열이면 실패로 표시
|
||||
if (!formData[fieldName] || formData[fieldName] === "") {
|
||||
hasAllocationFailure = true;
|
||||
failedFields.push(fieldName);
|
||||
}
|
||||
}
|
||||
} catch (allocateError) {
|
||||
console.error(`❌ ${fieldName} 코드 할당 오류:`, allocateError);
|
||||
// 오류 시 기존 값 유지
|
||||
// 🆕 기존 값이 빈 문자열이면 실패로 표시
|
||||
if (!formData[fieldName] || formData[fieldName] === "") {
|
||||
hasAllocationFailure = true;
|
||||
failedFields.push(fieldName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 🆕 채번 규칙 할당 실패 시 저장 중단
|
||||
if (hasAllocationFailure) {
|
||||
const fieldNames = failedFields.join(", ");
|
||||
toast.error(`채번 규칙 할당에 실패했습니다 (${fieldNames}). 화면 설정에서 채번 규칙을 확인해주세요.`);
|
||||
console.error(`❌ 채번 규칙 할당 실패로 저장 중단. 실패 필드: ${fieldNames}`);
|
||||
console.error("💡 해결 방법: 화면관리에서 해당 필드의 채번 규칙 설정을 확인하세요.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
console.log("✅ 채번 규칙 할당 완료");
|
||||
@@ -3039,6 +3060,7 @@ export class ButtonActionExecutor {
|
||||
config: ButtonActionConfig,
|
||||
rowData: any,
|
||||
context: ButtonActionContext,
|
||||
isCreateMode: boolean = false, // 🆕 복사 모드에서 true로 전달
|
||||
): Promise<void> {
|
||||
const { groupByColumns = [] } = config;
|
||||
|
||||
@@ -3112,10 +3134,11 @@ export class ButtonActionExecutor {
|
||||
const modalEvent = new CustomEvent("openEditModal", {
|
||||
detail: {
|
||||
screenId: config.targetScreenId,
|
||||
title: config.editModalTitle || "데이터 수정",
|
||||
title: isCreateMode ? (config.editModalTitle || "데이터 복사") : (config.editModalTitle || "데이터 수정"),
|
||||
description: description,
|
||||
modalSize: config.modalSize || "lg",
|
||||
editData: rowData,
|
||||
isCreateMode: isCreateMode, // 🆕 복사 모드에서 INSERT로 처리되도록
|
||||
groupByColumns: groupByColumns.length > 0 ? groupByColumns : undefined, // 🆕 그룹핑 컬럼 전달
|
||||
tableName: context.tableName, // 🆕 테이블명 전달
|
||||
buttonConfig: config, // 🆕 버튼 설정 전달 (제어로직 실행용)
|
||||
@@ -3230,23 +3253,61 @@ export class ButtonActionExecutor {
|
||||
"code",
|
||||
];
|
||||
|
||||
// 🆕 화면 설정에서 채번 규칙 가져오기
|
||||
let screenNumberingRules: Record<string, string> = {};
|
||||
if (config.targetScreenId) {
|
||||
try {
|
||||
const { screenApi } = await import("@/lib/api/screen");
|
||||
const layout = await screenApi.getLayout(config.targetScreenId);
|
||||
|
||||
// 레이아웃에서 채번 규칙이 설정된 컴포넌트 찾기
|
||||
const findNumberingRules = (components: any[]): void => {
|
||||
for (const comp of components) {
|
||||
const compConfig = comp.componentConfig || {};
|
||||
// text-input 컴포넌트의 채번 규칙 확인
|
||||
if (compConfig.autoGeneration?.type === "numbering_rule" && compConfig.autoGeneration?.options?.numberingRuleId) {
|
||||
const columnName = compConfig.columnName || comp.columnName;
|
||||
if (columnName) {
|
||||
screenNumberingRules[columnName] = compConfig.autoGeneration.options.numberingRuleId;
|
||||
console.log(`📋 화면 설정에서 채번 규칙 발견: ${columnName} → ${compConfig.autoGeneration.options.numberingRuleId}`);
|
||||
}
|
||||
}
|
||||
// 중첩된 컴포넌트 확인
|
||||
if (comp.children && Array.isArray(comp.children)) {
|
||||
findNumberingRules(comp.children);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (layout?.components) {
|
||||
findNumberingRules(layout.components);
|
||||
}
|
||||
console.log("📋 화면 설정에서 찾은 채번 규칙:", screenNumberingRules);
|
||||
} catch (error) {
|
||||
console.warn("⚠️ 화면 레이아웃 조회 실패:", error);
|
||||
}
|
||||
}
|
||||
|
||||
// 품목코드 필드를 찾아서 무조건 공백으로 초기화
|
||||
let resetFieldName = "";
|
||||
for (const field of itemCodeFields) {
|
||||
if (copiedData[field] !== undefined) {
|
||||
const originalValue = copiedData[field];
|
||||
const ruleIdKey = `${field}_numberingRuleId`;
|
||||
const hasNumberingRule =
|
||||
rowData[ruleIdKey] !== undefined && rowData[ruleIdKey] !== null && rowData[ruleIdKey] !== "";
|
||||
|
||||
// 1순위: 원본 데이터에서 채번 규칙 ID 확인
|
||||
// 2순위: 화면 설정에서 채번 규칙 ID 확인
|
||||
const numberingRuleId = rowData[ruleIdKey] || screenNumberingRules[field];
|
||||
const hasNumberingRule = numberingRuleId !== undefined && numberingRuleId !== null && numberingRuleId !== "";
|
||||
|
||||
// 품목코드를 무조건 공백으로 초기화
|
||||
copiedData[field] = "";
|
||||
|
||||
// 채번 규칙 ID가 있으면 복사 (저장 시 자동 생성)
|
||||
if (hasNumberingRule) {
|
||||
copiedData[ruleIdKey] = rowData[ruleIdKey];
|
||||
copiedData[ruleIdKey] = numberingRuleId;
|
||||
console.log(`✅ 품목코드 초기화 (채번 규칙 있음): ${field} (기존값: ${originalValue})`);
|
||||
console.log(`📋 채번 규칙 ID 복사: ${ruleIdKey} = ${rowData[ruleIdKey]}`);
|
||||
console.log(`📋 채번 규칙 ID 설정: ${ruleIdKey} = ${numberingRuleId}`);
|
||||
} else {
|
||||
console.log(`✅ 품목코드 초기화 (수동 입력 필요): ${field} (기존값: ${originalValue})`);
|
||||
}
|
||||
@@ -3303,9 +3364,9 @@ export class ButtonActionExecutor {
|
||||
|
||||
switch (editMode) {
|
||||
case "modal":
|
||||
// 모달로 복사 폼 열기 (편집 모달 재사용)
|
||||
console.log("📋 모달로 복사 폼 열기");
|
||||
await this.openEditModal(config, rowData, context);
|
||||
// 모달로 복사 폼 열기 (편집 모달 재사용, INSERT 모드로)
|
||||
console.log("📋 모달로 복사 폼 열기 (INSERT 모드)");
|
||||
await this.openEditModal(config, rowData, context, true); // 🆕 isCreateMode: true
|
||||
break;
|
||||
|
||||
case "navigate":
|
||||
@@ -3316,8 +3377,8 @@ export class ButtonActionExecutor {
|
||||
|
||||
default:
|
||||
// 기본값: 모달
|
||||
console.log("📋 기본 모달로 복사 폼 열기");
|
||||
this.openEditModal(config, rowData, context);
|
||||
console.log("📋 기본 모달로 복사 폼 열기 (INSERT 모드)");
|
||||
this.openEditModal(config, rowData, context, true); // 🆕 isCreateMode: true
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error("❌ openCopyForm 실행 중 오류:", error);
|
||||
|
||||
Reference in New Issue
Block a user