This commit is contained in:
seb
2026-11-01 21:11:38 +01:00
parent aeb79c1acb
commit 833cee5040

View File

@@ -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));
}