Add article pack units with Redux-cached company unit list.

Edit amount/unit on the price tile (pc×1 = no pack unit), manage units in a dialog, and keep the default pc unit non-deletable.
This commit is contained in:
seb
2026-08-01 06:37:47 +02:00
parent 1b8652ceaf
commit 80f093649a
7 changed files with 841 additions and 25 deletions

47
src/utils/units.js Normal file
View File

@@ -0,0 +1,47 @@
/** Company default unit created with every article (`ensureDefaultUnit`). */
export const DEFAULT_UNIT_SI = 'pc';
/**
* Common unit presets for the editor UI (not an API).
* Selecting a preset upserts via PUT /api/unit when missing.
*/
export const COMMON_UNITS = [
{ si: 'pc', basesi: 'pc', basesifactor: 1, symbol: 'Stk', name: 'Stück' },
{ si: 'g', basesi: 'g', basesifactor: 1, symbol: 'g', name: 'Gramm' },
{ si: 'kg', basesi: 'kg', basesifactor: 1, symbol: 'kg', name: 'Kilogramm' },
{ si: 'ml', basesi: 'l', basesifactor: 0.001, symbol: 'ml', name: 'Milliliter' },
{ si: 'l', basesi: 'l', basesifactor: 1, symbol: 'L', name: 'Liter' },
{ si: 'm', basesi: 'm', basesifactor: 1, symbol: 'm', name: 'Meter' },
{ si: 'cm', basesi: 'm', basesifactor: 0.01, symbol: 'cm', name: 'Zentimeter' },
];
export function unitLabel(unit) {
if (!unit) return '';
if (unit.symbol && unit.name) return `${unit.symbol} · ${unit.name}`;
return unit.symbol || unit.name || unit.si;
}
export function findUnitBySi(units, si) {
if (!Array.isArray(units) || si == null) return null;
return units.find((row) => row.si === si) ?? null;
}
/** DB default on create: pc × 1 means “no pack unit” in the UI. */
export function isNoPackUnit(si, amount) {
return si === DEFAULT_UNIT_SI && Number(amount) === 1;
}
export function firstPackUnitSi(units, preferredSi) {
const list = Array.isArray(units) ? units : [];
if (
preferredSi &&
preferredSi !== DEFAULT_UNIT_SI &&
findUnitBySi(list, preferredSi)
) {
return preferredSi;
}
const nonPc = list.find((row) => row.si !== DEFAULT_UNIT_SI);
if (nonPc) return nonPc.si;
if (preferredSi && preferredSi !== DEFAULT_UNIT_SI) return preferredSi;
return '';
}