From 86db927efe0fca149c18a0bec1173ae2e739d410 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Thu, 11 Sep 2025 13:57:23 +0700 Subject: [PATCH] fix: price calc --- src/controllers/05-quotation-controller.ts | 13 +++-- src/controllers/09-debit-note-controller.ts | 60 ++++++++++++--------- 2 files changed, 40 insertions(+), 33 deletions(-) diff --git a/src/controllers/05-quotation-controller.ts b/src/controllers/05-quotation-controller.ts index d283dbb..942e4d1 100644 --- a/src/controllers/05-quotation-controller.ts +++ b/src/controllers/05-quotation-controller.ts @@ -532,10 +532,10 @@ export class QuotationController extends Controller { ); const pricePerUnit = finalPrice / (1 + VAT_DEFAULT); const vat = (body.agentPrice ? p.agentPriceCalcVat : p.calcVat) - ? pricePerUnit * (1 + VAT_DEFAULT) * v.amount - - ((v.discount || 0) / (1 + VAT_DEFAULT)) * VAT_DEFAULT + ? ((pricePerUnit * (1 + VAT_DEFAULT) * v.amount - (v.discount || 0)) / + (1 + VAT_DEFAULT)) * + VAT_DEFAULT : 0; - return { order: i + 1, productId: v.productId, @@ -819,8 +819,9 @@ export class QuotationController extends Controller { ); const pricePerUnit = finalPrice / (1 + VAT_DEFAULT); const vat = (record.agentPrice ? p.agentPriceCalcVat : p.calcVat) - ? pricePerUnit * (1 + VAT_DEFAULT) * v.amount - - ((v.discount || 0) / (1 + VAT_DEFAULT)) * VAT_DEFAULT + ? ((pricePerUnit * (1 + VAT_DEFAULT) * v.amount - (v.discount || 0)) / + (1 + VAT_DEFAULT)) * + VAT_DEFAULT : 0; return { @@ -866,8 +867,6 @@ export class QuotationController extends Controller { }, ); - console.log(price); - const changed = list?.some((lhs) => { const found = record.productServiceList.find((rhs) => { return ( diff --git a/src/controllers/09-debit-note-controller.ts b/src/controllers/09-debit-note-controller.ts index 5fcdee9..f87bbe9 100644 --- a/src/controllers/09-debit-note-controller.ts +++ b/src/controllers/09-debit-note-controller.ts @@ -430,12 +430,18 @@ export class DebitNoteController extends Controller { const list = body.productServiceList.map((v, i) => { const p = product.find((p) => p.id === v.productId)!; - const price = body.agentPrice ? p.agentPrice : p.price; - const pricePerUnit = p.vatIncluded ? price / (1 + VAT_DEFAULT) : price; - const vat = p.calcVat - ? (pricePerUnit * (v.discount ? v.amount : 1) - (v.discount || 0)) * - VAT_DEFAULT * - (!v.discount ? v.amount : 1) + + const vatIncluded = body.agentPrice ? p.agentPriceVatIncluded : p.vatIncluded; + + const originalPrice = body.agentPrice ? p.agentPrice : p.price; + const finalPrice = precisionRound( + originalPrice + (vatIncluded ? 0 : originalPrice * VAT_DEFAULT), + ); + const pricePerUnit = finalPrice / (1 + VAT_DEFAULT); + const vat = (body.agentPrice ? p.agentPriceCalcVat : p.calcVat) + ? ((pricePerUnit * (1 + VAT_DEFAULT) * v.amount - (v.discount || 0)) / + (1 + VAT_DEFAULT)) * + VAT_DEFAULT : 0; return { @@ -458,15 +464,13 @@ export class DebitNoteController 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), ); @@ -673,12 +677,18 @@ export class DebitNoteController extends Controller { } const list = body.productServiceList.map((v, i) => { const p = product.find((p) => p.id === v.productId)!; - const price = body.agentPrice ? p.agentPrice : p.price; - const pricePerUnit = p.vatIncluded ? price / (1 + VAT_DEFAULT) : price; - const vat = p.calcVat - ? (pricePerUnit * (v.discount ? v.amount : 1) - (v.discount || 0)) * - VAT_DEFAULT * - (!v.discount ? v.amount : 1) + + const vatIncluded = record.agentPrice ? p.agentPriceVatIncluded : p.vatIncluded; + + const originalPrice = record.agentPrice ? p.agentPrice : p.price; + const finalPrice = precisionRound( + originalPrice + (vatIncluded ? 0 : originalPrice * VAT_DEFAULT), + ); + const pricePerUnit = finalPrice / (1 + VAT_DEFAULT); + const vat = (record.agentPrice ? p.agentPriceCalcVat : p.calcVat) + ? ((pricePerUnit * (1 + VAT_DEFAULT) * v.amount - (v.discount || 0)) / + (1 + VAT_DEFAULT)) * + VAT_DEFAULT : 0; return { @@ -701,15 +711,13 @@ export class DebitNoteController 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), );