From 3d95eb8caaf07360cddf96b9d71458e37e1b6144 Mon Sep 17 00:00:00 2001 From: DDD1542 Date: Mon, 6 Apr 2026 17:12:12 +0900 Subject: [PATCH 1/4] =?UTF-8?q?=EC=A7=B9.=EC=A7=B9=EC=A7=B9=20=EA=B1=B0?= =?UTF-8?q?=EB=A6=AC=EB=A9=B4=20=EB=91=90=EB=93=A4=EA=B2=A8=20=ED=8C=B8?= =?UTF-8?q?=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../COMPANY_16/purchase/supplier/page.tsx | 2864 ++++++++++++++--- .../(main)/COMPANY_16/sales/customer/page.tsx | 101 +- frontend/components/common/ConfirmDialog.tsx | 17 +- frontend/components/ui/alert-dialog.tsx | 6 +- 4 files changed, 2517 insertions(+), 471 deletions(-) diff --git a/frontend/app/(main)/COMPANY_16/purchase/supplier/page.tsx b/frontend/app/(main)/COMPANY_16/purchase/supplier/page.tsx index 4eb3ed3f..c2111d7e 100644 --- a/frontend/app/(main)/COMPANY_16/purchase/supplier/page.tsx +++ b/frontend/app/(main)/COMPANY_16/purchase/supplier/page.tsx @@ -1,57 +1,126 @@ "use client"; -import React, { useState, useEffect, useCallback, useMemo } from "react"; +/** + * 공급업체관리 — Type B 마스터-디테일 레이아웃 (리디자인) + * + * 좌측: 공급업체 목록 (supplier_mng) + * 우측: 품목별 단가 + 납품처 정보 탭 + * + * 모달: + * - 공급업체 등록/수정 (supplier_mng) + * - 품목 추가 (item_info 검색 → supplier_item_mapping + supplier_item_prices) + * - 납품처 등록 (delivery_destination) + */ + +import React, { useState, useEffect, useCallback, useRef } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; -import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, DialogDescription } from "@/components/ui/dialog"; +import { Badge } from "@/components/ui/badge"; +import { Tabs, TabsList, TabsTrigger, TabsContent } from "@/components/ui/tabs"; import { Label } from "@/components/ui/label"; -import { Checkbox } from "@/components/ui/checkbox"; import { ResizableHandle, ResizablePanel, ResizablePanelGroup } from "@/components/ui/resizable"; -import { Plus, Trash2, Save, Loader2, FileSpreadsheet, Download, Pencil, Truck, Search, Settings2 } from "lucide-react"; +import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, DialogDescription } from "@/components/ui/dialog"; +import { + Plus, Trash2, Save, Loader2, FileSpreadsheet, Download, Pencil, + Users, Package, MapPin, Search, X, Tag, Coins, Settings2, GripVertical, + ChevronRight, ChevronDown, +} from "lucide-react"; +import { + DndContext, closestCenter, PointerSensor, useSensor, useSensors, DragEndEvent, +} from "@dnd-kit/core"; +import { SortableContext, verticalListSortingStrategy, useSortable, arrayMove } from "@dnd-kit/sortable"; +import { CSS } from "@dnd-kit/utilities"; import { cn } from "@/lib/utils"; import { apiClient } from "@/lib/api/client"; import { useAuth } from "@/hooks/useAuth"; import { toast } from "sonner"; import { useConfirmDialog } from "@/components/common/ConfirmDialog"; -import { ExcelUploadModal } from "@/components/common/ExcelUploadModal"; +import { MultiTableExcelUploadModal } from "@/components/common/MultiTableExcelUploadModal"; +import { autoDetectMultiTableConfig, TableChainConfig } from "@/lib/api/multiTableExcel"; import { exportToExcel } from "@/lib/utils/excelExport"; -import { useTableSettings } from "@/hooks/useTableSettings"; +import { validateField, validateForm, formatField } from "@/lib/utils/validation"; +import { getAvailableNumberingRulesForScreen, previewNumberingCode, allocateNumberingCode } from "@/lib/api/numberingRule"; import { TableSettingsModal } from "@/components/common/TableSettingsModal"; +import { useTableSettings } from "@/hooks/useTableSettings"; import { DynamicSearchFilter, FilterValue } from "@/components/common/DynamicSearchFilter"; import { EDataTable, EDataTableColumn } from "@/components/common/EDataTable"; const SUPPLIER_TABLE = "supplier_mng"; const MAPPING_TABLE = "supplier_item_mapping"; +const PRICE_TABLE = "supplier_item_prices"; +const DELIVERY_TABLE = "delivery_destination"; +const CONTACT_TABLE = "supplier_contact"; -const SUPPLIER_COLUMNS = [ +const SUPPLIER_GRID_COLUMNS = [ + { key: "supplier_code", label: "공급업체코드" }, + { key: "supplier_name", label: "공급업체명" }, + { key: "division", label: "거래유형" }, { key: "contact_person", label: "담당자" }, - { key: "contact_phone", label: "연락처" }, + { key: "contact_phone", label: "전화번호" }, + { key: "email", label: "이메일" }, + { key: "business_number", label: "사업자번호" }, + { key: "address", label: "주소" }, { key: "status", label: "상태" }, ]; +function SortableMappingRow({ id, children }: { id: string; children: React.ReactNode }) { + const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({ id }); + const style: React.CSSProperties = { + transform: CSS.Transform.toString(transform), transition, + opacity: isDragging ? 0.5 : 1, + }; + return ( +
+
+ +
+ {children} +
+ ); +} + export default function SupplierManagementPage() { const { user } = useAuth(); - const { confirm, ConfirmDialogComponent } = useConfirmDialog(); + const { confirm, ConfirmDialogComponent, isConfirmOpenRef } = useConfirmDialog(); + const ts = useTableSettings("c16-supplier", SUPPLIER_TABLE, SUPPLIER_GRID_COLUMNS); + const dndSensors = useSensors(useSensor(PointerSensor, { activationConstraint: { distance: 5 } })); - // 검색 필터 (DynamicSearchFilter) + // 검색 필터 (DynamicSearchFilter에서 관리) const [searchFilters, setSearchFilters] = useState([]); // 좌측: 공급업체 목록 const [suppliers, setSuppliers] = useState([]); + const [rawSuppliers, setRawSuppliers] = useState([]); const [supplierLoading, setSupplierLoading] = useState(false); + const [showInactive, setShowInactive] = useState(false); + const [mainContactMap, setMainContactMap] = useState>({}); + const [supplierCount, setSupplierCount] = useState(0); const [selectedSupplierId, setSelectedSupplierId] = useState(null); - // 우측: 품목 매핑 - const [mappingItems, setMappingItems] = useState([]); - const [mappingLoading, setMappingLoading] = useState(false); - const [mappingCheckedIds, setMappingCheckedIds] = useState([]); + // 우측: 탭 + const [rightTab, setRightTab] = useState<"items" | "delivery">("items"); + // 우측: 품목 단가 + const [priceItems, setPriceItems] = useState([]); + const [priceGroups, setPriceGroups] = useState>({}); + const [priceLoading, setPriceLoading] = useState(false); + const [priceCheckedIds, setPriceCheckedIds] = useState([]); + const [expandedItems, setExpandedItems] = useState>(new Set()); + const [collapsedPriceCards, setCollapsedPriceCards] = useState>(new Set()); + // 우측: 납품처 + const [deliveryItems, setDeliveryItems] = useState([]); + const [deliveryLoading, setDeliveryLoading] = useState(false); - // 공급업체 등록/수정 모달 + // 품목 편집 데이터 (더블클릭 시 상세 입력 모달 재활용) + const [editItemData, setEditItemData] = useState(null); + const savingRef = useRef(false); + + // 거래처 모달 const [supplierModalOpen, setSupplierModalOpen] = useState(false); const [supplierEditMode, setSupplierEditMode] = useState(false); const [supplierForm, setSupplierForm] = useState>({}); + const [formErrors, setFormErrors] = useState>({}); const [saving, setSaving] = useState(false); // 품목 추가 모달 (1단계: 검색/선택) @@ -61,79 +130,188 @@ export default function SupplierManagementPage() { const [itemSearchLoading, setItemSearchLoading] = useState(false); const [itemCheckedIds, setItemCheckedIds] = useState>(new Set()); - // 품목 상세 입력 모달 (2단계) + // 품목 상세 입력 모달 (2단계: 거래처 품번/품명 + 단가) const [itemDetailOpen, setItemDetailOpen] = useState(false); const [selectedItemsForDetail, setSelectedItemsForDetail] = useState([]); - const [itemMappings, setItemMappings] = useState>({}); - const [editItemData, setEditItemData] = useState(null); + const [itemMappings, setItemMappings] = useState>>({}); + const [itemPrices, setItemPrices] = useState>>({}); + const [priceCategoryOptions, setPriceCategoryOptions] = useState>({}); + + // 거래처 모달 탭 + const [supplierModalTab, setSupplierModalTab] = useState<"basic" | "contacts" | "delivery">("basic"); + // 담당자 (supplier_contact) - 모달 내 + const [modalContacts, setModalContacts] = useState([]); + const [modalContactLoading, setModalContactLoading] = useState(false); + const [modalContactForm, setModalContactForm] = useState>({}); + const [modalContactEditId, setModalContactEditId] = useState(null); + const [modalContactFormOpen, setModalContactFormOpen] = useState(false); + const [modalContactSaving, setModalContactSaving] = useState(false); + // 납품처 (delivery_destination) - 모달 내 + const [modalDeliveries, setModalDeliveries] = useState([]); + const [modalDeliveryLoading, setModalDeliveryLoading] = useState(false); + const [modalDeliveryForm, setModalDeliveryForm] = useState>({}); + const [modalDeliveryEditId, setModalDeliveryEditId] = useState(null); + const [modalDeliveryFormOpen, setModalDeliveryFormOpen] = useState(false); + const [modalDeliverySaving, setModalDeliverySaving] = useState(false); + const [modalDeliveryFormErrors, setModalDeliveryFormErrors] = useState>({}); + + const [continuousInput, setContinuousInput] = useState(false); + + // 세금유형 (기본정보 탭 내) + const [taxTypeRows, setTaxTypeRows] = useState<{ _id: string; tax_type_name: string; rate: string }[]>([]); + const [taxTypeOptions, setTaxTypeOptions] = useState<{ code: string; label: string }[]>([]); // 엑셀 const [excelUploadOpen, setExcelUploadOpen] = useState(false); + const [excelChainConfig, setExcelChainConfig] = useState(null); + const [excelDetecting, setExcelDetecting] = useState(false); - // 테이블 설정 - const ts = useTableSettings("c16-supplier", SUPPLIER_TABLE, SUPPLIER_COLUMNS); + // 카테고리 + const [categoryOptions, setCategoryOptions] = useState>({}); + const [employeeOptions, setEmployeeOptions] = useState<{ user_id: string; user_name: string; position_name?: string }[]>([]); - // 좌측: 공급업체 조회 + + // 카테고리 로드 + useEffect(() => { + const flatten = (vals: any[]): { code: string; label: string }[] => { + const result: { code: string; label: string }[] = []; + for (const v of vals) { + result.push({ code: v.valueCode, label: v.valueLabel }); + if (v.children?.length) result.push(...flatten(v.children)); + } + return result; + }; + const load = async () => { + const optMap: Record = {}; + for (const col of ["division", "status"]) { + try { + const res = await apiClient.get(`/table-categories/${SUPPLIER_TABLE}/${col}/values`); + if (res.data?.success) optMap[col] = flatten(res.data.data || []); + } catch { /* skip */ } + } + setCategoryOptions(optMap); + + const priceOpts: Record = {}; + for (const col of ["base_price_type", "currency_code", "discount_type", "rounding_type", "rounding_unit_value"]) { + try { + const res = await apiClient.get(`/table-categories/${PRICE_TABLE}/${col}/values`); + if (res.data?.success) priceOpts[col] = flatten(res.data.data || []); + } catch { /* skip */ } + } + setPriceCategoryOptions(priceOpts); + + // 세금유형 카테고리 + try { + const taxRes = await apiClient.get(`/table-categories/supplier_tax_type/tax_type_name/values`); + if (taxRes.data?.success) setTaxTypeOptions(flatten(taxRes.data.data || [])); + } catch { /* skip */ } + }; + load(); + apiClient.post(`/table-management/tables/user_info/data`, { page: 1, size: 500, autoFilter: true }) + .then((res) => { + const users = res.data?.data?.data || res.data?.data?.rows || []; + setEmployeeOptions(users.map((u: any) => ({ + user_id: u.user_id, user_name: u.user_name || u.user_id, position_name: u.position_name, + }))); + }).catch(() => {}); + }, []); + + // 공급업체 목록 조회 const fetchSuppliers = useCallback(async () => { setSupplierLoading(true); try { - const filters = searchFilters.map((f) => ({ columnName: f.columnName, operator: f.operator, value: f.value })); + const filters = searchFilters.map(f => ({ + columnName: f.columnName, + operator: f.operator, + value: f.value, + })); + const res = await apiClient.post(`/table-management/tables/${SUPPLIER_TABLE}/data`, { page: 1, size: 500, dataFilter: filters.length > 0 ? { enabled: true, filters } : undefined, autoFilter: true, + sort: { columnName: "supplier_code", order: "desc" }, }); - setSuppliers(res.data?.data?.data || res.data?.data?.rows || []); - } catch { + const raw = res.data?.data?.data || res.data?.data?.rows || []; + setRawSuppliers(raw); + + const resolve = (col: string, code: string) => { + if (!code) return ""; + return categoryOptions[col]?.find((o) => o.code === code)?.label || code; + }; + const data = raw.map((r: any) => { + const mainContact = mainContactMap[r.id]; + return { + ...r, + division: resolve("division", r.division), + status: resolve("status", r.status), + contact_person: mainContact?.contact_name || "", + contact_phone: mainContact?.contact_phone || "", + email: mainContact?.contact_email || "", + }; + }); + // 공급업체코드 숫자 기준 내림차순 정렬 + data.sort((a: any, b: any) => { + const aNum = parseInt((a.supplier_code || "").replace(/\D/g, ""), 10) || 0; + const bNum = parseInt((b.supplier_code || "").replace(/\D/g, ""), 10) || 0; + return bNum - aNum; + }); + setSuppliers(data); + setSupplierCount(res.data?.data?.total || raw.length); + } catch (err) { + console.error("거래처 조회 실패:", err); toast.error("공급업체 목록을 불러오는데 실패했습니다."); } finally { setSupplierLoading(false); } - }, [searchFilters]); + }, [searchFilters, categoryOptions, employeeOptions, mainContactMap]); useEffect(() => { fetchSuppliers(); }, [fetchSuppliers]); - const selectedSupplier = suppliers.find((s) => s.id === selectedSupplierId); - const isColVisible = (key: string) => ts.isVisible(key); - const supplierColSpan = 2 + SUPPLIER_COLUMNS.filter((c) => isColVisible(c.key)).length; + // 메인 담당자 조회 (최초 1번 + 저장 후 갱신) + const fetchMainContacts = useCallback(async () => { + try { + const contactRes = await apiClient.post(`/table-management/tables/${CONTACT_TABLE}/data`, { + page: 1, size: 500, autoFilter: true, + dataFilter: { enabled: true, filters: [{ columnName: "is_main", operator: "equals", value: "Y" }] }, + }); + const allContacts = contactRes.data?.data?.data || contactRes.data?.data?.rows || []; + const map: Record = {}; + for (const c of allContacts) { + if ((c.is_main === "Y" || c.is_main === true) && c.supplier_id) { + map[c.supplier_id] = c; + } + } + setMainContactMap(map); + } catch { /* skip */ } + }, []); - const mainTableColumns = useMemo(() => { - const cols: EDataTableColumn[] = [ - { key: "supplier_code", label: "공급업체코드", width: "w-[120px]" }, - { key: "supplier_name", label: "공급업체명" }, - ]; - if (isColVisible("contact_person")) cols.push({ key: "contact_person", label: "담당자", width: "w-[90px]", render: (v) => v || "-" }); - if (isColVisible("contact_phone")) cols.push({ key: "contact_phone", label: "연락처", width: "w-[120px]", render: (v) => v || "-" }); - if (isColVisible("status")) cols.push({ - key: "status", label: "상태", width: "w-[70px]", align: "center", - render: (v) => ( - {v || "-"} - ), - }); - return cols; - }, [ts.visibleColumns]); // eslint-disable-line react-hooks/exhaustive-deps + useEffect(() => { fetchMainContacts(); }, [fetchMainContacts]); - // 우측: 품목 매핑 조회 + const selectedSupplier = suppliers.find((c) => c.id === selectedSupplierId); + + // 선택된 공급업체의 품목 단가 조회 useEffect(() => { - if (!selectedSupplier?.supplier_code) { setMappingItems([]); setMappingCheckedIds([]); return; } - setMappingCheckedIds([]); - const fetchMappings = async () => { - setMappingLoading(true); + if (!selectedSupplier?.supplier_code) { setPriceItems([]); setPriceCheckedIds([]); return; } + setPriceCheckedIds([]); + const fetchItems = async () => { + setPriceLoading(true); try { - const mapRes = await apiClient.post(`/table-management/tables/${MAPPING_TABLE}/data`, { + const mappingRes = await apiClient.post(`/table-management/tables/${MAPPING_TABLE}/data`, { page: 1, size: 500, - dataFilter: { enabled: true, filters: [{ columnName: "supplier_id", operator: "equals", value: selectedSupplier.supplier_code }] }, + dataFilter: { enabled: true, filters: [ + { columnName: "supplier_id", operator: "equals", value: selectedSupplier.supplier_code }, + ]}, autoFilter: true, }); - const mappings = mapRes.data?.data?.data || mapRes.data?.data?.rows || []; - const itemIds = [...new Set(mappings.map((m: any) => m.item_id).filter(Boolean))]; + const mappings = mappingRes.data?.data?.data || mappingRes.data?.data?.rows || []; + + const itemIds = [...new Set(mappings.map((r: any) => r.item_id).filter(Boolean))]; let itemMap: Record = {}; if (itemIds.length > 0) { try { @@ -147,356 +325,1384 @@ export default function SupplierManagementPage() { } } catch { /* skip */ } } - setMappingItems(mappings.map((m: any) => ({ - ...m, - item_number: m.item_id || "", - item_name: itemMap[m.item_id]?.item_name || "", - }))); - } catch { - toast.error("품목 정보를 불러오는데 실패했습니다."); + + let allPrices: any[] = []; + if (mappings.length > 0) { + try { + const priceRes = await apiClient.post(`/table-management/tables/${PRICE_TABLE}/data`, { + page: 1, size: 500, + dataFilter: { enabled: true, filters: [ + { columnName: "supplier_id", operator: "equals", value: selectedSupplier.supplier_code }, + ]}, + autoFilter: true, + }); + allPrices = priceRes.data?.data?.data || priceRes.data?.data?.rows || []; + } catch { /* skip */ } + } + + const priceResolve = (col: string, code: string) => { + if (!code) return ""; + return priceCategoryOptions[col]?.find((o) => o.code === code)?.label || code; + }; + const today = new Date().toISOString().split("T")[0]; + + // 품목 기준 그룹핑 — master: 첫 매핑 + 현재 단가, details: 전체 단가 리스트 + const grouped: Record = {}; + const flatItems: any[] = []; + const seenItemIds = new Set(); + for (const m of mappings) { + const itemKey = m.item_id || ""; + if (seenItemIds.has(itemKey)) continue; // 품목당 첫 매핑만 마스터 + seenItemIds.add(itemKey); + + const itemInfo = itemMap[itemKey] || {}; + const itemPriceList = allPrices + .filter((p: any) => p.item_id === itemKey) + .sort((a: any, b: any) => (a.start_date || "").localeCompare(b.start_date || "")); + const todayPrice = itemPriceList.find((p: any) => + (!p.start_date || p.start_date <= today) && (!p.end_date || p.end_date >= today) + ) || itemPriceList[0] || {}; + + const masterRow = { + ...m, + item_number: itemKey, + item_name: itemInfo.item_name || "", + base_price_type: priceResolve("base_price_type", todayPrice.base_price_type || ""), + base_price: todayPrice.base_price || "", + discount_type: priceResolve("discount_type", todayPrice.discount_type || ""), + discount_value: todayPrice.discount_value || "", + calculated_price: todayPrice.calculated_price || todayPrice.unit_price || "", + currency_code: priceResolve("currency_code", todayPrice.currency_code || ""), + }; + + // 단가 리스트 (라벨 변환) + const priceDetails = itemPriceList.map((p: any) => ({ + ...p, + base_price_type_label: priceResolve("base_price_type", p.base_price_type || ""), + discount_type_label: priceResolve("discount_type", p.discount_type || ""), + currency_label: priceResolve("currency_code", p.currency_code || ""), + is_current: (!p.start_date || p.start_date <= today) && (!p.end_date || p.end_date >= today), + })); + + grouped[itemKey] = { master: masterRow, details: priceDetails }; + flatItems.push(masterRow); + } + setPriceGroups(grouped); + setPriceItems(flatItems); + } catch (err) { + console.error("품목 조회 실패:", err); } finally { - setMappingLoading(false); + setPriceLoading(false); } }; - fetchMappings(); + fetchItems(); }, [selectedSupplier?.supplier_code]); - // 단가 자동 계산 - const calcPrice = (base: string, discType: string, discVal: string): string => { - const bp = Number(base) || 0; - const dv = Number(discVal) || 0; - if (discType === "rate") return String(Math.round(bp * (1 - dv / 100))); - if (discType === "amount") return String(Math.round(bp - dv)); - return String(bp); + // 납품처 조회 + useEffect(() => { + if (!selectedSupplier?.supplier_code) { setDeliveryItems([]); return; } + const fetchDelivery = async () => { + setDeliveryLoading(true); + try { + const res = await apiClient.post(`/table-management/tables/${DELIVERY_TABLE}/data`, { + page: 1, size: 500, + dataFilter: { enabled: true, filters: [ + { columnName: "supplier_code", operator: "equals", value: selectedSupplier.supplier_code }, + ]}, + autoFilter: true, + }); + setDeliveryItems(res.data?.data?.data || res.data?.data?.rows || []); + } catch { setDeliveryItems([]); } finally { setDeliveryLoading(false); } + }; + fetchDelivery(); + }, [selectedSupplier?.supplier_code]); + + const getCategoryLabel = (col: string, code: string) => { + if (!code) return ""; + return categoryOptions[col]?.find((o) => o.code === code)?.label || code; + }; + + // 모달 내 담당자 목록 조회 + const fetchModalContacts = useCallback(async (supplierId: string) => { + setModalContactLoading(true); + try { + const res = await apiClient.post(`/table-management/tables/${CONTACT_TABLE}/data`, { + page: 1, size: 200, + dataFilter: { enabled: true, filters: [{ columnName: "supplier_id", operator: "equals", value: supplierId }] }, + autoFilter: true, + }); + setModalContacts(res.data?.data?.data || res.data?.data?.rows || []); + } catch { setModalContacts([]); } finally { setModalContactLoading(false); } + }, []); + + // 모달 내 납품처 목록 조회 + const fetchModalDeliveries = useCallback(async (supplierCode: string) => { + setModalDeliveryLoading(true); + try { + const res = await apiClient.post(`/table-management/tables/${DELIVERY_TABLE}/data`, { + page: 1, size: 200, + dataFilter: { enabled: true, filters: [{ columnName: "supplier_code", operator: "equals", value: supplierCode }] }, + autoFilter: true, + }); + setModalDeliveries(res.data?.data?.data || res.data?.data?.rows || []); + } catch { setModalDeliveries([]); } finally { setModalDeliveryLoading(false); } + }, []); + + // 담당자 저장 (등록/수정) + const handleModalContactSave = async () => { + if (!modalContactForm.contact_name) { toast.error("담당자명은 필수입니다."); return; } + if (modalContactEditId) { + // 수정 — 로컬 리스트에서 교체 + setModalContacts((prev) => prev.map((c) => + c._localId === modalContactEditId ? { ...c, ...modalContactForm } : c + )); + } else { + // 추가 — 로컬 리스트에 카드 추가 + setModalContacts((prev) => [...prev, { + ...modalContactForm, + _localId: `local_${Date.now()}_${Math.random()}`, + _isNew: true, + }]); + } + setModalContactFormOpen(false); + setModalContactForm({}); + setModalContactEditId(null); + }; + + // 담당자 삭제 + const handleModalContactDelete = (contactId: string) => { + setModalContacts((prev) => prev.filter((c) => (c._localId || c.id) !== contactId)); + }; + + // 납품처 자동채번 유틸 + const generateDeliveryCode = async () => { + try { + const ruleRes = await apiClient.get(`/numbering-rules/by-column/${DELIVERY_TABLE}/destination_code`); + const ruleData = ruleRes.data; + if (ruleData?.success && ruleData?.data?.ruleId) { + const ruleId = ruleData.data.ruleId; + const allRes = await apiClient.post(`/table-management/tables/${DELIVERY_TABLE}/data`, { + page: 1, size: 500, autoFilter: true, + sort: { columnName: "destination_code", order: "desc" }, + }); + const allRows = allRes.data?.data?.data || allRes.data?.data?.rows || []; + let maxSeq = 0; + for (const row of allRows) { + const match = (row.destination_code || "").match(/(\d+)$/); + if (match) { const seq = parseInt(match[1], 10); if (seq > maxSeq) maxSeq = seq; } + } + // 로컬에 추가된 것도 포함 + for (const d of modalDeliveries) { + const match = (d.destination_code || "").match(/(\d+)$/); + if (match) { const seq = parseInt(match[1], 10); if (seq > maxSeq) maxSeq = seq; } + } + const previewRes = await previewNumberingCode(ruleId); + if (previewRes.success && previewRes.data?.generatedCode) { + const previewCode = previewRes.data.generatedCode; + const prefix = previewCode.replace(/\d+$/, ""); + const seqLen = (previewCode.match(/(\d+)$/) || ["", "001"])[1].length; + return prefix + String(maxSeq + 1).padStart(seqLen, "0"); + } + } + } catch { /* skip */ } + return ""; + }; + + // 납품처 저장 (모달 내) + const handleModalDeliverySave = async () => { + if (!modalDeliveryForm.destination_name) { toast.error("납품처명은 필수입니다."); return; } + if (modalDeliveryEditId) { + setModalDeliveries((prev) => prev.map((d) => + (d._localId || d.id) === modalDeliveryEditId ? { ...d, ...modalDeliveryForm } : d + )); + } else { + setModalDeliveries((prev) => [...prev, { + ...modalDeliveryForm, + _localId: `local_${Date.now()}_${Math.random()}`, + _isNew: true, + }]); + } + setModalDeliveryFormOpen(false); + setModalDeliveryForm({}); + setModalDeliveryEditId(null); + setModalDeliveryFormErrors({}); + }; + + const handleModalDeliveryDelete = (deliveryId: string) => { + setModalDeliveries((prev) => prev.filter((d) => (d._localId || d.id) !== deliveryId)); + }; + + // 공급업체 등록 모달 열기 + const openSupplierRegister = async () => { + setSupplierForm({}); + setFormErrors({}); + setSupplierEditMode(false); + setSupplierModalTab("basic"); + setModalContacts([]); + setModalDeliveries([]); + setModalContactFormOpen(false); + setModalDeliveryFormOpen(false); + setTaxTypeRows([]); + setSupplierModalOpen(true); + // 거래처 코드 자동 채번 — 기존 데이터 max값 기반 + try { + const ruleRes = await apiClient.get(`/numbering-rules/by-column/${SUPPLIER_TABLE}/supplier_code`); + const ruleData = ruleRes.data; + if (ruleData?.success && ruleData?.data?.ruleId) { + const ruleId = ruleData.data.ruleId; + // 기존 데이터에서 CUST-XXX 패턴의 최대 순번 조회 + const allRes = await apiClient.post(`/table-management/tables/${SUPPLIER_TABLE}/data`, { + page: 1, size: 500, autoFilter: true, + sort: { columnName: "supplier_code", order: "desc" }, + }); + const allRows = allRes.data?.data?.data || allRes.data?.data?.rows || []; + let maxSeq = 0; + for (const row of allRows) { + const code = row.supplier_code || ""; + const match = code.match(/(\d+)$/); + if (match) { + const seq = parseInt(match[1], 10); + if (seq > maxSeq) maxSeq = seq; + } + } + // preview로 접두어 패턴 가져오기 + const previewRes = await previewNumberingCode(ruleId); + if (previewRes.success && previewRes.data?.generatedCode) { + const previewCode = previewRes.data.generatedCode; + const prefix = previewCode.replace(/\d+$/, ""); + const seqLength = (previewCode.match(/(\d+)$/) || ["", "001"])[1].length; + const nextSeq = maxSeq + 1; + const nextCode = prefix + String(nextSeq).padStart(seqLength, "0"); + setSupplierForm((prev) => ({ ...prev, supplier_code: nextCode, _numberingRuleId: ruleId })); + } + } + } catch { /* skip */ } }; - const openSupplierRegister = () => { setSupplierForm({}); setSupplierEditMode(false); setSupplierModalOpen(true); }; const openSupplierEdit = () => { if (!selectedSupplier) return; - setSupplierForm({ ...selectedSupplier }); + const rawData = rawSuppliers.find((c) => c.id === selectedSupplierId); + setSupplierForm({ ...(rawData || selectedSupplier) }); + setFormErrors({}); setSupplierEditMode(true); + setSupplierModalTab("basic"); + setModalContactFormOpen(false); + setModalDeliveryFormOpen(false); + setModalContactForm({}); + setModalDeliveryForm({}); + setModalContactEditId(null); + setModalDeliveryEditId(null); + // 수정 모드에서는 바로 조회 + const code = (rawData || selectedSupplier).supplier_code; + const id = (rawData || selectedSupplier).id; + if (id) { + fetchModalContacts(id); + // 세금유형 로드 + apiClient.post(`/table-management/tables/supplier_tax_type/data`, { + page: 1, size: 100, + dataFilter: { enabled: true, filters: [{ columnName: "supplier_id", operator: "equals", value: id }] }, + autoFilter: true, + }).then((res: any) => { + const rows = res.data?.data?.data || res.data?.data?.rows || []; + setTaxTypeRows(rows.map((r: any) => ({ _id: r.id, tax_type_name: r.tax_type_name || "", rate: String(r.rate || "") }))); + }).catch(() => setTaxTypeRows([])); + } + if (code) fetchModalDeliveries(code); setSupplierModalOpen(true); }; + // 폼 필드 변경 시 자동 포맷팅 + 실시간 검증 + const handleFormChange = (field: string, value: string) => { + const formatted = formatField(field, value); + setSupplierForm((prev) => ({ ...prev, [field]: formatted })); + const error = validateField(field, formatted); + setFormErrors((prev) => { + const next = { ...prev }; + if (error) next[field] = error; else delete next[field]; + return next; + }); + }; + + // 세금유형/담당자/납품처를 한번에 저장하는 헬퍼 + const saveSubTables = async (supplierId: string, supplierCode: string) => { + // 세금유형 — 기존 삭제 후 재생성 + try { + const existTax = await apiClient.post(`/table-management/tables/supplier_tax_type/data`, { + page: 1, size: 100, + dataFilter: { enabled: true, filters: [{ columnName: "supplier_id", operator: "equals", value: supplierId }] }, + autoFilter: true, + }); + const existRows = existTax.data?.data?.data || existTax.data?.data?.rows || []; + if (existRows.length > 0) { + await apiClient.delete(`/table-management/tables/supplier_tax_type/delete`, { + data: existRows.map((r: any) => ({ id: r.id })), + }); + } + for (const t of taxTypeRows.filter((r) => r.tax_type_name)) { + await apiClient.post(`/table-management/tables/supplier_tax_type/add`, { + id: crypto.randomUUID(), supplier_id: supplierId, + tax_type_name: t.tax_type_name, tax_type_id: t.tax_type_name, + rate: t.rate ? Number(t.rate) : 0, + }); + } + } catch { /* skip */ } + + // 담당자 — 기존 삭제 후 전체 재생성 + try { + const existContacts = await apiClient.post(`/table-management/tables/${CONTACT_TABLE}/data`, { + page: 1, size: 100, + dataFilter: { enabled: true, filters: [{ columnName: "supplier_id", operator: "equals", value: supplierId }] }, + autoFilter: true, + }); + const existCRows = existContacts.data?.data?.data || existContacts.data?.data?.rows || []; + if (existCRows.length > 0) { + await apiClient.delete(`/table-management/tables/${CONTACT_TABLE}/delete`, { + data: existCRows.map((r: any) => ({ id: r.id })), + }); + } + } catch { /* skip */ } + for (const c of modalContacts) { + try { + await apiClient.post(`/table-management/tables/${CONTACT_TABLE}/add`, { + id: crypto.randomUUID(), supplier_id: supplierId, + contact_name: c.contact_name || "", contact_phone: c.contact_phone || "", + contact_email: c.contact_email || "", department: c.department || "", + is_main: c.is_main || "N", memo: c.memo || "", + }); + } catch { /* skip */ } + } + + // 납품처 — 기존 삭제 후 전체 재생성 + try { + const existDeliveries = await apiClient.post(`/table-management/tables/${DELIVERY_TABLE}/data`, { + page: 1, size: 100, + dataFilter: { enabled: true, filters: [{ columnName: "supplier_code", operator: "equals", value: supplierCode }] }, + autoFilter: true, + }); + const existDRows = existDeliveries.data?.data?.data || existDeliveries.data?.data?.rows || []; + if (existDRows.length > 0) { + await apiClient.delete(`/table-management/tables/${DELIVERY_TABLE}/delete`, { + data: existDRows.map((r: any) => ({ id: r.id })), + }); + } + } catch { /* skip */ } + for (const d of modalDeliveries) { + try { + await apiClient.post(`/table-management/tables/${DELIVERY_TABLE}/add`, { + id: crypto.randomUUID(), supplier_code: supplierCode, + destination_code: d.destination_code || "", destination_name: d.destination_name || "", + address: d.address || "", manager_name: d.manager_name || "", + phone: d.phone || "", memo: d.memo || "", is_default: d.is_default || "N", + }); + } catch { /* skip */ } + } + }; + const handleSupplierSave = async () => { if (!supplierForm.supplier_name) { toast.error("공급업체명은 필수입니다."); return; } + if (!supplierForm.status) { toast.error("상태는 필수입니다."); return; } + const errors = validateForm(supplierForm, ["contact_phone", "email", "business_number"]); + setFormErrors(errors); + if (Object.keys(errors).length > 0) { + toast.error("입력 형식을 확인해주세요."); + return; + } setSaving(true); try { - const { id, created_date, updated_date, writer, company_code, status: _s, ...fields } = supplierForm; + const { id, created_date, updated_date, writer, company_code, _numberingRuleId, ...fields } = supplierForm; const cleanFields: Record = {}; - for (const [key, value] of Object.entries(fields)) cleanFields[key] = value === "" ? null : value; - if (supplierEditMode && id) { - await apiClient.put(`/table-management/tables/${SUPPLIER_TABLE}/edit`, { originalData: { id }, updatedData: cleanFields }); - toast.success("수정되었습니다."); - } else { - await apiClient.post(`/table-management/tables/${SUPPLIER_TABLE}/add`, { id: crypto.randomUUID(), ...cleanFields }); - toast.success("등록되었습니다."); + for (const [key, value] of Object.entries(fields)) { + cleanFields[key] = value === "" ? null : value; } - setSupplierModalOpen(false); + + if (supplierEditMode && id) { + // 수정 + await apiClient.put(`/table-management/tables/${SUPPLIER_TABLE}/edit`, { + originalData: { id }, updatedData: cleanFields, + }); + await saveSubTables(id, cleanFields.supplier_code || supplierForm.supplier_code); + toast.success("저장되었습니다."); + } else { + // 신규 등록 + await apiClient.post(`/table-management/tables/${SUPPLIER_TABLE}/add`, cleanFields); + // id 획득 + const res = await apiClient.post(`/table-management/tables/${SUPPLIER_TABLE}/data`, { + page: 1, size: 1, + dataFilter: { enabled: true, filters: [{ columnName: "supplier_code", operator: "equals", value: cleanFields.supplier_code }] }, + autoFilter: true, + }); + const newRow = (res.data?.data?.data || res.data?.data?.rows || [])[0]; + if (newRow?.id) { + await saveSubTables(newRow.id, cleanFields.supplier_code); + } + toast.success("공급업체가 등록되었습니다."); + } + fetchSuppliers(); + fetchMainContacts(); + if (!supplierEditMode && continuousInput) { + // 연속입력 — 폼 초기화하고 모달 유지 + setSupplierForm({}); + setModalContacts([]); + setModalDeliveries([]); + setTaxTypeRows([]); + setSupplierModalTab("basic"); + // 새 코드 채번 + try { + const ruleRes = await apiClient.get(`/numbering-rules/by-column/${SUPPLIER_TABLE}/supplier_code`); + const ruleData = ruleRes.data; + if (ruleData?.success && ruleData?.data?.ruleId) { + const ruleId = ruleData.data.ruleId; + const allRes = await apiClient.post(`/table-management/tables/${SUPPLIER_TABLE}/data`, { page: 1, size: 500, autoFilter: true, sort: { columnName: "supplier_code", order: "desc" } }); + const allRows = allRes.data?.data?.data || allRes.data?.data?.rows || []; + let maxSeq = 0; + for (const row of allRows) { const match = (row.supplier_code || "").match(/(\d+)$/); if (match) { const seq = parseInt(match[1], 10); if (seq > maxSeq) maxSeq = seq; } } + const previewRes = await previewNumberingCode(ruleId); + if (previewRes.success && previewRes.data?.generatedCode) { + const prefix = previewRes.data.generatedCode.replace(/\d+$/, ""); + const seqLen = (previewRes.data.generatedCode.match(/(\d+)$/) || ["", "001"])[1].length; + setSupplierForm({ supplier_code: prefix + String(maxSeq + 1).padStart(seqLen, "0") }); + } + } + } catch { /* skip */ } + toast.success("등록 완료. 다음 공급업체를 입력하세요."); + } else { + setSupplierModalOpen(false); + // 우측 패널 갱신 + if (selectedSupplierId) { + const cid = selectedSupplierId; + setSelectedSupplierId(null); + setTimeout(() => setSelectedSupplierId(cid), 50); + } + } } catch (err: any) { toast.error(err.response?.data?.message || "저장에 실패했습니다."); - } finally { setSaving(false); } + } finally { + setSaving(false); + } }; + // 공급업체 삭제 const handleSupplierDelete = async () => { if (!selectedSupplierId) return; - const ok = await confirm("공급업체를 삭제하시겠습니까?", { description: "관련된 품목 매핑 정보도 함께 삭제됩니다.", variant: "destructive", confirmText: "삭제" }); + const ok = await confirm("공급업체를 삭제하시겠습니까?", { + variant: "destructive", confirmText: "삭제", + }); if (!ok) return; try { - await apiClient.delete(`/table-management/tables/${SUPPLIER_TABLE}/delete`, { data: [{ id: selectedSupplierId }] }); + await apiClient.delete(`/table-management/tables/${SUPPLIER_TABLE}/delete`, { + data: [{ id: selectedSupplierId }], + }); toast.success("삭제되었습니다."); setSelectedSupplierId(null); fetchSuppliers(); } catch { toast.error("삭제에 실패했습니다."); } }; + // 품목 검색 const searchItems = async () => { setItemSearchLoading(true); try { const filters: any[] = []; if (itemSearchKeyword) filters.push({ columnName: "item_name", operator: "contains", value: itemSearchKeyword }); const res = await apiClient.post(`/table-management/tables/item_info/data`, { - page: 1, size: 50, + page: 1, size: 500, dataFilter: filters.length > 0 ? { enabled: true, filters } : undefined, autoFilter: true, }); const allItems = res.data?.data?.data || res.data?.data?.rows || []; - const existingItemIds = new Set(mappingItems.map((m: any) => m.item_id)); - setItemSearchResults(allItems.filter((item: any) => !existingItemIds.has(item.item_number))); + const existingItemIds = new Set(priceItems.map((p: any) => p.item_id || p.item_number)); + const PURCHASE_CODE = "CAT_MMDJB7R4_TO3T"; + setItemSearchResults(allItems.filter((item: any) => { + if (existingItemIds.has(item.item_number) || existingItemIds.has(item.id)) return false; + const div = item.division || ""; + return div.includes(PURCHASE_CODE) || div.includes("구매"); + })); } catch { /* skip */ } finally { setItemSearchLoading(false); } }; + // 품목 선택 완료 → 상세 입력 모달로 전환 const goToItemDetail = () => { const selected = itemSearchResults.filter((i) => itemCheckedIds.has(i.id)); if (selected.length === 0) { toast.error("품목을 선택해주세요."); return; } setSelectedItemsForDetail(selected); const mappings: typeof itemMappings = {}; + const prices: typeof itemPrices = {}; for (const item of selected) { const key = item.item_number || item.id; - mappings[key] = { - supplier_item_code: "", supplier_item_name: "", - base_price: item.standard_price || "", discount_type: "none", - discount_value: "", calculated_price: item.standard_price || "", - currency_code: "", start_date: "", end_date: "", - lead_time_days: "", min_order_qty: "", - }; + mappings[key] = []; + prices[key] = [{ + _id: `p_${Date.now()}_${Math.random()}`, + start_date: "", end_date: "", currency_code: "CAT_MLAMDKVN_PZJI", + base_price_type: "CAT_MLAMFGFT_4RZW", base_price: item.standard_price || item.selling_price || "", + discount_type: "", discount_value: "", rounding_type: "", rounding_unit_value: "", + calculated_price: item.standard_price || item.selling_price || "", + }]; } setItemMappings(mappings); + setItemPrices(prices); setItemSelectOpen(false); - setEditItemData(null); setItemDetailOpen(true); }; - const updateMapping = (itemKey: string, field: string, value: string) => { + // 거래처 품번/품명 행 추가 + const addMappingRow = (itemKey: string) => { + setItemMappings((prev) => ({ + ...prev, + [itemKey]: [...(prev[itemKey] || []), { _id: `m_${Date.now()}_${Math.random()}`, supplier_item_code: "", supplier_item_name: "" }], + })); + }; + + const removeMappingRow = (itemKey: string, rowId: string) => { + setItemMappings((prev) => ({ + ...prev, + [itemKey]: (prev[itemKey] || []).filter((r) => r._id !== rowId), + })); + }; + + const handleMappingDragEnd = (itemKey: string, event: DragEndEvent) => { + const { active, over } = event; + if (!over || active.id === over.id) return; setItemMappings((prev) => { - const cur = prev[itemKey] || {} as any; - const updated = { ...cur, [field]: value }; - if (["base_price", "discount_type", "discount_value"].includes(field)) { - updated.calculated_price = calcPrice(updated.base_price, updated.discount_type, updated.discount_value); - } - return { ...prev, [itemKey]: updated }; + const arr = [...(prev[itemKey] || [])]; + const oldIdx = arr.findIndex((r) => r._id === active.id); + const newIdx = arr.findIndex((r) => r._id === over.id); + return { ...prev, [itemKey]: arrayMove(arr, oldIdx, newIdx) }; }); }; - const openEditItem = (row: any) => { - const itemKey = row.item_id || row.item_number; - setSelectedItemsForDetail([{ item_number: itemKey, item_name: row.item_name || "" }]); - setItemMappings({ - [itemKey]: { - supplier_item_code: row.supplier_item_code || "", - supplier_item_name: row.supplier_item_name || "", - base_price: row.base_price ? String(row.base_price) : "", - discount_type: row.discount_type || "none", - discount_value: row.discount_value ? String(row.discount_value) : "", - calculated_price: row.calculated_price ? String(row.calculated_price) : "", - currency_code: row.currency_code || "", - start_date: row.start_date ? String(row.start_date).split("T")[0] : "", - end_date: row.end_date ? String(row.end_date).split("T")[0] : "", - lead_time_days: row.lead_time_days ? String(row.lead_time_days) : "", - min_order_qty: row.min_order_qty ? String(row.min_order_qty) : "", - }, - }); + const updateMappingRow = (itemKey: string, rowId: string, field: string, value: string) => { + setItemMappings((prev) => ({ + ...prev, + [itemKey]: (prev[itemKey] || []).map((r) => r._id === rowId ? { ...r, [field]: value } : r), + })); + }; + + // 단가 행 추가 + const addPriceRow = (itemKey: string) => { + setItemPrices((prev) => ({ + ...prev, + [itemKey]: [...(prev[itemKey] || []), { + _id: `p_${Date.now()}_${Math.random()}`, + start_date: "", end_date: "", currency_code: "CAT_MLAMDKVN_PZJI", + base_price_type: "CAT_MLAMFGFT_4RZW", base_price: "", + discount_type: "", discount_value: "", rounding_type: "", rounding_unit_value: "", + calculated_price: "", + }], + })); + }; + + const removePriceRow = (itemKey: string, rowId: string) => { + setItemPrices((prev) => ({ + ...prev, + [itemKey]: (prev[itemKey] || []).filter((r) => r._id !== rowId), + })); + }; + + const updatePriceRow = (itemKey: string, rowId: string, field: string, value: string) => { + setItemPrices((prev) => ({ + ...prev, + [itemKey]: (prev[itemKey] || []).map((r) => { + if (r._id !== rowId) return r; + const updated = { ...r, [field]: value }; + if (["base_price", "discount_type", "discount_value", "rounding_unit_value"].includes(field)) { + const bp = Number(updated.base_price) || 0; + const dv = Number(updated.discount_value) || 0; + const dt = updated.discount_type; + let calc = bp; + if (dt === "CAT_MLAMBEC8_URQA") calc = bp * (1 - dv / 100); + else if (dt === "CAT_MLAMBLFM_JTLO") calc = bp - dv; + // 절삭/반올림 적용 + const rv = updated.rounding_unit_value; + const roundOpts = priceCategoryOptions["rounding_unit_value"] || []; + const roundLabel = roundOpts.find((o) => o.code === rv)?.label || ""; + if (roundLabel.includes("절삭") || roundLabel.includes("버림") || roundLabel.includes("floor")) { + calc = Math.floor(calc); + } else if (roundLabel.includes("올림") || roundLabel.includes("ceil")) { + calc = Math.ceil(calc); + } else if (roundLabel.includes("반올림") || roundLabel.includes("round")) { + calc = Math.round(calc); + } else if (rv) { + // 단위 값 기반 (예: 10원 단위 절삭) + const unit = Number(rv); + if (!isNaN(unit) && unit > 0) calc = Math.floor(calc / unit) * unit; + } + updated.calculated_price = String(Math.floor(calc)); + } + return updated; + }), + })); + }; + + // 품목 편집 열기 + const openEditItem = async (row: any) => { + const itemKey = row.item_number || row.item_id; + let itemInfo: any = { item_number: itemKey, item_name: row.item_name || "", size: "", unit: "" }; + try { + const res = await apiClient.post(`/table-management/tables/item_info/data`, { + page: 1, size: 1, + dataFilter: { enabled: true, filters: [{ columnName: "item_number", operator: "equals", value: itemKey }] }, + autoFilter: true, + }); + const found = (res.data?.data?.data || res.data?.data?.rows || [])[0]; + if (found) itemInfo = found; + } catch { /* skip */ } + + let mappingRows: any[] = []; + try { + const mapRes = await apiClient.post(`/table-management/tables/${MAPPING_TABLE}/data`, { + page: 1, size: 100, + dataFilter: { enabled: true, filters: [ + { columnName: "supplier_id", operator: "equals", value: selectedSupplier!.supplier_code }, + { columnName: "item_id", operator: "equals", value: itemKey }, + ]}, autoFilter: true, + }); + const allMappings = mapRes.data?.data?.data || mapRes.data?.data?.rows || []; + mappingRows = allMappings + .filter((m: any) => m.supplier_item_code || m.supplier_item_name) + .map((m: any) => ({ + _id: `m_existing_${m.id}`, + supplier_item_code: m.supplier_item_code || "", + supplier_item_name: m.supplier_item_name || "", + })); + } catch { /* skip */ } + + let priceRows: any[] = []; + try { + const priceRes = await apiClient.post(`/table-management/tables/${PRICE_TABLE}/data`, { + page: 1, size: 100, + dataFilter: { enabled: true, filters: [ + { columnName: "supplier_id", operator: "equals", value: selectedSupplier!.supplier_code }, + { columnName: "item_id", operator: "equals", value: itemKey }, + ]}, autoFilter: true, + }); + const allPriceData = priceRes.data?.data?.data || priceRes.data?.data?.rows || []; + priceRows = allPriceData.map((p: any) => ({ + _id: `p_existing_${p.id}`, + start_date: p.start_date ? String(p.start_date).split("T")[0] : "", + end_date: p.end_date ? String(p.end_date).split("T")[0] : "", + currency_code: p.currency_code || "CAT_MLAMDKVN_PZJI", + base_price_type: p.base_price_type || "CAT_MLAMFGFT_4RZW", + base_price: p.base_price ? String(p.base_price) : "", + discount_type: p.discount_type || "", + discount_value: p.discount_value ? String(p.discount_value) : "", + rounding_type: p.rounding_type || "", + rounding_unit_value: p.rounding_unit_value || "", + calculated_price: p.calculated_price ? String(p.calculated_price) : "", + })); + } catch { /* skip */ } + + if (priceRows.length === 0) { + priceRows.push({ + _id: `p_${Date.now()}`, start_date: "", end_date: "", currency_code: "CAT_MLAMDKVN_PZJI", + base_price_type: "CAT_MLAMFGFT_4RZW", base_price: "", discount_type: "", discount_value: "", + rounding_type: "", rounding_unit_value: "", calculated_price: "", + }); + } + + setSelectedItemsForDetail([itemInfo]); + setItemMappings({ [itemKey]: mappingRows }); + setItemPrices({ [itemKey]: priceRows }); setEditItemData(row); setItemDetailOpen(true); }; const handleItemDetailSave = async () => { if (!selectedSupplier) return; - const isEdit = !!editItemData; + if (savingRef.current) return; + savingRef.current = true; + const isEditingExisting = !!editItemData; setSaving(true); try { for (const item of selectedItemsForDetail) { const itemKey = item.item_number || item.id; - const m = itemMappings[itemKey]; - if (!m) continue; - const fields: Record = { - supplier_id: selectedSupplier.supplier_code, item_id: itemKey, - supplier_item_code: m.supplier_item_code || null, - supplier_item_name: m.supplier_item_name || null, - base_price: m.base_price ? Number(m.base_price) : null, - discount_type: m.discount_type === "none" ? null : m.discount_type || null, - discount_value: m.discount_value ? Number(m.discount_value) : null, - calculated_price: m.calculated_price ? Number(m.calculated_price) : null, - currency_code: m.currency_code || null, - start_date: m.start_date || null, - end_date: m.end_date || null, - lead_time_days: m.lead_time_days ? Number(m.lead_time_days) : null, - min_order_qty: m.min_order_qty ? Number(m.min_order_qty) : null, - }; - if (isEdit && editItemData?.id) { - await apiClient.put(`/table-management/tables/${MAPPING_TABLE}/edit`, { originalData: { id: editItemData.id }, updatedData: fields }); + const mappingRows = itemMappings[itemKey] || []; + + if (isEditingExisting && editItemData?.id) { + // 기존 매핑 조회 + let existingMaps: any[] = []; + try { + const existingMappings = await apiClient.post(`/table-management/tables/${MAPPING_TABLE}/data`, { + page: 1, size: 100, + dataFilter: { enabled: true, filters: [ + { columnName: "supplier_id", operator: "equals", value: selectedSupplier.supplier_code }, + { columnName: "item_id", operator: "equals", value: itemKey }, + ]}, autoFilter: true, + }); + existingMaps = existingMappings.data?.data?.data || existingMappings.data?.data?.rows || []; + } catch { /* skip */ } + + // 매핑 upsert: 기존 것은 update, 새 것은 insert, 남은 것은 delete + const usedExistingIds = new Set(); + let firstMappingId: string | null = editItemData.id; + for (let mi = 0; mi < mappingRows.length; mi++) { + const existMap = existingMaps[mi]; + if (existMap) { + // update + await apiClient.put(`/table-management/tables/${MAPPING_TABLE}/edit`, { + originalData: { id: existMap.id }, + updatedData: { + supplier_item_code: mappingRows[mi].supplier_item_code || "", + supplier_item_name: mappingRows[mi].supplier_item_name || "", + }, + }); + usedExistingIds.add(existMap.id); + if (mi === 0) firstMappingId = existMap.id; + } else { + // insert + const mRes = await apiClient.post(`/table-management/tables/${MAPPING_TABLE}/add`, { + id: crypto.randomUUID(), + supplier_id: selectedSupplier.supplier_code, item_id: itemKey, + supplier_item_code: mappingRows[mi].supplier_item_code || "", + supplier_item_name: mappingRows[mi].supplier_item_name || "", + }); + if (mi === 0 && !firstMappingId) firstMappingId = mRes.data?.data?.id || null; + } + } + // 초과분 delete + const toDeleteMaps = existingMaps.filter((m) => !usedExistingIds.has(m.id)); + if (toDeleteMaps.length > 0) { + await apiClient.delete(`/table-management/tables/${MAPPING_TABLE}/delete`, { + data: toDeleteMaps.map((m: any) => ({ id: m.id })), + }); + } + + // 기존 단가 조회 + let existingPriceRows: any[] = []; + try { + const existingPrices = await apiClient.post(`/table-management/tables/${PRICE_TABLE}/data`, { + page: 1, size: 100, + dataFilter: { enabled: true, filters: [ + { columnName: "supplier_id", operator: "equals", value: selectedSupplier.supplier_code }, + { columnName: "item_id", operator: "equals", value: itemKey }, + ]}, autoFilter: true, + }); + existingPriceRows = existingPrices.data?.data?.data || existingPrices.data?.data?.rows || []; + } catch { /* skip */ } + + // 단가 upsert + const priceRows = (itemPrices[itemKey] || []).filter((p) => + (p.base_price && Number(p.base_price) > 0) || p.start_date + ); + const usedPriceIds = new Set(); + for (let pi = 0; pi < priceRows.length; pi++) { + const price = priceRows[pi]; + const priceData = { + mapping_id: firstMappingId || editItemData.id, + supplier_id: selectedSupplier.supplier_code, item_id: itemKey, + start_date: price.start_date || null, end_date: price.end_date || null, + currency_code: price.currency_code || null, base_price_type: price.base_price_type || null, + base_price: price.base_price ? Number(price.base_price) : null, + unit_price: price.calculated_price ? Number(price.calculated_price) : (price.base_price ? Number(price.base_price) : null), + discount_type: price.discount_type || null, discount_value: price.discount_value ? Number(price.discount_value) : null, + rounding_type: price.rounding_type || null, rounding_unit_value: price.rounding_unit_value || null, + calculated_price: price.calculated_price ? Number(price.calculated_price) : null, + }; + const existPrice = existingPriceRows[pi]; + if (existPrice) { + await apiClient.put(`/table-management/tables/${PRICE_TABLE}/edit`, { + originalData: { id: existPrice.id }, + updatedData: priceData, + }); + usedPriceIds.add(existPrice.id); + } else { + await apiClient.post(`/table-management/tables/${PRICE_TABLE}/add`, { + id: crypto.randomUUID(), ...priceData, + }); + } + } + // 초과분 delete + const toDeletePrices = existingPriceRows.filter((p) => !usedPriceIds.has(p.id)); + if (toDeletePrices.length > 0) { + await apiClient.delete(`/table-management/tables/${PRICE_TABLE}/delete`, { + data: toDeletePrices.map((p: any) => ({ id: p.id })), + }); + } } else { - await apiClient.post(`/table-management/tables/${MAPPING_TABLE}/add`, { id: crypto.randomUUID(), ...fields }); + if (!mappingRows.length || !mappingRows[0]?.supplier_item_code) { + const existingCheck = await apiClient.post(`/table-management/tables/${MAPPING_TABLE}/data`, { + page: 1, size: 1, + dataFilter: { enabled: true, filters: [ + { columnName: "supplier_id", operator: "equals", value: selectedSupplier.supplier_code }, + { columnName: "item_id", operator: "equals", value: itemKey }, + ]}, autoFilter: true, + }); + if ((existingCheck.data?.data?.data || existingCheck.data?.data?.rows || []).length > 0) { + toast.warning(`${item.item_name || itemKey} 품목은 이미 등록되어 있습니다.`); + continue; + } + } + + let mappingId: string | null = null; + const mappingRes = await apiClient.post(`/table-management/tables/${MAPPING_TABLE}/add`, { + id: crypto.randomUUID(), + supplier_id: selectedSupplier.supplier_code, item_id: itemKey, + supplier_item_code: mappingRows[0]?.supplier_item_code || "", + supplier_item_name: mappingRows[0]?.supplier_item_name || "", + }); + mappingId = mappingRes.data?.data?.id || null; + + for (let mi = 1; mi < mappingRows.length; mi++) { + await apiClient.post(`/table-management/tables/${MAPPING_TABLE}/add`, { + id: crypto.randomUUID(), + supplier_id: selectedSupplier.supplier_code, item_id: itemKey, + supplier_item_code: mappingRows[mi].supplier_item_code || "", + supplier_item_name: mappingRows[mi].supplier_item_name || "", + }); + } + + const priceRows = (itemPrices[itemKey] || []).filter((p) => + (p.base_price && Number(p.base_price) > 0) || p.start_date + ); + for (const price of priceRows) { + await apiClient.post(`/table-management/tables/${PRICE_TABLE}/add`, { + id: crypto.randomUUID(), + mapping_id: mappingId || "", supplier_id: selectedSupplier.supplier_code, item_id: itemKey, + start_date: price.start_date || null, end_date: price.end_date || null, + currency_code: price.currency_code || null, base_price_type: price.base_price_type || null, + base_price: price.base_price ? Number(price.base_price) : null, + unit_price: price.calculated_price ? Number(price.calculated_price) : (price.base_price ? Number(price.base_price) : null), + discount_type: price.discount_type || null, discount_value: price.discount_value ? Number(price.discount_value) : null, + rounding_type: price.rounding_type || null, rounding_unit_value: price.rounding_unit_value || null, + calculated_price: price.calculated_price ? Number(price.calculated_price) : null, + }); + } } } - toast.success(isEdit ? "수정되었습니다." : `${selectedItemsForDetail.length}개 품목이 추가되었습니다.`); + toast.success(isEditingExisting ? "수정되었습니다." : `${selectedItemsForDetail.length}개 품목이 추가되었습니다.`); setItemDetailOpen(false); setEditItemData(null); setItemCheckedIds(new Set()); - const sid = selectedSupplierId; + const cid = selectedSupplierId; setSelectedSupplierId(null); - setTimeout(() => setSelectedSupplierId(sid), 50); + setTimeout(() => setSelectedSupplierId(cid), 50); } catch (err: any) { toast.error(err.response?.data?.message || "저장에 실패했습니다."); - } finally { setSaving(false); } + } finally { + setSaving(false); + savingRef.current = false; + } }; - const handleMappingDelete = async () => { - if (mappingCheckedIds.length === 0) return; - const ok = await confirm(`선택한 ${mappingCheckedIds.length}개 품목 매핑을 삭제하시겠습니까?`, { variant: "destructive", confirmText: "삭제" }); + // 품목 매핑 삭제 + const handlePriceItemDelete = async () => { + if (priceCheckedIds.length === 0) return; + const ok = await confirm(`선택한 ${priceCheckedIds.length}개 품목 매핑을 삭제하시겠습니까?`, { + description: "관련된 단가 정보도 함께 삭제됩니다.", + variant: "destructive", confirmText: "삭제", + }); if (!ok) return; try { - await apiClient.delete(`/table-management/tables/${MAPPING_TABLE}/delete`, { data: mappingCheckedIds.map((id) => ({ id })) }); - toast.success(`${mappingCheckedIds.length}개 품목 매핑이 삭제되었습니다.`); - setMappingCheckedIds([]); - const sid = selectedSupplierId; + for (const mappingId of priceCheckedIds) { + try { + const priceRes = await apiClient.post(`/table-management/tables/${PRICE_TABLE}/data`, { + page: 1, size: 500, + dataFilter: { enabled: true, filters: [{ columnName: "mapping_id", operator: "equals", value: mappingId }] }, + autoFilter: true, + }); + const prices = priceRes.data?.data?.data || priceRes.data?.data?.rows || []; + if (prices.length > 0) { + await apiClient.delete(`/table-management/tables/${PRICE_TABLE}/delete`, { + data: prices.map((p: any) => ({ id: p.id })), + }); + } + } catch { /* skip */ } + } + await apiClient.delete(`/table-management/tables/${MAPPING_TABLE}/delete`, { + data: priceCheckedIds.map((id) => ({ id })), + }); + toast.success(`${priceCheckedIds.length}개 품목 매핑이 삭제되었습니다.`); + setPriceCheckedIds([]); + const cid = selectedSupplierId; setSelectedSupplierId(null); - setTimeout(() => setSelectedSupplierId(sid), 50); - } catch { toast.error("삭제에 실패했습니다."); } + setTimeout(() => setSelectedSupplierId(cid), 50); + } catch { + toast.error("삭제에 실패했습니다."); + } }; + // 컬럼 가시성 헬퍼 + const isColumnVisible = (key: string) => ts.isVisible(key); + + const supplierColSpan = 1 + ["supplier_code", "supplier_name", "contact_person", "contact_phone", "division", "status"] + .filter((k) => isColumnVisible(k)).length; + + // EDataTable 컬럼 정의 (공급업체 목록) + const supplierColumns: EDataTableColumn[] = [ + ...(isColumnVisible("supplier_code") ? [{ key: "supplier_code", label: "공급업체코드", width: "w-[120px]" }] : []), + ...(isColumnVisible("supplier_name") ? [{ key: "supplier_name", label: "공급업체명", minWidth: "min-w-[140px]" }] : []), + ...(isColumnVisible("division") ? [{ + key: "division", + label: "거래유형", + width: "w-[80px]", + render: (val: any) => + val ? ( + + {val} + + ) : null, + }] : []), + ...(isColumnVisible("contact_person") ? [{ key: "contact_person", label: "담당자", width: "w-[80px]" }] : []), + ...(isColumnVisible("contact_phone") ? [{ key: "contact_phone", label: "전화번호", width: "w-[120px]" }] : []), + ...(isColumnVisible("email") ? [{ key: "email", label: "이메일", width: "w-[160px]" }] : []), + ...(isColumnVisible("business_number") ? [{ key: "business_number", label: "사업자번호", width: "w-[120px]" }] : []), + ...(isColumnVisible("address") ? [{ key: "address", label: "주소", minWidth: "min-w-[150px]" }] : []), + ...(isColumnVisible("status") ? [{ + key: "status", + label: "상태", + width: "w-[70px]", + render: (val: any) => + val ? ( + + {val} + + ) : null, + }] : []), + ]; + + // 엑셀 다운로드 const handleExcelDownload = async () => { if (suppliers.length === 0) return; - await exportToExcel(suppliers.map((s) => ({ - 공급업체코드: s.supplier_code, 공급업체명: s.supplier_name, - 담당자: s.contact_person, 연락처: s.contact_phone, - 사업자번호: s.business_number, 이메일: s.email, 상태: s.status, - })), "공급업체관리.xlsx", "공급업체"); - toast.success("다운로드 완료"); + toast.loading("엑셀 데이터 준비 중...", { id: "excel-dl" }); + try { + const allMappings: any[] = []; + const mapRes = await apiClient.post(`/table-management/tables/${MAPPING_TABLE}/data`, { + page: 1, size: 5000, autoFilter: true, + }); + const mappings = mapRes.data?.data?.data || mapRes.data?.data?.rows || []; + const itemIds = [...new Set(mappings.map((m: any) => m.item_id).filter(Boolean))]; + let itemMap: Record = {}; + if (itemIds.length > 0) { + try { + const itemRes = await apiClient.post(`/table-management/tables/item_info/data`, { + page: 1, size: itemIds.length + 10, + dataFilter: { enabled: true, filters: [{ columnName: "item_number", operator: "in", value: itemIds }] }, + autoFilter: true, + }); + for (const item of (itemRes.data?.data?.data || itemRes.data?.data?.rows || [])) { + itemMap[item.item_number] = item; + } + } catch { /* skip */ } + } + for (const m of mappings) { + const itemInfo = itemMap[m.item_id] || {}; + allMappings.push({ ...m, item_name: itemInfo.item_name || "", item_spec: itemInfo.size || "" }); + } + + const rows: Record[] = []; + for (const c of suppliers) { + const suppMappings = allMappings.filter((m) => m.supplier_id === c.supplier_code); + if (suppMappings.length === 0) { + rows.push({ + 공급업체코드: c.supplier_code, 공급업체명: c.supplier_name, + 거래유형: getCategoryLabel("division", c.division), + 담당자: c.contact_person, 전화번호: c.contact_phone, + 사업자번호: c.business_number, 이메일: c.email, + 상태: getCategoryLabel("status", c.status), + 품목코드: "", 품명: "", 규격: "", + 공급업체품번: "", 공급업체품명: "", + 기준가: "", 할인유형: "", 할인값: "", 단가: "", 통화: "", + }); + } else { + for (const m of suppMappings) { + rows.push({ + 공급업체코드: c.supplier_code, 공급업체명: c.supplier_name, + 거래유형: getCategoryLabel("division", c.division), + 담당자: c.contact_person, 전화번호: c.contact_phone, + 사업자번호: c.business_number, 이메일: c.email, + 상태: getCategoryLabel("status", c.status), + 품목코드: m.item_id || "", 품명: m.item_name || "", 규격: m.item_spec || "", + 공급업체품번: m.supplier_item_code || "", 공급업체품명: m.supplier_item_name || "", + 기준가: m.base_price || "", 할인유형: m.discount_type || "", 할인값: m.discount_value || "", + 단가: m.calculated_price || "", 통화: m.currency_code || "", + }); + } + } + } + await exportToExcel(rows, "공급업체관리.xlsx", "거래처+품목"); + toast.dismiss("excel-dl"); + toast.success(`${rows.length}행 다운로드 완료`); + } catch (err) { + toast.dismiss("excel-dl"); + toast.error("다운로드에 실패했습니다."); + } }; return ( -
- {/* 검색 바 */} +
+ {/* 검색 필터 (DynamicSearchFilter) */} - - -
- } /> - {/* 분할 패널 */} -
- + {/* 액션 버튼 영역 */} +
+
+ + +
+
+ + {/* 마스터-디테일 분할 패널 */} +
+ {/* 좌측: 공급업체 목록 */} - +
-
-
-

공급업체 목록

- {suppliers.length}건 - {supplierLoading && } + {/* 패널 헤더 */} +
+
+ 공급업체 목록 + + {supplierCount}건 +
-
- - - - + + +
+ + {/* 거래처 테이블 */} c.status !== "거래정지"))} + rowKey={(row) => row.id} loading={supplierLoading} emptyMessage="등록된 공급업체가 없어요" selectedId={selectedSupplierId} onSelect={(id) => setSelectedSupplierId(id)} - onRowDoubleClick={() => openSupplierEdit()} - showPagination={true} + onRowDoubleClick={(row) => { setSelectedSupplierId(row.id); openSupplierEdit(); }} + showRowNumber + showPagination + defaultPageSize={20} draggableColumns={false} - columnOrderKey="c16-supplier-main" + columnOrderKey="c16-supplier" />
- {/* 우측: 품목 매핑 */} - + {/* 우측: 디테일 패널 */} +
{!selectedSupplierId ? ( + /* 빈 상태 */
-
- -
공급업체를 선택해주세요
-
좌측에서 공급업체를 선택하면 품목 정보가 표시돼요
+
+ +
공급업체를 선택해주세요
+
좌측에서 공급업체를 선택하면 상세 정보가 표시돼요
) : ( <> -
-

{selectedSupplier?.supplier_name || "-"}

- {selectedSupplier?.supplier_code || "-"} -
-
-
- 등록 품목 - {mappingItems.length}건 + {/* 탭 + 버튼 통합 헤더 */} + setRightTab(v as "items" | "delivery")} + className="flex flex-col flex-1 overflow-hidden gap-0" + > +
+ + + 공급업체별 품목정보 + {Object.keys(priceGroups).length > 0 && ( + {Object.keys(priceGroups).length} + )} + + + 납품처 정보 + {deliveryItems.length > 0 && ( + {deliveryItems.length} + )} + + +
+ {rightTab === "items" ? ( + <> + + + + ) : ( + + )} +
-
- - -
-
-
- - - - - 0 && mappingCheckedIds.length === mappingItems.length} - onCheckedChange={(checked) => { - if (checked) setMappingCheckedIds(mappingItems.map((m) => m.id)); - else setMappingCheckedIds([]); - }} - /> - - 품목코드 - 품명 - 공급업체품번 - 기준가 - 단가 - 통화 - 리드타임 - - - - {mappingLoading ? ( - - ) : mappingItems.length === 0 ? ( - 등록된 품목이 없어요 - ) : mappingItems.map((m) => ( - openEditItem(m)} - onClick={() => setMappingCheckedIds((prev) => { - const next = [...prev]; - const idx = next.indexOf(m.id); - if (idx >= 0) next.splice(idx, 1); else next.push(m.id); - return next; + + {/* 품목정보 탭 */} + +
+
+ + + + 0 && priceCheckedIds.length === priceItems.length} + onChange={(e) => setPriceCheckedIds(e.target.checked ? priceItems.map((p) => p.id) : [])} + /> + + 품목코드 + 품명 + 공급업체품번 + 공급업체품명 + 기준유형 + 기준가 + 할인유형 + 할인값 + 단가 + 통화 + + + + {priceLoading ? ( + + + + + + ) : Object.keys(priceGroups).length === 0 ? ( + + + 등록된 품목이 없어요 + + + ) : Object.entries(priceGroups).map(([itemKey, group]) => { + const isExpanded = expandedItems.has(itemKey); + const m = group.master; + const isChecked = priceCheckedIds.includes(m.id); + return ( + + {/* 마스터 행 */} + { + setExpandedItems((prev) => { + const next = new Set(prev); + if (next.has(itemKey)) next.delete(itemKey); else next.add(itemKey); + return next; + }); + }} + onDoubleClick={() => openEditItem(m)} + > + { + e.stopPropagation(); + setPriceCheckedIds((prev) => + prev.includes(m.id) ? prev.filter((id) => id !== m.id) : [...prev, m.id] + ); + }} + > + + + +
+ {isExpanded + ? + : + } + {m.item_number} +
+
+ {m.item_name} + {m.supplier_item_code} + {m.supplier_item_name} + {m.base_price_type} + + {m.base_price ? Number(m.base_price).toLocaleString() : ""} + + {m.discount_type} + {m.discount_value ? Number(m.discount_value).toLocaleString() : ""} + + {m.calculated_price ? Number(m.calculated_price).toLocaleString() : ""} + + {m.currency_code} +
+ + {/* 현재 단가 카드 (펼쳤을 때) */} + {isExpanded && (() => { + const cp = group.details.find((p) => p.is_current) || group.details[0]; + if (!cp) return ( + + 등록된 단가가 없어요 + + ); + return ( + + +
+ {/* 카드 헤더 */} +
+
+ + 적용 단가 + 현재 +
+ {group.details.length > 1 && ( + 전체 {group.details.length}건 중 + )} +
+ {/* 카드 내용 */} +
+
+ 기간 + + {cp.start_date ? String(cp.start_date).split("T")[0] : "—"} ~ {cp.end_date ? String(cp.end_date).split("T")[0] : "—"} + +
+
+ 기준유형 + {cp.base_price_type_label || "-"} +
+
+ 기준가 + {cp.base_price ? Number(cp.base_price).toLocaleString() : "-"} +
+
+ 할인유형 + {cp.discount_type_label && cp.discount_type_label !== "할인없음" ? cp.discount_type_label : "-"} +
+
+ 할인값 + {cp.discount_value ? Number(cp.discount_value).toLocaleString() : "-"} +
+
+ 단수처리 + + {cp.rounding_unit_value + ? (priceCategoryOptions["rounding_unit_value"]?.find((o) => o.code === cp.rounding_unit_value)?.label || cp.rounding_unit_value) + : "-"} + +
+ +
+ 계산단가 + + {(cp.calculated_price || cp.unit_price) ? Number(cp.calculated_price || cp.unit_price).toLocaleString() : "-"} + {cp.currency_label} + +
+
+
+
+
+ ); + })()} +
+ ); })} - > - e.stopPropagation()}> - setMappingCheckedIds((prev) => - checked ? [...prev, m.id] : prev.filter((id) => id !== m.id) - )} - /> - - {m.item_number} - {m.item_name || "-"} - {m.supplier_item_code || "-"} - {m.base_price ? Number(m.base_price).toLocaleString() : "-"} - {m.calculated_price ? Number(m.calculated_price).toLocaleString() : "-"} - {m.currency_code || "-"} - {m.lead_time_days ? `${m.lead_time_days}일` : "-"} - - ))} -
-
-
+ + +
+ + + {/* 납품처 탭 */} + +
+ + + + 납품처코드 + 납품처명 + 주소 + 담당자 + 전화번호 + 메모 + 메인 + + + + {deliveryLoading ? ( + + + + + + ) : deliveryItems.length === 0 ? ( + + + 등록된 납품처가 없어요 + + + ) : deliveryItems.map((d) => ( + + {d.destination_code} + {d.destination_name} + {d.address} + {d.manager_name} + {d.phone} + {d.memo} + + {d.is_default && ( + 메인 + )} + + + ))} + +
+
+
+ )}
@@ -504,198 +1710,982 @@ export default function SupplierManagementPage() {
- {/* 공급업체 등록/수정 모달 */} - - - + {/* ── 모달: 공급업체 등록/수정 (3탭) ── */} + { + if (!open && isConfirmOpenRef.current) return; + setSupplierModalOpen(open); + if (!open) { + setModalContactFormOpen(false); + setModalDeliveryFormOpen(false); + setModalContactForm({}); + setModalDeliveryForm({}); + setModalContactEditId(null); + setModalDeliveryEditId(null); + fetchSuppliers(); + if (supplierForm.supplier_code) { + const cid = selectedSupplierId; + setSelectedSupplierId(null); + setTimeout(() => setSelectedSupplierId(cid), 50); + } + } + }}> + + {supplierEditMode ? "공급업체 수정" : "공급업체 등록"} - {supplierEditMode ? "공급업체 정보를 수정합니다." : "새로운 공급업체를 등록합니다."} + + {supplierEditMode ? "공급업체 정보를 수정합니다." : "새 공급업체를 등록합니다."} + -
-
- - setSupplierForm((p) => ({ ...p, supplier_code: e.target.value }))} placeholder="공급업체 코드" className="h-9" disabled={supplierEditMode} /> + + setSupplierModalTab(v)} className="flex flex-col flex-1 overflow-hidden"> +
+ + + 기본정보 + + + 담당자 관리 + {modalContacts.length > 0 && ( + {modalContacts.length} + )} + + + 납품처 관리 + {modalDeliveries.length > 0 && ( + {modalDeliveries.length} + )} + +
-
- - setSupplierForm((p) => ({ ...p, supplier_name: e.target.value }))} placeholder="공급업체명" className="h-9" /> -
-
- - setSupplierForm((p) => ({ ...p, contact_person: e.target.value }))} placeholder="담당자명" className="h-9" /> -
-
- - setSupplierForm((p) => ({ ...p, contact_phone: e.target.value }))} placeholder="010-0000-0000" className="h-9" /> -
-
- - setSupplierForm((p) => ({ ...p, business_number: e.target.value }))} placeholder="000-00-00000" className="h-9" /> -
-
- - setSupplierForm((p) => ({ ...p, email: e.target.value }))} placeholder="example@email.com" className="h-9" /> -
-
- - setSupplierForm((p) => ({ ...p, address: e.target.value }))} placeholder="사업장 주소" className="h-9" /> -
-
- - + + {/* 기본정보 탭 */} + +
+
+
+ + setSupplierForm((p) => ({ ...p, supplier_code: e.target.value }))} + placeholder={supplierEditMode ? "" : "자동 생성"} + className={cn("h-9 font-mono", !supplierEditMode && supplierForm.supplier_code && "bg-muted")} + readOnly={!supplierEditMode && !!supplierForm.supplier_code} + /> +
+
+ + setSupplierForm((p) => ({ ...p, supplier_name: e.target.value }))} + placeholder="공급업체명" + className="h-9" + /> +
+
+ + +
+
+ + +
+
+ + setSupplierForm((p) => ({ ...p, contact_person: e.target.value }))} + placeholder="공급업체담당자" + className="h-9" + /> +
+
+ + handleFormChange("contact_phone", e.target.value)} + placeholder="010-0000-0000" + className={cn("h-9", formErrors.contact_phone && "border-destructive")} + /> + {formErrors.contact_phone &&

{formErrors.contact_phone}

} +
+
+ + handleFormChange("email", e.target.value)} + placeholder="example@email.com" + className={cn("h-9", formErrors.email && "border-destructive")} + /> + {formErrors.email &&

{formErrors.email}

} +
+
+ + handleFormChange("business_number", e.target.value)} + placeholder="000-00-00000" + className={cn("h-9", formErrors.business_number && "border-destructive")} + /> + {formErrors.business_number &&

{formErrors.business_number}

} +
+
+ + setSupplierForm((p) => ({ ...p, address: e.target.value }))} + placeholder="주소" + className="h-9" + /> +
+
+ + {/* 세금유형 */} +
+
+ + +
+
+ {taxTypeRows.map((row, idx) => ( +
+ {idx + 1} + + { + const v = e.target.value.replace(/[^\d.]/g, ""); + setTaxTypeRows((prev) => prev.map((r) => r._id === row._id ? { ...r, rate: v } : r)); + }} + placeholder="세율 %" + className="h-9 text-[13px] w-[80px] text-right" + /> + % + +
+ ))} +
+
+
+
+ + {/* 담당자 관리 탭 */} + +
+ {/* 담당자 목록 */} +
+ + 담당자 {modalContacts.length}명 + + +
+ +
+ {modalContactLoading ? ( +
+ +
+ ) : modalContacts.length === 0 ? ( +
+ + 등록된 담당자가 없어요 +
+ ) : ( + + + + 담당자명 + 전화번호 + 이메일 + 부서 + 메인 + 메모 + 관리 + + + + {[...modalContacts].sort((a, b) => { + const aMain = a.is_main === "Y" || a.is_main === true ? 0 : 1; + const bMain = b.is_main === "Y" || b.is_main === true ? 0 : 1; + return aMain - bMain; + }).map((c) => ( + + {c.contact_name} + {c.contact_phone} + {c.contact_email} + {c.department} + + + + {c.memo} + +
+ + +
+
+
+ ))} +
+
+ )} +
+ + {/* 담당자 폼 (인라인) */} + {modalContactFormOpen && ( +
+
{modalContactEditId ? "담당자 수정" : "담당자 추가"}
+
+
+ + setModalContactForm((p) => ({ ...p, contact_name: e.target.value }))} + placeholder="담당자명" + className="h-8 text-sm" + /> +
+
+ + { + const formatted = formatField("phone", e.target.value); + setModalContactForm((p) => ({ ...p, contact_phone: formatted })); + }} + placeholder="010-0000-0000" + className="h-8 text-sm" + /> +
+
+ + setModalContactForm((p) => ({ ...p, contact_email: e.target.value }))} + placeholder="example@email.com" + className="h-8 text-sm" + /> +
+
+ + setModalContactForm((p) => ({ ...p, department: e.target.value }))} + placeholder="부서명" + className="h-8 text-sm" + /> +
+
+ + setModalContactForm((p) => ({ ...p, memo: e.target.value }))} + placeholder="메모" + className="h-8 text-sm" + /> +
+
+ +
+
+
+ + +
+
+ )} +
+
+ + {/* ── 탭3: 납품처 관리 ── */} + +
+ {/* 납품처 목록 헤더 */} +
+ + 납품처 {modalDeliveries.length}개 + + +
+ +
+ {modalDeliveryLoading ? ( +
+ +
+ ) : modalDeliveries.length === 0 ? ( +
+ + 등록된 납품처가 없어요 +
+ ) : ( + + + + 납품처코드 + 납품처명 + 주소 + 담당자 + 전화번호 + 메모 + 메인 + 관리 + + + + {[...modalDeliveries].sort((a, b) => { + const aMain = a.is_default === "Y" || a.is_default === true ? 0 : 1; + const bMain = b.is_default === "Y" || b.is_default === true ? 0 : 1; + return aMain - bMain; + }).map((d) => ( + + {d.destination_code} + {d.destination_name} + {d.address} + {d.manager_name} + {d.phone} + {d.memo} + + + + +
+ + +
+
+
+ ))} +
+
+ )} +
+ + {/* 납품처 폼 (인라인) */} + {modalDeliveryFormOpen && ( +
+
{modalDeliveryEditId ? "납품처 수정" : "납품처 추가"}
+
+
+ + setModalDeliveryForm((p) => ({ ...p, destination_code: e.target.value }))} + placeholder="자동 생성" + className={cn("h-8 text-sm font-mono", modalDeliveryForm.destination_code && "bg-muted")} + readOnly={!!modalDeliveryForm.destination_code && !modalDeliveryEditId} + /> +
+
+ + setModalDeliveryForm((p) => ({ ...p, destination_name: e.target.value }))} + placeholder="납품처명" + className="h-8 text-sm" + /> +
+
+ + setModalDeliveryForm((p) => ({ ...p, manager_name: e.target.value }))} + placeholder="담당자" + className="h-8 text-sm" + /> +
+
+ + setModalDeliveryForm((p) => ({ ...p, address: e.target.value }))} + placeholder="주소" + className="h-8 text-sm" + /> +
+
+ + { + const formatted = formatField("phone", e.target.value); + setModalDeliveryForm((p) => ({ ...p, phone: formatted })); + const err = validateField("phone", formatted); + setModalDeliveryFormErrors((p) => { + const n = { ...p }; + if (err) n.phone = err; else delete n.phone; + return n; + }); + }} + placeholder="010-0000-0000" + className={cn("h-8 text-sm", modalDeliveryFormErrors.phone && "border-destructive")} + /> + {modalDeliveryFormErrors.phone &&

{modalDeliveryFormErrors.phone}

} +
+
+ + setModalDeliveryForm((p) => ({ ...p, memo: e.target.value }))} + placeholder="메모" + className="h-8 text-sm" + /> +
+
+ +
+
+
+ + +
+
+ )} +
+
+ +
+ {!supplierEditMode && ( + + )} + - +
- {/* 품목 선택 모달 */} + {/* ── 모달: 품목 선택 (1단계) ── */} - + 품목 선택 - 공급업체에 추가할 품목을 선택하세요. + 거래처에 추가할 품목을 선택하세요. -
- + setItemSearchKeyword(e.target.value)} onKeyDown={(e) => e.key === "Enter" && searchItems()} - className="h-9 flex-1" /> + className="h-9 flex-1" + />
- +
- - - + + 0 && itemCheckedIds.size === itemSearchResults.length} - onCheckedChange={(checked) => { - if (checked) setItemCheckedIds(new Set(itemSearchResults.map((i) => i.id))); + onChange={(e) => { + if (e.target.checked) setItemCheckedIds(new Set(itemSearchResults.map((i) => i.id))); else setItemCheckedIds(new Set()); }} /> - 품목코드 - 품명 - 규격 + 품목코드 + 품명 + 규격 + 재질 단위 - 기준단가 {itemSearchResults.length === 0 ? ( - 검색 결과가 없어요 + + + 검색 결과가 없어요 + + ) : itemSearchResults.map((item) => ( - setItemCheckedIds((prev) => { const next = new Set(prev); if (next.has(item.id)) next.delete(item.id); else next.add(item.id); return next; })}> - {}} /> - {item.item_number} + setItemCheckedIds((prev) => { + const next = new Set(prev); + if (next.has(item.id)) next.delete(item.id); else next.add(item.id); + return next; + })} + > + + + + + {item.item_number} + {item.item_name} - {item.size || "-"} - {item.unit || "-"} - {item.standard_price ? Number(item.standard_price).toLocaleString() : "-"} + {item.size} + {item.material} + {item.unit} ))}
- -
- {itemCheckedIds.size}개 선택됨 -
- - -
+ + {itemCheckedIds.size}개 선택됨 +
+ +
- {/* 품목 상세 입력/수정 모달 */} + {/* ── 모달: 품목 상세 입력 (2단계) ── */} - + - 품목 매핑 {editItemData ? "수정" : "등록"} — {selectedSupplier?.supplier_name || ""} - {editItemData ? "공급업체 품번/단가 정보를 수정합니다." : "품목별 공급업체 품번과 단가를 입력합니다."} + + 품목 상세정보 {editItemData ? "수정" : "입력"} — {selectedSupplier?.supplier_name || ""} + + + {editItemData + ? "거래처 품번/품명과 기간별 단가를 수정합니다." + : "선택한 품목의 거래처 품번/품명과 기간별 단가를 설정합니다."} + -
+
{selectedItemsForDetail.map((item, idx) => { const itemKey = item.item_number || item.id; - const m = itemMappings[itemKey] || {} as any; + const mappingRows = itemMappings[itemKey] || []; + const prices = itemPrices[itemKey] || []; + return ( -
-
- {idx + 1}. {item.item_name || itemKey} - {itemKey} +
+ {/* 품목 헤더 */} +
+
{idx + 1}. {item.item_name || itemKey}
+
{itemKey} | {item.size || ""} | {item.unit || ""}
-
-
-
- - updateMapping(itemKey, "supplier_item_code", e.target.value)} placeholder="공급업체 자체 품번" className="h-9 text-sm" /> + +
+ {/* 좌: 거래처 품번/품명 */} +
+
+ + 거래처 품번/품명 관리 + +
-
- - updateMapping(itemKey, "supplier_item_name", e.target.value)} placeholder="공급업체 자체 품명" className="h-9 text-sm" /> +
+ {mappingRows.length === 0 ? ( +
입력된 거래처 품번이 없어요
+ ) : ( + handleMappingDragEnd(itemKey, e)} + > + r._id)} strategy={verticalListSortingStrategy}> + {mappingRows.map((mRow, mIdx) => ( + + {mIdx + 1} + updateMappingRow(itemKey, mRow._id, "supplier_item_code", e.target.value)} + placeholder="거래처 품번" + className="h-9 text-[13px] flex-1" + /> + updateMappingRow(itemKey, mRow._id, "supplier_item_name", e.target.value)} + placeholder="거래처 품명" + className="h-9 text-[13px] flex-1" + /> + + + ))} + + + )}
-
- 단가 정보 -
-
- - updateMapping(itemKey, "base_price", e.target.value)} className="h-8 text-xs text-right" placeholder="0" /> -
-
- - -
-
- - updateMapping(itemKey, "discount_value", e.target.value)} className="h-8 text-xs text-right" placeholder="0" /> -
-
- - -
+ + {/* 우: 기간별 단가 */} +
+
+ + 기간별 단가 설정 + +
-
-
- - updateMapping(itemKey, "currency_code", e.target.value)} className="h-8 text-xs" placeholder="KRW" /> -
-
- - updateMapping(itemKey, "start_date", e.target.value)} className="h-8 text-xs" /> -
-
- - updateMapping(itemKey, "end_date", e.target.value)} className="h-8 text-xs" /> -
-
-
-
-
- - updateMapping(itemKey, "lead_time_days", e.target.value)} className="h-8 text-xs" placeholder="0" /> -
-
- - updateMapping(itemKey, "min_order_qty", e.target.value)} className="h-8 text-xs" placeholder="0" /> -
+
+ {prices.map((price, pIdx) => ( +
+
setCollapsedPriceCards((prev) => { + const next = new Set(prev); + if (next.has(price._id)) next.delete(price._id); else next.add(price._id); + return next; + })} + > +
+ {collapsedPriceCards.has(price._id) + ? + : + } + 단가 {pIdx + 1} + {collapsedPriceCards.has(price._id) && price.calculated_price && ( + + {price.start_date || "—"} ~ {price.end_date || "—"} · {Number(price.calculated_price).toLocaleString()} {priceCategoryOptions["currency_code"]?.find((o) => o.code === price.currency_code)?.label || ""} + + )} +
+
+ {prices.length > 1 && ( + + )} +
+
+ {!collapsedPriceCards.has(price._id) &&
+ {/* 기간 + 통화 */} +
+
+ + { + const v = e.target.value; + updatePriceRow(itemKey, price._id, "start_date", v); + if (price.end_date && v > price.end_date) { + updatePriceRow(itemKey, price._id, "end_date", v); + } + }} + max={price.end_date || undefined} + className="h-9 text-[13px] w-full" + /> +
+ ~ +
+ + updatePriceRow(itemKey, price._id, "end_date", e.target.value)} + min={price.start_date || undefined} + className="h-9 text-[13px] w-full" + /> +
+
+ + +
+
+ {/* 기준가/할인/반올림 */} +
+
+ +
+ { + const raw = e.target.value.replace(/[^\d.-]/g, ""); + updatePriceRow(itemKey, price._id, "base_price", raw); + }} + className="h-9 text-[13px] text-right flex-1" + placeholder="기준가" + /> +
+ +
+ { + const raw = e.target.value.replace(/[^\d.-]/g, ""); + updatePriceRow(itemKey, price._id, "discount_value", raw); + }} + className="h-9 text-[13px] text-right w-[60px]" + placeholder="0" + /> +
+ +
+
+ {/* 계산 단가 */} +
+ 계산 단가: + + {price.calculated_price ? Number(price.calculated_price).toLocaleString() : "-"} + + {price.calculated_price && price.currency_code && ( + + {priceCategoryOptions["currency_code"]?.find((o) => o.code === price.currency_code)?.label || ""} + + )} +
+
} +
+ ))}
@@ -704,19 +2694,41 @@ export default function SupplierManagementPage() { })}
- +
- + {/* 엑셀 업로드 (멀티테이블) */} + {excelChainConfig && ( + { + setExcelUploadOpen(open); + if (!open) setExcelChainConfig(null); + }} + config={excelChainConfig} + onSuccess={() => { + fetchSuppliers(); + const cid = selectedSupplierId; + setSelectedSupplierId(null); + setTimeout(() => setSelectedSupplierId(cid), 50); + }} + /> + )} {/* 테이블 설정 모달 */} e.user_id === r.internal_manager)?.user_name || r.internal_manager) - : "", }; }); // 거래처코드 숫자 기준 내림차순 정렬 @@ -1389,7 +1386,7 @@ export default function CustomerManagementPage() {
-
- - -
( - + {c.contact_name} {c.contact_phone} {c.contact_email} @@ -1987,12 +1968,24 @@ export default function CustomerManagementPage() { ? "bg-primary text-primary-foreground border-primary shadow-sm shadow-primary/30" : "bg-transparent text-muted-foreground border-muted-foreground/20 hover:border-primary/50 hover:text-primary" )} - onClick={() => { - setModalContacts((prev) => prev.map((item) => - (item._localId || item.id) === (c._localId || c.id) - ? { ...item, is_main: (item.is_main === "Y" || item.is_main === true) ? "N" : "Y" } - : item - )); + onClick={async () => { + const isCurrentMain = c.is_main === "Y" || c.is_main === true; + if (isCurrentMain) { + setModalContacts((prev) => prev.map((item) => + (item._localId || item.id) === (c._localId || c.id) ? { ...item, is_main: "N" } : item + )); + } else { + const existingMain = modalContacts.find((x) => (x.is_main === "Y" || x.is_main === true) && (x._localId || x.id) !== (c._localId || c.id)); + if (existingMain) { + const ok = await confirm(`현재 메인 담당자는 "${existingMain.contact_name}"입니다. 변경하시겠습니까?`); + if (!ok) return; + } + setModalContacts((prev) => prev.map((item) => + (item._localId || item.id) === (c._localId || c.id) + ? { ...item, is_main: "Y" } + : { ...item, is_main: "N" } + )); + } }} > {(c.is_main === "Y" || c.is_main === true) ? "★ 메인" : "메인"} @@ -2084,7 +2077,17 @@ export default function CustomerManagementPage() { setModalContactForm((p) => ({ ...p, is_main: e.target.checked ? "Y" : "N" }))} + onChange={async (e) => { + if (e.target.checked) { + const existingMain = modalContacts.find((x) => (x.is_main === "Y" || x.is_main === true) && (x._localId || x.id) !== modalContactEditId); + if (existingMain) { + const ok = await confirm(`현재 메인 담당자는 "${existingMain.contact_name}"입니다. 변경하시겠습니까?`); + if (!ok) return; + setModalContacts((prev) => prev.map((item) => ({ ...item, is_main: "N" }))); + } + } + setModalContactForm((p) => ({ ...p, is_main: e.target.checked ? "Y" : "N" })); + }} className="rounded" /> 메인 담당자 @@ -2172,12 +2175,24 @@ export default function CustomerManagementPage() { ? "bg-primary text-primary-foreground border-primary shadow-sm shadow-primary/30" : "bg-transparent text-muted-foreground border-muted-foreground/20 hover:border-primary/50 hover:text-primary" )} - onClick={() => { - setModalDeliveries((prev) => prev.map((item) => - (item._localId || item.id) === (d._localId || d.id) - ? { ...item, is_default: (item.is_default === "Y" || item.is_default === true) ? "N" : "Y" } - : item - )); + onClick={async () => { + const isCurrentMain = d.is_default === "Y" || d.is_default === true; + if (isCurrentMain) { + setModalDeliveries((prev) => prev.map((item) => + (item._localId || item.id) === (d._localId || d.id) ? { ...item, is_default: "N" } : item + )); + } else { + const existingMain = modalDeliveries.find((x) => (x.is_default === "Y" || x.is_default === true) && (x._localId || x.id) !== (d._localId || d.id)); + if (existingMain) { + const ok = await confirm(`현재 메인 납품처는 "${existingMain.destination_name}"입니다. 변경하시겠습니까?`); + if (!ok) return; + } + setModalDeliveries((prev) => prev.map((item) => + (item._localId || item.id) === (d._localId || d.id) + ? { ...item, is_default: "Y" } + : { ...item, is_default: "N" } + )); + } }} > {(d.is_default === "Y" || d.is_default === true) ? "★ 메인" : "메인"} @@ -2286,7 +2301,17 @@ export default function CustomerManagementPage() { setModalDeliveryForm((p) => ({ ...p, is_default: e.target.checked ? "Y" : "N" }))} + onChange={async (e) => { + if (e.target.checked) { + const existingMain = modalDeliveries.find((x) => (x.is_default === "Y" || x.is_default === true) && (x._localId || x.id) !== modalDeliveryEditId); + if (existingMain) { + const ok = await confirm(`현재 메인 납품처는 "${existingMain.destination_name}"입니다. 변경하시겠습니까?`); + if (!ok) return; + setModalDeliveries((prev) => prev.map((item) => ({ ...item, is_default: "N" }))); + } + } + setModalDeliveryForm((p) => ({ ...p, is_default: e.target.checked ? "Y" : "N" })); + }} className="rounded" /> 메인 납품처로 설정 diff --git a/frontend/components/common/ConfirmDialog.tsx b/frontend/components/common/ConfirmDialog.tsx index 6450ffdc..a49731b2 100644 --- a/frontend/components/common/ConfirmDialog.tsx +++ b/frontend/components/common/ConfirmDialog.tsx @@ -60,11 +60,13 @@ export function useConfirmDialog() { const [title, setTitle] = useState(""); const [options, setOptions] = useState({}); const resolveRef = useRef<((value: boolean) => void) | null>(null); + const isOpenRef = useRef(false); const confirm = useCallback((msg: string, opts?: ConfirmOptions): Promise => { setTitle(msg); setOptions(opts || {}); setOpen(true); + isOpenRef.current = true; return new Promise((resolve) => { resolveRef.current = resolve; }); @@ -73,11 +75,13 @@ export function useConfirmDialog() { const handleConfirm = () => { setOpen(false); resolveRef.current?.(true); + setTimeout(() => { isOpenRef.current = false; }, 100); }; const handleCancel = () => { setOpen(false); resolveRef.current?.(false); + setTimeout(() => { isOpenRef.current = false; }, 100); }; const variant = options.variant || "default"; @@ -86,7 +90,12 @@ export function useConfirmDialog() { const ConfirmDialogComponent = ( { if (!v) handleCancel(); }}> - + e.preventDefault()} + onPointerDownOutside={(e) => e.preventDefault()} + onInteractOutside={(e) => e.preventDefault()} + >
@@ -103,10 +112,10 @@ export function useConfirmDialog() {
- + { e.stopPropagation(); handleCancel(); }}> {options.cancelText || "취소"} - + { e.stopPropagation(); handleConfirm(); }} className={cn(config.buttonClass)}> {options.confirmText || "확인"} @@ -114,5 +123,5 @@ export function useConfirmDialog() { ); - return { confirm, ConfirmDialogComponent }; + return { confirm, ConfirmDialogComponent, isConfirmOpenRef: isOpenRef }; } diff --git a/frontend/components/ui/alert-dialog.tsx b/frontend/components/ui/alert-dialog.tsx index 821c7c31..7eec8451 100644 --- a/frontend/components/ui/alert-dialog.tsx +++ b/frontend/components/ui/alert-dialog.tsx @@ -121,7 +121,7 @@ const AlertDialogContent = React.forwardRef< return (
@@ -147,11 +147,11 @@ const AlertDialogContent = React.forwardRef<
- + Date: Mon, 6 Apr 2026 17:12:30 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=EC=A7=B9=EC=A7=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/(main)/COMPANY_16/sales/customer/page.tsx | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/frontend/app/(main)/COMPANY_16/sales/customer/page.tsx b/frontend/app/(main)/COMPANY_16/sales/customer/page.tsx index 514ee0b1..0ba76d35 100644 --- a/frontend/app/(main)/COMPANY_16/sales/customer/page.tsx +++ b/frontend/app/(main)/COMPANY_16/sales/customer/page.tsx @@ -2078,15 +2078,18 @@ export default function CustomerManagementPage() { type="checkbox" checked={modalContactForm.is_main === "Y" || modalContactForm.is_main === true} onChange={async (e) => { - if (e.target.checked) { + const checked = e.target.checked; + if (checked) { const existingMain = modalContacts.find((x) => (x.is_main === "Y" || x.is_main === true) && (x._localId || x.id) !== modalContactEditId); if (existingMain) { const ok = await confirm(`현재 메인 담당자는 "${existingMain.contact_name}"입니다. 변경하시겠습니까?`); if (!ok) return; setModalContacts((prev) => prev.map((item) => ({ ...item, is_main: "N" }))); } + setModalContactForm((p) => ({ ...p, is_main: "Y" })); + } else { + setModalContactForm((p) => ({ ...p, is_main: "N" })); } - setModalContactForm((p) => ({ ...p, is_main: e.target.checked ? "Y" : "N" })); }} className="rounded" /> @@ -2302,15 +2305,18 @@ export default function CustomerManagementPage() { type="checkbox" checked={modalDeliveryForm.is_default === "Y" || modalDeliveryForm.is_default === true} onChange={async (e) => { - if (e.target.checked) { + const checked = e.target.checked; + if (checked) { const existingMain = modalDeliveries.find((x) => (x.is_default === "Y" || x.is_default === true) && (x._localId || x.id) !== modalDeliveryEditId); if (existingMain) { const ok = await confirm(`현재 메인 납품처는 "${existingMain.destination_name}"입니다. 변경하시겠습니까?`); if (!ok) return; setModalDeliveries((prev) => prev.map((item) => ({ ...item, is_default: "N" }))); } + setModalDeliveryForm((p) => ({ ...p, is_default: "Y" })); + } else { + setModalDeliveryForm((p) => ({ ...p, is_default: "N" })); } - setModalDeliveryForm((p) => ({ ...p, is_default: e.target.checked ? "Y" : "N" })); }} className="rounded" /> From cd22c5aca2db11fe154a4ce35cf5729bd2005f71 Mon Sep 17 00:00:00 2001 From: DDD1542 Date: Mon, 6 Apr 2026 17:23:09 +0900 Subject: [PATCH 3/4] 123 --- .../COMPANY_16/purchase/supplier/page.tsx | 24 ++++++++++++------- .../(main)/COMPANY_16/sales/customer/page.tsx | 12 +++++++--- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/frontend/app/(main)/COMPANY_16/purchase/supplier/page.tsx b/frontend/app/(main)/COMPANY_16/purchase/supplier/page.tsx index c2111d7e..88da1d20 100644 --- a/frontend/app/(main)/COMPANY_16/purchase/supplier/page.tsx +++ b/frontend/app/(main)/COMPANY_16/purchase/supplier/page.tsx @@ -56,7 +56,7 @@ const CONTACT_TABLE = "supplier_contact"; const SUPPLIER_GRID_COLUMNS = [ { key: "supplier_code", label: "공급업체코드" }, { key: "supplier_name", label: "공급업체명" }, - { key: "division", label: "거래유형" }, + { key: "division", label: "공급업체유형" }, { key: "contact_person", label: "담당자" }, { key: "contact_phone", label: "전화번호" }, { key: "email", label: "이메일" }, @@ -452,9 +452,12 @@ export default function SupplierManagementPage() { const handleModalContactSave = async () => { if (!modalContactForm.contact_name) { toast.error("담당자명은 필수입니다."); return; } if (modalContactEditId) { - // 수정 — 로컬 리스트에서 교체 + // 수정 — 로컬 리스트에서 교체 + 메인 설정 시 다른 메인 해제 + const isSettingMain = modalContactForm.is_main === "Y" || modalContactForm.is_main === true; setModalContacts((prev) => prev.map((c) => - c._localId === modalContactEditId ? { ...c, ...modalContactForm } : c + (c._localId || c.id) === modalContactEditId + ? { ...c, ...modalContactForm } + : isSettingMain ? { ...c, is_main: "N" } : c )); } else { // 추가 — 로컬 리스트에 카드 추가 @@ -512,8 +515,11 @@ export default function SupplierManagementPage() { const handleModalDeliverySave = async () => { if (!modalDeliveryForm.destination_name) { toast.error("납품처명은 필수입니다."); return; } if (modalDeliveryEditId) { + const isSettingMain = modalDeliveryForm.is_default === "Y" || modalDeliveryForm.is_default === true; setModalDeliveries((prev) => prev.map((d) => - (d._localId || d.id) === modalDeliveryEditId ? { ...d, ...modalDeliveryForm } : d + (d._localId || d.id) === modalDeliveryEditId + ? { ...d, ...modalDeliveryForm } + : isSettingMain ? { ...d, is_default: "N" } : d )); } else { setModalDeliveries((prev) => [...prev, { @@ -544,7 +550,7 @@ export default function SupplierManagementPage() { setModalDeliveryFormOpen(false); setTaxTypeRows([]); setSupplierModalOpen(true); - // 거래처 코드 자동 채번 — 기존 데이터 max값 기반 + // 공급업체 코드 자동 채번 — 기존 데이터 max값 기반 try { const ruleRes = await apiClient.get(`/numbering-rules/by-column/${SUPPLIER_TABLE}/supplier_code`); const ruleData = ruleRes.data; @@ -1233,7 +1239,7 @@ export default function SupplierManagementPage() { ...(isColumnVisible("supplier_name") ? [{ key: "supplier_name", label: "공급업체명", minWidth: "min-w-[140px]" }] : []), ...(isColumnVisible("division") ? [{ key: "division", - label: "거래유형", + label: "공급업체유형", width: "w-[80px]", render: (val: any) => val ? ( @@ -1298,7 +1304,7 @@ export default function SupplierManagementPage() { if (suppMappings.length === 0) { rows.push({ 공급업체코드: c.supplier_code, 공급업체명: c.supplier_name, - 거래유형: getCategoryLabel("division", c.division), + 공급업체유형: getCategoryLabel("division", c.division), 담당자: c.contact_person, 전화번호: c.contact_phone, 사업자번호: c.business_number, 이메일: c.email, 상태: getCategoryLabel("status", c.status), @@ -1310,7 +1316,7 @@ export default function SupplierManagementPage() { for (const m of suppMappings) { rows.push({ 공급업체코드: c.supplier_code, 공급업체명: c.supplier_name, - 거래유형: getCategoryLabel("division", c.division), + 공급업체유형: getCategoryLabel("division", c.division), 담당자: c.contact_person, 전화번호: c.contact_phone, 사업자번호: c.business_number, 이메일: c.email, 상태: getCategoryLabel("status", c.status), @@ -1763,7 +1769,7 @@ export default function SupplierManagementPage() {
- + setSupplierForm((p) => ({ ...p, supplier_code: e.target.value }))} diff --git a/frontend/app/(main)/COMPANY_16/sales/customer/page.tsx b/frontend/app/(main)/COMPANY_16/sales/customer/page.tsx index 0ba76d35..20bde7c4 100644 --- a/frontend/app/(main)/COMPANY_16/sales/customer/page.tsx +++ b/frontend/app/(main)/COMPANY_16/sales/customer/page.tsx @@ -452,9 +452,12 @@ export default function CustomerManagementPage() { const handleModalContactSave = async () => { if (!modalContactForm.contact_name) { toast.error("담당자명은 필수입니다."); return; } if (modalContactEditId) { - // 수정 — 로컬 리스트에서 교체 + // 수정 — 로컬 리스트에서 교체 + 메인 설정 시 다른 메인 해제 + const isSettingMain = modalContactForm.is_main === "Y" || modalContactForm.is_main === true; setModalContacts((prev) => prev.map((c) => - c._localId === modalContactEditId ? { ...c, ...modalContactForm } : c + (c._localId || c.id) === modalContactEditId + ? { ...c, ...modalContactForm } + : isSettingMain ? { ...c, is_main: "N" } : c )); } else { // 추가 — 로컬 리스트에 카드 추가 @@ -512,8 +515,11 @@ export default function CustomerManagementPage() { const handleModalDeliverySave = async () => { if (!modalDeliveryForm.destination_name) { toast.error("납품처명은 필수입니다."); return; } if (modalDeliveryEditId) { + const isSettingMain = modalDeliveryForm.is_default === "Y" || modalDeliveryForm.is_default === true; setModalDeliveries((prev) => prev.map((d) => - (d._localId || d.id) === modalDeliveryEditId ? { ...d, ...modalDeliveryForm } : d + (d._localId || d.id) === modalDeliveryEditId + ? { ...d, ...modalDeliveryForm } + : isSettingMain ? { ...d, is_default: "N" } : d )); } else { setModalDeliveries((prev) => [...prev, { From 19e3f7adc2004cfb68d54fa025c91abadbb8b4a5 Mon Sep 17 00:00:00 2001 From: DDD1542 Date: Mon, 6 Apr 2026 17:23:19 +0900 Subject: [PATCH 4/4] =?UTF-8?q?feat:=20=EA=B3=B5=EA=B8=89=EC=97=85?= =?UTF-8?q?=EC=B2=B4=20=EB=B0=8F=20=EA=B1=B0=EB=9E=98=EC=B2=98=20=EA=B4=80?= =?UTF-8?q?=EB=A6=AC=20=ED=8E=98=EC=9D=B4=EC=A7=80=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EA=B1=B0=EB=9E=98=EC=9C=A0=ED=98=95=20=EB=9D=BC=EB=B2=A8=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20=EB=B0=8F=20=EB=A9=94=EC=9D=B8=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95=20=EB=A1=9C=EC=A7=81=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/app/(main)/COMPANY_16/purchase/supplier/page.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frontend/app/(main)/COMPANY_16/purchase/supplier/page.tsx b/frontend/app/(main)/COMPANY_16/purchase/supplier/page.tsx index 88da1d20..8910a0d7 100644 --- a/frontend/app/(main)/COMPANY_16/purchase/supplier/page.tsx +++ b/frontend/app/(main)/COMPANY_16/purchase/supplier/page.tsx @@ -1788,12 +1788,12 @@ export default function SupplierManagementPage() { />
- +