diff --git a/reconcile.py b/reconcile.py index a0ceeb8..8a9f34b 100644 --- a/reconcile.py +++ b/reconcile.py @@ -1,10 +1,28 @@ -from decimal import Decimal +from decimal import Decimal, ROUND_HALF_UP, localcontext def reconcile(rows): - """Return the total per vendor for accepted rows.""" - totals = {} + """Return each vendor's total from its latest non-void invoices.""" + winners = {} for row in rows: - if row["status"] != "void": - vendor = row["vendor"] - totals[vendor] = totals.get(vendor, Decimal(0)) + Decimal(row["amount"]) - return {vendor: str(value.quantize(Decimal("0.01"))) for vendor, value in totals.items()} + key = (row["vendor"].strip(), row["invoice"].strip()) + revision = int(row["revision"]) + previous = winners.get(key) + if previous is None or revision >= previous[0]: + winners[key] = (revision, row.copy()) + + amounts_by_vendor = {} + for (vendor, _), (_, row) in winners.items(): + if row["status"].strip().casefold() != "void": + amounts_by_vendor.setdefault(vendor, []).append(Decimal(row["amount"])) + + totals = {} + cent = Decimal("0.01") + with localcontext() as context: + for vendor, amounts in amounts_by_vendor.items(): + # Cover all input places and cents, with room for carries in the sum. + highest_place = max(amount.adjusted() for amount in amounts) + lowest_place = min(-2, min(amount.as_tuple().exponent for amount in amounts)) + context.prec = max(1, highest_place - lowest_place + 1) + len(str(len(amounts))) + total = sum(amounts, Decimal(0)).quantize(cent, rounding=ROUND_HALF_UP) + totals[vendor] = str(total.copy_abs() if total.is_zero() else total) + return totals diff --git a/test_reconcile.py b/test_reconcile.py index 63ce8d5..926c289 100644 --- a/test_reconcile.py +++ b/test_reconcile.py @@ -6,3 +6,51 @@ class Tests(TestCase): self.assertEqual(reconcile([ {"vendor": "A", "invoice": "17", "revision": 1, "status": "ok", "amount": "4"} ]), {"A": "4.00"}) + + def test_uses_highest_integer_revision_then_last_row_and_strips_keys(self): + rows = [ + {"vendor": " A ", "invoice": " INV ", "revision": "9", "status": "ok", "amount": "bad"}, + {"vendor": "A", "invoice": "INV", "revision": "10", "status": "void", "amount": "bad"}, + {"vendor": "A ", "invoice": " INV", "revision": 10, "status": " OK ", "amount": "2"}, + {"vendor": "A", "invoice": "INV", "revision": 8, "status": "void", "amount": "bad"}, + {"vendor": "A", "invoice": "inv", "revision": 1, "status": "ok", "amount": "3"}, + {"vendor": "a", "invoice": "INV", "revision": 1, "status": "ok", "amount": "4"}, + ] + original = [row.copy() for row in rows] + + self.assertEqual(reconcile(row for row in rows), {"A": "5.00", "a": "4.00"}) + self.assertEqual(rows, original) + + def test_generator_can_reuse_a_row_dictionary(self): + def rows(): + row = {"vendor": "A", "invoice": "1", "revision": 2, "status": "ok", "amount": "2"} + yield row + row.update(revision=1, amount="bad") + yield row + + self.assertEqual(reconcile(rows()), {"A": "2.00"}) + + def test_void_winner_discards_group_without_parsing_amounts(self): + self.assertEqual(reconcile([ + {"vendor": "A", "invoice": "1", "revision": 2, "status": "ok", "amount": "bad"}, + {"vendor": "A", "invoice": "1", "revision": 2, "status": " \tVoId\n"}, + {"vendor": "A", "invoice": "2", "revision": 1, "status": "ok", "amount": "3"}, + {"vendor": "B", "invoice": "1", "revision": 1, "status": " VOID ", "amount": "bad"}, + ]), {"A": "3.00"}) + + def test_rounds_vendor_total_half_up_and_keeps_zero_totals(self): + self.assertEqual(reconcile([ + {"vendor": "A", "invoice": "1", "revision": 1, "status": "ok", "amount": "0.002"}, + {"vendor": "A", "invoice": "2", "revision": 1, "status": "ok", "amount": "0.003"}, + {"vendor": "B", "invoice": "1", "revision": 1, "status": "ok", "amount": "-0.005"}, + {"vendor": "C", "invoice": "1", "revision": 1, "status": "ok", "amount": "-0.004"}, + {"vendor": "D", "invoice": "1", "revision": 1, "status": "ok", "amount": "1"}, + {"vendor": "D", "invoice": "2", "revision": 1, "status": "ok", "amount": "-1"}, + ]), {"A": "0.01", "B": "-0.01", "C": "0.00", "D": "0.00"}) + + def test_sum_preserves_small_amounts_when_large_amounts_cancel(self): + self.assertEqual(reconcile([ + {"vendor": "A", "invoice": "1", "revision": 1, "status": "ok", "amount": "1e28"}, + {"vendor": "A", "invoice": "2", "revision": 1, "status": "ok", "amount": "0.005"}, + {"vendor": "A", "invoice": "3", "revision": 1, "status": "ok", "amount": "-1e28"}, + ]), {"A": "0.01"})