manicanldes-backend/src/costing/cost-math.ts

20 lines
664 B
TypeScript

import { AppError } from '../common/errors/app-error';
import { scaled } from '../procurement/purchase-order-money';
const MAX_CENTS = 999999999999n;
export function formatCost(cents: bigint) {
if (cents > MAX_CENTS) throw new AppError('COST_AMOUNT_LIMIT');
return `${cents / 100n}.${(cents % 100n).toString().padStart(2, '0')}`;
}
export function estimateLineCost(quantity: string, unitCost: string) {
const cents = (scaled(quantity, 3) * scaled(unitCost, 4) + 50000n) / 100000n;
return formatCost(cents);
}
export function sumCosts(amounts: string[]) {
return formatCost(
amounts.reduce((total, amount) => total + scaled(amount, 2), 0n),
);
}