From fd6273264231917e67f1c6e8908dcaa611681c00 Mon Sep 17 00:00:00 2001 From: Thanaphon Frappet Date: Wed, 29 Jan 2025 16:03:27 +0700 Subject: [PATCH] refactor: use agent price --- src/pages/05_quotation/QuotationForm.vue | 1 + src/pages/05_quotation/preview/ViewForm.vue | 49 +++++++++++++++++---- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/pages/05_quotation/QuotationForm.vue b/src/pages/05_quotation/QuotationForm.vue index 5bcc5ffb..62d0dea2 100644 --- a/src/pages/05_quotation/QuotationForm.vue +++ b/src/pages/05_quotation/QuotationForm.vue @@ -1122,6 +1122,7 @@ function storeDataLocal() { }, selectedWorker: selectedWorker.value, createdBy: quotationFormState.value.createdBy('tha'), + agentPrice: agentPrice.value, }, }), ); diff --git a/src/pages/05_quotation/preview/ViewForm.vue b/src/pages/05_quotation/preview/ViewForm.vue index 28eff17e..255fbd6d 100644 --- a/src/pages/05_quotation/preview/ViewForm.vue +++ b/src/pages/05_quotation/preview/ViewForm.vue @@ -19,6 +19,7 @@ import { QuotationPayload, Details, QuotationFull, + ProductRelation, } from 'src/stores/quotations/types'; // NOTE: Import Components @@ -40,11 +41,12 @@ type Product = { code: string; detail: string; amount: number; - priceUnit: number; + pricePerUnit: number; discount: number; vat: number; value: number; calcVat: boolean; + product: ProductRelation; }; type SummaryPrice = { @@ -60,6 +62,8 @@ const branch = ref(); const productList = ref([]); const bankList = ref([]); +const agentPrice = ref(false); + const elements = ref([]); const chunks = ref([[]]); const attachmentList = ref< @@ -194,6 +198,8 @@ onMounted(async () => { customer.value = resCustomerBranch; } + agentPrice.value = data.value.agentPrice || parsed?.meta?.agentPrice; + details.value = { code: parsed?.meta?.source?.code ?? data.value?.code, createdAt: @@ -247,11 +253,12 @@ onMounted(async () => { code: v.product.code, detail: v.product.name, amount: v.amount || 0, - priceUnit: v.pricePerUnit || 0, + pricePerUnit: v.pricePerUnit || 0, discount: v.discount || 0, vat: v.vat || 0, value: precisionRound(price + (v.product.calcVat ? vat : 0)), calcVat: v.product.calcVat, + product: v.product, }; }, ) || []; @@ -273,10 +280,13 @@ onMounted(async () => { const vat = (finalPriceNoVat * c.amount - c.discount) * (config.value?.vat || 0.07); + const calcVat = + c.product[agentPrice.value ? 'agentPriceCalcVat' : 'calcVat']; + a.totalPrice = precisionRound(a.totalPrice + price); a.totalDiscount = precisionRound(a.totalDiscount + Number(c.discount)); - a.vat = c.product.calcVat ? precisionRound(a.vat + vat) : a.vat; - a.vatExcluded = c.product.calcVat + a.vat = calcVat ? precisionRound(a.vat + vat) : a.vat; + a.vatExcluded = calcVat ? a.vatExcluded : precisionRound(a.vatExcluded + price); a.finalPrice = precisionRound( @@ -285,7 +295,6 @@ onMounted(async () => { a.vat - Number(data.value?.discount || 0), ); - return a; }, { @@ -300,6 +309,20 @@ onMounted(async () => { assignData(); }); +function calcPrice(c: Product) { + const originalPrice = c.pricePerUnit; + const finalPriceWithVat = precisionRound( + originalPrice + originalPrice * (config.value?.vat || 0.07), + ); + const finalPriceNoVat = finalPriceWithVat / (1 + (config.value?.vat || 0.07)); + + const price = finalPriceNoVat * c.amount - c.discount; + const vat = c.product[agentPrice.value ? 'agentPriceCalcVat' : 'calcVat'] + ? (finalPriceNoVat * c.amount - c.discount) * (config.value?.vat || 0.07) + : 0; + return precisionRound(price + vat); +} + watch(elements, () => {}); function print() { @@ -359,7 +382,15 @@ function print() { {{ v.detail }} {{ v.amount }} - {{ formatNumberDecimal(v.priceUnit, 2) }} + {{ + formatNumberDecimal( + v.pricePerUnit + + (v.product[agentPrice ? 'agentPriceCalcVat' : 'calcVat'] + ? v.pricePerUnit * (config?.vat || 0.07) + : 0), + 2, + ) + }} {{ formatNumberDecimal(v.discount, 2) }} @@ -367,9 +398,9 @@ function print() { {{ formatNumberDecimal( - v.calcVat + v.product.calcVat ? precisionRound( - (v.priceUnit * v.amount - v.discount) * + (v.pricePerUnit * v.amount - v.discount) * (config?.vat || 0.07), ) : 0, @@ -378,7 +409,7 @@ function print() { }} - {{ formatNumberDecimal(v.value, 2) }} + {{ formatNumberDecimal(calcPrice(v), 2) }}