Fix zero consumption for open-period reports with sparse readings

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 <noreply@anthropic.com>
This commit is contained in:
jtricerolph 2026-10-12 12:00:15 +00:00
parent fc15b48527
commit 68dab06b0a

View file

@ -136,7 +136,7 @@ export async function getPeriodConsumption(meterId, periodStart, periodEnd) {
const endPoint = await interpolatedValueAtDate(meterId, periodEnd) const endPoint = await interpolatedValueAtDate(meterId, periodEnd)
if (!startPoint || !endPoint) { 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 last = await readingOnOrBefore(meterId, periodEnd)
const hasData = !!(first && last && last.reading_date > first.reading_date) const hasData = !!(first && last && last.reading_date > first.reading_date)
const basis = classifyBasis(hasData, periodStart, periodEnd, first, last) const basis = classifyBasis(hasData, periodStart, periodEnd, first, last)