{
  "id": "invoices",
  "title": "Invoice and payment reconciliation",
  "eyebrow": "Invoices",
  "summary": "Connect customers, invoices, payments and allocations while keeping recorded balances separate from cash receipts.",
  "businessQuestion": "Which overdue invoices need review, and how much settled payment has been allocated to one invoice?",
  "scopeNote": "Fictional, read-only authoring example. The result covers only the listed records and the stated cutoff. USD and EUR are kept separate. Recorded outstanding balances are inputs, not a calculation from allocations. The functions do not authorize collection or payment.",
  "keyDistinctions": [
    "An invoice balance is separate from a payment and its allocation.",
    "A posted allocation backed by a pending payment is not counted as settled.",
    "Disputed, paid, future-due and other-currency invoices are excluded from the requested review.",
    "Missing settlement or dispute evidence requires review, not a guessed status."
  ],
  "sources": [
    {
      "id": "receivables",
      "label": "Receivables snapshot",
      "system": "Fictional accounts-receivable ledger",
      "description": "Customer identities, invoices, due instants, dispute states and recorded balances."
    },
    {
      "id": "payments",
      "label": "Payment ledger",
      "system": "Fictional treasury export",
      "description": "Payment amounts, currencies and recorded settlement states."
    },
    {
      "id": "allocations",
      "label": "Allocation ledger",
      "system": "Fictional receivables application ledger",
      "description": "Amounts explicitly allocated from payments to invoices; allocation posting and payment settlement are separate."
    }
  ],
  "records": [
    {
      "id": "customer-harbor",
      "type": "customer",
      "label": "Harbor Facilities",
      "properties": {
        "customer_id": "ar:customer:harbor",
        "display_name": "Harbor Facilities"
      },
      "sourceId": "receivables"
    },
    {
      "id": "inv-1001",
      "type": "invoice",
      "label": "Invoice 1001 · partly paid",
      "properties": {
        "invoice_id": "ar:inv-1001",
        "status": "issued",
        "currency": "USD",
        "total_minor": 250000,
        "outstanding_minor": 150000,
        "due_at_epoch_seconds": 1790726400,
        "disputed": false
      },
      "sourceId": "receivables"
    },
    {
      "id": "inv-1002",
      "type": "invoice",
      "label": "Invoice 1002 · disputed",
      "properties": {
        "invoice_id": "ar:inv-1002",
        "status": "issued",
        "currency": "USD",
        "total_minor": 90000,
        "outstanding_minor": 90000,
        "due_at_epoch_seconds": 1789430400,
        "disputed": true
      },
      "sourceId": "receivables"
    },
    {
      "id": "inv-1003",
      "type": "invoice",
      "label": "Invoice 1003 · paid",
      "properties": {
        "invoice_id": "ar:inv-1003",
        "status": "paid",
        "currency": "USD",
        "total_minor": 50000,
        "outstanding_minor": 0,
        "due_at_epoch_seconds": 1788998400,
        "disputed": false
      },
      "sourceId": "receivables"
    },
    {
      "id": "inv-1004",
      "type": "invoice",
      "label": "Invoice 1004 · EUR",
      "properties": {
        "invoice_id": "ar:inv-1004",
        "status": "issued",
        "currency": "EUR",
        "total_minor": 40000,
        "outstanding_minor": 40000,
        "due_at_epoch_seconds": 1789862400,
        "disputed": false
      },
      "sourceId": "receivables"
    },
    {
      "id": "inv-1005",
      "type": "invoice",
      "label": "Invoice 1005 · not yet due",
      "properties": {
        "invoice_id": "ar:inv-1005",
        "status": "issued",
        "currency": "USD",
        "total_minor": 70000,
        "outstanding_minor": 70000,
        "due_at_epoch_seconds": 1792454400,
        "disputed": false
      },
      "sourceId": "receivables"
    },
    {
      "id": "payment-a",
      "type": "payment",
      "label": "Payment A",
      "properties": {
        "payment_id": "payments:payment-a",
        "status": "settled",
        "currency": "USD",
        "amount_minor": 90000
      },
      "sourceId": "payments"
    },
    {
      "id": "payment-b",
      "type": "payment",
      "label": "Payment B",
      "properties": {
        "payment_id": "payments:payment-b",
        "status": "settled",
        "currency": "USD",
        "amount_minor": 40000
      },
      "sourceId": "payments"
    },
    {
      "id": "payment-c",
      "type": "payment",
      "label": "Payment C",
      "properties": {
        "payment_id": "payments:payment-c",
        "status": "pending",
        "currency": "USD",
        "amount_minor": 30000
      },
      "sourceId": "payments"
    },
    {
      "id": "allocation-a",
      "type": "payment_allocation",
      "label": "Allocation A",
      "properties": {
        "allocation_id": "ar:allocation-a",
        "status": "posted",
        "currency": "USD",
        "amount_minor": 60000
      },
      "sourceId": "allocations"
    },
    {
      "id": "allocation-b",
      "type": "payment_allocation",
      "label": "Allocation B",
      "properties": {
        "allocation_id": "ar:allocation-b",
        "status": "posted",
        "currency": "USD",
        "amount_minor": 40000
      },
      "sourceId": "allocations"
    },
    {
      "id": "allocation-c",
      "type": "payment_allocation",
      "label": "Allocation C",
      "properties": {
        "allocation_id": "ar:allocation-c",
        "status": "posted",
        "currency": "USD",
        "amount_minor": 30000
      },
      "sourceId": "allocations"
    }
  ],
  "links": [
    {
      "id": "inv-1001.customer",
      "from": "inv-1001",
      "property": "billed_to",
      "to": "customer-harbor",
      "sourceId": "receivables"
    },
    {
      "id": "inv-1002.customer",
      "from": "inv-1002",
      "property": "billed_to",
      "to": "customer-harbor",
      "sourceId": "receivables"
    },
    {
      "id": "inv-1003.customer",
      "from": "inv-1003",
      "property": "billed_to",
      "to": "customer-harbor",
      "sourceId": "receivables"
    },
    {
      "id": "inv-1004.customer",
      "from": "inv-1004",
      "property": "billed_to",
      "to": "customer-harbor",
      "sourceId": "receivables"
    },
    {
      "id": "inv-1005.customer",
      "from": "inv-1005",
      "property": "billed_to",
      "to": "customer-harbor",
      "sourceId": "receivables"
    },
    {
      "id": "payment-a.customer",
      "from": "payment-a",
      "property": "received_from",
      "to": "customer-harbor",
      "sourceId": "payments"
    },
    {
      "id": "allocation-a.invoice",
      "from": "allocation-a",
      "property": "applied_to",
      "to": "inv-1001",
      "sourceId": "allocations"
    },
    {
      "id": "allocation-a.payment",
      "from": "allocation-a",
      "property": "uses_payment",
      "to": "payment-a",
      "sourceId": "allocations"
    },
    {
      "id": "payment-b.customer",
      "from": "payment-b",
      "property": "received_from",
      "to": "customer-harbor",
      "sourceId": "payments"
    },
    {
      "id": "allocation-b.invoice",
      "from": "allocation-b",
      "property": "applied_to",
      "to": "inv-1001",
      "sourceId": "allocations"
    },
    {
      "id": "allocation-b.payment",
      "from": "allocation-b",
      "property": "uses_payment",
      "to": "payment-b",
      "sourceId": "allocations"
    },
    {
      "id": "payment-c.customer",
      "from": "payment-c",
      "property": "received_from",
      "to": "customer-harbor",
      "sourceId": "payments"
    },
    {
      "id": "allocation-c.invoice",
      "from": "allocation-c",
      "property": "applied_to",
      "to": "inv-1001",
      "sourceId": "allocations"
    },
    {
      "id": "allocation-c.payment",
      "from": "allocation-c",
      "property": "uses_payment",
      "to": "payment-c",
      "sourceId": "allocations"
    }
  ],
  "questions": [
    {
      "id": "overdue-invoices",
      "title": "Which Harbor USD invoices are overdue and undisputed?",
      "explanation": "Traverse customer → invoice, then require issued status, USD, a due instant before the cutoff, a positive recorded balance and an explicit false dispute flag. Invoice 1001 has 150,000 cents ($1,500) outstanding.",
      "arguments": {
        "customerId": "customer-harbor",
        "currency": "USD",
        "dueBeforeEpochSeconds": 1791374400
      },
      "definition": {
        "artifactVersion": "t2k.graph-function.v1",
        "functionRef": "example.invoices:reasoning_function:overdue_review",
        "inputs": {
          "customerId": "string",
          "currency": "string",
          "dueBeforeEpochSeconds": "integer"
        },
        "output": {
          "kind": "rows",
          "fields": {
            "invoiceId": "string",
            "outstandingMinor": "integer",
            "currency": "string"
          }
        },
        "maxAgeSeconds": 3600,
        "limits": {
          "maxRows": 100,
          "maxWork": 10000
        },
        "steps": [
          {
            "id": "customer",
            "op": "lookup",
            "typeRef": "example.invoices:customer",
            "as": "customer",
            "entityId": {
              "argument": "customerId"
            }
          },
          {
            "id": "invoices",
            "op": "traverse",
            "from": "customer",
            "source": "customer",
            "relation": "example.invoices:relation:invoice:billed_to:customer",
            "direction": "incoming",
            "as": "invoice"
          },
          {
            "id": "overdue",
            "op": "filter",
            "from": "invoices",
            "all": [
              {
                "left": {
                  "binding": "invoice",
                  "property": "example.invoices:invoice.status"
                },
                "operator": "eq",
                "right": {
                  "literal": "issued"
                }
              },
              {
                "left": {
                  "binding": "invoice",
                  "property": "example.invoices:invoice.currency"
                },
                "operator": "eq",
                "right": {
                  "argument": "currency"
                }
              },
              {
                "left": {
                  "binding": "invoice",
                  "property": "example.invoices:invoice.due_at_epoch_seconds"
                },
                "operator": "lt",
                "right": {
                  "argument": "dueBeforeEpochSeconds"
                }
              },
              {
                "left": {
                  "binding": "invoice",
                  "property": "example.invoices:invoice.outstanding_minor"
                },
                "operator": "gt",
                "right": {
                  "literal": 0
                }
              },
              {
                "left": {
                  "binding": "invoice",
                  "property": "example.invoices:invoice.disputed"
                },
                "operator": "eq",
                "right": {
                  "literal": false
                }
              }
            ]
          },
          {
            "id": "rows",
            "op": "project",
            "from": "overdue",
            "fields": {
              "invoiceId": {
                "entity": "invoice"
              },
              "outstandingMinor": {
                "binding": "invoice",
                "property": "example.invoices:invoice.outstanding_minor"
              },
              "currency": {
                "binding": "invoice",
                "property": "example.invoices:invoice.currency"
              }
            }
          }
        ],
        "return": "rows"
      },
      "expected": {
        "status": "complete",
        "value": [
          {
            "invoiceId": "inv-1001",
            "outstandingMinor": 150000,
            "currency": "USD"
          }
        ]
      },
      "variants": [
        {
          "id": "invoice-disputed",
          "label": "Invoice 1001 becomes disputed",
          "explanation": "An explicitly disputed invoice leaves this review set; no collection action is implied by either answer.",
          "patches": [
            {
              "claimId": "inv-1001.disputed",
              "value": true
            }
          ],
          "expected": {
            "status": "complete",
            "value": []
          }
        },
        {
          "id": "proposed-dispute-state",
          "label": "Dispute state is unreviewed",
          "explanation": "A proposed false dispute flag is not an accepted fact, so the query cannot complete.",
          "patches": [
            {
              "claimId": "inv-1001.disputed",
              "status": "proposed"
            }
          ],
          "expected": {
            "status": "needs_review",
            "value": null
          }
        }
      ]
    },
    {
      "id": "settled-allocations",
      "title": "How much settled USD payment is allocated to invoice 1001?",
      "explanation": "Follow invoice → allocation → payment. Sum the two posted allocations backed by settled USD payments: 60,000 + 40,000 cents. Payment A is 90,000 cents, but only 60,000 is allocated here; the pending payment’s 30,000-cent allocation is excluded.",
      "arguments": {
        "invoiceId": "inv-1001",
        "currency": "USD"
      },
      "definition": {
        "artifactVersion": "t2k.graph-function.v1",
        "functionRef": "example.invoices:reasoning_function:settled_allocations",
        "inputs": {
          "invoiceId": "string",
          "currency": "string"
        },
        "output": {
          "kind": "scalar",
          "valueType": "number"
        },
        "maxAgeSeconds": 3600,
        "limits": {
          "maxRows": 100,
          "maxWork": 10000
        },
        "steps": [
          {
            "id": "invoice",
            "op": "lookup",
            "typeRef": "example.invoices:invoice",
            "as": "invoice",
            "entityId": {
              "argument": "invoiceId"
            }
          },
          {
            "id": "invoice_currency",
            "op": "filter",
            "from": "invoice",
            "all": [
              {
                "left": {
                  "binding": "invoice",
                  "property": "example.invoices:invoice.currency"
                },
                "operator": "eq",
                "right": {
                  "argument": "currency"
                }
              }
            ]
          },
          {
            "id": "allocations",
            "op": "traverse",
            "from": "invoice_currency",
            "source": "invoice",
            "relation": "example.invoices:relation:payment_allocation:applied_to:invoice",
            "direction": "incoming",
            "as": "allocation"
          },
          {
            "id": "payments",
            "op": "traverse",
            "from": "allocations",
            "source": "allocation",
            "relation": "example.invoices:relation:payment_allocation:uses_payment:payment",
            "direction": "outgoing",
            "as": "payment"
          },
          {
            "id": "settled",
            "op": "filter",
            "from": "payments",
            "all": [
              {
                "left": {
                  "binding": "allocation",
                  "property": "example.invoices:payment_allocation.status"
                },
                "operator": "eq",
                "right": {
                  "literal": "posted"
                }
              },
              {
                "left": {
                  "binding": "allocation",
                  "property": "example.invoices:payment_allocation.currency"
                },
                "operator": "eq",
                "right": {
                  "argument": "currency"
                }
              },
              {
                "left": {
                  "binding": "payment",
                  "property": "example.invoices:payment.status"
                },
                "operator": "eq",
                "right": {
                  "literal": "settled"
                }
              },
              {
                "left": {
                  "binding": "payment",
                  "property": "example.invoices:payment.currency"
                },
                "operator": "eq",
                "right": {
                  "argument": "currency"
                }
              }
            ]
          },
          {
            "id": "amounts",
            "op": "project",
            "from": "settled",
            "fields": {
              "amountMinor": {
                "binding": "allocation",
                "property": "example.invoices:payment_allocation.amount_minor"
              }
            }
          },
          {
            "id": "total",
            "op": "aggregate",
            "from": "amounts",
            "operation": "sum",
            "field": "amountMinor"
          }
        ],
        "return": "total"
      },
      "expected": {
        "status": "complete",
        "value": 100000
      },
      "variants": [
        {
          "id": "missing-settlement",
          "label": "Settlement evidence is missing",
          "explanation": "Removing Payment B’s settlement state prevents a complete total; it is not silently treated as unsettled or settled.",
          "patches": [
            {
              "claimId": "payment-b.status",
              "remove": true
            }
          ],
          "expected": {
            "status": "needs_review",
            "value": null
          }
        },
        {
          "id": "stale-allocation",
          "label": "Allocation evidence is stale",
          "explanation": "An old allocation amount cannot support a current complete total, even when the payment remains settled.",
          "patches": [
            {
              "claimId": "allocation-a.amount_minor",
              "observedAt": "2026-10-07T09:00:00Z"
            }
          ],
          "expected": {
            "status": "needs_review",
            "value": null
          }
        }
      ],
      "presentation": {
        "kind": "money",
        "currency": "USD",
        "minorUnitScale": 100,
        "label": "Settled payments allocated to this invoice"
      }
    }
  ]
}
