diff --git a/frontend/js/main.js b/frontend/js/main.js index edfa675..d358283 100644 --- a/frontend/js/main.js +++ b/frontend/js/main.js @@ -219,7 +219,8 @@ async function renderToday(container) { const bmr = calcBmr(weights); const intake = Math.round(foodTotals.kcal ?? 0); const actKcal = Math.round(actTotals.kcal ?? 0); - const expend = bmr != null ? bmr + actKcal : null; + // Show expenditure even without BMR if there are activities + const expend = bmr != null ? bmr + actKcal : actKcal > 0 ? actKcal : null; const balance = expend != null ? intake - expend : null; const hasGuessed = food.some(f => f.nutrients_guessed); @@ -3902,13 +3903,19 @@ function createProductSearch(parentEl, onSelect, opts) { function calcBmr(weights) { const p = state.profile; - if (!p.height_cm || !p.birth_year) return null; if (p.bmr_estimated) return parseInt(p.bmr_estimated); - const kg = weights.length ? weights[weights.length - 1].weight_kg - : state.latestWeight?.weight_kg; + // Need height + birth_year for formula + const height = parseInt(p.height_cm); + const birthYear = parseInt(p.birth_year); + if (!height || !birthYear) return parseInt(p.bmr_formula) || null; + // Weight: today's weighing → latest weighing → profile weight + const kg = parseFloat( + (weights.length ? weights[weights.length - 1].weight_kg : null) + ?? state.latestWeight?.weight_kg + ?? p.weight_kg + ); if (!kg) return parseInt(p.bmr_formula) || null; - return BMR.calculateBmr(kg, parseInt(p.height_cm), - BMR.getAge(parseInt(p.birth_year)), p.sex ?? "m"); + return BMR.calculateBmr(kg, height, BMR.getAge(birthYear), p.sex ?? "m"); } function balanceClass(balance) {