From 84a93e04e72223d47c5d170b86b6d44e7654e000 Mon Sep 17 00:00:00 2001 From: Patrick Delcroix Date: Thu, 28 Sep 2023 11:41:30 +0200 Subject: [PATCH] use ORM for value calculation + fix test (#74) --- policy/test_values.py | 20 +++++++------ policy/values.py | 66 +++++++++++++++++++++++++++---------------- 2 files changed, 53 insertions(+), 33 deletions(-) diff --git a/policy/test_values.py b/policy/test_values.py index 5bfd4fb..7fe4636 100644 --- a/policy/test_values.py +++ b/policy/test_values.py @@ -6,13 +6,15 @@ from insuree.models import Relation from .values import * from .test_helpers import create_test_policy - +from core.apps import CoreConfig +from dateutil.relativedelta import relativedelta class PolicyValuesTestCase(TestCase): + test_child_dob=py_datetime.date.today()- relativedelta(years= CoreConfig.age_of_majority-2) def test_new_policy_basis(self): head_insuree = create_test_insuree(with_family=True, custom_props={"dob": core.datetime.date(1985, 5, 5)}) spouse = Relation.objects.get(id=8) - create_test_insuree( + create_test_insuree( with_family = False, custom_props={ "dob": core.datetime.date(1989, 9, 9), "relationship": spouse, @@ -39,9 +41,9 @@ def test_new_policy_basis(self): # let's add a child and shift date to 1st of a month child = Relation.objects.get(id=4) - create_test_insuree( + create_test_insuree(with_family = False, custom_props={ - "dob": core.datetime.date(2020, 2, 20), + "dob": self.test_child_dob, "relationship": child, "family": head_insuree.family }) @@ -56,7 +58,7 @@ def test_new_policy_basis(self): def test_new_policy_lump_sum_and_cycles(self): head_insuree = create_test_insuree(with_family=True, custom_props={"dob": core.datetime.date(1985, 5, 5)}) spouse = Relation.objects.get(id=8) - create_test_insuree( + create_test_insuree( with_family = False, custom_props={ "dob": core.datetime.date(1989, 9, 9), "relationship": spouse, @@ -89,9 +91,9 @@ def test_new_policy_lump_sum_and_cycles(self): # let's add a child (outside threshold) and shift date in 1st cycle grace period child = Relation.objects.get(id=4) - create_test_insuree( + create_test_insuree( with_family = False, custom_props={ - "dob": core.datetime.date(2020, 2, 20), + "dob": self.test_child_dob, "relationship": child, "family": head_insuree.family }) @@ -106,7 +108,7 @@ def test_new_policy_lump_sum_and_cycles(self): def test_new_policy_admin_period_max_members_insurance_period(self): head_insuree = create_test_insuree(with_family=True, custom_props={"dob": core.datetime.date(1985, 5, 5)}) spouse = Relation.objects.get(id=8) - create_test_insuree( + create_test_insuree(with_family = False, custom_props={ "dob": core.datetime.date(1989, 9, 9), "relationship": spouse, @@ -142,7 +144,7 @@ def test_new_policy_admin_period_max_members_insurance_period(self): child = Relation.objects.get(id=4) create_test_insuree( custom_props={ - "dob": core.datetime.date(2020, 2, 20), + "dob": self.test_child_dob, "relationship": child, "family": head_insuree.family }) diff --git a/policy/values.py b/policy/values.py index 52bb687..b96d894 100644 --- a/policy/values.py +++ b/policy/values.py @@ -1,9 +1,11 @@ from django.utils.translation import gettext as _ +from django.db.models import Q,Count import datetime as py_datetime from decimal import * -from core.apps import CoreConfig from .models import Policy +from core.apps import CoreConfig +from dateutil.relativedelta import relativedelta def cycle_start(product, cycle, ref_date): c = getattr(product, "start_cycle_%s" % (cycle + 1), None) @@ -68,31 +70,47 @@ def family_counts(product, family): other_children = 0 extra_children = 0 total = 0 - # sad, but can't get the limit inside the prefetch - # product.max_members is NOT NULL (but can be 0) - for member in family.members.all()[:product.max_members]: - total += 1 - age = member.age() - if age >= CoreConfig.age_of_majority and member.relationship_id != 7: - adults += 1 - elif age >= CoreConfig.age_of_majority: - other_adults += 1 - elif member.relationship_id != 7: - children += 1 - else: - other_children += 1 - if product.threshold: - extra_adults = max(0, adults - product.threshold) - extra_children = max(0, children - (product.threshold - adults + extra_adults)) + date_threshold = py_datetime.date.today()- relativedelta(years= CoreConfig.age_of_majority) + counts= family.members.filter( + Q(validity_to__isnull=True), + ).aggregate( + adults=Count('id',filter=Q(Q(dob__lt=date_threshold) & ~Q(relationship_id=7))), + children=Count('id', filter=Q(Q(dob__gte=date_threshold) & ~Q(relationship_id=7))), + other_children=Count('id', filter=Q(relationship_id=7, dob__gte=date_threshold)), + other_adults=Count('id', filter=Q(relationship_id=7, dob__lt=date_threshold)) + ) + adults = counts['adults'] + children = counts['children'] + other_children = counts['other_children'] + other_adults = counts['other_adults'] + + over_children = 0 + over_adults = 0 + + if product.max_members: + over_adults = max(0,adults - product.max_members) + over_children = max(0,adults + children - over_adults - product.max_members) + over_other_children = max(0,adults + children + other_children -over_adults -over_children- product.max_members) + over_other_adults = max(0,adults + other_adults + children + other_children -over_adults - over_other_children - over_children - product.max_members) + + # remove over from count + children -= over_children + adults -= over_adults + other_children -= over_other_children + other_adults -= over_other_adults + if product.threshold: + extra_adults = max(0, adults - product.threshold) + extra_children = max(0,children + adults - extra_adults - product.threshold) + return { - "adults": adults, - "extra_adults": extra_adults, - "other_adults": other_adults, - "children": children, - "extra_children": extra_children, - "other_children": other_children, - "total": total, + "adults":adults -extra_adults, # adult part of the "lump sum" + "extra_adults": extra_adults, # adult not part of the "lump sum" because of threshold + "other_adults": other_adults , # adult never of the "lump sum" + "children": children-extra_children, # children part of the "lump sum" + "extra_children": extra_children, # children never part of the "lump sum" because of threshold + "other_children": other_children,# children never part of the "lump sum" + "total": adults + other_adults + children + other_children, }