{"files":{"main.py":"from core import run\nfrom policy import POLICY\ndef evaluate(payload):\n    return run(payload, POLICY)\n","policy.py":"POLICY = {\"currency\": \"USD\", \"duplicate_event\": \"first\", \"fee_refundable\": True, \"inclusive_limit\": True, \"max_refund_age_days\": 30, \"min_invoice_cents\": 10, \"order\": \"id\", \"refund_overflow\": \"cap\", \"refunds_first\": True, \"rounding\": \"per_line\", \"status_case\": \"lower\", \"void_is_tombstone\": True}\n","helpers.py":"from decimal import Decimal, ROUND_HALF_UP\n\ndef to_cents(s):\n    d = Decimal(str(s).strip()) * 100\n    return int(d.quantize(Decimal(1), rounding=ROUND_HALF_UP))\n\ndef dedupe(events, mode):\n    d = {}\n    for e in events:\n        k = e['eid']\n        if k in d:\n            if mode == 'last':\n                d[k] = e\n        else:\n            d[k] = e\n    return list(d.values())\n","core.py":"from decimal import Decimal, ROUND_HALF_UP\nfrom helpers import to_cents, dedupe\n\ndef run(payload, policy):\n    invs = (payload or {}).get('invoices') or []\n    out = []\n    for inv in invs:\n        if inv.get('currency') != policy['currency']:\n            continue\n        lines = inv.get('lines') or []\n        if policy['rounding'] == 'per_line':\n            principal = sum(to_cents(x) for x in lines)\n        else:\n            tot = sum((Decimal(str(x).strip()) for x in lines), Decimal(0)) * 100\n            principal = int(tot.quantize(Decimal(1), rounding=ROUND_HALF_UP))\n        fee = inv.get('fee_cents', 0)\n        charge = principal + fee\n        if charge < policy['min_invoice_cents']:\n            continue\n        events = dedupe(inv.get('events') or [], policy['duplicate_event'])\n        if policy['refunds_first']:\n            events = [e for e in events if e['kind'] == 'refund'] + [e for e in events if e['kind'] != 'refund']\n        ceiling = charge if policy['fee_refundable'] else principal\n        active = True\n        tomb = False\n        refunded = 0\n        created = inv['created']\n        for e in events:\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'] - created\n                lim = policy['max_refund_age_days']\n                if age < 0:\n                    continue\n                if age > lim or (age == lim and not policy['inclusive_limit']):\n                    continue\n                remaining = ceiling - refunded\n                c = e['cents']\n                if c > remaining:\n                    if policy['refund_overflow'] == 'cap':\n                        c = remaining\n                    else:\n                        continue\n                refunded += c\n        if not active:\n            st = 'void'\n        elif refunded == charge:\n            st = 'refunded'\n        else:\n            st = 'open'\n        st = st.upper() if policy['status_case'] == 'upper' else st.lower()\n        out.append({'id': inv['id'], 'charge': charge, 'refunded': refunded,\n                    'balance': charge - refunded, 'status': st, '_c': created})\n    if policy['order'] == 'created_id':\n        out.sort(key=lambda r: (r['_c'], r['id']))\n    else:\n        out.sort(key=lambda r: r['id'])\n    for r in out:\n        del r['_c']\n    return {'invoices': out}\n"}}