From 833cee50407e03b83df6b03739fa0f46cbedf688 Mon Sep 17 00:00:00 2001 From: seb Date: Sun, 1 Nov 2026 21:11:38 +0100 Subject: [PATCH] u --- src/api/routes/article/shared.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/api/routes/article/shared.js b/src/api/routes/article/shared.js index 426ad78..3731e0a 100644 --- a/src/api/routes/article/shared.js +++ b/src/api/routes/article/shared.js @@ -127,6 +127,24 @@ export function formatPrice(amount) { return CURRENCY_FORMATTER.format(Number(amount)); } +const BASEPRICE_FORMATTER = new Intl.NumberFormat('de-DE', { + minimumFractionDigits: 2, + maximumFractionDigits: 2, +}); + +/** Formats the price per base unit, e.g. "€ 4,05 / L". Empty when unit data or price is missing. */ +export function formatBasePrice(net, taxRate, article) { + const unit = article?.unit; + const unitamount = Number(article?.unitamount); + const factor = Number(unit?.basesifactor); + if (net == null || !unit || !(unitamount > 0) || !(factor > 0)) return ''; + const baseAmount = unitamount * factor; + const perBase = (net * (1 + taxRate / 100)) / baseAmount; + const label = factor === 1 ? (unit.symbol || unit.basesi) : unit.basesi; + if (!label) return ''; + return `€ ${BASEPRICE_FORMATTER.format(perBase)} / ${label}`; +} + export function interpolateString(str, article) { if (typeof str !== 'string') return str; const basePrice = Array.isArray(article?.prices) && article.prices.length > 0 @@ -144,6 +162,7 @@ export function interpolateString(str, article) { return str .replace(/\{\{name\}\}/gi, article?.name || '') + .replace(/\{\{baseprice\}\}/gi, formatBasePrice(net, taxRate, article)) .replace(/\{\{price\}\}/gi, formatPrice(gross)); }