Skip to content

Commit

Permalink
Add in extra params so statement due task can be called with auth_acc…
Browse files Browse the repository at this point in the history
…ount_id param (bcgov#1708)
  • Loading branch information
seeker25 authored Aug 27, 2024
1 parent bba30ee commit 99c08c2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
9 changes: 6 additions & 3 deletions jobs/payment-jobs/invoke_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,12 @@ def run(job_name, argument=None):
UnpaidInvoiceNotifyTask.notify_unpaid_invoices()
application.logger.info('<<<< Completed Sending notification for OB invoices >>>>')
case 'STATEMENTS_DUE':
action_date_override = argument[0] if len(argument) == 1 else None
StatementDueTask.process_unpaid_statements(action_date_override=action_date_override)
application.logger.info('<<<< Completed Sending notification for unpaid statements >>>>')
action_date_override = argument[0] if len(argument) >= 1 else None
auth_account_override = argument[1] if len(argument) >= 2 else None
StatementDueTask.process_unpaid_statements(action_date_override=action_date_override,
auth_account_override=auth_account_override)
application.logger.info(
'<<<< Completed Sending notification for unpaid statements >>>>')
case 'ROUTING_SLIP':
RoutingSlipTask.link_routing_slips()
RoutingSlipTask.process_void()
Expand Down
8 changes: 2 additions & 6 deletions jobs/payment-jobs/tasks/cfs_create_invoice_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,8 @@ def _cancel_rs_invoices(cls):
for receipt in receipts:
CFSService.unapply_receipt(cfs_account, receipt.receipt_number,
invoice_reference.invoice_number)

# Adjust to zero: -invoice.total + invoice.total = 0
adjustment_negative_amount = -invoice.total
CFSService.adjust_invoice(cfs_account=cfs_account,
inv_number=invoice_reference.invoice_number,
amount=adjustment_negative_amount)
# This used to be adjust invoice, but the suggested way from Tara is to use reverse invoice.
CFSService.reverse_invoice(invoice_reference.invoice_number)

except Exception as e: # NOQA # pylint: disable=broad-except
capture_message(
Expand Down
30 changes: 25 additions & 5 deletions jobs/payment-jobs/tasks/statement_due_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from datetime import datetime, timedelta, timezone
from dateutil.relativedelta import relativedelta

import pytz
from flask import current_app
from pay_api.models import db
from pay_api.models.cfs_account import CfsAccount as CfsAccountModel
Expand Down Expand Up @@ -51,14 +52,17 @@ class StatementDueTask: # pylint: disable=too-few-public-methods
unpaid_status = [InvoiceStatus.SETTLEMENT_SCHEDULED.value, InvoiceStatus.PARTIAL.value,
InvoiceStatus.APPROVED.value]
action_date_override = None
auth_account_override = None
statement_date_override = None

@classmethod
def process_unpaid_statements(cls, statement_date_override=None, action_date_override=None):
def process_unpaid_statements(cls, action_date_override=None,
auth_account_override=None, statement_date_override=None):
"""Notify for unpaid statements with an amount owing."""
eft_enabled = flags.is_on('enable-eft-payment-method', default=False)
if eft_enabled:
cls.action_date_override = action_date_override
cls.auth_account_override = auth_account_override
cls.statement_date_override = statement_date_override
cls._update_invoice_overdue_status()
cls._notify_for_monthly()
Expand All @@ -67,12 +71,23 @@ def process_unpaid_statements(cls, statement_date_override=None, action_date_ove
def _update_invoice_overdue_status(cls):
"""Update the status of any invoices that are overdue."""
# Needs to be non timezone aware.
now = datetime.now(tz=timezone.utc).replace(tzinfo=None)
if cls.action_date_override:
now = datetime.strptime(cls.action_date_override, '%Y-%m-%d').replace(hour=8)
offset_hours = -now.astimezone(pytz.timezone('America/Vancouver')).utcoffset().total_seconds() / 60 / 60
now = now.replace(hour=int(offset_hours), minute=0, second=0)
else:
now = datetime.now(tz=timezone.utc).replace(tzinfo=None)
query = db.session.query(InvoiceModel) \
.filter(InvoiceModel.payment_method_code == PaymentMethod.EFT.value,
InvoiceModel.overdue_date.isnot(None),
InvoiceModel.overdue_date <= now,
InvoiceModel.invoice_status_code.in_(cls.unpaid_status))
if cls.auth_account_override:
current_app.logger.info(f'Using auth account override for auth_account_id: {cls.auth_account_override}')
payment_account_id = db.session.query(PaymentAccountModel.id) \
.filter(PaymentAccountModel.auth_account_id == cls.auth_account_override) \
.one()
query = query.filter(InvoiceModel.payment_account_id == payment_account_id[0])
query.update({InvoiceModel.invoice_status_code: InvoiceStatus.OVERDUE.value}, synchronize_session='fetch')
db.session.commit()

Expand Down Expand Up @@ -101,6 +116,10 @@ def _notify_for_monthly(cls):
StatementFrequency.MONTHLY)
eft_payment_accounts = [pay_account for _, pay_account in statement_settings
if pay_account.payment_method == PaymentMethod.EFT.value]
if cls.auth_account_override:
current_app.logger.info(f'Using auth account override for auth_account_id: {cls.auth_account_override}')
eft_payment_accounts = [pay_account for pay_account in eft_payment_accounts
if pay_account.auth_account_id == cls.auth_account_override]

current_app.logger.info(f'Processing {len(eft_payment_accounts)} EFT accounts for monthly reminders.')
for payment_account in eft_payment_accounts:
Expand Down Expand Up @@ -179,8 +198,10 @@ def _determine_action_and_due_date_by_invoice(cls, statement: StatementModel):
seven_days_before_invoice_due = day_invoice_due - timedelta(days=7)

# Needs to be non timezone aware for comparison.
now_date = datetime.strptime(cls.action_date_override, '%Y-%m-%d').date() if cls.action_date_override \
else datetime.now(tz=timezone.utc).replace(tzinfo=None).date()
if cls.action_date_override:
now_date = datetime.strptime(cls.action_date_override, '%Y-%m-%d').date()
else:
now_date = datetime.now(tz=timezone.utc).replace(tzinfo=None).date()

if invoice.invoice_status_code == InvoiceStatus.OVERDUE.value:
return StatementNotificationAction.OVERDUE, day_invoice_due
Expand All @@ -194,7 +215,6 @@ def _determine_action_and_due_date_by_invoice(cls, statement: StatementModel):
def _determine_recipient_emails(cls, statement: StatementRecipientsModel) -> str:
if (recipients := StatementRecipientsModel.find_all_recipients_for_payment_id(statement.payment_account_id)):
recipients = ','.join([str(recipient.email) for recipient in recipients])

return recipients

current_app.logger.error(f'No recipients found for payment_account_id: {statement.payment_account_id}. Skip.')
Expand Down
2 changes: 1 addition & 1 deletion jobs/payment-jobs/tests/jobs/test_statement_due_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,6 @@ def test_overdue_invoices_updated(setup, session):
assert invoice2.invoice_status_code == InvoiceStatus.APPROVED.value
assert account.payment_method == PaymentMethod.EFT.value

StatementDueTask.process_unpaid_statements()
StatementDueTask.process_unpaid_statements(auth_account_override=account.auth_account_id)
assert invoice.invoice_status_code == InvoiceStatus.OVERDUE.value
assert invoice2.invoice_status_code == InvoiceStatus.APPROVED.value

0 comments on commit 99c08c2

Please sign in to comment.