Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21519 - Rework partner disbursement job so it works with multiple link and unlinking #1637

Merged
merged 62 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
922bdeb
Fix filename off by 1 ms bug
seeker25 Jul 18, 2024
c3fa976
21519 - Work on partner disbursements
seeker25 Sep 6, 2024
caaebc8
Fix filename off by 1 ms bug
seeker25 Jul 18, 2024
151ef1e
21519 - Work on partner disbursements
seeker25 Sep 6, 2024
b59a21f
Merge branch '21519' of https://github.com/seeker25/sbc-pay into 21519
seeker25 Sep 6, 2024
769b969
Fix missing import again
seeker25 Sep 6, 2024
2b94dad
fix pylock
seeker25 Sep 6, 2024
cb23f39
lint issues
seeker25 Sep 6, 2024
18a5486
small changes wiring up eft_short_names service
seeker25 Sep 9, 2024
b42b98c
comment
seeker25 Sep 9, 2024
0e59f10
fix partnerDisbursements in reversal for statements, reversal for inv…
seeker25 Sep 9, 2024
f2866ad
check for amount
seeker25 Sep 9, 2024
5f24314
lint fix
seeker25 Sep 9, 2024
da18381
Merge branch 'main' of https://github.com/bcgov/sbc-pay into 21519
seeker25 Sep 13, 2024
0b6b7ae
more tweaks
seeker25 Sep 13, 2024
8cd1241
Small unit test changes
seeker25 Sep 16, 2024
cc87b50
Merge branch 'main' of https://github.com/bcgov/sbc-pay into 21519
seeker25 Sep 16, 2024
409b3ea
update pay-api for pay-queue
seeker25 Sep 16, 2024
85a23f2
update to bcgov repo
seeker25 Sep 16, 2024
8ce43e0
Merge branch 'main' of https://github.com/bcgov/sbc-pay into 21519
seeker25 Sep 16, 2024
ae0cb68
lint + test fix
seeker25 Sep 16, 2024
e43609d
more minors
seeker25 Sep 16, 2024
cefa6fe
Fix migration
seeker25 Sep 16, 2024
f849162
update pay-queue poetry
seeker25 Sep 16, 2024
40339a3
Merge branch 'main' of https://github.com/bcgov/sbc-pay into 21519
seeker25 Sep 17, 2024
9f2587f
refactor pay-queue, plus add in extra for partner disbursements
seeker25 Sep 17, 2024
e1fee05
Cleanup
seeker25 Sep 17, 2024
5cba60a
cleanup
seeker25 Sep 17, 2024
c5cd16a
add encoding
seeker25 Sep 17, 2024
943cc0f
comment move
seeker25 Sep 17, 2024
5c93533
type hint
seeker25 Sep 17, 2024
a5d29d8
small tweak
seeker25 Sep 17, 2024
d6a6924
tweak
seeker25 Sep 17, 2024
8dc8885
changes for job
seeker25 Sep 17, 2024
cbe0610
Fix unit tests for jobs
seeker25 Sep 17, 2024
f932e7d
more clean up
seeker25 Sep 17, 2024
f12de89
Unit tests and fixes
seeker25 Sep 17, 2024
d9988e9
text
seeker25 Sep 17, 2024
a603190
Small tweak for unit test failures
seeker25 Sep 17, 2024
1f6bf08
Fix unit test by limiting to 1 character for invoice id
seeker25 Sep 17, 2024
f3f5b1b
Merge branch 'main' of https://github.com/bcgov/sbc-pay into 21519
seeker25 Oct 1, 2024
207df2e
update poetry lock
seeker25 Oct 1, 2024
8987b82
Clean up migrations
seeker25 Oct 1, 2024
90b6041
shuffle migrations up
seeker25 Oct 1, 2024
7b71327
Fix dep
seeker25 Oct 1, 2024
2d5e7ca
Fixes when doing side by side testing
seeker25 Oct 1, 2024
0452279
Fix line numbers
seeker25 Oct 1, 2024
c29620b
exclude service fees from partner disbursements model
seeker25 Oct 1, 2024
082c8d8
small fix for service fees
seeker25 Oct 1, 2024
f0f0306
minor tweaks
seeker25 Oct 2, 2024
bd5779f
Merge branch 'main' of https://github.com/bcgov/sbc-pay into 21519
seeker25 Oct 2, 2024
33545c9
fix migration
seeker25 Oct 2, 2024
e540d73
resync
seeker25 Oct 2, 2024
175c1d5
Tweak partial refund
seeker25 Oct 2, 2024
21bef6f
remove comment
seeker25 Oct 2, 2024
27ae40c
migration to populate partner disbursements
seeker25 Oct 2, 2024
543e74b
lint fixes
seeker25 Oct 2, 2024
468f2b0
Small tweak
seeker25 Oct 2, 2024
7221d7e
update reference
seeker25 Oct 2, 2024
e756f8e
rename enum
seeker25 Oct 2, 2024
64c1486
Change reversal code a tad
seeker25 Oct 3, 2024
d96c7c8
Merge branch 'main' of https://github.com/bcgov/sbc-pay into 21519
seeker25 Oct 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions jobs/payment-jobs/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion jobs/payment-jobs/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ readme = "README.md"

[tool.poetry.dependencies]
python = "^3.12"
pay-api = {git = "https://github.com/ochiu/sbc-pay.git", branch = "22982-EFT-Wire", subdirectory = "pay-api"}
pay-api = {git = "https://github.com/seeker25/sbc-pay.git", branch = "21519", subdirectory = "pay-api"}
flask = "^3.0.2"
flask-sqlalchemy = "^3.1.1"
sqlalchemy = "^2.0.28"
Expand Down
57 changes: 49 additions & 8 deletions jobs/payment-jobs/tasks/ap_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from typing import List

from datetime import date, datetime, timezone
from datetime import date, datetime, timedelta, timezone
import time
from flask import current_app
from more_itertools import batched
Expand All @@ -25,13 +25,15 @@
from pay_api.models import EjvHeader as EjvHeaderModel
from pay_api.models import EjvLink as EjvLinkModel
from pay_api.models import Invoice as InvoiceModel
from pay_api.models import Receipt as ReceiptModel
from pay_api.models import Refund as RefundModel
from pay_api.models import RoutingSlip as RoutingSlipModel
from pay_api.models import db
from pay_api.utils.enums import DisbursementStatus, EjvFileType, EJVLinkType, RoutingSlipStatus
from pay_api.utils.enums import (
DisbursementStatus, EjvFileType, EJVLinkType, InvoiceStatus, PaymentMethod, RoutingSlipStatus)
from sqlalchemy import Date, cast
from tasks.common.cgi_ap import CgiAP
from tasks.common.dataclasses import APLine
from tasks.ejv_partner_distribution_task import EjvPartnerDistributionTask


class ApTask(CgiAP):
Expand Down Expand Up @@ -106,13 +108,52 @@ def _create_routing_slip_refund_file(cls): # pylint:disable=too-many-locals, to

cls._create_file_and_upload(ap_content)

@classmethod
def get_invoices_for_disbursement(cls, partner):
"""Return invoices for disbursement. Used by EJV and AP."""
disbursement_date = datetime.now(tz=timezone.utc).replace(tzinfo=None) \
- timedelta(days=current_app.config.get('DISBURSEMENT_DELAY_IN_DAYS'))
invoices: List[InvoiceModel] = db.session.query(InvoiceModel) \
.filter(InvoiceModel.invoice_status_code == InvoiceStatus.PAID.value) \
.filter(
InvoiceModel.payment_method_code.notin_([PaymentMethod.INTERNAL.value,
PaymentMethod.DRAWDOWN.value,
PaymentMethod.EFT.value])) \
.filter((InvoiceModel.disbursement_status_code.is_(None)) |
(InvoiceModel.disbursement_status_code == DisbursementStatus.ERRORED.value)) \
.filter(~InvoiceModel.receipts.any(cast(ReceiptModel.receipt_date, Date) >= disbursement_date.date())) \
.filter(InvoiceModel.corp_type_code == partner.code) \
.all()
current_app.logger.info(invoices)
return invoices

@classmethod
def get_invoices_for_refund_reversal(cls, partner):
"""Return invoices for refund reversal."""
# REFUND_REQUESTED for credit card payments, CREDITED for AR and REFUNDED for other payments.
refund_inv_statuses = (InvoiceStatus.REFUNDED.value, InvoiceStatus.REFUND_REQUESTED.value,
InvoiceStatus.CREDITED.value)

invoices: List[InvoiceModel] = db.session.query(InvoiceModel) \
.filter(InvoiceModel.invoice_status_code.in_(refund_inv_statuses)) \
.filter(
InvoiceModel.payment_method_code.notin_([PaymentMethod.INTERNAL.value,
PaymentMethod.DRAWDOWN.value,
PaymentMethod.EFT.value])) \
.filter(InvoiceModel.disbursement_status_code == DisbursementStatus.COMPLETED.value) \
.filter(InvoiceModel.corp_type_code == partner.code) \
.all()
current_app.logger.info(invoices)
return invoices

@classmethod
def _create_non_gov_disbursement_file(cls): # pylint:disable=too-many-locals
"""Create AP file for disbursement for non government entities without a GL code via EFT and upload to CGI."""
cls.ap_type = EjvFileType.NON_GOV_DISBURSEMENT
bca_partner = CorpTypeModel.find_by_code('BCA')
total_invoices: List[InvoiceModel] = EjvPartnerDistributionTask().get_invoices_for_disbursement(bca_partner) + \
EjvPartnerDistributionTask().get_invoices_for_refund_reversal(bca_partner)
# TODO these two functions need to be reworked when we onboard BCA again.
total_invoices: List[InvoiceModel] = cls.get_invoices_for_disbursement(bca_partner) + \
cls.get_invoices_for_refund_reversal(bca_partner)

current_app.logger.info(f'Found {len(total_invoices)} to disburse.')
if not total_invoices:
Expand Down Expand Up @@ -168,10 +209,10 @@ def _create_non_gov_disbursement_file(cls): # pylint:disable=too-many-locals

@classmethod
def _create_file_and_upload(cls, ap_content):
file_path_with_name, trg_file_path = cls.create_inbox_and_trg_files(ap_content)
cls.upload(ap_content, cls.get_file_name(), file_path_with_name, trg_file_path)
file_path_with_name, trg_file_path, file_name = cls.create_inbox_and_trg_files(ap_content)
cls.upload(ap_content, file_name, file_path_with_name, trg_file_path)
db.session.commit()
# Add a sleep to prevent collision on file name.
# Sleep to prevent collision on file name.
time.sleep(1)

@classmethod
Expand Down
5 changes: 3 additions & 2 deletions jobs/payment-jobs/tasks/common/cgi_ejv.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ def get_trg_suffix(cls):
def create_inbox_and_trg_files(cls, ejv_content):
"""Create inbox and trigger files."""
file_path: str = tempfile.gettempdir()
file_path_with_name = f'{file_path}/{cls.get_file_name()}'
file_name = cls.get_file_name()
file_path_with_name = f'{file_path}/{file_name}'
trg_file_path = f'{file_path_with_name}.{cls.get_trg_suffix()}'
with open(file_path_with_name, 'a+', encoding='utf-8') as jv_file:
jv_file.write(ejv_content)
Expand All @@ -157,4 +158,4 @@ def create_inbox_and_trg_files(cls, ejv_content):
with open(trg_file_path, 'a+', encoding='utf-8') as trg_file:
trg_file.write('')
trg_file.close()
return file_path_with_name, trg_file_path
return file_path_with_name, trg_file_path, file_name
26 changes: 26 additions & 0 deletions jobs/payment-jobs/tasks/common/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,41 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""Common dataclasses for tasks, dataclasses allow for cleaner code with autocompletion in vscode."""
from decimal import Decimal

from dataclasses import dataclass
from typing import List, Optional
from dataclass_wizard import JSONWizard
from pay_api.models import DistributionCode as DistributionCodeModel
from pay_api.models import Invoice as InvoiceModel
from pay_api.models import PartnerDisbursements as PartnerDisbursementModel
from pay_api.models import PaymentLineItem as LineItemModel
from pay_api.utils.enums import InvoiceStatus
from tasks.common.enums import PaymentDetailsGlStatus


@dataclass
class DisbursementLineItem:
"""DTO mapping for disbursement line item."""

amount: Decimal
flow_through: str
description_identifier: str
is_reversal: bool
target_type: str
identifier: int


@dataclass
class Disbursement:
"""DTO mapping for disbursement."""

bcreg_distribution_code: DistributionCodeModel
partner_distribution_code: DistributionCodeModel
line_item: DisbursementLineItem
seeker25 marked this conversation as resolved.
Show resolved Hide resolved
target: InvoiceModel | PartnerDisbursementModel


@dataclass
class RefundData(JSONWizard):
"""Refund data from order status query."""
Expand Down
Loading
Loading