From c430b6082e89bda16f8a08017b99341284c8a201 Mon Sep 17 00:00:00 2001 From: net Date: Mon, 15 Sep 2025 17:02:25 +0700 Subject: [PATCH] fix: calc price --- src/pages/11_credit-note/FormPage.vue | 25 +++--- .../11_credit-note/document-view/MainPage.vue | 36 ++------- .../expansion/ProductExpansion.vue | 31 ++++---- src/stores/credit-note/index.ts | 76 +++++++++++++++++++ src/stores/quotations/types.ts | 1 + 5 files changed, 119 insertions(+), 50 deletions(-) diff --git a/src/pages/11_credit-note/FormPage.vue b/src/pages/11_credit-note/FormPage.vue index fae332e6..a73d1d6d 100644 --- a/src/pages/11_credit-note/FormPage.vue +++ b/src/pages/11_credit-note/FormPage.vue @@ -31,6 +31,7 @@ import { EditButton, UndoButton, } from 'src/components/button'; +import { precisionRound } from 'src/utils/arithmetic'; import { DebitNote, useDebitNote } from 'src/stores/debit-note'; import { RequestWork } from 'src/stores/request-list/types'; import { storeToRefs } from 'pinia'; @@ -40,6 +41,8 @@ import { useI18n } from 'vue-i18n'; import { QForm } from 'quasar'; import { getName } from 'src/services/keycloak'; +import { RequestWorkStatus } from 'src/stores/request-list/types'; + const route = useRoute(); const router = useRouter(); const creditNote = useCreditNote(); @@ -49,6 +52,7 @@ const configStore = useConfigStore(); const { data: config } = storeToRefs(configStore); const { t } = useI18n(); +const agentPrice = ref(false); const refForm = ref>(); const creditNoteData = ref(); const quotationData = ref(); @@ -206,22 +210,23 @@ function getPrice( ) { return list.reduce( (a, c) => { - const pricePerUnit = - c.product.pricePerUnit - c.product.discount / c.product.amount; - const amount = c.list.length; - const priceNoVat = pricePerUnit; - const priceDiscountNoVat = priceNoVat * amount; + const value = creditNote.getfinalPriceCredit(c.list || []); - const rawVatTotal = priceDiscountNoVat * (config.value?.vat || 0.07); + a.totalPrice = precisionRound(a.totalPrice + value.totalPrice); + a.totalDiscount = precisionRound(a.totalDiscount + value.totalDiscount); + a.vat = precisionRound(a.vat + value.vat); + a.vatIncluded = precisionRound(a.vatIncluded + value.vatIncluded); + a.vatExcluded = precisionRound(a.vatExcluded + value.vatExcluded); + a.finalPrice = precisionRound(a.finalPrice + value.finalPrice); - a.totalPrice = a.totalPrice + priceDiscountNoVat; - a.vat = c.product.vat !== 0 ? a.vat + rawVatTotal : a.vat; - a.finalPrice = a.totalPrice + a.vat; return a; }, { totalPrice: 0, + totalDiscount: 0, vat: 0, + vatIncluded: 0, + vatExcluded: 0, finalPrice: 0, }, ); @@ -296,6 +301,8 @@ async function getQuotation() { if (!ret) return; quotationData.value = ret; + + agentPrice.value = quotationData.value.agentPrice; } async function submit() { diff --git a/src/pages/11_credit-note/document-view/MainPage.vue b/src/pages/11_credit-note/document-view/MainPage.vue index e15fe13d..0ebbe50e 100644 --- a/src/pages/11_credit-note/document-view/MainPage.vue +++ b/src/pages/11_credit-note/document-view/MainPage.vue @@ -245,22 +245,9 @@ function calcPricePerUnit(product: RequestWork['productService']['product']) { : product.price; } -function calcPrice( - product: RequestWork['productService']['product'], - amount: number, - vat: number = 0, -) { - const pricePerUnit = agentPrice.value ? product.agentPrice : product.price; - - const priceNoVat = product.vatIncluded - ? pricePerUnit / (1 + (config.value?.vat || 0.07)) - : pricePerUnit; - const priceDiscountNoVat = priceNoVat * amount - 0; - - const rawVatTotal = - vat === 0 ? 0 : priceDiscountNoVat * (config.value?.vat || 0.07); - - return precisionRound(priceNoVat * amount + rawVatTotal); +function calcPrice(c: RequestWork[]): number { + const price = creditNoteStore.getfinalPriceCredit(c); + return price.finalPrice; } watch(elements, () => {}); @@ -327,13 +314,11 @@ function closeAble() { {{ $t('preview.pricePerUnit') }} {{ $t('preview.value') }} - {{ console.log(chunks) }} - {{ console.log(chunk) }} {{ i + 1 }} {{ v.product.product.code }} {{ v.product.product.name }} - + {{ formatNumberDecimal( calcPricePerUnit(v.product.product) + @@ -345,13 +330,8 @@ function closeAble() { ) }} - - {{ - formatNumberDecimal( - calcPrice(v.product.product, v.list.length, v.product.vat), - 2, - ) - }} + + {{ formatNumberDecimal(calcPrice(v.list), 2) }} @@ -409,8 +389,8 @@ function closeAble() { {{ formatNumberDecimal( summaryPrice.totalPrice - - summaryPrice.totalDiscount + - summaryPrice.vat, + summaryPrice.totalDiscount - + summaryPrice.vatExcluded, 2, ) }} diff --git a/src/pages/11_credit-note/expansion/ProductExpansion.vue b/src/pages/11_credit-note/expansion/ProductExpansion.vue index af4e6ea3..a20dd953 100644 --- a/src/pages/11_credit-note/expansion/ProductExpansion.vue +++ b/src/pages/11_credit-note/expansion/ProductExpansion.vue @@ -12,10 +12,13 @@ import { baseUrl, formatNumberDecimal } from 'src/stores/utils'; import { precisionRound } from 'src/utils/arithmetic'; import { productColumn } from '../constants'; +import { useCreditNote } from 'src/stores/credit-note'; + const configStore = useConfigStore(); const { data: config } = storeToRefs(configStore); const currentExpanded = ref([]); +const creditNote = useCreditNote(); defineProps<{ readonly?: boolean; @@ -44,15 +47,22 @@ function calcPricePerUnit(product: RequestWork['productService']) { return product.pricePerUnit - product.discount / product.amount; } -function calcPrice(c: RequestWork['productService'], amount: number) { - const pricePerUnit = c.pricePerUnit - c.discount / c.amount; - const priceNoVat = pricePerUnit; - const priceDiscountNoVat = priceNoVat * amount; +function calcVat(c: RequestWork['productService']) { + const vatFactor = c.product.serviceChargeCalcVat + ? (config.value?.vat ?? 0.07) + : 0; - const rawVatTotal = - c.vat === 0 ? 0 : priceDiscountNoVat * (config.value?.vat || 0.07); + const price = precisionRound( + c.pricePerUnit * (1 + vatFactor) - c.discount / (1 + vatFactor), + ); - return precisionRound(priceNoVat * amount + rawVatTotal); + const vat = (price / (1 + vatFactor)) * vatFactor; + return vat; +} + +function calcPrice(c: RequestWork[]) { + const price = creditNote.getfinalPriceCredit(c); + return price.finalPrice; }