From 4ee8b24cfadf47452807fa2af801385ed60ab47c Mon Sep 17 00:00:00 2001 From: dujinkim Date: Mon, 15 Sep 2025 14:41:01 +0000 Subject: (대표님) 작업사항 - rfqLast, tbeLast, pdfTron, userAuth MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../evcp/(evcp)/rfq-last/[id]/compare/page.tsx | 69 ++ .../partners/(partners)/rfq-last/[id]/page.tsx | 3 - app/[lng]/partners/(partners)/tbe-last/page.tsx | 88 +++ app/[lng]/pdftron-viewer/page.tsx | 507 ++++++++++++++ app/api/contracts/prepare-template/route.ts | 4 +- app/api/document-reviews/[id]/route.ts | 138 ++++ app/api/files/[...path]/route.ts | 38 +- app/api/partners/rfq-last/[id]/response/route.ts | 12 +- .../partners/tbe/[sessionId]/documents/route.ts | 275 ++++++++ app/api/pdftron-comments/xfdf/count/route.ts | 171 +++++ app/api/pdftron-comments/xfdf/route.ts | 362 ++++++++++ .../sessions/[sessionId]/vendor-questions/route.ts | 131 ++++ .../sessions/[sessionId]/vendor-remarks/route.ts | 57 ++ app/api/upload/signed-contract/route.ts | 43 +- config/menuConfig.ts | 6 + db/schema/rfqLastTBE.ts | 211 +++--- db/schema/rfqVendor.ts | 224 +++++- i18n/locales/en/menu.json | 8 +- i18n/locales/ko/menu.json | 8 +- lib/admin-users/service.ts | 2 +- lib/basic-contract/gen-service.ts | 33 +- .../viewer/basic-contract-sign-viewer.tsx | 1 + lib/bidding/pre-quote/service.ts | 49 +- lib/export-to-excel.ts | 316 +++++++++ lib/rfq-last/attachment/vendor-response-table.tsx | 387 ++++++++--- lib/rfq-last/compare-action.ts | 500 ++++++++++++++ lib/rfq-last/quotation-compare-view.tsx | 755 +++++++++++++++++++++ lib/rfq-last/service.ts | 376 +++++++++- lib/rfq-last/table/rfq-seal-toggle-cell.tsx | 93 +++ lib/rfq-last/table/rfq-table-columns.tsx | 73 +- lib/rfq-last/table/rfq-table-toolbar-actions.tsx | 222 ++++-- .../editor/vendor-response-editor.tsx | 335 +++++---- lib/rfq-last/vendor-response/service.ts | 175 +---- lib/rfq-last/vendor-response/validations.ts | 4 +- .../vendor-quotations-table-columns.tsx | 2 +- .../vendor-response/vendor-quotations-table.tsx | 2 +- lib/rfq-last/vendor/rfq-vendor-table.tsx | 223 ++++-- lib/soap/ecc/send/pcr-confirm.ts | 2 +- lib/tbe-last/service.ts | 231 ++++++- lib/tbe-last/table/documents-sheet.tsx | 543 +++++++++++++++ lib/tbe-last/table/evaluation-dialog.tsx | 432 ++++++++++++ lib/tbe-last/table/pr-items-dialog.tsx | 83 +++ lib/tbe-last/table/session-detail-dialog.tsx | 103 +++ lib/tbe-last/table/tbe-last-table-columns.tsx | 214 +++--- lib/tbe-last/table/tbe-last-table.tsx | 279 ++------ lib/tbe-last/vendor-tbe-service.ts | 355 ++++++++++ lib/tbe-last/vendor/tbe-table-columns.tsx | 335 +++++++++ lib/tbe-last/vendor/tbe-table.tsx | 222 ++++++ lib/tbe-last/vendor/vendor-comment-dialog.tsx | 313 +++++++++ .../vendor/vendor-document-upload-dialog.tsx | 326 +++++++++ lib/tbe-last/vendor/vendor-documents-sheet.tsx | 602 ++++++++++++++++ .../vendor/vendor-evaluation-view-dialog.tsx | 250 +++++++ lib/tbe-last/vendor/vendor-pr-items-dialog.tsx | 253 +++++++ lib/users/auth/verifyCredentails.ts | 4 +- lib/users/repository.ts | 7 +- 55 files changed, 9342 insertions(+), 1115 deletions(-) create mode 100644 app/[lng]/evcp/(evcp)/rfq-last/[id]/compare/page.tsx create mode 100644 app/[lng]/partners/(partners)/tbe-last/page.tsx create mode 100644 app/[lng]/pdftron-viewer/page.tsx create mode 100644 app/api/document-reviews/[id]/route.ts create mode 100644 app/api/partners/tbe/[sessionId]/documents/route.ts create mode 100644 app/api/pdftron-comments/xfdf/count/route.ts create mode 100644 app/api/pdftron-comments/xfdf/route.ts create mode 100644 app/api/tbe/sessions/[sessionId]/vendor-questions/route.ts create mode 100644 app/api/tbe/sessions/[sessionId]/vendor-remarks/route.ts create mode 100644 lib/export-to-excel.ts create mode 100644 lib/rfq-last/compare-action.ts create mode 100644 lib/rfq-last/quotation-compare-view.tsx create mode 100644 lib/rfq-last/table/rfq-seal-toggle-cell.tsx create mode 100644 lib/tbe-last/table/documents-sheet.tsx create mode 100644 lib/tbe-last/table/evaluation-dialog.tsx create mode 100644 lib/tbe-last/table/pr-items-dialog.tsx create mode 100644 lib/tbe-last/table/session-detail-dialog.tsx create mode 100644 lib/tbe-last/vendor-tbe-service.ts create mode 100644 lib/tbe-last/vendor/tbe-table-columns.tsx create mode 100644 lib/tbe-last/vendor/tbe-table.tsx create mode 100644 lib/tbe-last/vendor/vendor-comment-dialog.tsx create mode 100644 lib/tbe-last/vendor/vendor-document-upload-dialog.tsx create mode 100644 lib/tbe-last/vendor/vendor-documents-sheet.tsx create mode 100644 lib/tbe-last/vendor/vendor-evaluation-view-dialog.tsx create mode 100644 lib/tbe-last/vendor/vendor-pr-items-dialog.tsx diff --git a/app/[lng]/evcp/(evcp)/rfq-last/[id]/compare/page.tsx b/app/[lng]/evcp/(evcp)/rfq-last/[id]/compare/page.tsx new file mode 100644 index 00000000..097b99eb --- /dev/null +++ b/app/[lng]/evcp/(evcp)/rfq-last/[id]/compare/page.tsx @@ -0,0 +1,69 @@ +import { Suspense } from "react"; +import { notFound } from "next/navigation"; +import { QuotationCompareView } from "@/lib/rfq-last/quotation-compare-view"; +import { Loader2 } from "lucide-react"; +import { getComparisonData } from "@/lib/rfq-last/compare-action"; + +interface ComparePageProps { + params: { + id: string; + }; + searchParams: { + vendors?: string; + }; +} + +export default async function ComparePage({ + params, + searchParams +}: ComparePageProps) { + const rfqId = parseInt(params.id); + + console.log(rfqId,"rfqId") + console.log(searchParams.vendors,"searchParams.vendors") + + // URL에서 벤더 ID들 파싱 + const vendorIds = searchParams.vendors + ?.split(',') + .map(id => parseInt(id)) + .filter(id => !isNaN(id)) || []; + + if (!rfqId || vendorIds.length < 2) { + notFound(); + } + + // 서버에서 데이터 가져오기 + const data = await getComparisonData(rfqId, vendorIds); + + if (!data) { + notFound(); + } + + return ( +
+ {/* 페이지 헤더 */} +
+
+

견적 비교

+

+ {data.rfqInfo.rfqCode} - {data.rfqInfo.rfqTitle} +

+
+
+ 비교 업체: {data.vendors.length}개 +
+
+ + {/* 비교 뷰 컴포넌트 */} + + +
+ } + > + + + + ); +} \ No newline at end of file diff --git a/app/[lng]/partners/(partners)/rfq-last/[id]/page.tsx b/app/[lng]/partners/(partners)/rfq-last/[id]/page.tsx index a0e278cb..7a68e3a2 100644 --- a/app/[lng]/partners/(partners)/rfq-last/[id]/page.tsx +++ b/app/[lng]/partners/(partners)/rfq-last/[id]/page.tsx @@ -135,9 +135,6 @@ export default async function VendorResponsePage({ params }: PageProps) { ) .orderBy(basicContract.createdAt) - console.log(basicContracts,"basicContracts") - console.log(rfqDetail,"rfqDetail") - return (
diff --git a/app/[lng]/partners/(partners)/tbe-last/page.tsx b/app/[lng]/partners/(partners)/tbe-last/page.tsx new file mode 100644 index 00000000..62a982c7 --- /dev/null +++ b/app/[lng]/partners/(partners)/tbe-last/page.tsx @@ -0,0 +1,88 @@ +import { type SearchParams } from "@/types/table" +import { getValidFilters } from "@/lib/data-table" +import { getTBEforVendor } from "@/lib/tbe-last/vendor-tbe-service" +import { searchParamsTBELastCache } from "@/lib/tbe-last/validations" +import { getServerSession } from "next-auth" +import { authOptions } from "@/app/api/auth/[...nextauth]/route" +import { TbeVendorTable } from "@/lib/tbe-last/vendor/tbe-table" +import * as React from "react" +import { Skeleton } from "@/components/ui/skeleton" +import { DataTableSkeleton } from "@/components/data-table/data-table-skeleton" +import { Shell } from "@/components/shell" +import { InformationButton } from "@/components/information/information-button" +interface IndexPageProps { + // Next.js 13 App Router에서 기본으로 주어지는 객체들 + params: { + lng: string + id: string + } + searchParams: Promise +} + +export default async function RfqTBEPage(props: IndexPageProps) { + const resolvedParams = await props.params + const lng = resolvedParams.lng + + // 2) SearchParams 파싱 (Zod) + // - "filters", "page", "perPage", "sort" 등 contact 전용 컬럼 + const searchParams = await props.searchParams + const search = searchParamsTBELastCache.parse(searchParams) + const validFilters = getValidFilters(search.filters) + + const session = await getServerSession(authOptions) + const vendorId = session?.user.companyId + // const vendorId = "17" + + const idAsNumber = Number(vendorId) + + const promises = Promise.all([ + getTBEforVendor({ + ...search, + filters: validFilters, + }, + idAsNumber) + ]) + + + return ( + +
+
+
+
+

+ TBE 관리 +

+ +
+ {/*

+ TBE에 응답하고 커뮤니케이션을 할 수 있습니다.{" "} +

*/} +
+
+
+ + }> + {/* */} + + + } + > + + +
+ ) +} diff --git a/app/[lng]/pdftron-viewer/page.tsx b/app/[lng]/pdftron-viewer/page.tsx new file mode 100644 index 00000000..bde60a41 --- /dev/null +++ b/app/[lng]/pdftron-viewer/page.tsx @@ -0,0 +1,507 @@ +// app/pdftron-viewer/page.tsx + +"use client" + +import * as React from "react" +import { useSearchParams } from "next/navigation" +import { Button } from "@/components/ui/button" +import { ArrowLeft, MessageSquare, Download, Upload } from "lucide-react" +import { Badge } from "@/components/ui/badge" +import { useSession } from "next-auth/react" +import { useToast } from "@/hooks/use-toast" +import type { WebViewerInstance } from "@pdftron/webviewer" + +// PDFTron 코멘트 타입 정의 +interface PDFTronComment { + id: number + documentReviewId: number + pdftronDocumentId: string + xfdfString: string + annotationData: any + commentSummary?: { + total: number + open: number + resolved: number + rejected: number + deferred: number + byCategory: Record + bySeverity: Record + byAuthor: Record + } + createdBy: number + createdByName?: string + createdByType: "buyer" | "vendor" + createdAt: Date + updatedAt: Date +} + +export default function PDFTronViewerPage() { + const { data: session, status } = useSession() + const searchParams = useSearchParams() + const viewerRef = React.useRef(null) + const [instance, setInstance] = React.useState(null) + const [isLoading, setIsLoading] = React.useState(true) + const [lastSavedTime, setLastSavedTime] = React.useState(null) + const [isSaving, setIsSaving] = React.useState(false) + const [annotationCount, setAnnotationCount] = React.useState(0) + const { toast } = useToast() + const initialized = React.useRef(false) + const isCancelled = React.useRef(false) + const autoSaveTimerRef = React.useRef(null) + const xfdfLoadedRef = React.useRef(false) // XFDF 로딩 완료 여부 추적 + + // URL 파라미터에서 정보 가져오기 + const filePath = searchParams.get('filePath') + const documentId = searchParams.get('documentId') + const documentReviewId = searchParams.get('documentReviewId') + const sessionId = searchParams.get('sessionId') + const documentName = searchParams.get('documentName') + + // PDFTron WebViewer 초기화 - session과 XFDF 모두 준비된 후 실행 + React.useEffect(() => { + if (!initialized.current && viewerRef.current && filePath && session && documentReviewId) { + initialized.current = true + isCancelled.current = false + + // XFDF 먼저 로드한 후 WebViewer 초기화 + loadAndInitializeViewer() + } + + return () => { + if (instance) { + try { + instance.UI.dispose() + } catch (error) { + console.warn("Error disposing viewer:", error) + } + } + isCancelled.current = true + + // 타이머 정리 + if (autoSaveTimerRef.current) { + clearTimeout(autoSaveTimerRef.current) + } + } + }, [filePath, session, documentReviewId, sessionId]) + + const loadAndInitializeViewer = async () => { + try { + // 1. 먼저 기존 XFDF 로드 + let existingXFDF = "" + try { + const response = await fetch(`/api/pdftron-comments/xfdf?documentReviewId=${documentReviewId}`) + if (response.ok) { + const data = await response.json() + if (data.xfdfString) { + existingXFDF = data.xfdfString + console.log("Loaded existing XFDF successfully") + } + } + } catch (error) { + console.error("Failed to load XFDF:", error) + } + + // 2. WebViewer 초기화 + await initializeWebViewer(existingXFDF) + + } catch (error) { + console.error("Failed to initialize viewer:", error) + setIsLoading(false) + toast({ + title: "Error", + description: "Failed to initialize document viewer", + variant: "destructive" + }) + } + } + + const initializeWebViewer = async (existingXFDF: string) => { + try { + console.log("Starting WebViewer initialization...") + console.log("File path:", filePath) + console.log("Current session:", session) + console.log("Has existing XFDF:", !!existingXFDF) + + // 동적 import 사용 + const { default: WebViewer } = await import("@pdftron/webviewer") + + if (isCancelled.current || !viewerRef.current) { + console.log("WebViewer initialization cancelled") + return + } + + // WebViewer 인스턴스 생성 + const webviewerInstance = await WebViewer( + { + path: "/pdftronWeb", + licenseKey: process.env.NEXT_PUBLIC_PDFTRON_LICENSE_KEY || process.env.NEXT_PUBLIC_PDFTRON_WEBVIEW_KEY, + initialDoc: filePath!, + }, + viewerRef.current + ) + + if (isCancelled.current) { + console.log("WebViewer initialization cancelled after creation") + return + } + + setInstance(webviewerInstance) + + if (!webviewerInstance.Core) { + console.error("WebViewer Core is not available") + setIsLoading(false) + return + } + + const { documentViewer, annotationManager, Annotations } = webviewerInstance.Core + + // 현재 사용자 설정 + const currentUser = session?.user?.email || session?.user?.name || 'Anonymous' + console.log("Setting current user:", currentUser) + annotationManager.setCurrentUser(currentUser) + + // 권한 설정 - 자기 annotation만 수정/삭제 가능 + annotationManager.setPermissionCheckCallback((author: string, annotation: any) => { + // 자기가 만든 annotation만 수정 가능 + return author === currentUser + }) + + // 문서 로드 완료 시 + documentViewer.addEventListener('documentLoaded', async () => { + console.log("Document loaded successfully") + setIsLoading(false) + + console.log(existingXFDF) + + // 기존 XFDF 적용 + if (existingXFDF && !xfdfLoadedRef.current) { + console.log(existingXFDF, "existingXFDF") + + try { + await annotationManager.importAnnotations(existingXFDF) + xfdfLoadedRef.current = true + console.log("Imported existing annotations from XFDF") + + // 초기 annotation 수 설정 + const annotations = annotationManager.getAnnotationsList() + setAnnotationCount(annotations.length) + + // 마지막 저장 시간 설정 + setLastSavedTime(new Date()) + } catch (error) { + console.error("Failed to import XFDF:", error) + toast({ + title: "Warning", + description: "Failed to load existing annotations", + variant: "destructive" + }) + } + } + + // UI 설정 (1초 지연) + setTimeout(() => { + setupUI() + }, 1000) + }) + + // UI 설정 함수 + const setupUI = async () => { + try { + console.log("Setting up UI features...") + + // Review 모드 annotation 도구 활성화 + try { + // 주석 도구 활성화 + webviewerInstance.UI.enableElements(['highlightToolButton']) + webviewerInstance.UI.enableElements(['stickyToolButton']) + webviewerInstance.UI.enableElements(['freeTextToolButton']) + webviewerInstance.UI.enableElements(['underlineToolButton']) + webviewerInstance.UI.enableElements(['strikeoutToolButton']) + webviewerInstance.UI.enableElements(['squigglyToolButton']) + + // 노트 패널 열기 + webviewerInstance.UI.openElements(['notesPanel']) + } catch (e) { + console.log("Could not enable annotation tools:", e) + } + + // 커스텀 이벤트 리스너 설정 + setupAnnotationListeners() + } catch (error) { + console.error("Error setting up UI:", error) + } + } + + // Annotation 이벤트 리스너 설정 + const setupAnnotationListeners = () => { + // 자동 저장 함수 + const handleAutoSave = async () => { + if (!documentReviewId) { + console.log("No documentReviewId, skipping auto-save") + return + } + + // 이미 저장 중이면 스킵 + if (isSaving) { + console.log("Already saving, skipping...") + return + } + + setIsSaving(true) + + try { + const xfdfString = await annotationManager.exportAnnotations() + + // Annotation 요약 정보 생성 + const annotations = annotationManager.getAnnotationsList() + const summary = { + total: annotations.length, + open: annotations.filter((a: any) => a.getCustomData('status') !== 'resolved').length, + resolved: annotations.filter((a: any) => a.getCustomData('status') === 'resolved').length, + rejected: annotations.filter((a: any) => a.getCustomData('status') === 'rejected').length, + deferred: annotations.filter((a: any) => a.getCustomData('status') === 'deferred').length, + byCategory: {} as Record, + bySeverity: {} as Record, + byAuthor: {} as Record + } + + annotations.forEach((annotation: any) => { + const category = annotation.getCustomData('category') || 'general' + const severity = annotation.getCustomData('severity') || 'minor' + const author = annotation.Author || 'Anonymous' + + summary.byCategory[category] = (summary.byCategory[category] || 0) + 1 + summary.bySeverity[severity] = (summary.bySeverity[severity] || 0) + 1 + summary.byAuthor[author] = (summary.byAuthor[author] || 0) + 1 + }) + + // 서버에 저장 + const response = await fetch('/api/pdftron-comments/xfdf', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + documentReviewId: parseInt(documentReviewId), + sessionId: sessionId ? parseInt(sessionId) :0, + pdftronDocumentId: documentId, + xfdfString: xfdfString, + commentSummary: summary, + createdByType: 'buyer' + }) + }) + + if (response.ok) { + setLastSavedTime(new Date()) + setAnnotationCount(annotations.length) + console.log("Auto-save successful") + } else { + console.error("Auto-save failed") + toast({ + title: "Error", + description: "Failed to save annotations", + variant: "destructive" + }) + } + } catch (error) { + console.error("Auto-save error:", error) + toast({ + title: "Error", + description: "Failed to save annotations", + variant: "destructive" + }) + } finally { + setIsSaving(false) + } + } + + // Annotation 변경 감지 + annotationManager.addEventListener('annotationChanged', (annotations: any[], action: string) => { + if (action === 'add' || action === 'modify' || action === 'delete') { + // 새 annotation에 기본 메타데이터 추가 + if (action === 'add') { + annotations.forEach(annotation => { + if (!annotation.getCustomData('category')) { + annotation.setCustomData('category', 'general') + annotation.setCustomData('severity', 'minor') + annotation.setCustomData('status', 'open') + annotation.setCustomData('createdBy', session?.user?.id || '') + annotation.setCustomData('createdByType', 'buyer') + annotation.setCustomData('createdAt', new Date().toISOString()) + + // 기본 색상 설정 (minor = yellow) + try { + if (Annotations) { + annotation.Color = new Annotations.Color(250, 204, 21) + } + } catch (e) { + console.log("Could not set annotation color") + } + } + }) + } + + // 자동 저장 - 2초 디바운싱 + if (autoSaveTimerRef.current) { + clearTimeout(autoSaveTimerRef.current) + } + + autoSaveTimerRef.current = setTimeout(() => { + console.log("Auto-saving annotations...") + handleAutoSave() + }, 2000) + } + }) + + // 코멘트 변경 감지 + annotationManager.addEventListener('annotationCommentsChanged', () => { + // 자동 저장 - 1.5초 디바운싱 + if (autoSaveTimerRef.current) { + clearTimeout(autoSaveTimerRef.current) + } + + autoSaveTimerRef.current = setTimeout(() => { + console.log("Auto-saving comments...") + handleAutoSave() + }, 1500) + }) + + // Annotation 선택 시 기본값 설정 + annotationManager.addEventListener('annotationSelected', (annotations: any, action: string) => { + if (annotations && annotations.length > 0) { + const annotation = annotations[0] + + // 기본 커스텀 데이터 설정 + if (!annotation.getCustomData('category')) { + annotation.setCustomData('category', 'general') + annotation.setCustomData('severity', 'minor') + annotation.setCustomData('status', 'open') + annotation.setCustomData('createdBy', session?.user?.id || '') + annotation.setCustomData('createdByType', 'buyer') + annotation.setCustomData('createdAt', new Date().toISOString()) + } + } + }) + } + + } catch (error) { + console.error("WebViewer initialization failed:", error) + setIsLoading(false) + toast({ + title: "Error", + description: "Failed to initialize document viewer", + variant: "destructive" + }) + } + } + + + + // 통계 정보 가져오기 + const getAnnotationStats = () => { + if (!instance) return null + + const { annotationManager } = instance.Core + const annotations = annotationManager.getAnnotationsList() + + return { + total: annotations.length, + open: annotations.filter((a: any) => a.getCustomData('status') !== 'resolved').length, + resolved: annotations.filter((a: any) => a.getCustomData('status') === 'resolved').length + } + } + + // 시간 포맷팅 + const formatLastSaved = () => { + if (!lastSavedTime) return null + + const now = new Date() + const diff = Math.floor((now.getTime() - lastSavedTime.getTime()) / 1000) + + if (diff < 60) return "Just saved" + if (diff < 3600) return `Saved ${Math.floor(diff / 60)} min ago` + if (diff < 86400) return `Saved ${Math.floor(diff / 3600)} hours ago` + return `Saved ${Math.floor(diff / 86400)} days ago` + } + + const stats = getAnnotationStats() + const lastSavedText = formatLastSaved() + + return ( +
+ {/* Header */} +
+
+ +
+

{documentName || 'Document Viewer'}

+
+ Review Mode + + User: {session?.user?.email || session?.user?.name || 'Loading...'} + {stats && stats.total > 0 && ( + <> + + + + {stats.open} open / {stats.total} total + + + )} + {isSaving && ( + <> + + +
Auto-saving...
+
+ + )} + {!isSaving && lastSavedText && ( + <> + + + ✓ {lastSavedText} + + + )} +
+
+
+ +
+ + {/* PDFTron Viewer */} +
+ {(isLoading || status === "loading") && ( +
+
+
+

+ {status === "loading" ? "Loading session..." : "Loading document..."} +

+

+ Initializing PDFTron viewer... +

+
+
+ )} +
+
+
+ ) +} \ No newline at end of file diff --git a/app/api/contracts/prepare-template/route.ts b/app/api/contracts/prepare-template/route.ts index 189643b5..7d0f39c6 100644 --- a/app/api/contracts/prepare-template/route.ts +++ b/app/api/contracts/prepare-template/route.ts @@ -5,7 +5,7 @@ import { eq, and, ilike } from "drizzle-orm"; export async function POST(request: NextRequest) { try { - const { templateName, vendorId } = await request.json(); + const { templateName, vendorId, biddingId, biddingCompanyId } = await request.json(); // 템플릿 조회 const [template] = await db @@ -65,7 +65,7 @@ export async function POST(request: NextRequest) { business_size: vendor.businessSize || '', credit_rating: vendor.creditRating || '', template_type: templateName, - contract_number: `BC-${new Date().getFullYear()}-${String(vendorId).padStart(4, '0')}-${Date.now()}`, + contract_number: `BC-${new Date().getFullYear()}-${biddingId || '0'}-${String(vendorId).padStart(4, '0')}-${Date.now()}`, }; return NextResponse.json({ diff --git a/app/api/document-reviews/[id]/route.ts b/app/api/document-reviews/[id]/route.ts new file mode 100644 index 00000000..472f93bf --- /dev/null +++ b/app/api/document-reviews/[id]/route.ts @@ -0,0 +1,138 @@ +// app/api/document-reviews/[id]/route.ts + +import { NextRequest, NextResponse } from "next/server" +import db from "@/db/db" +import { rfqLastTbeDocumentReviews } from "@/db/schema" +import { eq } from "drizzle-orm" +import { getServerSession } from "next-auth" +import { authOptions } from "@/app/api/auth/[...nextauth]/route" +import { revalidateTag } from "next/cache" + +// PATCH - 문서 리뷰 업데이트 +export async function PATCH( + request: NextRequest, + { params }: { params: { id: string } } +) { + try { + const session = await getServerSession(authOptions) + if (!session?.user) { + return NextResponse.json({ error: "인증이 필요합니다." }, { status: 401 }) + } + + const reviewId = parseInt(params.id) + if (!reviewId) { + return NextResponse.json({ error: "Invalid review ID" }, { status: 400 }) + } + + const body = await request.json() + const { reviewStatus, reviewComments } = body + + // 현재 문서 리뷰 조회 + const [currentReview] = await db + .select() + .from(rfqLastTbeDocumentReviews) + .where(eq(rfqLastTbeDocumentReviews.id, reviewId)) + .limit(1) + + if (!currentReview) { + return NextResponse.json({ error: "Review not found" }, { status: 404 }) + } + + // 권한 체크 - 구매자만 리뷰 가능 (또는 admin) + const userId = typeof session.user.id === 'string' ? parseInt(session.user.id) : session.user.id + const isAdmin = (session.user as any).roles?.includes('admin') || false + + // 여기서는 구매자 권한 체크를 간단히 처리 + // 실제로는 세션의 role이나 type을 확인해야 함 + + // 업데이트할 데이터 준비 + const updateData: any = { + updatedAt: new Date() + } + + if (reviewStatus !== undefined) { + updateData.reviewStatus = reviewStatus + } + + if (reviewComments !== undefined) { + updateData.reviewComments = reviewComments + } + + // 리뷰 상태가 변경되면 관련 필드도 업데이트 + if (reviewStatus && reviewStatus !== currentReview.reviewStatus) { + updateData.reviewedBy = userId + updateData.reviewedAt = new Date() + + // 상태에 따른 추가 필드 설정 + switch (reviewStatus) { + case "승인": + updateData.technicalCompliance = true + updateData.qualityAcceptable = true + updateData.requiresRevision = false + break + case "반려": + updateData.technicalCompliance = false + updateData.qualityAcceptable = false + updateData.requiresRevision = true + break + case "보류": + updateData.requiresRevision = true + break + } + } + + // 업데이트 실행 + const [updated] = await db + .update(rfqLastTbeDocumentReviews) + .set(updateData) + .where(eq(rfqLastTbeDocumentReviews.id, reviewId)) + .returning() + + // 캐시 초기화 + if (currentReview.tbeSessionId) { + revalidateTag(`tbe-session-${currentReview.tbeSessionId}`) + } + + return NextResponse.json(updated) + } catch (error) { + console.error("Failed to update document review:", error) + return NextResponse.json({ + error: "Failed to update document review" + }, { status: 500 }) + } +} + +// GET - 문서 리뷰 조회 +export async function GET( + request: NextRequest, + { params }: { params: { id: string } } +) { + try { + const session = await getServerSession(authOptions) + if (!session?.user) { + return NextResponse.json({ error: "인증이 필요합니다." }, { status: 401 }) + } + + const reviewId = parseInt(params.id) + if (!reviewId) { + return NextResponse.json({ error: "Invalid review ID" }, { status: 400 }) + } + + const [review] = await db + .select() + .from(rfqLastTbeDocumentReviews) + .where(eq(rfqLastTbeDocumentReviews.id, reviewId)) + .limit(1) + + if (!review) { + return NextResponse.json({ error: "Review not found" }, { status: 404 }) + } + + return NextResponse.json(review) + } catch (error) { + console.error("Failed to fetch document review:", error) + return NextResponse.json({ + error: "Failed to fetch document review" + }, { status: 500 }) + } +} \ No newline at end of file diff --git a/app/api/files/[...path]/route.ts b/app/api/files/[...path]/route.ts index 3fb60347..88211f5b 100644 --- a/app/api/files/[...path]/route.ts +++ b/app/api/files/[...path]/route.ts @@ -31,6 +31,7 @@ const getMimeType = (filePath: string): string => { const isAllowedPath = (requestedPath: string): boolean => { const allowedPaths = [ 'basicContract', + 'contracts', 'basicContract/template', 'basicContract/signed', 'vendorFormReportSample', @@ -64,7 +65,12 @@ export async function GET( ) { try { // 요청된 파일 경로 구성 - const requestedPath = params.path.join('/'); + const decodedPath = params.path.map(segment => + decodeURIComponent(segment) + ); + + // 디코딩된 경로로 조합 + const requestedPath = decodedPath.join('/'); console.log(`📂 파일 요청: ${requestedPath}`); @@ -124,10 +130,14 @@ export async function GET( console.log(`✅ 파일 서빙 성공: ${fileName} (${stats.size} bytes)`); - // ✅ Content-Disposition 헤더 결정 + const encodedFileName = encodeURIComponent(fileName) + .replace(/'/g, "%27") + .replace(/"/g, "%22"); + const contentDisposition = forceDownload - ? `attachment; filename="${fileName}"` // 강제 다운로드 - : `inline; filename="${fileName}"`; // 브라우저에서 열기 + ? `attachment; filename="${encodedFileName}"; filename*=UTF-8''${encodedFileName}` + : `inline; filename="${encodedFileName}"; filename*=UTF-8''${encodedFileName}`; + // Range 요청 처리 (큰 파일의 부분 다운로드 지원) const range = request.headers.get('range'); @@ -176,7 +186,12 @@ export async function HEAD( { params }: { params: { path: string[] } } ) { try { - const requestedPath = params.path.join('/'); + const decodedPath = params.path.map(segment => + decodeURIComponent(segment) + ); + + // 디코딩된 경로로 조합 + const requestedPath = decodedPath.join('/'); // ✅ HEAD 요청에서도 다운로드 강제 여부 확인 const url = new URL(request.url); @@ -207,11 +222,16 @@ export async function HEAD( const mimeType = getMimeType(filePath); const fileName = path.basename(filePath); - // ✅ HEAD 요청에서도 Content-Disposition 헤더 적용 - const contentDisposition = forceDownload - ? `attachment; filename="${fileName}"` // 강제 다운로드 - : `inline; filename="${fileName}"`; // 브라우저에서 열기 + const encodedFileName = encodeURIComponent(fileName) + .replace(/'/g, "%27") + .replace(/"/g, "%22"); + + const contentDisposition = forceDownload + ? `attachment; filename="${encodedFileName}"; filename*=UTF-8''${encodedFileName}` + : `inline; filename="${encodedFileName}"; filename*=UTF-8''${encodedFileName}`; + + return new NextResponse(null, { headers: { 'Content-Type': mimeType, diff --git a/app/api/partners/rfq-last/[id]/response/route.ts b/app/api/partners/rfq-last/[id]/response/route.ts index db320dde..1fc9d5dd 100644 --- a/app/api/partners/rfq-last/[id]/response/route.ts +++ b/app/api/partners/rfq-last/[id]/response/route.ts @@ -156,7 +156,10 @@ export async function POST( const fileRecords = [] if (files.length > 0) { - for (const file of files) { + for (let i = 0; i < files.length; i++) { + const file = files[i] + const metadata = data.fileMetadata?.[i] // 인덱스로 메타데이터 매칭 + try { const filename = `${uuidv4()}_${file.name.replace(/[^a-zA-Z0-9.-]/g, '_')}` const filepath = path.join(uploadDir, filename) @@ -165,28 +168,25 @@ export async function POST( if (file.size > 50 * 1024 * 1024) { // 50MB 이상 await saveFileStream(file, filepath) } else { - // 작은 파일은 기존 방식 const buffer = Buffer.from(await file.arrayBuffer()) await writeFile(filepath, buffer) } fileRecords.push({ vendorResponseId: result.id, - attachmentType: (file as any).attachmentType || "기타", + attachmentType: metadata?.attachmentType || "기타", // 메타데이터에서 가져옴 fileName: filename, originalFileName: file.name, filePath: `/uploads/rfq/${rfqId}/${filename}`, fileSize: file.size, fileType: file.type, - description: (file as any).description, + description: metadata?.description || "", // 메타데이터에서 가져옴 uploadedBy: session.user.id, }) } catch (fileError) { console.error(`Failed to save file ${file.name}:`, fileError) - // 파일 저장 실패 시 계속 진행 (다른 파일들은 저장) } } - // DB에 파일 정보 저장 if (fileRecords.length > 0) { await db.insert(rfqLastVendorAttachments).values(fileRecords) diff --git a/app/api/partners/tbe/[sessionId]/documents/route.ts b/app/api/partners/tbe/[sessionId]/documents/route.ts new file mode 100644 index 00000000..0045ea43 --- /dev/null +++ b/app/api/partners/tbe/[sessionId]/documents/route.ts @@ -0,0 +1,275 @@ +// app/api/partners/tbe/[sessionId]/documents/route.ts +import { NextRequest, NextResponse } from "next/server" +import { getServerSession } from "next-auth/next" +import { authOptions } from "@/app/api/auth/[...nextauth]/route" +import db from "@/db/db" +import { + rfqLastTbeDocumentReviews, + rfqLastTbeSessions, + rfqLastTbeHistory, + rfqLastTbeVendorDocuments +} from "@/db/schema" +import { eq, and } from "drizzle-orm" +import { writeFile, mkdir } from "fs/promises" +import { createWriteStream } from "fs" +import { pipeline } from "stream/promises" +import path from "path" +import { v4 as uuidv4 } from "uuid" + +// 1GB 파일 지원을 위한 설정 +export const config = { + api: { + bodyParser: { + sizeLimit: '1gb', + }, + responseLimit: false, + }, +} + +// 스트리밍으로 파일 저장 +async function saveFileStream(file: File, filepath: string) { + const stream = file.stream() + const writeStream = createWriteStream(filepath) + await pipeline(stream, writeStream) +} + +// POST: TBE 문서 업로드 +export async function POST(request: NextRequest, { params }: { params: { sessionId: string } }) { + try { + const session = await getServerSession(authOptions) + if (!session?.user || session.user.domain !== "partners") { + return NextResponse.json({ error: "Unauthorized" }, { status: 401 }) + } + + const tbeSessionId = Number(params.sessionId) + const formData = await request.formData() + + // ✅ 프런트 기frfqLastTbeVendorDocuments본값 'other' 등을 안전한 enum으로 매핑 + const documentType = (formData.get("documentType") as string | undefined) + const documentName = (formData.get("documentName") as string | undefined)?.trim() || "Untitled" + const description = (formData.get("description") as string | undefined) || "" + const file = formData.get("file") as File | null + + if (!file) { + return NextResponse.json({ error: "파일이 필요합니다" }, { status: 400 }) + } + + // 세션/권한 + const tbeSession = await db.query.rfqLastTbeSessions.findFirst({ + where: eq(rfqLastTbeSessions.id, tbeSessionId), + with: { vendor: true }, + }) + if (!tbeSession) return NextResponse.json({ error: "TBE 세션을 찾을 수 없습니다" }, { status: 404 }) + + // 권한 체크: 회사 기준으로 통일 (위/아래 GET도 동일 기준을 권장) + if (tbeSession.vendor?.id !== session.user.companyId) { + return NextResponse.json({ error: "권한이 없습니다" }, { status: 403 }) + } + + // 저장 경로 + const isDev = process.env.NODE_ENV === "development" + const uploadDir = isDev + ? path.join(process.cwd(), "public", "uploads", "tbe", String(tbeSessionId), "vendor") + : path.join(process.env.NAS_PATH || "/nas", "uploads", "tbe", String(tbeSessionId), "vendor") + + await mkdir(uploadDir, { recursive: true }) + + const safeOriginal = file.name.replace(/[^a-zA-Z0-9.\-_\s]/g, "_") + const filename = `${uuidv4()}_${safeOriginal}` + const filepath = path.join(uploadDir, filename) + + try { + if (file.size > 50 * 1024 * 1024) { + await saveFileStream(file, filepath) + } else { + const buffer = Buffer.from(await file.arrayBuffer()) + await writeFile(filepath, buffer) + } + } catch (e) { + console.error("파일 저장 실패:", e) + return NextResponse.json({ error: "파일 저장에 실패했습니다" }, { status: 500 }) + } + + // 트랜잭션 + const result = await db.transaction(async (tx) => { + // 1) 벤더 업로드 문서 insert + const [vendorDoc] = await tx + .insert(rfqLastTbeVendorDocuments) + .values({ + tbeSessionId, + documentType, // enum 매핑된 값 + isResponseToReviewId: null, // 필요 시 formData에서 받아 세팅 + fileName: filename, + originalFileName: file.name, + filePath: `/uploads/tbe/${tbeSessionId}/vendor/${filename}`, + fileSize: Number(file.size), + fileType: file.type || null, + documentNo: null, + revisionNo: null, + issueDate: null, + description, + submittalRemarks: null, + reviewRequired: true, + reviewStatus: "pending", + submittedBy: session.user.id, + submittedAt: new Date(), + reviewedBy: null, + reviewedAt: null, + reviewComments: null, + }) + .returning() + + // 2) (선택) 기존 리뷰 테이블에도 “벤더가 올린 검토대상 문서”로 남기고 싶다면 유지 + // 필요 없다면 아래 블록은 제거 가능 + const [documentReview] = await tx + .insert(rfqLastTbeDocumentReviews) + .values({ + tbeSessionId, + vendorAttachmentId:vendorDoc.id, + documentSource: "vendor", + documentType: documentType, // 동일 매핑 + documentName: documentName, // UX 표시용 이름 + reviewStatus: "미검토", + reviewComments: description, + createdAt: new Date(), + updatedAt: new Date(), + }) + .returning() + + // 3) 세션 상태 전환 + if (tbeSession.status === "준비중") { + await tx + .update(rfqLastTbeSessions) + .set({ + status: "진행중", + actualStartDate: new Date(), + updatedAt: new Date(), + updatedBy: session.user.id, + }) + .where(eq(rfqLastTbeSessions.id, tbeSessionId)) + } + + // 4) 이력 + await tx.insert(rfqLastTbeHistory).values({ + tbeSessionId, + actionType: "document_review", + changeDescription: `벤더 문서 업로드: ${documentName}`, + changeDetails: { + vendorDocumentId: vendorDoc.id, + documentReviewId: documentReview.id, + documentName: documentName, + documentType: documentType, + filePath: vendorDoc.filePath, + }, + performedBy: session.user.id, + performedByType: "vendor", + performedAt: new Date(), + }) + + if (tbeSession.status === "준비중") { + await tx.insert(rfqLastTbeHistory).values({ + tbeSessionId, + actionType: "status_change", + previousStatus: "준비중", + newStatus: "진행중", + changeDescription: "벤더 문서 업로드로 인한 상태 변경", + performedBy: session.user.id, + performedByType: "vendor", + performedAt: new Date(), + }) + } + + return { + vendorDoc, + documentReview, + } + }) + + return NextResponse.json({ + success: true, + data: { + vendorDocumentId: result.vendorDoc.id, + filePath: result.vendorDoc.filePath, + originalFileName: result.vendorDoc.originalFileName, + fileSize: result.vendorDoc.fileSize, + fileType: result.vendorDoc.fileType, + }, + message: "문서가 성공적으로 업로드되었습니다", + }) + } catch (error) { + console.error("TBE 문서 업로드 오류:", error) + return NextResponse.json({ error: "문서 업로드에 실패했습니다" }, { status: 500 }) + } +} + +// GET: TBE 세션의 문서 목록 조회 +export async function GET( + request: NextRequest, + { params }: { params: { sessionId: string } } +) { + try { + const session = await getServerSession(authOptions) + if (!session?.user || session.user.domain !== "partners") { + return NextResponse.json({ error: "Unauthorized" }, { status: 401 }) + } + + const tbeSessionId = parseInt(params.sessionId) + + // TBE 세션 확인 및 권한 체크 + const tbeSession = await db.query.rfqLastTbeSessions.findFirst({ + where: eq(rfqLastTbeSessions.id, tbeSessionId), + with: { + vendor: true, + documentReviews: { + orderBy: (reviews, { desc }) => [desc(reviews.createdAt)], + } + } + }) + + if (!tbeSession) { + return NextResponse.json({ error: "TBE 세션을 찾을 수 없습니다" }, { status: 404 }) + } + + // 벤더 권한 확인 + if (tbeSession.vendor.userId !== session.user.id) { + return NextResponse.json({ error: "권한이 없습니다" }, { status: 403 }) + } + + // PDFTron 코멘트 수 집계 (필요시) + const documentsWithDetails = await Promise.all( + tbeSession.documentReviews.map(async (doc) => { + // PDFTron 코멘트 수 조회 + const pdftronComments = await db.query.rfqLastTbePdftronComments.findFirst({ + where: eq(rfqLastTbePdftronComments.documentReviewId, doc.id), + }) + + return { + ...doc, + comments: pdftronComments?.commentSummary || { + totalCount: 0, + openCount: 0, + }, + } + }) + ) + + return NextResponse.json({ + success: true, + session: { + id: tbeSession.id, + sessionCode: tbeSession.sessionCode, + sessionTitle: tbeSession.sessionTitle, + sessionStatus: tbeSession.status, + evaluationResult: tbeSession.evaluationResult, + }, + documents: documentsWithDetails, + }) + + } catch (error) { + console.error("문서 목록 조회 오류:", error) + return NextResponse.json( + { error: "문서 목록 조회에 실패했습니다" }, + { status: 500 } + ) + } +} \ No newline at end of file diff --git a/app/api/pdftron-comments/xfdf/count/route.ts b/app/api/pdftron-comments/xfdf/count/route.ts new file mode 100644 index 00000000..19127ea9 --- /dev/null +++ b/app/api/pdftron-comments/xfdf/count/route.ts @@ -0,0 +1,171 @@ +// app/api/pdftron-comments/xfdf/count/route.ts +import { NextRequest, NextResponse } from "next/server" +import { getServerSession } from "next-auth" +import { authOptions } from "@/app/api/auth/[...nextauth]/route" +import db from "@/db/db" +import { rfqLastTbePdftronComments } from "@/db/schema" +import { inArray } from "drizzle-orm" +import { parseStringPromise } from "xml2js" + +type Counts = { totalCount: number; openCount: number } + +function fromCommentSummary(summary: any | null | undefined): Counts | null { + if (!summary) return null + // commentSummary가 다음 형태를 따른다고 가정: + // { totalCount?: number, openCount?: number } 또는 유사 구조 + const t = Number((summary as any)?.totalCount) + const o = Number((summary as any)?.openCount) + if (Number.isFinite(t)) { + return { totalCount: t, openCount: Number.isFinite(o) ? o : t } + } + return null +} + +async function fromXfdfString(xfdf: string | null | undefined): Promise { + if (!xfdf) return null + try { + const xml = await parseStringPromise(xfdf, { explicitArray: true }) + // XFDF 기본 구조: xfdf.annotations[0].annotation = [...] + const ann = + xml?.xfdf?.annotations?.[0]?.annotation ?? + xml?.xfdf?.fdf?.annots?.[0]?.annot ?? + [] // 방어적 + const total = Array.isArray(ann) ? ann.length : 0 + + // “오픈/클로즈드” 판단 로직은 팀의 규칙에 맞게 조정: + // - 상태(StateModel/State) 혹은 CustomData를 쓰는 경우가 많음. + // - 기본 폴백: 전부 오픈으로 간주. + let open = total + + // 예: Completed 이면 클로즈드로 처리 + // (실제 저장 스키마에 맞춰 커스터마이즈하세요.) + let closed = 0 + if (Array.isArray(ann)) { + for (const a of ann) { + const status = + a?.status?.[0] || + a?.["it:status"]?.[0] || + a?.state?.[0] || + a?.custom?.[0]?.status?.[0] + if ( + typeof status === "string" && + ["Completed", "Resolved", "Accepted", "Rejected", "Closed"].includes(status) + ) { + closed += 1 + } + } + } + open = Math.max(total - closed, 0) + + return { totalCount: total, openCount: open } + } catch { + return null + } +} + + +type CommentSummary = { + total?: number + open?: number + resolved?: number + rejected?: number + deferred?: number + byAuthor?: Record + byCategory?: Record + bySeverity?: Record +} + +type Counts = { totalCount: number; openCount: number } + +function countsFromSummary(s?: CommentSummary | null): Counts | null { + if (!s) return null + + // 1) open이 있으면 그걸 신뢰 + if (Number.isFinite(s.open) && Number.isFinite(s.total)) { + return { totalCount: s.total!, openCount: s.open! } + } + + // 2) open이 없으면 상태 기반으로 계산 + if (Number.isFinite(s.total)) { + const resolved = Number(s.resolved ?? 0) + const rejected = Number(s.rejected ?? 0) + const deferred = Number(s.deferred ?? 0) + const open = Math.max(s.total! - resolved - rejected - deferred, 0) + return { totalCount: s.total!, openCount: open } + } + + // 3) total이 누락된 희귀 케이스 → 분포 합으로 추정 + const sum = (...recs: (Record | undefined)[]) => + recs.reduce((acc, r) => acc + (r ? Object.values(r).reduce((a, b) => a + (b || 0), 0) : 0), 0) + + const guessedTotal = sum(s.byAuthor, s.byCategory, s.bySeverity) + if (guessedTotal > 0) { + const open = Number(s.open ?? Math.max(guessedTotal - Number(s.resolved ?? 0) - Number(s.rejected ?? 0) - Number(s.deferred ?? 0), 0)) + return { totalCount: guessedTotal, openCount: open } + } + + return null +} + +export async function GET(request: NextRequest) { + try { + const session = await getServerSession(authOptions) + if (!session?.user) { + return NextResponse.json({ error: "인증이 필요합니다." }, { status: 401 }) + } + + const idsParam = request.nextUrl.searchParams.get("ids") + if (!idsParam) { + return NextResponse.json({ error: "ids is required (comma-separated)" }, { status: 400 }) + } + + const ids = idsParam + .split(",") + .map((s) => s.trim()) + .filter(Boolean) + .map((s) => Number(s)) + .filter((n) => Number.isFinite(n)) + + if (ids.length === 0) { + return NextResponse.json({ error: "no valid ids" }, { status: 400 }) + } + + // 한 번에 조회 + const rows = await db + .select() + .from(rfqLastTbePdftronComments) + .where(inArray(rfqLastTbePdftronComments.documentReviewId, ids)) + + const result: Record< + number, + { totalCount: number; openCount: number; updatedAt: string | null } + > = {} + + // 기본값: 코멘트 없음 → 0/0 + for (const id of ids) { + result[id] = { totalCount: 0, openCount: 0, updatedAt: null } + } + + // 요약 우선 → XFDF 파싱 폴백 + await Promise.all( + rows.map(async (r: any) => { + const id = Number(r.documentReviewId) + let counts = + countsFromSummary(r.commentSummary as CommentSummary) || + (await fromXfdfString(r.xfdfString)) || // 폴백 + { totalCount: 0, openCount: 0 } + + result[id] = { + totalCount: counts.totalCount, + openCount: counts.openCount, + updatedAt: r.updatedAt ?? null, + } + }) + ) + + return NextResponse.json({ data: result }) + } catch (err) { + console.error("xfdf/count GET error:", err) + return NextResponse.json({ error: "Failed to fetch counts" }, { status: 500 }) + } +} diff --git a/app/api/pdftron-comments/xfdf/route.ts b/app/api/pdftron-comments/xfdf/route.ts new file mode 100644 index 00000000..f2cd7b81 --- /dev/null +++ b/app/api/pdftron-comments/xfdf/route.ts @@ -0,0 +1,362 @@ +// app/api/pdftron-comments/xfdf/route.ts + +import { NextRequest, NextResponse } from "next/server" +import db from "@/db/db" +import { rfqLastTbePdftronComments } from "@/db/schema" +import { eq, and, desc } from "drizzle-orm" +import { getServerSession } from "next-auth" +import { authOptions } from "@/app/api/auth/[...nextauth]/route" +import { parseStringPromise } from "xml2js" +import { revalidateTag } from "next/cache" + +// GET - XFDF 조회 +export async function GET(request: NextRequest) { + try { + const session = await getServerSession(authOptions) + if (!session?.user) { + return NextResponse.json({ error: "인증이 필요합니다." }, { status: 401 }) + } + + const searchParams = request.nextUrl.searchParams + const documentReviewId = searchParams.get('documentReviewId') + + if (!documentReviewId) { + return NextResponse.json({ error: "documentReviewId is required" }, { status: 400 }) + } + + // 해당 문서의 코멘트 조회 + const [comment] = await db + .select() + .from(rfqLastTbePdftronComments) + .where( + eq(rfqLastTbePdftronComments.documentReviewId, parseInt(documentReviewId)) + ) + .limit(1) + + if (!comment) { + return NextResponse.json({ xfdfString: null }) + } + + // 권한 체크 + const userId = typeof session.user.id === 'string' ? parseInt(session.user.id) : session.user.id + const isAdmin = (session.user as any).roles?.includes('admin') || false + const canEdit = comment.createdBy === userId || isAdmin + + return NextResponse.json({ + xfdfString: comment.xfdfString, + annotationData: comment.annotationData, + commentSummary: comment.commentSummary, + canEdit: canEdit, + createdBy: comment.createdBy, + createdByType: comment.createdByType, + lastModifiedBy: comment.lastModifiedBy, + updatedAt: comment.updatedAt + }) + } catch (error) { + console.error("Failed to fetch XFDF:", error) + return NextResponse.json({ error: "Failed to fetch XFDF" }, { status: 500 }) + } +} + +// POST - XFDF 저장 (upsert 방식) +export async function POST(request: NextRequest) { + try { + const session = await getServerSession(authOptions) + if (!session?.user) { + return NextResponse.json({ error: "인증이 필요합니다." }, { status: 401 }) + } + + const body = await request.json() + const { + documentReviewId, + sessionId, + pdftronDocumentId, + xfdfString, + commentSummary, + createdByType + } = body + + // 필수 필드 검증 + if (!documentReviewId || !pdftronDocumentId || !xfdfString) { + return NextResponse.json({ + error: "Missing required fields" + }, { status: 400 }) + } + + const userId = typeof session.user.id === 'string' ? parseInt(session.user.id) : session.user.id + const isAdmin = (session.user as any).roles?.includes('admin') || false + + // XFDF 파싱하여 annotation 데이터 추출 + const annotationData = await parseXFDF(xfdfString) + + // 트랜잭션으로 처리 + const result = await db.transaction(async (tx) => { + // 기존 코멘트 확인 + const [existing] = await tx + .select() + .from(rfqLastTbePdftronComments) + .where( + and( + eq(rfqLastTbePdftronComments.documentReviewId, parseInt(documentReviewId)), + eq(rfqLastTbePdftronComments.pdftronDocumentId, pdftronDocumentId) + ) + ) + .limit(1) + + if (existing) { + // 권한 체크 - 다른 사용자의 annotation 수정 방지 + if (!isAdmin) { + const currentAnnotations = existing.annotationData?.annotations || [] + const newAnnotations = annotationData.annotations || [] + + // 다른 사용자가 만든 annotation이 수정/삭제되었는지 체크 + for (const oldAnn of currentAnnotations) { + // 다른 사용자가 만든 annotation + if (oldAnn.customData?.createdBy && oldAnn.customData.createdBy !== userId) { + const newAnn = newAnnotations.find((n: any) => n.id === oldAnn.id) + + // 삭제되었거나 수정되었으면 에러 + if (!newAnn || JSON.stringify(newAnn) !== JSON.stringify(oldAnn)) { + throw new Error("You can only modify your own annotations") + } + } + } + } + + // 기존 레코드 업데이트 + const [updated] = await tx + .update(rfqLastTbePdftronComments) + .set({ + xfdfString, + annotationData, + commentSummary, + lastModifiedBy: userId, + updatedAt: new Date() + }) + .where(eq(rfqLastTbePdftronComments.id, existing.id)) + .returning() + + return updated + } else { + // 새 레코드 삽입 + const [inserted] = await tx + .insert(rfqLastTbePdftronComments) + .values({ + documentReviewId: parseInt(documentReviewId), + pdftronDocumentId, + xfdfString, + annotationData, + commentSummary, + createdBy: userId, + createdByType: createdByType || 'buyer', + lastModifiedBy: userId, + createdAt: new Date(), + updatedAt: new Date() + }) + .returning() + + return inserted + } + }) + + revalidateTag(`tbe-session-${sessionId}`) + + + return NextResponse.json(result) + } catch (error: any) { + console.error("Failed to save XFDF:", error) + + if (error.message === "You can only modify your own annotations") { + return NextResponse.json({ + error: "You can only modify your own annotations" + }, { status: 403 }) + } + + return NextResponse.json({ error: "Failed to save XFDF" }, { status: 500 }) + } +} + +// DELETE - XFDF 삭제 +export async function DELETE(request: NextRequest) { + try { + const session = await getServerSession(authOptions) + if (!session?.user) { + return NextResponse.json({ error: "인증이 필요합니다." }, { status: 401 }) + } + + const searchParams = request.nextUrl.searchParams + const documentReviewId = searchParams.get('documentReviewId') + const tbeSessionId = searchParams.get('sessionId') + + if (!documentReviewId) { + return NextResponse.json({ + error: "Missing required parameters" + }, { status: 400 }) + } + + const userId = typeof session.user.id === 'string' ? parseInt(session.user.id) : session.user.id + const isAdmin = (session.user as any).roles?.includes('admin') || false + + // 권한 체크 + const [existing] = await db + .select() + .from(rfqLastTbePdftronComments) + .where( + eq(rfqLastTbePdftronComments.documentReviewId, parseInt(documentReviewId)) + ) + .limit(1) + + if (!existing) { + return NextResponse.json({ error: "Comment not found" }, { status: 404 }) + } + + if (existing.createdBy !== userId && !isAdmin) { + return NextResponse.json({ error: "Unauthorized" }, { status: 403 }) + } + + // 삭제 + await db + .delete(rfqLastTbePdftronComments) + .where(eq(rfqLastTbePdftronComments.id, existing.id)) + + revalidateTag(`tbe-session-${tbeSessionId}`) + + + return NextResponse.json({ success: true }) + } catch (error) { + console.error("Failed to delete XFDF:", error) + return NextResponse.json({ error: "Failed to delete XFDF" }, { status: 500 }) + } +} + +// XFDF 파싱 함수 - xml2js 사용 +async function parseXFDF(xfdfString: string): Promise { + try { + // xml2js로 파싱 + const result = await parseStringPromise(xfdfString, { + explicitArray: false, + ignoreAttrs: false, + mergeAttrs: false, + explicitRoot: false, + tagNameProcessors: [(name) => name.toLowerCase()] + }) + + const annotations: any[] = [] + + // annots 노드 확인 + const annots = result?.annots + if (!annots) { + return { annotations: [] } + } + + // 모든 annotation 타입 처리 + const annotTypes = [ + 'highlight', 'text', 'freetext', 'ink', 'square', + 'circle', 'line', 'polygon', 'polyline', 'stamp', + 'caret', 'fileattachment', 'sound', 'strikeout', + 'underline', 'squiggly', 'redact' + ] + + for (const type of annotTypes) { + const items = annots[type] + if (!items) continue + + // 배열이 아니면 배열로 변환 + const itemArray = Array.isArray(items) ? items : [items] + + for (const item of itemArray) { + const annotation: any = { + id: item.$?.name || '', + type: type, + page: parseInt(item.$?.page || '1'), + author: item.$?.title || '', + subject: item.$?.subject || '', + createdDate: item.$?.creationdate || '', + modifiedDate: item.$?.date || '', + } + + // contents 가져오기 + if (item.contents) { + annotation.contents = typeof item.contents === 'string' + ? item.contents + : item.contents._ || '' + } + + // color 가져오기 + if (item.$?.color) { + annotation.color = item.$.color + } + + // opacity 가져오기 + if (item.$?.opacity) { + annotation.opacity = parseFloat(item.$.opacity) + } + + // custom data 가져오기 + if (item.customdata) { + annotation.customData = {} + const properties = item.customdata.property + if (properties) { + const propArray = Array.isArray(properties) ? properties : [properties] + for (const prop of propArray) { + const name = prop.$?.name + const value = prop._ || prop + if (name && value) { + // 숫자 타입 변환 + if (name === 'createdBy' || name === 'resolvedBy') { + annotation.customData[name] = parseInt(value) + } else { + annotation.customData[name] = value + } + } + } + } + } + + // replies 가져오기 + if (item.reply) { + annotation.replies = [] + const replies = Array.isArray(item.reply) ? item.reply : [item.reply] + for (const reply of replies) { + annotation.replies.push({ + author: reply.$?.title || '', + contents: typeof reply.contents === 'string' + ? reply.contents + : reply.contents?._ || '', + createdDate: reply.$?.creationdate || '' + }) + } + } + + // coords 가져오기 (rect, vertices 등) + if (item.$?.rect) { + annotation.coords = item.$.rect.split(',').map(Number) + } else if (item.$?.vertices) { + annotation.coords = item.$.vertices.split(';').join(',').split(',').map(Number) + } else if (item.$?.coords) { + annotation.coords = item.$.coords.split(',').map(Number) + } + + // appearance 정보 + if (item.appearance) { + annotation.appearance = item.appearance + } + + // popup 정보 + if (item.popup) { + annotation.popup = { + open: item.popup.$?.open === 'true', + rect: item.popup.$?.rect + } + } + + annotations.push(annotation) + } + } + + return { annotations } + } catch (error) { + console.error("Failed to parse XFDF:", error) + return { annotations: [] } + } +} \ No newline at end of file diff --git a/app/api/tbe/sessions/[sessionId]/vendor-questions/route.ts b/app/api/tbe/sessions/[sessionId]/vendor-questions/route.ts new file mode 100644 index 00000000..8308b040 --- /dev/null +++ b/app/api/tbe/sessions/[sessionId]/vendor-questions/route.ts @@ -0,0 +1,131 @@ +// app/api/tbe/sessions/[sessionId]/vendor-questions/route.ts + +import { NextRequest, NextResponse } from "next/server" +import { getServerSession } from "next-auth" +import { authOptions } from "@/app/api/auth/[...nextauth]/route" +import { + addVendorQuestion, + getVendorQuestions, + answerVendorQuestion +} from "@/lib/tbe-last/vendor-tbe-service" + +interface Props { + params: { + sessionId: string + } +} + +// GET: 질문 목록 조회 +export async function GET( + request: NextRequest, + { params }: Props +) { + try { + const session = await getServerSession(authOptions) + if (!session?.user?.companyId) { + return NextResponse.json( + { error: "Unauthorized" }, + { status: 401 } + ) + } + + const vendorId = typeof session.user.companyId === 'string' + ? parseInt(session.user.companyId) + : session.user.companyId + + const sessionId = parseInt(params.sessionId) + + const questions = await getVendorQuestions(sessionId, vendorId) + + return NextResponse.json(questions) + + } catch (error) { + console.error("Get questions error:", error) + return NextResponse.json( + { error: "Failed to get questions" }, + { status: 500 } + ) + } +} + +// POST: 새 질문 추가 +export async function POST( + request: NextRequest, + { params }: Props +) { + try { + const session = await getServerSession(authOptions) + if (!session?.user?.companyId) { + return NextResponse.json( + { error: "Unauthorized" }, + { status: 401 } + ) + } + + const vendorId = typeof session.user.companyId === 'string' + ? parseInt(session.user.companyId) + : session.user.companyId + + const sessionId = parseInt(params.sessionId) + const body = await request.json() + + const question = await addVendorQuestion( + sessionId, + vendorId, + { + category: body.category || "general", + question: body.question, + priority: body.priority || "normal", + status: "open" + } + ) + + return NextResponse.json(question) + + } catch (error) { + console.error("Add question error:", error) + return NextResponse.json( + { error: "Failed to add question" }, + { status: 500 } + ) + } +} + +// PATCH: 질문에 답변 추가 (구매자용) +export async function PATCH( + request: NextRequest, + { params }: Props +) { + try { + const session = await getServerSession(authOptions) + if (!session?.user) { + return NextResponse.json( + { error: "Unauthorized" }, + { status: 401 } + ) + } + + const sessionId = parseInt(params.sessionId) + const body = await request.json() + + const { questionId, answer } = body + + if (!questionId || !answer) { + return NextResponse.json( + { error: "Question ID and answer are required" }, + { status: 400 } + ) + } + + const result = await answerVendorQuestion(sessionId, questionId, answer) + + return NextResponse.json(result) + + } catch (error) { + console.error("Answer question error:", error) + return NextResponse.json( + { error: "Failed to answer question" }, + { status: 500 } + ) + } +} \ No newline at end of file diff --git a/app/api/tbe/sessions/[sessionId]/vendor-remarks/route.ts b/app/api/tbe/sessions/[sessionId]/vendor-remarks/route.ts new file mode 100644 index 00000000..d2dc7797 --- /dev/null +++ b/app/api/tbe/sessions/[sessionId]/vendor-remarks/route.ts @@ -0,0 +1,57 @@ +// ========================================== +// app/api/tbe/sessions/[sessionId]/vendor-remarks/route.ts +// ========================================== + +import { NextRequest, NextResponse } from "next/server" +import { getServerSession } from "next-auth" +import { authOptions } from "@/app/api/auth/[...nextauth]/route" +import { updateVendorRemarks } from "@/lib/tbe-last/vendor-tbe-service" + +interface Props { + params: { + sessionId: string + } +} + +// PUT: 벤더 의견 업데이트 +export async function PUT( + request: NextRequest, + { params }: Props +) { + try { + const session = await getServerSession(authOptions) + if (!session?.user?.companyId) { + return NextResponse.json( + { error: "Unauthorized" }, + { status: 401 } + ) + } + + const vendorId = typeof session.user.companyId === 'string' + ? parseInt(session.user.companyId) + : session.user.companyId + + const sessionId = parseInt(params.sessionId) + const body = await request.json() + + const { remarks } = body + + if (!remarks) { + return NextResponse.json( + { error: "Remarks are required" }, + { status: 400 } + ) + } + + const updated = await updateVendorRemarks(sessionId, vendorId, remarks) + + return NextResponse.json(updated) + + } catch (error) { + console.error("Update remarks error:", error) + return NextResponse.json( + { error: "Failed to update remarks" }, + { status: 500 } + ) + } +} \ No newline at end of file diff --git a/app/api/upload/signed-contract/route.ts b/app/api/upload/signed-contract/route.ts index 86109eec..8547f0e4 100644 --- a/app/api/upload/signed-contract/route.ts +++ b/app/api/upload/signed-contract/route.ts @@ -1,12 +1,10 @@ // app/api/upload/signed-contract/route.ts import { NextRequest, NextResponse } from 'next/server'; -import fs from 'fs/promises'; -import path from 'path'; -import { v4 as uuidv4 } from 'uuid'; import db from "@/db/db"; import { basicContract } from '@/db/schema'; import { eq } from 'drizzle-orm'; import { revalidateTag } from 'next/cache'; +import { saveBuffer } from '@/lib/file-stroage'; export async function POST(request: NextRequest) { try { @@ -19,25 +17,37 @@ export async function POST(request: NextRequest) { return NextResponse.json({ result: false, error: '필수 파라미터가 누락되었습니다.' }, { status: 400 }); } - const originalName = `${tableRowId}_${templateName}`; - const ext = path.extname(originalName); - const uniqueName = uuidv4() + ext; + // 원본 파일명 설정 + const originalFileName = `${tableRowId}_${templateName}`; - const publicDir = path.join(process.cwd(), "public", "basicContract"); - const relativePath = `/basicContract/signed/${uniqueName}`; - const absolutePath = path.join(publicDir, uniqueName); + // 파일을 Buffer로 변환 const buffer = Buffer.from(await file.arrayBuffer()); - await fs.mkdir(publicDir, { recursive: true }); - await fs.writeFile(absolutePath, buffer); + // saveBuffer 함수를 사용하여 파일 저장 + const saveResult = await saveBuffer({ + buffer: buffer, + fileName: file.name, // 실제 업로드된 파일명 + directory: 'basicContract/signed', // 저장 디렉토리 + originalName: originalFileName, // DB에 저장할 원본명 + userId: undefined // 필요시 사용자 ID 추가 + }); + + // 저장 실패 시 에러 반환 + if (!saveResult.success) { + return NextResponse.json({ + result: false, + error: saveResult.error || '파일 저장에 실패했습니다.' + }, { status: 500 }); + } + // DB 업데이트 await db.transaction(async (tx) => { await tx .update(basicContract) .set({ status: "VENDOR_SIGNED", - fileName: originalName, - filePath: relativePath, + fileName: saveResult.originalName || originalFileName, // 원본 파일명 + filePath: saveResult.publicPath, // 웹 접근 가능한 경로 updatedAt: new Date(), completedAt: new Date() }) @@ -48,7 +58,12 @@ export async function POST(request: NextRequest) { revalidateTag("basic-contract-requests"); revalidateTag("basicContractView-vendor"); - return NextResponse.json({ result: true }); + return NextResponse.json({ + result: true, + filePath: saveResult.publicPath, + fileName: saveResult.fileName + }); + } catch (error) { console.error('서명된 계약서 저장 오류:', error); const errorMessage = error instanceof Error ? error.message : "알 수 없는 오류"; diff --git a/config/menuConfig.ts b/config/menuConfig.ts index 4e468347..6a726d49 100644 --- a/config/menuConfig.ts +++ b/config/menuConfig.ts @@ -936,6 +936,12 @@ export const mainNavVendor: MenuSection[] = [ titleKey: "menu.vendor.engineering.title", useGrouping: true, items: [ + { + titleKey: "menu.vendor.engineering.tbe", + href: `/partners/tbe-last`, + descriptionKey: "menu.vendor.engineering.tbe_desc", + // groupKey: "groups.shipbuilding", + }, { titleKey: "menu.vendor.engineering.data_input_ship", href: `/partners/vendor-data`, diff --git a/db/schema/rfqLastTBE.ts b/db/schema/rfqLastTBE.ts index ba7e30b5..1efb43bb 100644 --- a/db/schema/rfqLastTBE.ts +++ b/db/schema/rfqLastTBE.ts @@ -40,7 +40,7 @@ export const rfqLastTbeSessions = pgTable( // 평가 결과 (단순화) evaluationResult: varchar("evaluation_result", { length: 30 }) - .$type<"pass" | "conditional_pass" | "non_pass" | null>(), + .$type<"Acceptable" | "Acceptable with Comment" | "Not Acceptable" | null>(), // 조건부 승인 시 조건 conditionalRequirements: text("conditional_requirements"), @@ -123,7 +123,7 @@ export const rfqLastTbeDocumentReviews = pgTable( // 벤더 문서인 경우 vendorAttachmentId: integer("vendor_attachment_id") - .references(() => rfqLastVendorAttachments.id, { onDelete: "cascade" }), + .references(() => rfqLastTbeVendorDocuments.id, { onDelete: "cascade" }), // 검토 정보 documentType: varchar("document_type", { length: 50 }), @@ -169,6 +169,7 @@ export const rfqLastTbeDocumentReviews = pgTable( // ========================================== // 3. PDFTron 코멘트 관리 // ========================================== +// 수정된 스키마 (버전 관리 제거) export const rfqLastTbePdftronComments = pgTable( "rfq_last_tbe_pdftron_comments", { @@ -177,65 +178,80 @@ export const rfqLastTbePdftronComments = pgTable( .notNull() .references(() => rfqLastTbeDocumentReviews.id, { onDelete: "cascade" }), - // PDFTron 관련 정보 + // PDFTron 문서 식별자 pdftronDocumentId: varchar("pdftron_document_id", { length: 255 }).notNull(), - pdftronAnnotationId: varchar("pdftron_annotation_id", { length: 255 }).notNull(), - annotationType: varchar("annotation_type", { length: 50 }), // highlight, note, drawing, etc. - // 위치 정보 - pageNumber: integer("page_number"), - xPosition: numeric("x_position", { precision: 10, scale: 4 }).$type(), - yPosition: numeric("y_position", { precision: 10, scale: 4 }).$type(), - coordinates: jsonb("coordinates"), // 복잡한 도형의 경우 - - // 코멘트 내용 - commentText: text("comment_text"), - commentCategory: varchar("comment_category", { length: 50 }) - .$type<"technical" | "commercial" | "quality" | "compliance" | "general">(), - - severity: varchar("severity", { length: 20 }) - .$type<"minor" | "major" | "critical">() - .default("minor"), - - // 상태 관리 - status: varchar("status", { length: 30 }) - .$type<"open" | "resolved" | "rejected" | "deferred">() - .default("open"), - - // 해결 정보 - resolvedBy: integer("resolved_by") - .references(() => users.id, { onDelete: "set null" }), - resolvedAt: timestamp("resolved_at", { withTimezone: true }).$type(), - resolutionNote: text("resolution_note"), - - // 답변 스레드 - replies: jsonb("replies").$type<{ - userId: number; - userName: string; - message: string; - createdAt: string; - }[]>(), + // XFDF XML 전체 저장 (모든 annotation 포함) + xfdfString: text("xfdf_string").notNull(), + + // 파싱된 annotation 데이터 (검색/필터링용) + annotationData: jsonb("annotation_data").$type<{ + annotations: { + id: string; + type: string; + page: number; + author: string; + subject: string; + contents?: string; + color?: string; + opacity?: number; + createdDate: string; + modifiedDate?: string; + customData?: { + category?: "technical" | "commercial" | "quality" | "compliance" | "general"; + severity?: "minor" | "major" | "critical"; + status?: "open" | "resolved" | "rejected" | "deferred"; + createdBy?: number; + createdByType?: "buyer" | "vendor"; + resolvedBy?: number; + resolvedAt?: string; + resolutionNote?: string; + }; + replies?: { + author: string; + contents: string; + createdDate: string; + }[]; + coords?: number[]; // 좌표 데이터 + }[]; + }>(), + + // 요약 정보 (빠른 조회용) + commentSummary: jsonb("comment_summary").$type<{ + total: number; + open: number; + resolved: number; + rejected: number; + deferred: number; + byCategory: Record; + bySeverity: Record; + byAuthor: Record; + }>(), // 작성자 정보 createdBy: integer("created_by") .notNull() - .references(() => users.id, { onDelete: "set null" }), + .references(() => users.id, { onDelete: "restrict" }), createdByType: varchar("created_by_type", { length: 20 }) .$type<"buyer" | "vendor">() .notNull(), + // 마지막 수정자 + lastModifiedBy: integer("last_modified_by") + .references(() => users.id, { onDelete: "set null" }), + createdAt: timestamp("created_at").defaultNow().notNull(), updatedAt: timestamp("updated_at").defaultNow().notNull(), }, (table) => ({ documentReviewIdx: index("idx_pdftron_doc_review").on(table.documentReviewId), - statusIdx: index("idx_pdftron_status").on(table.status), - // PDFTron ID들에 대한 유니크 제약 - uniquePdftronAnnotation: uniqueIndex("unique_pdftron_annotation") - .on(table.pdftronDocumentId, table.pdftronAnnotationId), - }) -); + documentIdIdx: index("idx_pdftron_doc_id").on(table.pdftronDocumentId), + // documentReviewId와 pdftronDocumentId 조합 유니크 + uniqueDocument: uniqueIndex("unique_document") + .on(table.documentReviewId, table.pdftronDocumentId), + }) +) // ========================================== // 4. TBE 새로운 벤더 첨부파일 (TBE 중 추가 제출) // ========================================== @@ -478,6 +494,9 @@ export const tbeLastView = pgView("tbe_last_view").as((qb) => { series: sql`${rfqsLast.series}`.as("series"), rfqStatus: sql`${rfqsLast.status}`.as("rfq_status"), rfqDueDate: sql`${rfqsLast.dueDate}`.as("rfq_due_date"), + picName: sql`${rfqsLast.picName}`.as("pic_name"), + EngPicName: sql`${rfqsLast.EngPicName}`.as("eng_pic_name"), + // 패키지 정보 packageNo: sql`${rfqsLast.packageNo}`.as("package_no"), @@ -591,68 +610,74 @@ export const tbeLastView = pgView("tbe_last_view").as((qb) => { // TBE 문서 상세 뷰 (구매자 + 벤더 문서 통합) // ========================================== export const tbeDocumentsView = pgView("tbe_documents_view").as((qb) => { - const ba = alias(rfqLastAttachments, "ba"); - const baRev = alias(rfqLastAttachmentRevisions, "ba_rev"); + const dr = alias(rfqLastTbeDocumentReviews, "dr") + const ba = alias(rfqLastAttachments, "ba") + const baRev = alias(rfqLastAttachmentRevisions, "ba_rev") + const vd = alias(rfqLastTbeVendorDocuments, "vd") + return qb .select({ - // 문서 검토 ID - documentReviewId: sql`dr.id`.as("document_review_id"), - tbeSessionId: sql`COALESCE(dr.tbe_session_id, vd.tbe_session_id)`.as("tbe_session_id"), - - // 문서 구분 - documentSource: sql` - CASE - WHEN dr.id IS NOT NULL THEN dr.document_source - WHEN vd.id IS NOT NULL THEN 'vendor' - ELSE NULL - END - `.as("document_source"), - - // 문서 정보 - documentId: sql`COALESCE(dr.buyer_attachment_id, vd.id)`.as("document_id"), - documentType: sql`COALESCE(dr.document_type, vd.document_type)`.as("document_type"), - documentName: sql`COALESCE(dr.document_name, vd.file_name)`.as("document_name"), + // 기본키/세션 + documentReviewId: sql`dr.id`.as("document_review_id"), + tbeSessionId: sql`dr.tbe_session_id`.as("tbe_session_id"), + + // 소스 + documentSource: sql<"buyer" | "vendor">`dr.document_source`.as("document_source"), + + // 문서 식별자: buyer면 buyerAttachmentId, vendor면 vendorAttachmentId + documentId: sql` + CASE + WHEN dr.document_source = 'buyer' THEN dr.buyer_attachment_id + WHEN dr.document_source = 'vendor' THEN dr.vendor_attachment_id + ELSE NULL + END + `.as("document_id"), + + // 표시 정보 + documentType: sql`dr.document_type`.as("document_type"), + documentName: sql`dr.document_name`.as("document_name"), + + // 파일 메타: buyer면 ba_rev.*, vendor면 vd.* originalFileName: sql`COALESCE(ba_rev.original_file_name, vd.original_file_name)`.as("original_file_name"), - filePath: sql`COALESCE(ba_rev.file_path, vd.file_path)`.as("file_path"), - fileSize: sql`COALESCE(ba_rev.file_size, vd.file_size)`.as("file_size"), - fileType: sql`COALESCE(ba_rev.file_type, vd.file_type)`.as("file_type"), + filePath: sql`COALESCE(ba_rev.file_path, vd.file_path)`.as("file_path"), + fileSize: sql`COALESCE(ba_rev.file_size, vd.file_size)`.as("file_size"), + fileType: sql`COALESCE(ba_rev.file_type, vd.file_type)`.as("file_type"), - // 검토 상태 - reviewStatus: sql`COALESCE(dr.review_status, vd.review_status, '미검토')`.as("review_status"), + // 리뷰 상태/정보 (dr 기준) + reviewStatus: sql`dr.review_status`.as("review_status"), technicalCompliance: sql`dr.technical_compliance`.as("technical_compliance"), qualityAcceptable: sql`dr.quality_acceptable`.as("quality_acceptable"), requiresRevision: sql`COALESCE(dr.requires_revision, false)`.as("requires_revision"), - // PDFTron 관련 + // PDFTron hasPdftronComments: sql`COALESCE(dr.has_pdftron_comments, false)`.as("has_pdftron_comments"), pdftronDocumentId: sql`dr.pdftron_document_id`.as("pdftron_document_id"), pdftronAnnotationCount: sql`COALESCE(dr.pdftron_annotation_count, 0)`.as("pdftron_annotation_count"), - // 검토 정보 - reviewedBy: sql`COALESCE(dr.reviewed_by, vd.reviewed_by)`.as("reviewed_by"), - reviewedAt: sql`COALESCE(dr.reviewed_at, vd.reviewed_at)`.as("reviewed_at"), - reviewComments: sql`COALESCE(dr.review_comments, vd.review_comments)`.as("review_comments"), - - // 제출 정보 (벤더 문서인 경우) - submittedBy: sql`vd.submitted_by`.as("submitted_by"), - submittedAt: sql`vd.submitted_at`.as("submitted_at"), - - // 타임스탬프 - createdAt: sql`COALESCE(dr.created_at, vd.submitted_at)`.as("created_at"), - updatedAt: sql`COALESCE(dr.updated_at, vd.submitted_at)`.as("updated_at"), + // 검토자/타임스탬프 + reviewedBy: sql`dr.reviewed_by`.as("reviewed_by"), + reviewedAt: sql`dr.reviewed_at`.as("reviewed_at"), + reviewComments: sql`dr.review_comments`.as("review_comments"), + + // 제출 정보(벤더 문서일 때 vd의 제출 정보 노출, 아니면 null) + submittedBy: sql` + CASE WHEN dr.document_source = 'vendor' THEN vd.submitted_by ELSE NULL END + `.as("submitted_by"), + submittedAt: sql` + CASE WHEN dr.document_source = 'vendor' THEN vd.submitted_at ELSE NULL END + `.as("submitted_at"), + + // 생성/업데이트 시각: 리뷰기준 + createdAt: sql`dr.created_at`.as("created_at"), + updatedAt: sql`dr.updated_at`.as("updated_at"), }) - .from( - sql`( - SELECT * FROM rfq_last_tbe_document_reviews - ) dr - FULL OUTER JOIN ( - SELECT * FROM rfq_last_tbe_vendor_documents - ) vd ON false - ` - ) + .from(dr) + // buyer: 리뷰가 가리키는 첨부 개정에 조인 .leftJoin(ba, sql`dr.buyer_attachment_id = ${ba.id}`) - .leftJoin(baRev, sql`dr.buyer_attachment_revision_id = ${baRev.id}`); -}); + .leftJoin(baRev, sql`dr.buyer_attachment_revision_id = ${baRev.id}`) + // vendor: 리뷰가 가리키는 벤더 문서에 조인 + .leftJoin(vd, sql`dr.vendor_attachment_id = ${vd.id}`) +}) // Type exports export type TbeLastView = typeof tbeLastView.$inferSelect; diff --git a/db/schema/rfqVendor.ts b/db/schema/rfqVendor.ts index 5752b1c2..0ddf109b 100644 --- a/db/schema/rfqVendor.ts +++ b/db/schema/rfqVendor.ts @@ -1,9 +1,10 @@ import { pgTable, pgView, serial, varchar, text, timestamp, boolean, integer, numeric, date, alias, jsonb } from "drizzle-orm/pg-core"; -import { eq, sql, relations } from "drizzle-orm"; +import { eq, sql, relations,and } from "drizzle-orm"; import { rfqsLast, rfqLastDetails, rfqPrItems } from "./rfqLast"; import { users } from "./users"; import { vendors } from "./vendors"; import { incoterms, paymentTerms } from "./procurementRFQ"; +import { projects } from "./projects"; // ========================================== // 1. 벤더 응답 메인 테이블 (견적서 헤더) @@ -458,4 +459,223 @@ export const vendorQuotationItemsRelations = relations( export type VendorResponse = typeof rfqLastVendorResponses.$inferSelect; export type VendorQuotationItem = typeof rfqLastVendorQuotationItems.$inferSelect; export type VendorAttachment = typeof rfqLastVendorAttachments.$inferSelect; -export type VendorResponseHistory = typeof rfqLastVendorResponseHistory.$inferSelect; \ No newline at end of file +export type VendorResponseHistory = typeof rfqLastVendorResponseHistory.$inferSelect; + + +// vendorQuotationView - 벤더별 견적 현황을 보여주는 통합 뷰 +export const vendorQuotationView = pgView("vendor_quotation_view").as((qb) => { + const createdByUser = alias(users, "created_by_user"); + const updatedByUser = alias(users, "updated_by_user"); + const sentByUser = alias(users, "sent_by_user"); + const picUser = alias(users, "pic_user"); + + return qb + .select({ + // ===== RFQ 기본 정보 (rfqsLastView에서 가져온 필드들) ===== + id: sql`${rfqsLast.id}`.as("id"), + rfqCode: sql`${rfqsLast.rfqCode}`.as("rfq_code"), + series: sql`${rfqsLast.series}`.as("series"), + rfqSealedYn: sql`${rfqsLast.rfqSealedYn}`.as("rfq_sealed_yn"), + + // RFQ 타입 정보 + rfqType: sql`${rfqsLast.rfqType}`.as("rfq_type"), + rfqTitle: sql`${rfqsLast.rfqTitle}`.as("rfq_title"), + + // ITB 관련 필드 + projectCompany: sql`${rfqsLast.projectCompany}`.as("project_company"), + projectFlag: sql`${rfqsLast.projectFlag}`.as("project_flag"), + projectSite: sql`${rfqsLast.projectSite}`.as("project_site"), + smCode: sql`${rfqsLast.smCode}`.as("sm_code"), + + // RFQ 추가 필드 + prNumber: sql`${rfqsLast.prNumber}`.as("pr_number"), + prIssueDate: sql`${rfqsLast.prIssueDate}`.as("pr_issue_date"), + + // 프로젝트 정보 + projectId: sql`${rfqsLast.projectId}`.as("project_id"), + projectCode: sql`${projects.code}`.as("project_code"), + projectName: sql`${projects.name}`.as("project_name"), + + // 아이템 정보 + itemCode: sql`${rfqsLast.itemCode}`.as("item_code"), + itemName: sql`${rfqsLast.itemName}`.as("item_name"), + + // 패키지 정보 + packageNo: sql`${rfqsLast.packageNo}`.as("package_no"), + packageName: sql`${rfqsLast.packageName}`.as("package_name"), + + engPicName: sql`${rfqsLast.EngPicName}`.as("eng_pic_name"), + + // 상태와 날짜 + status: sql`${rfqsLast.status}`.as("status"), + rfqSendDate: sql`${rfqsLast.rfqSendDate}`.as("rfq_send_date"), + dueDate: sql`${rfqsLast.dueDate}`.as("due_date"), + + // PIC 정보 + picId: sql`${rfqsLast.pic}`.as("pic_id"), + picCode: sql`${rfqsLast.picCode}`.as("pic_code"), + picName: sql`${rfqsLast.picName}`.as("pic_name"), + picUserName: sql`${picUser.name}`.as("pic_user_name"), + + // 감사 정보 + createdBy: sql`${rfqsLast.createdBy}`.as("created_by"), + createdByUserName: sql`${createdByUser.name}`.as("created_by_user_name"), + createdAt: sql`${rfqsLast.createdAt}`.as("created_at"), + sentBy: sql`${rfqsLast.sentBy}`.as("sent_by"), + sentByUserName: sql`${sentByUser.name}`.as("sent_by_user_name"), + updatedBy: sql`${rfqsLast.updatedBy}`.as("updated_by"), + updatedByUserName: sql`${updatedByUser.name}`.as("updated_by_user_name"), + updatedAt: sql`${rfqsLast.updatedAt}`.as("updated_at"), + remark: sql`${rfqsLast.remark}`.as("remark"), + + // ===== 벤더별 정보 ===== + vendorId: sql`${vendors.id}`.as("vendor_id"), + vendorName: sql`${vendors.vendorName}`.as("vendor_name"), + vendorCode: sql`${vendors.vendorCode}`.as("vendor_code"), + + // rfqLastDetails 정보 + rfqLastDetailsId: sql`${rfqLastDetails.id}`.as("rfq_last_details_id"), + emailSentAt: sql`${rfqLastDetails.emailSentAt}`.as("email_sent_at"), + emailStatus: sql`${rfqLastDetails.emailStatus}`.as("email_status"), + shortList: sql`${rfqLastDetails.shortList}`.as("short_list"), + + // ===== 벤더 응답 정보 (rfqLastVendorResponses) ===== + vendorResponseId: sql`${rfqLastVendorResponses.id}`.as("vendor_response_id"), + + // 참여 상태 + participationStatus: sql`${rfqLastVendorResponses.participationStatus}`.as("participation_status"), + participationRepliedAt: sql`${rfqLastVendorResponses.participationRepliedAt}`.as("participation_replied_at"), + nonParticipationReason: sql`${rfqLastVendorResponses.nonParticipationReason}`.as("non_participation_reason"), + + // 응답 상태 + responseStatus: sql`${rfqLastVendorResponses.status}`.as("response_status"), + responseVersion: sql`${rfqLastVendorResponses.responseVersion}`.as("response_version"), + submittedAt: sql`${rfqLastVendorResponses.submittedAt}`.as("submitted_at"), + + // 금액 정보 + totalAmount: sql`${rfqLastVendorResponses.totalAmount}`.as("total_amount"), + vendorCurrency: sql`${rfqLastVendorResponses.vendorCurrency}`.as("vendor_currency"), + + // 벤더 제안 조건 + vendorPaymentTermsCode: sql`${rfqLastVendorResponses.vendorPaymentTermsCode}`.as("vendor_payment_terms_code"), + vendorIncotermsCode: sql`${rfqLastVendorResponses.vendorIncotermsCode}`.as("vendor_incoterms_code"), + vendorDeliveryDate: sql`${rfqLastVendorResponses.vendorDeliveryDate}`.as("vendor_delivery_date"), + + // ===== 계산된 필드 - displayStatus ===== + displayStatus: sql` + CASE + WHEN ${rfqLastVendorResponses.participationStatus} = '불참' THEN '불참' + WHEN ${rfqLastVendorResponses.participationStatus} = '참여' THEN + COALESCE(${rfqLastVendorResponses.status}, '작성중') + WHEN ${rfqLastVendorResponses.participationStatus} = '미응답' OR ${rfqLastVendorResponses.participationStatus} IS NULL THEN + CASE + WHEN ${rfqLastDetails.emailSentAt} IS NOT NULL THEN '미응답' + ELSE NULL + END + ELSE '미응답' + END + `.as("display_status"), + + // ===== 집계 정보 (RFQ 레벨) ===== + vendorCount: sql`( + SELECT COUNT(*) + FROM rfq_last_details d + WHERE d.rfqs_last_id = ${rfqsLast.id} + AND d.is_latest = true + )`.as("vendor_count"), + + shortListedVendorCount: sql`( + SELECT COUNT(*) + FROM rfq_last_details d + WHERE d.rfqs_last_id = ${rfqsLast.id} + AND d.short_list = true + AND d.is_latest = true + )`.as("short_listed_vendor_count"), + + quotationReceivedCount: sql`( + SELECT COUNT(DISTINCT r.vendor_id) + FROM rfq_last_vendor_responses r + WHERE r.rfqs_last_id = ${rfqsLast.id} + AND r.submitted_at IS NOT NULL + AND r.is_latest = true + )`.as("quotation_received_count"), + + earliestQuotationSubmittedAt: sql`( + SELECT MIN(r.submitted_at) + FROM rfq_last_vendor_responses r + WHERE r.rfqs_last_id = ${rfqsLast.id} + AND r.submitted_at IS NOT NULL + AND r.is_latest = true + )`.as("earliest_quotation_submitted_at"), + + // PR Items 관련 정보 + majorItemMaterialCode: sql`( + SELECT material_code + FROM rfq_pr_items + WHERE rfqs_last_id = ${rfqsLast.id} + AND major_yn = true + LIMIT 1 + )`.as("major_item_material_code"), + + majorItemMaterialDescription: sql`( + SELECT material_description + FROM rfq_pr_items + WHERE rfqs_last_id = ${rfqsLast.id} + AND major_yn = true + LIMIT 1 + )`.as("major_item_material_description"), + + majorItemMaterialCategory: sql`( + SELECT material_category + FROM rfq_pr_items + WHERE rfqs_last_id = ${rfqsLast.id} + AND major_yn = true + LIMIT 1 + )`.as("major_item_material_category"), + + majorItemPrNo: sql`( + SELECT pr_no + FROM rfq_pr_items + WHERE rfqs_last_id = ${rfqsLast.id} + AND major_yn = true + LIMIT 1 + )`.as("major_item_pr_no"), + + prItemsCount: sql`( + SELECT COUNT(*) + FROM rfq_pr_items + WHERE rfqs_last_id = ${rfqsLast.id} + )`.as("pr_items_count"), + + majorItemsCount: sql`( + SELECT COUNT(*) + FROM rfq_pr_items + WHERE rfqs_last_id = ${rfqsLast.id} + AND major_yn = true + )`.as("major_items_count") + }) + .from(rfqsLast) + .innerJoin(rfqLastDetails, + and( + eq(rfqLastDetails.rfqsLastId, rfqsLast.id), + eq(rfqLastDetails.isLatest, true) + ) + ) + .leftJoin(vendors, eq(rfqLastDetails.vendorsId, vendors.id)) + .leftJoin(rfqLastVendorResponses, + and( + eq(rfqLastVendorResponses.rfqsLastId, rfqsLast.id), + eq(rfqLastVendorResponses.vendorId, vendors.id), + eq(rfqLastVendorResponses.isLatest, true) + ) + ) + .leftJoin(projects, eq(rfqsLast.projectId, projects.id)) + .leftJoin(createdByUser, eq(rfqsLast.createdBy, createdByUser.id)) + .leftJoin(updatedByUser, eq(rfqsLast.updatedBy, updatedByUser.id)) + .leftJoin(sentByUser, eq(rfqsLast.sentBy, sentByUser.id)) + .leftJoin(picUser, eq(rfqsLast.pic, picUser.id)); +}); + +// Type export +export type VendorQuotationView = typeof vendorQuotationView.$inferSelect; + diff --git a/i18n/locales/en/menu.json b/i18n/locales/en/menu.json index fdd056f4..402bf1ae 100644 --- a/i18n/locales/en/menu.json +++ b/i18n/locales/en/menu.json @@ -160,10 +160,12 @@ "tbe_ship_desc": "History management of TBE and vendor responses", "tbe_plant": "Technical (Quality) Evaluation (TBE) Offshore", "tbe_plant_desc": "History management of TBE generated from S-EDP and vendor responses", - "po_issuance": "PO Issuance", + "po_issuance": "PO/Contract Management", "po_issuance_desc": "PO (Purchase Order) confirmation/signature request/contract details storage", "po_amendment": "PO Amendment Issuance", "po_amendment_desc": "Amendment PO (Purchase Order) creation/signature request/contract details storage", + "pcr": "PCR", + "pcr_desc": "Purchase Change Request management", "general_contract": "General Contract", "general_contract_desc": "General contract management" }, @@ -224,12 +226,16 @@ "po_desc": "Order list confirmation and electronic signature", "po_amendment": "PO Amendment", "po_amendment_desc": "Order list confirmation and electronic signature", + "pcr": "PCR", + "pcr_desc": "Purchase Change Request management", "general_contract": "General Contract", "general_contract_desc": "Order list confirmation and electronic signature", "rfq_response":"견적 응답", "rfq_response_desc":"견적 요청에 대한 응답 작성" }, "engineering": { + "tbe": "TBE", + "tbe_desc": "Technical Bid Evaluation", "title": "Engineering", "data_input_ship": "Data Input", "data_input_ship_desc": "Vendor data input based on reference information", diff --git a/i18n/locales/ko/menu.json b/i18n/locales/ko/menu.json index e9e1b87f..d6c3d340 100644 --- a/i18n/locales/ko/menu.json +++ b/i18n/locales/ko/menu.json @@ -164,10 +164,12 @@ "tbe_ship_desc": "TBE와 업체의 응답에 대한 이력 관리", "tbe_plant": "기술(품질) 평가 (TBE) 해양", "tbe_plant_desc": "S-EDP로부터 생성된 TBE와 업체의 응답에 대한 이력 관리", - "po_issuance": "PO 발행", + "po_issuance": "PO/계약 관리", "po_issuance_desc": "PO(구매 발주서) 확인/서명 요청/계약 내역 저장", "po_amendment": "변경 PO 발행", "po_amendment_desc": "변경 PO(구매 발주서) 생성/서명 요청/계약 내역 저장", + "pcr": "PCR", + "pcr_desc": "PCR 관리", "general_contract": "일반 계약", "general_contract_desc": "일반 계약 관리" }, @@ -227,6 +229,8 @@ "po_desc": "발주 리스트 확인 및 전자서명", "po_amendment": "PO Amendment", "po_amendment_desc": "발주 리스트 확인 및 전자서명", + "pcr": "PCR", + "pcr_desc": "PCR 관리", "general_contract": "일반 계약", "general_contract_desc": "발주 리스트 확인 및 전자서명", "rfq_response":"견적 응답", @@ -234,6 +238,8 @@ }, "engineering": { "title": "Engineering", + "tbe": "TBE", + "tbe_desc": "Technical Bid Evaluation", "data_input_ship": "데이터 입력", "data_input_ship_desc": "기준 정보에 입각한 협력업체 데이터 입력", "document_list_ship": "문서/도서 리스트 및 제출(조선)", diff --git a/lib/admin-users/service.ts b/lib/admin-users/service.ts index c253f481..70c04aa1 100644 --- a/lib/admin-users/service.ts +++ b/lib/admin-users/service.ts @@ -262,7 +262,7 @@ export async function createAdminUser(input: CreateUserSchema & { language?: str // 3. 유저 생성 const [newUser] = await insertUser(tx, { name: input.name, - email: input.email, + email: input.email.toLowerCase(), phone: input.phone?.trim(), // 전화번호 앞뒤 공백 제거 domain: input.domain, companyId: input.companyId ?? null, diff --git a/lib/basic-contract/gen-service.ts b/lib/basic-contract/gen-service.ts index 5619f98e..aa9efbc1 100644 --- a/lib/basic-contract/gen-service.ts +++ b/lib/basic-contract/gen-service.ts @@ -8,6 +8,7 @@ import { eq, and, ilike } from "drizzle-orm"; import { addDays } from "date-fns"; import { writeFile, mkdir } from "fs/promises"; import path from "path"; +import { saveBuffer } from '@/lib/file-storage'; // 추가 interface BasicContractParams { templateName: string; @@ -348,17 +349,19 @@ export async function saveContractPdf({ templateId: number; }) { try { - // 1. PDF 파일 저장 - const outputDir = path.join(process.cwd(), process.env.NAS_PATH, "contracts", "generated"); - await mkdir(outputDir, { recursive: true }); + // 1. PDF 파일 저장 (공용 saveBuffer 사용) + const saveResult = await saveBuffer({ + buffer: Buffer.from(pdfBuffer), + fileName: fileName, // 원본 파일명 (확장자 포함) + directory: 'contracts/generated', + originalName: fileName, // DB에 저장할 원본명 + userId: params.requestedBy // 요청자 ID를 userId로 사용 + }); - const timestamp = Date.now(); - const finalFileName = `${fileName.replace('.pdf', '')}_${timestamp}.pdf`; - const outputPath = path.join(outputDir, finalFileName); - - await writeFile(outputPath, Buffer.from(pdfBuffer)); - - const relativePath = `/contracts/generated/${finalFileName}`; + // 저장 실패 시 에러 처리 + if (!saveResult.success) { + throw new Error(saveResult.error || 'PDF 파일 저장에 실패했습니다.'); + } // 2. DB에 계약서 레코드 생성 const [newContract] = await db @@ -371,8 +374,8 @@ export async function saveContractPdf({ generalContractId: params.generalContractId, requestedBy: params.requestedBy, status: "PENDING", - fileName: finalFileName, - filePath: relativePath, + fileName: saveResult.originalName || fileName, // 원본 파일명 + filePath: saveResult.publicPath, // 웹 접근 가능한 경로 deadline: addDays(new Date(), 10), createdAt: new Date(), updatedAt: new Date(), @@ -385,8 +388,10 @@ export async function saveContractPdf({ templateName: params.templateName, status: newContract.status, deadline: newContract.deadline, - pdfPath: relativePath, - pdfFileName: finalFileName + pdfPath: saveResult.publicPath, // 공용 함수가 반환한 경로 + pdfFileName: saveResult.originalName || fileName, // 원본 파일명 + hashedFileName: saveResult.fileName, // 실제 저장된 해시 파일명 (필요시 사용) + securityChecks: saveResult.securityChecks // 보안 검증 결과 (디버깅용) }; } catch (error) { console.error("PDF 저장 실패:", error); diff --git a/lib/basic-contract/viewer/basic-contract-sign-viewer.tsx b/lib/basic-contract/viewer/basic-contract-sign-viewer.tsx index e52f0d79..5698428e 100644 --- a/lib/basic-contract/viewer/basic-contract-sign-viewer.tsx +++ b/lib/basic-contract/viewer/basic-contract-sign-viewer.tsx @@ -594,6 +594,7 @@ export function BasicContractSignViewer({ isComplete: false }); + console.log(filePath, "filePath") console.log(surveyTemplate, "surveyTemplate") const conditionalHandler = useConditionalSurvey(surveyTemplate); diff --git a/lib/bidding/pre-quote/service.ts b/lib/bidding/pre-quote/service.ts index 680a8ff5..7f054a66 100644 --- a/lib/bidding/pre-quote/service.ts +++ b/lib/bidding/pre-quote/service.ts @@ -11,7 +11,7 @@ import { mkdir, writeFile } from 'fs/promises' import path from 'path' import { revalidateTag, revalidatePath } from 'next/cache' import { basicContract } from '@/db/schema/basicContractDocumnet' -import { saveFile } from '@/lib/file-stroage' +import { saveFile ,saveBuffer} from '@/lib/file-stroage' // userId를 user.name으로 변환하는 유틸리티 함수 async function getUserNameById(userId: string): Promise { @@ -1225,10 +1225,7 @@ export async function sendBiddingBasicContracts( const results = [] const savedContracts = [] - // 트랜잭션 시작 - const contractsDir = path.join(process.cwd(), `${process.env.NAS_PATH}`, "contracts", "generated"); - await mkdir(contractsDir, { recursive: true }); - + // 트랜잭션 시작 - contractsDir 제거 (saveBuffer가 처리) const result = await db.transaction(async (tx) => { // 각 벤더별로 기본계약 생성 및 이메일 발송 for (const vendor of vendorData) { @@ -1288,6 +1285,7 @@ export async function sendBiddingBasicContracts( if (vendor.contractRequirements.projectGtcYn) contractTypes.push({ type: 'Project_GTC', templateName: '기술' }) if (vendor.contractRequirements.agreementYn) contractTypes.push({ type: '기술자료', templateName: '기술자료' }) console.log("contractTypes", contractTypes) + for (const contractType of contractTypes) { // PDF 데이터 찾기 (include를 사용하여 유연하게 찾기) console.log("generatedPdfs", generatedPdfs.map(pdf => pdf.key)) @@ -1301,11 +1299,22 @@ export async function sendBiddingBasicContracts( continue } - // 파일 저장 (rfq-last 방식) + // 파일 저장 - saveBuffer 사용 const fileName = `${contractType.type}_${vendor.vendorCode || vendor.vendorId}_${vendor.biddingCompanyId}_${Date.now()}.pdf` - const filePath = path.join(contractsDir, fileName); + + const saveResult = await saveBuffer({ + buffer: Buffer.from(pdfData.buffer), + fileName: fileName, + directory: 'contracts/generated', + originalName: fileName, + userId: currentUser.id + }) - await writeFile(filePath, Buffer.from(pdfData.buffer)); + // 저장 실패 시 처리 + if (!saveResult.success) { + console.error(`PDF 저장 실패: ${saveResult.error}`) + continue + } // 템플릿 정보 조회 (rfq-last 방식) const [template] = await db @@ -1343,8 +1352,8 @@ export async function sendBiddingBasicContracts( .set({ requestedBy: currentUser.id, status: "PENDING", // 재발송 상태 - fileName: fileName, - filePath: `/contracts/generated/${fileName}`, + fileName: saveResult.originalName || fileName, // 원본 파일명 + filePath: saveResult.publicPath, // saveBuffer가 반환한 공개 경로 deadline: new Date(Date.now() + 10 * 24 * 60 * 60 * 1000), updatedAt: new Date(), }) @@ -1364,8 +1373,8 @@ export async function sendBiddingBasicContracts( generalContractId: null, requestedBy: currentUser.id, status: 'PENDING', - fileName: fileName, - filePath: `/contracts/generated/${fileName}`, + fileName: saveResult.originalName || fileName, // 원본 파일명 + filePath: saveResult.publicPath, // saveBuffer가 반환한 공개 경로 deadline: new Date(Date.now() + 10 * 24 * 60 * 60 * 1000), // 10일 후 createdAt: new Date(), updatedAt: new Date(), @@ -1380,19 +1389,10 @@ export async function sendBiddingBasicContracts( vendorName: vendor.vendorName, contractId: contractRecord.id, contractType: contractType.type, - fileName: fileName, - filePath: `/contracts/generated/${fileName}`, + fileName: saveResult.originalName || fileName, + filePath: saveResult.publicPath, + hashedFileName: saveResult.fileName, // 실제 저장된 파일명 (디버깅용) }) - - // savedContracts에 추가 (rfq-last 방식) - // savedContracts.push({ - // vendorId: vendor.vendorId, - // vendorName: vendor.vendorName, - // templateName: contractType.templateName, - // contractId: contractRecord.id, - // fileName: fileName, - // isUpdated: !!existingContract, // 업데이트 여부 표시 - // }) } // 이메일 발송 (선택사항) @@ -1439,7 +1439,6 @@ export async function sendBiddingBasicContracts( ) } } - // 기존 기본계약 조회 (서버 액션) export async function getExistingBasicContractsForBidding(biddingId: number) { try { diff --git a/lib/export-to-excel.ts b/lib/export-to-excel.ts new file mode 100644 index 00000000..b35c18d6 --- /dev/null +++ b/lib/export-to-excel.ts @@ -0,0 +1,316 @@ +// lib/utils/export-to-excel.ts + +import ExcelJS from 'exceljs' + +interface ExportToExcelOptions { + filename?: string + sheetName?: string + headers?: string[] + dateFormat?: string + autoFilter?: boolean + freezeHeader?: boolean +} + +/** + * 데이터 배열을 Excel 파일로 내보내기 (ExcelJS 사용) + * @param data - 내보낼 데이터 배열 + * @param options - 내보내기 옵션 + */ +export async function exportDataToExcel( + data: Record[], + options: ExportToExcelOptions = {} +) { + const { + filename = 'export', + sheetName = 'Sheet1', + headers, + dateFormat = 'yyyy-mm-dd', + autoFilter = true, + freezeHeader = true + } = options + + try { + // 데이터가 없으면 반환 + if (!data || data.length === 0) { + console.warn('No data to export') + return false + } + + // 워크북 생성 + const workbook = new ExcelJS.Workbook() + workbook.creator = 'TBE System' + workbook.created = new Date() + + // 워크시트 추가 + const worksheet = workbook.addWorksheet(sheetName, { + properties: { + defaultRowHeight: 20 + } + }) + + // 헤더 처리 + const finalHeaders = headers || Object.keys(data[0]) + + // 컬럼 정의 + const columns = finalHeaders.map(header => ({ + header, + key: header, + width: Math.min( + Math.max( + header.length, + ...data.map(row => { + const value = row[header] + if (value === null || value === undefined) return 0 + return String(value).length + }) + ) + 2, + 50 + ) + })) + + worksheet.columns = columns + + // 데이터 추가 + data.forEach(row => { + const rowData: Record = {} + + finalHeaders.forEach(header => { + const value = row[header] + + // null/undefined 처리 + if (value === null || value === undefined) { + rowData[header] = '' + } + // Date 객체 처리 + else if (value instanceof Date) { + rowData[header] = value + } + // boolean 처리 + else if (typeof value === 'boolean') { + rowData[header] = value ? 'Yes' : 'No' + } + // 숫자 처리 + else if (typeof value === 'number') { + rowData[header] = value + } + // 기타 (문자열 등) + else { + rowData[header] = String(value) + } + }) + + worksheet.addRow(rowData) + }) + + // 헤더 스타일링 + const headerRow = worksheet.getRow(1) + headerRow.font = { bold: true } + headerRow.fill = { + type: 'pattern', + pattern: 'solid', + fgColor: { argb: 'FFE0E0E0' } + } + headerRow.alignment = { vertical: 'middle', horizontal: 'center' } + headerRow.height = 25 + + // 헤더 테두리 + headerRow.eachCell((cell) => { + cell.border = { + top: { style: 'thin' }, + left: { style: 'thin' }, + bottom: { style: 'thin' }, + right: { style: 'thin' } + } + }) + + // 데이터 행 스타일링 + worksheet.eachRow((row, rowNumber) => { + if (rowNumber > 1) { + row.alignment = { vertical: 'middle' } + row.eachCell((cell) => { + cell.border = { + top: { style: 'thin' }, + left: { style: 'thin' }, + bottom: { style: 'thin' }, + right: { style: 'thin' } + } + }) + } + }) + + // 자동 필터 추가 + if (autoFilter) { + worksheet.autoFilter = { + from: { row: 1, column: 1 }, + to: { row: data.length + 1, column: columns.length } + } + } + + // 헤더 고정 + if (freezeHeader) { + worksheet.views = [ + { state: 'frozen', ySplit: 1 } + ] + } + + // 날짜 포맷 적용 + worksheet.eachRow((row, rowNumber) => { + if (rowNumber > 1) { + row.eachCell((cell, colNumber) => { + const header = finalHeaders[colNumber - 1] + const value = data[rowNumber - 2][header] + + if (value instanceof Date) { + cell.numFmt = dateFormat === 'yyyy-mm-dd' + ? 'yyyy-mm-dd' + : 'mm/dd/yyyy' + } + // 숫자 포맷 (천단위 구분) + else if (typeof value === 'number' && header.toLowerCase().includes('quantity')) { + cell.numFmt = '#,##0' + } + }) + } + }) + + // 파일 다운로드 + const buffer = await workbook.xlsx.writeBuffer() + const blob = new Blob([buffer], { + type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' + }) + + const url = URL.createObjectURL(blob) + const link = document.createElement('a') + const timestamp = new Date().toISOString().slice(0, 10) + const finalFilename = `${filename}_${timestamp}.xlsx` + + link.href = url + link.download = finalFilename + link.style.display = 'none' + + document.body.appendChild(link) + link.click() + document.body.removeChild(link) + + URL.revokeObjectURL(url) + + return true + } catch (error) { + console.error('Excel export error:', error) + throw new Error('Failed to export Excel file') + } +} + +/** + * Date 객체를 Excel 형식으로 포맷팅 + */ +function formatDateForExcel(date: Date, format: string): string { + const year = date.getFullYear() + const month = String(date.getMonth() + 1).padStart(2, '0') + const day = String(date.getDate()).padStart(2, '0') + + switch (format) { + case 'yyyy-mm-dd': + return `${year}-${month}-${day}` + case 'dd/mm/yyyy': + return `${day}/${month}/${year}` + case 'mm/dd/yyyy': + return `${month}/${day}/${year}` + default: + return date.toLocaleDateString() + } +} + +/** + * CSV 파일로 내보내기 (대안 옵션) + */ +export function exportDataToCSV( + data: Record[], + filename: string = 'export' +) { + try { + if (!data || data.length === 0) { + console.warn('No data to export') + return + } + + // 헤더 추출 + const headers = Object.keys(data[0]) + + // CSV 문자열 생성 + let csvContent = headers.join(',') + '\n' + + data.forEach(row => { + const values = headers.map(header => { + const value = row[header] + + // null/undefined 처리 + if (value === null || value === undefined) return '' + + // 콤마나 줄바꿈이 있으면 따옴표로 감싸기 + const stringValue = String(value) + if (stringValue.includes(',') || stringValue.includes('\n')) { + return `"${stringValue.replace(/"/g, '""')}"` + } + + return stringValue + }) + + csvContent += values.join(',') + '\n' + }) + + // BOM 추가 (Excel에서 UTF-8 인식) + const BOM = '\uFEFF' + const blob = new Blob([BOM + csvContent], { type: 'text/csv;charset=utf-8;' }) + + // 다운로드 링크 생성 + const link = document.createElement('a') + const url = URL.createObjectURL(blob) + + link.setAttribute('href', url) + link.setAttribute('download', `${filename}_${new Date().toISOString().slice(0, 10)}.csv`) + link.style.visibility = 'hidden' + + document.body.appendChild(link) + link.click() + document.body.removeChild(link) + + return true + } catch (error) { + console.error('CSV export error:', error) + throw new Error('Failed to export CSV file') + } +} + +/** + * 간단한 데이터 내보내기 헬퍼 + * Excel이 안되면 CSV로 fallback + */ +export async function exportData( + data: Record[], + options: ExportToExcelOptions & { format?: 'excel' | 'csv' } = {} +) { + const { format = 'excel', ...exportOptions } = options + + try { + if (format === 'csv') { + return exportDataToCSV(data, exportOptions.filename) + } else { + return exportDataToExcel(data, exportOptions) + } + } catch (error) { + console.error(`Failed to export as ${format}, trying CSV as fallback`) + + // Excel 실패 시 CSV로 시도 + if (format === 'excel') { + try { + return exportDataToCSV(data, exportOptions.filename) + } catch (csvError) { + console.error('Both Excel and CSV export failed') + throw csvError + } + } + + throw error + } +} \ No newline at end of file diff --git a/lib/rfq-last/attachment/vendor-response-table.tsx b/lib/rfq-last/attachment/vendor-response-table.tsx index 6e1a02c8..f9388752 100644 --- a/lib/rfq-last/attachment/vendor-response-table.tsx +++ b/lib/rfq-last/attachment/vendor-response-table.tsx @@ -17,7 +17,7 @@ import { FileCode, Building2, Calendar, - AlertCircle + AlertCircle, X } from "lucide-react"; import { format, formatDistanceToNow, isValid, isBefore, isAfter } from "date-fns"; import { ko } from "date-fns/locale"; @@ -46,6 +46,22 @@ import { cn } from "@/lib/utils"; import { getRfqVendorAttachments } from "@/lib/rfq-last/service"; import { downloadFile } from "@/lib/file-download"; import { toast } from "sonner"; +import { + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue, +} from "@/components/ui/select"; +import { + Dialog, + DialogContent, + DialogDescription, + DialogFooter, + DialogHeader, + DialogTitle, +} from "@/components/ui/dialog"; + // 타입 정의 interface VendorAttachment { @@ -138,24 +154,79 @@ export function VendorResponseTable({ const [isRefreshing, setIsRefreshing] = React.useState(false); const [selectedRows, setSelectedRows] = React.useState([]); - // 데이터 새로고침 - const handleRefresh = React.useCallback(async () => { - setIsRefreshing(true); + + + const [isUpdating, setIsUpdating] = React.useState(false); + const [showTypeDialog, setShowTypeDialog] = React.useState(false); + const [selectedType, setSelectedType] = React.useState<"구매" | "설계" | "">(""); + console.log(data,"data") + + const [selectedVendor, setSelectedVendor] = React.useState(null); + + const filteredData = React.useMemo(() => { + if (!selectedVendor) return data; + return data.filter(item => item.vendorName === selectedVendor); + }, [data, selectedVendor]); + + + + // 데이터 새로고침 + const handleRefresh = React.useCallback(async () => { + setIsRefreshing(true); + try { + const result = await getRfqVendorAttachments(rfqId); + if (result.vendorSuccess && result.vendorData) { + setData(result.vendorData); + toast.success("데이터를 새로고침했습니다."); + } else { + toast.error("데이터를 불러오는데 실패했습니다."); + } + } catch (error) { + console.error("Refresh error:", error); + toast.error("새로고침 중 오류가 발생했습니다."); + } finally { + setIsRefreshing(false); + } + }, [rfqId]); + + const toggleVendorFilter = (vendor: string) => { + if (selectedVendor === vendor) { + setSelectedVendor(null); // 이미 선택된 벤더를 다시 클릭하면 필터 해제 + } else { + setSelectedVendor(vendor); + // 필터 변경 시 선택 초기화 (옵션) + setSelectedRows([]); + } + }; + + // 문서 유형 일괄 변경 + const handleBulkTypeChange = React.useCallback(async () => { + if (!selectedType || selectedRows.length === 0) return; + + setIsUpdating(true); try { - const result = await getRfqVendorAttachments(rfqId); - if (result.success && result.data) { - setData(result.data); - toast.success("데이터를 새로고침했습니다."); + const ids = selectedRows.map(row => row.id); + const result = await updateAttachmentTypes(ids, selectedType as "구매" | "설계"); + + if (result.success) { + toast.success(result.message); + // 데이터 새로고침 + await handleRefresh(); + // 선택 초기화 + setSelectedRows([]); + setShowTypeDialog(false); + setSelectedType(""); } else { - toast.error("데이터를 불러오는데 실패했습니다."); + toast.error(result.message); } } catch (error) { - console.error("Refresh error:", error); - toast.error("새로고침 중 오류가 발생했습니다."); + toast.error("문서 유형 변경 중 오류가 발생했습니다."); } finally { - setIsRefreshing(false); + setIsUpdating(false); } - }, [rfqId]); + }, [selectedType, selectedRows, handleRefresh]); + + // 액션 처리 const handleAction = React.useCallback(async (action: DataTableRowAction) => { @@ -282,56 +353,56 @@ export function VendorResponseTable({ }, size: 300, }, - { - accessorKey: "description", - header: ({ column }) => , - cell: ({ row }) => ( -
- {row.original.description || "-"} -
- ), - size: 200, - }, - { - accessorKey: "validTo", - header: ({ column }) => , - cell: ({ row }) => { - const { validFrom, validTo } = row.original; - const validity = checkValidity(validTo); + // { + // accessorKey: "description", + // header: ({ column }) => , + // cell: ({ row }) => ( + //
+ // {row.original.description || "-"} + //
+ // ), + // size: 200, + // }, + // { + // accessorKey: "validTo", + // header: ({ column }) => , + // cell: ({ row }) => { + // const { validFrom, validTo } = row.original; + // const validity = checkValidity(validTo); - if (!validTo) return -; + // if (!validTo) return -; - return ( - - - -
- {validity === "expired" && ( - - )} - {validity === "expiring-soon" && ( - - )} - - {format(new Date(validTo), "yyyy-MM-dd")} - -
-
- -

유효기간: {validFrom ? format(new Date(validFrom), "yyyy-MM-dd") : "?"} ~ {format(new Date(validTo), "yyyy-MM-dd")}

- {validity === "expired" &&

만료됨

} - {validity === "expiring-soon" &&

곧 만료 예정

} -
-
-
- ); - }, - size: 120, - }, + // return ( + // + // + // + //
+ // {validity === "expired" && ( + // + // )} + // {validity === "expiring-soon" && ( + // + // )} + // + // {format(new Date(validTo), "yyyy-MM-dd")} + // + //
+ //
+ // + //

유효기간: {validFrom ? format(new Date(validFrom), "yyyy-MM-dd") : "?"} ~ {format(new Date(validTo), "yyyy-MM-dd")}

+ // {validity === "expired" &&

만료됨

} + // {validity === "expiring-soon" &&

곧 만료 예정

} + //
+ //
+ //
+ // ); + // }, + // size: 120, + // }, { accessorKey: "responseStatus", header: ({ column }) => , @@ -424,13 +495,13 @@ export function VendorResponseTable({ label: "문서 유형", type: "select", options: [ - { label: "견적서", value: "견적서" }, - { label: "기술제안서", value: "기술제안서" }, - { label: "인증서", value: "인증서" }, - { label: "카탈로그", value: "카탈로그" }, - { label: "도면", value: "도면" }, - { label: "테스트성적서", value: "테스트성적서" }, - { label: "기타", value: "기타" }, + { label: "구매", value: "구매" }, + { label: "설계", value: "설계" }, + // { label: "인증서", value: "인증서" }, + // { label: "카탈로그", value: "카탈로그" }, + // { label: "도면", value: "도면" }, + // { label: "테스트성적서", value: "테스트성적서" }, + // { label: "기타", value: "기타" }, ] }, { id: "documentNo", label: "문서번호", type: "text" }, @@ -448,23 +519,35 @@ export function VendorResponseTable({ { label: "취소", value: "취소" }, ] }, - { id: "validFrom", label: "유효시작일", type: "date" }, - { id: "validTo", label: "유효종료일", type: "date" }, + // { id: "validFrom", label: "유효시작일", type: "date" }, + // { id: "validTo", label: "유효종료일", type: "date" }, { id: "uploadedAt", label: "업로드일", type: "date" }, ]; - // 추가 액션 버튼들 + // 추가 액션 버튼들 수정 const additionalActions = React.useMemo(() => (
{selectedRows.length > 0 && ( - + <> + {/* 문서 유형 변경 버튼 */} + + + + )}
- ), [selectedRows, isRefreshing, handleBulkDownload, handleRefresh]); + ), [selectedRows, isRefreshing, handleBulkDownload, handleRefresh]) // 벤더별 그룹 카운트 const vendorCounts = React.useMemo(() => { @@ -490,18 +573,71 @@ export function VendorResponseTable({ return (
- {/* 벤더별 요약 정보 */} -
- {Array.from(vendorCounts.entries()).map(([vendor, count]) => ( - - {vendor}: {count} - - ))} + {/* 벤더 필터 섹션 */} +
+ {/* 필터 헤더 */} +
+ + 벤더별 필터 + + {selectedVendor && ( + + )} +
+ + {/* 벤더 버튼들 */} +
+ {/* 전체 보기 버튼 */} + + + {/* 각 벤더별 버튼 */} + {Array.from(vendorCounts.entries()).map(([vendor, count]) => ( + + ))} +
+ + {/* 현재 필터 상태 표시 */} + {selectedVendor && ( +
+ + + "{selectedVendor}" 벤더의 {filteredData.length}개 항목만 표시 중 + +
+ )}
{additionalActions} + + {/* 문서 유형 변경 다이얼로그 */} + + + + 문서 유형 변경 + + 선택한 {selectedRows.length}개 항목의 문서 유형을 변경합니다. + + + +
+
+ + +
+ + {/* 현재 선택된 항목들의 정보 표시 */} +
+

변경될 항목:

+
    + {selectedRows.slice(0, 5).map((row) => ( +
  • + • {row.vendorName} - {row.originalFileName} +
  • + ))} + {selectedRows.length > 5 && ( +
  • + ... 외 {selectedRows.length - 5}개 +
  • + )} +
+
+
+ + + + + +
+
); } \ No newline at end of file diff --git a/lib/rfq-last/compare-action.ts b/lib/rfq-last/compare-action.ts new file mode 100644 index 00000000..5d210631 --- /dev/null +++ b/lib/rfq-last/compare-action.ts @@ -0,0 +1,500 @@ +"use server"; + +import db from "@/db/db"; +import { eq, and, inArray } from "drizzle-orm"; +import { + rfqsLast, + rfqLastDetails, + rfqPrItems, + rfqLastVendorResponses, + rfqLastVendorQuotationItems, + vendors, + paymentTerms, + incoterms, +} from "@/db/schema"; + +export interface ComparisonData { + rfqInfo: { + id: number; + rfqCode: string; + rfqTitle: string; + rfqType: string; + projectCode?: string; + projectName?: string; + dueDate: Date | null; + packageNo?: string; + packageName?: string; + }; + vendors: VendorComparison[]; + prItems: PrItemComparison[]; + summary: { + lowestBidder: string; + highestBidder: string; + priceRange: { + min: number; + max: number; + average: number; + }; + currency: string; + }; +} + +export interface VendorComparison { + vendorId: number; + vendorName: string; + vendorCode: string; + vendorCountry?: string; + + // 응답 정보 + responseId: number; + participationStatus: string; + responseStatus: string; + submittedAt: Date | null; + + // 가격 정보 + totalAmount: number; + currency: string; + rank?: number; + priceVariance?: number; // 평균 대비 차이 % + + // 구매자 제시 조건 + buyerConditions: { + currency: string; + paymentTermsCode: string; + paymentTermsDesc?: string; + incotermsCode: string; + incotermsDesc?: string; + deliveryDate: Date | null; + contractDuration?: string; + taxCode?: string; + placeOfShipping?: string; + placeOfDestination?: string; + + // 추가 조건 + firstYn: boolean; + firstDescription?: string; + sparepartYn: boolean; + sparepartDescription?: string; + materialPriceRelatedYn: boolean; + }; + + // 벤더 제안 조건 + vendorConditions: { + currency?: string; + paymentTermsCode?: string; + paymentTermsDesc?: string; + incotermsCode?: string; + incotermsDesc?: string; + deliveryDate?: Date | null; + contractDuration?: string; + taxCode?: string; + placeOfShipping?: string; + placeOfDestination?: string; + + // 추가 조건 응답 + firstAcceptance?: "수용" | "부분수용" | "거부"; + firstDescription?: string; + sparepartAcceptance?: "수용" | "부분수용" | "거부"; + sparepartDescription?: string; + materialPriceRelatedYn?: boolean; + materialPriceRelatedReason?: string; + }; + + // 조건 차이 분석 + conditionDifferences: { + hasDifferences: boolean; + differences: string[]; + criticalDifferences: string[]; // 중요한 차이점 + }; + + // 비고 + generalRemark?: string; + technicalProposal?: string; +} + +export interface PrItemComparison { + prItemId: number; + prNo: string; + prItem: string; + materialCode: string; + materialDescription: string; + requestedQuantity: number; + uom: string; + requestedDeliveryDate: Date | null; + + vendorQuotes: { + vendorId: number; + vendorName: string; + unitPrice: number; + totalPrice: number; + currency: string; + quotedQuantity: number; + deliveryDate?: Date | null; + leadTime?: number; + manufacturer?: string; + modelNo?: string; + technicalCompliance: boolean; + alternativeProposal?: string; + itemRemark?: string; + priceRank?: number; + }[]; + + priceAnalysis: { + lowestPrice: number; + highestPrice: number; + averagePrice: number; + priceVariance: number; // 표준편차 + }; +} + +export async function getComparisonData( + rfqId: number, + vendorIds: number[] +): Promise { + try { + // 1. RFQ 기본 정보 조회 + const rfqData = await db + .select({ + id: rfqsLast.id, + rfqCode: rfqsLast.rfqCode, + rfqTitle: rfqsLast.rfqTitle, + rfqType: rfqsLast.rfqType, + // projectCode: rfqsLast.projectCode, + // projectName: rfqsLast.projectName, + dueDate: rfqsLast.dueDate, + packageNo: rfqsLast.packageNo, + packageName: rfqsLast.packageName, + }) + .from(rfqsLast) + .where(eq(rfqsLast.id, rfqId)) + .limit(1); + + if (!rfqData[0]) return null; + + // 2. 벤더별 정보 및 응답 조회 + const vendorData = await db + .select({ + // 벤더 정보 + vendorId: vendors.id, + vendorName: vendors.vendorName, + vendorCode: vendors.vendorCode, + vendorCountry: vendors.country, + + // RFQ Details (구매자 조건) + detailId: rfqLastDetails.id, + buyerCurrency: rfqLastDetails.currency, + buyerPaymentTermsCode: rfqLastDetails.paymentTermsCode, + buyerIncotermsCode: rfqLastDetails.incotermsCode, + buyerIncotermsDetail: rfqLastDetails.incotermsDetail, + buyerDeliveryDate: rfqLastDetails.deliveryDate, + buyerContractDuration: rfqLastDetails.contractDuration, + buyerTaxCode: rfqLastDetails.taxCode, + buyerPlaceOfShipping: rfqLastDetails.placeOfShipping, + buyerPlaceOfDestination: rfqLastDetails.placeOfDestination, + buyerFirstYn: rfqLastDetails.firstYn, + buyerFirstDescription: rfqLastDetails.firstDescription, + buyerSparepartYn: rfqLastDetails.sparepartYn, + buyerSparepartDescription: rfqLastDetails.sparepartDescription, + buyerMaterialPriceRelatedYn: rfqLastDetails.materialPriceRelatedYn, + + // 벤더 응답 + responseId: rfqLastVendorResponses.id, + participationStatus: rfqLastVendorResponses.participationStatus, + responseStatus: rfqLastVendorResponses.status, + submittedAt: rfqLastVendorResponses.submittedAt, + totalAmount: rfqLastVendorResponses.totalAmount, + responseCurrency: rfqLastVendorResponses.currency, + + // 벤더 제안 조건 + vendorCurrency: rfqLastVendorResponses.vendorCurrency, + vendorPaymentTermsCode: rfqLastVendorResponses.vendorPaymentTermsCode, + vendorIncotermsCode: rfqLastVendorResponses.vendorIncotermsCode, + vendorIncotermsDetail: rfqLastVendorResponses.vendorIncotermsDetail, + vendorDeliveryDate: rfqLastVendorResponses.vendorDeliveryDate, + vendorContractDuration: rfqLastVendorResponses.vendorContractDuration, + vendorTaxCode: rfqLastVendorResponses.vendorTaxCode, + vendorPlaceOfShipping: rfqLastVendorResponses.vendorPlaceOfShipping, + vendorPlaceOfDestination: rfqLastVendorResponses.vendorPlaceOfDestination, + + // 추가 조건 응답 + vendorFirstAcceptance: rfqLastVendorResponses.vendorFirstAcceptance, + vendorFirstDescription: rfqLastVendorResponses.vendorFirstDescription, + vendorSparepartAcceptance: rfqLastVendorResponses.vendorSparepartAcceptance, + vendorSparepartDescription: rfqLastVendorResponses.vendorSparepartDescription, + vendorMaterialPriceRelatedYn: rfqLastVendorResponses.vendorMaterialPriceRelatedYn, + vendorMaterialPriceRelatedReason: rfqLastVendorResponses.vendorMaterialPriceRelatedReason, + + // 비고 + generalRemark: rfqLastVendorResponses.generalRemark, + technicalProposal: rfqLastVendorResponses.technicalProposal, + }) + .from(vendors) + .innerJoin( + rfqLastDetails, + and( + eq(rfqLastDetails.vendorsId, vendors.id), + eq(rfqLastDetails.rfqsLastId, rfqId), + eq(rfqLastDetails.isLatest, true) + ) + ) + .leftJoin( + rfqLastVendorResponses, + and( + eq(rfqLastVendorResponses.vendorId, vendors.id), + eq(rfqLastVendorResponses.rfqsLastId, rfqId), + eq(rfqLastVendorResponses.isLatest, true) + ) + ) + .where(inArray(vendors.id, vendorIds)); + + // 3. Payment Terms와 Incoterms 설명 조회 + const paymentTermsData = await db + .select({ + code: paymentTerms.code, + description: paymentTerms.description, + }) + .from(paymentTerms); + + const incotermsData = await db + .select({ + code: incoterms.code, + description: incoterms.description, + }) + .from(incoterms); + + const paymentTermsMap = new Map( + paymentTermsData.map(pt => [pt.code, pt.description]) + ); + const incotermsMap = new Map( + incotermsData.map(ic => [ic.code, ic.description]) + ); + + // 4. PR Items 조회 + const prItems = await db + .select({ + id: rfqPrItems.id, + prNo: rfqPrItems.prNo, + prItem: rfqPrItems.prItem, + materialCode: rfqPrItems.materialCode, + materialDescription: rfqPrItems.materialDescription, + quantity: rfqPrItems.quantity, + uom: rfqPrItems.uom, + deliveryDate: rfqPrItems.deliveryDate, + }) + .from(rfqPrItems) + .where(eq(rfqPrItems.rfqsLastId, rfqId)); + + // 5. 벤더별 견적 아이템 조회 + const quotationItems = await db + .select({ + vendorResponseId: rfqLastVendorQuotationItems.vendorResponseId, + prItemId: rfqLastVendorQuotationItems.rfqPrItemId, + unitPrice: rfqLastVendorQuotationItems.unitPrice, + totalPrice: rfqLastVendorQuotationItems.totalPrice, + currency: rfqLastVendorQuotationItems.currency, + quantity: rfqLastVendorQuotationItems.quantity, + deliveryDate: rfqLastVendorQuotationItems.vendorDeliveryDate, + leadTime: rfqLastVendorQuotationItems.leadTime, + manufacturer: rfqLastVendorQuotationItems.manufacturer, + modelNo: rfqLastVendorQuotationItems.modelNo, + technicalCompliance: rfqLastVendorQuotationItems.technicalCompliance, + alternativeProposal: rfqLastVendorQuotationItems.alternativeProposal, + itemRemark: rfqLastVendorQuotationItems.itemRemark, + }) + .from(rfqLastVendorQuotationItems) + .where( + inArray( + rfqLastVendorQuotationItems.vendorResponseId, + vendorData.map(v => v.responseId).filter(id => id != null) + ) + ); + + // 6. 데이터 가공 및 분석 + const validAmounts = vendorData + .map(v => v.totalAmount) + .filter(a => a != null && a > 0); + + const minAmount = Math.min(...validAmounts); + const maxAmount = Math.max(...validAmounts); + const avgAmount = validAmounts.reduce((a, b) => a + b, 0) / validAmounts.length; + + // 벤더별 비교 데이터 구성 + const vendorComparisons: VendorComparison[] = vendorData.map((v, index) => { + const differences: string[] = []; + const criticalDifferences: string[] = []; + + // 조건 차이 분석 + if (v.vendorCurrency && v.vendorCurrency !== v.buyerCurrency) { + criticalDifferences.push(`통화: ${v.buyerCurrency} → ${v.vendorCurrency}`); + } + + if (v.vendorPaymentTermsCode && v.vendorPaymentTermsCode !== v.buyerPaymentTermsCode) { + differences.push(`지급조건: ${v.buyerPaymentTermsCode} → ${v.vendorPaymentTermsCode}`); + } + + if (v.vendorIncotermsCode && v.vendorIncotermsCode !== v.buyerIncotermsCode) { + differences.push(`인코텀즈: ${v.buyerIncotermsCode} → ${v.vendorIncotermsCode}`); + } + + if (v.vendorDeliveryDate && v.buyerDeliveryDate) { + const buyerDate = new Date(v.buyerDeliveryDate); + const vendorDate = new Date(v.vendorDeliveryDate); + if (vendorDate > buyerDate) { + criticalDifferences.push(`납기: ${Math.ceil((vendorDate.getTime() - buyerDate.getTime()) / (1000 * 60 * 60 * 24))}일 지연`); + } + } + + if (v.vendorFirstAcceptance === "거부" && v.buyerFirstYn) { + criticalDifferences.push("초도품 거부"); + } + + if (v.vendorSparepartAcceptance === "거부" && v.buyerSparepartYn) { + criticalDifferences.push("스페어파트 거부"); + } + + return { + vendorId: v.vendorId, + vendorName: v.vendorName, + vendorCode: v.vendorCode, + vendorCountry: v.vendorCountry, + + responseId: v.responseId || 0, + participationStatus: v.participationStatus || "미응답", + responseStatus: v.responseStatus || "대기중", + submittedAt: v.submittedAt, + + totalAmount: v.totalAmount || 0, + currency: v.responseCurrency || v.buyerCurrency || "USD", + rank: 0, // 나중에 계산 + priceVariance: v.totalAmount ? ((v.totalAmount - avgAmount) / avgAmount) * 100 : 0, + + buyerConditions: { + currency: v.buyerCurrency || "USD", + paymentTermsCode: v.buyerPaymentTermsCode || "", + paymentTermsDesc: paymentTermsMap.get(v.buyerPaymentTermsCode || ""), + incotermsCode: v.buyerIncotermsCode || "", + incotermsDesc: incotermsMap.get(v.buyerIncotermsCode || ""), + deliveryDate: v.buyerDeliveryDate, + contractDuration: v.buyerContractDuration, + taxCode: v.buyerTaxCode, + placeOfShipping: v.buyerPlaceOfShipping, + placeOfDestination: v.buyerPlaceOfDestination, + firstYn: v.buyerFirstYn || false, + firstDescription: v.buyerFirstDescription, + sparepartYn: v.buyerSparepartYn || false, + sparepartDescription: v.buyerSparepartDescription, + materialPriceRelatedYn: v.buyerMaterialPriceRelatedYn || false, + }, + + vendorConditions: { + currency: v.vendorCurrency, + paymentTermsCode: v.vendorPaymentTermsCode, + paymentTermsDesc: paymentTermsMap.get(v.vendorPaymentTermsCode || ""), + incotermsCode: v.vendorIncotermsCode, + incotermsDesc: incotermsMap.get(v.vendorIncotermsCode || ""), + deliveryDate: v.vendorDeliveryDate, + contractDuration: v.vendorContractDuration, + taxCode: v.vendorTaxCode, + placeOfShipping: v.vendorPlaceOfShipping, + placeOfDestination: v.vendorPlaceOfDestination, + firstAcceptance: v.vendorFirstAcceptance, + firstDescription: v.vendorFirstDescription, + sparepartAcceptance: v.vendorSparepartAcceptance, + sparepartDescription: v.vendorSparepartDescription, + materialPriceRelatedYn: v.vendorMaterialPriceRelatedYn, + materialPriceRelatedReason: v.vendorMaterialPriceRelatedReason, + }, + + conditionDifferences: { + hasDifferences: differences.length > 0 || criticalDifferences.length > 0, + differences, + criticalDifferences, + }, + + generalRemark: v.generalRemark, + technicalProposal: v.technicalProposal, + }; + }); + + // 가격 순위 계산 + vendorComparisons.sort((a, b) => a.totalAmount - b.totalAmount); + vendorComparisons.forEach((v, index) => { + v.rank = index + 1; + }); + + // PR 아이템별 비교 데이터 구성 + const prItemComparisons: PrItemComparison[] = prItems.map(item => { + const itemQuotes = quotationItems + .filter(q => q.prItemId === item.id) + .map(q => { + const vendor = vendorData.find(v => v.responseId === q.vendorResponseId); + return { + vendorId: vendor?.vendorId || 0, + vendorName: vendor?.vendorName || "", + unitPrice: q.unitPrice || 0, + totalPrice: q.totalPrice || 0, + currency: q.currency || "USD", + quotedQuantity: q.quantity || 0, + deliveryDate: q.deliveryDate, + leadTime: q.leadTime, + manufacturer: q.manufacturer, + modelNo: q.modelNo, + technicalCompliance: q.technicalCompliance || true, + alternativeProposal: q.alternativeProposal, + itemRemark: q.itemRemark, + priceRank: 0, + }; + }); + + // 아이템별 가격 순위 + itemQuotes.sort((a, b) => a.unitPrice - b.unitPrice); + itemQuotes.forEach((q, index) => { + q.priceRank = index + 1; + }); + + const unitPrices = itemQuotes.map(q => q.unitPrice); + const avgPrice = unitPrices.reduce((a, b) => a + b, 0) / unitPrices.length || 0; + const variance = Math.sqrt( + unitPrices.reduce((sum, price) => sum + Math.pow(price - avgPrice, 2), 0) / unitPrices.length + ); + + return { + prItemId: item.id, + prNo: item.prNo || "", + prItem: item.prItem || "", + materialCode: item.materialCode || "", + materialDescription: item.materialDescription || "", + requestedQuantity: item.quantity || 0, + uom: item.uom || "", + requestedDeliveryDate: item.deliveryDate, + vendorQuotes: itemQuotes, + priceAnalysis: { + lowestPrice: Math.min(...unitPrices) || 0, + highestPrice: Math.max(...unitPrices) || 0, + averagePrice: avgPrice, + priceVariance: variance, + }, + }; + }); + + // 최종 데이터 구성 + return { + rfqInfo: rfqData[0], + vendors: vendorComparisons, + prItems: prItemComparisons, + summary: { + lowestBidder: vendorComparisons[0]?.vendorName || "", + highestBidder: vendorComparisons[vendorComparisons.length - 1]?.vendorName || "", + priceRange: { + min: minAmount, + max: maxAmount, + average: avgAmount, + }, + currency: vendorComparisons[0]?.currency || "USD", + }, + }; + } catch (error) { + console.error("견적 비교 데이터 조회 실패:", error); + return null; + } +} \ No newline at end of file diff --git a/lib/rfq-last/quotation-compare-view.tsx b/lib/rfq-last/quotation-compare-view.tsx new file mode 100644 index 00000000..0e15a7bf --- /dev/null +++ b/lib/rfq-last/quotation-compare-view.tsx @@ -0,0 +1,755 @@ +"use client"; + +import * as React from "react"; +import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; +import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; +import { Badge } from "@/components/ui/badge"; +import { Button } from "@/components/ui/button"; +import { + Trophy, + TrendingUp, + TrendingDown, + AlertCircle, + CheckCircle, + XCircle, + ChevronDown, + ChevronUp, + Info, + DollarSign, + Calendar, + Package, + Globe, + FileText, + Truck, + AlertTriangle, +} from "lucide-react"; +import { cn } from "@/lib/utils"; +import { format } from "date-fns"; +import { ko } from "date-fns/locale"; +import { + Tooltip, + TooltipContent, + TooltipProvider, + TooltipTrigger, +} from "@/components/ui/tooltip"; +import { + Collapsible, + CollapsibleContent, + CollapsibleTrigger, +} from "@/components/ui/collapsible"; +import type { ComparisonData, VendorComparison, PrItemComparison } from "../actions"; + +interface QuotationCompareViewProps { + data: ComparisonData; +} + +export function QuotationCompareView({ data }: QuotationCompareViewProps) { + const [expandedItems, setExpandedItems] = React.useState>(new Set()); + const [selectedMetric, setSelectedMetric] = React.useState<"price" | "delivery" | "compliance">("price"); + + // 아이템 확장/축소 토글 + const toggleItemExpansion = (itemId: number) => { + setExpandedItems((prev) => { + const newSet = new Set(prev); + if (newSet.has(itemId)) { + newSet.delete(itemId); + } else { + newSet.add(itemId); + } + return newSet; + }); + }; + + // 순위에 따른 색상 + const getRankColor = (rank: number) => { + switch (rank) { + case 1: + return "text-green-600 bg-green-50"; + case 2: + return "text-blue-600 bg-blue-50"; + case 3: + return "text-orange-600 bg-orange-50"; + default: + return "text-gray-600 bg-gray-50"; + } + }; + + // 가격 차이 색상 + const getVarianceColor = (variance: number) => { + if (variance < -5) return "text-green-600"; + if (variance > 5) return "text-red-600"; + return "text-gray-600"; + }; + + // 조건 일치 여부 아이콘 + const getComplianceIcon = (matches: boolean) => { + return matches ? ( + + ) : ( + + ); + }; + + // 금액 포맷 + const formatAmount = (amount: number, currency: string = "USD") => { + return new Intl.NumberFormat("ko-KR", { + style: "currency", + currency: currency, + minimumFractionDigits: 0, + maximumFractionDigits: 2, + }).format(amount); + }; + + return ( +
+ {/* 요약 카드 */} +
+ {/* 최저가 벤더 */} + + + + + 최저가 벤더 + + + +

{data.summary.lowestBidder}

+

+ {formatAmount(data.summary.priceRange.min, data.summary.currency)} +

+
+
+ + {/* 평균 가격 */} + + + + + 평균 가격 + + + +

+ {formatAmount(data.summary.priceRange.average, data.summary.currency)} +

+

+ {data.vendors.length}개 업체 평균 +

+
+
+ + {/* 가격 범위 */} + + + + + 가격 범위 + + + +

+ {((data.summary.priceRange.max - data.summary.priceRange.min) / data.summary.priceRange.min * 100).toFixed(1)}% +

+

+ 최저가 대비 최고가 차이 +

+
+
+ + {/* 조건 불일치 */} + + + + + 조건 불일치 + + + +

+ {data.vendors.filter(v => v.conditionDifferences.hasDifferences).length}개 +

+

+ 제시 조건과 차이 있음 +

+
+
+
+ + {/* 탭 뷰 */} + + + 종합 비교 + 조건 비교 + 아이템별 비교 + 상세 분석 + + + {/* 종합 비교 */} + + + + 가격 순위 + + +
+ {data.vendors.map((vendor) => ( +
+
+
+ {vendor.rank} +
+
+

{vendor.vendorName}

+

+ {vendor.vendorCode} • {vendor.vendorCountry} +

+
+
+ +
+ {/* 조건 차이 표시 */} + {vendor.conditionDifferences.criticalDifferences.length > 0 && ( + + + + + + 중요 차이 {vendor.conditionDifferences.criticalDifferences.length} + + + +
+ {vendor.conditionDifferences.criticalDifferences.map((diff, idx) => ( +

{diff}

+ ))} +
+
+
+
+ )} + + {/* 가격 정보 */} +
+

+ {formatAmount(vendor.totalAmount, vendor.currency)} +

+

+ {vendor.priceVariance && vendor.priceVariance > 0 ? "+" : ""} + {vendor.priceVariance?.toFixed(1)}% vs 평균 +

+
+
+
+ ))} +
+
+
+
+ + {/* 조건 비교 */} + + + + 거래 조건 비교 + + + + + + + + {data.vendors.map((vendor) => ( + + ))} + + + + {/* 통화 */} + + + + {data.vendors.map((vendor) => ( + + ))} + + + {/* 지급조건 */} + + + + {data.vendors.map((vendor) => ( + + ))} + + + {/* 인코텀즈 */} + + + + {data.vendors.map((vendor) => ( + + ))} + + + {/* 납기 */} + + + + {data.vendors.map((vendor) => { + const vendorDate = vendor.vendorConditions.deliveryDate || vendor.buyerConditions.deliveryDate; + const isDelayed = vendorDate && vendor.buyerConditions.deliveryDate && + new Date(vendorDate) > new Date(vendor.buyerConditions.deliveryDate); + + return ( + + ); + })} + + + {/* 초도품 */} + + + + {data.vendors.map((vendor) => ( + + ))} + + + {/* 스페어파트 */} + + + + {data.vendors.map((vendor) => ( + + ))} + + + {/* 연동제 */} + + + + {data.vendors.map((vendor) => ( + + ))} + + +
항목구매자 제시 + {vendor.vendorName} +
통화{data.vendors[0]?.buyerConditions.currency} +
+ {vendor.vendorConditions.currency || vendor.buyerConditions.currency} + {vendor.vendorConditions.currency !== vendor.buyerConditions.currency && ( + 변경 + )} +
+
지급조건 + + + + {data.vendors[0]?.buyerConditions.paymentTermsCode} + + + {data.vendors[0]?.buyerConditions.paymentTermsDesc} + + + + +
+ + + + {vendor.vendorConditions.paymentTermsCode || vendor.buyerConditions.paymentTermsCode} + + + {vendor.vendorConditions.paymentTermsDesc || vendor.buyerConditions.paymentTermsDesc} + + + + {vendor.vendorConditions.paymentTermsCode && + vendor.vendorConditions.paymentTermsCode !== vendor.buyerConditions.paymentTermsCode && ( + 변경 + )} +
+
인코텀즈{data.vendors[0]?.buyerConditions.incotermsCode} +
+ {vendor.vendorConditions.incotermsCode || vendor.buyerConditions.incotermsCode} + {vendor.vendorConditions.incotermsCode !== vendor.buyerConditions.incotermsCode && ( + 변경 + )} +
+
납기 + {data.vendors[0]?.buyerConditions.deliveryDate + ? format(new Date(data.vendors[0].buyerConditions.deliveryDate), "yyyy-MM-dd") + : "-"} + +
+ {vendorDate ? format(new Date(vendorDate), "yyyy-MM-dd") : "-"} + {isDelayed && ( + 지연 + )} +
+
초도품 + {data.vendors[0]?.buyerConditions.firstYn ? "요구" : "해당없음"} + + {vendor.buyerConditions.firstYn && ( + + {vendor.vendorConditions.firstAcceptance || "미응답"} + + )} + {!vendor.buyerConditions.firstYn && "-"} +
스페어파트 + {data.vendors[0]?.buyerConditions.sparepartYn ? "요구" : "해당없음"} + + {vendor.buyerConditions.sparepartYn && ( + + {vendor.vendorConditions.sparepartAcceptance || "미응답"} + + )} + {!vendor.buyerConditions.sparepartYn && "-"} +
연동제 + {data.vendors[0]?.buyerConditions.materialPriceRelatedYn ? "적용" : "미적용"} + +
+ {vendor.vendorConditions.materialPriceRelatedYn !== undefined + ? vendor.vendorConditions.materialPriceRelatedYn ? "적용" : "미적용" + : vendor.buyerConditions.materialPriceRelatedYn ? "적용" : "미적용"} + {vendor.vendorConditions.materialPriceRelatedReason && ( + + + + + + +

+ {vendor.vendorConditions.materialPriceRelatedReason} +

+
+
+
+ )} +
+
+
+
+
+ + {/* 아이템별 비교 */} + + + + PR 아이템별 가격 비교 + + +
+ {data.prItems.map((item) => ( + toggleItemExpansion(item.prItemId)} + > +
+ +
+
+
+ {expandedItems.has(item.prItemId) ? ( + + ) : ( + + )} + +
+
+

{item.materialDescription}

+

+ {item.materialCode} • {item.prNo} • {item.requestedQuantity} {item.uom} +

+
+
+
+

단가 범위

+

+ {formatAmount(item.priceAnalysis.lowestPrice)} ~ {formatAmount(item.priceAnalysis.highestPrice)} +

+
+
+
+ + +
+ + + + + + + + + + + + + + {item.vendorQuotes.map((quote) => ( + + + + + + + + + + ))} + +
벤더단가총액수량납기제조사순위
{quote.vendorName} + {formatAmount(quote.unitPrice, quote.currency)} + + {formatAmount(quote.totalPrice, quote.currency)} + {quote.quotedQuantity} + {quote.deliveryDate + ? format(new Date(quote.deliveryDate), "yyyy-MM-dd") + : quote.leadTime + ? `${quote.leadTime}일` + : "-"} + + {quote.manufacturer && ( +
+

{quote.manufacturer}

+ {quote.modelNo && ( +

{quote.modelNo}

+ )} +
+ )} +
+ + #{quote.priceRank} + +
+ + {/* 가격 분석 요약 */} +
+
+
+

평균 단가

+

+ {formatAmount(item.priceAnalysis.averagePrice)} +

+
+
+

가격 편차

+

+ ±{formatAmount(item.priceAnalysis.priceVariance)} +

+
+
+

최저가 업체

+

+ {item.vendorQuotes.find(q => q.priceRank === 1)?.vendorName} +

+
+
+

가격 차이

+

+ {((item.priceAnalysis.highestPrice - item.priceAnalysis.lowestPrice) / + item.priceAnalysis.lowestPrice * 100).toFixed(1)}% +

+
+
+
+
+
+
+
+ ))} +
+
+
+
+ + {/* 상세 분석 */} + +
+ {/* 위험 요소 분석 */} + + + + + 위험 요소 분석 + + + +
+ {data.vendors.map((vendor) => { + if (!vendor.conditionDifferences.hasDifferences) return null; + + return ( +
+

{vendor.vendorName}

+ {vendor.conditionDifferences.criticalDifferences.length > 0 && ( +
+

중요 차이점:

+ {vendor.conditionDifferences.criticalDifferences.map((diff, idx) => ( +

• {diff}

+ ))} +
+ )} + {vendor.conditionDifferences.differences.length > 0 && ( +
+

일반 차이점:

+ {vendor.conditionDifferences.differences.map((diff, idx) => ( +

• {diff}

+ ))} +
+ )} +
+ ); + })} +
+
+
+ + {/* 추천 사항 */} + + + + + 선정 추천 + + + +
+ {/* 가격 기준 추천 */} +
+

가격 우선 선정

+

+ {data.vendors[0]?.vendorName} - {formatAmount(data.vendors[0]?.totalAmount || 0)} +

+ {data.vendors[0]?.conditionDifferences.hasDifferences && ( +

+ ⚠️ 조건 차이 검토 필요 +

+ )} +
+ + {/* 조건 준수 기준 추천 */} +
+

조건 준수 우선 선정

+ {(() => { + const compliantVendor = data.vendors.find(v => !v.conditionDifferences.hasDifferences); + if (compliantVendor) { + return ( +
+

+ {compliantVendor.vendorName} - {formatAmount(compliantVendor.totalAmount)} +

+

+ 모든 조건 충족 (가격 순위: #{compliantVendor.rank}) +

+
+ ); + } + return ( +

+ 모든 조건을 충족하는 벤더 없음 +

+ ); + })()} +
+ + {/* 균형 추천 */} +
+

균형 선정 (추천)

+ {(() => { + // 가격 순위와 조건 차이를 고려한 점수 계산 + const scoredVendors = data.vendors.map(v => ({ + ...v, + score: (v.rank || 10) + v.conditionDifferences.criticalDifferences.length * 3 + + v.conditionDifferences.differences.length + })); + scoredVendors.sort((a, b) => a.score - b.score); + const recommended = scoredVendors[0]; + + return ( +
+

+ {recommended.vendorName} - {formatAmount(recommended.totalAmount)} +

+

+ 가격 순위 #{recommended.rank}, 조건 차이 최소화 +

+
+ ); + })()} +
+
+
+
+
+ + {/* 벤더별 비고사항 */} + {data.vendors.some(v => v.generalRemark || v.technicalProposal) && ( + + + 벤더 제안사항 및 비고 + + +
+ {data.vendors.map((vendor) => { + if (!vendor.generalRemark && !vendor.technicalProposal) return null; + + return ( +
+

{vendor.vendorName}

+ {vendor.generalRemark && ( +
+

일반 비고:

+

{vendor.generalRemark}

+
+ )} + {vendor.technicalProposal && ( +
+

기술 제안:

+

{vendor.technicalProposal}

+
+ )} +
+ ); + })} +
+
+
+ )} +
+
+
+ ); +} \ No newline at end of file diff --git a/lib/rfq-last/service.ts b/lib/rfq-last/service.ts index 9943c02d..02429b6a 100644 --- a/lib/rfq-last/service.ts +++ b/lib/rfq-last/service.ts @@ -2847,7 +2847,7 @@ export async function sendRfqToVendors({ const picInfo = await getPicInfo(rfqData.picId, rfqData.picName); // 3. 프로젝트 정보 조회 - const projectInfo = rfqData.projectId + const projectInfo = rfqData.projectId ? await getProjectInfo(rfqData.projectId) : null; @@ -2856,7 +2856,7 @@ export async function sendRfqToVendors({ const designAttachments = await getDesignAttachments(rfqId); // 5. 벤더별 처리 - const { results, errors, savedContracts, tbeSessionsCreated } = + const { results, errors, savedContracts, tbeSessionsCreated } = await processVendors({ rfqId, rfqData, @@ -2979,17 +2979,26 @@ async function prepareEmailAttachments(rfqId: number, attachmentIds: number[]) { ); const emailAttachments = []; - + for (const { attachment, revision } of attachments) { if (revision?.filePath) { try { - const fullPath = path.join( - process.cwd(), - `${process.env.NAS_PATH}`, + + const isProduction = process.env.NODE_ENV === "production"; + + const fullPath = isProduction + + path.join( + process.cwd(), + `public`, + revision.filePath + ) + : path.join( + `${process.env.NAS_PATH}`, revision.filePath ); const fileBuffer = await fs.readFile(fullPath); - + emailAttachments.push({ filename: revision.originalFileName, content: fileBuffer, @@ -3052,9 +3061,9 @@ async function processVendors({ // PDF 저장 디렉토리 준비 const contractsDir = path.join( - process.cwd(), - `${process.env.NAS_PATH}`, - "contracts", + process.cwd(), + `${process.env.NAS_PATH}`, + "contracts", "generated" ); await mkdir(contractsDir, { recursive: true }); @@ -3077,18 +3086,18 @@ async function processVendors({ }); results.push(vendorResult.result); - + if (vendorResult.contracts) { savedContracts.push(...vendorResult.contracts); } - + if (vendorResult.tbeSession) { tbeSessionsCreated.push(vendorResult.tbeSession); } } catch (error) { console.error(`벤더 ${vendor.vendorName} 처리 실패:`, error); - + errors.push({ vendorId: vendor.vendorId, vendorName: vendor.vendorName, @@ -3182,7 +3191,7 @@ function prepareEmailRecipients(vendor: any, picEmail: string) { vendor.customEmails?.forEach((custom: any) => { if (custom.email !== vendor.selectedMainEmail && - !vendor.additionalEmails.includes(custom.email)) { + !vendor.additionalEmails.includes(custom.email)) { ccEmails.push(custom.email); } }); @@ -3235,14 +3244,14 @@ async function handleRfqDetail({ ); // 새 detail 생성 - const { - id, - updatedBy, - updatedAt, - isLatest, - sendVersion: oldSendVersion, - emailResentCount, - ...restRfqDetail + const { + id, + updatedBy, + updatedAt, + isLatest, + sendVersion: oldSendVersion, + emailResentCount, + ...restRfqDetail } = rfqDetail; const [newRfqDetail] = await tx @@ -3265,7 +3274,7 @@ async function handleRfqDetail({ }) .returning(); - await tx + await tx .update(basicContract) .set({ rfqCompanyId: newRfqDetail.id, @@ -3273,7 +3282,7 @@ async function handleRfqDetail({ .where( and( eq(basicContract.rfqCompanyId, rfqDetail.id), - eq(rfqLastDetails.vendorsId, vendor.vendorId), + eq(basicContract.vendorId, vendor.vendorId), ) ); @@ -3382,7 +3391,7 @@ async function createOrUpdateContract({ }) .where(eq(basicContract.id, existingContract.id)) .returning(); - + return { ...updated, isUpdated: true }; } else { // 새로 생성 @@ -3401,7 +3410,7 @@ async function createOrUpdateContract({ updatedAt: new Date() }) .returning(); - + return { ...created, isUpdated: false }; } } @@ -3503,11 +3512,11 @@ async function handleTbeSession({ sessionType: "initial", status: "준비중", evaluationResult: null, - plannedStartDate: rfqData.dueDate - ? addDays(new Date(rfqData.dueDate), 1) + plannedStartDate: rfqData.dueDate + ? addDays(new Date(rfqData.dueDate), 1) : addDays(new Date(), 14), - plannedEndDate: rfqData.dueDate - ? addDays(new Date(rfqData.dueDate), 7) + plannedEndDate: rfqData.dueDate + ? addDays(new Date(rfqData.dueDate), 7) : addDays(new Date(), 21), leadEvaluatorId: rfqData.picId, createdBy: Number(currentUser.id), @@ -3536,11 +3545,11 @@ async function handleTbeSession({ async function generateTbeSessionCode(tx: any) { const year = new Date().getFullYear(); const pattern = `TBE-${year}-%`; - + const [lastTbeSession] = await tx .select({ sessionCode: rfqLastTbeSessions.sessionCode }) .from(rfqLastTbeSessions) - .where(like(rfqLastTbeSessions.sessionCode,pattern )) + .where(like(rfqLastTbeSessions.sessionCode, pattern)) .orderBy(sql`${rfqLastTbeSessions.sessionCode} DESC`) .limit(1); @@ -3624,7 +3633,7 @@ async function updateRfqStatus(rfqId: number, userId: number) { updatedAt: new Date() }) .where(eq(rfqsLast.id, rfqId)); - } +} export async function updateRfqDueDate( rfqId: number, @@ -4006,4 +4015,305 @@ function getTemplateNameByType( case "기술자료": return "기술"; default: return contractType; } +} + + +export async function updateAttachmentTypes( + attachmentIds: number[], + attachmentType: "구매" | "설계" +) { + try { + // 권한 체크 등 필요시 추가 + + await db + .update(rfqLastVendorAttachments) + .set({ attachmentType }) + .where(inArray(rfqLastVendorAttachments.id, attachmentIds)); + + // 페이지 리밸리데이션 + // revalidatePath("/rfq"); + + return { success: true, message: `${attachmentIds.length}개 항목이 "${attachmentType}"로 변경되었습니다.` }; + } catch (error) { + console.error("Failed to update attachment types:", error); + return { success: false, message: "문서 유형 변경에 실패했습니다." }; + } +} + +// 단일 RFQ 밀봉 토글 +export async function toggleRfqSealed(rfqId: number) { + try { + const session = await getServerSession(authOptions) + + if (!session?.user) { + throw new Error("인증이 필요합니다.") + } + // 현재 상태 조회 + const [currentRfq] = await db + .select({ rfqSealedYn: rfqsLast.rfqSealedYn }) + .from(rfqsLast) + .where(eq(rfqsLast.id, rfqId)); + + if (!currentRfq) { + throw new Error("RFQ를 찾을 수 없습니다."); + } + + // 상태 토글 + const [updated] = await db + .update(rfqsLast) + .set({ + rfqSealedYn: !currentRfq.rfqSealedYn, + updatedBy: Number(session.user.id), + updatedAt: new Date(), + }) + .where(eq(rfqsLast.id, rfqId)) + .returning(); + + revalidatePath("/evcp/rfq-last"); + + return { + success: true, + data: updated, + message: updated.rfqSealedYn ? "견적이 밀봉되었습니다." : "견적 밀봉이 해제되었습니다.", + }; + } catch (error) { + console.error("RFQ 밀봉 상태 변경 실패:", error); + return { + success: false, + error: error instanceof Error ? error.message : "알 수 없는 오류가 발생했습니다.", + }; + } +} + +// 여러 RFQ 일괄 밀봉 +export async function sealMultipleRfqs(rfqIds: number[]) { + try { + const session = await getServerSession(authOptions) + + if (!session?.user) { + throw new Error("인증이 필요합니다.") + } + + if (!rfqIds || rfqIds.length === 0) { + throw new Error("선택된 RFQ가 없습니다."); + } + + const updated = await db + .update(rfqsLast) + .set({ + rfqSealedYn: true, + updatedBy: Number(session.user.id), + updatedAt: new Date(), + }) + .where(inArray(rfqsLast.id, rfqIds)) + .returning(); + + revalidatePath("/evcp/rfq-last"); + + return { + success: true, + count: updated.length, + message: `${updated.length}건의 견적이 밀봉되었습니다.`, + }; + } catch (error) { + console.error("RFQ 일괄 밀봉 실패:", error); + return { + success: false, + error: error instanceof Error ? error.message : "알 수 없는 오류가 발생했습니다.", + }; + } +} + +// 여러 RFQ 일괄 밀봉 해제 +export async function unsealMultipleRfqs(rfqIds: number[]) { + try { + const session = await getServerSession(authOptions) + + if (!session?.user) { + throw new Error("인증이 필요합니다.") + } + if (!rfqIds || rfqIds.length === 0) { + throw new Error("선택된 RFQ가 없습니다."); + } + + const updated = await db + .update(rfqsLast) + .set({ + rfqSealedYn: false, + updatedBy: Number(session.user.id), + updatedAt: new Date(), + }) + .where(inArray(rfqsLast.id, rfqIds)) + .returning(); + + revalidatePath("/evcp/rfq-last"); + + return { + success: true, + count: updated.length, + message: `${updated.length}건의 견적 밀봉이 해제되었습니다.`, + }; + } catch (error) { + console.error("RFQ 밀봉 해제 실패:", error); + return { + success: false, + error: error instanceof Error ? error.message : "알 수 없는 오류가 발생했습니다.", + }; + } +} + +// 단일 RFQ 밀봉 (밀봉만) +export async function sealRfq(rfqId: number) { + try { + const session = await getServerSession(authOptions) + + if (!session?.user) { + throw new Error("인증이 필요합니다.") + } + + const [updated] = await db + .update(rfqsLast) + .set({ + rfqSealedYn: true, + updatedBy: Number(session.user.id), + updatedAt: new Date(), + }) + .where(eq(rfqsLast.id, rfqId)) + .returning(); + + if (!updated) { + throw new Error("RFQ를 찾을 수 없습니다."); + } + + revalidatePath("/evcp/rfq-last"); + + return { + success: true, + data: updated, + message: "견적이 밀봉되었습니다.", + }; + } catch (error) { + console.error("RFQ 밀봉 실패:", error); + return { + success: false, + error: error instanceof Error ? error.message : "알 수 없는 오류가 발생했습니다.", + }; + } +} + +// 단일 RFQ 밀봉 해제 +export async function unsealRfq(rfqId: number) { + try { + const session = await getServerSession(authOptions) + + if (!session?.user) { + throw new Error("인증이 필요합니다.") + } + + const [updated] = await db + .update(rfqsLast) + .set({ + rfqSealedYn: false, + updatedBy: Number(session.user.id), + updatedAt: new Date(), + }) + .where(eq(rfqsLast.id, rfqId)) + .returning(); + + if (!updated) { + throw new Error("RFQ를 찾을 수 없습니다."); + } + + revalidatePath("/evcp/rfq-last"); + + return { + success: true, + data: updated, + message: "견적 밀봉이 해제되었습니다.", + }; + } catch (error) { + console.error("RFQ 밀봉 해제 실패:", error); + return { + success: false, + error: error instanceof Error ? error.message : "알 수 없는 오류가 발생했습니다.", + }; + } +} + + + +export async function updateShortList( + rfqId: number, + vendorIds: number[], + shortListStatus: boolean = true +) { + try { + // 권한 체크 등 필요한 검증 + const session = await getServerSession(authOptions) + + if (!session?.user) { + throw new Error("인증이 필요합니다.") + } + + // 트랜잭션으로 처리 + const result = await db.transaction(async (tx) => { + // 해당 RFQ의 모든 벤더들의 shortList를 먼저 false로 설정 (선택적) + // 만약 선택된 것만 true로 하고 나머지는 그대로 두려면 이 부분 제거 + await tx + .update(rfqLastDetails) + .set({ + shortList: false, + updatedBy: session.user.id, + updatedAt: new Date() + }) + .where( + and( + eq(rfqLastDetails.rfqsLastId, rfqId), + eq(rfqLastDetails.isLatest, true) + ) + ); + + // 선택된 벤더들의 shortList를 true로 설정 + if (vendorIds.length > 0) { + const updates = await Promise.all( + vendorIds.map(vendorId => + tx + .update(rfqLastDetails) + .set({ + shortList: shortListStatus, + updatedBy: session.user.id, + updatedAt: new Date() + }) + .where( + and( + eq(rfqLastDetails.rfqsLastId, rfqId), + eq(rfqLastDetails.vendorsId, vendorId), + eq(rfqLastDetails.isLatest, true) + ) + ) + .returning() + ) + ); + + return { + success: true, + updatedCount: updates.length, + vendorIds + }; + } + + return { + success: true, + updatedCount: 0, + vendorIds: [] + }; + }); + + // revalidatePath(`/buyer/rfq/${rfqId}`); + return result; + + } catch (error) { + console.error("Short List 업데이트 실패:", error); + throw new Error("Short List 업데이트에 실패했습니다."); + } } \ No newline at end of file diff --git a/lib/rfq-last/table/rfq-seal-toggle-cell.tsx b/lib/rfq-last/table/rfq-seal-toggle-cell.tsx new file mode 100644 index 00000000..99360978 --- /dev/null +++ b/lib/rfq-last/table/rfq-seal-toggle-cell.tsx @@ -0,0 +1,93 @@ + +"use client"; + +import * as React from "react"; +import { Lock, LockOpen } from "lucide-react"; +import { Button } from "@/components/ui/button"; +import { + Tooltip, + TooltipContent, + TooltipProvider, + TooltipTrigger, +} from "@/components/ui/tooltip"; +import { toast } from "sonner"; +import { toggleRfqSealed } from "../service"; + +interface RfqSealToggleCellProps { + rfqId: number; + isSealed: boolean; + onUpdate?: () => void; +} + +export function RfqSealToggleCell({ + rfqId, + isSealed, + onUpdate +}: RfqSealToggleCellProps) { + const [isLoading, setIsLoading] = React.useState(false); + const [currentSealed, setCurrentSealed] = React.useState(isSealed); + + const handleToggle = async (e: React.MouseEvent) => { + e.stopPropagation(); // 행 선택 방지 + + setIsLoading(true); + try { + const result = await toggleRfqSealed(rfqId); + + if (result.success) { + setCurrentSealed(result.data?.rfqSealedYn ?? !currentSealed); + toast.success(result.message); + onUpdate?.(); // 테이블 데이터 새로고침 + } else { + toast.error(result.error); + } + } catch (error) { + toast.error("밀봉 상태 변경 중 오류가 발생했습니다."); + } finally { + setIsLoading(false); + } + }; + + return ( + + + + + + +

{currentSealed ? "밀봉 해제하기" : "밀봉하기"}

+
+
+
+ ); +} + +export const sealColumn = { + accessorKey: "rfqSealedYn", + header: ({ column }) => , + cell: ({ row, table }) => ( + { + // 테이블 데이터를 새로고침하는 로직 + // 이 부분은 상위 컴포넌트에서 refreshData 함수를 prop으로 전달받아 사용 + const meta = table.options.meta as any; + meta?.refreshData?.(); + }} + /> + ), + size: 80, + }; \ No newline at end of file diff --git a/lib/rfq-last/table/rfq-table-columns.tsx b/lib/rfq-last/table/rfq-table-columns.tsx index 5f5efcb4..eaf00660 100644 --- a/lib/rfq-last/table/rfq-table-columns.tsx +++ b/lib/rfq-last/table/rfq-table-columns.tsx @@ -18,6 +18,7 @@ import { DataTableRowAction } from "@/types/table"; import { format, differenceInDays } from "date-fns"; import { ko } from "date-fns/locale"; import { useRouter } from "next/navigation"; +import { RfqSealToggleCell } from "./rfq-seal-toggle-cell"; type NextRouter = ReturnType; @@ -120,18 +121,18 @@ export function getRfqColumns({ { accessorKey: "rfqSealedYn", header: ({ column }) => , - cell: ({ row }) => { - const isSealed = row.original.rfqSealedYn; - return ( -
- {isSealed ? ( - - ) : ( - - )} -
- ); - }, + cell: ({ row, table }) => ( + { + // 테이블 데이터를 새로고침하는 로직 + // 이 부분은 상위 컴포넌트에서 refreshData 함수를 prop으로 전달받아 사용 + const meta = table.options.meta as any; + meta?.refreshData?.(); + }} + /> + ), size: 80, }, @@ -453,18 +454,18 @@ export function getRfqColumns({ { accessorKey: "rfqSealedYn", header: ({ column }) => , - cell: ({ row }) => { - const isSealed = row.original.rfqSealedYn; - return ( -
- {isSealed ? ( - - ) : ( - - )} -
- ); - }, + cell: ({ row, table }) => ( + { + // 테이블 데이터를 새로고침하는 로직 + // 이 부분은 상위 컴포넌트에서 refreshData 함수를 prop으로 전달받아 사용 + const meta = table.options.meta as any; + meta?.refreshData?.(); + }} + /> + ), size: 80, }, @@ -815,18 +816,18 @@ export function getRfqColumns({ { accessorKey: "rfqSealedYn", header: ({ column }) => , - cell: ({ row }) => { - const isSealed = row.original.rfqSealedYn; - return ( -
- {isSealed ? ( - - ) : ( - - )} -
- ); - }, + cell: ({ row, table }) => ( + { + // 테이블 데이터를 새로고침하는 로직 + // 이 부분은 상위 컴포넌트에서 refreshData 함수를 prop으로 전달받아 사용 + const meta = table.options.meta as any; + meta?.refreshData?.(); + }} + /> + ), size: 80, }, diff --git a/lib/rfq-last/table/rfq-table-toolbar-actions.tsx b/lib/rfq-last/table/rfq-table-toolbar-actions.tsx index 9b696cbd..91b2798f 100644 --- a/lib/rfq-last/table/rfq-table-toolbar-actions.tsx +++ b/lib/rfq-last/table/rfq-table-toolbar-actions.tsx @@ -2,7 +2,7 @@ import * as React from "react"; import { type Table } from "@tanstack/react-table"; -import { Download, RefreshCw, Plus } from "lucide-react"; +import { Download, RefreshCw, Plus, Lock, LockOpen } from "lucide-react"; import { Button } from "@/components/ui/button"; import { @@ -12,8 +12,20 @@ import { DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; +import { + AlertDialog, + AlertDialogAction, + AlertDialogCancel, + AlertDialogContent, + AlertDialogDescription, + AlertDialogFooter, + AlertDialogHeader, + AlertDialogTitle, +} from "@/components/ui/alert-dialog"; +import { toast } from "sonner"; import { RfqsLastView } from "@/db/schema"; import { CreateGeneralRfqDialog } from "./create-general-rfq-dialog"; +import { sealMultipleRfqs, unsealMultipleRfqs } from "../service"; interface RfqTableToolbarActionsProps { table: Table; @@ -27,6 +39,43 @@ export function RfqTableToolbarActions({ rfqCategory = "itb", }: RfqTableToolbarActionsProps) { const [isExporting, setIsExporting] = React.useState(false); + const [isSealing, setIsSealing] = React.useState(false); + const [sealDialogOpen, setSealDialogOpen] = React.useState(false); + const [sealAction, setSealAction] = React.useState<"seal" | "unseal">("seal"); + + const selectedRows = table.getFilteredSelectedRowModel().rows; + const selectedRfqIds = selectedRows.map(row => row.original.id); + + // 선택된 항목들의 밀봉 상태 확인 + const sealedCount = selectedRows.filter(row => row.original.rfqSealedYn).length; + const unsealedCount = selectedRows.filter(row => !row.original.rfqSealedYn).length; + + const handleSealAction = React.useCallback(async (action: "seal" | "unseal") => { + setSealAction(action); + setSealDialogOpen(true); + }, []); + + const confirmSealAction = React.useCallback(async () => { + setIsSealing(true); + try { + const result = sealAction === "seal" + ? await sealMultipleRfqs(selectedRfqIds) + : await unsealMultipleRfqs(selectedRfqIds); + + if (result.success) { + toast.success(result.message); + table.toggleAllRowsSelected(false); // 선택 해제 + onRefresh?.(); // 데이터 새로고침 + } else { + toast.error(result.error); + } + } catch (error) { + toast.error("작업 중 오류가 발생했습니다."); + } finally { + setIsSealing(false); + setSealDialogOpen(false); + } + }, [sealAction, selectedRfqIds, table, onRefresh]); const handleExportCSV = React.useCallback(async () => { setIsExporting(true); @@ -36,6 +85,7 @@ export function RfqTableToolbarActions({ return { "RFQ 코드": original.rfqCode || "", "상태": original.status || "", + "밀봉여부": original.rfqSealedYn ? "밀봉" : "미밀봉", "프로젝트 코드": original.projectCode || "", "프로젝트명": original.projectName || "", "자재코드": original.itemCode || "", @@ -89,6 +139,7 @@ export function RfqTableToolbarActions({ return { "RFQ 코드": original.rfqCode || "", "상태": original.status || "", + "밀봉여부": original.rfqSealedYn ? "밀봉" : "미밀봉", "프로젝트 코드": original.projectCode || "", "프로젝트명": original.projectName || "", "자재코드": original.itemCode || "", @@ -115,48 +166,143 @@ export function RfqTableToolbarActions({ }, [table]); return ( -
- {onRefresh && ( - - )} - - - + <> +
+ {onRefresh && ( - - - - 전체 데이터 내보내기 - - - 선택한 항목 내보내기 ({table.getFilteredSelectedRowModel().rows.length}개) - - - - - {/* rfqCategory가 'general'일 때만 일반견적 생성 다이얼로그 표시 */} - {rfqCategory === "general" && ( - - ) } -
+ )} + + {/* 견적 밀봉/해제 버튼 */} + {selectedRfqIds.length > 0 && ( + + + + + + handleSealAction("seal")} + disabled={unsealedCount === 0} + > + + 선택 항목 밀봉 ({unsealedCount}개) + + handleSealAction("unseal")} + disabled={sealedCount === 0} + > + + 선택 항목 밀봉 해제 ({sealedCount}개) + + +
+ 전체 {selectedRfqIds.length}개 선택됨 +
+
+
+ )} + + + + + + + + 전체 데이터 내보내기 + + + 선택한 항목 내보내기 ({table.getFilteredSelectedRowModel().rows.length}개) + + + + + {/* rfqCategory가 'general'일 때만 일반견적 생성 다이얼로그 표시 */} + {rfqCategory === "general" && ( + + )} +
+ + {/* 밀봉 확인 다이얼로그 */} + + + + + {sealAction === "seal" ? "견적 밀봉 확인" : "견적 밀봉 해제 확인"} + + + {sealAction === "seal" + ? `선택한 ${unsealedCount}개의 견적을 밀봉하시겠습니까? 밀봉된 견적은 업체에서 수정할 수 없습니다.` + : `선택한 ${sealedCount}개의 견적 밀봉을 해제하시겠습니까? 밀봉이 해제되면 업체에서 견적을 수정할 수 있습니다.`} + + + + 취소 + + {isSealing ? "처리 중..." : "확인"} + + + + + ); +} + +// CSV 내보내기 유틸리티 함수 +function exportTableToCSV({ data, filename }: { data: any[]; filename: string }) { + if (!data || data.length === 0) { + console.warn("No data to export"); + return; + } + + const headers = Object.keys(data[0]); + const csvContent = [ + headers.join(","), + ...data.map(row => + headers.map(header => { + const value = row[header]; + // 값에 쉼표, 줄바꿈, 따옴표가 있으면 따옴표로 감싸기 + if (typeof value === "string" && (value.includes(",") || value.includes("\n") || value.includes('"'))) { + return `"${value.replace(/"/g, '""')}"`; + } + return value; + }).join(",") + ) + ].join("\n"); + + const blob = new Blob(["\uFEFF" + csvContent], { type: "text/csv;charset=utf-8;" }); + const link = document.createElement("a"); + link.href = URL.createObjectURL(blob); + link.download = filename; + link.click(); + URL.revokeObjectURL(link.href); } \ No newline at end of file diff --git a/lib/rfq-last/vendor-response/editor/vendor-response-editor.tsx b/lib/rfq-last/vendor-response/editor/vendor-response-editor.tsx index c146e42b..34259d37 100644 --- a/lib/rfq-last/vendor-response/editor/vendor-response-editor.tsx +++ b/lib/rfq-last/vendor-response/editor/vendor-response-editor.tsx @@ -1,6 +1,6 @@ "use client" -import { useState } from "react" +import { useState,useEffect } from "react" import { useForm, FormProvider } from "react-hook-form" import { zodResolver } from "@hookform/resolvers/zod" import * as z from "zod" @@ -163,18 +163,74 @@ export default function VendorResponseEditor({ const methods = useForm({ resolver: zodResolver(vendorResponseSchema), - defaultValues + defaultValues, + mode: 'onChange' // 추가: 실시간 validation }) + const { formState: { errors, isValid } } = methods + + useEffect(() => { + if (Object.keys(errors).length > 0) { + console.log('Validation errors:', errors) + } + }, [errors]) + + + + const handleFormSubmit = (isSubmit: boolean = false) => { + // 임시저장일 경우 validation 없이 바로 저장 + if (!isSubmit) { + const formData = methods.getValues() + onSubmit(formData, false) + return + } + + // 제출일 경우에만 validation 수행 + methods.handleSubmit( + (data) => onSubmit(data, isSubmit), + (errors) => { + console.error('Form validation errors:', errors) + + // 첫 번째 에러 필드로 포커스 이동 + const firstErrorField = Object.keys(errors)[0] + if (firstErrorField) { + // 어느 탭에 에러가 있는지 확인 + if (firstErrorField.startsWith('vendor') && + !firstErrorField.startsWith('vendorFirst') && + !firstErrorField.startsWith('vendorSparepart')) { + setActiveTab('terms') + } else if (firstErrorField === 'quotationItems') { + setActiveTab('items') + } + + // 구체적인 에러 메시지 표시 + if (errors.quotationItems) { + toast.error("견적 품목 정보를 확인해주세요. 모든 품목의 단가와 총액을 입력해야 합니다.") + } else { + toast.error("입력 정보를 확인해주세요.") + } + } + } + )() + } + const onSubmit = async (data: VendorResponseFormData, isSubmit: boolean = false) => { + console.log('onSubmit called with:', { data, isSubmit }) // 디버깅용 + setLoading(true) setUploadProgress(0) try { const formData = new FormData() + const fileMetadata = attachments.map((file: any) => ({ + attachmentType: file.attachmentType || "기타", + description: file.description || "" + })) + + // 기본 데이터 추가 - formData.append('data', JSON.stringify({ + const submitData = { ...data, rfqsLastId: rfq.id, rfqLastDetailsId: rfqDetail.id, @@ -183,69 +239,76 @@ export default function VendorResponseEditor({ submittedAt: isSubmit ? new Date().toISOString() : null, submittedBy: isSubmit ? userId : null, totalAmount: data.quotationItems.reduce((sum, item) => sum + item.totalPrice, 0), - updatedBy: userId - })) + updatedBy: userId, + fileMetadata + } + + console.log('Submitting data:', submitData) // 디버깅용 + + formData.append('data', JSON.stringify(submitData)) // 첨부파일 추가 attachments.forEach((file, index) => { formData.append(`attachments`, file) }) - // const response = await fetch(`/api/partners/rfq-last/${rfq.id}/response`, { - // method: existingResponse ? 'PUT' : 'POST', - // body: formData - // }) - - // if (!response.ok) { - // throw new Error('응답 저장에 실패했습니다.') - // } - - // XMLHttpRequest 사용하여 업로드 진행률 추적 - const xhr = new XMLHttpRequest() - - // Promise로 감싸서 async/await 사용 가능하게 - const uploadPromise = new Promise((resolve, reject) => { - // 업로드 진행률 이벤트 - xhr.upload.addEventListener('progress', (event) => { - if (event.lengthComputable) { - const percentComplete = Math.round((event.loaded / event.total) * 100) - setUploadProgress(percentComplete) - } - }) - - // 완료 이벤트 - xhr.addEventListener('load', () => { - if (xhr.status >= 200 && xhr.status < 300) { - setUploadProgress(100) - resolve(JSON.parse(xhr.responseText)) - } else { - reject(new Error('응답 저장에 실패했습니다.')) + // XMLHttpRequest 사용하여 업로드 진행률 추적 + const xhr = new XMLHttpRequest() + + const uploadPromise = new Promise((resolve, reject) => { + xhr.upload.addEventListener('progress', (event) => { + if (event.lengthComputable) { + const percentComplete = Math.round((event.loaded / event.total) * 100) + setUploadProgress(percentComplete) + } + }) + + xhr.addEventListener('load', () => { + if (xhr.status >= 200 && xhr.status < 300) { + setUploadProgress(100) + try { + const response = JSON.parse(xhr.responseText) + resolve(response) + } catch (e) { + console.error('Response parsing error:', e) + reject(new Error('응답 파싱 실패')) } - }) - - // 에러 이벤트 - xhr.addEventListener('error', () => { - reject(new Error('네트워크 오류가 발생했습니다.')) - }) - - // 요청 전송 - xhr.open(existingResponse ? 'PUT' : 'POST', `/api/partners/rfq-last/${rfq.id}/response`) - xhr.send(formData) + } else { + console.error('Server error:', xhr.status, xhr.responseText) + reject(new Error(`서버 오류: ${xhr.status}`)) + } + }) + + xhr.addEventListener('error', () => { + console.error('Network error') + reject(new Error('네트워크 오류가 발생했습니다.')) }) + + // 요청 전송 + const method = existingResponse ? 'PUT' : 'POST' + const url = `/api/partners/rfq-last/${rfq.id}/response` + + console.log(`Sending ${method} request to ${url}`) // 디버깅용 - await uploadPromise + xhr.open(method, url) + xhr.send(formData) + }) + + await uploadPromise toast.success(isSubmit ? "견적서가 제출되었습니다." : "견적서가 저장되었습니다.") router.push('/partners/rfq-last') router.refresh() } catch (error) { - console.error('Error:', error) - toast.error("오류가 발생했습니다.") + console.error('Submit error:', error) // 더 상세한 에러 로깅 + toast.error(error instanceof Error ? error.message : "오류가 발생했습니다.") } finally { setLoading(false) + setUploadProgress(0) } } + const totalAmount = methods.watch('quotationItems')?.reduce( (sum, item) => sum + (item.totalPrice || 0), 0 ) || 0 @@ -256,7 +319,10 @@ export default function VendorResponseEditor({ return ( -
onSubmit(data, false))}> + { + e.preventDefault() // 기본 submit 동작 방지 + handleFormSubmit(false) + }}>
{/* 헤더 정보 */} @@ -293,92 +359,92 @@ export default function VendorResponseEditor({ - {basicContracts.length > 0 ? ( -
- {/* 계약 목록 - 그리드 레이아웃 */} -
- {basicContracts.map((contract) => ( -
-
-
- -
-
-

- {contract.templateName} -

- - {contract.signedAt ? ( - <> - - 서명완료 - + {basicContracts.length > 0 ? ( +
+ {/* 계약 목록 - 그리드 레이아웃 */} +
+ {basicContracts.map((contract) => ( +
+
+
+ +
+
+

+ {contract.templateName} +

+ + {contract.signedAt ? ( + <> + + 서명완료 + + ) : ( + <> + + 서명대기 + + )} + +

+ {contract.signedAt + ? `${formatDate(new Date(contract.signedAt))}` + : contract.deadline + ? `~${formatDate(new Date(contract.deadline))}` + : '마감일 없음'} +

+
+
+
+ ))} +
+ + {/* 서명 상태 요약 및 액션 */} + {basicContracts.some(contract => !contract.signedAt) ? ( +
+
+ +
+

+ 서명 대기: {basicContracts.filter(c => !c.signedAt).length}/{basicContracts.length}개 +

+

+ 견적서 제출 전 모든 계약서 서명 필요 +

+
+
+ +
+ ) : ( + + + + 모든 기본계약 서명 완료 + + + )} +
) : ( - <> - - 서명대기 - +
+ +

+ 이 RFQ에 요청된 기본계약이 없습니다 +

+
)} -
-

- {contract.signedAt - ? `${formatDate(new Date(contract.signedAt))}` - : contract.deadline - ? `~${formatDate(new Date(contract.deadline))}` - : '마감일 없음'} -

-
-
-
- ))} -
- - {/* 서명 상태 요약 및 액션 */} - {basicContracts.some(contract => !contract.signedAt) ? ( -
-
- -
-

- 서명 대기: {basicContracts.filter(c => !c.signedAt).length}/{basicContracts.length}개 -

-

- 견적서 제출 전 모든 계약서 서명 필요 -

-
-
- -
- ) : ( - - - - 모든 기본계약 서명 완료 - - - )} -
- ) : ( -
- -

- 이 RFQ에 요청된 기본계약이 없습니다 -

-
- )} -
+ @@ -429,8 +495,9 @@ export default function VendorResponseEditor({ 취소 - {selectedRows.length > 0 && ( - <> - - - - )} - -
- ), [selectedRows, isRefreshing, isLoadingSendData, handleBulkSend]); + const additionalActions = React.useMemo(() => { + + // 참여 의사가 있는 선택된 벤더 수 계산 + const participatingCount = selectedRows.length; + const shortListCount = selectedRows.filter(v=>v.shortList).length; + + // 견적서가 있는 선택된 벤더 수 계산 + const quotationCount = selectedRows.filter(row => + row.response?.submission?.submittedAt + ).length; + + return ( +
+ + + {selectedRows.length > 0 && ( + <> + {/* Short List 확정 버튼 */} + + + {/* 견적 비교 버튼 */} + + + {/* 정보 일괄 입력 버튼 */} + + + {/* RFQ 발송 버튼 */} + + + )} + + +
+ ); + }, [selectedRows, isRefreshing, isLoadingSendData, handleBulkSend, handleShortListConfirm, handleQuotationCompare, isUpdatingShortList]); return ( <> diff --git a/lib/soap/ecc/send/pcr-confirm.ts b/lib/soap/ecc/send/pcr-confirm.ts index 439ec6f8..7799d007 100644 --- a/lib/soap/ecc/send/pcr-confirm.ts +++ b/lib/soap/ecc/send/pcr-confirm.ts @@ -43,7 +43,7 @@ export interface PCRConfirmResponse { // 1,ZMM_PCR,PCR_REQ,M,CHAR,10,PCR 요청번호 // 2,ZMM_PCR,PCR_REQ_SEQ,M,NUMC,5,PCR 요청순번 // 3,ZMM_PCR,PCR_DEC_DATE,M,DATS,8,PCR 결정일 -// 4,ZMM_PCR,EBELN,M,CHAR,10,구매오더 +// 4,ZMM_PCR,EBELN,M,CHAR,10,구매오더(PO번호) // 5,ZMM_PCR,EBELP,M,NUMC,5,구매오더 품번 // 6,ZMM_PCR,WAERS,,CUKY,5,PCR 통화 // 7,ZMM_PCR,PCR_NETPR,,CURR,"13,2",PCR 단가 diff --git a/lib/tbe-last/service.ts b/lib/tbe-last/service.ts index 760f66ac..d9046524 100644 --- a/lib/tbe-last/service.ts +++ b/lib/tbe-last/service.ts @@ -6,10 +6,11 @@ import db from "@/db/db"; import { and, desc, asc, eq, sql, or, isNull, isNotNull, ne, inArray } from "drizzle-orm"; import { tbeLastView, tbeDocumentsView } from "@/db/schema"; import { rfqPrItems } from "@/db/schema/rfqLast"; -import { rfqLastTbeDocumentReviews, rfqLastTbePdftronComments, rfqLastTbeVendorDocuments } from "@/db/schema"; +import { rfqLastTbeDocumentReviews, rfqLastTbePdftronComments, rfqLastTbeVendorDocuments,rfqLastTbeSessions } from "@/db/schema"; import { filterColumns } from "@/lib/filter-columns"; import { GetTBELastSchema } from "./validations"; - +import { getServerSession } from "next-auth" +import { authOptions } from "@/app/api/auth/[...nextauth]/route" // ========================================== // 1. TBE 세션 목록 조회 // ========================================== @@ -87,8 +88,8 @@ export async function getAllTBELast(input: GetTBELastSchema) { // 2. TBE 세션 상세 조회 // ========================================== export async function getTBESessionDetail(sessionId: number) { - return unstable_cache( - async () => { + // return unstable_cache( + // async () => { // 세션 기본 정보 const [session] = await db .select() @@ -153,13 +154,13 @@ export async function getTBESessionDetail(sessionId: number) { prItems, documents: documentsWithComments, }; - }, - [`tbe-session-${sessionId}`], - { - revalidate: 60, - tags: [`tbe-session-${sessionId}`], - } - )(); + // }, + // [`tbe-session-${sessionId}`], + // { + // revalidate: 60, + // tags: [`tbe-session-${sessionId}`], + // } + // )(); } // ========================================== @@ -190,25 +191,6 @@ export async function getDocumentComments(documentReviewId: number) { return comments; } -// ========================================== -// 4. TBE 평가 결과 업데이트 -// ========================================== -export async function updateTBEEvaluation( - sessionId: number, - data: { - evaluationResult: "pass" | "conditional_pass" | "non_pass"; - conditionalRequirements?: string; - technicalSummary?: string; - commercialSummary?: string; - overallRemarks?: string; - } -) { - // 실제 업데이트 로직 - // await db.update(rfqLastTbeSessions)... - - // 캐시 무효화 - return { success: true }; -} // ========================================== // 5. 벤더 문서 업로드 @@ -244,4 +226,193 @@ export async function uploadVendorDocument( .returning(); return document; +} + +interface UpdateEvaluationData { + evaluationResult?: "Acceptable" | "Acceptable with Comment" | "Not Acceptable" + conditionalRequirements?: string + conditionsFulfilled?: boolean + technicalSummary?: string + commercialSummary?: string + overallRemarks?: string + approvalRemarks?: string + status?: "준비중" | "진행중" | "검토중" | "보류" | "완료" | "취소" +} + +export async function updateTbeEvaluation( + tbeSessionId: number, + data: UpdateEvaluationData +) { + try { + const session = await getServerSession(authOptions) + if (!session?.user) { + return { success: false, error: "인증이 필요합니다" } + } + + const userId = typeof session.user.id === 'string' ? parseInt(session.user.id) : session.user.id + + // 현재 TBE 세션 조회 + const [currentTbeSession] = await db + .select() + .from(rfqLastTbeSessions) + .where(eq(rfqLastTbeSessions.id, tbeSessionId)) + .limit(1) + + if (!currentTbeSession) { + return { success: false, error: "TBE 세션을 찾을 수 없습니다" } + } + + // 업데이트 데이터 준비 + const updateData: any = { + updatedBy: userId, + updatedAt: new Date() + } + + // 평가 결과 관련 필드 + if (data.evaluationResult !== undefined) { + updateData.evaluationResult = data.evaluationResult + } + + // 조건부 승인 관련 (Acceptable with Comment인 경우) + if (data.evaluationResult === "Acceptable with Comment") { + if (data.conditionalRequirements !== undefined) { + updateData.conditionalRequirements = data.conditionalRequirements + } + if (data.conditionsFulfilled !== undefined) { + updateData.conditionsFulfilled = data.conditionsFulfilled + } + } else if (data.evaluationResult === "Acceptable") { + // Acceptable인 경우 조건부 필드 초기화 + updateData.conditionalRequirements = null + updateData.conditionsFulfilled = true + } else if (data.evaluationResult === "Not Acceptable") { + // Not Acceptable인 경우 조건부 필드 초기화 + updateData.conditionalRequirements = null + updateData.conditionsFulfilled = false + } + + // 평가 요약 필드 + if (data.technicalSummary !== undefined) { + updateData.technicalSummary = data.technicalSummary + } + if (data.commercialSummary !== undefined) { + updateData.commercialSummary = data.commercialSummary + } + if (data.overallRemarks !== undefined) { + updateData.overallRemarks = data.overallRemarks + } + + // 승인 관련 필드 + if (data.approvalRemarks !== undefined) { + updateData.approvalRemarks = data.approvalRemarks + updateData.approvedBy = userId + updateData.approvedAt = new Date() + } + + // 상태 업데이트 + if (data.status !== undefined) { + updateData.status = data.status + + // 완료 상태로 변경 시 종료일 설정 + if (data.status === "완료") { + updateData.actualEndDate = new Date() + } + } + + // TBE 세션 업데이트 + const [updated] = await db + .update(rfqLastTbeSessions) + .set(updateData) + .where(eq(rfqLastTbeSessions.id, tbeSessionId)) + .returning() + + // 캐시 초기화 + revalidateTag(`tbe-session-${tbeSessionId}`) + revalidateTag(`tbe-sessions`) + + // RFQ 관련 캐시도 초기화 + if (currentTbeSession.rfqsLastId) { + revalidateTag(`rfq-${currentTbeSession.rfqsLastId}`) + } + + return { + success: true, + data: updated, + message: "평가가 성공적으로 저장되었습니다" + } + + } catch (error) { + console.error("Failed to update TBE evaluation:", error) + return { + success: false, + error: error instanceof Error ? error.message : "평가 저장에 실패했습니다" + } + } +} + +export async function getTbeVendorDocuments(tbeSessionId: number) { + + try { + const documents = await db + .select({ + id: rfqLastTbeVendorDocuments.id, + documentName: rfqLastTbeVendorDocuments.originalFileName, + documentType: rfqLastTbeVendorDocuments.documentType, + fileName: rfqLastTbeVendorDocuments.fileName, + fileSize: rfqLastTbeVendorDocuments.fileSize, + fileType: rfqLastTbeVendorDocuments.fileType, + documentNo: rfqLastTbeVendorDocuments.documentNo, + revisionNo: rfqLastTbeVendorDocuments.revisionNo, + issueDate: rfqLastTbeVendorDocuments.issueDate, + description: rfqLastTbeVendorDocuments.description, + submittedAt: rfqLastTbeVendorDocuments.submittedAt, + // 검토 정보는 rfqLastTbeDocumentReviews에서 가져옴 + reviewStatus: rfqLastTbeDocumentReviews.reviewStatus, + reviewComments: rfqLastTbeDocumentReviews.reviewComments, + reviewedAt: rfqLastTbeDocumentReviews.reviewedAt, + requiresRevision: rfqLastTbeDocumentReviews.requiresRevision, + technicalCompliance: rfqLastTbeDocumentReviews.technicalCompliance, + qualityAcceptable: rfqLastTbeDocumentReviews.qualityAcceptable, + }) + .from(rfqLastTbeVendorDocuments) + .leftJoin( + rfqLastTbeDocumentReviews, + and( + eq(rfqLastTbeDocumentReviews.vendorAttachmentId, rfqLastTbeVendorDocuments.id), + eq(rfqLastTbeDocumentReviews.documentSource, "vendor") + ) + ) + .where(eq(rfqLastTbeVendorDocuments.tbeSessionId, tbeSessionId)) + .orderBy(rfqLastTbeVendorDocuments.submittedAt) + + // 문서 정보 매핑 (reviewStatus는 이미 한글로 저장되어 있음) + const mappedDocuments = documents.map(doc => ({ + ...doc, + reviewStatus: doc.reviewStatus || "미검토", // null인 경우 기본값 + reviewRequired: doc.requiresRevision || false, // UI 호환성을 위해 필드명 매핑 + })) + + return { + success: true, + documents: mappedDocuments, + } + } catch (error) { + console.error("Failed to fetch vendor documents:", error) + return { + success: false, + error: "벤더 문서를 불러오는데 실패했습니다", + documents: [], + } + } +} +// 리뷰 상태 매핑 함수 +function mapReviewStatus(status: string | null): string { + const statusMap: Record = { + "pending": "미검토", + "reviewing": "검토중", + "approved": "승인", + "rejected": "반려", + } + + return status ? (statusMap[status] || status) : "미검토" } \ No newline at end of file diff --git a/lib/tbe-last/table/documents-sheet.tsx b/lib/tbe-last/table/documents-sheet.tsx new file mode 100644 index 00000000..96e6e178 --- /dev/null +++ b/lib/tbe-last/table/documents-sheet.tsx @@ -0,0 +1,543 @@ +// lib/tbe-last/table/dialogs/documents-sheet.tsx + +"use client" + +import * as React from "react" +import { + Sheet, + SheetContent, + SheetHeader, + SheetTitle, + SheetDescription +} from "@/components/ui/sheet" +import { Badge } from "@/components/ui/badge" +import { Button } from "@/components/ui/button" +import { Input } from "@/components/ui/input" +import { Textarea } from "@/components/ui/textarea" +import { + Table, + TableBody, + TableCell, + TableHead, + TableHeader, + TableRow, +} from "@/components/ui/table" +import { + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue, +} from "@/components/ui/select" +import { + DropdownMenu, + DropdownMenuContent, + DropdownMenuItem, + DropdownMenuTrigger, +} from "@/components/ui/dropdown-menu" +import { ScrollArea } from "@/components/ui/scroll-area" +import { formatDate } from "@/lib/utils" +import { downloadFile, getFileInfo } from "@/lib/file-download" +import { + FileText, + Eye, + Download, + MoreHorizontal, + Filter, + MessageSquare, + CheckCircle, + XCircle, + Clock, + AlertCircle, + Save, +} from "lucide-react" +import { toast } from "sonner" +import { useRouter } from "next/navigation" + +interface DocumentsSheetProps { + open: boolean + onOpenChange: (open: boolean) => void + sessionDetail: any + isLoading: boolean +} + +type CommentCount = { totalCount: number; openCount: number } +type CountMap = Record + +export function DocumentsSheet({ + open, + onOpenChange, + sessionDetail, + isLoading +}: DocumentsSheetProps) { + + console.log(sessionDetail, "sessionDetail") + + const [sourceFilter, setSourceFilter] = React.useState<"all" | "buyer" | "vendor">("all") + const [searchTerm, setSearchTerm] = React.useState("") + const [editingReviewId, setEditingReviewId] = React.useState(null) + const [reviewData, setReviewData] = React.useState>({}) + const [isSaving, setIsSaving] = React.useState>({}) + const [commentCounts, setCommentCounts] = React.useState({}) // <-- 추가 + const [countLoading, setCountLoading] = React.useState(false) + const router = useRouter() + + const allReviewIds = React.useMemo(() => { + const docs = sessionDetail?.documents ?? [] + const ids = new Set() + for (const d of docs) { + const id = Number(d?.documentReviewId) + if (Number.isFinite(id)) ids.add(id) + } + return Array.from(ids) + }, [sessionDetail?.documents]) + + React.useEffect(() => { + let aborted = false + ; (async () => { + if (allReviewIds.length === 0) { + setCommentCounts({}) + return + } + setCountLoading(true) + try { + // 너무 길어질 수 있으니 적당히 나눠서 호출(옵션) + const chunkSize = 100 + const chunks: number[][] = [] + for (let i = 0; i < allReviewIds.length; i += chunkSize) { + chunks.push(allReviewIds.slice(i, i + chunkSize)) + } + + const merged: CountMap = {} + for (const c of chunks) { + const qs = encodeURIComponent(c.join(",")) + const res = await fetch(`/api/pdftron-comments/xfdf/count?ids=${qs}`, { + credentials: "include", + cache: "no-store", + }) + if (!res.ok) throw new Error(`count api ${res.status}`) + const json = await res.json() + if (aborted) return + const data = (json?.data ?? {}) as Record + for (const [k, v] of Object.entries(data)) { + const idNum = Number(k) + if (Number.isFinite(idNum)) { + merged[idNum] = { totalCount: v.totalCount ?? 0, openCount: v.openCount ?? 0 } + } + } + } + if (!aborted) setCommentCounts(merged) + } catch (e) { + console.error("Failed to load comment counts", e) + } finally { + if (!aborted) setCountLoading(false) + } + })() + return () => { + aborted = true + } + }, [allReviewIds.join(",")]) // 의존성: id 목록이 바뀔 때만 + + // 문서 초기 데이터 설정 + React.useEffect(() => { + if (sessionDetail?.documents) { + const initialData: Record = {} + sessionDetail.documents.forEach((doc: any) => { + initialData[doc.documentReviewId] = { + reviewStatus: doc.reviewStatus || "미검토", + reviewComments: doc.reviewComments || "" + } + }) + setReviewData(initialData) + } + }, [sessionDetail]) + + // PDFtron 뷰어 열기 + const handleOpenPDFTron = (doc: any) => { + if (!doc.filePath) { + toast.error("파일 경로를 찾을 수 없습니다") + return + } + + const params = new URLSearchParams({ + filePath: doc.filePath, + documentId: doc.documentId.toString(), + documentReviewId: doc.documentReviewId?.toString() || '', + sessionId: sessionDetail?.session?.tbeSessionId?.toString() || '', + documentName: doc.documentName || '', + mode: 'review' + }) + + window.open(`/pdftron-viewer?${params.toString()}`, '_blank') + } + + // 파일이 PDFtron에서 열 수 있는지 확인 + const canOpenInPDFTron = (filePath: string) => { + if (!filePath) return false + const ext = filePath.split('.').pop()?.toLowerCase() + const supportedFormats = ['pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'jpg', 'jpeg', 'png', 'tiff', 'bmp'] + return supportedFormats.includes(ext || '') + } + + // 파일 다운로드 + const handleDownload = async (doc: any) => { + if (!doc.filePath) { + toast.error("파일 경로를 찾을 수 없습니다") + return + } + + await downloadFile(doc.filePath, doc.originalFileName || doc.documentName, { + action: 'download', + showToast: true, + onError: (error) => { + console.error('Download error:', error) + } + }) + } + + // 리뷰 상태 저장 + const handleSaveReview = async (doc: any) => { + const reviewId = doc.documentReviewId + setIsSaving({ ...isSaving, [reviewId]: true }) + + try { + // API 호출하여 리뷰 상태 저장 + const response = await fetch(`/api/document-reviews/${reviewId}`, { + method: 'PATCH', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + reviewStatus: reviewData[reviewId]?.reviewStatus, + reviewComments: reviewData[reviewId]?.reviewComments + }) + }) + + if (!response.ok) throw new Error('Failed to save review') + + toast.success("리뷰 저장 완료") + router.refresh() + setEditingReviewId(null) + } catch (error) { + console.error('Save review error:', error) + toast.error("리뷰 저장 실패") + } finally { + setIsSaving({ ...isSaving, [reviewId]: false }) + } + } + + // 리뷰 상태 아이콘 + const getReviewStatusIcon = (status: string) => { + switch (status) { + case "승인": + return + case "반려": + return + case "보류": + return + default: + return + } + } + + // 필터링된 문서 목록 + const filteredDocuments = React.useMemo(() => { + if (!sessionDetail?.documents) return [] + + return sessionDetail.documents.filter((doc: any) => { + // Source 필터 + if (sourceFilter !== "all" && doc.documentSource !== sourceFilter) { + return false + } + + // 검색어 필터 + if (searchTerm) { + const searchLower = searchTerm.toLowerCase() + return ( + doc.documentName?.toLowerCase().includes(searchLower) || + doc.documentType?.toLowerCase().includes(searchLower) || + doc.reviewComments?.toLowerCase().includes(searchLower) + ) + } + + return true + }) + }, [sessionDetail?.documents, sourceFilter, searchTerm]) + + return ( + + + + Documents & Review Management + + 문서 검토 및 코멘트 관리 + + + + {/* 필터 및 검색 */} +
+
+ + +
+ + setSearchTerm(e.target.value)} + className="max-w-sm" + /> + +
+ + Total: {filteredDocuments.length} + + {sessionDetail?.documents && ( + <> + + Buyer: {sessionDetail.documents.filter((d: any) => d.documentSource === "buyer").length} + + + Vendor: {sessionDetail.documents.filter((d: any) => d.documentSource === "vendor").length} + + + )} +
+
+ + {/* 문서 테이블 */} + {isLoading ? ( +
Loading...
+ ) : ( + + + + + Source + Document Name + Type + Review Status + Comments + Review Notes + Uploaded + Actions + + + + {filteredDocuments.length === 0 ? ( + + + No documents found + + + ) : ( + filteredDocuments.map((doc: any) => ( + + + + {doc.documentSource} + + + + +
+ + {doc.documentName} +
+
+ + {doc.documentType} + + + {editingReviewId === doc.documentReviewId ? ( + + ) : ( +
+ {getReviewStatusIcon(reviewData[doc.documentReviewId]?.reviewStatus || doc.reviewStatus)} + + {reviewData[doc.documentReviewId]?.reviewStatus || doc.reviewStatus || "미검토"} + +
+ )} +
+ + + + {(() => { + const id = Number(doc.documentReviewId) + const counts = Number.isFinite(id) ? commentCounts[id] : undefined + if (countLoading && !counts) { + return Loading… + } + if (!counts || counts.totalCount === 0) { + return - + } + return ( +
+ + + {counts.totalCount} + {counts.openCount > 0 && ( + + ({counts.openCount} open) + + )} + +
+ ) + })()} +
+ + + {editingReviewId === doc.documentReviewId ? ( +