fix: Distribute multi-month payments by per-month expected fee
reconcile() previously split a multi-month payment evenly across months, which falsely flagged months as underpaid when their expected fees differed (e.g. 1250 CZK for 02+03+04 2026 with rates 750/350/150 was shown as 416/month with two months red). The allocation now runs per matched member: greedy when the share covers the total expected (each month gets its expected fee, surplus -> credit), proportional by expected fee otherwise. Out-of-window months keep the previous even-split-to-credit behavior. 6 new test cases. Also adds CHANGELOG.md and a changelog convention in CLAUDE.md. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -385,8 +385,7 @@ def reconcile(
|
||||
})
|
||||
continue
|
||||
|
||||
num_allocations = len(matched_members) * len(matched_months)
|
||||
per_allocation = amount / num_allocations if num_allocations > 0 else 0
|
||||
member_share = amount / len(matched_members) if matched_members else 0
|
||||
|
||||
for member_name, confidence in matched_members:
|
||||
if member_name not in ledger:
|
||||
@@ -397,20 +396,64 @@ def reconcile(
|
||||
unmatched.append(tx)
|
||||
continue
|
||||
|
||||
for month_key in matched_months:
|
||||
entry = {
|
||||
"amount": per_allocation,
|
||||
"date": tx["date"],
|
||||
"sender": tx["sender"],
|
||||
"message": tx["message"],
|
||||
"confidence": confidence,
|
||||
}
|
||||
if month_key in ledger[member_name]:
|
||||
ledger[member_name][month_key]["paid"] += per_allocation
|
||||
ledger[member_name][month_key]["transactions"].append(entry)
|
||||
else:
|
||||
# Future month — track as credit
|
||||
credits[member_name] = credits.get(member_name, 0) + int(per_allocation)
|
||||
in_window = [(m, ledger[member_name][m]["expected"]) for m in matched_months if m in ledger[member_name]]
|
||||
out_of_window = [m for m in matched_months if m not in ledger[member_name]]
|
||||
|
||||
# Out-of-window months (outside display range): even split → credit, same as before.
|
||||
n_total = len(matched_months)
|
||||
if out_of_window and n_total > 0:
|
||||
out_credit = member_share / n_total * len(out_of_window)
|
||||
credits[member_name] = credits.get(member_name, 0) + int(out_credit)
|
||||
else:
|
||||
out_credit = 0.0
|
||||
|
||||
in_window_share = member_share - out_credit
|
||||
|
||||
if not in_window:
|
||||
continue
|
||||
|
||||
total_expected = sum(e for _, e in in_window)
|
||||
|
||||
if total_expected > 0 and in_window_share >= total_expected:
|
||||
# Greedy phase: payment covers all in-window fees; overflow → credit.
|
||||
credits[member_name] = credits.get(member_name, 0) + int(in_window_share - total_expected)
|
||||
for m, exp in in_window:
|
||||
alloc = float(exp)
|
||||
ledger[member_name][m]["paid"] += alloc
|
||||
ledger[member_name][m]["transactions"].append({
|
||||
"amount": alloc,
|
||||
"date": tx["date"],
|
||||
"sender": tx["sender"],
|
||||
"message": tx["message"],
|
||||
"confidence": confidence,
|
||||
})
|
||||
elif total_expected > 0:
|
||||
# Proportional phase: distribute in_window_share by each month's expected fee.
|
||||
# Last month absorbs any float remainder so the sum equals in_window_share exactly.
|
||||
remaining = in_window_share
|
||||
for i, (m, exp) in enumerate(in_window):
|
||||
alloc = remaining if i == len(in_window) - 1 else in_window_share * exp / total_expected
|
||||
remaining -= alloc
|
||||
ledger[member_name][m]["paid"] += alloc
|
||||
ledger[member_name][m]["transactions"].append({
|
||||
"amount": alloc,
|
||||
"date": tx["date"],
|
||||
"sender": tx["sender"],
|
||||
"message": tx["message"],
|
||||
"confidence": confidence,
|
||||
})
|
||||
else:
|
||||
# Fallback: no expected fees (prepayment before attendance recorded); even split.
|
||||
per_month = in_window_share / len(in_window)
|
||||
for m, _ in in_window:
|
||||
ledger[member_name][m]["paid"] += per_month
|
||||
ledger[member_name][m]["transactions"].append({
|
||||
"amount": per_month,
|
||||
"date": tx["date"],
|
||||
"sender": tx["sender"],
|
||||
"message": tx["message"],
|
||||
"confidence": confidence,
|
||||
})
|
||||
|
||||
# Calculate final total balances (window + off-window credits)
|
||||
final_balances: dict[str, int] = {}
|
||||
|
||||
Reference in New Issue
Block a user