|
8 | 8 |
|
9 | 9 |
|
10 | 10 | def validate(report_version: ReportVersion) -> dict[str, ReportValidationError]:
|
| 11 | + """ |
| 12 | + Validates emission allocation data for all facility reports associated with a given report version. |
| 13 | +
|
| 14 | + For each facility report, checks that the sum of allocated emission quantities for each product category |
| 15 | + matches the reported emission total, unless the allocation methodology is "Not applicable". |
| 16 | + Returns a dictionary of errors keyed by facility and emission category if mismatches are found. |
| 17 | +
|
| 18 | + Args: |
| 19 | + report_version (ReportVersion): The report version to validate. |
| 20 | +
|
| 21 | + Returns: |
| 22 | + dict[str, ReportValidationError]: A dictionary of validation errors, keyed by facility and category. |
| 23 | + """ |
11 | 24 |
|
12 | 25 | facility_reports = FacilityReport.objects.filter(report_version=report_version)
|
13 | 26 |
|
14 | 27 | errors = {}
|
15 | 28 |
|
16 | 29 | for fr in facility_reports:
|
17 | 30 | emission_data = ReportEmissionAllocationService.get_emission_allocation_data(report_version.id, fr.facility_id)
|
18 |
| - for category in emission_data.report_product_emission_allocations: |
19 |
| - total_allocated = sum(product.allocated_quantity for product in category.products) |
20 |
| - if total_allocated != category.emission_total: |
21 |
| - errors[ |
22 |
| - f"facility_{fr.facility_id}_category_{category.emission_category_id}_allocation_mismatch" |
23 |
| - ] = ReportValidationError( |
24 |
| - Severity.ERROR, |
25 |
| - f"Allocated emissions for {fr.facility_name} in '{category.emission_category_name}' category do not match reported emissions.", |
26 |
| - ) |
| 31 | + if emission_data.allocation_methodology != "Not applicable": |
| 32 | + for category in emission_data.report_product_emission_allocations: |
| 33 | + total_allocated = sum(product.allocated_quantity for product in category.products) |
| 34 | + if total_allocated != category.emission_total: |
| 35 | + errors[ |
| 36 | + f"facility_{fr.facility_id}_category_{category.emission_category_id}_allocation_mismatch" |
| 37 | + ] = ReportValidationError( |
| 38 | + Severity.ERROR, |
| 39 | + f"Allocated emissions for {fr.facility_name} in '{category.emission_category_name}' category do not match reported emissions.", |
| 40 | + ) |
27 | 41 | return errors
|
0 commit comments