diff --git a/backend/src/lib/cost-calc.js b/backend/src/lib/cost-calc.js index e2a5277..d367542 100644 --- a/backend/src/lib/cost-calc.js +++ b/backend/src/lib/cost-calc.js @@ -350,10 +350,17 @@ export async function getMeterEstimateForPeriod(meterId, periodStart, periodEnd, const today = toISODate(new Date()) const asOfDate = today < periodEnd ? today : periodEnd const daysInPeriod = daysInclusive(periodStart, periodEnd) - const remainingDays = Math.max(daysBetween(asOfDate, periodEnd), 0) const { daily_rate } = await getTrailingDailyRate(meterId, windowDays) - const { consumption: actualToDate, has_data } = await getPeriodConsumption(meterId, periodStart, asOfDate) + const { consumption: actualToDate, has_data, last: lastReading } = await getPeriodConsumption(meterId, periodStart, asOfDate) + + // Project from the meter's last actual reading, not from "today" — a meter + // read only every couple of weeks will almost always have its last reading + // sitting days before today, and projecting only from today would silently + // drop that gap from both the actual and projected totals. + const lastKnownDate = lastReading ? toISODate(lastReading.reading_date) : periodStart + const projectionFrom = lastKnownDate > periodStart ? lastKnownDate : periodStart + const remainingDays = Math.max(daysBetween(projectionFrom, periodEnd), 0) let projectedConsumption = null if (daily_rate != null) {