{
  "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    results = []\n    for invoice in payload['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\n        ceiling = principal + (invoice['fee_cents'] if policy['fee_refundable'] else 0)\n        refunded = 0\n        active = True\n        tombstoned = False\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        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                if age < 0 or age > limit or (age == limit and not policy['inclusive_limit']):\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        if policy['status_case'] == 'upper':\n            status = status.upper()\n        else:\n            status = 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': '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\nfrom fractions import Fraction\n\n\ndef round_half_up(value):\n    whole, remainder = divmod(value.numerator, value.denominator)\n    return whole + int(2 * remainder >= value.denominator)\n\n\ndef principal_cents(lines, rounding):\n    amounts = [Fraction(Decimal(line)) * 100 for line in lines]\n    if rounding == 'per_line':\n        return sum(round_half_up(amount) for amount in amounts)\n    return round_half_up(sum(amounts, Fraction(0)))\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"
  }
}