From cf8dac0c6490469dab88a560004b0c07dbd48612 Mon Sep 17 00:00:00 2001
From: dujinkim <dujin.kim@dtsolution.co.kr>
Date: Thu, 18 Sep 2025 00:23:40 +0000
Subject: (대표님) rfq, 계약, 서명 등
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../viewer/basic-contract-sign-viewer.tsx          | 72 +++++++++++++++++++++-
 1 file changed, 70 insertions(+), 2 deletions(-)

(limited to 'lib/basic-contract/viewer/basic-contract-sign-viewer.tsx')

diff --git a/lib/basic-contract/viewer/basic-contract-sign-viewer.tsx b/lib/basic-contract/viewer/basic-contract-sign-viewer.tsx
index 5698428e..77bfaf41 100644
--- a/lib/basic-contract/viewer/basic-contract-sign-viewer.tsx
+++ b/lib/basic-contract/viewer/basic-contract-sign-viewer.tsx
@@ -25,6 +25,7 @@ import { getActiveSurveyTemplate, getVendorSignatureFile, type SurveyTemplateWit
 import { useConditionalSurvey } from '../vendor-table/survey-conditional';
 import { SurveyComponent } from './SurveyComponent';
 import { GtcClausesComponent } from './GtcClausesComponent';
+import { getBuyerSignatureFileWithFallback } from "@/lib/shi-signature/buyer-signature";
 
 interface FileInfo {
   path: string;
@@ -256,6 +257,63 @@ class AutoSignatureFieldDetector {
   }
 }
 
+const applyBuyerSignatureAutomatically = async (instance: WebViewerInstance) => {
+  const { Core } = instance;
+  const { documentViewer, annotationManager } = Core;
+  const document = documentViewer.getDocument();
+
+  if (!document) return;
+
+  try {
+    console.log('🔍 구매자 서명란 자동 서명 시작...');
+    
+    // "삼성중공업_서명란" 텍스트 검색
+    const searchText = '삼성중공업_서명란';
+    const textSearchIterator = await document.getTextSearchIterator();
+    textSearchIterator.begin(searchText, Core.Search.Mode.PAGE_STOP | Core.Search.Mode.HIGHLIGHT);
+    
+    let searchResult = await textSearchIterator.next();
+    
+    if (searchResult && searchResult.resultCode === Core.Search.ResultCode.FOUND) {
+      const pageNumber = searchResult.pageNum;
+      const quads = searchResult.quads;
+      
+      if (quads && quads.length > 0) {
+        const quad = quads[0];
+        const x = Math.min(quad.x1, quad.x2, quad.x3, quad.x4);
+        const y = Math.min(quad.y1, quad.y2, quad.y3, quad.y4);
+        const textHeight = Math.abs(quad.y3 - quad.y1);
+        
+        // 구매자 서명 이미지 가져오기
+        const buyerSignature = await getBuyerSignatureFileWithFallback();
+        
+        if (buyerSignature) {
+          // 스탬프 어노테이션 생성
+          const stamp = new Core.Annotations.StampAnnotation();
+          stamp.PageNumber = pageNumber;
+          stamp.X = x;
+          stamp.Y = y + textHeight + 5; // 텍스트 아래 5픽셀
+          stamp.Width = 150;
+          stamp.Height = 50;
+          
+          await stamp.setImageData(buyerSignature.data.dataUrl);
+          
+          // 어노테이션 추가
+          annotationManager.addAnnotation(stamp);
+          annotationManager.drawAnnotationsFromList([stamp]);
+          
+          console.log('✅ 구매자 서명 자동 적용 완료');
+          toast.info('삼성중공업 서명이 자동으로 적용되었습니다.', {
+            duration: 3000
+          });
+        }
+      }
+    }
+  } catch (error) {
+    console.error('구매자 자동 서명 처리 실패:', error);
+  }
+};
+
 
 function useAutoSignatureFields(instance: WebViewerInstance | null, mode: 'vendor' | 'buyer' = 'vendor') {
   const [signatureFields, setSignatureFields] = useState<string[]>([]);
@@ -790,10 +848,20 @@ export function BasicContractSignViewer({
             const { WidgetFlags } = Annotations;
             const FitMode = newInstance.UI.FitMode;
 
-            const handleDocumentLoaded = () => {
+            const handleDocumentLoaded = async () => {
               setFileLoading(false);
               newInstance.UI.setFitMode(FitMode.FitWidth);
-
+            
+              // GTC 템플릿이 아닌 경우에만 구매자 자동 서명 적용
+              if (!templateName.includes('GTC')) {
+                try {
+                  // 구매자 서명란 찾아서 자동 서명
+                  await applyBuyerSignatureAutomatically(newInstance);
+                } catch (error) {
+                  console.error('구매자 자동 서명 실패:', error);
+                }
+              }
+            
               requestAnimationFrame(() => {
                 try {
                   documentViewer.refreshAll();
-- 
cgit v1.2.3

