From 63b19145286756a2cb83c87957d060ab112c99a9 Mon Sep 17 00:00:00 2001 From: michaelglenister Date: Thu, 26 Sep 2024 18:27:49 +0200 Subject: [PATCH 1/2] Calculating subtotals for finpos --- .../management/commands/make_sub_totals.py | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 municipal_finance/management/commands/make_sub_totals.py diff --git a/municipal_finance/management/commands/make_sub_totals.py b/municipal_finance/management/commands/make_sub_totals.py new file mode 100644 index 000000000..9092dff5a --- /dev/null +++ b/municipal_finance/management/commands/make_sub_totals.py @@ -0,0 +1,62 @@ +from django.core.management.base import BaseCommand, CommandError + +from municipal_finance.models import ( + FinancialPositionFactsV2, + FinancialPositionItemsV2, + AmountTypeV2, +) +from scorecard.models import MunicipalityProfile + + +# This is supposed to be a temporary fix to add sub-totals to the cubes + + +class Command(BaseCommand): + help = "Make sub totals" + + def handle(self, *args, **options): + total_items = {"0190": [], "0200": []} + + year_list = ["2018", "2019", "2020", "2021", "2022"] + + demarcation_codes = MunicipalityProfile.objects.values_list( + "demarcation_code", flat=True + ) + demarcation_codes_list = list(demarcation_codes) + # demarcation_codes_list = ["BUF", "TSH"] + + amount_types = AmountTypeV2.objects.values_list("id", flat=True) + amount_types_list = list(amount_types) + # amount_types_list = ['1','2',] + + for fin_year in year_list: + for item_key in total_items: + for muni_code in demarcation_codes_list: + for amount_type in amount_types_list: + sub_total = 0 + for item in total_items[item_key]: + finpos = FinancialPositionFactsV2.objects.filter( + financial_year=fin_year, + item_id__code=item, + demarcation_code=muni_code, + amount_type=amount_type, + ) + if finpos: + if finpos[0].amount: + sub_total += finpos[0].amount + print(finpos[0].amount) + + amount_type_model = AmountTypeV2.objects.get(id=amount_type) + item_code = FinancialPositionItemsV2.objects.get(code=item_key) + + if sub_total != 0: + FinancialPositionFactsV2.objects.update_or_create( + financial_year=fin_year, + financial_period=fin_year, + period_code=f"{fin_year}{amount_type_model.code}", + item_id=item_code.id, + demarcation_code=muni_code, + amount_type=amount_type_model, + period_length="year", + amount=sub_total, + ) From 99bfb64864fa23c20d1fc7e872ba26bfaa5ae546 Mon Sep 17 00:00:00 2001 From: michaelglenister Date: Fri, 27 Sep 2024 10:52:16 +0200 Subject: [PATCH 2/2] Adding sub-totals to finpos --- .../management/commands/make_sub_totals.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/municipal_finance/management/commands/make_sub_totals.py b/municipal_finance/management/commands/make_sub_totals.py index 9092dff5a..a9fc512e1 100644 --- a/municipal_finance/management/commands/make_sub_totals.py +++ b/municipal_finance/management/commands/make_sub_totals.py @@ -15,7 +15,22 @@ class Command(BaseCommand): help = "Make sub totals" def handle(self, *args, **options): - total_items = {"0190": [], "0200": []} + total_items = { + "0190": [ + "0120", + "0130", + "0140", + "0150", + "0160", + "0170", + "0180", + ], + "0430": ["0350", "0360", "0370", "0380", "0390", "0400", "0410", "0420"], + "0490": ["0450", "0460", "0470", "0480"], + "0500": ["0430", "0490"], + "0510": ["0320", "0500"], + "0560": ["0530", "0540", "0550"], + } year_list = ["2018", "2019", "2020", "2021", "2022"] @@ -44,7 +59,6 @@ def handle(self, *args, **options): if finpos: if finpos[0].amount: sub_total += finpos[0].amount - print(finpos[0].amount) amount_type_model = AmountTypeV2.objects.get(id=amount_type) item_code = FinancialPositionItemsV2.objects.get(code=item_key)