-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
233 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
commcare_connect/opportunity/management/commands/auto_approval_opportunities.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from django.core.management import BaseCommand | ||
|
||
from commcare_connect.opportunity.models import CompletedWorkStatus, Opportunity, OpportunityAccess | ||
from commcare_connect.opportunity.utils.completed_work import update_status | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Run auto-approval logic over opportunity" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"--opp", type=int, required=True, help="ID of the opportunity to run auto-approval logic on" | ||
) | ||
|
||
def handle(self, *args, opp: int, **options): | ||
try: | ||
opportunity = Opportunity.objects.get(id=opp) | ||
access_objects = OpportunityAccess.objects.filter( | ||
opportunity=opportunity, suspended=False, opportunity__auto_approve_payments=True | ||
) | ||
for access in access_objects: | ||
completed_works = access.completedwork_set.exclude( | ||
status__in=[CompletedWorkStatus.rejected, CompletedWorkStatus.over_limit] | ||
) | ||
update_status(completed_works, access, False) | ||
|
||
self.stdout.write(self.style.SUCCESS(f"Successfully processed opportunity with id {opp}")) | ||
|
||
except Opportunity.DoesNotExist: | ||
self.stdout.write(self.style.ERROR(f"Opportunity with id {opp} does not exist.")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from commcare_connect.opportunity.models import CompletedWorkStatus | ||
|
||
|
||
def update_status(completed_works, opportunity_access, compute_payment=True): | ||
""" | ||
Updates the status of completed works and optionally calculates & update total payment_accrued. | ||
""" | ||
payment_accrued = 0 | ||
for completed_work in completed_works: | ||
if completed_work.completed_count < 1: | ||
continue | ||
|
||
if opportunity_access.opportunity.auto_approve_payments: | ||
update_completed_work_status(completed_work) | ||
|
||
if compute_payment: | ||
approved_count = completed_work.approved_count | ||
if approved_count > 0 and completed_work.status == CompletedWorkStatus.approved: | ||
payment_accrued += approved_count * completed_work.payment_unit.amount | ||
|
||
if compute_payment: | ||
opportunity_access.payment_accrued = payment_accrued | ||
opportunity_access.save() | ||
|
||
|
||
def update_completed_work_status(completed_work): | ||
visits = completed_work.uservisit_set.values_list("status", "reason") | ||
if any(status == "rejected" for status, _ in visits): | ||
completed_work.status = CompletedWorkStatus.rejected | ||
completed_work.reason = "\n".join(reason for _, reason in visits if reason) | ||
elif all(status == "approved" for status, _ in visits): | ||
completed_work.status = CompletedWorkStatus.approved | ||
|
||
completed_work.save() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.