From 0b52c64a9acb8347b7300fc8b42f78285a0d1142 Mon Sep 17 00:00:00 2001 From: michaelglenister Date: Fri, 27 Sep 2024 11:24:08 +0200 Subject: [PATCH 1/3] Adding sub total codes --- .../management/commands/make_sub_totals.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/municipal_finance/management/commands/make_sub_totals.py b/municipal_finance/management/commands/make_sub_totals.py index a9fc512e1..c4f0b46c7 100644 --- a/municipal_finance/management/commands/make_sub_totals.py +++ b/municipal_finance/management/commands/make_sub_totals.py @@ -25,6 +25,19 @@ def handle(self, *args, **options): "0170", "0180", ], + "0310": [ + "0210", + "0220", + "0230", + "0240", + "0250", + "0260", + "0270", + "0280", + "0290", + "0300", + ], + "0320": ["0190", "0310"], "0430": ["0350", "0360", "0370", "0380", "0390", "0400", "0410", "0420"], "0490": ["0450", "0460", "0470", "0480"], "0500": ["0430", "0490"], From 1fabeb9c0b72f28c09862648cfdbc2ad3c436a34 Mon Sep 17 00:00:00 2001 From: michaelglenister Date: Fri, 27 Sep 2024 11:55:16 +0200 Subject: [PATCH 2/3] Handle existing rows --- .../management/commands/make_sub_totals.py | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/municipal_finance/management/commands/make_sub_totals.py b/municipal_finance/management/commands/make_sub_totals.py index c4f0b46c7..17f6717af 100644 --- a/municipal_finance/management/commands/make_sub_totals.py +++ b/municipal_finance/management/commands/make_sub_totals.py @@ -77,13 +77,16 @@ def handle(self, *args, **options): 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, - ) + try: + 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, + ) + except (ValueError, TypeError) as e: + pass From 524d2ffe003799c0ffb7d7df275ed45119fe7985 Mon Sep 17 00:00:00 2001 From: michaelglenister Date: Fri, 27 Sep 2024 11:59:38 +0200 Subject: [PATCH 3/3] Add default field to update --- .../management/commands/make_sub_totals.py | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/municipal_finance/management/commands/make_sub_totals.py b/municipal_finance/management/commands/make_sub_totals.py index 17f6717af..abeb9e8cf 100644 --- a/municipal_finance/management/commands/make_sub_totals.py +++ b/municipal_finance/management/commands/make_sub_totals.py @@ -77,16 +77,15 @@ def handle(self, *args, **options): item_code = FinancialPositionItemsV2.objects.get(code=item_key) if sub_total != 0: - try: - 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, - ) - except (ValueError, TypeError) as e: - pass + 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", + defaults={ + 'amount': sub_total, + } + )