fix(pdf): drop zero-amount rows; multi-date rows clean description
Two corrections from real-statement feedback:
**1. Drop rows where the transaction amount is exactly 0.**
Bank statements include date+amount-shaped noise like
"INTEREST EARNED 0.00", "PAGE TOTAL 0.00", "BALANCE FORWARD
0.00 1,234.56" — all match the date+amount heuristic but
aren't transactions. New filter in
``scan_pdf_for_transactions``: drop rows whose ``amount_1``
parses to exactly 0. Non-zero balances in ``amount_2`` don't
rescue a zero amount_1 — leftmost amount is the canonical
transaction amount. Unparsed-but-non-empty amount strings are
kept (user verifies in the editor).
**2. Multi-date rows: first date wins for the column, every
date excluded from the description.** Chase / BofA / Wells
commonly show both a transaction date and a posting date per
row:
01/13 01/14 COFFEE SHOP $4.50
Before this fix, ``_find_dates_in_words`` returned the first
date only and the second date leaked into description as
"01/14 COFFEE SHOP". Now it returns ALL dates with their word
ranges; the scanner uses ``dates[0]`` as the canonical date
and passes every range to the description builder for
exclusion.
The detector's two-pass strategy now also guards against
mixing full-year and short-date matches on the same row.
Previously, a header line like ``Page 1/2 of 3 ... Statement
Date 01/13/2026`` would return both ``1/2`` and ``01/13/2026``,
and ``1/2`` (being leftmost) would have won the date column.
Now: if any full-year date is found on the row, short patterns
are NOT also collected — full year anchors interpretation. A
row with no full-year date (Chase short-date case) still falls
back to short patterns and collects all of them.
New tests:
- ``test_multiple_dates_returned_in_position_order`` —
``01/13`` + ``01/14`` both returned, in order
- ``TestMultiDateRow.test_first_date_wins_second_excluded_from_description``
— end-to-end through ``scan_pdf_for_transactions``
- ``TestZeroAmountRowsAreDropped.test_zero_amount_row_dropped``
— "INTEREST EARNED 0.00" row dropped while real txn kept
- ``test_negative_amount_kept`` — pin that -40.00 is not
treated as zero by the filter
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -151,14 +151,29 @@ class TestFindDatesInWords:
|
||||
def test_short_pattern_does_not_shadow_full_year(self):
|
||||
"""If a full-year date is present, short patterns shouldn't
|
||||
steal — e.g. ``Page 1/2 of 3 ... 01/13/2026 Coffee`` should
|
||||
return the real ``01/13/2026``, not the ``1/2`` page marker."""
|
||||
return the real ``01/13/2026`` first."""
|
||||
row = [
|
||||
_w("Page", 0, 0), _w("1/2", 40, 0), _w("of", 80, 0),
|
||||
_w("3", 100, 0),
|
||||
_w("01/13/2026", 200, 0), _w("Coffee", 300, 0),
|
||||
]
|
||||
result = _find_dates_in_words(row)
|
||||
assert result and result[0][2] == "01/13/2026"
|
||||
# Full-year match wins position 0 in the returned list.
|
||||
assert result[0][2] == "01/13/2026"
|
||||
|
||||
def test_multiple_dates_returned_in_position_order(self):
|
||||
"""Chase-style transaction with both posting and txn dates."""
|
||||
row = [
|
||||
_w("01/13", 0, 0), _w("01/14", 50, 0),
|
||||
_w("Coffee", 100, 0), _w("$4.50", 200, 0),
|
||||
]
|
||||
result = _find_dates_in_words(row)
|
||||
assert len(result) == 2
|
||||
assert result[0][2] == "01/13"
|
||||
assert result[1][2] == "01/14"
|
||||
# First date claims word 0, second claims word 1
|
||||
assert result[0][:2] == (0, 1)
|
||||
assert result[1][:2] == (1, 2)
|
||||
|
||||
def test_no_date(self):
|
||||
row = [_w("Just", 0, 0), _w("text", 50, 0)]
|
||||
|
||||
Reference in New Issue
Block a user