DoSoapCalc/frontend/lib/calculators/candle.ts

73 lines
2.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// lib/calculators/candle.ts
// Калькулятор свечей для frontend (пример)
import type { Calculator, CalculatorField, CalculationResult } from '@/types/calculator';
const fieldSchema: CalculatorField[] = [
{ id: 'candleName', name: 'candleName', label: 'Название свечи', type: 'text', required: true },
{ id: 'weight', name: 'weight', label: 'Вес свечи, г', type: 'number', required: true },
{ id: 'waxPrice', name: 'waxPrice', label: 'Цена воска за 1 кг, руб', type: 'number', required: false },
{ id: 'wickPrice', name: 'wickPrice', label: 'Цена фитиля, руб', type: 'number', required: false },
{ id: 'aromaPrice', name: 'aromaPrice', label: 'Цена отдушки, руб', type: 'number', required: false },
{ id: 'aromaWeight', name: 'aromaWeight', label: 'Фасовка отдушки, г', type: 'number', required: false },
{ id: 'moldPrice', name: 'moldPrice', label: 'Цена формы, руб', type: 'number', required: false },
{ id: 'box', name: 'box', label: 'Упаковка, руб', type: 'number', required: false },
{ id: 'markup', name: 'markup', label: 'Наценка, %', type: 'number', required: false },
];
function round(val: number): number {
return Math.round(val * 10) / 10;
}
function calculate(data: Record<string, any>): CalculationResult {
const {
weight = 0,
waxPrice = 0,
wickPrice = 0,
aromaPrice = 0,
aromaWeight = 1,
moldPrice = 0,
box = 0,
} = data;
const wax = (weight / 1000) * waxPrice;
const wick = wickPrice;
const aroma = ((weight * 0.08) / aromaWeight) * aromaPrice; // 8% отдушки для свечей
const mold = moldPrice / 50; // Амортизация формы на 50 свечей
const packaging = box;
const subtotal = wax + wick + aroma + mold + packaging;
const operational = subtotal * 0.05;
const total = subtotal + operational;
return {
wax: round(wax),
wick: round(wick),
aroma: round(aroma),
mold: round(mold),
packaging: round(packaging),
operational: round(operational),
total: round(total),
};
}
function getRequiredFields(): string[] {
return fieldSchema.filter((f) => f.required).map((f) => f.name);
}
function getNumericFields(): string[] {
return fieldSchema.filter((f) => f.type === 'number').map((f) => f.name);
}
const candleCalculator: Calculator = {
id: 'candle',
name: 'Свеча',
fieldSchema,
calculate,
getRequiredFields,
getNumericFields,
};
export default candleCalculator;