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

View file

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