Skip to content

Commit

Permalink
17829 - Add in new is_overdue flag (#1271)
Browse files Browse the repository at this point in the history
* Add in new is_overdue flag

* Update type hint
  • Loading branch information
seeker25 authored Oct 3, 2023
1 parent 97d2875 commit 69e6a5e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions pay-api/src/pay_api/models/statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,4 @@ class Meta: # pylint: disable=too-few-public-methods

from_date = fields.Date(tzinfo=pytz.timezone(LEGISLATIVE_TIMEZONE))
to_date = fields.Date(tzinfo=pytz.timezone(LEGISLATIVE_TIMEZONE))
is_overdue = fields.Boolean()
21 changes: 20 additions & 1 deletion pay-api/src/pay_api/services/statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
"""Service class to control all the operations related to statements."""
from datetime import date, datetime
from typing import List

from flask import current_app
from sqlalchemy import func
Expand Down Expand Up @@ -122,7 +123,7 @@ def find_by_account_id(auth_account_id: str, page: int, limit: int):
"""Find statements by account id."""
current_app.logger.debug(f'<search_purchase_history {auth_account_id}')
statements, total = StatementModel.find_all_statements_for_account(auth_account_id, page, limit)

statements = Statement.populate_overdue_from_invoices(statements)
statements_schema = StatementModelSchema()
data = {
'total': total,
Expand Down Expand Up @@ -192,3 +193,21 @@ def get_summary(auth_account_id: str):
'total_due': total_due,
'oldest_overdue_date': oldest_overdue_date
}

@staticmethod
def populate_overdue_from_invoices(statements: List[StatementModel]):
"""Populate is_overdue field for statements."""
# Invoice status can change after a statement has been generated.
statement_ids = [statements.id for statements in statements]
overdue_statements = db.session.query(func.count(InvoiceModel.id).label('overdue_invoices'),
StatementInvoicesModel.statement_id) \
.join(StatementInvoicesModel) \
.filter(InvoiceModel.invoice_status_code == InvoiceStatus.OVERDUE.value) \
.filter(StatementInvoicesModel.invoice_id == InvoiceModel.id) \
.filter(StatementInvoicesModel.statement_id.in_(statement_ids)) \
.group_by(StatementInvoicesModel.statement_id) \
.all()
overdue_statements = {statement.statement_id: statement.overdue_invoices for statement in overdue_statements}
for statement in statements:
statement.is_overdue = overdue_statements.get(statement.id, 0) > 0
return statements
5 changes: 3 additions & 2 deletions pay-api/tests/unit/services/test_statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from pay_api.models import PaymentAccount
from pay_api.services.statement import Statement as StatementService
from pay_api.utils.enums import StatementFrequency
from pay_api.utils.enums import InvoiceStatus, StatementFrequency
from tests.utilities.base_test import (
factory_invoice, factory_invoice_reference, factory_payment, factory_payment_line_item,
factory_premium_payment_account, factory_statement, factory_statement_invoices, factory_statement_settings,
Expand All @@ -33,7 +33,7 @@ def test_statement_find_by_account(session):

payment = factory_payment()
payment.save()
i = factory_invoice(payment_account=bcol_account)
i = factory_invoice(payment_account=bcol_account, status_code=InvoiceStatus.OVERDUE.value)
i.save()
factory_invoice_reference(i.id).save()

Expand All @@ -48,6 +48,7 @@ def test_statement_find_by_account(session):
statements = StatementService.find_by_account_id(payment_account.auth_account_id, page=1, limit=10)
assert statements is not None
assert statements.get('total') == 1
assert statements.get('items')[0].get('is_overdue') is True


def test_get_statement_report(session):
Expand Down

0 comments on commit 69e6a5e

Please sign in to comment.