summaryrefslogtreecommitdiff
path: root/lib/soap/ecc/send/pcr-confirm.ts
diff options
context:
space:
mode:
Diffstat (limited to 'lib/soap/ecc/send/pcr-confirm.ts')
-rw-r--r--lib/soap/ecc/send/pcr-confirm.ts682
1 files changed, 682 insertions, 0 deletions
diff --git a/lib/soap/ecc/send/pcr-confirm.ts b/lib/soap/ecc/send/pcr-confirm.ts
new file mode 100644
index 00000000..7ac2d931
--- /dev/null
+++ b/lib/soap/ecc/send/pcr-confirm.ts
@@ -0,0 +1,682 @@
+
+'use server'
+
+import { sendSoapXml, type SoapSendConfig, type SoapLogInfo, type SoapSendResult } from "@/lib/soap/sender";
+
+// ECC PCR 확인 엔드포인트
+const ECC_PCR_CONFIRM_ENDPOINT = "http://shii8dvddb01.hec.serp.shi.samsung.net:50000/sap/xi/engine?type=entry&version=3.0&Sender.Service=P2038_D&Interface=http%3A%2F%2Fshi.samsung.co.kr%2FP2_MM%2FMMM%5EP2MM3019_SO";
+
+// PCR 확인 요청 데이터 타입
+export interface PCRConfirmRequest {
+ ZMM_PCR: Array<{
+ PCR_REQ: string; // PCR 요청번호 (M)
+ PCR_REQ_SEQ: string; // PCR 요청순번 (M)
+ PCR_DEC_DATE: string; // PCR 결정일 (M, YYYYMMDD)
+ EBELN: string; // 구매오더 (M)
+ EBELP: string; // 구매오더 품번 (M)
+ WAERS?: string; // PCR 통화
+ PCR_NETPR?: string; // PCR 단가
+ PEINH?: string; // Price Unit, 수량에 대한 PER 당 단가
+ PCR_NETWR?: string; // PCR 금액
+ PCR_STATUS: string; // PCR 상태 (M)
+ REJ_CD?: string; // Reject 코드
+ REJ_RSN?: string; // Reject 사유
+ CONFIRM_CD?: string; // Confirm 코드
+ CONFIRM_RSN?: string; // Confirm 사유
+ }>;
+}
+
+// PCR 확인 응답 데이터 타입 (참고용)
+export interface PCRConfirmResponse {
+ ZMM_RT?: Array<{
+ PCR_REQ: string; // PCR 요청번호
+ PCR_REQ_SEQ: string; // PCR 요청순번
+ EBELN: string; // 구매오더
+ EBELP: string; // 구매오더 품번
+ MSGTY?: string; // Message Type
+ MSGTXT?: string; // Message Text
+ }>;
+}
+
+// 보낼 때 ZMM_PCR 객체 안에 담아보내야 함
+// SEQ,Table,Field,M/O,Type,Size,Description
+// 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,구매오더
+// 5,ZMM_PCR,EBELP,M,NUMC,5,구매오더 품번
+// 6,ZMM_PCR,WAERS,,CUKY,5,PCR 통화
+// 7,ZMM_PCR,PCR_NETPR,,CURR,"13,2",PCR 단가
+// 8,ZMM_PCR,PEINH,,DEC,5,"Price Unit, 수량에 대한 PER 당 단가"
+// 9,ZMM_PCR,PCR_NETWR,,CURR,"13,2",PCR 금액
+// 10,ZMM_PCR,PCR_STATUS,M,CHAR,1,PCR 상태
+// 11,ZMM_PCR,REJ_CD,,CHAR,10,Reject 코드
+// 12,ZMM_PCR,REJ_RSN,,CHAR,50,Reject 사유
+// 13,ZMM_PCR,CONFIRM_CD,,CHAR,10,Confirm 코드
+// 14,ZMM_PCR,CONFIRM_RSN,,CHAR,50,Confirm 사유
+// 15,ZMM_RT,PCR_REQ,M,CHAR,10,PCR 요청번호 REPLY(NOT TO SEND)
+// 16,ZMM_RT,PCR_REQ_SEQ,M,NUMC,5,PCR 요청순번 REPLY(NOT TO SEND)
+// 17,ZMM_RT,EBELN,M,CHAR,10,구매오더 REPLY(NOT TO SEND)
+// 18,ZMM_RT,EBELP,M,NUMC,5,구매오더 품번 REPLY(NOT TO SEND)
+// 19,ZMM_RT,MSGTY,,CHAR,1,Message Type REPLY(NOT TO SEND)
+// 20,ZMM_RT,MSGTXT,,CHAR,100,Message Text REPLY(NOT TO SEND)
+
+// SOAP Body Content 생성 함수
+function createPCRConfirmSoapBodyContent(pcrData: PCRConfirmRequest): Record<string, unknown> {
+ return {
+ 'ns0:MT_P2MM3019_S': {
+ 'ZMM_PCR': pcrData.ZMM_PCR
+ }
+ };
+}
+
+// PCR 데이터 검증 함수
+function validatePCRConfirmData(pcrData: PCRConfirmRequest): { isValid: boolean; errors: string[] } {
+ const errors: string[] = [];
+
+ // ZMM_PCR 배열 검증
+ if (!pcrData.ZMM_PCR || !Array.isArray(pcrData.ZMM_PCR) || pcrData.ZMM_PCR.length === 0) {
+ errors.push('ZMM_PCR은 필수이며 최소 1개 이상의 PCR 데이터가 있어야 합니다.');
+ } else {
+ pcrData.ZMM_PCR.forEach((item, index) => {
+ // 필수 필드 검증
+ if (!item.PCR_REQ || typeof item.PCR_REQ !== 'string' || item.PCR_REQ.trim() === '') {
+ errors.push(`ZMM_PCR[${index}].PCR_REQ은 필수입니다.`);
+ } else if (item.PCR_REQ.length > 10) {
+ errors.push(`ZMM_PCR[${index}].PCR_REQ은 10자를 초과할 수 없습니다.`);
+ }
+
+ if (!item.PCR_REQ_SEQ || typeof item.PCR_REQ_SEQ !== 'string' || item.PCR_REQ_SEQ.trim() === '') {
+ errors.push(`ZMM_PCR[${index}].PCR_REQ_SEQ는 필수입니다.`);
+ } else if (item.PCR_REQ_SEQ.length > 5) {
+ errors.push(`ZMM_PCR[${index}].PCR_REQ_SEQ는 5자를 초과할 수 없습니다.`);
+ }
+
+ if (!item.PCR_DEC_DATE || typeof item.PCR_DEC_DATE !== 'string' || item.PCR_DEC_DATE.trim() === '') {
+ errors.push(`ZMM_PCR[${index}].PCR_DEC_DATE는 필수입니다.`);
+ } else if (!/^\d{8}$/.test(item.PCR_DEC_DATE)) {
+ errors.push(`ZMM_PCR[${index}].PCR_DEC_DATE는 YYYYMMDD 형식이어야 합니다.`);
+ }
+
+ if (!item.EBELN || typeof item.EBELN !== 'string' || item.EBELN.trim() === '') {
+ errors.push(`ZMM_PCR[${index}].EBELN은 필수입니다.`);
+ } else if (item.EBELN.length > 10) {
+ errors.push(`ZMM_PCR[${index}].EBELN은 10자를 초과할 수 없습니다.`);
+ }
+
+ if (!item.EBELP || typeof item.EBELP !== 'string' || item.EBELP.trim() === '') {
+ errors.push(`ZMM_PCR[${index}].EBELP는 필수입니다.`);
+ } else if (item.EBELP.length > 5) {
+ errors.push(`ZMM_PCR[${index}].EBELP는 5자를 초과할 수 없습니다.`);
+ }
+
+ if (!item.PCR_STATUS || typeof item.PCR_STATUS !== 'string' || item.PCR_STATUS.trim() === '') {
+ errors.push(`ZMM_PCR[${index}].PCR_STATUS는 필수입니다.`);
+ } else if (item.PCR_STATUS.length > 1) {
+ errors.push(`ZMM_PCR[${index}].PCR_STATUS는 1자를 초과할 수 없습니다.`);
+ }
+
+ // 선택 필드 길이 검증
+ if (item.WAERS && item.WAERS.length > 5) {
+ errors.push(`ZMM_PCR[${index}].WAERS는 5자를 초과할 수 없습니다.`);
+ }
+
+ if (item.REJ_CD && item.REJ_CD.length > 10) {
+ errors.push(`ZMM_PCR[${index}].REJ_CD는 10자를 초과할 수 없습니다.`);
+ }
+
+ if (item.REJ_RSN && item.REJ_RSN.length > 50) {
+ errors.push(`ZMM_PCR[${index}].REJ_RSN은 50자를 초과할 수 없습니다.`);
+ }
+
+ if (item.CONFIRM_CD && item.CONFIRM_CD.length > 10) {
+ errors.push(`ZMM_PCR[${index}].CONFIRM_CD는 10자를 초과할 수 없습니다.`);
+ }
+
+ if (item.CONFIRM_RSN && item.CONFIRM_RSN.length > 50) {
+ errors.push(`ZMM_PCR[${index}].CONFIRM_RSN은 50자를 초과할 수 없습니다.`);
+ }
+ });
+ }
+
+ return {
+ isValid: errors.length === 0,
+ errors
+ };
+}
+
+// ECC로 PCR 확인 SOAP XML 전송하는 함수
+async function sendPCRConfirmToECC(pcrData: PCRConfirmRequest): Promise<SoapSendResult> {
+ try {
+ // 데이터 검증
+ const validation = validatePCRConfirmData(pcrData);
+ if (!validation.isValid) {
+ return {
+ success: false,
+ message: `데이터 검증 실패: ${validation.errors.join(', ')}`
+ };
+ }
+
+ // SOAP Body Content 생성
+ const soapBodyContent = createPCRConfirmSoapBodyContent(pcrData);
+
+ // SOAP 전송 설정
+ const config: SoapSendConfig = {
+ endpoint: ECC_PCR_CONFIRM_ENDPOINT,
+ envelope: soapBodyContent,
+ soapAction: 'http://sap.com/xi/WebService/soap1.1',
+ timeout: 30000, // PCR 확인은 30초 타임아웃
+ retryCount: 3,
+ retryDelay: 1000
+ };
+
+ // 로그 정보
+ const logInfo: SoapLogInfo = {
+ direction: 'OUTBOUND',
+ system: 'S-ERP ECC',
+ interface: 'IF_ECC_EVCP_PCR_CONFIRM'
+ };
+
+ const pcrReqs = pcrData.ZMM_PCR.map(item => `${item.PCR_REQ}-${item.PCR_REQ_SEQ}`).join(', ');
+ console.log(`📤 PCR 확인 요청 전송 시작 - PCR Requests: ${pcrReqs}`);
+ console.log(`🔍 확인 대상 PCR ${pcrData.ZMM_PCR.length}개`);
+
+ // SOAP XML 전송
+ const result = await sendSoapXml(config, logInfo);
+
+ if (result.success) {
+ console.log(`✅ PCR 확인 요청 전송 성공 - PCR Requests: ${pcrReqs}`);
+ } else {
+ console.error(`❌ PCR 확인 요청 전송 실패 - PCR Requests: ${pcrReqs}, 오류: ${result.message}`);
+ }
+
+ return result;
+
+ } catch (error) {
+ console.error('❌ PCR 확인 전송 중 오류 발생:', error);
+ return {
+ success: false,
+ message: error instanceof Error ? error.message : 'Unknown error'
+ };
+ }
+}
+
+// ========================================
+// 메인 PCR 확인 서버 액션 함수들
+// ========================================
+
+// 단일 PCR 확인 요청 처리
+export async function confirmPCR(pcrItem: {
+ PCR_REQ: string;
+ PCR_REQ_SEQ: string;
+ PCR_DEC_DATE: string;
+ EBELN: string;
+ EBELP: string;
+ PCR_STATUS: string;
+ WAERS?: string;
+ PCR_NETPR?: string;
+ PEINH?: string;
+ PCR_NETWR?: string;
+ REJ_CD?: string;
+ REJ_RSN?: string;
+ CONFIRM_CD?: string;
+ CONFIRM_RSN?: string;
+}): Promise<{
+ success: boolean;
+ message: string;
+ responseData?: string;
+ pcrRequest?: string;
+}> {
+ try {
+ console.log(`🚀 PCR 확인 요청 시작 - PCR: ${pcrItem.PCR_REQ}-${pcrItem.PCR_REQ_SEQ}`);
+
+ const pcrData: PCRConfirmRequest = {
+ ZMM_PCR: [pcrItem]
+ };
+
+ const result = await sendPCRConfirmToECC(pcrData);
+
+ return {
+ success: result.success,
+ message: result.success ? 'PCR 확인 요청이 성공적으로 전송되었습니다.' : result.message,
+ responseData: result.responseText,
+ pcrRequest: `${pcrItem.PCR_REQ}-${pcrItem.PCR_REQ_SEQ}`
+ };
+
+ } catch (error) {
+ console.error('❌ PCR 확인 요청 처리 실패:', error);
+ return {
+ success: false,
+ message: error instanceof Error ? error.message : 'Unknown error'
+ };
+ }
+}
+
+// 여러 PCR 배치 확인 요청 처리
+export async function confirmMultiplePCRs(pcrItems: Array<{
+ PCR_REQ: string;
+ PCR_REQ_SEQ: string;
+ PCR_DEC_DATE: string;
+ EBELN: string;
+ EBELP: string;
+ PCR_STATUS: string;
+ WAERS?: string;
+ PCR_NETPR?: string;
+ PEINH?: string;
+ PCR_NETWR?: string;
+ REJ_CD?: string;
+ REJ_RSN?: string;
+ CONFIRM_CD?: string;
+ CONFIRM_RSN?: string;
+}>): Promise<{
+ success: boolean;
+ message: string;
+ results?: Array<{ pcrRequest: string; success: boolean; error?: string }>;
+}> {
+ try {
+ console.log(`🚀 배치 PCR 확인 요청 시작: ${pcrItems.length}개`);
+
+ // 모든 PCR을 하나의 요청으로 처리
+ const pcrData: PCRConfirmRequest = {
+ ZMM_PCR: pcrItems
+ };
+
+ const result = await sendPCRConfirmToECC(pcrData);
+
+ // 결과 정리
+ const results = pcrItems.map(item => ({
+ pcrRequest: `${item.PCR_REQ}-${item.PCR_REQ_SEQ}`,
+ success: result.success,
+ error: result.success ? undefined : result.message
+ }));
+
+ const successCount = result.success ? pcrItems.length : 0;
+ const failCount = pcrItems.length - successCount;
+
+ console.log(`🎉 배치 PCR 확인 완료: 성공 ${successCount}개, 실패 ${failCount}개`);
+
+ return {
+ success: result.success,
+ message: result.success
+ ? `배치 PCR 확인 성공: ${successCount}개`
+ : `배치 PCR 확인 실패: ${result.message}`,
+ results
+ };
+
+ } catch (error) {
+ console.error('❌ 배치 PCR 확인 중 전체 오류 발생:', error);
+ return {
+ success: false,
+ message: error instanceof Error ? error.message : 'Unknown error'
+ };
+ }
+}
+
+// 개별 처리 방식의 배치 PCR 확인 (각각 따로 전송)
+export async function confirmMultiplePCRsIndividually(pcrItems: Array<{
+ PCR_REQ: string;
+ PCR_REQ_SEQ: string;
+ PCR_DEC_DATE: string;
+ EBELN: string;
+ EBELP: string;
+ PCR_STATUS: string;
+ WAERS?: string;
+ PCR_NETPR?: string;
+ PEINH?: string;
+ PCR_NETWR?: string;
+ REJ_CD?: string;
+ REJ_RSN?: string;
+ CONFIRM_CD?: string;
+ CONFIRM_RSN?: string;
+}>): Promise<{
+ success: boolean;
+ message: string;
+ results?: Array<{ pcrRequest: string; success: boolean; error?: string }>;
+}> {
+ try {
+ console.log(`🚀 개별 PCR 확인 요청 시작: ${pcrItems.length}개`);
+
+ const results: Array<{ pcrRequest: string; success: boolean; error?: string }> = [];
+
+ for (const pcrItem of pcrItems) {
+ try {
+ const pcrRequest = `${pcrItem.PCR_REQ}-${pcrItem.PCR_REQ_SEQ}`;
+ console.log(`📤 PCR 확인 처리 중: ${pcrRequest}`);
+
+ const pcrData: PCRConfirmRequest = {
+ ZMM_PCR: [pcrItem]
+ };
+
+ const result = await sendPCRConfirmToECC(pcrData);
+
+ if (result.success) {
+ console.log(`✅ PCR 확인 성공: ${pcrRequest}`);
+ results.push({
+ pcrRequest,
+ success: true
+ });
+ } else {
+ console.error(`❌ PCR 확인 실패: ${pcrRequest}, 오류: ${result.message}`);
+ results.push({
+ pcrRequest,
+ success: false,
+ error: result.message
+ });
+ }
+
+ // 개별 처리간 지연 (시스템 부하 방지)
+ if (pcrItems.length > 1) {
+ await new Promise(resolve => setTimeout(resolve, 500));
+ }
+
+ } catch (error) {
+ const pcrRequest = `${pcrItem.PCR_REQ}-${pcrItem.PCR_REQ_SEQ}`;
+ console.error(`❌ PCR 확인 처리 실패: ${pcrRequest}`, error);
+ results.push({
+ pcrRequest,
+ success: false,
+ error: error instanceof Error ? error.message : 'Unknown error'
+ });
+ }
+ }
+
+ const successCount = results.filter(r => r.success).length;
+ const failCount = results.length - successCount;
+
+ console.log(`🎉 개별 PCR 확인 완료: 성공 ${successCount}개, 실패 ${failCount}개`);
+
+ return {
+ success: failCount === 0,
+ message: `개별 PCR 확인 완료: 성공 ${successCount}개, 실패 ${failCount}개`,
+ results
+ };
+
+ } catch (error) {
+ console.error('❌ 개별 PCR 확인 중 전체 오류 발생:', error);
+ return {
+ success: false,
+ message: error instanceof Error ? error.message : 'Unknown error'
+ };
+ }
+}
+
+// 테스트용 PCR 확인 함수 (샘플 데이터 포함)
+export async function confirmTestPCR(): Promise<{
+ success: boolean;
+ message: string;
+ responseData?: string;
+ testData?: PCRConfirmRequest;
+}> {
+ try {
+ console.log('🧪 테스트용 PCR 확인 시작');
+
+ // 테스트용 샘플 데이터 생성
+ const testPCRData: PCRConfirmRequest = {
+ ZMM_PCR: [{
+ PCR_REQ: 'TEST_PCR01',
+ PCR_REQ_SEQ: '00001',
+ PCR_DEC_DATE: '20241201',
+ EBELN: 'TEST_PO01',
+ EBELP: '00010',
+ PCR_STATUS: 'A',
+ WAERS: 'KRW',
+ PCR_NETPR: '1000.00',
+ PEINH: '1',
+ PCR_NETWR: '1000.00',
+ CONFIRM_CD: 'CONF',
+ CONFIRM_RSN: '테스트 확인'
+ }]
+ };
+
+ const result = await sendPCRConfirmToECC(testPCRData);
+
+ return {
+ success: result.success,
+ message: result.success ? '테스트 PCR 확인이 성공적으로 전송되었습니다.' : result.message,
+ responseData: result.responseText,
+ testData: testPCRData
+ };
+
+ } catch (error) {
+ console.error('❌ 테스트 PCR 확인 실패:', error);
+ return {
+ success: false,
+ message: error instanceof Error ? error.message : 'Unknown error'
+ };
+ }
+}
+
+// ========================================
+// 유틸리티 함수들
+// ========================================
+
+// PCR 확인 데이터 생성 헬퍼 함수
+function createPCRConfirmData(pcrItems: Array<{
+ PCR_REQ: string;
+ PCR_REQ_SEQ: string;
+ PCR_DEC_DATE: string;
+ EBELN: string;
+ EBELP: string;
+ PCR_STATUS: string;
+ WAERS?: string;
+ PCR_NETPR?: string;
+ PEINH?: string;
+ PCR_NETWR?: string;
+ REJ_CD?: string;
+ REJ_RSN?: string;
+ CONFIRM_CD?: string;
+ CONFIRM_RSN?: string;
+}>): PCRConfirmRequest {
+ return {
+ ZMM_PCR: pcrItems
+ };
+}
+
+// PCR 요청번호 검증 함수
+function validatePCRNumber(pcrReq: string): { isValid: boolean; error?: string } {
+ if (!pcrReq || typeof pcrReq !== 'string') {
+ return { isValid: false, error: 'PCR 요청번호는 문자열이어야 합니다.' };
+ }
+
+ const trimmed = pcrReq.trim();
+ if (trimmed === '') {
+ return { isValid: false, error: 'PCR 요청번호는 비어있을 수 없습니다.' };
+ }
+
+ // 기본적인 길이 검증 (10자 제한 - WSDL에서 CHAR 10으로 정의)
+ if (trimmed.length > 10) {
+ return { isValid: false, error: 'PCR 요청번호는 10자를 초과할 수 없습니다.' };
+ }
+
+ return { isValid: true };
+}
+
+// PCR 요청순번 검증 함수
+function validatePCRSequence(pcrReqSeq: string): { isValid: boolean; error?: string } {
+ if (!pcrReqSeq || typeof pcrReqSeq !== 'string') {
+ return { isValid: false, error: 'PCR 요청순번은 문자열이어야 합니다.' };
+ }
+
+ const trimmed = pcrReqSeq.trim();
+ if (trimmed === '') {
+ return { isValid: false, error: 'PCR 요청순번은 비어있을 수 없습니다.' };
+ }
+
+ // 숫자 형식 검증 (NUMC 5자리)
+ if (!/^\d{1,5}$/.test(trimmed)) {
+ return { isValid: false, error: 'PCR 요청순번은 1-5자리 숫자여야 합니다.' };
+ }
+
+ return { isValid: true };
+}
+
+// PCR 결정일 검증 함수
+function validatePCRDecisionDate(pcrDecDate: string): { isValid: boolean; error?: string } {
+ if (!pcrDecDate || typeof pcrDecDate !== 'string') {
+ return { isValid: false, error: 'PCR 결정일은 문자열이어야 합니다.' };
+ }
+
+ const trimmed = pcrDecDate.trim();
+ if (trimmed === '') {
+ return { isValid: false, error: 'PCR 결정일은 비어있을 수 없습니다.' };
+ }
+
+ // YYYYMMDD 형식 검증
+ if (!/^\d{8}$/.test(trimmed)) {
+ return { isValid: false, error: 'PCR 결정일은 YYYYMMDD 형식이어야 합니다.' };
+ }
+
+ // 날짜 유효성 검증
+ const year = parseInt(trimmed.substring(0, 4));
+ const month = parseInt(trimmed.substring(4, 6));
+ const day = parseInt(trimmed.substring(6, 8));
+
+ if (month < 1 || month > 12) {
+ return { isValid: false, error: 'PCR 결정일의 월이 올바르지 않습니다.' };
+ }
+
+ if (day < 1 || day > 31) {
+ return { isValid: false, error: 'PCR 결정일의 일이 올바르지 않습니다.' };
+ }
+
+ // 실제 날짜 검증
+ const date = new Date(year, month - 1, day);
+ if (date.getFullYear() !== year || date.getMonth() !== month - 1 || date.getDate() !== day) {
+ return { isValid: false, error: 'PCR 결정일이 유효하지 않은 날짜입니다.' };
+ }
+
+ return { isValid: true };
+}
+
+// PCR 상태 검증 함수
+function validatePCRStatus(pcrStatus: string): { isValid: boolean; error?: string } {
+ if (!pcrStatus || typeof pcrStatus !== 'string') {
+ return { isValid: false, error: 'PCR 상태는 문자열이어야 합니다.' };
+ }
+
+ const trimmed = pcrStatus.trim();
+ if (trimmed === '') {
+ return { isValid: false, error: 'PCR 상태는 비어있을 수 없습니다.' };
+ }
+
+ if (trimmed.length !== 1) {
+ return { isValid: false, error: 'PCR 상태는 1자리여야 합니다.' };
+ }
+
+ return { isValid: true };
+}
+
+// 구매오더 검증 함수
+function validatePurchaseOrder(ebeln: string): { isValid: boolean; error?: string } {
+ if (!ebeln || typeof ebeln !== 'string') {
+ return { isValid: false, error: '구매오더는 문자열이어야 합니다.' };
+ }
+
+ const trimmed = ebeln.trim();
+ if (trimmed === '') {
+ return { isValid: false, error: '구매오더는 비어있을 수 없습니다.' };
+ }
+
+ if (trimmed.length > 10) {
+ return { isValid: false, error: '구매오더는 10자를 초과할 수 없습니다.' };
+ }
+
+ return { isValid: true };
+}
+
+// 구매오더 품번 검증 함수
+function validatePurchaseOrderItem(ebelp: string): { isValid: boolean; error?: string } {
+ if (!ebelp || typeof ebelp !== 'string') {
+ return { isValid: false, error: '구매오더 품번은 문자열이어야 합니다.' };
+ }
+
+ const trimmed = ebelp.trim();
+ if (trimmed === '') {
+ return { isValid: false, error: '구매오더 품번은 비어있을 수 없습니다.' };
+ }
+
+ // 숫자 형식 검증 (NUMC 5자리)
+ if (!/^\d{1,5}$/.test(trimmed)) {
+ return { isValid: false, error: '구매오더 품번은 1-5자리 숫자여야 합니다.' };
+ }
+
+ return { isValid: true };
+}
+
+// 통화 검증 함수
+function validateCurrency(waers?: string): { isValid: boolean; error?: string } {
+ if (!waers) {
+ return { isValid: true }; // 선택 필드
+ }
+
+ if (typeof waers !== 'string') {
+ return { isValid: false, error: '통화는 문자열이어야 합니다.' };
+ }
+
+ const trimmed = waers.trim();
+ if (trimmed.length > 5) {
+ return { isValid: false, error: '통화는 5자를 초과할 수 없습니다.' };
+ }
+
+ return { isValid: true };
+}
+
+// 여러 PCR 아이템 검증 함수
+function validatePCRItems(pcrItems: Array<{
+ PCR_REQ: string;
+ PCR_REQ_SEQ: string;
+ PCR_DEC_DATE: string;
+ EBELN: string;
+ EBELP: string;
+ PCR_STATUS: string;
+ WAERS?: string;
+}>): { isValid: boolean; errors: string[] } {
+ const errors: string[] = [];
+
+ if (!Array.isArray(pcrItems) || pcrItems.length === 0) {
+ errors.push('최소 1개 이상의 PCR 아이템이 필요합니다.');
+ return { isValid: false, errors };
+ }
+
+ pcrItems.forEach((item, index) => {
+ const pcrReqValid = validatePCRNumber(item.PCR_REQ);
+ if (!pcrReqValid.isValid) {
+ errors.push(`PCR[${index}] 요청번호: ${pcrReqValid.error}`);
+ }
+
+ const pcrSeqValid = validatePCRSequence(item.PCR_REQ_SEQ);
+ if (!pcrSeqValid.isValid) {
+ errors.push(`PCR[${index}] 요청순번: ${pcrSeqValid.error}`);
+ }
+
+ const pcrDateValid = validatePCRDecisionDate(item.PCR_DEC_DATE);
+ if (!pcrDateValid.isValid) {
+ errors.push(`PCR[${index}] 결정일: ${pcrDateValid.error}`);
+ }
+
+ const eBelnValid = validatePurchaseOrder(item.EBELN);
+ if (!eBelnValid.isValid) {
+ errors.push(`PCR[${index}] 구매오더: ${eBelnValid.error}`);
+ }
+
+ const eBelpValid = validatePurchaseOrderItem(item.EBELP);
+ if (!eBelpValid.isValid) {
+ errors.push(`PCR[${index}] 구매오더품번: ${eBelpValid.error}`);
+ }
+
+ const statusValid = validatePCRStatus(item.PCR_STATUS);
+ if (!statusValid.isValid) {
+ errors.push(`PCR[${index}] 상태: ${statusValid.error}`);
+ }
+
+ const currencyValid = validateCurrency(item.WAERS);
+ if (!currencyValid.isValid) {
+ errors.push(`PCR[${index}] 통화: ${currencyValid.error}`);
+ }
+ });
+
+ return {
+ isValid: errors.length === 0,
+ errors
+ };
+}