since3-portal/modules/admin/lib/internal-costs.ts
2026-10-11 18:22:32 +02:00

30 lines
No EOL
1.2 KiB
TypeScript

import { formatCurrency } from "@/modules/shared/lib/invoice-math";
export const PLANS = ["free", "paid"] as const
export const BILLING_CYCLES = ["monthly", "yearly", "once"] as const
export type Plan = (typeof PLANS)[number]
export type BillingCycle = (typeof BILLING_CYCLES)[number]
export const BILLING_LABELS: Record<BillingCycle, { label: string; suffix: string }> = {
monthly: { label: "Monatlich", suffix: "/ Monat" },
yearly: { label: "Jährlich", suffix: "/ Jahr" },
once: { label: "Einmalig", suffix: "einmalig" },
}
type Cost = { plan: Plan; price: number | null; billing_cycle: BillingCycle | null }
export function formatCost(cost: Cost) {
if (cost.plan === "free" || cost.price === null || cost.billing_cycle === null) return "Kostenlos"
return `${formatCurrency(cost.price)} ${BILLING_LABELS[cost.billing_cycle].suffix}`
}
// Laufende Kosten pro Monat: jährliche Beträge anteilig, einmalige zählen nicht
export function monthlyCost(costs: Cost[]) {
return costs.reduce((sum, cost) => {
if (cost.plan !== "paid" || cost.price === null) return sum
if (cost.billing_cycle === "monthly") return sum + cost.price
if (cost.billing_cycle === "yearly") return sum + cost.price / 12
return sum
}, 0)
}