fix: calc price
Some checks failed
Spell Check / Spell Check with Typos (push) Failing after 7s

This commit is contained in:
Thanaphon Saengchan 2025-09-15 17:02:25 +07:00
parent 46f8a02188
commit 17b18812d7
5 changed files with 119 additions and 50 deletions

View file

@ -9,6 +9,10 @@ import { defineStore } from 'pinia';
import { api } from 'src/boot/axios';
import { PaginationResult } from 'src/types';
import { RequestWork, RequestWorkStatus } from 'src/stores/request-list/types';
import { precisionRound } from 'src/utils/arithmetic';
import { useConfigStore } from 'src/stores/config';
import { manageAttachment, manageFile } from '../utils';
const ENDPOINT = 'credit-note';
@ -80,6 +84,7 @@ export async function acceptCreditNote(id: string) {
}
export const useCreditNote = defineStore('credit-note-store', () => {
const configStore = useConfigStore();
const data = ref<Data[]>([]);
const page = ref<number>(1);
const pageMax = ref<number>(1);
@ -90,6 +95,75 @@ export const useCreditNote = defineStore('credit-note-store', () => {
[Status.Success]: 0,
});
function getfinalPriceCredit(c: RequestWork[]): {
totalPrice: number;
totalDiscount: number;
vat: number;
vatIncluded: number;
vatExcluded: number;
finalPrice: number;
} {
const price = c.reduce(
(acc, crr) => {
const servicesChargeStepCount =
crr.productService.work?.productOnWork?.find(
(item) => item.productId === crr.productService.productId,
).stepCount;
const successCount = crr.stepStatus.filter(
(item) => item.workStatus === RequestWorkStatus.Completed,
).length;
const vatFactor =
crr.productService.vat > 0 ? (configStore.data?.vat ?? 0.07) : 0;
const price = precisionRound(
crr.productService.pricePerUnit * (1 + vatFactor) -
crr.productService.discount,
);
const vat = crr.productService.product.calcVat
? (price / (1 + vatFactor)) * vatFactor
: 0;
acc.totalPrice = precisionRound(
acc.totalPrice + price + crr.productService.discount,
);
acc.totalDiscount = precisionRound(
acc.totalDiscount + crr.productService.discount,
);
acc.vat = precisionRound(acc.vat + vat);
acc.vatExcluded = crr.productService.product.agentPriceCalcVat
? acc.vatExcluded
: precisionRound(acc.vatExcluded + price / (1 + vatFactor));
if (servicesChargeStepCount && successCount) {
acc.finalPrice = precisionRound(
acc.finalPrice +
price -
crr.productService.product.serviceCharge * successCount,
);
return acc;
}
acc.finalPrice = precisionRound(acc.finalPrice + price);
return acc;
},
{
totalPrice: 0,
totalDiscount: 0,
vat: 0,
vatIncluded: 0,
vatExcluded: 0,
finalPrice: 0,
},
);
return price;
}
return {
data,
page,
@ -97,6 +171,8 @@ export const useCreditNote = defineStore('credit-note-store', () => {
pageSize,
stats,
getfinalPriceCredit,
getCreditNoteStats,
getCreditNote,
getCreditNoteList,

View file

@ -178,6 +178,7 @@ type WorkRelation = {
createdByUserId?: string;
updatedAt: string;
updatedByUserId?: string;
productOnWork?: { productId: string; stepCount: number }[];
};
type ServiceRelation = {