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

duration and utilized #456

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
24 changes: 16 additions & 8 deletions commcare_connect/opportunity/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.conf import settings
from django.db import models
from django.db.models import Count, F, Max, Q, Sum
from django.utils.dateparse import parse_datetime
from django.utils.functional import cached_property
from django.utils.timezone import now
from django.utils.translation import gettext
Expand Down Expand Up @@ -129,15 +130,10 @@ def claimed_budget(self):

@property
def utilised_budget(self):
completed_works = CompletedWork.objects.filter(opportunity_access__opportunity=self)
payment_unit_counts = completed_works.values("payment_unit").annotate(
completed_count=Count("id"), amount=F("payment_unit__amount")
)
users = OpportunityAccess.objects.filter(opportunity=self)
utilised = 0
for payment_unit_count in payment_unit_counts:
completed_count = payment_unit_count["completed_count"]
amount = payment_unit_count["amount"]
utilised += completed_count * amount
for u in users:
utilised += u.payment_accrued
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might need to add the org_pay component here as well for the managed opportunities.

return utilised

@property
Expand Down Expand Up @@ -586,6 +582,18 @@ def __setattr__(self, name, value):
def images(self):
return BlobMeta.objects.filter(parent_id=self.xform_id, content_type__startswith="image/")

@property
def duration(self):
duration = None
start = self.form_json["metadata"].get("timeStart")
end = self.form_json["metatdata"].get("timeEnd")
if start and end:
try:
duration = parse_datetime(end) - parse_datetime(start)
except (TypeError, ValueError):
pass
return duration


class OpportunityClaim(models.Model):
opportunity_access = models.OneToOneField(OpportunityAccess, on_delete=models.CASCADE)
Expand Down
1 change: 1 addition & 0 deletions commcare_connect/opportunity/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class UserVisitTable(OrgContextTable):
)
reason = columns.Column("Rejected Reason", accessor="reason", visible=False)
justification = columns.Column("Justification", accessor="justification", visible=False)
duration = columns.Column("Duration", accessor="duration", visible=False)

deliver_unit = columns.Column("Unit Name", accessor="deliver_unit__name")
entity_id = columns.Column("Entity ID", accessor="entity_id", visible=False)
Expand Down
6 changes: 3 additions & 3 deletions commcare_connect/opportunity/tests/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ def test_export_user_visit_data(mobile_user_with_connect_link):

assert exporter.export("csv") == (
"Visit ID,Visit date,Status,Username,Name of User,Unit Name,Rejected Reason,"
"Entity ID,Entity Name,Flags,form.name,form.group.q\r\n"
f",{date1.isoformat()},Pending,{username},{name},{deliver_unit.name},,,,,test_form1,\r\n"
f",{date2.isoformat()},Pending,{username},{name},{deliver_unit.name},,abc,A B C,,test_form2,b\r\n"
"Duration,Entity ID,Entity Name,Flags,form.name,form.group.q\r\n"
f",{date1.isoformat()},Pending,{username},{name},{deliver_unit.name},,,,,,test_form1,\r\n"
f",{date2.isoformat()},Pending,{username},{name},{deliver_unit.name},,,abc,A B C,,test_form2,b\r\n"
)


Expand Down
Loading