diff options
Diffstat (limited to 'lib/general-contracts/service.ts')
| -rw-r--r-- | lib/general-contracts/service.ts | 99 |
1 files changed, 86 insertions, 13 deletions
diff --git a/lib/general-contracts/service.ts b/lib/general-contracts/service.ts index 52301dae..2422706a 100644 --- a/lib/general-contracts/service.ts +++ b/lib/general-contracts/service.ts @@ -11,6 +11,7 @@ import { basicContract, basicContractTemplates } from '@/db/schema/basicContract import { vendors } from '@/db/schema/vendors'
import { users } from '@/db/schema/users'
import { projects } from '@/db/schema/projects'
+import { items } from '@/db/schema/items'
import { filterColumns } from '@/lib/filter-columns'
import { saveDRMFile } from '@/lib/file-stroage'
import { decryptWithServerAction } from '@/components/drm/drmUtils'
@@ -1312,13 +1313,73 @@ export async function sendContractApprovalRequest( const contractId = newContract.id
+ // const items: {
+ // id: number;
+ // createdAt: Date;
+ // updatedAt: Date;
+ // contractId: number;
+ // itemCode: string | null;
+ // quantity: string | null;
+ // contractAmount: string | null;
+ // contractCurrency: string | null;
+ // contractDeliveryDate: string | null;
+ // specification: string | null;
+ // itemInfo: string | null;
+ // quantityUnit: string | null;
+ // totalWeight: string | null;
+ // weightUnit: string | null;
+ // contractUnitPrice: string | null;
+ // }[]
+
// contractItems 테이블에 품목 정보 저장 (general-contract-items가 있을 때만)
if (contractSummary.items && contractSummary.items.length > 0) {
- // 새 품목 추가
+ const projectNo = contractSummary.basicInfo?.projectCode || contractSummary.basicInfo?.projectId?.toString() || 'NULL'
+
for (const item of contractSummary.items) {
+ let itemId: number
+
+ // 1. items 테이블에서 itemCode로 기존 아이템 검색
+ if (item.itemCode) {
+ // const existingItem = await db
+ // .select({ id: items.id })
+ // .from(items)
+ // .where(and(
+ // eq(items.itemCode, item.itemCode),
+ // eq(items.ProjectNo, projectNo)
+ // ))
+ // .limit(1)
+ const existingItem = await db
+ .select({ id: items.id })
+ .from(items)
+ .where(
+ eq(items.itemCode, item.itemCode)
+ )
+ .limit(1)
+
+ if (existingItem.length > 0) {
+ // 기존 아이템이 있으면 해당 ID 사용
+ itemId = existingItem[0].id
+ } else {
+ // 기존 아이템이 없으면 새로 생성
+ const newItem = await db.insert(items).values({
+ ProjectNo: projectNo,
+ itemCode: item.itemCode,
+ itemName: item.itemInfo || item.description || item.itemCode,
+ packageCode: item.itemCode,
+ description: item.specification || item.description || '',
+ unitOfMeasure: item.quantityUnit || 'EA',
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ }).returning({ id: items.id })
+
+ itemId = newItem[0].id
+ }
+
+
+ // 2. contractItems에 저장
await db.insert(contractItems).values({
contractId,
- itemId: item.itemId || 2602, // 기본값 설정
+ itemId: itemId,
description: item.itemInfo || item.description || '',
quantity: Math.floor(Number(item.quantity) || 1), // 정수로 변환
unitPrice: item.contractUnitPrice || item.unitPrice || 0,
@@ -1327,6 +1388,10 @@ export async function sendContractApprovalRequest( totalLineAmount: item.contractAmount || item.totalLineAmount || 0,
remark: item.remark || '',
})
+ }else{
+ //아이템코드가 없으니 pass
+ continue
+ }
}
}
@@ -1409,21 +1474,29 @@ export async function sendContractApprovalRequest( language: "ko",
},
})
+ // 계약 상태 업데이트
+ await db.update(generalContracts)
+ .set({
+ status: 'Contract Accept Request',
+ lastUpdatedAt: new Date()
+ })
+ .where(eq(generalContracts.id, generalContractId))
+
} catch (error) {
console.error('계약승인요청 전송 오류:', error)
-
+
}
- //계약상태변경
- revalidatePath('/evcp/general-contracts')
- revalidatePath('/evcp/general-contracts/detail')
- revalidatePath('/evcp/general-contracts/detail/contract-approval-request-dialog')
- revalidatePath('/evcp/general-contracts/detail/contract-approval-request-dialog')
- return {
- success: true,
- message: '계약승인요청이 성공적으로 전송되었습니다.',
- pdfPath: saveResult.publicPath
- }
+
+ revalidatePath('/evcp/general-contracts')
+ revalidatePath('/evcp/general-contracts/detail')
+ revalidatePath('/evcp/general-contracts/detail/contract-approval-request-dialog')
+
+ return {
+ success: true,
+ message: '계약승인요청이 성공적으로 전송되었습니다.',
+ pdfPath: saveResult.publicPath
+ }
} catch (error: any) {
console.error('계약승인요청 전송 오류:', error)
|
