refactor: calculate utility
This commit is contained in:
parent
9535774a30
commit
3b1e84b6d8
2 changed files with 12 additions and 7 deletions
|
|
@ -22,6 +22,7 @@ import {
|
||||||
} from "../services/permission";
|
} from "../services/permission";
|
||||||
import { isSystem } from "../utils/keycloak";
|
import { isSystem } from "../utils/keycloak";
|
||||||
import { isUsedError, notFoundError, relationError } from "../utils/error";
|
import { isUsedError, notFoundError, relationError } from "../utils/error";
|
||||||
|
import { precisionRound } from "../utils/arithmetic";
|
||||||
|
|
||||||
type QuotationCreate = {
|
type QuotationCreate = {
|
||||||
status?: Status;
|
status?: Status;
|
||||||
|
|
@ -332,14 +333,14 @@ export class QuotationController extends Controller {
|
||||||
|
|
||||||
const price = list.reduce(
|
const price = list.reduce(
|
||||||
(a, c) => {
|
(a, c) => {
|
||||||
const price = c.pricePerUnit * c.amount;
|
const price = precisionRound(c.pricePerUnit * c.amount);
|
||||||
const discount = Math.round(price * (c.discount || 0) * 100) / 100;
|
const discount = precisionRound(price - (c.discount || 0));
|
||||||
const vat = Math.round((price - discount) * c.vat * 100) / 100;
|
const vat = precisionRound((price - discount) * c.vat);
|
||||||
|
|
||||||
a.totalPrice += price;
|
a.totalPrice = precisionRound(a.totalPrice + price);
|
||||||
a.totalDiscount += discount;
|
a.totalDiscount = precisionRound(a.totalDiscount + discount);
|
||||||
a.vat += vat;
|
a.vat = precisionRound(a.vat + vat);
|
||||||
a.finalPrice += price - discount + vat;
|
a.finalPrice = precisionRound(a.totalPrice - a.totalDiscount + a.vat);
|
||||||
|
|
||||||
return a;
|
return a;
|
||||||
},
|
},
|
||||||
|
|
|
||||||
4
src/utils/arithmetic.ts
Normal file
4
src/utils/arithmetic.ts
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
export function precisionRound(number: number, precision = 2) {
|
||||||
|
var factor = Math.pow(10, precision);
|
||||||
|
return Math.round(number * factor) / factor;
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue