{"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, deduplicate_events\n\n\ndef run(payload, policy):\n    invoices = payload.get('invoices', [])\n    if policy['order'] == 'created':\n        ordered = sorted(invoices, key=lambda item: (item['created'], item['id']))\n    else:\n        ordered = sorted(invoices, key=lambda item: item['id'])\n\n    results = []\n    for invoice in ordered:\n        if invoice['currency'] != policy['currency']:\n            continue\n\n        principal = principal_cents(invoice['lines'], policy['rounding'])\n        charge = principal + invoice['fee_cents']\n        if charge < policy['min_invoice_cents']:\n            continue\n\n        ceiling = principal\n        if policy['fee_refundable']:\n            ceiling += invoice['fee_cents']\n\n        events = deduplicate_events(invoice['events'], policy['duplicate_event'])\n        if policy['refunds_first']:\n            events = sorted(events, key=lambda event: event['kind'] != 'refund')\n\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                if age < 0:\n                    continue\n                if age > limit or (age == limit and not policy['inclusive_limit']):\n                    continue\n\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        if not active:\n            status = 'void'\n        elif refunded == charge:\n            status = 'refunded'\n        else:\n            status = 'open'\n        if policy['status_case'] == 'upper':\n            status = status.upper()\n        else:\n            status = status.lower()\n\n        results.append({\n            'id': invoice['id'],\n            'charge': charge,\n            'refunded': refunded,\n            'balance': charge - refunded,\n            'status': status,\n        })\n\n    return {'invoices': results}\n","policy.py":"POLICY = {\n    'currency': 'CAD',\n    'duplicate_event': 'last',\n    'fee_refundable': False,\n    'inclusive_limit': False,\n    'max_refund_age_days': 45,\n    'min_invoice_cents': 25,\n    'order': 'created',\n    'refund_overflow': 'reject',\n    'refunds_first': False,\n    'rounding': 'invoice',\n    'status_case': 'upper',\n    'void_is_tombstone': False,\n}\n","helpers.py":"from decimal import Decimal\n\n\ndef cent_components(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    return coefficient, parts.exponent + 2\n\n\ndef round_components(components):\n    if not components:\n        return 0\n    exponent = min(0, min(power for _, power in components))\n    total = sum(coefficient * 10 ** (power - exponent)\n                for coefficient, power in components)\n    divisor = 10 ** (-exponent)\n    whole, remainder = divmod(abs(total), divisor)\n    rounded = whole + int(remainder * 2 >= divisor)\n    return -rounded if total < 0 else rounded\n\n\ndef principal_cents(lines, rounding):\n    components = [cent_components(line) for line in lines]\n    if rounding == 'per_line':\n        return sum(round_components([component]) for component in components)\n    return round_components(components)\n\n\ndef deduplicate_events(events, mode):\n    unique = {}\n    for event in events:\n        eid = event['eid']\n        if mode == 'last' or eid not in unique:\n            unique[eid] = event\n    return list(unique.values())\n"}}