From 2b147ef1dcf21dfdb05837e3a55483edf0d5aedd Mon Sep 17 00:00:00 2001 From: 4aiman Date: Thu, 28 May 2026 23:15:35 +0300 Subject: [PATCH] Fix expenditure showing -- when BMR or profile incomplete MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two issues: 1. expend was null when BMR=null, even if GPS sessions had kcal. Now shows activity kcal alone if BMR unavailable. 2. calcBmr returned null if height/birth_year missing, but user had weight in profile. Now falls back: weight_log -> latestWeight -> profile.weight_kg. Also returns bmr_formula if height missing instead of null. BMR calculation chain: - bmr_estimated (manual override) takes priority - Then formula: needs height + birth_year + weight (from any source) - Then bmr_formula (pre-computed value in profile) - Last resort: null → expenditure shows activity kcal only --- frontend/js/main.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) 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) {