DoSoapCalc/frontend/lib/calc.ts
2025-06-04 02:46:45 +03:00

54 lines
1.2 KiB
TypeScript

// lib/calc.ts
type Input = {
weight: number;
basePrice: number;
aromaPrice: number;
aromaWeight: number;
pigmentPrice: number;
pigmentWeight: number;
moldPrice: number;
packaging: {
box: number;
filler: number;
ribbon: number;
label: number;
};
};
export function calculateTotal(data: Input) {
const {
weight,
basePrice,
aromaPrice,
aromaWeight,
pigmentPrice,
pigmentWeight,
moldPrice,
packaging,
} = data;
const base = (weight / 1000) * basePrice;
const aroma = ((weight * 0.01) / aromaWeight) * aromaPrice;
const pigment = ((weight * 0.005) / pigmentWeight) * pigmentPrice;
const mold = moldPrice / 100;
const packagingCost = packaging.box + packaging.filler + packaging.ribbon + packaging.label;
const subtotal = base + aroma + pigment + mold + packagingCost;
const operational = subtotal * 0.05;
const total = subtotal + operational;
return {
base: round(base),
aroma: round(aroma),
pigment: round(pigment),
mold: round(mold),
packaging: round(packagingCost),
operational: round(operational),
total: round(total),
};
}
function round(val: number) {
return Math.round(val * 10) / 10;
}