From ff18a1d4aa494fac44289dbec8309853da3420ff Mon Sep 17 00:00:00 2001 From: Graham Hukill Date: Mon, 16 Dec 2024 14:29:48 -0500 Subject: [PATCH] Handle missing fund_distribution 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 --- .gitignore | 2 +- ccslips/polines.py | 17 +++++++++-------- tests/test_polines.py | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index d37c967..526ebcd 100644 --- a/.gitignore +++ b/.gitignore @@ -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/ diff --git a/ccslips/polines.py b/ccslips/polines.py index c49a2d4..f4b280f 100644 --- a/ccslips/polines.py +++ b/ccslips/polines.py @@ -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": ( @@ -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]: diff --git a/tests/test_polines.py b/tests/test_polines.py index 337604c..87f3901 100644 --- a/tests/test_polines.py +++ b/tests/test_polines.py @@ -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"}},