Skip to content

Commit

Permalink
Calculation fixes (#1396)
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigo-barraza authored Feb 1, 2024
1 parent c6add95 commit 755014f
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions pay-api/src/pay_api/services/non_sufficient_funds.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from pay_api.models import PaymentLineItem as PaymentLineItemModel
from pay_api.models import db
from pay_api.utils.converter import Converter
from pay_api.utils.enums import AuthHeaderType, ContentType, InvoiceReferenceStatus, ReverseOperation
from pay_api.utils.enums import AuthHeaderType, ContentType, InvoiceReferenceStatus, InvoiceStatus, ReverseOperation
from pay_api.utils.user_context import user_context

from .oauth_service import OAuthService
Expand Down Expand Up @@ -86,7 +86,8 @@ def query_all_non_sufficient_funds_invoices(account_id: str):
.join(NonSufficientFundsModel,
NonSufficientFundsModel.invoice_number == InvoiceReferenceModel.invoice_number)
.join(PaymentAccountModel, PaymentAccountModel.id == InvoiceModel.payment_account_id)
.filter(PaymentAccountModel.auth_account_id == account_id)
.filter(PaymentAccountModel.auth_account_id == account_id,
InvoiceModel.invoice_status_code != InvoiceStatus.PAID.value)
.distinct(InvoiceModel.id)
.group_by(InvoiceModel.id, InvoiceReferenceModel.id)
)
Expand All @@ -99,10 +100,13 @@ def query_all_non_sufficient_funds_invoices(account_id: str):
[(PaymentLineItemModel.description == ReverseOperation.NSF.value, PaymentLineItemModel.total)],
else_=0)).label('nsf_amount')
)
.join(InvoiceReferenceModel, InvoiceReferenceModel.invoice_id == InvoiceModel.id)
.join(NonSufficientFundsModel,
NonSufficientFundsModel.invoice_number == InvoiceReferenceModel.invoice_number)
.join(PaymentAccountModel, PaymentAccountModel.id == InvoiceModel.payment_account_id)
.outerjoin(NonSufficientFundsModel, NonSufficientFundsModel.invoice_id == InvoiceModel.id)
.join(PaymentLineItemModel, PaymentLineItemModel.invoice_id == InvoiceModel.id)
.filter(PaymentAccountModel.auth_account_id == account_id)
.filter(PaymentAccountModel.auth_account_id == account_id,
InvoiceModel.invoice_status_code != InvoiceStatus.PAID.value)
.group_by(InvoiceModel.id)
.subquery()
)
Expand Down Expand Up @@ -148,7 +152,8 @@ def create_non_sufficient_funds_statement_pdf(account_id: str, **kwargs):
"""Create Non-Sufficient Funds statement pdf."""
current_app.logger.debug('<generate_non_sufficient_funds_statement_pdf')
invoice = NonSufficientFundsService.find_all_non_sufficient_funds_invoices(account_id=account_id)
cfs_account: CfsAccountModel = CfsAccountModel.find_latest_account_by_account_id(account_id)
payment_account = PaymentAccountModel.find_by_auth_account_id(account_id)
cfs_account: CfsAccountModel = CfsAccountModel.find_latest_account_by_account_id(payment_account.id)
invoice_reference: InvoiceReferenceModel = InvoiceReferenceModel.find_by_invoice_id_and_status(
invoice['invoices'][0]['id'], InvoiceReferenceStatus.ACTIVE.value)
account_url = current_app.config.get('AUTH_API_ENDPOINT') + f'orgs/{account_id}'
Expand All @@ -159,7 +164,7 @@ def create_non_sufficient_funds_statement_pdf(account_id: str, **kwargs):
'suspendedOn': datetime.strptime(account['suspendedOn'], '%Y-%m-%dT%H:%M:%S%z')
.strftime('%B %-d, %Y') if 'suspendedOn' in account else None,
'accountNumber': cfs_account.cfs_account,
'businessName': account['businessName'],
'businessName': account.get('businessName', account.get('createdBy')),
'totalAmountRemaining': invoice['total_amount_remaining'],
'totalAmount': invoice['total_amount'],
'nsfAmount': invoice['nsf_amount'],
Expand Down

0 comments on commit 755014f

Please sign in to comment.