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

Show payment metrics #405

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 4.2.5 on 2024-10-03 02:33

from django.db import migrations, models
from django.db import transaction

from commcare_connect.opportunity.models import OpportunityAccess
from commcare_connect.opportunity.visit_import import update_work_payment_date


@transaction.atomic
def update_paid_date_from_payments(apps, schema_editor):
accesses = OpportunityAccess.objects.all()
for access in accesses:
update_work_payment_date(access)


class Migration(migrations.Migration):
dependencies = [
("opportunity", "0058_paymentinvoice_payment_invoice"),
]

operations = [
migrations.AddField(
model_name="completedwork",
name="payment_date",
field=models.DateTimeField(null=True),
),
migrations.RunPython(update_paid_date_from_payments, migrations.RunPython.noop),
]
1 change: 1 addition & 0 deletions commcare_connect/opportunity/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ class CompletedWork(models.Model):
entity_name = models.CharField(max_length=255, null=True, blank=True)
reason = models.CharField(max_length=300, null=True, blank=True)
status_modified_date = models.DateTimeField(null=True)
payment_date = models.DateTimeField(null=True)

def __init__(self, *args, **kwargs):
self.status = CompletedWorkStatus.incomplete
Expand Down
38 changes: 37 additions & 1 deletion commcare_connect/opportunity/utils/completed_work.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
from commcare_connect.opportunity.models import CompletedWorkStatus, VisitReviewStatus, VisitValidationStatus
from commcare_connect.opportunity.models import (
CompletedWork,
CompletedWorkStatus,
OpportunityAccess,
Payment,
VisitReviewStatus,
VisitValidationStatus,
)


def update_status(completed_works, opportunity_access, compute_payment=True):
Expand Down Expand Up @@ -35,3 +42,32 @@ def update_status(completed_works, opportunity_access, compute_payment=True):
if compute_payment:
opportunity_access.payment_accrued = payment_accrued
opportunity_access.save()


def update_work_payment_date(access: OpportunityAccess):
payments = Payment.objects.filter(opportunity_access=access).order_by("date_paid")
completed_works = CompletedWork.objects.filter(opportunity_access=access).order_by("status_modified_date")

paid = 0
works_to_update = []
completed_works_iter = iter(completed_works)

try:
current_work = next(completed_works_iter)
except StopIteration:
return

hemant10yadav marked this conversation as resolved.
Show resolved Hide resolved
for payment in payments:
paid += payment.amount

while paid > current_work.payment_accrued:
current_work.payment_date = payment.date_paid
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is missing the logic deducts from the total paid when we assign that payment to a completed work. Right now this will run forever in most cases, because the total paid out is always greater than what is due for the next completed work. For example if each work accrues $5 and the first payment was $10, 10 is always greater than 5. We need to deduct what we are assigning to the work, or keep a similar running total for what we owe.

works_to_update.append(current_work)

try:
current_work = next(completed_works_iter)
except StopIteration:
break

if works_to_update:
CompletedWork.objects.bulk_update(works_to_update, ["payment_date"])
3 changes: 2 additions & 1 deletion commcare_connect/opportunity/visit_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
VisitValidationStatus,
)
from commcare_connect.opportunity.tasks import send_payment_notification
from commcare_connect.opportunity.utils.completed_work import update_status
from commcare_connect.opportunity.utils.completed_work import update_status, update_work_payment_date
from commcare_connect.utils.file import get_file_extension
from commcare_connect.utils.itertools import batched

Expand Down Expand Up @@ -263,6 +263,7 @@ def _bulk_update_payments(opportunity: Opportunity, imported_data: Dataset) -> P
payment = Payment.objects.create(opportunity_access=access, amount=amount)
seen_users.add(username)
payment_ids.append(payment.pk)
update_work_payment_date(access)
missing_users = set(usernames) - seen_users
send_payment_notification.delay(opportunity.id, payment_ids)
return PaymentImportStatus(seen_users, missing_users)
Expand Down
Loading