From 438faedf454193f8131a83b42795c1814fdcb860 Mon Sep 17 00:00:00 2001 From: Odysseus Chiu Date: Mon, 23 Oct 2023 10:57:21 -0700 Subject: [PATCH] 17224/17829 - EFT over due status handling updates (#1295) --- pay-api/src/pay_api/models/invoice.py | 6 +++++- pay-api/src/pay_api/services/statement.py | 16 ++++++++++++---- pay-api/tests/unit/models/test_invoice.py | 7 +++++++ 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/pay-api/src/pay_api/models/invoice.py b/pay-api/src/pay_api/models/invoice.py index 878061237..155199c68 100644 --- a/pay-api/src/pay_api/models/invoice.py +++ b/pay-api/src/pay_api/models/invoice.py @@ -17,6 +17,7 @@ from datetime import datetime from decimal import Decimal from typing import List, Optional +from dateutil.relativedelta import relativedelta from attrs import define from marshmallow import fields, post_dump @@ -34,6 +35,7 @@ from .payment_account import PaymentAccountSchema, PaymentAccountSearchModel from .payment_line_item import PaymentLineItem, PaymentLineItemSchema from .receipt import ReceiptSchema +from ..utils.util import current_local_time class Invoice(Audit): # pylint: disable=too-many-instance-attributes @@ -98,7 +100,9 @@ class Invoice(Audit): # pylint: disable=too-many-instance-attributes total = db.Column(db.Numeric(19, 2), nullable=False) paid = db.Column(db.Numeric(19, 2), nullable=True) payment_date = db.Column(db.DateTime, nullable=True) - overdue_date = db.Column(db.DateTime, nullable=True) + # default overdue_date to the first of next month + overdue_date = db.Column(db.DateTime, nullable=True, + default=lambda: current_local_time() + relativedelta(months=1, day=1)) refund_date = db.Column(db.DateTime, nullable=True) refund = db.Column(db.Numeric(19, 2), nullable=True) routing_slip = db.Column(db.String(50), nullable=True, index=True) diff --git a/pay-api/src/pay_api/services/statement.py b/pay-api/src/pay_api/services/statement.py index fdedb5d3d..8d9755eb5 100644 --- a/pay-api/src/pay_api/services/statement.py +++ b/pay-api/src/pay_api/services/statement.py @@ -171,8 +171,9 @@ def get_statement_report(statement_id: str, content_type: str, template_name='st return report_response, report_name @staticmethod - def get_summary(auth_account_id: str): + def get_summary(auth_account_id: str, statement_id: str = None): """Get summary for statements by account id.""" + # Used by payment jobs to get the total due amount for statements, keep in mind when modifying. # This is written outside of the model, because we have multiple model references that need to be included. # If we include these references inside of a model, it runs the risk of having circular dependencies. # It's easier to build out features if our models don't rely on other models. @@ -181,9 +182,16 @@ def get_summary(auth_account_id: str): .join(PaymentAccountModel) \ .join(StatementInvoicesModel) \ .filter(PaymentAccountModel.auth_account_id == auth_account_id) \ - .filter(InvoiceModel.invoice_status_code == InvoiceStatus.OVERDUE.value) \ - .filter(StatementInvoicesModel.invoice_id == InvoiceModel.id) \ - .group_by(InvoiceModel.payment_account_id) \ + .filter(InvoiceModel.invoice_status_code.in_((InvoiceStatus.SETTLEMENT_SCHEDULED.value, + InvoiceStatus.PARTIAL.value, + InvoiceStatus.CREATED.value, + InvoiceStatus.OVERDUE.value))) \ + .filter(StatementInvoicesModel.invoice_id == InvoiceModel.id) + + if statement_id: + result = result.filter(StatementInvoicesModel.statement_id == statement_id) + + result = result.group_by(InvoiceModel.payment_account_id) \ .one_or_none() total_due = float(result.total_due) if result else 0 diff --git a/pay-api/tests/unit/models/test_invoice.py b/pay-api/tests/unit/models/test_invoice.py index b7550fbd3..3b2e999c4 100644 --- a/pay-api/tests/unit/models/test_invoice.py +++ b/pay-api/tests/unit/models/test_invoice.py @@ -16,8 +16,11 @@ Test-Suite to ensure that the CorpType Class is working as expected. """ +from dateutil.relativedelta import relativedelta + from pay_api.models import Invoice, InvoiceSchema from pay_api.utils.enums import InvoiceStatus +from pay_api.utils.util import current_local_time from tests.utilities.base_test import factory_invoice, factory_payment, factory_payment_account @@ -34,6 +37,10 @@ def test_invoice(session): invoice.save() assert invoice.id is not None + # assert overdue default is set + assert invoice.overdue_date is not None + assert invoice.overdue_date.date() == (current_local_time() + relativedelta(months=1, day=1)).date() + def test_invoice_find_by_id(session): """Assert a invoice is stored.