diff --git a/reconcile.py b/reconcile.py index a0ceeb8..36ca6a8 100644 --- a/reconcile.py +++ b/reconcile.py @@ -1,10 +1,62 @@ -from decimal import Decimal +from decimal import Decimal, ROUND_HALF_UP + +_CENT = Decimal("0.01") +_ZERO = Decimal("0.00") + + +def _clean(value): + """Strip text values; leave non-strings untouched.""" + return value.strip() if isinstance(value, str) else value + + +def _revision(row): + return int(row["revision"]) + + +def _is_void(row): + return str(row["status"]).strip().lower() == "void" + + +def _amount(row): + """Exact Decimal amount; never called for losing or void rows.""" + value = row["amount"] + if isinstance(value, Decimal): + return value + return Decimal(str(value)) + + +def _money(value): + """Round to cents, printing a zero balance as 0.00.""" + rounded = value.quantize(_CENT, rounding=ROUND_HALF_UP) + if rounded == 0: + rounded = _ZERO + return str(rounded) + def reconcile(rows): - """Return the total per vendor for accepted rows.""" - totals = {} + """Return the total per vendor for accepted rows. + + Rows are grouped by (stripped vendor, stripped invoice), keeping case. + The winner of a group is the row with the largest integer revision; on a + tie the later row in input order wins. A group whose winner is void + contributes nothing at all. + """ + # rows may be a single-pass iterator, so resolve winners in one scan. + 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 = (_clean(row["vendor"]), _clean(row["invoice"])) + revision = _revision(row) + current = winners.get(key) + # ">=" so that a later row wins a tie on revision. + if current is None or revision >= current[0]: + winners[key] = (revision, row) + + totals = {} + for key, entry in winners.items(): + row = entry[1] + if _is_void(row): + continue + vendor = key[0] + totals[vendor] = totals.get(vendor, Decimal(0)) + _amount(row) + + return {vendor: _money(value) for vendor, value in totals.items()} diff --git a/test_reconcile.py b/test_reconcile.py index 63ce8d5..a61b966 100644 --- a/test_reconcile.py +++ b/test_reconcile.py @@ -1,8 +1,110 @@ from unittest import TestCase from reconcile import reconcile + +def row(vendor="A", invoice="1", revision=1, status="ok", amount="0"): + return {"vendor": vendor, "invoice": invoice, "revision": revision, + "status": status, "amount": amount} + + class Tests(TestCase): def test_single(self): self.assertEqual(reconcile([ {"vendor": "A", "invoice": "17", "revision": 1, "status": "ok", "amount": "4"} ]), {"A": "4.00"}) + + def test_highest_revision_wins_numerically(self): + self.assertEqual(reconcile([ + row(revision=9, amount="9"), + row(revision=10, amount="10"), + row(revision=2, amount="2"), + ]), {"A": "10.00"}) + + def test_tie_on_revision_takes_later_row(self): + self.assertEqual(reconcile([ + row(revision=3, amount="1"), + row(revision=3, amount="2"), + ]), {"A": "2.00"}) + + def test_void_winner_drops_whole_group(self): + self.assertEqual(reconcile([ + row(invoice="1", revision=1, amount="5"), + row(invoice="1", revision=2, status="void", amount="99"), + row(invoice="2", revision=1, amount="1"), + ]), {"A": "1.00"}) + + def test_all_groups_void_leaves_vendor_out(self): + self.assertEqual(reconcile([ + row(vendor="A", invoice="1", revision=1, status="void", amount="5"), + row(vendor="B", invoice="1", revision=1, amount="2"), + ]), {"B": "2.00"}) + + def test_status_is_stripped_and_case_insensitive(self): + self.assertEqual(reconcile([ + row(status=" VoId ", amount="7"), + ]), {}) + + def test_vendor_and_invoice_are_stripped_case_preserved(self): + self.assertEqual(reconcile([ + row(vendor=" Acme ", invoice=" 7 ", amount="1"), + row(vendor="Acme", invoice="7", amount="2"), + row(vendor="acme", invoice="7", amount="4"), + row(vendor="Acme", invoice="7", amount="8", revision=2), + ]), {"Acme": "8.00", "acme": "4.00"}) + + def test_amounts_of_losing_rows_are_never_parsed(self): + self.assertEqual(reconcile([ + row(revision=1, amount="not-a-number"), + row(revision=2, amount="1.5"), + row(invoice="2", revision=1, status="void", amount="junk"), + ]), {"A": "1.50"}) + + def test_decimal_sum_is_exact_and_rounded_half_up_once_per_vendor(self): + self.assertEqual(reconcile([ + row(invoice="1", amount="0.1"), + row(invoice="2", amount="0.2"), + row(invoice="3", amount="0.005"), + ]), {"A": "0.31"}) + + def test_half_up_at_the_cent(self): + self.assertEqual(reconcile([ + row(invoice="1", amount="2.675"), + ]), {"A": "2.68"}) + self.assertEqual(reconcile([ + row(invoice="1", amount="0.004"), + ]), {"A": "0.00"}) + + def test_zero_total_keeps_two_decimals(self): + self.assertEqual(reconcile([ + row(invoice="1", amount="5"), + row(invoice="2", amount="-5"), + ]), {"A": "0.00"}) + + def test_rounds_to_zero_prints_as_positive_zero(self): + self.assertEqual(reconcile([ + row(invoice="1", amount="-0.004"), + ]), {"A": "0.00"}) + + def test_multiple_vendors_are_summed_separately(self): + self.assertEqual(reconcile([ + row(vendor="A", amount="1.111"), + row(vendor="B", amount="2.222"), + row(vendor="A", invoice="2", amount="3.333"), + ]), {"A": "4.44", "B": "2.22"}) + + def test_accepts_single_pass_generator(self): + def rows(): + yield row(invoice="1", revision=1, amount="1") + yield row(invoice="1", revision=2, amount="2") + yield row(invoice="2", revision=1, amount="0.5") + + self.assertEqual(reconcile(rows()), {"A": "2.50"}) + + def test_input_rows_are_not_mutated(self): + rows = [ + row(vendor=" A ", invoice=" 1 ", status=" OK ", amount="1"), + row(vendor="B", invoice="2", revision=2, amount="2"), + ] + snapshot = [dict(item) for item in rows] + reconcile(rows) + self.assertEqual(rows, snapshot)