diff --git a/diagnostic_word/renderers.py b/diagnostic_word/renderers.py index 2d54b5fab..b5d2e38e4 100644 --- a/diagnostic_word/renderers.py +++ b/diagnostic_word/renderers.py @@ -16,6 +16,9 @@ from public_data.domain.impermeabilisation.difference.ImpermeabilisationDifferenceService import ( ImpermeabilisationDifferenceService, ) +from public_data.infra.consommation.progression.export.ConsoByDeterminantExportTableMapper import ( + ConsoByDeterminantExportTableMapper, +) from public_data.infra.consommation.progression.export.ConsoComparisonExportTableMapper import ( ConsoComparisonExportTableMapper, ) @@ -197,7 +200,15 @@ def get_context_data(self) -> Dict[str, Any]: series=annual_total_conso_chart.get_series(), line=False ), "communes_data_table": add_total_line_column(chart_conso_cities.get_series()), - "determinants_data_table": add_total_line_column(det_chart.get_series()), + "determinants_data_table": ConsoByDeterminantExportTableMapper.map( + consommation_progression=PublicDataContainer.consommation_progression_service() + .get_by_land( + land=diagnostic.land_proxy, + start_date=int(diagnostic.analyse_start_date), + end_date=int(diagnostic.analyse_end_date), + ) + .consommation + ), # Target 2031 "target_2031_consumed": target_2031_consumption, "projection_zan_cumulee_ref": round(objective_chart.total_2020, 1), diff --git a/public_data/infra/consommation/progression/export/ConsoByDeterminantExportTableMapper.py b/public_data/infra/consommation/progression/export/ConsoByDeterminantExportTableMapper.py new file mode 100644 index 000000000..98ae231ab --- /dev/null +++ b/public_data/infra/consommation/progression/export/ConsoByDeterminantExportTableMapper.py @@ -0,0 +1,36 @@ +from public_data.domain.consommation.entity import ConsommationCollection + + +class ConsoByDeterminantExportTableMapper: + @staticmethod + def map(consommation_progression: list[ConsommationCollection]): + category_to_attr = { + "Habitat": "habitat", + "Activité": "activite", + "Mixte": "mixte", + "Route": "route", + "Ferré": "ferre", + "Inconnu": "non_reseigne", + "Total": "total", + } + + headers = [str(conso.year) for conso in consommation_progression] + ["Total"] + + rows = [] + for category in category_to_attr: + category_values = [getattr(conso, category_to_attr[category]) for conso in consommation_progression] + category_total = sum(category_values) + # On arrondit ensuite pour ne pas fausser le total + category_values_rounded = [round(value, 2) for value in category_values] + category_total_rounded = round(category_total, 2) + rows.append( + { + "name": category, + "data": category_values_rounded + [category_total_rounded], + } + ) + + return { + "headers": headers, + "rows": rows, + }