74 lines
2.3 KiB
TypeScript
74 lines
2.3 KiB
TypeScript
/**
|
|
* Formats an integer amount in cents to German Euro representation.
|
|
* Example: 249 -> "2,49 €"
|
|
*/
|
|
export function formatCurrency(cents: number | null | undefined): string {
|
|
if (cents === null || cents === undefined || isNaN(cents)) {
|
|
return '0,00 €';
|
|
}
|
|
return new Intl.NumberFormat('de-DE', {
|
|
style: 'currency',
|
|
currency: 'EUR',
|
|
}).format(cents / 100);
|
|
}
|
|
|
|
/**
|
|
* Calculates stock value in cents.
|
|
* For integer/package units: quantity * packagePriceCents
|
|
* For partial packages: (quantity / packageSize) * packagePriceCents
|
|
*/
|
|
export function calculateStockValueCents(
|
|
quantity: number,
|
|
packageSize: number,
|
|
packagePriceCents: number | null | undefined
|
|
): number {
|
|
if (!packagePriceCents || packagePriceCents <= 0 || quantity <= 0) {
|
|
return 0;
|
|
}
|
|
if (packageSize <= 0) {
|
|
return Math.round(quantity * packagePriceCents);
|
|
}
|
|
const fraction = quantity / packageSize;
|
|
return Math.round(fraction * packagePriceCents);
|
|
}
|
|
|
|
/**
|
|
* Calculates base price (Grundpreis) per 1kg, 1l, 100g, or 1 Stück.
|
|
*/
|
|
export function calculateBasePrice(
|
|
priceCents: number,
|
|
packageSize: number,
|
|
packageUnit: string
|
|
): { basePriceCents: number; basePriceUnit: string } {
|
|
if (packageSize <= 0) {
|
|
return { basePriceCents: priceCents, basePriceUnit: packageUnit };
|
|
}
|
|
|
|
const unitLower = packageUnit.toLowerCase().trim();
|
|
|
|
if (unitLower === 'g' || unitLower === 'gramm') {
|
|
// Standard base price per 1 kg (1000 g)
|
|
const basePriceCents = Math.round((priceCents / packageSize) * 1000);
|
|
return { basePriceCents, basePriceUnit: '1 kg' };
|
|
}
|
|
|
|
if (unitLower === 'ml' || unitLower === 'milliliter') {
|
|
// Standard base price per 1 l (1000 ml)
|
|
const basePriceCents = Math.round((priceCents / packageSize) * 1000);
|
|
return { basePriceCents, basePriceUnit: '1 l' };
|
|
}
|
|
|
|
if (unitLower === 'kg' || unitLower === 'kilogramm') {
|
|
const basePriceCents = Math.round(priceCents / packageSize);
|
|
return { basePriceCents, basePriceUnit: '1 kg' };
|
|
}
|
|
|
|
if (unitLower === 'l' || unitLower === 'liter') {
|
|
const basePriceCents = Math.round(priceCents / packageSize);
|
|
return { basePriceCents, basePriceUnit: '1 l' };
|
|
}
|
|
|
|
const basePriceCents = Math.round(priceCents / packageSize);
|
|
return { basePriceCents, basePriceUnit: `1 ${packageUnit}` };
|
|
}
|