Skip to content

Commit

Permalink
Handle missing fund_distribution
Browse files Browse the repository at this point in the history
Why these changes are being introduced:

PO lines were pulled from Alma where the fund_distribution node in the
object contained a fund with empty strings for values like 'amount.sum'.
This threw an error when attemping to convert to a Decimal.

Default behavior is to return '$0.00' when a price cannot be determined
from the PO line, which will extend to this scenario.

How this addresses that need:
* get_total_price_from_fund_distribution is updated to handle a fund entry
with empty strings, and treat them as '$0.00'

Side effects of this change:
* Emails will successfully generate with '$0.00' reported when the fund
distribution information is empty strings

Relevant ticket(s):
* https://mitlibraries.atlassian.net/browse/IN-1127
  • Loading branch information
ghukill committed Dec 16, 2024
1 parent f99c771 commit ff18a1d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.idea/

.DS_Store
output/
Expand Down
17 changes: 9 additions & 8 deletions ccslips/polines.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def extract_credit_card_slip_data(client: AlmaClient, po_line_record: dict) -> d
fund_distribution = po_line_record.get("fund_distribution", [])
price = Decimal(po_line_record.get("price", {}).get("sum", "0.00"))
title = po_line_record.get("resource_metadata", {}).get("title", "Unknown title")

po_line_data = {
"cardholder": get_cardholder_from_notes(po_line_record.get("note")),
"invoice_number": (
Expand Down Expand Up @@ -81,13 +80,15 @@ def get_total_price_from_fund_distribution(
If no amounts or amount sums are listed in the fund distribution, the unit price is
returned as the total price.
"""
return (
sum(
Decimal(fund.get("amount", {}).get("sum", "0.00"))
for fund in fund_distribution
)
or unit_price
)
fund_amounts = []
for fund in fund_distribution:
fund_amount = fund.get("amount", {}).get("sum", "0.00")
# handle edge case where fund_distribution has funds with empty strings
if fund_amount == "":
fund_amount = "0.00"
fund_amounts.append(Decimal(fund_amount))

return sum(fund_amounts) or unit_price


def get_account_data(client: AlmaClient, fund_distribution: list[dict]) -> dict[str, str]:
Expand Down
20 changes: 20 additions & 0 deletions tests/test_polines.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,26 @@ def test_get_total_price_from_fund_distribution_no_amounts_or_sums_returns_unit_
) == Decimal("1.23")


def test_get_total_price_from_fund_distribution_contains_empty_string_values():
price = Decimal("4.56")
fund_distribution = [
{
"amount": {
"currency": {},
"sum": "",
},
"fund_code": {
"desc": "",
"value": "",
},
},
]
assert (
po.get_total_price_from_fund_distribution(fund_distribution, unit_price=price)
== price
)


def test_get_total_price_from_fund_distribution_sums_fund_amounts():
funds = [
{"fund_code": {"value": "amount-is-zero"}, "amount": {"sum": "0.00"}},
Expand Down

0 comments on commit ff18a1d

Please sign in to comment.