Skip to content

Commit

Permalink
AP disbursement changes + statement tweak. (#1133)
Browse files Browse the repository at this point in the history
* AP disbursement changes.

* Lint / test fixes.

* Minor tweak for generate_statements

* Remove -delta

* More tweaks.

* Back to using personal repos.
  • Loading branch information
seeker25 authored Mar 1, 2023
1 parent 8dab172 commit 93d0c09
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 20 deletions.
2 changes: 1 addition & 1 deletion jobs/payment-jobs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-e git+https://github.com/bcgov/sbc-common-components.git@a643801c373063dfa44a2f7213d9122d3ad03851#egg=sbc_common_components&subdirectory=python
-e git+https://github.com/bcgov/sbc-pay.git@e952ae4da28387fce8f7e706d47be56ff6858135#egg=pay_api&subdirectory=pay-api
-e git+https://github.com/seeker25/sbc-pay.git@aafc77ad4b1e0681f44580e14a4f559936c5d96e#egg=pay_api&subdirectory=pay-api
Flask-Caching==2.0.2
Flask-Migrate==2.7.0
Flask-Moment==1.0.5
Expand Down
3 changes: 1 addition & 2 deletions jobs/payment-jobs/tasks/ap_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ def _create_routing_slip_refund_file(cls): # pylint:disable=too-many-locals, to
current_app.logger.info(f'Creating refund for {rs.number}, Amount {rs.refund_amount}.')
refund: RefundModel = RefundModel.find_by_routing_slip_id(rs.id)
ap_content = f'{ap_content}{cls.get_ap_header(rs.refund_amount, rs.number, datetime.now())}'
ap_line = APLine(total=rs.refund_amount, invoice_number=rs.number, line_number=1,
invoice_date=datetime.now())
ap_line = APLine(total=rs.refund_amount, invoice_number=rs.number, line_number=1)
ap_content = f'{ap_content}{cls.get_ap_invoice_line(ap_line)}'
ap_content = f'{ap_content}{cls.get_ap_address(refund.details, rs.number)}'
total_line_count += 3
Expand Down
15 changes: 8 additions & 7 deletions jobs/payment-jobs/tasks/common/cgi_ap.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ def get_ap_header(cls, total, invoice_number, invoice_date):
invoice_type = 'ST'
remit_code = f"{current_app.config.get('CGI_AP_REMITTANCE_CODE'):<4}"
currency = 'CAD'
invoice_date = cls._get_invoice_date(invoice_date)
effective_date = cls._get_date(datetime.now())
invoice_date = cls._get_date(invoice_date)
oracle_invoice_batch_name = cls._get_oracle_invoice_batch_name(invoice_number)
disbursement_method = 'CHQ' if cls.ap_type == EjvFileType.REFUND else 'EFT'
term = f'{cls.EMPTY:<50}' if cls.ap_type == EjvFileType.REFUND else f'Immediate{cls.EMPTY:<41}'
ap_header = f'{cls._feeder_number()}APIH{cls.DELIMITER}{cls._supplier_number()}{cls._supplier_location()}' \
f'{invoice_number:<50}{cls._po_number()}{invoice_type}{invoice_date}GEN {disbursement_method} N' \
f'{remit_code}{cls.format_amount(total)}{currency}{invoice_date}' \
f'{remit_code}{cls.format_amount(total)}{currency}{effective_date}' \
f'{term}{cls.EMPTY:<60}{cls.EMPTY:<8}{cls.EMPTY:<8}' \
f'{oracle_invoice_batch_name:<30}{cls.EMPTY:<9}Y{cls.EMPTY:<110}{cls.DELIMITER}{os.linesep}'
return ap_header
Expand All @@ -65,12 +66,12 @@ def get_ap_invoice_line(cls, ap_line: APLine):
commit_line_number = f'{cls.EMPTY:<4}'
# Pad Zeros to four digits. EG. 0001
line_number = f'{ap_line.line_number:04}'
invoice_date = cls._get_invoice_date(ap_line.invoice_date)
effective_date = cls._get_date(datetime.now())
line_code = cls._get_line_code(ap_line)
ap_line = \
f'{cls._feeder_number()}APIL{cls.DELIMITER}{cls._supplier_number()}{cls._supplier_location()}' \
f'{ap_line.invoice_number:<50}{line_number}{commit_line_number}{cls.format_amount(ap_line.total)}' \
f'{line_code}{cls._distribution(ap_line.distribution)}{cls.EMPTY:<55}{invoice_date}{cls.EMPTY:<10}' \
f'{line_code}{cls._distribution(ap_line.distribution)}{cls.EMPTY:<55}{effective_date}{cls.EMPTY:<10}' \
f'{cls.EMPTY:<15}{cls.EMPTY:<15}{cls.EMPTY:<15}{cls.EMPTY:<15}{cls.EMPTY:<20}{cls.EMPTY:<4}' \
f'{cls.EMPTY:<30}{cls.EMPTY:<25}{cls.EMPTY:<30}{cls.EMPTY:<8}{cls.EMPTY:<1}{cls._dist_vendor()}' \
f'{cls.EMPTY:<110}{cls.DELIMITER}{os.linesep}'
Expand Down Expand Up @@ -151,9 +152,9 @@ def _po_number(cls):
return f'{cls.EMPTY:<20}'

@classmethod
def _get_invoice_date(cls, invoice_date):
"""Return invoice date."""
return invoice_date.strftime('%Y%m%d')
def _get_date(cls, date):
"""Return date."""
return date.strftime('%Y%m%d')

@classmethod
def _distribution(cls, distribution_code: str = None):
Expand Down
4 changes: 1 addition & 3 deletions jobs/payment-jobs/tasks/common/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.
"""Common dataclasses for tasks, dataclasses allow for cleaner code with autocompletion in vscode."""
from dataclasses import dataclass
from datetime import datetime
from typing import List, Optional
from dataclass_wizard import JSONWizard
from pay_api.models import Invoice as InvoiceModel
Expand Down Expand Up @@ -45,7 +44,6 @@ class APLine:
total: float
invoice_number: str
line_number: int
invoice_date: Optional[datetime] = None
is_reversal: Optional[bool] = None
distribution: Optional[str] = None

Expand All @@ -54,7 +52,7 @@ def from_invoice_and_line_item(cls, invoice: InvoiceModel, line_item: LineItemMo
distribution: str):
"""Build dataclass object from invoice."""
# Note the invoice_date should be the payment_date in the future.
return cls(total=line_item.total, invoice_number=invoice.id, invoice_date=invoice.created_on,
return cls(total=line_item.total, invoice_number=invoice.id,
line_number=line_number,
is_reversal=invoice.invoice_status_code in
[InvoiceStatus.REFUNDED.value, InvoiceStatus.REFUND_REQUESTED.value],
Expand Down
9 changes: 4 additions & 5 deletions jobs/payment-jobs/tasks/statement_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.
"""Service to manage PAYBC services."""

from datetime import datetime
from datetime import datetime, timedelta

from dateutil.parser import parse
from flask import current_app
Expand Down Expand Up @@ -41,11 +41,10 @@ def generate_statements(cls, date_override=None):
1. Get all payment accounts and it's active statement settings.
"""
target_time = get_local_time(datetime.now()) if date_override is None \
else datetime.strptime(date_override, '%Y-%m-%d')
else datetime.strptime(date_override, '%Y-%m-%d') + timedelta(days=1)
cls.skip_notify = date_override is not None
if date_override:
current_app.logger.debug(f'Generating statements for: {date_override} using date override,'
' this generates for the previous day/week/month.')
current_app.logger.debug(f'Generating statements for: {date_override} using date override.')
# If today is sunday - generate all weekly statements for pervious week
# If today is month beginning - generate all monthly statements for previous month
# For every day generate all daily statements for previous day
Expand Down Expand Up @@ -98,7 +97,7 @@ def _generate_monthly_statements(cls, target_time: datetime):
statement_settings = StatementSettingsModel.find_accounts_settings_by_frequency(previous_day,
StatementFrequency.MONTHLY)
current_app.logger.debug(f'Found {len(statement_settings)} accounts to generate MONTHLY statements')
last_month, last_month_year = get_previous_month_and_year()
last_month, last_month_year = get_previous_month_and_year(target_time)
search_filter = {
'monthFilter': {
'month': last_month,
Expand Down
4 changes: 2 additions & 2 deletions pay-api/src/pay_api/utils/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ def get_first_and_last_dates_of_month(month: int, year: int):
return start_date, end_date


def get_previous_month_and_year():
def get_previous_month_and_year(target_date=datetime.now()):
"""Return last month and year."""
last_month = datetime.now().replace(day=1) - timedelta(days=1)
last_month = target_date.replace(day=1) - timedelta(days=1)
return last_month.month, last_month.year


Expand Down

0 comments on commit 93d0c09

Please sign in to comment.