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

IN 1127 - Handle missing fund_distribution entries with empty strings #273

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading