diff --git a/js/ui/ingredientCard.js b/js/ui/ingredientCard.js index 8b6bde0..2254a55 100644 --- a/js/ui/ingredientCard.js +++ b/js/ui/ingredientCard.js @@ -107,6 +107,47 @@ function formatPackAwareAmount(amount, pantryUnit, packSize, packLabel) { return `${formatQty(qty)} ${unit}`; } +function normalizeQty(value) { + return Math.max(0, Math.round((Number(value) || 0) * 100) / 100); +} + +function formatPreciseQty(n) { + const rounded = Math.round((Number(n) || 0) * 1000) / 1000; + if (Number.isInteger(rounded)) return String(rounded); + return rounded.toFixed(3).replace(/0+$/, '').replace(/\.$/, ''); +} + +function formatPackCount(amount, packSize) { + if (!Number.isFinite(Number(packSize)) || Number(packSize) <= 0) return ''; + return `${formatPreciseQty((Number(amount) || 0) / Number(packSize))} opak.`; +} + +function getQtyStepMeta(def, product = null) { + const productPackSize = Number(product?.packSize); + if (Number.isFinite(productPackSize) && productPackSize > 0) { + return { + step: productPackSize, + usesPackStep: true, + stepLabel: product?.packLabel || formatQtyWithUnit(productPackSize, def.pantryUnit), + }; + } + + const ingredientPackSize = Number(def?.purchasePack?.amount); + if (Number.isFinite(ingredientPackSize) && ingredientPackSize > 0) { + return { + step: ingredientPackSize, + usesPackStep: true, + stepLabel: def.purchasePack?.label || formatQtyWithUnit(ingredientPackSize, def.pantryUnit), + }; + } + + return { + step: pantryQtyStep(def.id), + usesPackStep: false, + stepLabel: '', + }; +} + export function getIngredientCardHTML({ idBase, overlayClass = 'fixed inset-0 z-[70] hidden opacity-0 transition-opacity duration-200 flex items-center justify-center p-5', @@ -153,6 +194,7 @@ export function createIngredientCardController({ idBase, defaultSourceNote = 'Ze ingredientId: null, productId: null, selectedProductId: null, + allowProductSelection: true, sourceNote: defaultSourceNote, onProductChange: null, onAfterChange: null, @@ -234,7 +276,7 @@ export function createIngredientCardController({ idBase, defaultSourceNote = 'Ze } if (backBtn) { - backBtn.classList.toggle('hidden', !(hasProducts && state.productId)); + backBtn.classList.toggle('hidden', !(hasProducts && state.productId && state.allowProductSelection)); } } @@ -250,7 +292,7 @@ export function createIngredientCardController({ idBase, defaultSourceNote = 'Ze const hasProducts = ingredientHasProducts(def.id); const unitScope = def.pantryUnit === 'ml' ? 'na 100 ml' : 'na 100 g'; const hint = product - ? 'dokładne dla produktu' + ? '' : hasProducts ? 'orientacyjnie dla składnika' : ''; @@ -311,12 +353,20 @@ export function createIngredientCardController({ idBase, defaultSourceNote = 'Ze const qty = product ? (getPantryProducts(state.ingredientId, pantry).find((i) => i.productId === state.productId)?.qty || 0) : totalQty; - const step = product ? (product.packSize || pantryQtyStep(state.ingredientId)) : pantryQtyStep(state.ingredientId); + const { step, usesPackStep } = getQtyStepMeta(def, product); const packSize = product?.packSize || def.purchasePack?.amount || 0; const packLabel = product?.packLabel || def.purchasePack?.label || ''; const draftQty = state.stockEditorOpen - ? Math.max(0, Number(state.stockDraftQty ?? qty) || 0) + ? normalizeQty(state.stockDraftQty ?? qty) : qty; + const stockValueLabel = usesPackStep + ? formatPackCount(qty, step) + : formatPackAwareAmount(qty, def.pantryUnit, packSize, packLabel); + const stockSubLabel = usesPackStep ? formatQtyWithUnit(qty, def.pantryUnit) : ''; + const draftInputValue = usesPackStep + ? formatPreciseQty(draftQty / step) + : formatPreciseQty(draftQty); + const draftInputUnit = usesPackStep ? 'opak.' : unit; const actionLabel = state.stockEditorOpen ? 'Anuluj' : 'Zmień'; wrap.innerHTML = ` @@ -324,7 +374,8 @@ export function createIngredientCardController({ idBase, defaultSourceNote = 'Ze
${esc(formatPackAwareAmount(qty, def.pantryUnit, packSize, packLabel))}
+${esc(stockValueLabel)}
+ ${stockSubLabel ? `${esc(stockSubLabel)}
` : ''}${esc(formatQtyWithUnit(draftQty, def.pantryUnit))}
` : ''}${esc(hasShoppingItem ? formatPackAwareAmount(shoppingAmount, def.pantryUnit, packSize, packLabel) : 'Brak na liscie')}
+${esc(shopValueLabel)}
+ ${shopSubLabel ? `${esc(shopSubLabel)}
` : ''}${esc(formatQtyWithUnit(draftQty, def.pantryUnit))}
` : ''}