Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Total visit calculation not accounting for org pay, causing inflation in total visit count. #418

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading