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";
|
||||
import { isSystem } from "../utils/keycloak";
|
||||
import { isUsedError, notFoundError, relationError } from "../utils/error";
|
||||
import { precisionRound } from "../utils/arithmetic";
|
||||
|
||||
type QuotationCreate = {
|
||||
status?: Status;
|
||||
|
|
@ -332,14 +333,14 @@ export class QuotationController extends Controller {
|
|||
|
||||
const price = list.reduce(
|
||||
(a, c) => {
|
||||
const price = c.pricePerUnit * c.amount;
|
||||
const discount = Math.round(price * (c.discount || 0) * 100) / 100;
|
||||
const vat = Math.round((price - discount) * c.vat * 100) / 100;
|
||||
const price = precisionRound(c.pricePerUnit * c.amount);
|
||||
const discount = precisionRound(price - (c.discount || 0));
|
||||
const vat = precisionRound((price - discount) * c.vat);
|
||||
|
||||
a.totalPrice += price;
|
||||
a.totalDiscount += discount;
|
||||
a.vat += vat;
|
||||
a.finalPrice += price - discount + vat;
|
||||
a.totalPrice = precisionRound(a.totalPrice + price);
|
||||
a.totalDiscount = precisionRound(a.totalDiscount + discount);
|
||||
a.vat = precisionRound(a.vat + vat);
|
||||
a.finalPrice = precisionRound(a.totalPrice - a.totalDiscount + a.vat);
|
||||
|
||||
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