From ac584bce2f127f35a98c4a60e779f6c71032a2d8 Mon Sep 17 00:00:00 2001 From: Hardik Date: Wed, 6 May 2026 00:30:49 +0530 Subject: [PATCH] fix: totalAmount always stores grand total including GST manager-line-edit-actions: add gstRate to lineItemSchema; recalculate newTotal as sum(qty * unitPrice * (1 + gstRate)) and persist gstRate on recreated line items. seed: patch existing POs totalAmount to grand total (taxable * 1.18): PO-2026-00001: 8450 -> 9971 PO-2026-00002: 3200 -> 3776 PO-2026-00003: 950 -> 1121 --- .../approvals/[id]/manager-line-edit-actions.ts | 7 ++++++- App/pelagia-portal/prisma/seed.ts | 12 ++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/App/pelagia-portal/app/(portal)/approvals/[id]/manager-line-edit-actions.ts b/App/pelagia-portal/app/(portal)/approvals/[id]/manager-line-edit-actions.ts index 3a387e9..c228c10 100644 --- a/App/pelagia-portal/app/(portal)/approvals/[id]/manager-line-edit-actions.ts +++ b/App/pelagia-portal/app/(portal)/approvals/[id]/manager-line-edit-actions.ts @@ -14,6 +14,7 @@ const lineItemSchema = z.object({ unit: z.string().min(1), size: z.string().optional(), unitPrice: z.coerce.number().nonnegative(), + gstRate: z.coerce.number().min(0).max(1).default(0.18), }); export async function managerEditLineItems({ @@ -46,7 +47,10 @@ export async function managerEditLineItems({ unitPrice: Number(li.unitPrice), })); - const newTotal = parsed.data.reduce((sum, item) => sum + item.quantity * item.unitPrice, 0); + const newTotal = parsed.data.reduce( + (sum, item) => sum + item.quantity * item.unitPrice * (1 + item.gstRate), + 0 + ); await db.purchaseOrder.update({ where: { id: poId }, @@ -61,6 +65,7 @@ export async function managerEditLineItems({ size: item.size ?? null, unitPrice: item.unitPrice, totalPrice: item.quantity * item.unitPrice, + gstRate: item.gstRate, sortOrder: idx, })), }, diff --git a/App/pelagia-portal/prisma/seed.ts b/App/pelagia-portal/prisma/seed.ts index d91c968..866e524 100644 --- a/App/pelagia-portal/prisma/seed.ts +++ b/App/pelagia-portal/prisma/seed.ts @@ -188,12 +188,12 @@ async function main() { // Sample POs — upsert so re-running seed is safe const po1 = await db.purchaseOrder.upsert({ where: { poNumber: "PO-2026-00001" }, - update: { currency: "INR" }, + update: { currency: "INR", totalAmount: 9971.0 }, create: { poNumber: "PO-2026-00001", title: "Engine Room Spare Parts — MV Pelagia Star", status: "MGR_REVIEW", - totalAmount: 8450.0, + totalAmount: 9971.0, // 8450 taxable × 1.18 GST currency: "INR", submittedAt: new Date(), submitterId: technical.id, @@ -218,12 +218,12 @@ async function main() { await db.purchaseOrder.upsert({ where: { poNumber: "PO-2026-00002" }, - update: { currency: "INR" }, + update: { currency: "INR", totalAmount: 3776.0 }, create: { poNumber: "PO-2026-00002", title: "Crew Safety Equipment — MV Aegean Wind", status: "DRAFT", - totalAmount: 3200.0, + totalAmount: 3776.0, // 3200 taxable × 1.18 GST currency: "INR", submitterId: technical.id, vesselId: mv2.id, @@ -242,12 +242,12 @@ async function main() { await db.purchaseOrder.upsert({ where: { poNumber: "PO-2026-00003" }, - update: { currency: "INR" }, + update: { currency: "INR", totalAmount: 1121.0 }, create: { poNumber: "PO-2026-00003", title: "Navigation Charts Update — Fleet", status: "MGR_APPROVED", - totalAmount: 950.0, + totalAmount: 1121.0, // 950 taxable × 1.18 GST currency: "INR", submittedAt: new Date(Date.now() - 5 * 86400000), approvedAt: new Date(Date.now() - 2 * 86400000),