Merge pull request #349 from Hestia-Homes/fix/reporting-carbon-effective-baseline

fix(reporting): negative scenario carbon impact — move carbon fragments to effective baseline
This commit is contained in:
KhalimCK 2026-07-02 17:29:08 +01:00 committed by GitHub
commit 96245a05af
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -77,16 +77,19 @@ export const newApproachJoins = sql`
`;
/**
* Baseline carbon (t CO/yr). New: the lodged (gov-registry) value. Legacy:
* e.co2_emissions. Swap to `effective_co2_emissions_t_per_yr` if the
* modelling-adjusted baseline is wanted instead.
* Baseline carbon (t CO/yr). New: the effective (re-baselined) value the
* canonical "current" per ADR-0002, and the baseline the plans' post_* figures
* were scored against. The lodged value is NULL for predicted-only properties
* (no mirrored estimate is written), which made lodged-based reporting totals
* silently exclude them and drove "carbon saved" negative whenever their
* plans' post carbon counted on the scenario side. Legacy: e.co2_emissions.
*/
export const carbonSql = (e: Alias) =>
sql`CASE WHEN ${isNewApproachSql} THEN bp.lodged_co2_emissions_t_per_yr ELSE ${e}.co2_emissions END`;
sql`CASE WHEN ${isNewApproachSql} THEN bp.effective_co2_emissions_t_per_yr ELSE ${e}.co2_emissions END`;
/** Primary energy intensity (kWh/m²/yr). New: lodged value. Legacy: e.primary_energy_consumption. */
/** Primary energy intensity (kWh/m²/yr). New: effective value (ADR-0002, as carbonSql). Legacy: e.primary_energy_consumption. */
export const energyConsumptionSql = (e: Alias) =>
sql`CASE WHEN ${isNewApproachSql} THEN bp.lodged_primary_energy_intensity_kwh_per_m2_yr ELSE ${e}.primary_energy_consumption END`;
sql`CASE WHEN ${isNewApproachSql} THEN bp.effective_primary_energy_intensity_kwh_per_m2_yr ELSE ${e}.primary_energy_consumption END`;
/**
* Baseline SAP score. New: the lodged (gov-registry) score from
@ -566,9 +569,9 @@ export async function resolveConditionReport(
* effective score away from the lodged (gov-registry) EPC, so showing lodged as
* "current" misrepresents a plan's uplift e.g. lodged C/78 vs an effective
* baseline of C/71 with a post-retrofit C/71.4 reads as flat instead of the real
* improvement. The set-based reporting fragments (`sapSql`, `epcBandSql`,
* `carbonSql`, ) deliberately stay on `lodged_*` to keep portfolio/reporting
* totals matched to the gov register.
* improvement. The set-based reporting fragments (`effectiveSapSql`,
* `carbonSql`, `energyConsumptionSql`, ) read `effective_*` too (ADR-0002);
* only the dedicated "Lodged EPC" surfaces use the lodged fragments.
*/
export async function resolvePropertyHeadline(
propertyId: bigint,