From 68dab06b0a24089878e79f5d2d9869068117beee Mon Sep 17 00:00:00 2001 From: jtricerolph Date: Mon, 12 Oct 2026 12:00:15 +0000 Subject: [PATCH] Fix zero consumption for open-period reports with sparse readings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit getPeriodConsumption()'s fallback branch picked the "first" baseline reading via readingOnOrAfter(periodStart) before readingOnOrBefore(), so a manually-read meter with only one reading entered this month collapsed first and last onto the same row and reported no_data/£0, even with a valid prior-month baseline available. Prefer the pre-period reading first. Co-Authored-By: Claude Sonnet 5 --- backend/src/lib/cost-calc.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/lib/cost-calc.js b/backend/src/lib/cost-calc.js index d23d6ec..67c5e2c 100644 --- a/backend/src/lib/cost-calc.js +++ b/backend/src/lib/cost-calc.js @@ -136,7 +136,7 @@ export async function getPeriodConsumption(meterId, periodStart, periodEnd) { const endPoint = await interpolatedValueAtDate(meterId, periodEnd) if (!startPoint || !endPoint) { - const first = (await readingOnOrAfter(meterId, periodStart)) || (await readingOnOrBefore(meterId, periodStart)) + const first = (await readingOnOrBefore(meterId, periodStart)) || (await readingOnOrAfter(meterId, periodStart)) const last = await readingOnOrBefore(meterId, periodEnd) const hasData = !!(first && last && last.reading_date > first.reading_date) const basis = classifyBasis(hasData, periodStart, periodEnd, first, last)