From 1252731b60a65b0233b9821d4364a02d880ab0d8 Mon Sep 17 00:00:00 2001 From: Travis Semple Date: Fri, 6 Sep 2024 14:46:39 -0700 Subject: [PATCH] small tweaks --- .../tasks/ejv_partner_distribution_task.py | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/jobs/payment-jobs/tasks/ejv_partner_distribution_task.py b/jobs/payment-jobs/tasks/ejv_partner_distribution_task.py index 1f4de5045..d800936fd 100644 --- a/jobs/payment-jobs/tasks/ejv_partner_distribution_task.py +++ b/jobs/payment-jobs/tasks/ejv_partner_distribution_task.py @@ -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 @@ -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}', @@ -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( @@ -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] @@ -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 @@ -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.