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

Add claimed_budget and utilised_budget to model and ui #149

Merged
merged 8 commits into from
Oct 13, 2023
21 changes: 20 additions & 1 deletion commcare_connect/opportunity/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,32 @@ def __str__(self):

@property
def remaining_budget(self) -> int:
return self.total_budget - self.claimed_budget

@property
def claimed_budget(self):
return self.claimed_visits * self.budget_per_visit

@property
def utilised_budget(self):
return self.approved_visits * self.budget_per_visit

@property
def claimed_visits(self):
opp_access = OpportunityAccess.objects.filter(opportunity=self)
used_budget = OpportunityClaim.objects.filter(opportunity_access__in=opp_access).aggregate(
Sum("max_payments")
)["max_payments__sum"]
if used_budget is None:
used_budget = 0
return self.total_budget - used_budget
return used_budget

@property
def approved_visits(self):
approved_user_visits = UserVisit.objects.filter(
opportunity=self, status=VisitValidationStatus.approved
).count()
return approved_user_visits


class LearnModule(models.Model):
Expand Down
2 changes: 1 addition & 1 deletion commcare_connect/opportunity/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class OpportunityFactory(DjangoModelFactory):
max_visits_per_user = Faker("pyint", min_value=1, max_value=100)
daily_max_visits_per_user = Faker("pyint", min_value=1, max_value=SelfAttribute("..max_visits_per_user"))
end_date = Faker("date")
budget_per_visit = Faker("pyint", min_value=100, max_value=1000)
budget_per_visit = Faker("pyint", min_value=1, max_value=10)
total_budget = Faker("pyint", min_value=1000, max_value=10000)

class Meta:
Expand Down
85 changes: 54 additions & 31 deletions commcare_connect/templates/opportunity/opportunity_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,34 +86,60 @@ <h1 class="display-5 mb-0">{{ object.name }}</h1>
<p>
{{ object.description }}
</p>
<table class="table table-borderless">
<tbody>
<tr>
<th scope="row">Learn App</th>
<td>{{ object.learn_app.name }}</td>
</tr>
<tr>
<th scope="row">Deliver App</th>
<td>{{ object.deliver_app.name }}</td>
</tr>
<tr>
<th scope="row">Max Visits per user</th>
<td>{{ object.max_visits_per_user }}</td>
</tr>
<tr>
<th scope="row">Daily Visits per user</th>
<td>{{ object.daily_max_visits_per_user }}</td>
</tr>
<tr>
<th scope="row">Total Budget</th>
<td>{{ object.total_budget }}</td>
</tr>
<tr>
<th scope="row">Budget per visit</th>
<td>{{ object.budget_per_visit }}</td>
</tr>
</tbody>
</table>
<div class="row">
<div class="col-md-6">
<table class="table table-borderless">
<tbody>
<tr>
<th scope="row">Learn App</th>
<td>{{ object.learn_app.name }}</td>
</tr>
<tr>
<th scope="row">Deliver App</th>
<td>{{ object.deliver_app.name }}</td>
</tr>
<tr>
<th scope="row">Max Visits per user</th>
<td>{{ object.max_visits_per_user }}</td>
</tr>
<tr>
<th scope="row">Daily Visits per user</th>
<td>{{ object.daily_max_visits_per_user }}</td>
</tr>
<tr>
<th scope="row">Total Budget</th>
<td>{{ object.total_budget }}</td>
</tr>
</tbody>
</table>
</div>
<div class="col-md-6">
<table class="table table-borderless">
<tbody>
<tr>
<th scope="row">Budget per visit</th>
<td>{{ object.budget_per_visit }}</td>
</tr>
<tr>
<th scope="row">Claimed Budget</th>
<td>{{ object.claimed_budget }}</td>
</tr>
<tr>
<th scope="row">Utilised Budget</th>
<td>{{ object.utilised_budget }}</td>
</tr>
<tr>
<th scope="row">Claimed Visits</th>
<td>{{ object.claimed_visits }}</td>
</tr>
<tr>
<th scope="row">Approved Visits</th>
<td>{{ object.approved_visits }}</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="accordion" id="accordionExample">
<div class="accordion-item">
<h2 class="accordion-header" id="headingOne">
Expand Down Expand Up @@ -227,9 +253,6 @@ <h2 class="accordion-header" id="headingTwo">
</div>
</div>
</div>
<div class="pt-2 px-4">

</div>
</div>
{% endblock content %}

Expand Down