{"files": {"main.py": "from core import run\nfrom policy import POLICY\ndef evaluate(payload):\n    return run(payload, POLICY)\n", "core.py": "import copy\nfrom decimal import Decimal, ROUND_HALF_UP\nfrom helpers import line_to_cents, lines_total_cents, deduplicate_events, order_events\n\ndef run(payload, policy):\n    data = copy.deepcopy(payload)\n    if data is None:\n        return {\"invoices\": []}\n    if isinstance(data, list):\n        invoices_in = data\n    elif isinstance(data, dict):\n        invoices_in = data.get(\"invoices\", [])\n        if invoices_in is None:\n            invoices_in = []\n    else:\n        return {\"invoices\": []}\n    currency = policy.get(\"currency\")\n    rounding = policy.get(\"rounding\", \"per_line\")\n    min_invoice_cents = policy.get(\"min_invoice_cents\", 0)\n    duplicate_event = policy.get(\"duplicate_event\", \"first\")\n    refunds_first = policy.get(\"refunds_first\", False)\n    void_is_tombstone = policy.get(\"void_is_tombstone\", False)\n    max_refund_age_days = policy.get(\"max_refund_age_days\", 0)\n    inclusive_limit = policy.get(\"inclusive_limit\", False)\n    fee_refundable = policy.get(\"fee_refundable\", False)\n    refund_overflow = policy.get(\"refund_overflow\", \"cap\")\n    status_case = policy.get(\"status_case\", \"lower\")\n    order = policy.get(\"order\", \"id\")\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        inv_id = inv.get(\"id\")\n        created = inv.get(\"created\", 0)\n        lines = inv.get(\"lines\", []) or []\n        fee = inv.get(\"fee_cents\", 0) or 0\n        fee = int(fee)\n        principal = lines_total_cents(lines, rounding)\n        charge = principal + fee\n        if charge < min_invoice_cents:\n            continue\n        events = inv.get(\"events\", []) or []\n        deduped = deduplicate_events(list(events), duplicate_event)\n        ordered = order_events(deduped, refunds_first)\n        ceiling = principal + fee if fee_refundable else principal\n        active = True\n        tombstoned = False\n        refunded = 0\n        for ev in ordered:\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                if cents < 0:\n                    continue\n                if refund_overflow == \"reject\":\n                    if refunded + cents > ceiling:\n                        continue\n                    refunded += cents\n                else:\n                    remaining = ceiling - refunded\n                    if remaining <= 0:\n                        continue\n                    refunded += cents if cents <= remaining else remaining\n            else:\n                continue\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        else:\n            status = status.lower()\n        results.append((created, inv_id, {\"id\": inv_id, \"charge\": charge, \"refunded\": refunded, \"balance\": charge - refunded, \"status\": status}))\n    if order == \"created\":\n        results.sort(key=lambda x: (x[0], x[1]))\n    else:\n        results.sort(key=lambda x: x[1])\n    return {\"invoices\": [r[2] for r in results]}\n", "policy.py": "POLICY = {\n    \"currency\": \"USD\",\n    \"fee_refundable\": True,\n    \"min_invoice_cents\": 10,\n    \"status_case\": \"lower\",\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}\n", "helpers.py": "from decimal import Decimal, ROUND_HALF_UP\n\ndef _to_decimal(value):\n    return Decimal(str(value))\n\ndef line_to_cents(value):\n    cents = (_to_decimal(value) * Decimal(\"100\")).quantize(Decimal(\"1\"), rounding=ROUND_HALF_UP)\n    return int(cents)\n\ndef lines_total_cents(lines, rounding):\n    if rounding == \"per_line\":\n        total = 0\n        for line in lines:\n            total += line_to_cents(line)\n        return total\n    total_dollars = Decimal(\"0\")\n    for line in lines:\n        total_dollars += _to_decimal(line)\n    cents = (total_dollars * Decimal(\"100\")).quantize(Decimal(\"1\"), rounding=ROUND_HALF_UP)\n    return int(cents)\n\ndef deduplicate_events(events, mode):\n    mapping = {}\n    for ev in events:\n        if not isinstance(ev, dict):\n            continue\n        eid = ev.get(\"eid\")\n        if mode == \"last\":\n            mapping[eid] = ev\n        else:\n            if eid not in mapping:\n                mapping[eid] = ev\n    return list(mapping.values())\n\ndef order_events(events, refunds_first):\n    if not refunds_first:\n        return list(events)\n    refunds = [e for e in events if isinstance(e, dict) and e.get(\"kind\") == \"refund\"]\n    others = [e for e in events if not (isinstance(e, dict) and e.get(\"kind\") == \"refund\")]\n    return refunds + others\n"}}