Skip to content

Commit

Permalink
Merge pull request #2 from openimis/feature/CM-210
Browse files Browse the repository at this point in the history
CM-210: calculate the amount of payment based on advanced filters
  • Loading branch information
sniedzielski committed Jul 21, 2023
2 parents 06bea7c + dc648d7 commit b800f10
Showing 1 changed file with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from core.models import User
from core.utils import convert_to_python_value
from invoice.services import BillService
from social_protection.models import BeneficiaryStatus

Expand All @@ -22,13 +23,25 @@ def calculate(cls, calculation, payment_plan, **kwargs):
audit_user_id, start_date, end_date = \
calculation.get_payment_cycle_parameters(**kwargs)
user = User.objects.filter(i_user__id=audit_user_id).first()

# :TODO ticket - at this stage use fixed amount, also skip ceiling part
payment = payment_plan_parameters['calculation_rule']['fixed_batch']
advanced_filters_criteria = payment_plan_parameters['advanced_criteria']
for beneficiary in beneficiares:
# TODO add calculation mechanism once is developed as a part of CM-210
# ticket - at this stage use fixed amount
fixed_amount = payment_plan_parameters['calculation_rule']['fixed_batch']
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

additional_params = {
f"{cls.BENEFICIARY_TYPE}": beneficiary,
"amount": fixed_amount,
"amount": payment,
"user": user
}
calculation.run_convert(
Expand All @@ -37,6 +50,17 @@ def calculate(cls, calculation, payment_plan, **kwargs):
)
return "Calculation and transformation into bills completed successfully."

@classmethod
def _does_beneficiary_meet_condition(cls, beneficiary, condition):
condition_key, condition_value = condition.split("=")
json_key, lookup = condition_key.split('__')[0:2]
parsed_condition_value = convert_to_python_value(condition_value)
if json_key in beneficiary.json_ext:
return cls.BENEFICIARY_OBJECT.objects.filter(
id=beneficiary.id, **{f'json_ext__{json_key}__{lookup}': parsed_condition_value}
).exists()
return False

@classmethod
def convert(cls, payment_plan, **kwargs):
entity = kwargs.get('entity', None)
Expand All @@ -60,4 +84,4 @@ def _convert_entity_to_bill(cls, converter, converter_item, payment_plan, entity
'bill_data': bill,
'bill_data_line': bill_line_items,
'type_conversion': 'beneficiary - bill'
}
}

0 comments on commit b800f10

Please sign in to comment.