Skip to content

Commit

Permalink
Merge pull request #273 from MITLibraries/IN-1127-empty-string-funds
Browse files Browse the repository at this point in the history
IN 1127 - Handle missing fund_distribution entries with empty strings
  • Loading branch information
ghukill authored Dec 16, 2024
2 parents f99c771 + ff18a1d commit 2cd7b12
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 2cd7b12

Please sign in to comment.