{"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 principal_cents, deduplicate_events, order_events, format_status\n\ndef run(payload, policy):\n    if payload is None:\n        return {\"invoices\": []}\n    if isinstance(payload, list):\n        invoices_in = list(payload)\n    elif isinstance(payload, dict):\n        raw = payload.get(\"invoices\", [])\n        invoices_in = list(raw) if raw else []\n    else:\n        return {\"invoices\": []}\n    currency = policy.get(\"currency\")\n    rounding = policy.get(\"rounding\", \"invoice\")\n    min_invoice_cents = policy.get(\"min_invoice_cents\", 0)\n    duplicate_event = policy.get(\"duplicate_event\", \"last\")\n    refunds_first = policy.get(\"refunds_first\", False)\n    fee_refundable = policy.get(\"fee_refundable\", 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    refund_overflow = policy.get(\"refund_overflow\", \"reject\")\n    status_case = policy.get(\"status_case\", \"upper\")\n    order = policy.get(\"order\", \"created\")\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\", inv.get(\"created_day\", inv.get(\"day\", 0)))\n        lines = inv.get(\"lines\", []) or []\n        fee_cents = int(inv.get(\"fee_cents\", 0) or 0)\n        principal = principal_cents(list(lines), rounding)\n        charge = principal + fee_cents\n        if charge < min_invoice_cents:\n            continue\n        events_raw = list(inv.get(\"events\", []) or [])\n        events = deduplicate_events(events_raw, duplicate_event)\n        events = order_events(events, refunds_first)\n        active = True\n        tombstone = False\n        refunded = 0\n        ceiling = principal + (fee_cents if fee_refundable else 0)\n        for event in events:\n            if not isinstance(event, dict):\n                continue\n            kind = event.get(\"kind\")\n            if kind == \"void\":\n                active = False\n                if void_is_tombstone:\n                    tombstone = True\n            elif kind == \"restore\":\n                if not tombstone:\n                    active = True\n            elif kind == \"refund\":\n                if not active:\n                    continue\n                day = event.get(\"day\", 0)\n                age = day - created\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 = int(event.get(\"cents\", 0) or 0)\n                if refund_overflow == \"cap\":\n                    remaining = ceiling - refunded\n                    if remaining <= 0:\n                        continue\n                    refunded += cents if cents < remaining else remaining\n                else:\n                    if refunded + cents > ceiling:\n                        continue\n                    refunded += cents\n            else:\n                continue\n        status = format_status(active, refunded, charge, status_case)\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 item: (item[0], item[1]))\n    else:\n        results.sort(key=lambda item: item[1])\n    return {\"invoices\": [item[2] for item in results]}\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_decimal(value):\n    return Decimal(str(value))\n\ndef principal_cents(lines, rounding):\n    if rounding == \"per_line\":\n        total = 0\n        for line in lines:\n            cents = (_to_decimal(line) * Decimal(\"100\")).quantize(Decimal(\"1\"), rounding=ROUND_HALF_UP)\n            total += int(cents)\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    result = []\n    index_by_eid = {}\n    counter = 0\n    for event in events:\n        eid = event.get(\"eid\") if isinstance(event, dict) else None\n        if eid is None:\n            key = (\"__no_eid__\", counter)\n            counter += 1\n            index_by_eid[key] = len(result)\n            result.append(event)\n            continue\n        if eid in index_by_eid:\n            if mode == \"last\":\n                result[index_by_eid[eid]] = event\n            continue\n        index_by_eid[eid] = len(result)\n        result.append(event)\n    return 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\ndef format_status(active, refunded, charge, status_case):\n    if not active:\n        base = \"void\"\n    elif refunded == charge:\n        base = \"refunded\"\n    else:\n        base = \"open\"\n    if status_case == \"upper\":\n        return base.upper()\n    if status_case == \"lower\":\n        return base.lower()\n    if status_case == \"title\":\n        return base.title()\n    if status_case == \"capitalize\":\n        return base.capitalize()\n    return base\n"}}