I implemented this under the CURRENT policy; I have not run it. Several points in the spec were ambiguous, so I chose these readings:

- **Status case:** "upper" gives VOID, REFUNDED and OPEN.
- **Refund ceiling:** with fee_refundable false, the ceiling is the principal only (the rounded line total, excluding the fee).
- **Overflow:** with refund_overflow "reject", a refund whose cents exceed the remaining ceiling (ceiling minus refunded) is skipped entirely.
- **Refunded equals charge:** refunded can never exceed the principal, so with a nonzero fee and fee_refundable false, the status is never "refunded". The spec says to compare with charge, so I kept that comparison.
- **Duplicate events:** with "last", a repeated eid keeps its first position but takes the later event's value.
- **Void handling:** a void sets active to false. A restore only sets active to true when void_is_tombstone is false and no tombstone is set. With tombstone off, restore re-enables refunds.
- **Unknown kinds:** these are ignored.
- **Per-line rounding:** each line is rounded to cents with ROUND_HALF_UP, then summed. In invoice mode the exact decimal sum is rounded once.
- **Age check:** age = day − created must be at least 0 and below max_refund_age_days (equal allowed only when inclusive_limit is true).
- **Min invoice:** invoices whose charge is below min_invoice_cents are skipped.

```json
{"files": {"main.py": "from core import run\nfrom policy import POLICY\ndef evaluate(payload):\n    return run(payload, POLICY)\n", "policy.py": "POLICY = {\"currency\": \"CAD\", \"duplicate_event\": \"last\", \"fee_refundable\": False, \"inclusive_limit\": False, \"max_refund_age_days\": 45, \"min_invoice_cents\": 25, \"order\": \"created\", \"refund_overflow\": \"reject\", \"refunds_first\": False, \"rounding\": \"invoice\", \"status_case\": \"upper\", \"void_is_tombstone\": False}\n", "helpers.py": "from decimal import Decimal, ROUND_HALF_UP\n\ndef to_cents(d):\n    return int((d * 100).quantize(Decimal(1), rounding=ROUND_HALF_UP))\n\ndef principal(lines, mode):\n    ds = [Decimal(s) for s in lines]\n    if mode == 'per_line':\n        return sum((to_cents(d) for d in ds), 0)\n    return to_cents(sum(ds, Decimal(0)))\n\ndef dedupe(events, mode):\n    out = {}\n    for e in events:\n        if e['eid'] in out and mode != 'last':\n            continue\n        out[e['eid']] = e\n    return list(out.values())\n", "core.py": "from helpers import principal, dedupe\n\ndef run(payload, policy):\n    res = []\n    for inv in (payload or {}).get('invoices', []) or []:\n        if inv['currency'] != policy['currency']:\n            continue\n        p = principal(inv['lines'], policy['rounding'])\n        charge = p + inv['fee_cents']\n        if charge < policy['min_invoice_cents']:\n            continue\n        evs = dedupe(inv['events'], policy['duplicate_event'])\n        if policy['refunds_first']:\n            evs = [e for e in evs if e['kind'] == 'refund'] + [e for e in evs if e['kind'] != 'refund']\n        ceiling = p + (inv['fee_cents'] if policy['fee_refundable'] else 0)\n        active = True\n        tomb = False\n        refunded = 0\n        for e in evs:\n            k = e['kind']\n            if k == 'void':\n                active = False\n                if policy['void_is_tombstone']:\n                    tomb = True\n            elif k == 'restore':\n                if not tomb:\n                    active = True\n            elif k == 'refund':\n                if not active:\n                    continue\n                age = e['day'] - inv['created']\n                if age < 0:\n                    continue\n                lim = policy['max_refund_age_days']\n                if not (age < lim or (policy['inclusive_limit'] and age == lim)):\n                    continue\n                room = ceiling - refunded\n                amt = e['cents']\n                if amt > room:\n                    if policy['refund_overflow'] == 'reject':\n                        continue\n                    amt = room\n                refunded += amt\n        if not active:\n            st = 'void'\n        elif refunded == charge:\n            st = 'refunded'\n        else:\n            st = 'open'\n        if policy['status_case'] == 'upper':\n            st = st.upper()\n        res.append({'id': inv['id'], 'charge': charge, 'refunded': refunded,\n                    'balance': charge - refunded, 'status': st, '_c': inv['created']})\n    if policy['order'] == 'created':\n        res.sort(key=lambda r: (r['_c'], r['id']))\n    else:\n        res.sort(key=lambda r: r['id'])\n    for r in res:\n        del r['_c']\n    return {'invoices': res}\n"}}
```