From 49ffe4fbedc2fdb4466e915522e350f106c02bfa Mon Sep 17 00:00:00 2001 From: jdolkowski Date: Fri, 21 Jul 2023 12:31:44 +0200 Subject: [PATCH] CM-210: code refactor --- .../benefit_package_base_strategy.py | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/calcrule_social_protection/strategies/benefit_package_base_strategy.py b/calcrule_social_protection/strategies/benefit_package_base_strategy.py index 33df278..f0698ce 100644 --- a/calcrule_social_protection/strategies/benefit_package_base_strategy.py +++ b/calcrule_social_protection/strategies/benefit_package_base_strategy.py @@ -28,20 +28,11 @@ def calculate(cls, calculation, payment_plan, **kwargs): payment = payment_plan_parameters['calculation_rule']['fixed_batch'] advanced_filters_criteria = payment_plan_parameters['advanced_criteria'] for beneficiary in beneficiares: - for criterion in advanced_filters_criteria: - condition = criterion['custom_filter_condition'] - calculated_amount = float(criterion['amount']) - does_amount_apply_for_limitations = criterion['count_to_max'] - if cls._does_beneficiary_meet_condition(beneficiary, condition): - if does_amount_apply_for_limitations: - # TODO: ceiling part - continue - else: - payment += calculated_amount + calculated_payment = cls._calculate_payment(beneficiary, advanced_filters_criteria, payment) additional_params = { f"{cls.BENEFICIARY_TYPE}": beneficiary, - "amount": payment, + "amount": calculated_payment, "user": user } calculation.run_convert( @@ -50,6 +41,20 @@ def calculate(cls, calculation, payment_plan, **kwargs): ) return "Calculation and transformation into bills completed successfully." + @classmethod + def _calculate_payment(cls, beneficiary, advanced_filters_criteria, payment): + for criterion in advanced_filters_criteria: + condition = criterion['custom_filter_condition'] + calculated_amount = float(criterion['amount']) + does_amount_apply_for_limitations = criterion.get('count_to_max', False) + if cls._does_beneficiary_meet_condition(beneficiary, condition): + if does_amount_apply_for_limitations: + # TODO: ceiling part + continue + else: + payment += calculated_amount + return payment + @classmethod def _does_beneficiary_meet_condition(cls, beneficiary, condition): condition_key, condition_value = condition.split("=")