{"files": {"main.py": "from core import run\nfrom policy import POLICY\ndef evaluate(payload):\n    return run(payload, POLICY)\n", "core.py": "from helpers import compute_principal, deduplicate_events, order_events\n\ndef run(payload, policy):\n    if payload is None:\n        invoices_in = []\n    elif isinstance(payload, list):\n        invoices_in = list(payload)\n    elif isinstance(payload, dict):\n        raw = payload.get('invoices', [])\n        invoices_in = list(raw) if isinstance(raw, list) else []\n    else:\n        invoices_in = []\n    currency = policy.get('currency')\n    rounding = policy.get('rounding')\n    duplicate_event = policy.get('duplicate_event', 'first')\n    inclusive_limit = policy.get('inclusive_limit', False)\n    max_refund_age_days = policy.get('max_refund_age_days', 0)\n    refunds_first = policy.get('refunds_first', False)\n    refund_overflow = policy.get('refund_overflow', 'cap')\n    void_is_tombstone = policy.get('void_is_tombstone', False)\n    order = policy.get('order', 'id')\n    fee_refundable = policy.get('fee_refundable', False)\n    min_invoice_cents = policy.get('min_invoice_cents', 0)\n    status_case = policy.get('status_case', 'lower')\n    results = []\n    for inv in invoices_in:\n        if not isinstance(inv, dict):\n            continue\n        if inv.get('currency') != currency:\n            continue\n        lines = inv.get('lines', []) or []\n        fee = inv.get('fee_cents', 0) or 0\n        fee = int(fee)\n        principal = compute_principal(lines, rounding)\n        charge = principal + fee\n        if charge < min_invoice_cents:\n            continue\n        created = inv.get('created', 0)\n        events = inv.get('events', []) or []\n        events = deduplicate_events(list(events), duplicate_event)\n        events = order_events(events, refunds_first)\n        active = True\n        tombstoned = False\n        refunded = 0\n        ceiling = principal + fee if fee_refundable else principal\n        for ev in events:\n            if not isinstance(ev, dict):\n                continue\n            kind = ev.get('kind')\n            if kind == 'void':\n                active = False\n                if void_is_tombstone:\n                    tombstoned = True\n            elif kind == 'restore':\n                if not tombstoned:\n                    active = True\n            elif kind == 'refund':\n                if not active:\n                    continue\n                day = ev.get('day', 0)\n                try:\n                    age = day - created\n                except Exception:\n                    continue\n                if age < 0:\n                    continue\n                if inclusive_limit:\n                    if age > max_refund_age_days:\n                        continue\n                else:\n                    if age >= max_refund_age_days:\n                        continue\n                cents = ev.get('cents', 0) or 0\n                cents = int(cents)\n                remaining = ceiling - refunded\n                if remaining <= 0:\n                    continue\n                if cents > remaining:\n                    if refund_overflow == 'cap':\n                        cents = remaining\n                    else:\n                        continue\n                refunded += cents\n        balance = charge - refunded\n        if not active:\n            status = 'void'\n        elif refunded == charge:\n            status = 'refunded'\n        else:\n            status = 'open'\n        if status_case == 'upper':\n            status = status.upper()\n        elif status_case == 'lower':\n            status = status.lower()\n        elif status_case == 'title':\n            status = status.title()\n        results.append({'id': inv.get('id'), 'charge': charge, 'refunded': refunded, 'balance': balance, 'status': status, '_created': created})\n    if order == 'id':\n        results.sort(key=lambda x: x['id'])\n    else:\n        results.sort(key=lambda x: (x['_created'], x['id']))\n    out = [{'id': r['id'], 'charge': r['charge'], 'refunded': r['refunded'], 'balance': r['balance'], 'status': r['status']} for r in results]\n    return {'invoices': out}\n", "policy.py": "POLICY = {\n    \"currency\": \"USD\",\n    \"rounding\": \"per_line\",\n    \"duplicate_event\": \"first\",\n    \"inclusive_limit\": True,\n    \"max_refund_age_days\": 30,\n    \"refunds_first\": True,\n    \"refund_overflow\": \"cap\",\n    \"void_is_tombstone\": True,\n    \"order\": \"id\",\n    \"fee_refundable\": True,\n    \"min_invoice_cents\": 10,\n    \"status_case\": \"lower\",\n}\n", "helpers.py": "from decimal import Decimal, ROUND_HALF_UP\n\ndef _to_cents(dec_value):\n    return int((dec_value * Decimal(100)).quantize(Decimal('1'), rounding=ROUND_HALF_UP))\n\ndef compute_principal(lines, rounding):\n    if rounding == 'per_line':\n        total = 0\n        for line in lines:\n            total += _to_cents(Decimal(str(line)))\n        return total\n    total_dec = Decimal(0)\n    for line in lines:\n        total_dec += Decimal(str(line))\n    return _to_cents(total_dec)\n\ndef deduplicate_events(events, mode):\n    ordered = {}\n    extras = []\n    for ev in events:\n        if not isinstance(ev, dict) or 'eid' not in ev:\n            extras.append(ev)\n            continue\n        eid = ev.get('eid')\n        if mode == 'last':\n            ordered[eid] = ev\n        else:\n            if eid not in ordered:\n                ordered[eid] = ev\n    result = list(ordered.values())\n    return result + extras if extras else result\n\ndef order_events(events, refunds_first):\n    copied = list(events)\n    if not refunds_first:\n        return copied\n    refunds = [e for e in copied if isinstance(e, dict) and e.get('kind') == 'refund']\n    others = [e for e in copied if not (isinstance(e, dict) and e.get('kind') == 'refund')]\n    return refunds + others\n"}}