summaryrefslogtreecommitdiff
path: root/lib/soap/ecc/mapper
diff options
context:
space:
mode:
Diffstat (limited to 'lib/soap/ecc/mapper')
-rw-r--r--lib/soap/ecc/mapper/po-mapper.ts427
-rw-r--r--lib/soap/ecc/mapper/rfq-and-pr-mapper.ts429
2 files changed, 856 insertions, 0 deletions
diff --git a/lib/soap/ecc/mapper/po-mapper.ts b/lib/soap/ecc/mapper/po-mapper.ts
new file mode 100644
index 00000000..a6363a87
--- /dev/null
+++ b/lib/soap/ecc/mapper/po-mapper.ts
@@ -0,0 +1,427 @@
+import { debugLog, debugSuccess, debugError } from '@/lib/debug-utils';
+import db from '@/db/db';
+import {
+ contracts,
+ contractItems,
+} from '@/db/schema/contract';
+import { projects } from '@/db/schema/projects';
+import { vendors } from '@/db/schema/vendors';
+import { items } from '@/db/schema/items';
+import {
+ ZMM_HD,
+ ZMM_DT,
+} from '@/db/schema/ECC/ecc';
+import { eq } from 'drizzle-orm';
+
+// ECC 데이터 타입 정의
+export type ECCPOHeader = typeof ZMM_HD.$inferInsert;
+export type ECCPODetail = typeof ZMM_DT.$inferInsert;
+
+// 비즈니스 테이블 데이터 타입 정의
+export type ContractData = typeof contracts.$inferInsert;
+export type ContractItemData = typeof contractItems.$inferInsert;
+
+// 처리된 PO 데이터 구조
+export interface ProcessedPOData {
+ header: ECCPOHeader;
+ details: ECCPODetail[];
+}
+
+/**
+ * ECC PO 헤더 데이터를 contracts 테이블로 매핑
+ */
+export async function mapECCPOHeaderToBusiness(
+ eccHeader: ECCPOHeader
+): Promise<ContractData> {
+ debugLog('ECC PO 헤더 매핑 시작', { ebeln: eccHeader.EBELN });
+
+ // projectId 찾기 (PSPID → projects.code 기반)
+ let projectId: number | null = null;
+ if (eccHeader.PSPID) {
+ try {
+ const project = await db.query.projects.findFirst({
+ where: eq(projects.code, eccHeader.PSPID),
+ });
+ if (project) {
+ projectId = project.id;
+ debugLog('프로젝트 ID 찾음', { pspid: eccHeader.PSPID, projectId });
+ } else {
+ debugError('프로젝트를 찾을 수 없음', { pspid: eccHeader.PSPID });
+ }
+ } catch (error) {
+ debugError('프로젝트 조회 중 오류', { pspid: eccHeader.PSPID, error });
+ }
+ }
+
+ // vendorId 찾기 (LIFNR 기반)
+ let vendorId: number | null = null;
+ if (eccHeader.LIFNR) {
+ try {
+ const vendor = await db.query.vendors.findFirst({
+ where: eq(vendors.vendorCode, eccHeader.LIFNR),
+ });
+ if (vendor) {
+ vendorId = vendor.id;
+ debugLog('벤더 ID 찾음', { lifnr: eccHeader.LIFNR, vendorId });
+ } else {
+ debugError('벤더를 찾을 수 없음', { lifnr: eccHeader.LIFNR });
+ }
+ } catch (error) {
+ debugError('벤더 조회 중 오류', { lifnr: eccHeader.LIFNR, error });
+ }
+ }
+
+ // 날짜 파싱 함수 (YYYYMMDD -> YYYY-MM-DD 문자열 형식)
+ const parseDate = (dateStr: string | null): string | null => {
+ if (!dateStr || dateStr.length !== 8) return null;
+ try {
+ const year = dateStr.substring(0, 4);
+ const month = dateStr.substring(4, 6);
+ const day = dateStr.substring(6, 8);
+ return `${year}-${month}-${day}`;
+ } catch (error) {
+ debugError('날짜 파싱 오류', { dateStr, error });
+ return null;
+ }
+ };
+
+ // 금액 파싱 함수
+ const parseAmount = (amountStr: string | null): string | null => {
+ if (!amountStr) return null;
+ try {
+ // 문자열을 숫자로 변환 후 다시 문자열로 (소수점 처리)
+ const num = parseFloat(amountStr);
+ return isNaN(num) ? null : num.toString();
+ } catch (error) {
+ debugError('금액 파싱 오류', { amountStr, error });
+ return null;
+ }
+ };
+
+ // 매핑
+ const mappedData: ContractData = {
+ projectId: projectId || 1, // TODO: 기본값 설정, 실제로는 유효한 projectId 필요
+ vendorId: vendorId || 1, // TODO: 기본값 설정, 실제로는 유효한 vendorId 필요
+ contractNo: eccHeader.EBELN || '',
+ contractName: eccHeader.ZTITLE || eccHeader.EBELN || '',
+ status: eccHeader.ZPO_CNFM_STAT || 'ACTIVE',
+ startDate: parseDate(eccHeader.ZPO_DT || null),
+ endDate: null, // ZMM_DT에서 가져와야 함
+ deliveryDate: null, // ZMM_DT에서 가져와야 함
+ paymentTerms: eccHeader.ZTERM || null,
+ deliveryTerms: eccHeader.INCO1 || null,
+ deliveryLocation: eccHeader.ZUNLD_PLC_CD || null,
+ currency: eccHeader.ZPO_CURR || 'KRW',
+ totalAmount: parseAmount(eccHeader.ZPO_AMT || null),
+ netTotal: parseAmount(eccHeader.ZPO_AMT || null),
+ remarks: eccHeader.ETC_2 || null,
+ // 기본값들
+ discount: null,
+ tax: null,
+ shippingFee: null,
+ partialShippingAllowed: false,
+ partialPaymentAllowed: false,
+ version: 1,
+ };
+
+ debugSuccess('ECC PO 헤더 매핑 완료', { ebeln: eccHeader.EBELN });
+ return mappedData;
+}
+
+/**
+ * ECC PO 상세 데이터를 contract_items 테이블로 매핑
+ */
+export async function mapECCPODetailToBusiness(
+ eccDetail: ECCPODetail,
+ contractId: number
+): Promise<ContractItemData> {
+ debugLog('ECC PO 상세 매핑 시작', {
+ ebeln: eccDetail.EBELN,
+ ebelp: eccDetail.EBELP,
+ matnr: eccDetail.MATNR,
+ });
+
+ // itemId 찾기 또는 생성 (MATNR 기반으로 items 테이블에서 조회/삽입)
+ let itemId: number | null = null;
+ if (eccDetail.MATNR) {
+ try {
+ // 1. 먼저 기존 아이템 조회
+ const item = await db.query.items.findFirst({
+ where: eq(items.itemCode, eccDetail.MATNR),
+ });
+
+ if (item) {
+ itemId = item.id;
+ debugLog('기존 아이템 ID 찾음', {
+ matnr: eccDetail.MATNR,
+ itemId,
+ packageCode: item.packageCode
+ });
+ } else {
+ // 2. 아이템이 없으면 새로 생성
+ debugLog('아이템이 없어서 새로 생성', { matnr: eccDetail.MATNR });
+
+ // 프로젝트 정보 설정
+ const projectNo = eccDetail.PSPID || 'DEFAULT';
+ const packageCode = 'AUTO_GENERATED'; // 기본값으로 설정
+
+ const newItemData = {
+ ProjectNo: projectNo,
+ itemCode: eccDetail.MATNR,
+ itemName: eccDetail.MAKTX || eccDetail.MATNR || 'Unknown Item',
+ packageCode: packageCode,
+ smCode: null, // SM 코드는 ECC 데이터에서 제공되지 않음
+ description: eccDetail.MAKTX || null,
+ parentItemCode: null,
+ itemLevel: null,
+ deleteFlag: 'N',
+ unitOfMeasure: null,
+ steelType: null,
+ gradeMaterial: null,
+ changeDate: null,
+ baseUnitOfMeasure: null,
+ };
+
+ const [insertedItem] = await db.insert(items).values(newItemData).returning({ id: items.id });
+ itemId = insertedItem.id;
+
+ debugSuccess('새 아이템 생성 완료', {
+ matnr: eccDetail.MATNR,
+ itemId,
+ projectNo,
+ itemName: newItemData.itemName
+ });
+ }
+ } catch (error) {
+ debugError('아이템 조회/생성 중 오류', { matnr: eccDetail.MATNR, error });
+ }
+ }
+
+ // 수량 파싱
+ const parseQuantity = (qtyStr: string | null): number => {
+ if (!qtyStr) return 1;
+ try {
+ const num = parseInt(qtyStr);
+ return isNaN(num) ? 1 : num;
+ } catch (error) {
+ debugError('수량 파싱 오류', { qtyStr, error });
+ return 1;
+ }
+ };
+
+ // 금액 파싱
+ const parseAmount = (amountStr: string | null): string | null => {
+ if (!amountStr) return null;
+ try {
+ const num = parseFloat(amountStr);
+ return isNaN(num) ? null : num.toString();
+ } catch (error) {
+ debugError('금액 파싱 오류', { amountStr, error });
+ return null;
+ }
+ };
+
+ // 세율 계산 (MWSKZ 기반)
+ const calculateTaxRate = (mwskz: string | null): string | null => {
+ if (!mwskz) return null;
+ // 일반적인 한국 세율 매핑 (실제 비즈니스 로직에 따라 조정 필요)
+ switch (mwskz) {
+ case '10':
+ return '10.00';
+ case '00':
+ return '0.00';
+ default:
+ return '10.00'; // 기본값
+ }
+ };
+
+ const quantity = parseQuantity(eccDetail.MENGE || null);
+ const unitPrice = parseAmount(eccDetail.NETPR || null);
+ const taxRate = calculateTaxRate(eccDetail.MWSKZ || null);
+ const totalLineAmount = parseAmount(eccDetail.NETWR || null);
+
+ // 세액 계산
+ let taxAmount: string | null = null;
+ if (unitPrice && taxRate) {
+ try {
+ const unitPriceNum = parseFloat(unitPrice);
+ const taxRateNum = parseFloat(taxRate);
+ const calculatedTaxAmount = (unitPriceNum * quantity * taxRateNum) / 100;
+ taxAmount = calculatedTaxAmount.toString();
+ } catch (error) {
+ debugError('세액 계산 오류', { unitPrice, taxRate, quantity, error });
+ }
+ }
+
+ // 매핑
+ const mappedData: ContractItemData = {
+ contractId,
+ itemId: itemId || 1, // TODO: 기본값 설정, 실제로는 유효한 itemId 필요
+ description: eccDetail.MAKTX || null,
+ quantity,
+ unitPrice,
+ taxRate,
+ taxAmount,
+ totalLineAmount,
+ remark: eccDetail.ZPO_RMK || null,
+ };
+
+ debugSuccess('ECC PO 상세 매핑 완료', {
+ ebeln: eccDetail.EBELN,
+ ebelp: eccDetail.EBELP,
+ itemId,
+ });
+ return mappedData;
+}
+
+/**
+ * ECC PO 데이터를 비즈니스 테이블로 일괄 매핑 및 저장
+ */
+export async function mapAndSaveECCPOData(
+ processedPOs: ProcessedPOData[]
+): Promise<{ success: boolean; message: string; processedCount: number }> {
+ debugLog('ECC PO 데이터 일괄 매핑 및 저장 시작', {
+ poCount: processedPOs.length,
+ });
+
+ try {
+ const result = await db.transaction(async (tx) => {
+ let processedCount = 0;
+
+ for (const poData of processedPOs) {
+ const { header, details } = poData;
+
+ try {
+ // 1. 헤더 매핑 및 저장
+ const contractData = await mapECCPOHeaderToBusiness(header);
+
+ // 중복 체크 (contractNo 기준)
+ const existingContract = await tx.query.contracts.findFirst({
+ where: eq(contracts.contractNo, contractData.contractNo),
+ });
+
+ let contractId: number;
+ if (existingContract) {
+ // 기존 계약 업데이트
+ await tx
+ .update(contracts)
+ .set({
+ ...contractData,
+ updatedAt: new Date(),
+ })
+ .where(eq(contracts.id, existingContract.id));
+ contractId = existingContract.id;
+ debugLog('기존 계약 업데이트', { contractNo: contractData.contractNo, contractId });
+ } else {
+ // 새 계약 생성
+ const [insertedContract] = await tx
+ .insert(contracts)
+ .values(contractData)
+ .returning({ id: contracts.id });
+ contractId = insertedContract.id;
+ debugLog('새 계약 생성', { contractNo: contractData.contractNo, contractId });
+ }
+
+ // 2. 기존 contract_items 삭제 (교체 방식)
+ await tx
+ .delete(contractItems)
+ .where(eq(contractItems.contractId, contractId));
+
+ // 3. 상세 매핑 및 저장
+ if (details.length > 0) {
+ const contractItemsData: ContractItemData[] = [];
+
+ for (const detail of details) {
+ const itemData = await mapECCPODetailToBusiness(detail, contractId);
+ contractItemsData.push(itemData);
+ }
+
+ // 일괄 삽입
+ await tx.insert(contractItems).values(contractItemsData);
+ debugLog('계약 아이템 저장 완료', {
+ contractId,
+ itemCount: contractItemsData.length
+ });
+ }
+
+ processedCount++;
+ } catch (error) {
+ debugError('PO 데이터 처리 중 오류', {
+ ebeln: header.EBELN,
+ error
+ });
+ // 개별 PO 처리 실패 시 해당 PO만 스킵하고 계속 진행
+ continue;
+ }
+ }
+
+ return { processedCount };
+ });
+
+ debugSuccess('ECC PO 데이터 일괄 처리 완료', {
+ processedCount: result.processedCount,
+ });
+
+ return {
+ success: true,
+ message: `${result.processedCount}개의 PO 데이터가 성공적으로 처리되었습니다.`,
+ processedCount: result.processedCount,
+ };
+ } catch (error) {
+ debugError('ECC PO 데이터 처리 중 오류 발생', error);
+ return {
+ success: false,
+ message:
+ error instanceof Error
+ ? error.message
+ : '알 수 없는 오류가 발생했습니다.',
+ processedCount: 0,
+ };
+ }
+}
+
+/**
+ * ECC PO 데이터 유효성 검증
+ */
+export function validateECCPOData(
+ processedPOs: ProcessedPOData[]
+): { isValid: boolean; errors: string[] } {
+ const errors: string[] = [];
+
+ for (const poData of processedPOs) {
+ const { header, details } = poData;
+
+ // 헤더 데이터 검증
+ if (!header.EBELN) {
+ errors.push(`필수 필드 누락: EBELN (구매오더번호)`);
+ }
+ if (!header.LIFNR) {
+ errors.push(`필수 필드 누락: LIFNR (VENDOR코드) - EBELN: ${header.EBELN}`);
+ }
+
+ // 상세 데이터 검증
+ for (const detail of details) {
+ if (!detail.EBELN) {
+ errors.push(`필수 필드 누락: EBELN (구매오더번호) - EBELP: ${detail.EBELP}`);
+ }
+ if (!detail.EBELP) {
+ errors.push(`필수 필드 누락: EBELP (구매오더품목번호) - EBELN: ${detail.EBELN}`);
+ }
+ if (!detail.MATNR) {
+ errors.push(`필수 필드 누락: MATNR (자재코드) - EBELN: ${detail.EBELN}, EBELP: ${detail.EBELP}`);
+ }
+ }
+
+ // 헤더와 상세 간의 관계 검증
+ for (const detail of details) {
+ if (detail.EBELN !== header.EBELN) {
+ errors.push(`헤더와 상세의 EBELN이 일치하지 않음: Header ${header.EBELN}, Detail ${detail.EBELN}`);
+ }
+ }
+ }
+
+ return {
+ isValid: errors.length === 0,
+ errors,
+ };
+}
diff --git a/lib/soap/ecc/mapper/rfq-and-pr-mapper.ts b/lib/soap/ecc/mapper/rfq-and-pr-mapper.ts
new file mode 100644
index 00000000..e2258a4c
--- /dev/null
+++ b/lib/soap/ecc/mapper/rfq-and-pr-mapper.ts
@@ -0,0 +1,429 @@
+import { debugLog, debugSuccess, debugError } from '@/lib/debug-utils';
+import db from '@/db/db';
+import {
+ procurementRfqs,
+ prItems,
+ procurementRfqDetails,
+} from '@/db/schema/procurementRFQ';
+import {
+ PR_INFORMATION_T_BID_HEADER,
+ PR_INFORMATION_T_BID_ITEM,
+} from '@/db/schema/ECC/ecc';
+import { users } from '@/db/schema';
+import { employee } from '@/db/schema/knox/employee';
+import { eq, inArray } from 'drizzle-orm';
+
+// NON-SAP 데이터 처리
+import { oracleKnex } from '@/lib/oracle-db/db';
+import { findUserIdByEmployeeNumber } from '../../../users/knox-service';
+
+// ECC 데이터 타입 정의
+export type ECCBidHeader = typeof PR_INFORMATION_T_BID_HEADER.$inferInsert;
+export type ECCBidItem = typeof PR_INFORMATION_T_BID_ITEM.$inferInsert;
+
+// 비즈니스 테이블 데이터 타입 정의
+export type ProcurementRfqData = typeof procurementRfqs.$inferInsert;
+export type PrItemData = typeof prItems.$inferInsert;
+export type ProcurementRfqDetailData =
+ typeof procurementRfqDetails.$inferInsert;
+
+/**
+ * 시리즈 판단: 관련 PR 아이템들의 PSPID를 기반으로 결정
+ * - 아이템이 1개 이하: null
+ * - 아이템이 2개 이상이고 PSPID가 모두 동일: "SS"
+ * - 아이템이 2개 이상이고 PSPID가 서로 다름: "||"
+ */
+function computeSeriesFromItems(items: ECCBidItem[]): string | null {
+ if (items.length <= 1) return null;
+
+ const normalize = (v: unknown): string | null => {
+ if (typeof v !== 'string') return null;
+ const trimmed = v.trim();
+ return trimmed.length > 0 ? trimmed : null;
+ };
+
+ const uniquePspids = new Set<string | null>(
+ items.map((it) => normalize(it.PSPID as string | null | undefined))
+ );
+
+ return uniquePspids.size === 1 ? 'SS' : '||';
+}
+
+/**
+ * PERNR(사번)을 기준으로 사용자 ID를 찾는 함수
+ */
+async function findUserIdByPernr(pernr: string): Promise<number | null> {
+ try {
+ debugLog('PERNR로 사용자 ID 찾기 시작', { pernr });
+
+ // 현재 users 테이블에 사번을 따로 저장하지 않으므로 knox 기준으로 사번 --> epId --> user.id 순으로 찾기
+ // 1. employee 테이블에서 employeeNumber로 epId 찾기
+ const employeeResult = await db
+ .select({ epId: employee.epId })
+ .from(employee)
+ .where(eq(employee.employeeNumber, pernr))
+ .limit(1);
+
+ if (employeeResult.length === 0) {
+ debugError('사번에 해당하는 직원 정보를 찾을 수 없음', { pernr });
+ return null;
+ }
+
+ const epId = employeeResult[0].epId;
+ debugLog('직원 epId 찾음', { pernr, epId });
+
+ // 2. users 테이블에서 epId로 사용자 ID 찾기
+ const userResult = await db
+ .select({ id: users.id })
+ .from(users)
+ .where(eq(users.epId, epId))
+ .limit(1);
+
+ if (userResult.length === 0) {
+ debugError('epId에 해당하는 사용자 정보를 찾을 수 없음', { epId });
+ return null;
+ }
+
+ const userId = userResult[0].id;
+ debugSuccess('사용자 ID 찾음', { pernr, epId, userId });
+ return userId;
+ } catch (error) {
+ debugError('사용자 ID 찾기 중 오류 발생', { pernr, error });
+ return null;
+ }
+}
+
+
+
+/**
+ * 담당자 찾는 함수 (Non-SAP 데이터를 기준으로 찾음)
+ * Puchasing Group을 CMCTB_CD 에서 CD_CLF='MMA070' 조건으로, CD=EKGRP 조건으로 찾으면, USR_DF_CHAR_9 컬럼이 담당자 사번임. 기준으로 유저를 넣어줄 예정임
+ */
+async function findInChargeUserIdByEKGRP(EKGRP: string | null): Promise<number | null> {
+ try {
+ debugLog('담당자 찾기 시작', { EKGRP });
+ // NonSAP에서 담당자 사번 찾기
+
+ const result = await oracleKnex
+ .select('USR_DF_CHAR_9')
+ .from('CMCTB_CD')
+ .where('CD_CLF', 'MMA070')
+ .andWhere('CD', EKGRP)
+ .limit(1);
+
+ if (result.length === 0) {
+ debugError('담당자 찾기 중 오류 발생', { EKGRP });
+ return null;
+ }
+
+ const employeeNumber = result[0].USR_DF_CHAR_9;
+
+ // 임시 : Knox API 기준으로 사번에 해당하는 userId 찾기 (nonsap에서 제공하는 유저테이블로 변경 예정)
+ const userId = await findUserIdByEmployeeNumber(employeeNumber);
+
+ debugSuccess('담당자 찾음', { EKGRP, userId });
+ return userId;
+ } catch (error) {
+ debugError('담당자 찾기 중 오류 발생', { EKGRP, error });
+ return null;
+ }
+}
+
+// *****************************mapping functions*********************************
+
+/**
+ * ECC RFQ 헤더 데이터를 비즈니스 테이블로 매핑
+ */
+export async function mapECCRfqHeaderToBusiness(
+ eccHeader: ECCBidHeader
+): Promise<ProcurementRfqData> {
+ debugLog('ECC RFQ 헤더 매핑 시작', { anfnr: eccHeader.ANFNR });
+
+ // 날짜 파싱 (실패시 현재 Date 들어감)
+ let interfacedAt: Date = new Date();
+
+ if (eccHeader.ZRFQ_TRS_DT != null && eccHeader.ZRFQ_TRS_TM != null) {
+ try {
+ // SAP 날짜 형식 (YYYYMMDD) 파싱
+ const dateStr = eccHeader.ZRFQ_TRS_DT;
+ if (dateStr.length === 8) {
+ const year = parseInt(dateStr.substring(0, 4));
+ const month = parseInt(dateStr.substring(4, 6)) - 1; // 0-based
+ const day = parseInt(dateStr.substring(6, 8));
+ const hour = parseInt(eccHeader.ZRFQ_TRS_TM.substring(0, 2));
+ const minute = parseInt(eccHeader.ZRFQ_TRS_TM.substring(2, 4));
+ const second = parseInt(eccHeader.ZRFQ_TRS_TM.substring(4, 6));
+ interfacedAt = new Date(year, month, day, hour, minute, second);
+ }
+ } catch (error) {
+ debugError('날짜 파싱 오류', {
+ date: eccHeader.ZRFQ_TRS_DT,
+ time: eccHeader.ZRFQ_TRS_TM,
+ error,
+ });
+ }
+ }
+
+ // 담당자 찾기
+ const inChargeUserId = await findInChargeUserIdByEKGRP(eccHeader.EKGRP || null);
+
+ // 시리즈는 3가지의 케이스만 온다고 가정한다. (다른 케이스는 잘못된 것)
+ // 케이스 1. 한 RFQ에서 PR Item이 여러 개고, PSPID 값이 모두 같은 경우 => series 값은 "SS"
+ // 케이스 2. 한 RFQ에서 PR Item이 여러 개고, PSPID 값이 여러개인 경우 => series 값은 "||""
+ // 케이스 3. 한 RFQ에서 PR Item이 하나인 경우 => seires 값은 null
+ // 만약 위 케이스에 모두 속하지 않는 경우 케이스 3처럼 null 값을 넣는다.
+
+ // 매핑
+ const mappedData: ProcurementRfqData = {
+ rfqCode: eccHeader.ANFNR, // rfqCode=rfqNumber(ANFNR), 혼선이 있을 수 있으나 ECC에는 rfqCode라는 게 별도로 없음. rfqCode=rfqNumber
+ projectId: null, // PR 아이템 처리 후 업데이트. (로직은 추후 작성)
+ series: null, // PR 아이템 처리 후 업데이트. (로직은 추후 작성)
+ // itemGroup: null, // 대표 자재그룹: PR 아이템 처리 후 업데이트. (로직은 추후 작성)
+ itemCode: null, // 대표 자재코드: PR 아이템 처리 후 업데이트. (로직은 추후 작성)
+ itemName: null, // 대표 자재명: PR 아이템 처리 후 업데이트. (로직은 추후 작성)
+ dueDate: null, // eVCP에서 사용하는 컬럼이므로 불필요.
+ rfqSendDate: null, // eVCP에서 사용하는 컬럼이므로 불필요.
+ createdAt: interfacedAt,
+ status: 'RFQ Created', // eVCP에서 사용하는 컬럼, 기본값 RFQ Created 처리
+ rfqSealedYn: false, // eVCP에서 사용하는 컬럼, 기본값 false 처리
+ picCode: eccHeader.EKGRP || null, // Purchasing Group을 PIC로 사용 (구매측 임직원과 연계된 코드임)
+ remark: null, // remark 컬럼은 담당자 메모용으로 넣어줄 필요 없음.
+ sentBy: null, // 보내기 전의 RFQ를 대상으로 하므로 넣어줄 필요 없음.
+ createdBy: inChargeUserId || 1,
+ updatedBy: inChargeUserId || 1,
+ };
+
+ debugSuccess('ECC RFQ 헤더 매핑 완료', { anfnr: eccHeader.ANFNR });
+ return mappedData;
+}
+
+/**
+ * ECC RFQ 아이템 데이터를 비즈니스 테이블로 매핑
+ */
+export function mapECCRfqItemToBusiness(
+ eccItem: ECCBidItem,
+ rfqId: number
+): PrItemData {
+ debugLog('ECC RFQ 아이템 매핑 시작', {
+ anfnr: eccItem.ANFNR,
+ anfps: eccItem.ANFPS,
+ });
+
+ // 날짜 파싱
+ let deliveryDate: Date | null = null;
+ if (eccItem.LFDAT) {
+ try {
+ const dateStr = eccItem.LFDAT;
+ if (dateStr.length === 8) {
+ const year = parseInt(dateStr.substring(0, 4));
+ const month = parseInt(dateStr.substring(4, 6)) - 1;
+ const day = parseInt(dateStr.substring(6, 8));
+ deliveryDate = new Date(year, month, day);
+ }
+ } catch (error) {
+ debugError('아이템 날짜 파싱 오류', { date: eccItem.LFDAT, error });
+ }
+ }
+
+ // TODO: 시리즈인 경우 EBELP(Series PO Item Seq) 를 참조하는 로직 필요? 이 컬럼의 의미 확인 필요
+
+
+ const mappedData: PrItemData = {
+ procurementRfqsId: rfqId, // PR Item의 부모 RFQ ID [ok]
+ rfqItem: eccItem.ANFPS || null, // itemNo [ok]
+ prItem: eccItem.BANPO || null, // ECC PR No [ok]
+ prNo: eccItem.BANFN || null, // ECC PR No [ok]
+ materialCode: eccItem.MATNR || null, // ECC Material Number [ok]
+ materialCategory: eccItem.MATKL || null, // ECC Material Group [ok]
+ acc: eccItem.SAKTO || null, // ECC G/L Account Number [ok]
+ materialDescription: eccItem.TXZ01 || null, // ECC Short Text [ok] // TODO: 자재 테이블 참조해서 자재명 넣어주기 ?
+ size: null, // ECC에서 해당 정보 없음 // TODO: 이시원 프로에게 확인
+ deliveryDate, // ECC PR Delivery Date (parsed)
+ quantity: eccItem.MENGE ? Number(eccItem.MENGE) : null, // ECC PR Quantity [ok]
+ uom: eccItem.MEINS || null, // ECC PR UOM [ok]
+ grossWeight: eccItem.BRGEW ? Number(eccItem.BRGEW) : null, // ECC PR Gross Weight [ok]
+ gwUom: eccItem.GEWEI || null, // ECC PR Gross Weight UOM [ok]
+ specNo: null, // ECC에서 해당 정보 없음, TODO: 이시원 프로 - material 참조해서 넣어주는건지, PR 마다 고유한건지 확인
+ specUrl: null, // ECC에서 해당 정보 없음, TODO: 이시원 프로에게 material 참조해서 넣어주는건지, PR 마다 고유한건지 확인
+ trackingNo: null, // TODO: 이시원 프로에게 확인 필요. I/F 정의서 어느 항목인지 추정 불가
+ majorYn: false, // 기본값 false 할당, 필요시 eVCP에서 수정
+ projectDef: eccItem.PSPID || null, // Project Key 로 처리. // TODO: 프로젝트 테이블 참조해 코드로 처리하기
+ projectSc: null, // ECC에서 해당 정보 없음 // TODO: pspid 기준으로 찾아 넣어주기
+ projectKl: null, // ECC에서 해당 정보 없음 // TODO: pspid 기준으로 찾아 넣어주기
+ projectLc: null, // ECC에서 해당 정보 없음 // TODO: pspid 기준으로 찾아 넣어주기
+ projectDl: null, // ECC에서 해당 정보 없음 // TODO: pspid 기준으로 찾아 넣어주기
+ remark: null, // remark 컬럼은 담당자 메모용으로 넣어줄 필요 없음.
+ };
+
+ debugSuccess('ECC RFQ 아이템 매핑 완료', {
+ rfqItem: eccItem.ANFPS,
+ materialCode: eccItem.MATNR,
+ });
+ return mappedData;
+}
+
+/**
+ * ECC 데이터를 비즈니스 테이블로 일괄 매핑 및 저장
+ */
+export async function mapAndSaveECCRfqData(
+ eccHeaders: ECCBidHeader[],
+ eccItems: ECCBidItem[]
+): Promise<{ success: boolean; message: string; processedCount: number }> {
+ debugLog('ECC 데이터 일괄 매핑 및 저장 시작', {
+ headerCount: eccHeaders.length,
+ itemCount: eccItems.length,
+ });
+
+ try {
+ const result = await db.transaction(async (tx) => {
+ // 1) 헤더별 관련 아이템 그룹핑 + 시리즈 계산 + 헤더 매핑을 병렬로 수행
+ const rfqGroups = await Promise.all(
+ eccHeaders.map(async (eccHeader) => {
+ const relatedItems = eccItems.filter((item) => item.ANFNR === eccHeader.ANFNR);
+ const series = computeSeriesFromItems(relatedItems);
+ const rfqData = await mapECCRfqHeaderToBusiness(eccHeader);
+ rfqData.series = series;
+ return { rfqCode: rfqData.rfqCode, rfqData, relatedItems };
+ })
+ );
+
+ const rfqRecords = rfqGroups.map((g) => g.rfqData);
+
+ // 2) RFQ 다건 삽입 (중복은 무시). 반환된 레코드로 일부 ID 매핑
+ const inserted = await tx
+ .insert(procurementRfqs)
+ .values(rfqRecords)
+ .onConflictDoNothing()
+ .returning({ id: procurementRfqs.id, rfqCode: procurementRfqs.rfqCode });
+
+ const rfqCodeToId = new Map<string, number>();
+ for (const row of inserted) {
+ if (row.rfqCode) {
+ rfqCodeToId.set(row.rfqCode, row.id);
+ }
+ }
+
+ // 3) 반환되지 않은 기존 RFQ 들의 ID 조회하여 매핑 보완
+ const allCodes = rfqRecords
+ .map((r) => r.rfqCode)
+ .filter((c): c is string => typeof c === 'string' && c.length > 0);
+ const missingCodes = allCodes.filter((c) => !rfqCodeToId.has(c));
+ if (missingCodes.length > 0) {
+ const existing = await tx
+ .select({ id: procurementRfqs.id, rfqCode: procurementRfqs.rfqCode })
+ .from(procurementRfqs)
+ .where(inArray(procurementRfqs.rfqCode, missingCodes));
+ for (const row of existing) {
+ if (row.rfqCode) {
+ rfqCodeToId.set(row.rfqCode, row.id);
+ }
+ }
+ }
+
+ // 4) 모든 아이템을 한 번에 생성할 데이터로 변환
+ const allItemsToInsert: PrItemData[] = [];
+ for (const group of rfqGroups) {
+ const rfqCode = group.rfqCode;
+ if (!rfqCode) continue;
+ const rfqId = rfqCodeToId.get(rfqCode);
+ if (!rfqId) {
+ debugError('RFQ ID 매핑 누락', { rfqCode });
+ throw new Error(`RFQ ID를 찾을 수 없습니다: ${rfqCode}`);
+ }
+
+ for (const eccItem of group.relatedItems) {
+ const itemData = mapECCRfqItemToBusiness(eccItem, rfqId);
+ allItemsToInsert.push(itemData);
+ }
+ }
+
+ // 5) 아이템 일괄 삽입 (chunk 처리로 파라미터 제한 회피)
+ const ITEM_CHUNK_SIZE = 1000;
+ for (let i = 0; i < allItemsToInsert.length; i += ITEM_CHUNK_SIZE) {
+ const chunk = allItemsToInsert.slice(i, i + ITEM_CHUNK_SIZE);
+ await tx.insert(prItems).values(chunk);
+ }
+
+ return { processedCount: rfqRecords.length };
+ });
+
+ debugSuccess('ECC 데이터 일괄 처리 완료', {
+ processedCount: result.processedCount,
+ });
+
+ return {
+ success: true,
+ message: `${result.processedCount}개의 RFQ 데이터가 성공적으로 처리되었습니다.`,
+ processedCount: result.processedCount,
+ };
+ } catch (error) {
+ debugError('ECC 데이터 처리 중 오류 발생', error);
+ return {
+ success: false,
+ message:
+ error instanceof Error
+ ? error.message
+ : '알 수 없는 오류가 발생했습니다.',
+ processedCount: 0,
+ };
+ }
+}
+
+/**
+ * ECC 데이터 유효성 검증
+ */
+export function validateECCRfqData(
+ eccHeaders: ECCBidHeader[],
+ eccItems: ECCBidItem[]
+): { isValid: boolean; errors: string[] } {
+ const errors: string[] = [];
+
+ // 헤더 데이터 검증
+ for (const header of eccHeaders) {
+ if (!header.ANFNR) {
+ errors.push(`필수 필드 누락: ANFNR (Bidding/RFQ Number)`);
+ }
+ if (!header.ZBSART) {
+ errors.push(
+ `필수 필드 누락: ZBSART (Bidding Type) - ANFNR: ${header.ANFNR}`
+ );
+ }
+ }
+
+ // 아이템 데이터 검증
+ for (const item of eccItems) {
+ if (!item.ANFNR) {
+ errors.push(
+ `필수 필드 누락: ANFNR (Bidding/RFQ Number) - Item: ${item.ANFPS}`
+ );
+ }
+ if (!item.ANFPS) {
+ errors.push(`필수 필드 누락: ANFPS (Item Number) - ANFNR: ${item.ANFNR}`);
+ }
+ if (!item.BANFN) {
+ errors.push(
+ `필수 필드 누락: BANFN (Purchase Requisition Number) - ANFNR: ${item.ANFNR}, ANFPS: ${item.ANFPS}`
+ );
+ }
+ if (!item.BANPO) {
+ errors.push(
+ `필수 필드 누락: BANPO (Item Number of Purchase Requisition) - ANFNR: ${item.ANFNR}, ANFPS: ${item.ANFPS}`
+ );
+ }
+ }
+
+ // 헤더와 아이템 간의 관계 검증
+ const headerAnfnrs = new Set(eccHeaders.map((h) => h.ANFNR));
+ const itemAnfnrs = new Set(eccItems.map((i) => i.ANFNR));
+
+ for (const anfnr of itemAnfnrs) {
+ if (!headerAnfnrs.has(anfnr)) {
+ errors.push(`아이템의 ANFNR이 헤더에 존재하지 않음: ${anfnr}`);
+ }
+ }
+
+ return {
+ isValid: errors.length === 0,
+ errors,
+ };
+}