Skip to content

Commit

Permalink
Enhance EFT outstanding balance handling (bcgov#1608)
Browse files Browse the repository at this point in the history
  • Loading branch information
ochiu authored Jul 8, 2024
1 parent 23ee749 commit 33df3fe
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
25 changes: 19 additions & 6 deletions pay-api/src/pay_api/services/payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from dateutil import parser
from flask import current_app
from requests import HTTPError

from pay_api.models import CfsAccount as CfsAccountModel
from pay_api.models import EFTTransaction as EFTTransactionModel
Expand All @@ -38,7 +39,9 @@
AuthHeaderType, Code, ContentType, InvoiceReferenceStatus, InvoiceStatus, PaymentMethod, PaymentStatus,
PaymentSystem)
from pay_api.utils.user_context import user_context
from pay_api.utils.util import generate_receipt_number, get_local_formatted_date, get_local_formatted_date_time
from pay_api.utils.util import (
generate_receipt_number, generate_transaction_number, get_local_formatted_date, get_local_formatted_date_time)


from .code import Code as CodeService
from .oauth_service import OAuthService
Expand Down Expand Up @@ -605,20 +608,30 @@ def create_consolidated_invoices_payment(consolidated_invoices: List[InvoiceMode
invoice_total: Decimal):
"""Create payment for consolidated invoices and update invoice references."""
invoice_number = str(consolidated_invoices[-1].id) + '-C'
invoice_response = CFSService.get_invoice(cfs_account=cfs_account, inv_number=invoice_number)
prefixed_invoice_number = generate_transaction_number(invoice_number)
invoice_exists = False
try:
invoice_response = CFSService.get_invoice(cfs_account=cfs_account,
inv_number=prefixed_invoice_number)

invoice_exists = invoice_response.get('invoice_number', None) == prefixed_invoice_number
invoice_total_matches = Decimal(invoice_response.get('total', '0')) == invoice_total

invoice_exists = invoice_response.get('invoice_number', None) == invoice_number
invoice_total_matches = Decimal(invoice_response.get('total', '0')) == invoice_total
if invoice_exists and not invoice_total_matches:
raise BusinessException(Error.CFS_INVOICES_MISMATCH)

if invoice_exists and not invoice_total_matches:
raise BusinessException(Error.EFT_PAY_OUTSTANDING_INVOICES_MISMATCH)
except HTTPError as exception:
if exception.response.status_code != 404:
raise

if not invoice_exists:
invoice_response = CFSService.create_account_invoice(
transaction_number=invoice_number,
line_items=consolidated_line_items,
cfs_account=cfs_account)

invoice_number: str = invoice_response.get('invoice_number')

for invoice in consolidated_invoices:
inv_ref: InvoiceReferenceModel = InvoiceReferenceModel.find_by_invoice_id_and_status(
invoice_id=invoice.id, status_code=InvoiceReferenceStatus.ACTIVE.value)
Expand Down
3 changes: 2 additions & 1 deletion pay-api/src/pay_api/utils/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ class Error(Enum):
TRANSACTIONS_IN_PROGRESS = 'TRANSACTIONS_IN_PROGRESS', HTTPStatus.BAD_REQUEST
FROZEN_ACCOUNT = 'FROZEN_ACCOUNT', HTTPStatus.BAD_REQUEST

CFS_INVOICES_MISMATCH = 'CFS_INVOICES_MISMATCH', HTTPStatus.BAD_REQUEST

# EFT Errors
EFT_PAY_OUTSTANDING_INVOICES_MISMATCH = 'EFT_PAY_OUTSTANDING_INVOICES_MISMATCH', HTTPStatus.BAD_REQUEST
EFT_SHORT_NAME_EXISTS = 'EFT_SHORT_NAME_EXISTS', HTTPStatus.BAD_REQUEST
EFT_SHORT_NAME_ACCOUNT_ID_REQUIRED = 'EFT_SHORT_NAME_ACCOUNT_ID_REQUIRED', HTTPStatus.BAD_REQUEST
EFT_SHORT_NAME_ALREADY_MAPPED = 'EFT_SHORT_NAME_ALREADY_MAPPED', HTTPStatus.BAD_REQUEST
Expand Down

0 comments on commit 33df3fe

Please sign in to comment.