fix: price calc

This commit is contained in:
Thanaphon Saengchan 2025-09-12 11:24:30 +07:00 committed by Methapon2001
parent c0a2d3769d
commit 73b2d52fb0
2 changed files with 70 additions and 41 deletions

View file

@ -386,7 +386,7 @@ async function assignFormData(id: string) {
selectedProductGroup.value =
data.productServiceList[0]?.product.productGroup?.id || '';
(previousValue = {
((previousValue = {
id: data.id || undefined,
debitNoteQuotationId: data.debitNoteQuotationId || undefined,
productServiceList: structuredClone(
@ -412,7 +412,7 @@ async function assignFormData(id: string) {
quotationId: data.debitNoteQuotationId,
remark: data.remark || undefined,
}),
(currentFormData.value = structuredClone(previousValue));
(currentFormData.value = structuredClone(previousValue)));
assignProductServiceList();
assignSelectedWorker();
@ -434,6 +434,7 @@ async function getQuotation(id?: string) {
const data = await quotationStore.getQuotation(quotationId);
if (!!data) {
quotationData.value = data;
agentPrice.value = quotationData.value.agentPrice;
}
}
}
@ -539,6 +540,8 @@ function getPrice(
) {
if (filterHook) list = list.filter(filterHook);
console.log(list);
return list.reduce(
(a, c) => {
if (
@ -549,25 +552,28 @@ function getPrice(
return a;
}
const price = precisionRound(c.pricePerUnit * c.amount);
const vat =
precisionRound(
(c.pricePerUnit * (c.discount ? c.amount : 1) - c.discount) *
(config.value?.vat || 0.07),
) * (!c.discount ? c.amount : 1);
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;
const vatFactor = calcVat ? (config.value?.vat ?? 0.07) : 0;
const pricePerUnit = precisionRound(
(c.pricePerUnit * (1 + vatFactor)) / (1 + vatFactor),
);
const price = precisionRound(
(pricePerUnit * c.amount * (1 + vatFactor) - c.discount) /
(1 + vatFactor),
);
const vat = price * vatFactor;
a.totalPrice = precisionRound(a.totalPrice + price + c.discount);
a.totalDiscount = precisionRound(a.totalDiscount + c.discount);
a.vat = precisionRound(a.vat + vat);
a.vatExcluded = c.product.calcVat
? a.vatExcluded
: precisionRound(a.vat + vat);
a.finalPrice = precisionRound(
a.totalPrice -
a.totalDiscount +
a.vat -
Number(currentFormData.value.discount || 0),
);
: precisionRound(a.vatExcluded + price);
a.finalPrice = precisionRound(a.totalPrice - a.totalDiscount + a.vat);
return a;
},
@ -575,6 +581,7 @@ function getPrice(
totalPrice: 0,
totalDiscount: 0,
vat: 0,
vatIncluded: 0,
vatExcluded: 0,
finalPrice: 0,
},
@ -869,6 +876,7 @@ async function exampleReceipt(id: string) {
function storeDataLocal() {
// quotationFormData.value.productServiceList = productServiceList.value;
//
localStorage.setItem(
'debit-note-preview',
@ -877,6 +885,7 @@ function storeDataLocal() {
...currentFormData.value,
customerBranchId: quotationData.value?.customerBranchId,
registeredBranchId: quotationData.value?.registeredBranchId,
agentPrice: quotationData.value.agentPrice,
},
meta: {
source: {

View file

@ -82,6 +82,8 @@ const data = ref<
>();
const productServiceList = ref<ProductServiceList[]>([]);
const selectedInstallmentNo = ref<number[]>([]);
const agentPrice = ref<boolean>(false);
const summaryPrice = ref<SummaryPrice>({
totalPrice: 0,
@ -217,6 +219,8 @@ onMounted(async () => {
}
productServiceList.value = parsed.meta.productServicelist;
selectedInstallmentNo.value = parsed.meta.selectedInstallmentNo;
agentPrice.value = parsed.meta.agentPrice;
productList.value =
productServiceList.value?.map((v) => ({
@ -224,40 +228,56 @@ onMounted(async () => {
code: v.product.code,
detail: v.product.name,
amount: v.amount || 0,
priceUnit: v.pricePerUnit || 0,
priceUnit:
v.pricePerUnit +
(v.product[agentPrice.value ? 'agentPriceCalcVat' : 'calcVat']
? v.pricePerUnit * (config.value?.vat || 0.07)
: 0),
discount: v.discount || 0,
vat: v.vat || 0,
value: precisionRound(
(v.pricePerUnit || 0) * v.amount -
(v.discount || 0) +
(v.product.calcVat
? ((v.pricePerUnit || 0) * v.amount - (v.discount || 0)) *
(config.value?.vat || 0.07)
: 0),
(v.pricePerUnit +
(v.product[agentPrice.value ? 'agentPriceCalcVat' : 'calcVat']
? v.pricePerUnit * (config.value?.vat || 0.07)
: 0)) *
v.amount -
v.discount,
),
})) || [];
}
summaryPrice.value = (productServiceList.value || []).reduce(
(a, c) => {
const price = precisionRound((c.pricePerUnit || 0) * c.amount);
const vat = precisionRound(
((c.pricePerUnit || 0) * c.amount - (c.discount || 0)) *
(config.value?.vat || 0.07),
if (
selectedInstallmentNo.value?.length > 0 &&
c.installmentNo &&
!selectedInstallmentNo.value?.includes(c.installmentNo)
) {
return a;
}
const calcVat =
c.product[agentPrice.value ? 'agentPriceCalcVat' : 'calcVat'];
const vatFactor = calcVat ? (config.value?.vat ?? 0.07) : 0;
const pricePerUnit = precisionRound(
(c.pricePerUnit * (1 + vatFactor)) / (1 + vatFactor),
);
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;
const price = precisionRound(
(pricePerUnit * c.amount * (1 + vatFactor) - c.discount) /
(1 + vatFactor),
);
const vat = price * vatFactor;
a.totalPrice = precisionRound(a.totalPrice + price + c.discount);
a.totalDiscount = precisionRound(a.totalDiscount + c.discount);
a.vat = precisionRound(a.vat + vat);
a.vatExcluded = c.product.calcVat
? a.vatExcluded
: precisionRound(a.vat + vat);
a.finalPrice = precisionRound(
a.totalPrice -
a.totalDiscount +
a.vat -
Number(data.value?.discount || 0),
);
: precisionRound(a.vatExcluded + price);
a.finalPrice = precisionRound(a.totalPrice - a.totalDiscount + a.vat);
return a;
},
@ -413,8 +433,8 @@ function print() {
{{
formatNumberDecimal(
summaryPrice.totalPrice -
summaryPrice.totalDiscount +
summaryPrice.vat,
summaryPrice.totalDiscount -
summaryPrice.vatExcluded,
2,
)
}}