summaryrefslogtreecommitdiff
path: root/lib/general-contracts/service.ts
diff options
context:
space:
mode:
authordujinkim <dujin.kim@dtsolution.co.kr>2025-09-19 07:51:27 +0000
committerdujinkim <dujin.kim@dtsolution.co.kr>2025-09-19 07:51:27 +0000
commit9ecdfb23fe3df6a5df86782385002c562dfc1198 (patch)
tree4188cb7e6bf2c862d9c86a59d79946bd41217227 /lib/general-contracts/service.ts
parentb67861fbb424c7ad47ad1538f75e2945bd8890c5 (diff)
(대표님) rfq 히스토리, swp 등
Diffstat (limited to 'lib/general-contracts/service.ts')
-rw-r--r--lib/general-contracts/service.ts88
1 files changed, 57 insertions, 31 deletions
diff --git a/lib/general-contracts/service.ts b/lib/general-contracts/service.ts
index 8c74c616..52301dae 100644
--- a/lib/general-contracts/service.ts
+++ b/lib/general-contracts/service.ts
@@ -372,7 +372,8 @@ export async function createContract(data: Record<string, unknown>) {
try {
// 계약번호 자동 생성
// TODO: 구매 발주담당자 코드 필요 - 파라미터 추가
- const userId = data.registeredById as string
+ const rawUserId = data.registeredById
+ const userId = (rawUserId && !isNaN(Number(rawUserId))) ? String(rawUserId) : undefined
const contractNumber = await generateContractNumber(
userId,
data.type as string
@@ -676,6 +677,8 @@ export async function updateContractItems(contractId: number, items: Record<stri
specification: item.specification as string,
quantity: item.quantity as number,
quantityUnit: item.quantityUnit as string,
+ totalWeight: item.totalWeight as number,
+ weightUnit: item.weightUnit as string,
contractDeliveryDate: item.contractDeliveryDate as string,
contractUnitPrice: item.contractUnitPrice as number,
contractAmount: item.contractAmount as number,
@@ -1554,8 +1557,8 @@ async function mapContractSummaryToDb(contractSummary: any) {
// 계약번호 생성
const contractNumber = await generateContractNumber(
- basicInfo.contractType || basicInfo.type || 'UP',
- basicInfo.userId
+ basicInfo.userId,
+ basicInfo.contractType || basicInfo.type || 'UP'
)
return {
@@ -1584,38 +1587,38 @@ async function mapContractSummaryToDb(contractSummary: any) {
currency: basicInfo.currency || basicInfo.contractCurrency || 'USD',
totalAmount: Number(basicInfo.totalAmount || basicInfo.contractAmount || 0),
- // SAP ECC 관련 필드들
- poVersion: basicInfo.revision || 1,
- purchaseDocType: basicInfo.type || 'UP',
- purchaseOrg: basicInfo.purchaseOrg || '',
- purchaseGroup: basicInfo.purchaseGroup || '',
- exchangeRate: Number(basicInfo.exchangeRate || 1),
+ // // SAP ECC 관련 필드들
+ // poVersion: basicInfo.revision || 1,
+ // purchaseDocType: basicInfo.type || 'UP',
+ // purchaseOrg: basicInfo.purchaseOrg || '',
+ // purchaseGroup: basicInfo.purchaseGroup || '',
+ // exchangeRate: Number(basicInfo.exchangeRate || 1),
- // 계약/보증 관련
- contractGuaranteeCode: basicInfo.contractGuaranteeCode || '',
- defectGuaranteeCode: basicInfo.defectGuaranteeCode || '',
- guaranteePeriodCode: basicInfo.guaranteePeriodCode || '',
- advancePaymentYn: basicInfo.advancePaymentYn || 'N',
+ // // 계약/보증 관련
+ // contractGuaranteeCode: basicInfo.contractGuaranteeCode || '',
+ // defectGuaranteeCode: basicInfo.defectGuaranteeCode || '',
+ // guaranteePeriodCode: basicInfo.guaranteePeriodCode || '',
+ // advancePaymentYn: basicInfo.advancePaymentYn || 'N',
- // 전자계약/승인 관련
- electronicContractYn: basicInfo.electronicContractYn || 'Y',
- electronicApprovalDate: basicInfo.electronicApprovalDate || null,
- electronicApprovalTime: basicInfo.electronicApprovalTime || '',
- ownerApprovalYn: basicInfo.ownerApprovalYn || 'N',
+ // // 전자계약/승인 관련
+ // electronicContractYn: basicInfo.electronicContractYn || 'Y',
+ // electronicApprovalDate: basicInfo.electronicApprovalDate || null,
+ // electronicApprovalTime: basicInfo.electronicApprovalTime || '',
+ // ownerApprovalYn: basicInfo.ownerApprovalYn || 'N',
- // 기타
- plannedInOutFlag: basicInfo.plannedInOutFlag || 'I',
- settlementStandard: basicInfo.settlementStandard || 'A',
- weightSettlementFlag: basicInfo.weightSettlementFlag || 'N',
+ // // 기타
+ // plannedInOutFlag: basicInfo.plannedInOutFlag || 'I',
+ // settlementStandard: basicInfo.settlementStandard || 'A',
+ // weightSettlementFlag: basicInfo.weightSettlementFlag || 'N',
// 연동제 관련
priceIndexYn: basicInfo.priceIndexYn || 'N',
writtenContractNo: basicInfo.contractNumber || '',
contractVersion: basicInfo.revision || 1,
- // 부분 납품/결제
- partialShippingAllowed: basicInfo.partialShippingAllowed || false,
- partialPaymentAllowed: basicInfo.partialPaymentAllowed || false,
+ // // 부분 납품/결제
+ // partialShippingAllowed: basicInfo.partialShippingAllowed || false,
+ // partialPaymentAllowed: basicInfo.partialPaymentAllowed || false,
// 메모
remarks: basicInfo.notes || basicInfo.remarks || '',
@@ -1748,7 +1751,7 @@ export async function generateContractNumber(
const user = await db
.select({ userCode: users.userCode })
.from(users)
- .where(eq(users.id, userId))
+ .where(eq(users.id, parseInt(userId || '0')))
.limit(1);
if (user[0]?.userCode && user[0].userCode.length >= 3) {
purchaseManagerCode = user[0].userCode.substring(0, 3).toUpperCase();
@@ -1774,8 +1777,20 @@ export async function generateContractNumber(
let sequenceNumber = 1
if (existingContracts.length > 0) {
const lastContractNumber = existingContracts[0].contractNumber
- const lastSequence = parseInt(lastContractNumber.slice(-3))
- sequenceNumber = lastSequence + 1
+ const lastSequenceStr = lastContractNumber.slice(-3)
+
+ // contractNumber에서 숫자만 추출하여 sequence 찾기
+ const numericParts = lastContractNumber.match(/\d+/g)
+ if (numericParts && numericParts.length > 0) {
+ // 마지막 숫자 부분을 시퀀스로 사용 (일반적으로 마지막 3자리)
+ const potentialSequence = numericParts[numericParts.length - 1]
+ const lastSequence = parseInt(potentialSequence)
+
+ if (!isNaN(lastSequence)) {
+ sequenceNumber = lastSequence + 1
+ }
+ }
+ // 숫자를 찾지 못했거나 파싱 실패 시 sequenceNumber = 1 유지
}
// 일련번호를 3자리로 포맷팅
@@ -1797,8 +1812,19 @@ export async function generateContractNumber(
let sequenceNumber = 1
if (existingContracts.length > 0) {
const lastContractNumber = existingContracts[0].contractNumber
- const lastSequence = parseInt(lastContractNumber.slice(-3))
- sequenceNumber = lastSequence + 1
+
+ // contractNumber에서 숫자만 추출하여 sequence 찾기
+ const numericParts = lastContractNumber.match(/\d+/g)
+ if (numericParts && numericParts.length > 0) {
+ // 마지막 숫자 부분을 시퀀스로 사용
+ const potentialSequence = numericParts[numericParts.length - 1]
+ const lastSequence = parseInt(potentialSequence)
+
+ if (!isNaN(lastSequence)) {
+ sequenceNumber = lastSequence + 1
+ }
+ }
+ // 숫자를 찾지 못했거나 파싱 실패 시 sequenceNumber = 1 유지
}
// 최종 계약번호 생성: C + 발주담당자코드(3자리) + 계약종류(2자리) + 연도(2자리) + 일련번호(3자리)