Fix expenditure showing -- when BMR or profile incomplete

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
This commit is contained in:
4aiman 2026-05-28 23:15:35 +03:00
commit 2b147ef1dc

View file

@ -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) {