refactor: use price in quotation instead

This commit is contained in:
Methapon2001 2025-01-30 14:26:45 +07:00
parent 5c3d2f1499
commit ae88440870
2 changed files with 31 additions and 72 deletions

View file

@ -111,7 +111,7 @@ const formTaskList = ref<
let taskListGroup = computed(() => { let taskListGroup = computed(() => {
const cacheData = formTaskList.value.reduce< const cacheData = formTaskList.value.reduce<
{ {
product: RequestWork['productService']['product']; product: RequestWork['productService'];
list: RequestWork[]; list: RequestWork[];
}[] }[]
>((acc, curr) => { >((acc, curr) => {
@ -129,7 +129,7 @@ let taskListGroup = computed(() => {
exist.list.push(task.requestWork); exist.list.push(task.requestWork);
} else { } else {
acc.push({ acc.push({
product: task.requestWork.productService.product, product: task.requestWork.productService,
list: [record], list: [record],
}); });
} }
@ -189,42 +189,28 @@ async function initStatus() {
function getPrice( function getPrice(
list: { list: {
product: RequestWork['productService']['product']; product: RequestWork['productService'];
list: RequestWork[]; list: RequestWork[];
}[], }[],
) { ) {
return list.reduce( return list.reduce(
(a, c) => { (a, c) => {
const agentPrice = !!quotationData.value?.agentPrice; const pricePerUnit =
const pricePerUnit = agentPrice ? c.product.agentPrice : c.product.price; c.product.pricePerUnit - c.product.discount / c.product.amount;
const amount = c.list.length; const amount = c.list.length;
const discount = 0; const priceNoVat = pricePerUnit;
const priceNoVat = c.product[ const priceDiscountNoVat = priceNoVat * amount;
agentPrice ? 'agentPriceVatIncluded' : 'vatIncluded'
]
? pricePerUnit / (1 + (config.value?.vat || 0.07))
: pricePerUnit;
const priceDiscountNoVat = priceNoVat * amount - discount;
const rawVatTotal = priceDiscountNoVat * (config.value?.vat || 0.07); const rawVatTotal = priceDiscountNoVat * (config.value?.vat || 0.07);
// const rawVat = rawVatTotal / amount;
a.totalPrice = a.totalPrice + priceDiscountNoVat; a.totalPrice = a.totalPrice + priceDiscountNoVat;
a.totalDiscount = a.totalDiscount + Number(discount); a.vat = c.product.vat !== 0 ? a.vat + rawVatTotal : a.vat;
a.vat = c.product[agentPrice ? 'agentPriceCalcVat' : 'calcVat'] a.finalPrice = a.totalPrice + a.vat;
? a.vat + rawVatTotal
: a.vat;
a.vatExcluded = c.product[agentPrice ? 'agentPriceCalcVat' : 'calcVat']
? a.vatExcluded
: a.vat + rawVatTotal;
a.finalPrice = a.totalPrice - a.totalDiscount + a.vat;
return a; return a;
}, },
{ {
totalPrice: 0, totalPrice: 0,
totalDiscount: 0,
vat: 0, vat: 0,
vatExcluded: 0,
finalPrice: 0, finalPrice: 0,
}, },
); );
@ -754,12 +740,12 @@ onMounted(async () => {
" "
/> />
<!-- TODO: bind remark -->
<RemarkExpansion <RemarkExpansion
v-if="view !== CreditNoteStatus.Success" v-if="view !== CreditNoteStatus.Success"
:readonly="readonly"
v-model:remark="formData.remark" v-model:remark="formData.remark"
:default-remark="defaultRemark" :default-remark="defaultRemark"
:items="[]"
:readonly
> >
<template #hint> <template #hint>
{{ $t('general.hintRemark') }} {{ $t('general.hintRemark') }}

View file

@ -1,33 +1,26 @@
<script lang="ts" setup> <script lang="ts" setup>
import { ref } from 'vue'; import { ref } from 'vue';
import { QTableSlots } from 'quasar'; import { QTableSlots } from 'quasar';
import { storeToRefs } from 'pinia';
import { AddButton } from 'src/components/button'; import { AddButton } from 'src/components/button';
import TableEmployee from '../../09_task-order/TableEmployee.vue'; import TableEmployee from '../../09_task-order/TableEmployee.vue';
import { useConfigStore } from 'stores/config';
import { RequestWork } from 'src/stores/request-list'; import { RequestWork } from 'src/stores/request-list';
import { productColumn } from '../constants';
import { baseUrl, formatNumberDecimal } from 'src/stores/utils'; import { baseUrl, formatNumberDecimal } from 'src/stores/utils';
import { precisionRound } from 'src/utils/arithmetic'; import { precisionRound } from 'src/utils/arithmetic';
import { useConfigStore } from 'stores/config'; import { productColumn } from '../constants';
import { storeToRefs } from 'pinia';
const currentExpanded = ref<boolean[]>([]);
const configStore = useConfigStore(); const configStore = useConfigStore();
const { data: config } = storeToRefs(configStore); const { data: config } = storeToRefs(configStore);
const taskProduct = defineModel<{ productId: string; discount?: number }[]>( const currentExpanded = ref<boolean[]>([]);
'taskProduct',
{
default: [],
},
);
const props = defineProps<{ defineProps<{
readonly?: boolean; readonly?: boolean;
agentPrice?: boolean;
taskList: { taskList: {
product: RequestWork['productService']['product']; product: RequestWork['productService'];
list: RequestWork[]; list: RequestWork[];
}[]; }[];
}>(); }>();
@ -36,9 +29,7 @@ defineEmits<{
(e: 'addProduct'): void; (e: 'addProduct'): void;
}>(); }>();
defineExpose({ defineExpose({ calcPricePerUnit });
calcPricePerUnit,
});
function openList(index: number) { function openList(index: number) {
if (!currentExpanded.value[index]) { if (!currentExpanded.value[index]) {
@ -49,35 +40,17 @@ function openList(index: number) {
currentExpanded.value[index] = !currentExpanded.value[index]; currentExpanded.value[index] = !currentExpanded.value[index];
} }
function calcPricePerUnit(product: RequestWork['productService']['product']) { function calcPricePerUnit(product: RequestWork['productService']) {
const val = props.agentPrice ? product.agentPrice : product.price; return product.pricePerUnit - product.discount / product.amount;
if (product[props.agentPrice ? 'agentPriceCalcVat' : 'calcVat']) {
return val / (1 + (config.value?.vat || 0.07));
} else {
return val;
}
} }
function calcPrice( function calcPrice(c: RequestWork['productService'], amount: number) {
product: RequestWork['productService']['product'], const pricePerUnit = c.pricePerUnit - c.discount / c.amount;
amount: number, const priceNoVat = pricePerUnit;
) { const priceDiscountNoVat = priceNoVat * amount;
const pricePerUnit = props.agentPrice ? product.agentPrice : product.price;
const discount =
taskProduct.value.find((v) => v.productId === product.id)?.discount || 0;
const priceNoVat = product[
props.agentPrice ? 'agentPriceVatIncluded' : 'vatIncluded'
]
? pricePerUnit / (1 + (config.value?.vat || 0.07))
: pricePerUnit;
const priceDiscountNoVat = priceNoVat * amount - discount;
const rawVatTotal = product[ const rawVatTotal =
props.agentPrice ? 'agentPriceCalcVat' : 'calcVat' c.vat === 0 ? 0 : priceDiscountNoVat * (config.value?.vat || 0.07);
]
? priceDiscountNoVat * (config.value?.vat || 0.07)
: 0;
return precisionRound(priceNoVat * amount + rawVatTotal); return precisionRound(priceNoVat * amount + rawVatTotal);
} }
@ -141,7 +114,7 @@ function calcPrice(
<template <template
v-slot:body="props: { v-slot:body="props: {
row: { row: {
product: RequestWork['productService']['product']; product: RequestWork['productService'];
list: RequestWork[]; list: RequestWork[];
}; };
} & Omit<Parameters<QTableSlots['body']>[0], 'row'>" } & Omit<Parameters<QTableSlots['body']>[0], 'row'>"
@ -151,14 +124,14 @@ function calcPrice(
{{ props.rowIndex + 1 }} {{ props.rowIndex + 1 }}
</q-td> </q-td>
<q-td> <q-td>
{{ props.row.product.code }} {{ props.row.product.product.code }}
</q-td> </q-td>
<q-td style="width: 100%" class="text-left"> <q-td style="width: 100%" class="text-left">
<q-avatar class="q-mr-sm" size="md"> <q-avatar class="q-mr-sm" size="md">
<q-img <q-img
class="text-center" class="text-center"
:ratio="1" :ratio="1"
:src="`${baseUrl}/product/${props.row.product.id}/image/${props.row.product.selectedImage}`" :src="`${baseUrl}/product/${props.row.product.id}/image/${props.row.product.product.selectedImage}`"
> >
<template #error> <template #error>
<q-icon <q-icon
@ -169,7 +142,7 @@ function calcPrice(
</template> </template>
</q-img> </q-img>
</q-avatar> </q-avatar>
{{ props.row.product.name }} {{ props.row.product.product.name }}
</q-td> </q-td>
<q-td> <q-td>
{{ props.row.list.length }} {{ props.row.list.length }}
@ -178,7 +151,7 @@ function calcPrice(
{{ {{
formatNumberDecimal( formatNumberDecimal(
calcPricePerUnit(props.row.product) + calcPricePerUnit(props.row.product) +
(props.row.product.calcVat (props.row.product.vat > 0
? calcPricePerUnit(props.row.product) * ? calcPricePerUnit(props.row.product) *
(config?.vat || 0.07) (config?.vat || 0.07)
: 0), : 0),