diff options
Diffstat (limited to 'app/api/partners')
| -rw-r--r-- | app/api/partners/rfq-last/[id]/response/route.ts | 144 |
1 files changed, 135 insertions, 9 deletions
diff --git a/app/api/partners/rfq-last/[id]/response/route.ts b/app/api/partners/rfq-last/[id]/response/route.ts index 5d05db50..06ace9a0 100644 --- a/app/api/partners/rfq-last/[id]/response/route.ts +++ b/app/api/partners/rfq-last/[id]/response/route.ts @@ -7,9 +7,10 @@ import { rfqLastVendorResponses, rfqLastVendorQuotationItems, rfqLastVendorAttachments, - rfqLastVendorResponseHistory + rfqLastVendorResponseHistory, + rfqLastPriceAdjustmentForms } from "@/db/schema" -import { eq, and, inArray } from "drizzle-orm" +import { eq, and } from "drizzle-orm" import { writeFile, mkdir } from "fs/promises" import { createWriteStream } from "fs" import { pipeline } from "stream/promises" @@ -139,17 +140,77 @@ export async function POST( await tx.insert(rfqLastVendorQuotationItems).values(quotationItemsData) } - // 이력 기록 + // 3. 연동제 정보 저장 (연동제 적용이 true이고 연동제 정보가 있는 경우) + if (data.vendorMaterialPriceRelatedYn && data.priceAdjustmentForm && vendorResponse.id) { + const priceAdjustmentData: { + rfqLastVendorResponsesId: number; + itemName?: string | null; + adjustmentReflectionPoint?: string | null; + majorApplicableRawMaterial?: string | null; + adjustmentFormula?: string | null; + rawMaterialPriceIndex?: string | null; + referenceDate?: string | null; + comparisonDate?: string | null; + adjustmentRatio?: number | null; + notes?: string | null; + adjustmentConditions?: string | null; + majorNonApplicableRawMaterial?: string | null; + adjustmentPeriod?: string | null; + contractorWriter?: string | null; + adjustmentDate?: string | null; + nonApplicableReason?: string | null; + } = { + rfqLastVendorResponsesId: vendorResponse.id, + itemName: data.priceAdjustmentForm.itemName || null, + adjustmentReflectionPoint: data.priceAdjustmentForm.adjustmentReflectionPoint || null, + majorApplicableRawMaterial: data.priceAdjustmentForm.majorApplicableRawMaterial || null, + adjustmentFormula: data.priceAdjustmentForm.adjustmentFormula || null, + rawMaterialPriceIndex: data.priceAdjustmentForm.rawMaterialPriceIndex || null, + referenceDate: data.priceAdjustmentForm.referenceDate || null, + comparisonDate: data.priceAdjustmentForm.comparisonDate || null, + adjustmentRatio: data.priceAdjustmentForm.adjustmentRatio || null, + notes: data.priceAdjustmentForm.notes || null, + adjustmentConditions: data.priceAdjustmentForm.adjustmentConditions || null, + majorNonApplicableRawMaterial: data.priceAdjustmentForm.majorNonApplicableRawMaterial || null, + adjustmentPeriod: data.priceAdjustmentForm.adjustmentPeriod || null, + contractorWriter: data.priceAdjustmentForm.contractorWriter || null, + adjustmentDate: data.priceAdjustmentForm.adjustmentDate || null, + nonApplicableReason: data.priceAdjustmentForm.nonApplicableReason || null, + } + + // 기존 연동제 정보가 있는지 확인 + const existingPriceAdjustment = await tx + .select() + .from(rfqLastPriceAdjustmentForms) + .where(eq(rfqLastPriceAdjustmentForms.rfqLastVendorResponsesId, vendorResponse.id)) + .limit(1) + + if (existingPriceAdjustment.length > 0) { + // 업데이트 + await tx + .update(rfqLastPriceAdjustmentForms) + .set({ + ...priceAdjustmentData, + updatedAt: new Date(), + }) + .where(eq(rfqLastPriceAdjustmentForms.rfqLastVendorResponsesId, vendorResponse.id)) + } else { + // 새로 생성 + await tx.insert(rfqLastPriceAdjustmentForms).values(priceAdjustmentData) + } + } + + // 4. 이력 기록 await tx.insert(rfqLastVendorResponseHistory).values({ - vendorResponseId: vendorResponseId, - action: isNewResponse ? "생성" : (data.status === "제출완료" ? "제출" : "수정"), - previousStatus: existingResponse?.status || null, + vendorResponseId: vendorResponse.id, + action: "생성", + previousStatus: null, newStatus: data.status || "작성중", changeDetails: data, performedBy: session.user.id, }) - return { id: vendorResponseId, isNew: isNewResponse } + return { id: vendorResponse.id, isNew: true } }) // 파일 저장 (트랜잭션 밖에서 처리) @@ -262,7 +323,7 @@ export async function PUT( const previousStatus = existingResponse.status // 2. 새 버전 생성 (제출 시) 또는 기존 버전 업데이트 - let responseId = existingResponse.id + const responseId = existingResponse.id // if (data.status === "제출완료" && previousStatus !== "제출완료") { // // 기존 버전을 비활성화 @@ -362,7 +423,72 @@ export async function PUT( await tx.insert(rfqLastVendorQuotationItems).values(quotationItemsData) } - // 4. 이력 기록 + // 4. 연동제 정보 저장/업데이트 (연동제 적용이 true이고 연동제 정보가 있는 경우) + if (data.vendorMaterialPriceRelatedYn && data.priceAdjustmentForm && responseId) { + const priceAdjustmentData: { + rfqLastVendorResponsesId: number; + itemName?: string | null; + adjustmentReflectionPoint?: string | null; + majorApplicableRawMaterial?: string | null; + adjustmentFormula?: string | null; + rawMaterialPriceIndex?: string | null; + referenceDate?: string | null; + comparisonDate?: string | null; + adjustmentRatio?: number | null; + notes?: string | null; + adjustmentConditions?: string | null; + majorNonApplicableRawMaterial?: string | null; + adjustmentPeriod?: string | null; + contractorWriter?: string | null; + adjustmentDate?: string | null; + nonApplicableReason?: string | null; + } = { + rfqLastVendorResponsesId: responseId, + itemName: data.priceAdjustmentForm.itemName || null, + adjustmentReflectionPoint: data.priceAdjustmentForm.adjustmentReflectionPoint || null, + majorApplicableRawMaterial: data.priceAdjustmentForm.majorApplicableRawMaterial || null, + adjustmentFormula: data.priceAdjustmentForm.adjustmentFormula || null, + rawMaterialPriceIndex: data.priceAdjustmentForm.rawMaterialPriceIndex || null, + referenceDate: data.priceAdjustmentForm.referenceDate || null, + comparisonDate: data.priceAdjustmentForm.comparisonDate || null, + adjustmentRatio: data.priceAdjustmentForm.adjustmentRatio || null, + notes: data.priceAdjustmentForm.notes || null, + adjustmentConditions: data.priceAdjustmentForm.adjustmentConditions || null, + majorNonApplicableRawMaterial: data.priceAdjustmentForm.majorNonApplicableRawMaterial || null, + adjustmentPeriod: data.priceAdjustmentForm.adjustmentPeriod || null, + contractorWriter: data.priceAdjustmentForm.contractorWriter || null, + adjustmentDate: data.priceAdjustmentForm.adjustmentDate || null, + nonApplicableReason: data.priceAdjustmentForm.nonApplicableReason || null, + } + + // 기존 연동제 정보가 있는지 확인 + const existingPriceAdjustment = await tx + .select() + .from(rfqLastPriceAdjustmentForms) + .where(eq(rfqLastPriceAdjustmentForms.rfqLastVendorResponsesId, responseId)) + .limit(1) + + if (existingPriceAdjustment.length > 0) { + // 업데이트 + await tx + .update(rfqLastPriceAdjustmentForms) + .set({ + ...priceAdjustmentData, + updatedAt: new Date(), + }) + .where(eq(rfqLastPriceAdjustmentForms.rfqLastVendorResponsesId, responseId)) + } else { + // 새로 생성 + await tx.insert(rfqLastPriceAdjustmentForms).values(priceAdjustmentData) + } + } else if (data.vendorMaterialPriceRelatedYn === false && responseId) { + // 연동제 미적용 시 기존 데이터 삭제 + await tx + .delete(rfqLastPriceAdjustmentForms) + .where(eq(rfqLastPriceAdjustmentForms.rfqLastVendorResponsesId, responseId)) + } + + // 5. 이력 기록 await tx.insert(rfqLastVendorResponseHistory).values({ vendorResponseId: responseId, action: data.status === "제출완료" ? "제출" : "수정", |
