From eb91715ca78c586bfea34ee37396257797ccec08 Mon Sep 17 00:00:00 2001 From: Methapon Metanipat Date: Thu, 17 Oct 2024 15:52:29 +0700 Subject: [PATCH] feat: add vat exclude feature --- src/controllers/05-quotation-controller.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/controllers/05-quotation-controller.ts b/src/controllers/05-quotation-controller.ts index ac93fa8..51eab26 100644 --- a/src/controllers/05-quotation-controller.ts +++ b/src/controllers/05-quotation-controller.ts @@ -370,7 +370,9 @@ export class QuotationController extends Controller { const p = product.find((p) => p.id === v.productId)!; const price = body.agentPrice ? p.agentPrice : p.price; const pricePerUnit = p.vatIncluded ? precisionRound(price / (1 + VAT_DEFAULT)) : price; - const vat = precisionRound((pricePerUnit * v.amount - (v.discount || 0)) * VAT_DEFAULT); + const vat = p.calcVat + ? precisionRound((pricePerUnit * v.amount - (v.discount || 0)) * VAT_DEFAULT) + : 0; return { order: i + 1, @@ -394,6 +396,12 @@ export class QuotationController extends Controller { a.totalPrice = precisionRound(a.totalPrice + c.pricePerUnit * c.amount); 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, + ) + : 0; a.finalPrice = precisionRound( Math.max(a.totalPrice - a.totalDiscount + a.vat - (body.discount || 0), 0), ); @@ -404,6 +412,7 @@ export class QuotationController extends Controller { totalPrice: 0, totalDiscount: 0, vat: 0, + vatExcluded: 0, discount: body.discount, finalPrice: 0, }, @@ -613,7 +622,9 @@ export class QuotationController extends Controller { const p = product.find((p) => p.id === v.productId)!; const price = record.agentPrice ? p.agentPrice : p.price; const pricePerUnit = p.vatIncluded ? precisionRound(price / 1 + VAT_DEFAULT) : price; - const vat = precisionRound((pricePerUnit * v.amount - (v.discount || 0)) * VAT_DEFAULT); + const vat = p.calcVat + ? precisionRound((pricePerUnit * v.amount - (v.discount || 0)) * VAT_DEFAULT) + : 0; return { order: i + 1, @@ -637,6 +648,12 @@ export class QuotationController extends Controller { a.totalPrice = precisionRound(a.totalPrice + c.pricePerUnit * c.amount); 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, + ) + : 0; a.finalPrice = precisionRound( Math.max(a.totalPrice - a.totalDiscount + a.vat - (body.discount || 0), 0), ); @@ -647,6 +664,7 @@ export class QuotationController extends Controller { totalPrice: 0, totalDiscount: 0, vat: 0, + vatExcluded: 0, discount: body.discount, finalPrice: 0, },