From 5674a18cc3e46edec63a322cf3282384fc6177f0 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Thu, 11 Sep 2025 11:48:17 +0700 Subject: [PATCH] fix: price calc match with flow account --- src/controllers/05-quotation-controller.ts | 43 +++++++++++----------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/src/controllers/05-quotation-controller.ts b/src/controllers/05-quotation-controller.ts index 29face5..d283dbb 100644 --- a/src/controllers/05-quotation-controller.ts +++ b/src/controllers/05-quotation-controller.ts @@ -527,14 +527,13 @@ export class QuotationController extends Controller { const vatIncluded = body.agentPrice ? p.agentPriceVatIncluded : p.vatIncluded; const originalPrice = body.agentPrice ? p.agentPrice : p.price; - const finalPriceWithVat = precisionRound( + const finalPrice = precisionRound( originalPrice + (vatIncluded ? 0 : originalPrice * VAT_DEFAULT), ); - - const price = finalPriceWithVat; - const pricePerUnit = price / (1 + VAT_DEFAULT); + const pricePerUnit = finalPrice / (1 + VAT_DEFAULT); const vat = (body.agentPrice ? p.agentPriceCalcVat : p.calcVat) - ? (pricePerUnit * v.amount - (v.discount || 0)) * VAT_DEFAULT + ? pricePerUnit * (1 + VAT_DEFAULT) * v.amount - + ((v.discount || 0) / (1 + VAT_DEFAULT)) * VAT_DEFAULT : 0; return { @@ -557,13 +556,13 @@ export class QuotationController extends Controller { const price = list.reduce( (a, c) => { - a.totalPrice = precisionRound(a.totalPrice + c.pricePerUnit * c.amount); + const vat = c.vat ? VAT_DEFAULT : 0; + const price = c.pricePerUnit * c.amount * (1 + vat) - c.discount; + + a.totalPrice = precisionRound(a.totalPrice + price / (1 + vat) + c.discount); a.totalDiscount = precisionRound(a.totalDiscount + c.discount); a.vat = precisionRound(a.vat + c.vat); - a.vatExcluded = - c.vat === 0 - ? precisionRound(a.vatExcluded + (c.pricePerUnit * c.amount - (c.discount || 0))) - : a.vatExcluded; + a.vatExcluded = c.vat === 0 ? precisionRound(a.vatExcluded + price) : a.vatExcluded; a.finalPrice = precisionRound( Math.max(a.totalPrice - a.totalDiscount + a.vat - (body.discount || 0), 0), ); @@ -815,14 +814,13 @@ export class QuotationController extends Controller { const vatIncluded = record.agentPrice ? p.agentPriceVatIncluded : p.vatIncluded; const originalPrice = record.agentPrice ? p.agentPrice : p.price; - const finalPriceWithVat = precisionRound( + const finalPrice = precisionRound( originalPrice + (vatIncluded ? 0 : originalPrice * VAT_DEFAULT), ); - - const price = finalPriceWithVat; - const pricePerUnit = price / (1 + VAT_DEFAULT); + const pricePerUnit = finalPrice / (1 + VAT_DEFAULT); const vat = (record.agentPrice ? p.agentPriceCalcVat : p.calcVat) - ? (pricePerUnit * v.amount - (v.discount || 0)) * VAT_DEFAULT + ? pricePerUnit * (1 + VAT_DEFAULT) * v.amount - + ((v.discount || 0) / (1 + VAT_DEFAULT)) * VAT_DEFAULT : 0; return { @@ -845,15 +843,13 @@ export class QuotationController extends Controller { const price = list?.reduce( (a, c) => { - a.totalPrice = precisionRound(a.totalPrice + c.pricePerUnit * c.amount); + const vat = c.vat ? VAT_DEFAULT : 0; + const price = c.pricePerUnit * c.amount * (1 + vat) - c.discount; + + a.totalPrice = precisionRound(a.totalPrice + price / (1 + vat) + c.discount); a.totalDiscount = precisionRound(a.totalDiscount + c.discount); a.vat = precisionRound(a.vat + c.vat); - a.vatExcluded = - c.vat === 0 - ? precisionRound( - a.vatExcluded + (c.pricePerUnit * c.amount - (c.discount || 0)) * VAT_DEFAULT, - ) - : a.vatExcluded; + a.vatExcluded = c.vat === 0 ? precisionRound(a.vatExcluded + price) : a.vatExcluded; a.finalPrice = precisionRound( Math.max(a.totalPrice - a.totalDiscount + a.vat - (body.discount || 0), 0), ); @@ -869,6 +865,9 @@ export class QuotationController extends Controller { finalPrice: 0, }, ); + + console.log(price); + const changed = list?.some((lhs) => { const found = record.productServiceList.find((rhs) => { return (