{"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 deduplicate_events, format_status, order_events, principal_cents\n\ndef run(payload, policy):\n    policy = policy or {}\n    currency = policy.get('currency', 'USD')\n    duplicate_event = policy.get('duplicate_event', 'first')\n    fee_refundable = policy.get('fee_refundable', True)\n    inclusive_limit = policy.get('inclusive_limit', True)\n    max_refund_age_days = policy.get('max_refund_age_days', 30)\n    min_invoice_cents = policy.get('min_invoice_cents', 10)\n    order = policy.get('order', 'id')\n    refund_overflow = policy.get('refund_overflow', 'cap')\n    refunds_first = policy.get('refunds_first', True)\n    rounding = policy.get('rounding', 'per_line')\n    status_case = policy.get('status_case', 'lower')\n    void_is_tombstone = policy.get('void_is_tombstone', True)\n    source = []\n    if isinstance(payload, dict):\n        source = payload.get('invoices') or []\n    results = []\n    for invoice in source:\n        if invoice.get('currency') != currency:\n            continue\n        principal = principal_cents(invoice.get('lines') or [], rounding)\n        fee = invoice.get('fee_cents', 0)\n        charge = principal + fee\n        if charge < min_invoice_cents:\n            continue\n        ceiling = principal + (fee if fee_refundable else 0)\n        events = deduplicate_events(invoice.get('events') or [], duplicate_event)\n        events = order_events(events, refunds_first)\n        active = True\n        tombstone = False\n        refunded = 0\n        created = invoice.get('created')\n        for event in events:\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                age = event.get('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                remaining = ceiling - refunded\n                if remaining <= 0:\n                    continue\n                cents = event.get('cents', 0)\n                if refund_overflow == 'cap':\n                    refunded += min(cents, remaining)\n                else:\n                    if cents <= remaining:\n                        refunded += cents\n        if not active:\n            raw_status = 'void'\n        elif refunded == charge:\n            raw_status = 'refunded'\n        else:\n            raw_status = 'open'\n        item = {'id': invoice.get('id'), 'charge': charge, 'refunded': refunded, 'balance': charge - refunded, 'status': format_status(raw_status, status_case)}\n        results.append((created, item))\n    if order in ('created', 'created_id', 'created,id', '(created,id)', 'created_then_id'):\n        results.sort(key=lambda pair: (pair[0], pair[1]['id']))\n    else:\n        results.sort(key=lambda pair: pair[1]['id'])\n    return {'invoices': [item for _, item in results]}\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 line_to_cents(line):\n    return int((Decimal(line) * Decimal(100)).quantize(Decimal('1'), rounding=ROUND_HALF_UP))\n\ndef principal_cents(lines, rounding):\n    if rounding == 'per_line':\n        return sum(line_to_cents(line) for line in lines)\n    total = sum((Decimal(line) for line in lines), Decimal(0))\n    return int((total * Decimal(100)).quantize(Decimal('1'), rounding=ROUND_HALF_UP))\n\ndef deduplicate_events(events, mode):\n    chosen = {}\n    for event in events:\n        eid = event.get('eid')\n        if mode == 'last':\n            chosen[eid] = event\n        elif eid not in chosen:\n            chosen[eid] = event\n    return list(chosen.values())\n\ndef order_events(events, refunds_first):\n    ordered = list(events)\n    if not refunds_first:\n        return ordered\n    refunds = [event for event in ordered if event.get('kind') == 'refund']\n    others = [event for event in ordered if event.get('kind') != 'refund']\n    return refunds + others\n\ndef format_status(status, status_case):\n    if status_case == 'upper':\n        return status.upper()\n    if status_case == 'capitalize':\n        return status.capitalize()\n    if status_case == 'title':\n        return status.title()\n    return status.lower()\n"}}