Exclude sub-meters from category subtotals in Estimates/Reports/internal API

Water Softener is configured as a submeter (parent_meter_id) of Main Hotel
Water, but estimates.js, reports.js, and internal.js's cost + estimate
routes summed every active meter flatly regardless of parent/child, double-
counting the softener's consumption on top of the main meter's own reading.

Sub-meter rows are still shown individually (useful for diagnostics), just
excluded from the category subtotal/total sums, with a 'submeter' badge on
the row explaining why. Fixes the water figures on Estimates, Reports, and
downstream consumers of internal.js (Directors report, Weekly Actuals).
This commit is contained in:
jtricerolph 2026-08-17 09:56:02 +00:00
parent d6a787e244
commit d223592aec
6 changed files with 72 additions and 24 deletions

View file

@ -7,7 +7,7 @@ const VALID_WINDOWS = [7, 14, 30]
async function estimateForMeter(meter, periodStart, periodEnd, windowDays) {
const estimate = await getMeterEstimateForPeriod(meter.id, periodStart, periodEnd, windowDays)
return {
meter_id: meter.id, meter_name: meter.name,
meter_id: meter.id, meter_name: meter.name, parent_meter_id: meter.parent_meter_id,
category_id: meter.category_id, category_name: meter.category_name, unit_label: meter.unit_label,
...estimate,
}
@ -29,7 +29,7 @@ export async function estimateRoutes(app) {
if (category_id) { params.push(category_id); conditions.push(`m.category_id = $${params.length}`) }
params.push(globalDefault)
const { rows: meters } = await pool.query(
`SELECT m.id, m.name, m.category_id, c.name AS category_name, c.unit_label,
`SELECT m.id, m.name, m.category_id, m.parent_meter_id, c.name AS category_name, c.unit_label,
COALESCE(c.estimate_trailing_days, $${params.length}) AS trailing_days
FROM meters m JOIN meter_categories c ON c.id = m.category_id
WHERE ${conditions.join(' AND ')}`,
@ -42,7 +42,12 @@ export async function estimateRoutes(app) {
results.push(await estimateForMeter(m, start, end, windowDays))
}
const totals = results.reduce((acc, r) => ({
// Sub-meters (parent_meter_id set) measure a subset of their parent's own
// reading — e.g. the water softener sits inline on the main supply — so
// their consumption/cost is already counted via the parent and must be
// excluded here to avoid double-counting. Still returned as their own row
// above so the projection is visible per-device.
const totals = results.filter(r => !r.parent_meter_id).reduce((acc, r) => ({
total_pence: acc.total_pence + r.total_pence,
usage_cost_pence: acc.usage_cost_pence + r.usage_cost_pence,
standing_cost_pence: acc.standing_cost_pence + r.standing_cost_pence,

View file

@ -38,7 +38,7 @@ async function categoryCostBreakdown(start, end) {
}
for (const cat of categories) {
const { rows: meters } = await pool.query('SELECT id, name FROM meters WHERE category_id = $1 AND active = TRUE', [cat.id])
const { rows: meters } = await pool.query('SELECT id, name, parent_meter_id FROM meters WHERE category_id = $1 AND active = TRUE', [cat.id])
if (meters.length === 0) continue // no meters yet (e.g. Water/Oil seeded but unused) — nothing to report
const catTotals = {
@ -48,18 +48,26 @@ async function categoryCostBreakdown(start, end) {
for (const m of meters) {
const cost = await getMeterCostForPeriod(m.id, start, end)
if (cost.has_data) catTotals.consumption += cost.consumption
catTotals.usage_cost_pence += cost.usage_cost_pence
catTotals.standing_cost_pence += cost.standing_cost_pence
catTotals.ccl_cost_pence += cost.ccl_cost_pence
catTotals.rab_levy_cost_pence += cost.rab_levy_cost_pence
catTotals.metering_cost_pence += cost.metering_cost_pence
catTotals.other_charges_cost_pence += cost.other_charges_cost_pence
catTotals.vat_pence += cost.vat_pence
catTotals.total_pence += cost.total_pence
// Sub-meters (parent_meter_id set) measure a subset of their parent's
// own reading (e.g. the water softener sits inline on the main
// supply) — already counted via the parent, so excluded from the
// category totals to avoid double-counting. Still returned as its own
// row below for callers that want meter-level detail.
if (!m.parent_meter_id) {
if (cost.has_data) catTotals.consumption += cost.consumption
catTotals.usage_cost_pence += cost.usage_cost_pence
catTotals.standing_cost_pence += cost.standing_cost_pence
catTotals.ccl_cost_pence += cost.ccl_cost_pence
catTotals.rab_levy_cost_pence += cost.rab_levy_cost_pence
catTotals.metering_cost_pence += cost.metering_cost_pence
catTotals.other_charges_cost_pence += cost.other_charges_cost_pence
catTotals.vat_pence += cost.vat_pence
catTotals.total_pence += cost.total_pence
}
meterRows.push({
meter_id: m.id, meter_name: m.name,
meter_id: m.id, meter_name: m.name, parent_meter_id: m.parent_meter_id,
category_id: cat.id, category: cat.key, category_name: cat.name, unit_label: cat.unit_label,
consumption: cost.consumption, has_data: cost.has_data,
status: cost.status, as_of_date: cost.as_of_date,
@ -184,18 +192,26 @@ export async function internalRoutes(app) {
for (const cat of categories) {
const windowDays = cat.estimate_trailing_days || globalDefault
const { rows: meters } = await pool.query('SELECT id, name FROM meters WHERE category_id = $1 AND active = TRUE', [cat.id])
const { rows: meters } = await pool.query('SELECT id, name, parent_meter_id FROM meters WHERE category_id = $1 AND active = TRUE', [cat.id])
let catConsumption = 0
let catTotalPence = 0
for (const m of meters) {
const estimate = await getMeterEstimateForPeriod(m.id, start, end, windowDays)
remainingDays = estimate.remaining_days
if (estimate.projected_consumption != null) catConsumption += estimate.projected_consumption
catTotalPence += estimate.total_pence
// Sub-meters (parent_meter_id set) project a subset of their parent's
// own consumption (e.g. the water softener sits inline on the main
// supply) — already counted via the parent, so excluded from the
// category total to avoid double-counting. Still returned as its own
// row below for callers that want meter-level detail.
if (!m.parent_meter_id) {
if (estimate.projected_consumption != null) catConsumption += estimate.projected_consumption
catTotalPence += estimate.total_pence
}
meterRows.push({
meter_id: m.id, meter_name: m.name,
meter_id: m.id, meter_name: m.name, parent_meter_id: m.parent_meter_id,
category_id: cat.id, category: cat.key, category_name: cat.name, unit_label: cat.unit_label,
trailing_window_days: estimate.trailing_window_days, daily_rate: estimate.daily_rate,
actual_to_date: estimate.actual_to_date, remaining_days: estimate.remaining_days,

View file

@ -16,7 +16,7 @@ export async function reportRoutes(app) {
let where = ''
if (category_id) { params.push(category_id); where = 'WHERE m.category_id = $1' }
const { rows: meters } = await pool.query(
`SELECT m.id, m.name, m.category_id, c.name AS category_name, c.unit_label
`SELECT m.id, m.name, m.category_id, m.parent_meter_id, c.name AS category_name, c.unit_label
FROM meters m JOIN meter_categories c ON c.id = m.category_id
${where} ORDER BY c.sort_order, m.name`,
params
@ -26,13 +26,18 @@ export async function reportRoutes(app) {
for (const m of meters) {
const cost = await getMeterCostForPeriod(m.id, start, end)
results.push({
meter_id: m.id, meter_name: m.name,
meter_id: m.id, meter_name: m.name, parent_meter_id: m.parent_meter_id,
category_id: m.category_id, category_name: m.category_name, unit_label: m.unit_label,
...cost,
})
}
const totals = results.reduce((acc, r) => ({
// Sub-meters (parent_meter_id set) measure a subset of their parent's own
// reading (e.g. the water softener sits inline on the main supply), so
// their consumption/cost is already counted via the parent — excluded
// here to avoid double-counting; still shown as their own row above, and
// separately compared against their parent in the rollup report below.
const totals = results.filter(r => !r.parent_meter_id).reduce((acc, r) => ({
consumption: acc.consumption + (r.consumption || 0),
usage_cost_pence: acc.usage_cost_pence + r.usage_cost_pence,
standing_cost_pence: acc.standing_cost_pence + r.standing_cost_pence,