Match bills to charges that were split before the matcher ran
Problem
Splitting a transaction marks the parent entry excluded so its children are not counted twice. RecurringTransaction::Matcher#entries_for filtered on excluded: false, so a charge split before the matcher ran was never considered: the bill's occurrence stayed unpaid and turned overdue although the payment was there. Run the matcher first and split afterwards, and the same charge settles the bill. The result depended on which ran first.
The occurrence drawer's ranked candidate list (RecurringOccurrencesController#ranked_candidates) uses the same filter, so the split charge was missing from the suggestions there as well.
Fix
New scope Entry.not_excluded_or_split_parent: entries that are not excluded, plus split parents, which split! excludes so the children are not counted twice. The matcher and the ranked picker both use it. Every other excluded entry is still skipped: user exclusions and system ones (stale pending entries, pending duplicates of a posted transaction). The split-parent EXISTS subquery is now one private constant shared with excluding_split_parents.
A split charge whose children are all excluded still settles the bill: the bank took the money, and this is what already happened when the matcher ran before the split.
I checked the rest of the bill pipeline. The Identifier, the Cleaner (matching_transactions), orphan repair and the fallback picker do not filter on excluded, so they were not affected.
A split parent and its children pay once
A split parent and its children are the same money, and children keep the parent's merchant. With two bills from one merchant, one charge could settle both: a 15.99 charge split 5.00 + 10.99 paid a 15.99 bill through the parent and a 5.00 bill through a child, 20.99 of bills against 15.99 spent. Upstream already did this when the matcher ran before the split, and keeping split parents in the matcher made it happen when the split came first too.
Allocator#guard_entry_capacity! now refuses an entry whose split counterpart (Entry#split_counterparts: its parent, or its children) already has an allocation, suggestions included, with SplitCounterpartAllocatedError and its own message. Siblings are separate money, so each part can still pay its own bill, and no side can take more than the parent between them. Because the guard sits in the Allocator, it covers the matcher, the manual picker and both orders of split and match.
Allocator.remaining_by_entryreports the other side as used up, in the same single query, so the drawer shows it that way.- When the guard refuses a candidate, the matcher leaves the occurrence open for another charge.
- On equal confidence the matcher tries the parent before a child, so splitting before or after matching ends the same way.
Tests
matcher_test.rb: a charge split before matching settles the bill with a confirmed allocation on the parent; a split charge whose children are all excluded still settles the bill; a charge the user excluded matches nothing; with two bills from one merchant a split charge pays one, whether it was split before or after matching; the parts of a split can each pay their own bill.allocator_test.rb: a parent and its children never both pay (a suggestion included), siblings can, andremaining_by_entryreports the other side as used up in one query.recurring_allocations_controller_test.rb: paying with the other side of a split says why.recurring_occurrences_controller_test.rb: the ranked picker offers the split charge and leaves out the excluded one.
Summary by cubic
Fixes bill matching for charges that were split before the matcher ran, so a split charge settles its bill whether the split happens before or after matching.
- The matcher and the occurrence drawer's ranked picker filtered on
excluded: false, which dropped split parents and left the bill unpaid and overdue. Both now use theEntry.not_excluded_or_split_parentscope, keeping split parents while still skipping user and system exclusions, and the split-parent check is a shared private constant. - A split parent and its children are the same money, so the allocator now refuses an entry whose counterpart (its parent or one of its children) already pays a bill, suggestions included.
remaining_by_entryshows that side as used up and ignores allocations from ended series, and the matcher leaves the occurrence open for another charge when the write is refused. - When a split parent and its child are both candidates, the parent claims its bill first regardless of confidence, and the allocator locks a split as one entry so the two sides cannot race. Splitting before or after matching ends the same way. A split with all children excluded still settles the bill; a charge the user excluded still matches nothing.
Summary by CodeRabbit
- Bug Fixes
- Improved recurring transaction matching for split charges by considering eligible split entries and prioritizing candidates whose parent is also a candidate.
- Prevented a split transaction and its counterpart from being allocated to separate bills, while allowing separate split parts to pay separate bills.
- Explicitly excluded charges remain ineligible for matching.
- Added an alert when an allocation conflicts with an already allocated split counterpart.
- Updated allocation capacity calculations to account for split counterparts and ignore allocations from ended recurring series.