Skip to content

Commit

Permalink
use ORM for value calculation + fix test (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
delcroip authored and tdethier committed Jul 16, 2024
1 parent 911eb95 commit 84a93e0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 33 deletions.
20 changes: 11 additions & 9 deletions policy/test_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
})
Expand All @@ -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,
Expand Down Expand Up @@ -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
})
Expand All @@ -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,
Expand Down Expand Up @@ -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
})
Expand Down
66 changes: 42 additions & 24 deletions policy/values.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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,
}


Expand Down

0 comments on commit 84a93e0

Please sign in to comment.