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

@ -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,
)
}}