SPB Git

Repository Public

BreakEvenTool.swift 5.4 KB 73 lines swift

1import Foundation2import SheetCore3import SheetCharts4import FinanceModels5import AnalysisKit67/// Cost–volume–profit break-even analysis.8enum BreakEvenTool {9    static let descriptor: ToolDescriptor = {10        var t = ToolDescriptor(11            id: "finance.breakeven", title: L("Break-Even Analysis"), group: FinGroup.planning, systemImage: "plusminus.circle",12            summary: L("Break-even volume and revenue of a single product from fixed costs, price and variable cost, with target profit, margin of safety and operating leverage."),13            fields: [14                .number("fixed", L("Fixed costs"), 150_000, min: 0, help: L("For the period.")),15                .number("price", L("Price per unit"), 48, min: 0),16                .number("variable", L("Variable cost per unit"), 29, min: 0),17                .number("target", L("Target profit"), 50_000, min: 0, help: L("0 for none.")),18                .number("actual", L("Actual or planned units"), 12_000, min: 0, help: L("0 for none; used for the margin of safety and the operating leverage.")),19            ],20            conventions: [21                L("Contribution margin per unit = price − variable cost; contribution margin ratio = margin / price."),22                L("Break-even units = fixed costs / contribution margin (not defined when the margin is zero or negative); break-even revenue = units × price."),23                L("Units for a target profit = (fixed costs + target profit) / contribution margin."),24                L("Margin of safety = (actual − break-even) / actual; degree of operating leverage = total contribution / operating profit."),25                L("Costs are linear in volume: one product, constant price and unit cost, fixed costs unchanged over the range."),26            ],27            runTitle: L("Compute"),28            run: run)29        t.demo = { _, _ in }30        return t31    }()3233    static func run(_ input: ToolInput) throws -> ToolResult {34        let v = input.values, loc = input.locale35        let target = v.number("target"), actual = v.number("actual")36        let a = FinanceModels.BreakEven.analyze(fixedCosts: v.number("fixed"), pricePerUnit: v.number("price"), variableCostPerUnit: v.number("variable"),37                                                targetProfit: target > 0 ? target : nil, actualUnits: actual > 0 ? actual : nil)38        var metrics = [Metric(L("Contribution margin per unit"), a.contributionMargin, .money)]39        if let u = a.breakEvenUnits { metrics.append(Metric(L("Break-even volume"), u, .fixed(1), detail: L("units"))) }40        if let r = a.breakEvenRevenue { metrics.append(Metric(L("Break-even revenue"), r, .money)) }41        var items: [(String, Value, CellFormat)] = [42            (L("Fixed costs"), .number(a.fixedCosts), .money), (L("Price per unit"), .number(a.pricePerUnit), .money),43            (L("Variable cost per unit"), .number(a.variableCostPerUnit), .money), (L("Contribution margin per unit"), .number(a.contributionMargin), .money),44            (L("Contribution margin ratio"), .finite(a.contributionMarginRatio), .percent(2)),45            (L("Break-even volume (units)"), .finite(a.breakEvenUnits), .fixed(2)), (L("Break-even revenue"), .finite(a.breakEvenRevenue), .money),46        ]47        if target > 0 { items.append((L("Units for the target profit"), .finite(a.unitsForTargetProfit), .fixed(2))) }48        if actual > 0 {49            items.append((L("Margin of safety"), .finite(a.marginOfSafety), .percent(2)))50            items.append((L("Degree of operating leverage"), .finite(a.operatingLeverage), .fixed(3)))51        }52        var blocks: [ResultBlock] = [.metrics(metrics), .table(.keyValue(title: L("Break-even analysis"), header: (L("Quantity"), L("Value")), items))]53        guard let be = a.breakEvenUnits else {54            blocks.append(.warning(L("The contribution margin is zero or negative: no volume covers the fixed costs.")))55            return ToolResult(title: L("Break-Even Analysis"), blocks: blocks, sheetName: L("Break-even"))56        }57        let maxUnits = max(be * 2, actual * 1.25, a.unitsForTargetProfit ?? 0, 1)58        let pts = a.chartData(maxUnits: maxUnits, steps: 40)59        let x = pts.map(\.units)60        var spec = ChartKit.lines([.init(L("Revenue"), pts.map(\.revenue), x: x), .init(L("Total cost"), pts.map(\.totalCost), x: x),61                                   .init(L("Profit"), pts.map(\.profit), x: x, dash: .dash)],62                                  xTitle: L("Units"), yTitle: L("Amount"), yFormat: "#,##0")63        Fin.mark(&spec, vertical: be)64        Fin.mark(&spec, horizontal: 0)65        blocks.append(.chart(ChartBlock(title: L("Cost–volume–profit chart"), spec: spec,66                                        summary: L("Revenue and total cost lines crossing at %@ units, where revenue equals %@.", Fmt.fixed(be, 1, locale: loc),67                                                   Fmt.money(a.breakEvenRevenue ?? 0, locale: loc)), height: 250)))68        blocks.append(.interpretation(L("Profit is zero at %@ units (revenue of %@); each unit beyond that adds %@ of profit.", Fmt.fixed(be, 1, locale: loc),69                                        Fmt.money(a.breakEvenRevenue ?? 0, locale: loc), Fmt.money(a.contributionMargin, locale: loc))))70        return ToolResult(title: L("Break-Even Analysis"), subtitle: L("Break-even at %@ units", Fmt.fixed(be, 1, locale: loc)), blocks: blocks, sheetName: L("Break-even"))71    }72}73
Files main
Menu

Theme