Skip to content

Commit

Permalink
small tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
seeker25 committed Sep 6, 2024
1 parent 3687bbb commit 1252731
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions jobs/payment-jobs/tasks/ejv_partner_distribution_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
from pay_api.models import Receipt as ReceiptModel
from pay_api.models import db
from pay_api.utils.enums import DisbursementStatus, EjvFileType, EJVLinkType, InvoiceStatus, PaymentMethod
from sqlalchemy import Date, cast
from sqlalchemy import Date, and_, cast

from decimal import Decimal
from tasks.common.cgi_ejv import CgiEjv
from tasks.common.dataclasses import Disbursement, DisbursementLineItem

Expand Down Expand Up @@ -91,7 +92,7 @@ def get_disbursement_by_distribution_for_partner(partner) -> List[Disbursement]:
for invoice, payment_line_item, distribution_code in transactions + reversals:
disbursement_rows.append(Disbursement(
bcreg_distribution_code=distribution_code,
partner_distribution_code=distribution_code.partner_distribution_code,
partner_distribution_code=distribution_code.disbursement_distribution_code,
line_item=DisbursementLineItem(
amount=payment_line_item.total,
flow_through=f'{invoice.id:<110}',
Expand All @@ -107,20 +108,22 @@ def get_disbursement_by_distribution_for_partner(partner) -> List[Disbursement]:
# ################################################################# END OF Legacy way of handling disbursements.

# Partner disbursements - New

# TODO include partial refunds
partner_disbursements = db.session.query(PartnerDisbursementsModel,
PaymentLineItemModel,
DistributionCodeModel) \
.join(PaymentLineItemModel, PaymentLineItemModel.invoice_id == PartnerDisbursementsModel.target_id,
PartnerDisbursementsModel.target_type == EJVLinkType.INVOICE.value) \
.join(PaymentLineItemModel, and_(PaymentLineItemModel.invoice_id == PartnerDisbursementsModel.target_id,
PartnerDisbursementsModel.target_type == EJVLinkType.INVOICE.value)) \
.join(DistributionCodeModel,
DistributionCodeModel.distribution_code_id == PaymentLineItemModel.fee_distribution_id) \
.filter(PartnerDisbursementsModel.status_code.is_(None)) \
.filter(PartnerDisbursementsModel.corp_type_code == partner.code) \
.filter(PartnerDisbursementsModel.partner_code == partner.code) \
.filter(DistributionCodeModel.stop_ejv.is_(False) | DistributionCodeModel.stop_ejv.is_(None)) \
.order_by(DistributionCodeModel.distribution_code_id, PaymentLineItemModel.id) \
.all()

for partner_disbursement, distribution_code in partner_disbursements:
for partner_disbursement, payment_line_item, distribution_code in partner_disbursements:
suffix = 'PR' if partner_disbursement.disbursement_type == EJVLinkType.PARTIAL_REFUND else ''
flow_through = f'{partner.target_id}-{partner_disbursement.id}{suffix}'
disbursement_rows.append(Disbursement(
Expand All @@ -136,13 +139,13 @@ def get_disbursement_by_distribution_for_partner(partner) -> List[Disbursement]:
is_legacy=False
)
))
disbursement_rows.sort(key=lambda x: x.bcreg_distribution_code.id)
disbursement_rows.sort(key=lambda x: x.bcreg_distribution_code.distribution_code_id)
return disbursement_rows

@classmethod
def _create_ejv_file_for_partner(cls, batch_type: str): # pylint:disable=too-many-locals, too-many-statements
"""Create EJV file for the partner and upload."""
ejv_content, batch_total, control_total = '', 0.0, 0
ejv_content, batch_total, control_total = '', Decimal('0'), Decimal('0')
today = datetime.now(tz=timezone.utc)
disbursement_desc = current_app.config.get('CGI_DISBURSEMENT_DESC'). \
format(today.strftime('%B').upper(), f'{today.day:0>2}')[:100]
Expand Down Expand Up @@ -172,7 +175,7 @@ def _create_ejv_file_for_partner(cls, batch_type: str): # pylint:disable=too-ma
# debit_distribution and credit_distribution stays as is for invoices which are not PAID
ejv_content = '{}{}'.format(ejv_content, # pylint:disable=consider-using-f-string
cls.get_jv_header(batch_type, cls.get_journal_batch_name(batch_number),
journal_name, disbursement.total))
journal_name, disbursement.line_item.amount))
control_total += 1
line_number = 1
batch_total += disbursement.line_item.amount
Expand All @@ -181,7 +184,7 @@ def _create_ejv_file_for_partner(cls, batch_type: str): # pylint:disable=too-ma
description = f'{description[:100]:<100}'
for credit_debit_row in range(1, 2):
target_distribution = cls.get_distribution_string(
dl.partner_distribution_code if credit_debit_row == 1 else dl.bcreg_distribution_code
disbursement.partner_distribution_code if credit_debit_row == 1 else disbursement.bcreg_distribution_code
)
# For payment flow, credit the GL partner code, debit the BCREG GL code.
# Reversal is the opposite debit the GL partner code, credit the BCREG GL Code.
Expand Down

0 comments on commit 1252731

Please sign in to comment.