Skip to content

Commit

Permalink
Merge pull request #418 from dimagi/hy/total-visit-calculation-error
Browse files Browse the repository at this point in the history
Total visit calculation not accounting for org pay, causing inflation in total visit count.
  • Loading branch information
hemant10yadav authored Nov 7, 2024
2 parents 764f7dd + d38a8e1 commit 9457453
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
11 changes: 10 additions & 1 deletion commcare_connect/opportunity/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,16 @@ def approved_visits(self):

@property
def number_of_users(self):
return self.total_budget / self.budget_per_user
if not self.managed:
return self.total_budget / self.budget_per_user

budget_per_user = 0
payment_units = self.paymentunit_set.all()
org_pay = self.managedopportunity.org_pay_per_visit
for pu in payment_units:
budget_per_user += pu.max_total * (pu.amount + org_pay)

return self.total_budget / budget_per_user

@property
def allotted_visits(self):
Expand Down
1 change: 1 addition & 0 deletions commcare_connect/program/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Meta:

class ManagedOpportunityFactory(OpportunityFactory):
program = SubFactory(ProgramFactory)
org_pay_per_visit = Faker("random_int", min=500, max=1000)

class Meta:
model = ManagedOpportunity
Expand Down
20 changes: 20 additions & 0 deletions commcare_connect/program/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import pytest

from commcare_connect.opportunity.tests.factories import PaymentUnitFactory
from commcare_connect.program.models import ManagedOpportunity
from commcare_connect.program.tests.factories import ManagedOpportunityFactory


@pytest.mark.django_db
def test_managed_opportunity_stats():
opportunity = ManagedOpportunityFactory(total_budget=3600000, org_pay_per_visit=450)
PaymentUnitFactory(opportunity=opportunity, max_total=600, max_daily=5, amount=750)

opportunity = ManagedOpportunity.objects.get(id=opportunity.id)

assert opportunity.budget_per_user == 450000
assert opportunity.allotted_visits == 3000
assert opportunity.number_of_users == 5
assert opportunity.max_visits_per_user_new == 600
assert opportunity.daily_max_visits_per_user_new == 5
assert opportunity.budget_per_visit_new == 750

0 comments on commit 9457453

Please sign in to comment.