{"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, unique_events\n\n\ndef run(payload, policy):\n    source = payload.get('invoices', []) if isinstance(payload, dict) else (payload or [])\n    results = []\n    for invoice in source:\n        if invoice['currency'] != policy['currency']:\n            continue\n        principal = principal_cents(invoice.get('lines', []), policy['rounding'])\n        fee = invoice.get('fee_cents', 0)\n        charge = principal + fee\n        if charge < policy['min_invoice_cents']:\n            continue\n\n        events = unique_events(invoice.get('events', []), policy['duplicate_event'])\n        if policy['refunds_first']:\n            events = [event for event in events if event['kind'] == 'refund'] + [event for event in events if event['kind'] != 'refund']\n\n        ceiling = principal + (fee if policy['fee_refundable'] else 0)\n        refunded = 0\n        active = True\n        tombstoned = False\n        for event in events:\n            kind = event['kind']\n            if kind == 'void':\n                active = False\n                if policy['void_is_tombstone']:\n                    tombstoned = True\n            elif kind == 'restore':\n                if not tombstoned:\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                amount = event['cents']\n                remaining = ceiling - refunded\n                if amount > remaining:\n                    if policy['refund_overflow'] == 'reject':\n                        continue\n                    amount = remaining\n                refunded += amount\n\n        status = 'void' if not active else ('refunded' if refunded == charge else 'open')\n        status = status.upper() if policy['status_case'] == 'upper' else status.lower()\n        result = {\n            'id': invoice['id'],\n            'charge': charge,\n            'refunded': refunded,\n            'balance': charge - refunded,\n            'status': status,\n        }\n        results.append((invoice['created'], result))\n\n    if policy['order'] == 'created':\n        results.sort(key=lambda item: (item[0], item[1]['id']))\n    else:\n        results.sort(key=lambda item: item[1]['id'])\n    return {'invoices': [item[1] for item in results]}\n","policy.py":"POLICY = {\n    'currency': 'USD',\n    'duplicate_event': 'first',\n    'fee_refundable': True,\n    'inclusive_limit': True,\n    'max_refund_age_days': 30,\n    'min_invoice_cents': 10,\n    'order': 'id',\n    'refund_overflow': 'cap',\n    'refunds_first': True,\n    'rounding': 'per_line',\n    'status_case': 'lower',\n    'void_is_tombstone': True,\n}\n","helpers.py":"from decimal import Decimal\n\n\ndef decimal_parts(value):\n    parts = Decimal(value).as_tuple()\n    coefficient = 0\n    for digit in parts.digits:\n        coefficient = coefficient * 10 + digit\n    if parts.sign:\n        coefficient = -coefficient\n    if parts.exponent >= 0:\n        return coefficient * (10 ** parts.exponent), 0\n    return coefficient, -parts.exponent\n\n\ndef rounded_cents(coefficient, scale):\n    if scale <= 2:\n        return coefficient * (10 ** (2 - scale))\n    denominator = 10 ** (scale - 2)\n    return (2 * coefficient + denominator) // (2 * denominator)\n\n\ndef principal_cents(lines, rounding):\n    parts = [decimal_parts(line) for line in lines]\n    if rounding == 'per_line':\n        return sum(rounded_cents(coefficient, scale) for coefficient, scale in parts)\n    scale = max((item[1] for item in parts), default=0)\n    total = sum(coefficient * (10 ** (scale - item_scale)) for coefficient, item_scale in parts)\n    return rounded_cents(total, scale)\n\n\ndef unique_events(events, mode):\n    indexed = {}\n    for event in events:\n        eid = event['eid']\n        if eid not in indexed or mode == 'last':\n            indexed[eid] = event\n    return list(indexed.values())\n"}}