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

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