-
Notifications
You must be signed in to change notification settings - Fork 40
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
19722 - Unlinking EFT Task #1487
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
40e155b
Unlinking EFT
rodrigo-barraza ddb6863
Flake fixes
rodrigo-barraza cc69df7
Unlinking EFT test
rodrigo-barraza 8cc6571
Merge branch 'feature-queue-python-upgrade' into feature/19722
rodrigo-barraza 8a1f6d9
Cleanup and fixes
rodrigo-barraza c9b2c70
Fixing tests
rodrigo-barraza 3825a6a
Updates and fixes
rodrigo-barraza 6626acc
renaming function
rodrigo-barraza 4828e63
Lint fixes
rodrigo-barraza 8713bd8
Flake8 fix
rodrigo-barraza 52309ce
Test fix
rodrigo-barraza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,19 +18,21 @@ | |
|
||
from flask import current_app | ||
from pay_api.models import CfsAccount as CfsAccountModel | ||
from pay_api.models import EFTShortnames as EFTShortnameModel | ||
from pay_api.models import EFTShortnameLinks as EFTShortnameLinksModel | ||
from pay_api.models import EFTCredit as EFTCreditModel | ||
from pay_api.models import EFTShortnameLinks as EFTShortnameLinksModel | ||
from pay_api.models import EFTShortnames as EFTShortNamesModel | ||
from pay_api.models import Invoice as InvoiceModel | ||
from pay_api.models import InvoiceReference as InvoiceReferenceModel | ||
from pay_api.models import PaymentAccount as PaymentAccountModel | ||
from pay_api.models import Payment as PaymentModel | ||
from pay_api.models import PaymentAccount as PaymentAccountModel | ||
from pay_api.models import PaymentLineItem as PaymentLineItemModel | ||
from pay_api.models import Receipt as ReceiptModel | ||
from pay_api.models import db | ||
from pay_api.services.cfs_service import CFSService | ||
from pay_api.services import EFTShortNamesService | ||
from pay_api.services.cfs_service import CFSService | ||
from pay_api.services.receipt import Receipt | ||
from pay_api.utils.enums import CfsAccountStatus, EFTShortnameStatus, InvoiceReferenceStatus, InvoiceStatus | ||
from pay_api.utils.util import generate_receipt_number | ||
from pay_api.utils.enums import ( | ||
CfsAccountStatus, EFTShortnameStatus, InvoiceReferenceStatus, InvoiceStatus, ReverseOperation) | ||
from sentry_sdk import capture_message | ||
|
||
|
||
|
@@ -40,6 +42,7 @@ class EFTShortnameInfo: | |
|
||
id: int | ||
auth_account_id: str | ||
version: int | ||
|
||
|
||
class ElectronicFundsTransferTask: # pylint:disable=too-few-public-methods | ||
|
@@ -67,10 +70,11 @@ def link_electronic_funds_transfers(cls): | |
payment = db.session.query(PaymentModel) \ | ||
.join(PaymentAccountModel, PaymentAccountModel.id == PaymentModel.payment_account_id) \ | ||
.join(EFTCreditModel, EFTCreditModel.payment_account_id == PaymentAccountModel.id) \ | ||
.join(EFTShortnameModel, EFTShortnameModel.id == EFTCreditModel.short_name_id) \ | ||
.filter(EFTShortnameModel.id == eft_short_name.id).first() | ||
.join(EFTShortNamesModel, EFTShortNamesModel.id == EFTCreditModel.short_name_id) \ | ||
.filter(EFTShortNamesModel.id == eft_short_name.id).first() | ||
|
||
receipt_number = payment.id | ||
|
||
receipt_number = generate_receipt_number(payment.id) | ||
CFSService.create_cfs_receipt( | ||
cfs_account=cfs_account, | ||
rcpt_number=receipt_number, | ||
|
@@ -81,7 +85,7 @@ def link_electronic_funds_transfers(cls): | |
|
||
# apply receipt to cfs_account | ||
total_invoice_amount = cls._apply_electronic_funds_transfers_to_pending_invoices( | ||
eft_short_name, payment) | ||
eft_short_name, receipt_number) | ||
current_app.logger.debug(f'Total Invoice Amount : {total_invoice_amount}') | ||
|
||
eft_credit.remaining_amount = eft_credit.amount - total_invoice_amount | ||
|
@@ -96,16 +100,62 @@ def link_electronic_funds_transfers(cls): | |
continue | ||
|
||
@classmethod | ||
def _get_eft_short_names_by_status(cls, status: str) -> List[EFTShortnameModel]: | ||
def unlink_electronic_funds_transfers(cls): | ||
"""Unlink electronic funds transfers.""" | ||
eft_short_names = cls._get_eft_short_names_by_status(EFTShortnameStatus.UNLINKED.value) | ||
for eft_short_name in eft_short_names: | ||
try: | ||
current_app.logger.debug(f'Unlinking Electronic Funds Transfer: {eft_short_name.id}') | ||
payment_account: PaymentAccountModel = PaymentAccountModel.find_by_auth_account_id( | ||
eft_short_name.auth_account_id) | ||
cfs_account: CfsAccountModel = CfsAccountModel.find_effective_by_account_id(payment_account.id) | ||
eft_credit: EFTCreditModel = EFTCreditModel.find_by_payment_account_id(payment_account.id) | ||
|
||
payment = db.session.query(PaymentModel) \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. functionize this? looks duplicate to 60 |
||
.join(PaymentAccountModel, PaymentAccountModel.id == PaymentModel.payment_account_id) \ | ||
.join(EFTCreditModel, EFTCreditModel.payment_account_id == PaymentAccountModel.id) \ | ||
.join(EFTShortNamesModel, EFTShortNamesModel.id == EFTCreditModel.short_name_id) \ | ||
.filter(EFTShortNamesModel.id == eft_short_name.id).first() | ||
|
||
eft_short_name.version += 1 | ||
|
||
receipt_number = f'{payment.id}R{eft_short_name.version}' | ||
|
||
CFSService.reverse_rs_receipt_in_cfs(cfs_account, receipt_number, ReverseOperation.CORRECTION.value) | ||
|
||
CFSService.create_cfs_receipt( | ||
cfs_account=cfs_account, | ||
rcpt_number=f'R{receipt_number}', | ||
rcpt_date=payment.payment_date.strftime('%Y-%m-%d'), | ||
amount=payment.invoice_amount, | ||
payment_method=payment_account.payment_method, | ||
access_token=CFSService.get_fas_token().json().get('access_token')) | ||
|
||
cls._reset_invoices_and_references_to_created_for_electronic_funds_transfer(eft_short_name) | ||
|
||
cls._apply_electronic_funds_transfers_to_pending_invoices(eft_short_name, receipt_number) | ||
|
||
eft_credit.remaining_amount = eft_credit.amount | ||
|
||
eft_short_name.save() | ||
|
||
except Exception as e: # NOQA # pylint: disable=broad-except | ||
capture_message( | ||
f'Error on Processing UNLINKING for :={receipt_number}, ' | ||
f'EFT Short Name : {eft_short_name.id}, ERROR : {str(e)}', level='error') | ||
current_app.logger.error(e) | ||
continue | ||
|
||
@classmethod | ||
def _get_eft_short_names_by_status(cls, status: str) -> List[EFTShortNamesModel]: | ||
"""Get electronic funds transfer by state.""" | ||
query = db.session.query(EFTShortnameModel.id.label('short_name_id'), EFTShortnameLinksModel.auth_account_id) \ | ||
.join(EFTShortnameLinksModel, EFTShortnameLinksModel.eft_short_name_id == EFTShortnameModel.id) \ | ||
query = db.session.query(EFTShortNamesModel.id.label('short_name_id'), EFTShortnameLinksModel.auth_account_id, | ||
EFTShortNamesModel.version.label('short_name_version'), ) \ | ||
.join(EFTShortnameLinksModel, EFTShortnameLinksModel.eft_short_name_id == EFTShortNamesModel.id) \ | ||
.join(PaymentAccountModel, PaymentAccountModel.auth_account_id == EFTShortnameLinksModel.auth_account_id) \ | ||
.join(CfsAccountModel, CfsAccountModel.account_id == PaymentAccountModel.id) \ | ||
.filter(CfsAccountModel.status == CfsAccountStatus.ACTIVE.value) | ||
|
||
if status == EFTShortnameStatus.UNLINKED.value: | ||
query = query.filter(EFTShortnameLinksModel.id.is_(None)) | ||
if status == EFTShortnameStatus.LINKED.value: | ||
query = query.filter(EFTShortnameLinksModel.status_code == status) | ||
|
||
|
@@ -114,15 +164,16 @@ def _get_eft_short_names_by_status(cls, status: str) -> List[EFTShortnameModel]: | |
|
||
# Short name can have multiple linked accounts, prepare list of dataclasses with the associated | ||
# auth_account_ids for the outer processing loops | ||
for short_name_id, auth_account_id in result: | ||
short_name_results.append(EFTShortnameInfo(id=short_name_id, auth_account_id=auth_account_id)) | ||
for short_name_id, auth_account_id, short_name_version in result: | ||
short_name_results.append(EFTShortnameInfo(id=short_name_id, auth_account_id=auth_account_id, | ||
version=short_name_version)) | ||
|
||
return short_name_results | ||
|
||
@classmethod | ||
def _apply_electronic_funds_transfers_to_pending_invoices(cls, | ||
eft_short_name: EFTShortnameModel, | ||
payment: PaymentModel) -> float: | ||
eft_short_name: EFTShortNamesModel, | ||
receipt_number) -> float: | ||
"""Apply the electronic funds transfers again.""" | ||
current_app.logger.info( | ||
f'Applying electronic funds transfer to pending invoices: {eft_short_name.id}') | ||
|
@@ -143,10 +194,9 @@ def _apply_electronic_funds_transfers_to_pending_invoices(cls, | |
) | ||
|
||
# apply invoice to the CFS_ACCOUNT | ||
cls.apply_electronic_funds_transfer_to_invoice( | ||
payment_account, cfs_account, eft_short_name, invoice, invoice_reference.invoice_number, payment | ||
cls.apply_eft_receipt_to_invoice( | ||
payment_account, cfs_account, eft_short_name, invoice, invoice_reference.invoice_number, receipt_number | ||
) | ||
|
||
# IF invoice balance is zero, then update records. | ||
if CFSService.get_invoice(cfs_account=cfs_account, inv_number=invoice_reference.invoice_number) \ | ||
.get('amount_due') == 0: | ||
|
@@ -158,18 +208,44 @@ def _apply_electronic_funds_transfers_to_pending_invoices(cls, | |
return applied_amount | ||
|
||
@classmethod | ||
def apply_electronic_funds_transfer_to_invoice(cls, # pylint: disable = too-many-arguments, too-many-locals | ||
payment_account: PaymentAccountModel, | ||
cfs_account: CfsAccountModel, | ||
eft_short_name: EFTShortnameModel, | ||
invoice: InvoiceModel, | ||
invoice_number: str, | ||
payment: PaymentModel) -> bool: | ||
def _reset_invoices_and_references_to_created_for_electronic_funds_transfer(cls, | ||
eft_short_name: EFTShortNamesModel): | ||
"""Reset Invoices, Invoice references and Receipts for EFT.""" | ||
invoices: List[InvoiceModel] = db.session.query(InvoiceModel) \ | ||
.join(InvoiceReferenceModel, PaymentModel.invoice_number == InvoiceReferenceModel.invoice_number) \ | ||
.join(InvoiceModel, InvoiceReferenceModel.invoice_id == InvoiceModel.id) \ | ||
.join(PaymentLineItemModel, PaymentLineItemModel.invoice_id == InvoiceModel.id) \ | ||
.join(PaymentAccountModel, PaymentAccountModel.id == InvoiceModel.payment_account_id) \ | ||
.join(EFTCreditModel, EFTCreditModel.payment_account_id == PaymentAccountModel.id) \ | ||
.join(EFTShortNamesModel, EFTShortNamesModel.id == EFTCreditModel.short_name_id) \ | ||
.join(EFTShortnameLinksModel, EFTShortnameLinksModel.eft_short_name_id == EFTShortNamesModel.id) \ | ||
.filter(InvoiceModel.invoice_status_code == InvoiceStatus.PAID.value) \ | ||
.filter(EFTShortNamesModel.auth_account_id == eft_short_name.auth_account_id) \ | ||
.all() | ||
|
||
for invoice in invoices: | ||
# Reset Invoice and Invoice Reference statuses. | ||
invoice.invoice_status_code = InvoiceStatus.CREATED.value | ||
invoice_reference = InvoiceReferenceModel.find_by_invoice_id_and_status( | ||
invoice.id, InvoiceReferenceStatus.COMPLETED.value | ||
) | ||
invoice_reference.status_code = InvoiceReferenceStatus.ACTIVE.value | ||
# Delete receipts as they are now reversed in CFS. | ||
for receipt in ReceiptModel.find_all_receipts_for_invoice(invoice.id): | ||
db.session.delete(receipt) | ||
|
||
@classmethod | ||
def apply_eft_receipt_to_invoice(cls, # pylint: disable = too-many-arguments, too-many-locals | ||
payment_account: PaymentAccountModel, | ||
cfs_account: CfsAccountModel, | ||
eft_short_name: EFTShortNamesModel, | ||
invoice: InvoiceModel, | ||
invoice_number: str, | ||
receipt_number) -> bool: | ||
"""Apply electronic funds transfers (receipts in CFS) to invoice.""" | ||
has_errors = False | ||
# an invoice has to be applied to multiple receipts (incl. all linked RS); apply till the balance is zero | ||
try: | ||
receipt_number = generate_receipt_number(payment.id) | ||
current_app.logger.debug(f'Apply receipt {receipt_number} on invoice {invoice_number} ' | ||
f'for electronic funds transfer {eft_short_name.id}') | ||
|
||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might get tricky if someone switches from EFT to PAD, I'm almost thinking for CFS account we should include the payment method in there? Thoughts? Otherwise someone could switch to PAD, this job runs and it's using the entirely wrong CFS account?
Because right now you can only go from Credit Card -> Online banking (ONLINE BANKING uses CFS account, can't remember if credit card does)
or PAD (CFS account) -> BCOL (No CFS account)
or just straight EJV
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But we'll be introducing EFT -> PAD or EFT -> BCOL and vice versa