-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Small fixes and tweaks for partner reversals (#1775)
- Loading branch information
Showing
21 changed files
with
3,463 additions
and
3,244 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"""Small fix for status code | ||
Revision ID: ed487561aeeb | ||
Revises: d1996caed682 | ||
Create Date: 2024-10-10 11:36:29.069307 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
from pay_api.utils.enums import DisbursementStatus | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
# Note you may see foreign keys with distribution_codes_history | ||
# For disbursement_distribution_code_id, service_fee_distribution_code_id | ||
# Please ignore those lines and don't include in migration. | ||
|
||
revision = 'ed487561aeeb' | ||
down_revision = 'd1996caed682' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
op.execute( | ||
f"update partner_disbursements set status_code = '{DisbursementStatus.WAITING_FOR_JOB.value}' where status_code = 'WAITING_FOR_RECEIPT'" | ||
) | ||
|
||
|
||
def downgrade(): | ||
pass |
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 |
---|---|---|
|
@@ -137,6 +137,7 @@ extend-exclude = ''' | |
migrations | ||
| devops | ||
| .history | ||
| lib | ||
)/ | ||
''' | ||
|
||
|
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
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,81 @@ | ||
"""Partner Disbursements service.""" | ||
|
||
from datetime import datetime, timezone | ||
|
||
from flask import current_app | ||
|
||
from pay_api.models.corp_type import CorpType as CorpTypeModel | ||
from pay_api.models.invoice import Invoice as InvoiceModel | ||
from pay_api.models.partner_disbursements import PartnerDisbursements as PartnerDisbursementsModel | ||
from pay_api.utils.enums import DisbursementStatus, EJVLinkType | ||
|
||
|
||
class PartnerDisbursements: | ||
"""Partner Disbursements service.""" | ||
|
||
@staticmethod | ||
def _skip_partner_disbursement(invoice: InvoiceModel) -> bool: | ||
"""Determine if partner disbursement should be skipped.""" | ||
return ( | ||
invoice.total - invoice.service_fees <= 0 | ||
or bool(CorpTypeModel.find_by_code(invoice.corp_type_code).has_partner_disbursements) is False | ||
) | ||
|
||
@staticmethod | ||
def handle_payment(invoice: InvoiceModel): | ||
"""Insert a partner disbursement row if necessary with is_reversal as False.""" | ||
if PartnerDisbursements._skip_partner_disbursement(invoice): | ||
return | ||
|
||
latest_active_disbursement = PartnerDisbursementsModel.find_by_target_latest_exclude_cancelled( | ||
invoice.id, EJVLinkType.INVOICE.value | ||
) | ||
|
||
if latest_active_disbursement is None or latest_active_disbursement.is_reversal: | ||
PartnerDisbursementsModel( | ||
amount=invoice.total - invoice.service_fees, | ||
is_reversal=False, | ||
partner_code=invoice.corp_type_code, | ||
status_code=DisbursementStatus.WAITING_FOR_JOB.value, | ||
target_id=invoice.id, | ||
target_type=EJVLinkType.INVOICE.value, | ||
).flush() | ||
else: | ||
# If this was already called at invoice creation, it might be called again when mapping credits. | ||
# If we're mapping credits after invoice creation, we don't want to create a new row. | ||
current_app.logger.info(f"Skipping Partner Disbursement Payment creation for {invoice.id} already exists.") | ||
|
||
@staticmethod | ||
def handle_reversal(invoice: InvoiceModel): | ||
"""Cancel existing row or insert new row if non reversal is found.""" | ||
if PartnerDisbursements._skip_partner_disbursement(invoice): | ||
return | ||
|
||
if not ( | ||
latest_active_disbursement := PartnerDisbursementsModel.find_by_target_latest_exclude_cancelled( | ||
invoice.id, EJVLinkType.INVOICE.value | ||
) | ||
): | ||
current_app.logger.error(f"Existing Partner Disbursement not found for invoice {invoice.id}") | ||
return | ||
|
||
if latest_active_disbursement.is_reversal is True: | ||
current_app.logger.error(f"Duplicate Existing Partner Disbursement Reversal for invoice {invoice.id}") | ||
return | ||
|
||
match latest_active_disbursement.status_code: | ||
case DisbursementStatus.WAITING_FOR_JOB.value: | ||
# Note we never CANCEL a reversal. | ||
latest_active_disbursement.status_code = DisbursementStatus.CANCELLED.value | ||
latest_active_disbursement.processed_on = datetime.now(tz=timezone.utc) | ||
latest_active_disbursement.flush() | ||
case _: | ||
# We'll assume errored status should be fixed in the future to COMPLETED hopefully. | ||
PartnerDisbursementsModel( | ||
amount=invoice.total - invoice.service_fees, | ||
is_reversal=True, | ||
partner_code=invoice.corp_type_code, | ||
status_code=DisbursementStatus.WAITING_FOR_JOB.value, | ||
target_id=invoice.id, | ||
target_type=EJVLinkType.INVOICE.value, | ||
).flush() |
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.