{"files":{"main.py":"from core import run\nfrom policy import POLICY\n\n\ndef evaluate(payload):\n    return run(payload, POLICY)\n","core.py":"from helpers import principal_cents\n\n\ndef run(payload, policy):\n    invoices = payload.get('invoices', []) if isinstance(payload, dict) else (payload or [])\n    results = []\n    for invoice in invoices:\n        if invoice['currency'] != policy['currency']:\n            continue\n        principal = principal_cents(invoice['lines'], policy['rounding'])\n        charge = principal + invoice['fee_cents']\n        if charge < policy['min_invoice_cents']:\n            continue\n        ceiling = principal + (invoice['fee_cents'] if policy['fee_refundable'] else 0)\n        unique = {}\n        for event in invoice['events']:\n            eid = event['eid']\n            if eid not in unique or policy['duplicate_event'] == 'last':\n                unique[eid] = event\n        events = list(unique.values())\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        active = True\n        tombstone = False\n        refunded = 0\n        for event in events:\n            kind = event['kind']\n            if kind == 'void':\n                active = False\n                if policy['void_is_tombstone']:\n                    tombstone = True\n            elif kind == 'restore':\n                if not tombstone:\n                    active = True\n            elif kind == 'refund' and active:\n                age = event['day'] - invoice['created']\n                limit = policy['max_refund_age_days']\n                eligible = age >= 0 and (age < limit or (policy['inclusive_limit'] and age == limit))\n                if not eligible:\n                    continue\n                remaining = ceiling - refunded\n                amount = event['cents']\n                if amount > remaining:\n                    if policy['refund_overflow'] == 'reject':\n                        continue\n                    amount = remaining\n                refunded += amount\n        status = 'void' if not active else ('refunded' if refunded == charge else 'open')\n        if policy['status_case'] == 'upper':\n            status = status.upper()\n        row = {\n            'id': invoice['id'],\n            'charge': charge,\n            'refunded': refunded,\n            'balance': charge - refunded,\n            'status': status,\n        }\n        key = invoice['id'] if policy['order'] == 'id' else (invoice['created'], invoice['id'])\n        results.append((key, row))\n    results.sort(key=lambda item: item[0])\n    return {'invoices': [row for _, row in results]}\n","policy.py":"POLICY = {\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    'currency': 'USD',\n    'fee_refundable': True,\n    'min_invoice_cents': 10,\n    'status_case': 'lower',\n}\n","helpers.py":"from fractions import Fraction\n\n\ndef round_half_up(value):\n    return (2 * value.numerator + value.denominator) // (2 * value.denominator)\n\n\ndef principal_cents(lines, rounding):\n    values = [Fraction(line) * 100 for line in lines]\n    if rounding == 'per_line':\n        return sum(round_half_up(value) for value in values)\n    return round_half_up(sum(values, Fraction(0)))\n"}}