Created modular system for calculators, added soap and candles calculators, universal components, updated backend
80 lines
1.8 KiB
TypeScript
80 lines
1.8 KiB
TypeScript
// Функции расчета для калькулятора мыла
|
|
|
|
export function calculateSoapStep(
|
|
stepId: string,
|
|
values: Record<string, number>
|
|
): number {
|
|
const {
|
|
weight = 0,
|
|
basePrice = 0,
|
|
aromaPrice = 0,
|
|
aromaWeight = 0,
|
|
pigmentPrice = 0,
|
|
pigmentWeight = 0,
|
|
moldPrice = 0,
|
|
box = 0,
|
|
filler = 0,
|
|
ribbon = 0,
|
|
label = 0,
|
|
} = values;
|
|
|
|
switch (stepId) {
|
|
case 'base':
|
|
if (weight <= 0 || basePrice <= 0) return 0;
|
|
return (weight / 1000) * basePrice;
|
|
|
|
case 'aroma':
|
|
if (weight <= 0 || aromaWeight <= 0 || aromaPrice <= 0) return 0;
|
|
return ((weight * 0.01) / aromaWeight) * aromaPrice;
|
|
|
|
case 'pigment':
|
|
if (weight <= 0 || pigmentWeight <= 0 || pigmentPrice <= 0) return 0;
|
|
return ((weight * 0.005) / pigmentWeight) * pigmentPrice;
|
|
|
|
case 'mold':
|
|
if (moldPrice <= 0) return 0;
|
|
return moldPrice / 100;
|
|
|
|
case 'packaging':
|
|
return (box || 0) + (filler || 0) + (ribbon || 0) + (label || 0);
|
|
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
export function calculateSoapSubtotal(
|
|
subtotalId: string,
|
|
values: Record<string, number>,
|
|
steps: Record<string, number>
|
|
): number {
|
|
switch (subtotalId) {
|
|
case 'operational':
|
|
const subtotal =
|
|
(steps.base || 0) +
|
|
(steps.aroma || 0) +
|
|
(steps.pigment || 0) +
|
|
(steps.mold || 0) +
|
|
(steps.packaging || 0);
|
|
return subtotal * 0.05;
|
|
|
|
case 'total':
|
|
return (
|
|
(steps.base || 0) +
|
|
(steps.aroma || 0) +
|
|
(steps.pigment || 0) +
|
|
(steps.mold || 0) +
|
|
(steps.packaging || 0) +
|
|
(steps.operational || 0)
|
|
);
|
|
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
export function round(val: number): number {
|
|
return Math.round(val * 10) / 10;
|
|
}
|
|
|