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

24432 - Bottom of EFT statement dynamic amount owing should be immutable #1829

Merged
merged 20 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
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
59 changes: 38 additions & 21 deletions pay-api/src/pay_api/services/payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def search_purchase_history( # pylint: disable=too-many-locals, too-many-argume
return data

@classmethod
def create_payment_report_details(cls, purchases: Tuple, data: Dict): # pylint:disable=too-many-locals
def create_payment_report_details(cls, purchases: Tuple, data: Dict) -> dict: # pylint:disable=too-many-locals
"""Return payment report details by fetching the line items.

purchases is tuple of payment and invoice model records.
Expand Down Expand Up @@ -415,30 +415,46 @@ def create_payment_report(auth_account_id: str, search_filter: Dict, content_typ
return report_response

@staticmethod
def get_invoices_totals(invoices):
def get_invoices_totals(invoices: dict, statement: dict) -> dict:
"""Tally up totals for a list of invoices."""
total_stat_fees = 0
total_service_fees = 0
total = 0
total_paid = 0
totals = {
"statutoryFees": 0,
"serviceFees": 0,
"fees": 0,
"paid": 0,
"due": 0,
}

for invoice in invoices:
total += invoice.get("total", 0)
total_stat_fees += invoice.get("total", 0) - invoice.get("service_fees", 0)

total_service_fees += invoice.get("service_fees", 0)
total_paid += invoice.get("paid", 0)

# Format date to local
invoice["created_on"] = get_local_formatted_date(parser.parse(invoice["created_on"]))
total = invoice.get("total", 0)
service_fees = invoice.get("service_fees", 0)
paid = invoice.get("paid", 0)
refund = invoice.get("refund", 0)
payment_method = invoice.get("payment_method")
payment_date = invoice.get("payment_date")
refund_date = invoice.get("refund_date")

totals["fees"] += total
totals["statutoryFees"] += total - service_fees
totals["serviceFees"] += service_fees
totals["due"] += total
if not statement or payment_method != PaymentMethod.EFT.value:
totals["due"] -= paid
totals["paid"] += paid
else:
if payment_date and parser.parse(payment_date) <= parser.parse(statement.get("to_date", "")):
totals["due"] -= paid
totals["paid"] += paid
# Scenario where payment was refunded, paid $0, refund = invoice total
if (
paid == 0
and refund_date
and parser.parse(refund_date) <= parser.parse(statement.get("to_date", ""))
):
totals["due"] -= refund

return {
"statutoryFees": total_stat_fees,
"serviceFees": total_service_fees,
"fees": total,
"paid": total_paid,
"due": total - total_paid,
}
return totals

@staticmethod
@user_context
Expand Down Expand Up @@ -479,7 +495,8 @@ def generate_payment_report(report_inputs: PaymentReportInput, **kwargs): # pyl
}
else:
invoices = results.get("items", None)
totals = Payment.get_invoices_totals(invoices)
statement = kwargs.get("statement", {})
totals = Payment.get_invoices_totals(invoices, statement)

account_info = None
if kwargs.get("auth", None):
Expand Down
11 changes: 6 additions & 5 deletions pay-api/src/pay_api/services/statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,13 @@ def get_statement_eft_transactions(
@classmethod
def _populate_statement_summary(cls, statement: StatementModel, statement_invoices: List[InvoiceModel]) -> dict:
"""Populate statement summary with additional information."""
previous_statement: StatementModel = Statement.get_previous_statement(statement)
previous_statement = Statement.get_previous_statement(statement)
previous_totals = None
if previous_statement:
previous_invoices = StatementModel.find_all_payments_and_invoices_for_statement(previous_statement.id)
previous_items: dict = PaymentService.create_payment_report_details(purchases=previous_invoices, data=None)
previous_totals = PaymentService.get_invoices_totals(previous_items.get("items", None))
previous_items = PaymentService.create_payment_report_details(purchases=previous_invoices, data=None)
# Skip passing statement, we need the totals independent of the statement/payment date.
previous_totals = PaymentService.get_invoices_totals(previous_items.get("items", None), None)

latest_payment_date = None
for invoice in statement_invoices:
Expand Down Expand Up @@ -384,7 +385,7 @@ def get_statement_report(statement_id: str, content_type: str, **kwargs):
from_date_string: str = statement_svc.from_date.strftime(DT_SHORT_FORMAT)
to_date_string: str = statement_svc.to_date.strftime(DT_SHORT_FORMAT)

extension: str = "pdf" if content_type == ContentType.PDF.value else "csv"
extension = "pdf" if content_type == ContentType.PDF.value else "csv"

if statement_svc.frequency == StatementFrequency.DAILY.value:
report_name = f"{report_name}-{from_date_string}.{extension}"
Expand All @@ -393,7 +394,7 @@ def get_statement_report(statement_id: str, content_type: str, **kwargs):

statement_purchases = StatementModel.find_all_payments_and_invoices_for_statement(statement_id)

result_items: dict = PaymentService.create_payment_report_details(purchases=statement_purchases, data=None)
result_items = PaymentService.create_payment_report_details(purchases=statement_purchases, data=None)
statement = statement_svc.asdict()
statement["from_date"] = from_date_string
statement["to_date"] = to_date_string
Expand Down
Loading
Loading