From 871a6d46a769cbe9e87146434f4bcb2d6792ab81 Mon Sep 17 00:00:00 2001
From: dujinkim <dujin.kim@dtsolution.co.kr>
Date: Thu, 30 Oct 2025 10:44:47 +0000
Subject: (최겸) 구매 PQ/실사 재개발(테스트 필요), 정규업체등록 결재 개발, 실사
 의뢰 결재 후처리 등
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../vendors-table-toolbar-actions.tsx              | 91 +++++++++++++++++++++-
 1 file changed, 90 insertions(+), 1 deletion(-)

(limited to 'lib/pq/pq-review-table-new/vendors-table-toolbar-actions.tsx')

diff --git a/lib/pq/pq-review-table-new/vendors-table-toolbar-actions.tsx b/lib/pq/pq-review-table-new/vendors-table-toolbar-actions.tsx
index ea6b6189..98b1cc76 100644
--- a/lib/pq/pq-review-table-new/vendors-table-toolbar-actions.tsx
+++ b/lib/pq/pq-review-table-new/vendors-table-toolbar-actions.tsx
@@ -15,6 +15,7 @@ import {
   getFactoryLocationAnswer,
   getQMManagers
 } from "@/lib/pq/service"
+import { SiteVisitDialog } from "./site-visit-dialog"
 import { RequestInvestigationDialog } from "./request-investigation-dialog"
 import { CancelInvestigationDialog, ReRequestInvestigationDialog } from "./cancel-investigation-dialog"
 import { SendResultsDialog } from "./send-results-dialog"
@@ -49,6 +50,7 @@ export function VendorsTableToolbarActions({ table }: VendorsTableToolbarActions
   const [isCancelDialogOpen, setIsCancelDialogOpen] = React.useState(false)
   const [isSendResultsDialogOpen, setIsSendResultsDialogOpen] = React.useState(false)
   const [isReRequestDialogOpen, setIsReRequestDialogOpen] = React.useState(false)
+  const [isReinspectionDialogOpen, setIsReinspectionDialogOpen] = React.useState(false)
   const [isApprovalDialogOpen, setIsApprovalDialogOpen] = React.useState(false)
   const [isReRequestApprovalDialogOpen, setIsReRequestApprovalDialogOpen] = React.useState(false)
 
@@ -441,6 +443,53 @@ const handleOpenRequestDialog = async () => {
     }
   }
 
+  // 재실사 요청 처리
+  const handleRequestReinspection = async (data: {
+    qmManagerId: number,
+    forecastedAt: Date,
+    investigationAddress: string,
+    investigationNotes?: string
+  }) => {
+    try {
+      // 보완-재실사 대상 실사만 필터링
+      const supplementReinspectInvestigations = selectedRows.filter(row =>
+        row.original.investigation &&
+        row.original.investigation.evaluationResult === "SUPPLEMENT_REINSPECT"
+      );
+
+      if (supplementReinspectInvestigations.length === 0) {
+        toast.error("보완-재실사 대상 실사가 없습니다.");
+        return;
+      }
+
+      // 첫 번째 대상 실사로 재실사 요청 생성
+      const targetInvestigation = supplementReinspectInvestigations[0].original.investigation!;
+      const { requestSupplementReinspectionAction } = await import('@/lib/vendor-investigation/service');
+
+      const result = await requestSupplementReinspectionAction({
+        investigationId: targetInvestigation.id,
+        siteVisitData: {
+          inspectionDuration: 1.0, // 기본 1일
+          requestedStartDate: data.forecastedAt,
+          requestedEndDate: new Date(data.forecastedAt.getTime() + 24 * 60 * 60 * 1000), // 1일 후
+          shiAttendees: {},
+          vendorRequests: {},
+          additionalRequests: data.investigationNotes || "보완을 위한 재실사 요청입니다.",
+        }
+      });
+
+      if (result.success) {
+        toast.success("재실사 요청이 생성되었습니다.");
+        window.location.reload();
+      } else {
+        toast.error(result.error || "재실사 요청 생성 중 오류가 발생했습니다.");
+      }
+    } catch (error) {
+      console.error("재실사 요청 오류:", error);
+      toast.error("재실사 요청 중 오류가 발생했습니다.");
+    }
+  };
+
   // 실사 결과 발송 처리
   const handleSendInvestigationResults = async (data: { purchaseComment?: string }) => {
     try {
@@ -505,8 +554,14 @@ const handleOpenRequestDialog = async () => {
     row.original.investigation.investigationStatus === "CANCELED"
   ).length
 
+  // 재실사 요청 대상 수 확인 (보완-재실사 결과만)
+  const reinspectInvestigationsCount = selectedRows.filter(row =>
+    row.original.investigation &&
+    row.original.investigation.evaluationResult === "SUPPLEMENT_REINSPECT"
+  ).length
+
   // 미실사 PQ가 선택되었는지 확인
-  const hasNonInspectionPQ = selectedRows.some(row => 
+  const hasNonInspectionPQ = selectedRows.some(row =>
     row.original.type === "NON_INSPECTION"
   )
 
@@ -651,6 +706,22 @@ const handleOpenRequestDialog = async () => {
           <span className="hidden sm:inline">실사 재의뢰</span>
         </Button>
 
+        {/* 재실사 요청 버튼 */}
+        <Button
+          variant="outline"
+          size="sm"
+          onClick={() => setIsReinspectionDialogOpen(true)}
+          disabled={
+            isLoading ||
+            selectedRows.length === 0 ||
+            reinspectInvestigationsCount === 0
+          }
+          className="gap-2"
+        >
+          <RefreshCw className="size-4" aria-hidden="true" />
+          <span className="hidden sm:inline">재방문 실사 요청</span>
+        </Button>
+
         {/* 실사 결과 발송 버튼 */}
         <Button
           variant="outline"
@@ -727,6 +798,24 @@ const handleOpenRequestDialog = async () => {
         auditResults={auditResults}
       />
 
+      {/* 재방문실사 요청 Dialog */}
+      <SiteVisitDialog
+        isOpen={isReinspectionDialogOpen}
+        onClose={() => setIsReinspectionDialogOpen(false)}
+        onSubmit={handleRequestReinspection}
+        investigation={{
+          id: 0, // 재실사용으로 0으로 설정 (기존 데이터 로드 안함)
+          investigationMethod: "SITE_VISIT_EVAL",
+          investigationAddress: "",
+          vendorName: "재실사 대상",
+          vendorCode: "N/A",
+          projectName: "",
+          projectCode: "",
+          pqItems: null
+        }}
+        isReinspection={true}
+      />
+
       {/* 결재 미리보기 Dialog - 실사 의뢰 */}
       {session?.user && investigationFormData && (
         <ApprovalPreviewDialog
-- 
cgit v1.2.3

