# Payments & collections — module spec (generated from modules/payments/module.yaml)
# A starting vocabulary for phase 2: correct it with the customer, then write ERP_PAYMENTS.md.
# START HERE: read dodil://erp/build (the map) first; this spec is a vocabulary, not the method.
# WORKING CODE EXISTS — run `code.init` below (uvx --from git+https://git.dodil.io/dodil/erp-platform.git@v0.9.13 erp-compose init payments): it composes these bricks; do not rewrite the module.
schema: dodil.module-spec/v1
code:
  start_here: 'START HERE: read dodil://erp/build (the map) first; this spec is a vocabulary, not the method. Then, in the project folder: (1) uvx --from git+https://git.dodil.io/dodil/erp-platform.git@v0.9.13 erp-compose init payments; (2) SHOW the customer the diagram — `erp-compose process --module payments --playbook <playbook>` for a brick with playbooks (hr), else `erp-compose bpmn --from ERP_PAYMENTS.md --out processes/payments.bpmn` — and paste its step names, in order with who does each, into your message. The signed artifact is the BPMN: never prose, never "open bpmn.io".'
  status: This module has working, tested code. COMPOSE it; do not rewrite it.
  how: "Run: uvx --from git+https://git.dodil.io/dodil/erp-platform.git@v0.9.13 erp-compose init payments  (in a fresh folder; no DODIL login needed). It pins the bricks at these tags, installs them, writes compose.yaml, requirements.txt and tests/test_composition.py, and runs the checks. Then configure rows through the bricks' use cases. Write a new brick only for a need no brick covers."
  init: uvx --from git+https://git.dodil.io/dodil/erp-platform.git@v0.9.13 erp-compose init payments
  repos:
    - name: erp-brick-payments
      role: brick
      tag: v0.2.2
      clone: git clone --depth 1 --branch v0.2.2 https://git.dodil.io/dodil/erp-brick-payments.git
    - name: brick-business-partner
      role: brick
      tag: v0.4.0
      clone: git clone --depth 1 --branch v0.4.0 https://git.dodil.io/dodil/erp-brick-business-partner.git
    - name: erp-platform
      role: platform
      tag: v0.9.13
      clone: git clone --depth 1 --branch v0.9.13 https://git.dodil.io/dodil/erp-platform.git
  compose_yaml: |-
    # compose.yaml: what `uvx --from git+https://git.dodil.io/dodil/erp-platform.git@v0.9.13 erp-compose init payments` writes (for reference; run the command, do not
    # hand-write this). The exact format erp_platform.compose loads; check it: erp-compose check compose.yaml
    name: <customer>-payments
    version: 0.1.0
    tier: 1
    bricks:
      - { name: payments, brick: "brick_payments.application.use_cases:PAY", version: 0.2.2 }
      - { name: business-partner, brick: "brick_bp.application.use_cases:BP", version: 0.4.0 }
    # capabilities: omit to let each capability's only provider be used; a capability no brick
    # provides becomes a manual task. Map one only to choose between two providers.
  test_composition_py: |
    """The composition boots, its bricks answer through HTTP together, and a signed-in caller can read each one."""
    import inspect
    import pathlib
    from contextlib import ExitStack

    import anyio
    import httpx
    from sqlalchemy import create_engine
    from erp_platform import compose, seam
    from erp_platform.scenarios import Catalogue
    from erp_platform.scenarios.inprocess import stage

    COMPOSE = pathlib.Path(__file__).resolve().parent.parent / "compose.yaml"


    def test_composition_boots_and_every_brick_answers_a_signed_in_read(tmp_path):
        comp = compose.load(COMPOSE)
        assert compose.check(comp)["problems"] == []
        engine = create_engine(f"sqlite:///{tmp_path / 'bucket.db'}", connect_args={"check_same_thread": False})
        perms = sorted({u.permission for m in comp.members for u in m.brick.use_cases if u.permission})
        me = {"sub": "tester.1", "email": "tester@test", "app_roles": ["tester"], "permissions": perms}

        async def get(app, path):
            async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://t") as c:
                return await c.get(path, headers=seam.gateway_headers(me))

        with ExitStack() as st:
            app = stage(Catalogue("t", compose=comp, roles=None), st, lambda name: engine).app
            assert anyio.run(get, app, "/healthz").status_code == 200
            for m in comp.members:
                reads = [u for u in m.brick.use_cases if u.kind == "query" and u.method == "GET" and u.tool
                         and not u.path_params and all(d is not inspect.Parameter.empty for _, _, d in u.inputs)]
                assert reads, f"{m.name}: no plain read use case to call"
                # The first plain read may refuse an empty question (activity's GET /contacts wants a bp_id or an
                # email: 422). A refusal is the use case answering; a 5xx is the composition broken. So: no read
                # may 5xx, and at least one must answer 200.
                answers = []
                for u in reads:
                    r = anyio.run(get, app, f"/{m.name}{u.path}")
                    assert r.status_code < 500, f"{m.name} {u.path}: {r.status_code} {r.text[:200]}"
                    answers.append(f"{u.path} {r.status_code}")
                    if r.status_code == 200:
                        break
                else:
                    raise AssertionError(f"{m.name}: no plain read answered 200 ({', '.join(answers)})")
id: payments
name: Payments & collections
short: Payments
version: 8
maturity: tested
summary: Record what customers pay (typed in or straight from the bank statement), match it to the invoices it settles, refund what is owed back, and chase what is late, so "who owes us what" is always one true answer.
decisions:
  - decided: AI receipt matching (ai_suggest_match) defaults to suggest; auto-with-review, for EXACT matches only, is an opt-in setting
    by: Seemo, DODIL (reference customer)
    date: 2026-10-18
  - decided: credit holds live in payments (PAY-12, credit_status); sales-order and sales read them
    by: Seemo, DODIL (reference customer)
    date: 2026-10-18
  - decided: "reminders default to suggest: the AI drafts, the collector sends (reminder_mode, ai_draft_reminder)"
    by: Seemo, DODIL (reference customer)
    date: 2026-10-18
  - decided: the bank statement imports as ISO 20022 camt.053 and as CSV (PAY-02)
    by: Seemo, DODIL (reference customer)
    date: 2026-10-18
  - proposed: "v3: bank reconciliation of every statement line lives in payments; MT940 is read as well as camt.053 and CSV"
    status: for the SAP full-scope re-sign (2026-10-22)
  - proposed: "v3: a reconciliation rule suggests by default; a rule that posts without a person (auto) is the owner's control, listed for review the same day"
    status: for the SAP full-scope re-sign (2026-10-22)
  - proposed: "v3: a direct-debit run is approved by a second person before it is submitted to Bacs"
    status: for the SAP full-scope re-sign (2026-10-22)
facets:
  domain: finance
  industries:
    - any
  customer_words:
    - record a payment
    - who has paid
    - match the bank statement to invoices
    - chase late payers
    - refund the customer
    - statement of account
    - bank reconciliation
    - bank charges
    - direct debit
    - petty cash
    - paying in cheques
    - cash flow forecast
    - early payment discount taken
    - remittance advice
  replaces:
    - ticking invoices off a bank statement by hand
    - a chasing spreadsheet
    - Xero bank reconciliation for receivables
    - Odoo payment registration and follow-ups
  pillars:
    - sql
  first_module_fit: high
  depends_on:
    - module: ar
      why: the invoices and credit notes being settled
      required: true
    - module: business-partner
      why: a receipt and a credit balance belong to a party
      required: true
    - module: gl
      why: posting receipts, refunds and FX differences
      required: optional
    - module: tax
      why: CIS a contractor customer deducted; the VAT on a cash expense; the VAT liability a bank debit pays (v3)
      required: optional
    - module: purchasing
      why: supplier payments and bills due, matched on the statement and in the forecast (v3)
      required: optional
  feeds:
    - ar
    - gl
    - sales-order
    - tax
  owns_master_data: []
  references_master_data:
    - business_partner
actors:
  - id: cashier
    name: Whoever records money (bookkeeper, office manager)
    does: records receipts, imports the bank statement, allocates receipts to invoices, requests refunds
    sees: every receipt and every open invoice
    permissions:
      - pay:receipt:read
      - pay:receipt:write
      - pay:receipt:import
      - pay:allocation:write
      - pay:refund:request
      - pay:statement_line:reconcile
      - pay:remittance:import
      - pay:mandate:write
      - pay:dd_run:write
      - pay:cash_journal:write
      - pay:deposit_batch:write
      - pay:payout:export
    note: "composition role (L2C leftover): the cashier also holds ar:invoice:read, because payments reads invoice totals from invoicing under the caller"
  - id: collector
    name: Whoever chases money
    does: works the overdue list, records promises and disputes, sends or approves reminders
    sees: every open invoice and its history
    permissions:
      - pay:receipt:read
      - pay:collection:write
      - pay:collection_event:write
      - pay:reminder:send
      - pay:statement:write
  - id: approver
    name: Owner / finance manager
    does: approves refunds, sets credit limits and holds, sets the reminder policy, holds the controls on the cashier (tolerance, auto-matching, holds, time zone), signs the migration's reconciliation; v3 - keeps house banks and bank accounts, the reconciliation rules, approves direct-debit runs and cash differences, reads the cash position
    sees: everything
    permissions:
      - pay:receipt:read
      - pay:refund:approve
      - pay:credit:write
      - pay:settings:write
      - pay:settings:controls
      - pay:migration:sign
      - pay:bank_account:write
      - pay:reconciliation_rule:write
      - pay:dd_run:approve
      - pay:cash_journal:approve
      - pay:forecast:read
      - pay:payout:approve
  - id: customer
    name: The customer
    external: true
    does: pays, sends a remittance advice, promises or disputes
    sees: their own statement and reminders
    permissions: []
  - id: system
    name: The system
    does: imports statement lines, derives settlement and aging, suggests matches, runs the daily collections pass (reminders when told to, lapsed promises), takes allocations from invoicing, derives credit holds, runs the one-off Odoo migration; v3 - takes a bank feed, suggests statement-line matches, applies auto rules the owner allowed, collects direct debits on the entry day, posts realised FX differences
    sees: everything, through its own service account (one service client per job, each with only its exact grants)
    permissions:
      - pay:receipt:read
      - pay:receipt:ingest
      - pay:receipt:match
      - pay:collection:tick
      - pay:allocation:from_source
      - pay:migration:import
      - pay:statement:ingest
      - pay:statement_line:suggest
      - pay:dd:collect
      - pay:bank_account:read_unmasked
      - pay:deposit_batch:intake
      - pay:card_settlement:intake
      - pay:payout:intake
stories:
  - id: PAY-01
    kind: person
    actor: cashier
    story: As the person recording money, I want to record a receipt with its date, amount, currency and reference, so that the money is on the customer before I decide what it pays.
    given: a customer with bp_id 7311 and three open invoices
    when: I record 10,000.00 GBP received 2026-11-20 by bank transfer, reference "INV-2026-0041 0042"
    then: a receipt exists, unallocated; the customer's open items show it as unallocated cash; recording it again with the same bank reference is refused as a duplicate; a received_on after today (business_timezone) is refused, because money not yet received is not a receipt; a wrong amount is corrected by reversing the receipt and recording the right one, never by editing it
    screen: Receipts
    path:
      process: payments.receipt
      choices: {}
    reference: implemented (ar-core, as AR-05's receipt)
  - id: PAY-02
    kind: person
    actor: cashier
    story: As the cashier, I want to import the bank statement, so that every receipt is recorded from the bank, not typed.
    given: a camt.053 (or CSV) statement with 42 credit lines, 3 of them already imported last week
    when: I import it
    then: "39 new receipts keyed payment_id = \"bank:<account>:<statement entry ref>\", each dated by the bank's booking date; the 3 are recognised and skipped; lines dated before statement_import_from (the migration's cut-over) are skipped as already migrated. v3: every line of the statement, debit or credit, is also kept as a statement line and reconciled in PAY-21 (this replaces v2's \"debit lines are left for the ledger's bank reconciliation\"); a credit whose counterparty account is one of our own bank accounts, or that a reconciliation rule names (interest, a supplier's refund), is not made a customer receipt; MT940 is read as well as camt.053 and CSV"
    screen: Receipts
    reference: not built
  - id: PAY-03
    kind: person
    actor: cashier
    story: As the cashier, I want one receipt to settle several invoices, so that a customer paying three bills with one transfer is handled properly.
    given: a receipt of 10,000.00 and invoices of 4,000.00, 3,500.00 and 3,000.00
    when: I allocate it across them
    then: 4,000.00 and 3,500.00 are paid, 2,500.00 goes to the third, which keeps 500.00 outstanding, and 0.00 is left unallocated; every allocation is dated today (business_timezone), never earlier; allocating more than the receipt's remainder or more than an invoice's outstanding is refused, naming both figures
    screen: Receipts
    reference: implemented (ar-core, AR-05)
  - id: PAY-04
    kind: person
    actor: cashier
    story: As the cashier, I want a difference within tolerance (a bank charge, a rounding) to close the invoice, so that 1.50 short does not stay on the chase list for ever.
    given: a tolerance of 2.00 and a receipt of 998.50 against an invoice of 1,000.00
    when: I allocate it
    then: 998.50 allocates, and the 1.50 is proposed as a write-off to invoicing (AR-09) within the threshold; above the tolerance the difference stays outstanding
    screen: Receipts
    reference: not built
  - id: PAY-05
    kind: person
    actor: cashier
    story: As the cashier, I want money that pays nothing yet to stay on the customer as credit, so that it is used against the next invoice.
    given: an unallocated 500.00 on a customer
    when: their next invoice of 800.00 issues and I allocate the credit
    then: 500.00 allocates to the new invoice; 300.00 outstanding; the credit is never applied across currencies
    screen: Receipts
    reference: not built
  - id: PAY-06
    kind: person
    actor: cashier
    story: As the cashier, I want to undo an allocation with a reason, so that a mistake is corrected without deleting history.
    given: a receipt allocated to the wrong invoice
    when: I unallocate it with the reason "wrong invoice"
    then: a reversing allocation row (negative, same source, dated today, naming the row it reverses) is added; both are kept; the invoice's outstanding rises back from today; a statement already sent for an earlier date still re-computes to what it said
    screen: Receipts
    reference: not built
  - id: PAY-07
    kind: person
    actor: cashier
    story: As the cashier, I want a returned payment (a bounced direct debit, a recalled transfer) reversed, so that the invoices it paid are owed again.
    given: a receipt of 1,200.00 allocated to two invoices
    when: the bank returns it and I record the return
    then: the receipt is reversed on the bank's return date (today or earlier, not before it was received), its allocations are reversed with it by reversing rows, both invoices are outstanding again, and the reversal posts in the ledger
    screen: Receipts
    reference: not built
  - id: PAY-08
    kind: person
    actor: cashier
    story: As the cashier, I want to refund a customer's overpayment or credit, so that money owed back goes back with an approval.
    given: a customer with 300.00 unallocated credit
    when: I request a refund of 300.00 to their bank account and the approver approves, then I record it paid with the bank reference
    then: the refund is allocated against the credit (source_kind refund), the customer's credit is 0.00, the refund posts in the ledger on the bank's payment date (today or earlier, not before the approval); refunding more than the credit is refused; nobody approves their own refund
    screen: Refunds
    path:
      process: payments.refund
      choices: {}
    reference: not built
  - id: PAY-09
    kind: person
    actor: collector
    story: As the person chasing money, I want one list of what is overdue and by how long, so that I know who to call this morning.
    given: invoices past their due date
    when: I open the overdue list
    then: I see them in the configured aging buckets (0-30, 31-60, 61-90, 90+), aged from the basis the business chose, with customer, outstanding, age, last contact, and the AI's late-payment risk when it is on
    screen: Collections
    reference: implemented (ar-core, AR-06)
  - id: PAY-10
    kind: person
    actor: collector
    story: As the collector, I want to record what a customer promised or disputed, so that the next person picking it up is not starting again.
    given: a customer with several overdue invoices
    when: I log one promise to pay on 2026-12-05 covering all of them
    then: it is recorded once against the customer, shows on each invoice, and suppresses reminders until that date; a promise dated today or earlier is refused; a dispute suppresses them until it is resolved
    screen: Collections
    reference: implemented (ar-core, AR-07)
  - id: PAY-11
    kind: person
    actor: collector
    story: As the collector, I want to send a customer a statement of everything open, so that they can reconcile their side.
    given: a customer with open invoices, credit notes and unallocated receipts
    when: I produce an open-item statement as at 2026-11-30
    then: it lists every document unsettled at the end of 2026-11-30 (documents and allocations dated on or before it) with a closing balance equal to their sum, and records that it was sent; produced again later for the same date it shows the same balance, whatever was allocated since
    screen: Collections
    reference: implemented (ar-core, AR-08)
  - id: PAY-12
    kind: person
    actor: approver
    story: As the owner, I want a customer put on hold when they are far enough past due or over their limit, so that we stop taking orders from someone who is not paying.
    given: a credit limit of 20,000.00 and a hold rule "any invoice over 60 days"
    when: a customer has an invoice 61 days overdue
    then: the hold is derived and shown with its reason to sales-order and to sales; releasing it early is my act, recorded with a reason and an end date after today, effective from the moment it is recorded, never earlier; a new credit limit takes effect from tomorrow at the earliest, so what the limit WAS on any date stays answerable
    screen: Credit
    reference: not built
    customer_varies: whether limits exist at all; many small businesses want the warning without the block
  - id: PAY-13
    kind: person
    actor: approver
    story: As the owner, I want the controls on the people who record money held by me, so that the cashier cannot widen their own tolerance or switch on automatic matching.
    given: a cashier holding pay:settings:write and an owner holding pay:settings:controls
    when: the cashier tries to raise match_tolerance from 2.00 to 50.00, then the owner does
    then: the cashier is refused, naming the permission; the owner's change applies to allocations made from now on and none already made; who changed it, from what, to what and when is recorded
    screen: Settings
    reference: not built
  - id: PAY-14
    kind: person
    actor: cashier
    story: As the cashier, I want to accept, change or reject the suggested match, so that nothing is allocated without a person deciding, and the record says what the AI proposed and who agreed.
    given: a suggestion (PAY-32) to allocate a 7,500.00 receipt to INV-2026-0041 and INV-2026-0042, confidence 0.97
    when: I accept it; on another receipt I change one invoice before accepting; on a third I reject it
    then: the allocations cite the suggestion, its confidence and me as the one who accepted; the changed one records both what was proposed and what I chose; the rejected one allocates nothing and stays unallocated; in mode auto-with-review, the exact matches the system allocated are listed for me to confirm or undo (PAY-06) the same day
    screen: Receipts
    reference: not built
  - id: PAY-15
    kind: person
    actor: approver
    story: As the owner moving off Odoo, I want the migrated receipts and allocations reconciled to Odoo per customer, with every difference explained, so that I can sign that what we are owed is the same on both sides.
    given: the migration (PAY-44) has loaded a test copy of the bucket from the Odoo database
    when: I open the reconciliation
    then: every check under migration.reconcile shows Odoo's figure, ours and the difference, per customer and currency; a check that must equal and does not blocks the sign-off; an explained difference names its reason; I sign it (pay:migration:sign) and the signature records the figures it signed
    screen: Migration
    reference: not built
  - id: PAY-16
    kind: person
    actor: cashier
    story: As the cashier, I want a customer who paid early to get the discount the terms promised, with the VAT corrected, so that their invoice closes without a short payment on the chase list.
    given: invoice INV-2026-0090 of 1,200.00 (1,000.00 + 200.00 VAT) issued 2026-11-02 with terms 2/10 net 30 (the discount until 2026-11-12, frozen on the invoice); a receipt of 1,176.00 received 2026-11-10
    when: I allocate it
    then: "1,176.00 allocates, and the 24.00 difference is offered as the settlement discount (gate within_discount_window: received_on, the bank's date, on or before the invoice's settlement_discount_until; gate discount_matches_terms: exactly the percentage of the gross outstanding at receipt); applying it asks invoicing for the discount credit (ar AR-38: 20.00 + 4.00 VAT at the invoice's rates), which is then allocated, so the invoice is paid; under settlement_discount_vat invoice_wording the 24.00 is an allocation with source_kind settlement_discount instead, which tax reads for the return; a receipt dated 2026-11-13 gets no discount and the 24.00 stays outstanding (or goes to the tolerance, PAY-04)"
    screen: Receipts
    reference: not built
  - id: PAY-17
    kind: person
    actor: cashier
    story: As the cashier, I want to load a customer's remittance advice or the bank's lockbox file, so that a large payment covering forty invoices is allocated from their list, not by searching.
    given: a receipt of 48,210.00 from a retailer; their remittance file (CSV, or ISO 20022 camt.054 with structured remittance, or a BAI2 lockbox file from a US bank) listing 40 invoice numbers, 2 deductions with reasons, and amounts
    when: I import it
    then: remittance lines kept (keyed [remittance_id, line_no]) and linked to the receipt by amount, date and payer; a proposed allocation per named invoice is opened as a match suggestion with model "remittance" (not AI), which I accept, change or reject as in PAY-14; a deduction line becomes a dispute on its invoice (PAY-10) with the customer's reason, never a write-off; an invoice number we do not know is listed, not guessed; a lockbox file whose items are the bank's own record creates the receipts itself (keyed "lockbox:<account>:<batch>:<item>"); importing the same file again adds nothing
    screen: Receipts
    reference: not built
  - id: PAY-18
    kind: person
    actor: cashier
    story: As the cashier, I want a customer's Direct Debit Instruction recorded and lodged with Bacs, so that we can collect their invoices on the due date.
    given: "customer 7311 completes a DDI online: sort code 20-00-00, account 12345678, our Service User Number 123456 on house bank BARC-01"
    when: I record the mandate
    then: 'a mandate keyed by its reference (unique per SUN, 6-18 characters, e.g. DOD-7311-01) with the bank details stored masked except in the Bacs file, pending until lodged: the next submission carries it as AUDDIS 0N; with no AUDDIS rejection it is active; a rejection (e.g. "no account") records the reason and leaves it unusable; cancelling sends 0C; the payer is sent the DDI confirmation and the Direct Debit Guarantee; a mandate with no collection for 13 months is dormant and needs a new instruction'
    screen: Direct debits
    path:
      process: payments.direct_debit
      choices: {}
    reference: not built
  - id: PAY-19
    kind: person
    actor: cashier
    story: As the cashier, I want a direct-debit run that collects what is due from customers with active mandates, so that subscription invoices are paid on the day without anyone typing a payment.
    given: three customers with active mandates and invoices due on or before collection date 2026-12-01 (a working day); advance notice of 10 working days (or the 3 agreed in the DDI terms)
    when: I prepare the run, the approver approves it, the notices go, and I submit the file
    then: one run keyed "dd:<bank_account_id>:<collection_date>" naming each invoice; advance notices sent no later than the notice period before the collection date, else the invoice waits for the next run (gate advance_notice); the file is Bacs Standard 18 with transaction code 01 for a first collection, 17 regular, 19 final (SEPA pain.008 for a EUR account), kept as an object; on the entry day (collection date) each collection becomes a receipt keyed "dd:<run>:<mandate>" dated the collection date and allocated to its invoices with decided_how direct_debit; the approver may not be the preparer; a run already submitted is never rebuilt
    screen: Direct debits
    reference: not built
  - id: PAY-20
    kind: person
    actor: cashier
    story: As the cashier, I want the bank's unpaid direct-debit report and mandate amendments applied, so that a bounced collection is owed again and a cancelled mandate is never collected.
    given: the run of 2026-12-01; an ARUDD report with one collection returned reason 0 (refer to payer) and one reason 1 (instruction cancelled); an ADDACS report with one account transferred (C) to new details
    when: I import the reports
    then: each returned collection reverses its receipt by PAY-07 (the return dated by the report, the reason recorded), so its invoices are owed again; reason 0 may be re-presented once with code 18 in a later run, reason 1 cancels the mandate; the ADDACS C updates the mandate's details from its date; a DDICA indemnity claim reverses the collection and marks the mandate disputed; every report line is applied once (keyed by the report's reference and line)
    screen: Direct debits
    reference: not built
  - id: PAY-21
    kind: person
    actor: cashier
    story: As the cashier, I want every line of the bank statement reconciled, debits as well as credits, so that the bank balance in the books is the bank's.
    given: "November's camt.053 for account BARC-01: 42 customer credits (receipts), 6 supplier payments from purchasing's run, one transfer of 20,000.00 to our deposit account, the HMRC VAT direct debit of 31,350.00, bank charges of 12.50, interest received of 3.21, a paying-in of 4 cheques"
    when: I reconcile it
    then: "each line ends matched or posted, never deleted: customer credits to their receipts; supplier payments to purchasing's payments (which confirms them, PUR-29); the transfer to the matching credit on the deposit account (PAY-25); the HMRC debit to tax's liability for 26AD; charges and interest posted by rule (PAY-24); the paying-in to its deposit batch (PAY-28); a line with no match stays open with the AI's suggestion (PAY-22); the statement is reconciled only when every line is (PAY-34); undoing a match is a recorded reversal with a reason"
    screen: Bank reconciliation
    path:
      process: payments.bank_reconciliation
      choices: {}
    reference: not built
  - id: PAY-22
    kind: person
    actor: cashier
    story: As the cashier, I want a suggested match or account for each line nobody has matched, so that I confirm rather than investigate, and nothing is booked without me.
    given: an open debit "AMZN MKTP UK 45.99" with no rule
    when: the AI suggests "office supplies, account 7500, tax code S, a receipt image needed" from the payee and past postings, and I accept it, change the account, or reject it
    then: the suggestion is a row with its evidence, confidence and model; accepting posts the line with the account and tax code I confirmed (the VAT computed by tax with direction purchase, reclaimable only with a VAT receipt attached), decided_how accepted | changed and me as the decider; rejecting leaves the line open; the AI never posts; its suggestions are the eval set
    screen: Bank reconciliation
    reference: not built
  - id: PAY-23
    kind: person
    actor: approver
    story: As the owner, I want reconciliation rules for the lines that recur (charges, interest, standing orders), so that the cashier reconciles the unusual ones and the books stay consistent.
    given: a rule "debit, text contains SERVICE CHARGE, account BARC-01 -> account 7900 bank charges, tax code E, mode suggest"
    when: I save it, and later switch it to auto
    then: the rule is a dated row (from tomorrow); in suggest mode a matching line gets the rule's proposal for the cashier to accept; auto mode needs pay:settings:controls and posts the line with decided_how auto, listed for the cashier to confirm or undo the same day; a rule never matches a line to a customer's invoice (receipts go through allocation); two rules matching one line are both shown, neither applied automatically
    screen: Bank reconciliation
    reference: not built
  - id: PAY-24
    kind: person
    actor: cashier
    story: As the cashier, I want bank charges, card fees and interest posted from their statement lines, so that the expense and the income are in the books on the bank's date.
    given: "lines: charges 12.50 (debit), card-terminal fees 38.20 (debit), interest received 3.21 (credit)"
    when: they are posted by rule or by me
    then: "one journal per line dated by the bank's booking date (a document-dated fact): bank charges / bank 12.50 (tax code E), card fees / bank 38.20 (E), bank / interest income 3.21 (no VAT), journal id derived from the statement line, so posting twice changes nothing; the account comes from the rule or the person, never a default; a posted line is undone only by a reversing journal with a reason"
    screen: Bank reconciliation
    reference: not built
  - id: PAY-25
    kind: person
    actor: cashier
    story: As the cashier, I want a transfer between our own accounts matched on both statements, so that money in transit is neither lost nor counted twice.
    given: a debit of 20,000.00 on BARC-01 on 2026-11-14 to account BARC-02, and the credit on BARC-02 on 2026-11-15
    when: the statements are reconciled
    then: the two lines are matched as one transfer (counterparty account, amount and currency equal, dates within transfer_window_days); each posts on its own date through the transfer clearing account (BARC-01 -> clearing on the 14th, clearing -> BARC-02 on the 15th), so clearing is zero once both arrive and shows the money in transit between; a debit with no credit yet stays as in transit, listed; amounts in different currencies are never matched as one transfer
    screen: Bank reconciliation
    reference: not built
  - id: PAY-26
    kind: person
    actor: cashier
    story: As the person with the petty cash or the till, I want a cash journal that is counted and closed each day, so that cash in the drawer and in the books agree.
    given: cash journal PETTY-UK01 in GBP with an opening balance of 200.00; today a customer pays 50.00 in cash, I buy stamps for 12.00 (no VAT) and cleaning materials for 24.00 including 4.00 VAT with a receipt
    when: I record the entries, count 214.00 and close the day
    then: the customer's 50.00 is a receipt (method cash) allocated like any other; the expenses post to their accounts with the VAT computed by tax (direction purchase, reclaimable only with the receipt image kept); the expected balance is 214.00, counted 214.00, difference 0.00; a difference posts to cash over/short and needs the approver above cash_difference_threshold; a closed day refuses entries; entries are dated today (business_timezone), never later, and never in a closed day; a deposit to the bank is a transfer matched on the statement (PAY-25)
    screen: Cash journal
    reference: not built
  - id: PAY-27
    kind: person
    actor: approver
    story: As the finance manager, I want our banks and every account we hold recorded per company, so that receipts, payments and direct debits use the right account and the invoice shows where to pay.
    given: companies UK01 and UK02; Barclays (house bank BARC, sort code 20-00-00, SUN 123456) holds UK01's GBP current, GBP deposit and EUR accounts; Lloyds holds UK02's GBP current
    when: I record them and make the GBP current account UK01''s default for receipts in GBP
    then: house banks and bank accounts (bank_account_id, company_code, currency, purposes receive | pay | dd_collect, the ledger account, IBAN and BIC, account number shown masked); one default receiving account per (company, currency) as a dated row from tomorrow; invoicing prints the default's details (PAY-47); a statement for an account never posts to another company; an account is closed from a date after today and keeps its history; purchasing pays out of the accounts with purpose pay
    screen: Settings
    reference: not built
  - id: PAY-28
    kind: person
    actor: cashier
    story: As the cashier, I want the cheques I pay in together recorded as one deposit, so that the single bank credit matches them and each cheque still pays its own invoices.
    given: four cheques received 2026-11-18 for 1,200.00, 800.00, 450.00 and 2,050.00
    when: I add them to a deposit batch, close it with paying-in slip 004512, and the statement shows one credit of 4,500.00
    then: each cheque is a receipt (method cheque) allocated to its invoices; the batch keyed "dep:<bank_account_id>:<slip>" totals 4,500.00; the statement line matches the batch, not the cheques, and is not made a receipt again; a bounced cheque later reverses its own receipt (PAY-07) and leaves the batch matched; a batch whose total differs from the line is never matched
    screen: Receipts
    reference: not built
  - id: PAY-29
    kind: person
    actor: approver
    story: As the owner, I want today's cash position and a 13-week forecast, so that I see a shortfall before it happens.
    given: bank balances from the latest statements, open invoices with customers' payment habits, a direct-debit run on 2026-12-01, supplier bills due, the VAT return 26AD due 2027-02-07 and CIS due on the 22nd
    when: I open Cash position
    then: "per currency (never summed across currencies): today's position (each account's latest closing balance plus unreconciled entries); then per week, expected receipts (open invoices by due date shifted by the customer's average days late over the last 12 months, direct debits on their collection dates, payment requests), expected payments (purchasing's open bills by due date and approved runs, the VAT and CIS liabilities from tax), and the running balance; each figure drills to its documents; derived on read, a snapshot kept only when I save one"
    screen: Cash position
    reference: not built
  - id: PAY-30
    kind: system
    actor: system
    trigger: on read (derived)
    story: The system works out what is still outstanding on each invoice whenever anyone looks, so that "paid" is never stale and no job can be missed.
    given: an invoice of 1,000.00 with a 400.00 receipt and a 100.00 credit note allocated
    when: anyone opens it, the overdue list or a statement
    then: 500.00 outstanding, part paid, in the bucket its age falls in; all computed from the invoice total (invoicing's read route) and SUM(allocations); no stored paid flag
    reference: implemented (ar-core, AR-10)
  - id: PAY-31
    kind: system
    actor: system
    trigger: daily, for customers with reminders on
    story: The system sends (or drafts) a reminder at each level the business set, so that chasing does not depend on someone remembering.
    given: an invoice 7 days overdue, levels at 7, 14 and 30 days, reminder mode suggest
    when: the daily pass runs, twice
    then: one level-1 reminder draft for the collector, keyed (invoice, level), so the second pass adds nothing; in mode auto it is sent; in mode off nothing happens; a promise or dispute suppresses it
    reference: not built
  - id: PAY-32
    kind: system
    actor: system
    trigger: when a receipt is recorded or imported
    story: The system suggests which invoices a receipt pays, so that the cashier confirms rather than searches.
    given: a receipt of 7,500.00 with reference "INV41 + INV42" and open invoices INV-2026-0041 (4,000.00) and INV-2026-0042 (3,500.00)
    when: it is recorded
    then: a suggestion row (pay_match_suggestion) proposing both, with the evidence (reference text, exact sum), a confidence and the model that made it; nothing is allocated until the cashier decides (PAY-14), unless the mode is auto-with-review and the match is exact, and then the allocation records that the system made it from that suggestion and the day's list asks a person to review it
    reference: not built
  - id: PAY-33
    kind: system
    actor: system
    trigger: when an allocation (or its undo) links a receipt and an invoice whose rates to the functional currency differ
    story: The system posts the realised exchange difference when a foreign-currency invoice is paid, so that the receivable in sterling clears to zero.
    given: an invoice of EUR 1,000.00 issued at 0.8500 (GBP 850.00) and a receipt of EUR 1,000.00 received at 0.8600 (GBP 860.00, the ledger's rate on received_on, kept on the receipt)
    when: the receipt is allocated to the invoice
    then: "a journal {source_document \"alloc:<payment_id>:<invoice_id>:<seq>\", event fx}: receivable 10.00 / fx_gain 10.00 (a loss the other way), dated the allocation date, posted once; the invoice's functional receivable is then zero; undoing the allocation posts event fx_reversal; a same-currency functional allocation posts nothing; the rates are the invoice's frozen rate (invoicing's totals route) and the receipt's rate, never re-read later"
    reference: not built
  - id: PAY-34
    kind: system
    actor: system
    trigger: when a statement is imported, and when its last line is reconciled
    story: The system checks each statement balances and joins the one before, and that the books agree with the bank, so that a missing or doubled statement is caught.
    given: statement 2026-11 for BARC-01 with opening 52,400.10 and closing 61,035.77; October's closing was 52,400.10
    when: it is imported and reconciled
    then: opening + SUM(lines) = closing exactly, or the import is refused naming the difference; its opening must equal the previous statement's closing (gate statement_continuous) or a gap is named; once every line is reconciled, the ledger balance of the account's gl account at the closing date (gl's account balance route) equals the closing balance, or the difference is shown with the unreconciled items; the statement is reconciled only then
    reference: not built
  - id: PAY-35
    kind: system
    actor: system
    trigger: daily, when an open-banking feed provider is composed
    story: A bank feed delivers statement lines like a file, so that reconciliation runs on yesterday's transactions without anyone downloading anything.
    given: an account-information provider capability (UK Open Banking, read-only consent granted by the owner for 90 days)
    when: it posts the day's transactions
    then: the same import as PAY-02 (POST /statements/ingest with the lines and the booked balance), keyed by the bank's transaction id, so a line delivered twice or later in a file is one line; the consent's expiry is shown to the owner 7 days ahead; the provider never writes pay_ tables
    reference: not built
  - id: PAY-40
    kind: integration
    actor: system
    trigger: when invoicing, sales or sales-order asks for a balance
    story: Other modules read settlement, open items and the credit status through payments' routes.
    given: invoicing listing invoices, sales-order confirming an order
    when: they call settlement(invoice_ids) or credit_status(bp_id)
    then: outstanding, state and age per invoice (as at a date, today by default); hold, reason and balance per customer, the hold also as a readiness gate {gate credit_ok, met, reason, fix} sales-order shows on confirm; never a read of pay_ tables
    owner: payments
    reference: not built
  - id: PAY-41
    kind: integration
    actor: system
    trigger: when invoicing approves a credit note or a write-off (AR-30)
    story: Credit notes and write-offs allocate here, so that every reduction of a balance is in one table.
    given: an approved credit note of 100.00 against an invoice
    when: invoicing calls allocate(source_kind credit_note, source_id, invoice_id, amount)
    then: one allocation row; calling twice with the same (source, invoice, seq) adds nothing
    owner: payments
    reference: not built
  - id: PAY-42
    kind: integration
    actor: system
    trigger: when a receipt, a reversal or a refund is recorded and the ledger is installed
    story: Money in and out posts to the ledger.
    given: a receipt of 1,200.00 GBP to the bank account mapped to ledger account 1200
    when: it posts
    then: "bank 1,200.00 / receivable 1,200.00, journal_id derived from the payment_id, so re-posting changes nothing; a foreign-currency receipt posts the realised FX difference against the invoice's rate. v3: the body is the ledger's GL-30 shape {source_module: \"payments\", source_document: payment_id | refund_id | statement line id | cash entry id | \"alloc:<payment_id>:<invoice_id>:<seq>\", event: receipt | receipt_reversal | refund | bank_line | cash_entry | cash_difference | fx | fx_reversal, lines with purpose receivable (bp_id) or account_id (the bank account's gl_account_id)}; gl derives the id from (source_document, event); the FX difference is PAY-33"
    owner: gl
    reference: "partial: erp-brick-payments posts receipts, reversals and refunds in the GL-30 shape (wiring commit); the FX difference is not posted yet (PAY-33)"
  - id: PAY-43
    kind: integration
    actor: system
    trigger: when a card or direct-debit provider confirms a payment
    story: A payment provider's confirmation is a receipt like a bank line.
    given: a provider capability installed (card or direct debit)
    when: it confirms a payment for an invoice
    then: a receipt keyed "provider:<provider>:<payment ref>", allocated to the invoice it was taken for; a duplicate notification adds nothing
    owner: payments
    reference: not built
  - id: PAY-44
    kind: integration
    actor: system
    trigger: once, at the cut-over from Odoo (first into a test copy, then live)
    story: The migration loads Odoo's bank journals, customer payments, reconciliations, unreconciled bank lines and refunds as receipts, allocations and refunds, so that every open balance arrives with the history that explains it.
    given: an Odoo database read under migration.odoo.reads, and ar's migration already run (the invoices and credit notes exist, keyed by their Odoo numbers)
    when: the migration runs, then runs again
    then: rows as in migration.odoo.maps, each with origin "odoo:<model>:<id>", dated with Odoo's own dates (facts of the past, the one place back-dated rows are loaded) and never re-dated; the second run adds nothing (migration.odoo.idempotent_on); it writes through this module's use cases with pay:migration:import, never through the routes' person checks and never into ar_ tables
    owner: payments
    reference: not built
  - id: PAY-36
    kind: integration
    actor: system
    trigger: when the cashier allocates a receipt to a down-payment request (ar AR-35)
    story: A receipt that pays a payment request settles it into a deposit invoice and pays that, so that VAT follows the money.
    given: request DPR-2026-0004 for 6,000.00; a receipt of 6,000.00 received 2026-11-20
    when: I allocate the receipt to the request
    then: payments calls invoicing's POST /payment-requests/{request_id}/settle {payment_id, received_on, amount}, receives the deposit invoice id, and allocates the receipt to it (an ordinary allocation); a part payment settles a part; a retry settles once (invoicing keys on payment_id); the request itself never carries an allocation
    owner: ar
    reference: not built
  - id: PAY-45
    kind: integration
    actor: system
    trigger: when a contractor customer pays our invoice net of a CIS deduction
    story: The CIS a contractor customer deducted is allocated against our invoice and recorded with tax, so that the invoice is paid and the deduction is claimed back.
    given: our invoice of labour 2,000.00 (reverse charge, no VAT) to contractor 7311; a receipt of 1,600.00 and their payment and deduction statement showing 400.00 deducted, tax month ending 2026-12-05
    when: I allocate the receipt and record the deduction with the statement reference
    then: the receipt allocates 1,600.00; payments calls tax's POST /cis/suffered, then allocates 400.00 with source_kind cis_deduction, source_id the statement reference, so the invoice is paid; tax refusing (more than 30% of the labour) leaves the 400.00 outstanding with tax's reason; the same statement recorded twice adds nothing
    owner: tax
    reference: not built
  - id: PAY-46
    kind: integration
    actor: system
    trigger: when tax prepares a VAT return under cash accounting, or a bad-debt repayment
    story: Tax reads which receipts paid which invoices and when, so that cash accounting and bad debt relief are computed from the one allocation table.
    given: a business on cash accounting
    when: tax calls GET /cash-basis?from=&to= and GET /allocations?invoice_ids=&from=
    then: receipt allocations with the receipt's received_on and the allocation date, reversing rows included; never a read of pay_ tables
    owner: payments
    reference: not built
  - id: PAY-47
    kind: integration
    actor: system
    trigger: when invoicing issues or renders an invoice
    story: Invoicing prints our receiving bank account from payments, so that the account on the invoice is the one we reconcile.
    given: UK01's default GBP receiving account BARC-01
    when: invoicing calls GET /bank-accounts/default?company_code=UK01&currency=GBP
    then: 'account name, sort code, account number (in full for printing, the caller holding pay:receipt:read), IBAN and BIC; no default answers 404 with {gate: receiving_account, fix: "Payments > Settings > Bank accounts"}'
    owner: payments
    reference: not built
  - id: PAY-48
    kind: integration
    actor: system
    trigger: when a statement debit is matched to a supplier payment
    story: A supplier payment found on the statement confirms purchasing's payment, so that "paid" is the bank's word and nobody confirms it twice.
    given: purchasing's run paid Acme 4,200.00 from BARC-01 with reference PAY-RUN-0045-03
    when: the statement debit is matched to it
    then: payments calls purchasing's POST /supplier-payments/{payment_id}/confirm with {bank_ref, paid_on (the booking date)}; a payment already confirmed by its clerk is matched without a second confirmation; a rejected payment (a credit returning it) is matched to the reversal; payments never writes pur_ tables
    owner: purchasing
    reference: not built
  - id: PAY-49
    kind: integration
    actor: system
    trigger: when a shop seals a cash bag for the bank (pos POS-26)
    story: Another module hands over a cash bag it has sent to the bank, so that the single bank credit matches the bag and the cash in transit clears without anyone re-keying it.
    given: shop LON-SOHO seals bag 004513 with 1,240.00 GBP for BARC-SHOP-UK, expected on 2026-11-16; the statement of 2026-11-16 shows a credit of 1,240.00 and, a week later, one of 597.50 for bag 004514 sent as 600.00
    when: pos calls POST /deposit-batches/expected twice for bag 004513, then the statement lines are matched
    then: one deposit batch keyed batch_ref "dep:pos:LON-SOHO:004513", kind cash, deposited, total 1,240.00, clearing purpose cash_in_transit (the repeat answers the same batch); the credit of 1,240.00 matches it and posts bank 1,240.00 / cash_in_transit 1,240.00 (journal derived from the batch_ref and event deposit_match, so posting twice changes nothing), and is never made a receipt; the 597.50 credit does not match bag 004514 (gate batch_total_equals_line) until an approver who is not the cashier accepts the 2.50 difference with a reason, which posts cash_over_short 2.50 (event deposit_difference); payments never writes pos_ tables
    owner: payments
    reference: not built
  - id: PAY-50
    kind: integration
    actor: system
    trigger: when a shop closes a card terminal's batch (pos POS-26), and when the provider's payout reaches the bank
    story: Another module tells payments what a card provider owes us for a day, so that the provider's net credit is matched, its fees are posted, and card clearing is empty when everything was paid.
    given: LON-SOHO's terminal batch T1-2026-11-14 with gross 2,310.40 GBP from provider ACQ1; the owner's card_fee_max_pct 2.50; the statement of 2026-11-17 shows a credit of 2,282.69 from ACQ1 for that day and LON-KX's 1,000.00 batch of the same day (a combined payout of 3,267.69)
    when: pos calls POST /card-settlements/expected for each batch, and the credit is matched
    then: 'two expected settlements keyed "card:pos:LON-SOHO:T1-2026-11-14" and "card:pos:LON-KX:T1-2026-11-14"; the one credit of 3,267.69 matches both (one provider, a credit may settle several expected settlements, never part of one); fees = 3,310.40 - 3,267.69 = 42.71 (1.29%, within 2.50%) split pro rata by gross, rounded once, the rest to the last; the journal: bank 3,267.69, card_fees 42.71 (tax code E, the fee being exempt), card_clearing 3,310.40 (event card_settlement); a credit whose fees exceed card_fee_max_pct leaves the settlements in state differs, shown on the bank reconciliation screen until an approver accepts the difference (to card_fees) or records a claim against the provider with a reason; a repeated expected settlement answers the same row'
    owner: payments
    reference: not built
  - id: PAY-51
    kind: person
    actor: approver
    story: As the finance manager, I want money another module owes a person (an approved expense claim) paid out from our bank in a payout run, approved by me, so that a reimbursement is paid once, to the right account, and the module that asked knows it was paid.
    given: expense claim UK01-EXP-2026-00042 rev 1 approved for 352.40 GBP to Jane Doe (person partner 9001, person P-77); her bank details held by hr; paying account BARC-01
    when: expenses calls POST /payouts (payout_ref exp:UK01-EXP-2026-00042:r1:pay) twice; I approve it; the cashier exports the payout run of 2026-11-12; the statement of 2026-11-13 shows the debit
    then: one payout keyed payout_ref, requested, then approved by me (I am not the payee, matched on email AND sub, and not the cashier who exports it); export writes a pain.001 (or Bacs Standard 18) file of the approved payouts of BARC-01 for that date, the payee's account read from hr at export (GET /persons/P-77/bank-account, payments' client only, never shown on a screen nor stored outside the file object); the matched debit (or the cashier's bank_ref and date, today or earlier) makes it paid and posts employee_payable 352.40 / bank 352.40 (event payout, journal derived from the payout_ref); after commit payments calls expenses' POST /reports/UK01-EXP-2026-00042/paid {payout_ref, payment_id, paid_on 2026-11-13, amount 352.40} and retries until it answers 2xx; a payout the bank returns is returned (the journal reversed, event payout_return) and the requester is told nothing paid; a requested or approved payout may be cancelled by its requester (POST /payouts/{payout_ref}/cancel) or by me with a reason, never once exported
    screen: Payouts
    reference: not built
  - id: PAY-52
    kind: integration
    actor: approver
    trigger: when payroll approves a pay run (payroll PRL-12)
    story: Payroll hands payments one payout per payslip with the pay run as their batch, so that salaries are approved once as a batch, paid on the pay date in one Bacs file, and each payslip knows it was paid.
    given: "run UK01-M-2026-27-M08: 46 payouts with batch_ref UK01-M-2026-27-M08, net 98,412.60, due_on 2026-11-30, paying account BARC-01; the finance approver F is payee of one of them (person 2190)"
    when: payroll calls POST /payouts 46 times (and repeats them); F approves the batch; another approver approves F's payout; the cashier exports on 2026-11-26; the statement of 2026-11-30 shows the Bacs debit
    then: 46 payouts requested, a repeat answering the same; F''s approve_payout_batch approves the 45 of which F is not the payee (email AND sub) and leaves F''s own for another approver; export (approve by 2026-11-25 for a Bacs pay date of 2026-11-30, gate bacs_lead) writes one Bacs Standard 18 file for the batch with each payee''s account read from hr (HR-59, payments'' client only); the matched debit makes each paid and posts net_pay_payable / bank (event payout, journal per payout_ref); after commit payments calls payroll''s POST /pay-runs/UK01-M-2026-27-M08/payouts/{payout_ref}/paid {status paid, payment_id, paid_on, amount} and retries until 2xx; a payout the bank returns is returned and payroll is told with status returned; with the owner''s payout_auto_approve_sources naming payroll, the batch is approved by the request itself (approved_by "payroll")
    screen: Payouts
    owner: payments
    reference: not built
processes:
  - key: receipt
    pcf_id: 10744
    pcf_name: Process accounts receivable (AR)
    reference_names:
      - Order to Cash
      - Apply cash
    brick: payments
  - key: refund
    pcf_id: 10744
    pcf_name: Process accounts receivable (AR)
    reference_names:
      - Customer refunds
    brick: payments
  - key: collection
    pcf_id: 10745
    pcf_name: Manage and process collections
    reference_names:
      - Collections
      - Dunning
    brick: payments
  - key: bank_reconciliation
    pcf_id: 10744
    pcf_name: Process accounts receivable (AR)
    reference_names:
      - Electronic bank statement post-processing
      - Bank reconciliation
    brick: payments
  - key: direct_debit
    pcf_id: 10744
    pcf_name: Process accounts receivable (AR)
    reference_names:
      - Direct debit collection
      - Payment run for receivables
    brick: payments
  - key: cash_journal
    pcf_id: 10744
    pcf_name: Process accounts receivable (AR)
    reference_names:
      - Cash journal
      - Petty cash
    brick: payments
  - key: payout
    pcf_id: null
    pcf_name: Pay a person on another module's request
    reference_names:
      - Expense reimbursement payment
      - Payment run for employees
    brick: payments
workflows:
  - entity: receipt
    states:
      - received
      - reversed
    terminal:
      - received
      - reversed
    transitions:
      - key: customer_pays
        name: The customer pays
        from: none
        to: received
        by: customer
        effect: the money arrives in the bank or through a provider
      - key: record_customer_receipt
        name: Record the receipt
        from: none
        to: received
        by: cashier
        gate: received_on today or earlier (business_timezone); the bank reference is not on another receipt
        effect: unallocated cash on the customer
      - key: import_statement
        name: Import the bank statement line
        from: none
        to: received
        by: system
        gate: a statement was uploaded (camt.053 or CSV); the line is a credit dated on or after statement_import_from
        effect: keyed bank:account:entry, dated by the bank; a re-import adds nothing
      - key: ai_suggest_match
        name: AI suggests the invoices it pays (suggest)
        from: received
        to: received
        by: system
        gate: the receipt has unallocated money and the step is on
        effect: a suggestion row with its evidence, confidence and model; nothing allocated
      - key: decide_match
        name: Accept
        change or reject the suggestion: null
        from: received
        to: received
        by: cashier
        gate: an open suggestion; the receipt and the invoices still have the amounts it proposed
        effect: allocation rows citing the suggestion and the person who accepted it; the suggestion closed as accepted, changed or rejected
      - key: allocate
        name: Allocate to invoices
        from: received
        to: received
        by: cashier
        gate: never more than the receipt's remainder or an invoice's outstanding; the same currency; dated today
        effect: allocation rows; settlement derived
      - key: settle_difference
        name: Propose the small difference as a write-off
        from: received
        to: received
        by: cashier
        gate: the difference is within match_tolerance
        effect: a write-off proposal to invoicing (AR-09)
      - key: unallocate
        name: Undo an allocation
        from: received
        to: received
        by: cashier
        requires: reason
        gate: the row is not already reversed
        effect: a reversing allocation row dated today; history kept
      - key: reverse_receipt
        name: Reverse a returned payment
        from: received
        to: reversed
        by: cashier
        requires: bank_return_ref
        gate: the return date is today or earlier and not before received_on
        effect: its allocations reversed by reversing rows; the invoices are owed again
      - key: apply_settlement_discount
        name: Apply the early-settlement discount (v3)
        from: received
        to: received
        by: cashier
        gate: received within the invoice's discount window; the difference is exactly the terms' discount
        effect: invoicing's discount credit allocated (ar AR-38), or an allocation source_kind settlement_discount under invoice_wording (PAY-16)
      - key: record_cis_deduction
        name: Record CIS the customer deducted (v3)
        from: received
        to: received
        by: cashier
        requires: statement_ref
        gate: tax accepts the deduction (at most 30% of the labour)
        effect: tax's CIS suffered recorded; an allocation source_kind cis_deduction (PAY-45)
      - key: allocate_to_payment_request
        name: Allocate to a down-payment request (v3)
        from: received
        to: received
        by: cashier
        gate: an issued request with something remaining
        effect: invoicing settles it into a deposit invoice; the receipt allocated to that invoice (PAY-36)
      - key: post_fx_difference
        name: Post the realised exchange difference (v3)
        from: received
        to: received
        by: system
        gate: an allocation whose receipt and invoice rates differ
        effect: receivable against fx_gain or fx_loss, keyed on the allocation (PAY-33)
    readiness:
      - gate: received_not_future
        reason: Received on {received_on}, which is after today ({today}, {business_timezone})
        fix: Record it on the day the bank shows it
      - gate: reference_unused
        reason: Bank reference {bank_reference} is already on receipt {payment_id}
        fix: Open receipt {payment_id}
      - gate: within_remainder
        reason: Allocating {amount}; the receipt has {remainder} left and the invoice {outstanding} outstanding
        fix: Allocate at most the smaller figure
      - gate: same_currency
        reason: The receipt is {receipt_currency}, the invoice {invoice_currency}
        fix: Allocate a receipt in the invoice's currency
      - gate: within_tolerance
        reason: The difference {difference} is above the tolerance {match_tolerance}
        fix: Leave it outstanding, or ask the owner (pay:settings:controls)
      - gate: suggestion_current
        reason: Since the suggestion, {invoice_id} was settled or the receipt allocated elsewhere
        fix: Ask for a fresh suggestion, or allocate by hand
      - gate: return_dated_ok
        reason: The return date {returned_on} is after today or before the receipt ({received_on})
        fix: Use the date on the bank's return advice
      - gate: within_discount_window
        reason: Received on {received_on}; the discount ended on {settlement_discount_until}
        fix: "No discount: the difference stays outstanding"
      - gate: discount_matches_terms
        reason: "{difference} is not {pct}% of the gross outstanding {gross}"
        fix: Allocate; the difference stays outstanding or goes to the tolerance
      - gate: within_cis_rate
        reason: The deduction {deduction} is above 30% of the labour {labour}
        fix: Check the contractor's statement
    note: "How much of a receipt is allocated (unallocated, partly, fully) is DERIVED from its allocations. For the screens the brick answers each receipt's readiness: one {gate, met, reason, fix} per gate of its next transitions (met true or false, the reason in words with the figures, a fix that names the act or the link), so no screen re-derives a rule."
  - entity: refund
    states:
      - requested
      - approved
      - rejected
      - paid
    terminal:
      - rejected
      - paid
    transitions:
      - key: request_refund
        name: Request a refund
        from: none
        to: requested
        by: cashier
        gate: the customer has unallocated credit at least the amount, in the refund's currency
      - key: approve_refund
        name: Approve the refund
        from: requested
        to: approved
        by: approver
        approval: true
        gate: the approver is not the requester; the credit is still at least the amount
      - key: reject_refund
        name: Reject the refund
        from: requested
        to: rejected
        by: approver
        requires: reason
      - key: pay_refund
        name: Record the refund paid
        from: approved
        to: paid
        by: cashier
        requires: bank_ref
        gate: paid_on today or earlier and not before the approval
        effect: allocated against the credit; posted in the ledger
    readiness:
      - gate: credit_covers
        reason: Refund {amount}; the customer's unallocated credit in {currency} is {credit}
        fix: Refund at most {credit}
      - gate: second_person
        reason: You requested this refund
        fix: Another approver approves it
      - gate: paid_dated_ok
        reason: Paid on {paid_on}, after today or before the approval ({approved_on})
        fix: Use the date on the bank's payment
  - entity: collection
    states:
      - overdue
      - reminded
      - promised
      - disputed
      - resolved
    terminal:
      - resolved
    transitions:
      - key: fall_overdue
        name: The invoice falls overdue
        from: none
        to: overdue
        by: system
        gate: the due date passed with outstanding above zero
        timer: the due date
      - key: ai_draft_reminder
        name: AI drafts the reminder for this customer (suggest)
        from:
          - overdue
          - reminded
        to: reminded
        by: system
        gate: reminders are on and a level is due
        effect: a draft in the collector's queue (suggest) or sent (auto)
        timer: the level's days after due
      - key: send_reminder
        name: Send the reminder
        from: reminded
        to: reminded
        by: collector
        gate: "mode suggest: a person sends the draft"
      - key: call_customer
        name: Chase by hand
        from:
          - overdue
          - reminded
        to:
          - promised
          - disputed
        by: collector
        gate: a promise date after today, or a dispute reason
        effect: a promise with a date, or a dispute with a reason
      - key: customer_responds
        name: The customer promises or disputes
        from: reminded
        to: reminded
        by: customer
      - key: promise_lapses
        name: The promise date passes unpaid
        from: promised
        to: reminded
        by: system
        gate: outstanding above zero on the promised date
        timer: the promise date
      - key: resolve_payment_dispute
        name: Resolve the dispute
        from: disputed
        to: resolved
        by: collector
        requires: outcome
        effect: a credit note in invoicing, or reminders resume
      - key: paid_in_full
        name: Settled
        from:
          - overdue
          - reminded
          - promised
        to: resolved
        by: system
        gate: outstanding is zero (derived)
    readiness:
      - gate: promise_in_future
        reason: The promise date {promise_date} is today or earlier
        fix: Record the date the customer promised, after today
      - gate: level_due
        reason: Level {level} is due {days_after_due} days after {due_date}
        fix: Wait for the date, or chase by hand
      - gate: not_suppressed
        reason: A promise until {promise_date} or an open dispute suppresses reminders
        fix: Resolve the dispute, or wait for the promise date
    note: a collection is per customer and invoice; settlement and age are derived on read, never stored
  - entity: remittance
    states:
      - imported
      - applied
    terminal:
      - applied
    transitions:
      - key: import_remittance_file
        name: Import a remittance advice or lockbox file (v3)
        from: none
        to: imported
        by: cashier
        gate: CSV, camt.054 or BAI2; not imported before (keyed on the file's own reference)
        effect: lines kept and linked to their receipt; a remittance suggestion opened per receipt; a lockbox file records its receipts (PAY-17)
      - key: apply_remittance
        name: Accept the allocation the remittance proposes
        from: imported
        to: applied
        by: cashier
        gate: each line's invoice is open for at least the amount
        effect: allocations with decided_how remittance; deductions become disputes
    readiness:
      - transition: import_remittance_file
        gate: file_not_imported
        reason: This file ({file_ref}) was imported on {imported_on}
        fix: Open that import
      - transition: apply_remittance
        gate: invoice_open_for_amount
        reason: "{invoice_no} has {outstanding} outstanding; the remittance pays {amount}"
        fix: Change the line, or record the difference as a dispute
  - entity: bank_statement
    states:
      - imported
      - balanced
      - reconciled
    terminal:
      - reconciled
    transitions:
      - key: import_bank_statement
        name: Import the statement, every line (v3)
        from: none
        to: imported
        by: cashier
        gate: camt.053, MT940 or CSV for a known bank account; not imported before
        effect: a statement line per entry; customer credits also receipts (PAY-02); a bank feed (PAY-35) arrives the same way
      - key: check_statement_balance
        name: Check the statement balances and follows the last
        from: imported
        to: balanced
        by: system
        gate: opening + lines = closing; opening = the previous closing
        effect: or the difference named (PAY-34)
      - key: complete_bank_reconciliation
        name: Close the statement as reconciled
        from: balanced
        to: reconciled
        by: cashier
        gate: every line matched or posted; the ledger balance at the closing date equals the closing balance
        effect: the statement locked; a later change is a reversal on an open statement
    readiness:
      - transition: check_statement_balance
        gate: statement_balances
        reason: Opening {opening} + lines {lines} = {computed}, not the closing {closing}
        fix: Re-download the statement from the bank
      - transition: check_statement_balance
        gate: statement_continuous
        reason: The opening {opening} is not the previous closing {previous_closing} ({previous_statement})
        fix: Import the missing statement
      - transition: complete_bank_reconciliation
        gate: all_lines_reconciled
        reason: "{open_count} lines are still open"
        fix: Match or post each
      - transition: complete_bank_reconciliation
        gate: book_equals_bank
        reason: The ledger shows {book} for {gl_account_id} at {closing_date}; the bank {closing}
        fix: The unreconciled items are listed
  - entity: statement_line
    states:
      - open
      - matched
      - posted
    terminal: []
    transitions:
      - key: suggest_statement_match
        name: AI suggests a match or an account (suggest)
        from: open
        to: open
        by: system
        gate: the line is open and the step is on
        effect: a suggestion row with evidence and confidence; nothing posted (PAY-22)
      - key: apply_reconciliation_rule
        name: Apply the rule that names the line
        from: open
        to:
          - open
          - posted
        by: system
        gate: exactly one rule matches; in suggest mode it proposes, in auto mode (owner's control) it posts and lists the line for review
        effect: decided_how rule or auto (PAY-23)
      - key: match_statement_line
        name: Match to a receipt, supplier payment, transfer, refund, deposit batch, cash deposit, direct-debit run or HMRC payment
        from: open
        to: matched
        by: cashier
        gate: the same amount and currency; the counterpart is not matched to another line
        effect: the counterpart confirmed (purchasing PAY-48, tax's liability), the transfer clearing posted on each side (PAY-21, PAY-25, PAY-28)
      - key: post_statement_line
        name: Post the line to an account
        from: open
        to: posted
        by: cashier
        requires: account
        gate: an account and a tax code; VAT reclaimable only with a receipt image
        effect: a journal dated by the bank, keyed on the line (PAY-24)
      - key: unmatch_statement_line
        name: Undo a match or a posting
        from:
          - matched
          - posted
        to: open
        by: cashier
        requires: reason
        gate: its statement is not reconciled
        effect: a reversing journal for a posting; the counterpart released
    readiness:
      - transition: apply_reconciliation_rule
        gate: one_rule
        reason: Rules {rules} all match this line
        fix: Choose one, or narrow a rule
      - transition: match_statement_line
        gate: amounts_equal
        reason: The line is {amount} {currency}; {counterpart} is {other}
        fix: Match the right item, or post the difference
      - transition: match_statement_line
        gate: counterpart_free
        reason: "{counterpart} is already matched to line {other_line}"
        fix: Undo that match first
      - transition: post_statement_line
        gate: account_and_tax_code
        reason: A posting needs an account and a tax code
        fix: Choose them, or apply a rule
      - transition: unmatch_statement_line
        gate: statement_open
        reason: Statement {statement_id} is reconciled
        fix: Reverse on the current statement
  - entity: dd_mandate
    states:
      - pending
      - active
      - cancelled
      - dormant
    terminal:
      - cancelled
      - dormant
    transitions:
      - key: record_dd_mandate
        name: Record a Direct Debit Instruction (v3)
        from: none
        to: pending
        by: cashier
        requires: mandate_ref
        gate: a reference unique for the SUN; the bank details; the payer's confirmation
        effect: pending until lodged (PAY-18)
      - key: lodge_dd_mandate
        name: Lodge the instruction with Bacs (AUDDIS 0N)
        from: pending
        to:
          - active
          - cancelled
        by: system
        gate: the next submission
        effect: active without a rejection; an AUDDIS rejection cancels it with the reason
      - key: cancel_dd_mandate
        name: Cancel the instruction (AUDDIS 0C)
        from:
          - pending
          - active
        to: cancelled
        by: cashier
        requires: reason
      - key: apply_addacs_advice
        name: Apply the bank's amendment or cancellation (ADDACS)
        from: active
        to:
          - active
          - cancelled
        by: system
        effect: details changed from the advice's date, or cancelled with the code (PAY-20)
      - key: mark_mandate_dormant
        name: Mark an unused instruction dormant
        from: active
        to: dormant
        by: system
        gate: no collection for 13 months
        timer: daily
    readiness:
      - transition: record_dd_mandate
        gate: reference_unique
        reason: Mandate reference {mandate_ref} is already used for SUN {sun}
        fix: Use the next reference
      - transition: record_dd_mandate
        gate: house_bank_has_sun
        reason: "{bank_account_id} has no Service User Number"
        fix: Settings > Bank accounts
  - entity: dd_run
    states:
      - draft
      - approved
      - submitted
      - collected
    terminal:
      - collected
    transitions:
      - key: prepare_dd_run
        name: Prepare a direct-debit run (v3)
        from: none
        to: draft
        by: cashier
        gate: a collection date that is a working day; active mandates; invoices due on or before it
        effect: keyed dd:<bank_account_id>:<collection_date>, invoices named (PAY-19)
      - key: approve_dd_run
        name: Approve the run
        from: draft
        to: approved
        by: approver
        approval: true
        gate: the approver is not the preparer (email and sub)
      - key: send_dd_notices
        name: Send the advance notices
        from: approved
        to: approved
        by: system
        gate: each notice at least the notice period before the collection date
        effect: an invoice whose notice is late moves to the next run
      - key: submit_dd_run
        name: Submit the Bacs file
        from: approved
        to: submitted
        by: cashier
        requires: submission_ref
        gate: the file produced (Standard 18 or pain.008) and submitted by the input day
        effect: the file kept as an object; the run frozen
      - key: collect_dd_run
        name: Record the collections on the entry day
        from: submitted
        to: collected
        by: system
        gate: the collection date is today
        effect: a receipt per collection, allocated with decided_how direct_debit
        timer: the collection date
      - key: apply_arudd_returns
        name: Apply the unpaid direct-debit report (ARUDD)
        from: collected
        to: collected
        by: cashier
        effect: returned collections reversed (PAY-07); reason 0 may be re-presented once (code 18)
    readiness:
      - transition: prepare_dd_run
        gate: working_day
        reason: "{collection_date} is not a Bacs working day"
        fix: Pick the next working day
      - transition: approve_dd_run
        gate: second_person
        reason: You prepared this run
        fix: Another approver approves it
      - transition: send_dd_notices
        gate: advance_notice
        reason: The notice for {invoice_no} would reach the customer {days} working days before {collection_date}; {notice_days} are needed
        fix: It moves to the next run
      - transition: submit_dd_run
        gate: input_day
        reason: The file must be submitted by {input_day} for {collection_date}
        fix: Move the run's date
  - entity: cash_day
    states:
      - open
      - closed
    terminal:
      - closed
    transitions:
      - key: open_cash_day
        name: Open the cash journal's day (v3)
        from: none
        to: open
        by: cashier
        gate: the previous day is closed; today in business_timezone
        effect: the opening balance is the last counted balance
      - key: record_cash_entry
        name: Record a cash receipt
        expense or deposit: null
        from: open
        to: open
        by: cashier
        gate: dated today; an expense has an account and a tax code
        effect: a receipt, a posting with tax's VAT, or a transfer to the bank (PAY-26)
      - key: count_cash
        name: Count the cash
        from: open
        to: open
        by: cashier
        effect: the counted amount, by denomination
      - key: close_cash_day
        name: Close the day
        from: open
        to: closed
        by: cashier
        gate: counted; a difference above cash_difference_threshold approved by an approver who is not the cashier
        effect: the difference posted to cash over/short; the day refuses entries
      - key: approve_cash_difference
        name: Approve a cash difference
        from: open
        to: open
        by: approver
        approval: true
        requires: reason
        gate: the approver is not the person who counted
    readiness:
      - transition: record_cash_entry
        gate: day_open_today
        reason: Entries are dated today ({today}); day {day} is closed
        fix: Record it today
      - transition: close_cash_day
        gate: counted
        reason: The cash has not been counted
        fix: Count the cash
      - transition: close_cash_day
        gate: difference_approved
        reason: The difference {difference} is above {cash_difference_threshold}
        fix: An approver who did not count approves it
  - entity: deposit_batch
    states:
      - open
      - deposited
      - matched
    terminal:
      - matched
    transitions:
      - key: start_deposit_batch
        name: Start a paying-in (v3)
        from: none
        to: open
        by: cashier
        gate: a bank account with purpose receive
      - key: add_cheque_to_batch
        name: Add a cheque receipt to the paying-in
        from: open
        to: open
        by: cashier
        gate: a receipt with method cheque not in another batch
      - key: close_deposit_batch
        name: Close the paying-in with its slip
        from: open
        to: deposited
        by: cashier
        requires: slip_no
        effect: keyed dep:<bank_account_id>:<slip>; the total fixed (PAY-28)
      - key: match_deposit_batch
        name: Match the batch to its statement credit
        from: deposited
        to: matched
        by: cashier
        gate: the line's amount equals the batch total
        effect: "v6: an expected batch (kind cash) posts bank / its clearing_purpose (cash_in_transit), event deposit_match; a cheque batch posts nothing (its receipts did)"
      - key: expect_deposit_batch
        name: Take a bag another module sent to the bank (v6, PAY-49)
        from: none
        to: deposited
        by: system
        gate: batch_ref unique (a repeat answers the same); a bank account of the company with purpose receive, in the currency; amount above zero
        effect: a deposited batch of kind cash with no cheques, total = amount, clearing_purpose and source_module kept; matched like any batch
      - key: accept_deposit_difference
        name: Accept a counted difference on a bag
        from: deposited
        to: matched
        by: approver
        approval: true
        requires: reason
        gate: an expected cash batch; the line's amount differs from the total; the approver is not the cashier (email AND sub)
        effect: bank (the line) and cash_over_short (the difference) / the clearing purpose (the total), event deposit_difference
    readiness:
      - transition: match_deposit_batch
        gate: batch_total_equals_line
        reason: The batch is {total}; the line {amount}
        fix: Find the cheque missing from the batch; for a cash bag, an approver accepts the bank's count (PAY-49)
  - entity: card_settlement
    states:
      - expected
      - matched
      - differs
    terminal:
      - matched
    transitions:
      - key: expect_card_settlement
        name: Take an expected card settlement (v6, PAY-50)
        from: none
        to: expected
        by: system
        gate: settlement_ref unique (a repeat answers the same); the provider is a configured card provider of the company; gross above zero
        effect: "an expected settlement: provider, company, currency, gross, business_date, transaction_count; card_clearing carries the gross (posted by the module that sold)"
      - key: match_card_settlement
        name: Match the provider's credit
        from: expected
        to: matched
        by:
          - system
          - cashier
        gate: a statement credit from the provider settles one or more expected settlements of that provider whole; fees = gross - credit, at or under card_fee_max_pct of the gross
        effect: journal bank (credit) + card_fees (tax code E) / card_clearing (gross), event card_settlement, derived from the statement line id; the fees split pro rata by gross, rounded once
      - key: flag_card_difference
        name: Hold a settlement whose fees are too high
        from: expected
        to: differs
        by: system
        gate: the matched credit implies fees above card_fee_max_pct
        effect: listed on the bank reconciliation screen with the fee and the limit
      - key: accept_card_difference
        name: Accept the difference with a reason
        from: differs
        to: matched
        by: approver
        approval: true
        requires: reason
        gate: the approver is not the cashier who matched it (email AND sub)
        effect: posted as match_card_settlement, the whole difference to card_fees, or to a claim against the provider (purpose card_claim) when the reason is a claim
    readiness:
      - transition: match_card_settlement
        gate: fees_within_limit
        reason: the fees are {fee} ({fee_pct}%), above card_fee_max_pct {max}%
        fix: an approver accepts it, or claim it from the provider
      - transition: accept_card_difference
        gate: second_person
        reason: you matched this credit (same email or sign-in)
        fix: another approver
  - entity: payout
    states:
      - requested
      - approved
      - exported
      - paid
      - returned
      - cancelled
    terminal:
      - paid
      - returned
      - cancelled
    transitions:
      - key: request_payout
        name: Take a payout another module asks for (v6, PAY-51)
        from: none
        to: requested
        by: system
        gate: payout_ref unique (a repeat answers the same); the payee is a person partner (business-partner kind person); a paying bank account of the company in the currency; amount above zero; due_on today or later
        effect: a payout with its source_module and source_document, waiting for an approver
      - key: approve_payout
        name: Approve the payout
        from: requested
        to: approved
        by: approver
        approval: true
        gate: the approver is not the payee nor the requester's approver of the source document (email AND sub)
        effect: who and when
      - key: approve_payout_batch
        name: Approve a batch of payouts (v7, PAY-52)
        from: requested
        to: approved
        by:
          - approver
          - system
        approval: true
        gate: every payout of the batch_ref whose payee is not the approver (email AND sub); by system only when payout_auto_approve_sources names the source_module
        effect: the approver's own payout, if any, stays requested for another approver
      - key: export_payout_run
        name: Export the payout run
        from: approved
        to: exported
        by: cashier
        gate: the payee's bank account read from hr (GET /persons/{person_id}/bank-account) or, without it, typed and confirmed by a second person; the cashier is not the approver
        effect: a pain.001 or Bacs Standard 18 file of the approved payouts of one paying account and payment date, stored as an object; the bank details only in the file
      - key: confirm_payout_paid
        name: Confirm what the bank paid
        from: exported
        to: paid
        by:
          - system
          - cashier
        gate: a matched statement debit, or a bank_ref and paid_on today or earlier given by the cashier
        effect: journal <clearing_purpose> / bank (employee_payable for expenses, net_pay_payable for payroll), event payout, derived from the payout_ref; after commit the source is told (expenses' POST /reports/{source_document}/paid; v7 payroll's POST /pay-runs/{source_document}/payouts/{payout_ref}/paid), retried until it answers 2xx
      - key: record_payout_return
        name: Record a payout the bank returned
        from: paid
        to: returned
        by:
          - system
          - cashier
        gate: a statement credit naming the payout, or the cashier with the bank's return reference
        effect: "the journal reversed (event payout_return); the source is told it was not paid (v7: payroll's route with status returned)"
      - key: cancel_payout
        name: Cancel the payout
        from:
          - requested
          - approved
        to: cancelled
        by:
          - system
          - approver
        requires: reason
        gate: not exported; by system only for the module that requested it (POST /payouts/{payout_ref}/cancel)
    readiness:
      - transition: approve_payout
        gate: second_person
        reason: you are the payee, or approved the claim it pays (same email or sign-in)
        fix: another approver
      - transition: export_payout_run
        gate: payee_bank_known
        reason: no bank account for {payee} from hr
        fix: hr records it (the person's sensitive data), or type it and have a second person confirm it
      - transition: export_payout_run
        gate: exporter_not_approver
        reason: you approved a payout in this run
        fix: another cashier exports it
entities:
  - id: receipt
    meaning: money received from a customer
    natural_key: 'payment_id: derived from where it came from ("bank:<account>:<statement entry ref>", "provider:<provider>:<ref>"), else a uuid with the bank reference checked for duplicates (shared.yaml keys.payment_id)'
    key_attributes:
      - bp_id
      - currency
      - amount
      - received_on
      - method (transfer | card | direct_debit | cheque | cash | lockbox)
      - bank_account_id
      - bank_reference
      - remittance_text
      - status
      - batch_id (the deposit batch, v3)
      - fx_rate (the ledger's rate on received_on, kept, v3)
      - returned_on
      - bank_return_ref
      - return_reason_code (ARUDD, v3)
      - statement_line_id (v3)
      - origin
      - recorded_by
      - recorded_at
    relationships:
      - -> business_partner
      - 0..n allocation
    master_data: module-owned
    reference_table: pay_receipt
  - id: allocation
    meaning: what settled what; the single mechanism behind every reduction in a balance
    natural_key:
      - source_kind
      - source_id
      - invoice_id
      - seq
    key_attributes:
      - amount
      - currency
      - allocated_on
      - allocated_by
      - reason
      - reverses_seq
      - suggestion_id
      - decided_how (manual | accepted | changed | auto | remittance | direct_debit, v3)
      - fx_journal_id (the realised difference posted, v3)
      - origin
    master_data: module-owned
    reference_table: pay_allocation
    note: "source_kind is receipt | credit_note | writeoff | refund, and from v3 settlement_discount (under invoice_wording) and cis_deduction (source_id the contractor's statement reference). An undo is a negative row that names the row it reverses; nothing is deleted or edited. allocated_on is the day it was recorded (business_timezone), never earlier. outstanding(invoice, as_at) = total - SUM(amount) over rows with allocated_on <= as_at. decided_how is manual | accepted | changed | auto (auto-with-review), with suggestion_id when an AI suggestion was involved: the audit of what the AI proposed and who agreed."
  - id: match_suggestion
    meaning: what the AI proposed a receipt pays, and what a person decided
    natural_key: suggestion_id (derived "<payment_id>#<n>")
    key_attributes:
      - payment_id
      - proposed_lines (invoice_id
      - amount)
      - evidence
      - confidence
      - model
      - created_at
      - status (open | accepted | changed | rejected | superseded)
      - decided_by
      - decided_at
    master_data: module-owned
    reference_table: pay_match_suggestion
    note: a suggestion is never an allocation; it is kept after the decision, so the eval set (ai_steps) is the table itself
  - id: refund
    meaning: money paid back to a customer
    natural_key: refund_id
    key_attributes:
      - bp_id
      - currency
      - amount
      - status
      - requested_by
      - requested_at
      - approved_by
      - approved_at
      - reason
      - bank_ref
      - paid_on
      - origin
    master_data: module-owned
    reference_table: pay_refund
  - id: collection_event
    meaning: a reminder sent, a promise made, a dispute raised or resolved
    natural_key:
      - bp_id
      - kind
      - level
      - ref_id
    key_attributes:
      - invoice_id (nullable)
      - occurred_on
      - promise_date
      - reason
      - outcome
      - note
      - draft_text
    master_data: module-owned
    reference_table: pay_collection
    note: a promise is usually per CUSTOMER covering several invoices; a reminder is per invoice and level
  - id: statement
    meaning: an open-item statement as at a date, as it was sent
    natural_key:
      - bp_id
      - as_at
    key_attributes:
      - closing_balance
      - currency
      - sent_at
    master_data: module-owned
    reference_table: pay_statement
  - id: credit_policy
    meaning: a customer's credit limit from a date
    natural_key:
      - bp_id
      - valid_from
    key_attributes:
      - credit_limit_amount
      - currency
      - reason
      - set_by
      - set_at
      - origin
    master_data: module-owned (configuration)
    reference_table: pay_credit
    note: "dated rows: a new limit is a new row with valid_from after today; the limit on a date is the row with the latest valid_from on or before it; nothing is edited"
  - id: credit_override
    meaning: a person's manual hold or early release on a customer
    natural_key:
      - bp_id
      - kind
      - starts_at
    key_attributes:
      - ends_on
      - reason
      - set_by
    master_data: module-owned
    reference_table: pay_credit_override
    note: kind hold | release; starts_at is the moment it is recorded, never earlier; ends_on after today; lifting one early is a new row, not an edit. The hold itself is DERIVED (limit, oldest overdue, overrides)
  - id: house_bank
    meaning: a bank the business holds accounts with (v3)
    natural_key: house_bank_id (the business's own code, e.g. BARC)
    key_attributes:
      - name
      - bic
      - sort_code
      - address
      - sun (Bacs Service User Number, for direct debits)
      - creditor_id (SEPA, for EUR direct debits)
    master_data: module-owned (configuration)
    reference_table: pay_house_bank
  - id: bank_account
    meaning: "an account the business receives into or pays from (v3: purchasing pays out of the same accounts)"
    natural_key: bank_account_id (the business's own code)
    key_attributes:
      - name
      - currency
      - iban_or_sort_code_masked
      - gl_account_id
      - house_bank_id, company_code (shared.yaml keys.company_code), purposes (receive | pay | dd_collect), account_name, status (open | closed), closed_from (v3)
    master_data: module-owned (configuration)
    reference_table: pay_bank_account
    note: "v3: an account belongs to one company; the full account number is held for the Bacs file and the invoice print only, shown masked elsewhere; closing is from a date after today, history kept"
  - id: bank_account_read
    meaning: the audit row written for every unmasked read of one of our bank accounts (PAY-27 seam)
    natural_key: read_id (derived from bank_account_id, caller and requested_at)
    key_attributes:
      - bank_account_id
      - caller (the machine identity)
      - requested_at
      - purpose
    master_data: module-owned
    reference_table: pay_bank_account_read
    note: append-only; no person route reads or edits it except pay:settings:controls holders reading the log
  - id: default_bank_account
    meaning: the receiving account invoices print for a company and currency, from a date (v3)
    natural_key:
      - company_code
      - currency
      - valid_from
    key_attributes:
      - bank_account_id
      - set_by
    master_data: module-owned (configuration)
    reference_table: pay_default_bank_account
  - id: bank_statement
    meaning: one statement of one account as the bank sent it (v3)
    natural_key: statement_id = "<bank_account_id>:<the bank's statement id or the period>"
    key_attributes:
      - bank_account_id
      - from_date
      - to_date
      - opening_balance
      - closing_balance
      - format (camt053 | mt940 | csv | feed)
      - file_key
      - lifecycle
      - reconciled_by
      - reconciled_at
    master_data: module-owned
    reference_table: pay_bank_statement
  - id: statement_line
    meaning: one entry of a statement, credit or debit, and what it was reconciled to (v3)
    natural_key: line_id = "bank:<bank_account_id>:<the bank's entry reference>" (the same key a credit's receipt takes)
    key_attributes:
      - statement_id
      - booked_on
      - value_on
      - amount (signed)
      - currency
      - counterparty_name
      - counterparty_account_masked
      - text
      - bank_tx_code
      - lifecycle
      - matched_kind (receipt | supplier_payment | transfer | refund | deposit_batch | cash_deposit | dd_run | tax_liability | rule | manual)
      - matched_ref
      - account_id
      - tax_code
      - cost_centre
      - journal_id
      - decided_how
      - decided_by
      - decided_at
    master_data: module-owned
    reference_table: pay_statement_line
  - id: line_suggestion
    meaning: what the AI or a rule proposed for a statement line, and the decision (v3)
    natural_key: suggestion_id = "<line_id>#<n>"
    key_attributes:
      - line_id
      - source (ai | rule)
      - proposal (matched_kind
      - matched_ref
      - account_id
      - tax_code)
      - evidence
      - confidence
      - model
      - status (open | accepted | changed | rejected | superseded)
      - decided_by
      - decided_at
    master_data: module-owned
    reference_table: pay_line_suggestion
  - id: reconciliation_rule
    meaning: a recurring line's treatment, from a date (v3)
    natural_key:
      - rule_id
      - valid_from
    key_attributes:
      - bank_account_id (or any)
      - direction (debit | credit)
      - text_contains
      - counterparty_contains
      - amount_min
      - amount_max
      - account_id
      - tax_code
      - cost_centre
      - mode (suggest | auto)
      - set_by
    master_data: module-owned (configuration)
    reference_table: pay_reconciliation_rule
    note: auto needs pay:settings:controls; a rule never allocates to an invoice
  - id: remittance
    meaning: a remittance advice or lockbox file and its lines (v3)
    natural_key:
      - remittance_id
      - line_no
    key_attributes:
      - file_ref
      - format (csv | camt054 | bai2)
      - payer
      - payment_id
      - invoice_ref
      - amount
      - deduction_reason
      - status
    master_data: module-owned
    reference_table: pay_remittance
  - id: dd_mandate
    meaning: a customer's Direct Debit Instruction (v3)
    natural_key:
      - sun
      - mandate_ref
    key_attributes:
      - bp_id
      - bank_account_id (ours)
      - payer_sort_code
      - payer_account_masked
      - payer_account_name
      - scheme (bacs | sepa_core)
      - lifecycle
      - lodged_on
      - auddis_result
      - cancelled_reason
      - last_collected_on
    master_data: module-owned
    reference_table: pay_dd_mandate
  - id: dd_run
    meaning: one direct-debit collection run (v3)
    natural_key: run_id = "dd:<bank_account_id>:<collection_date>"
    key_attributes:
      - collection_date
      - input_day
      - lifecycle
      - prepared_by
      - approved_by
      - file_key
      - submission_ref
      - total
      - count
    relationships:
      - 1..n dd_collection
    master_data: module-owned
    reference_table: pay_dd_run
  - id: dd_collection
    meaning: one mandate's collection in a run (v3)
    natural_key:
      - run_id
      - mandate_ref
    key_attributes:
      - bp_id
      - invoice_ids
      - amount
      - transaction_code (01 | 17 | 18 | 19)
      - notice_sent_on
      - payment_id
      - return_reason_code
      - returned_on
    master_data: module-owned
    reference_table: pay_dd_collection
  - id: cash_journal
    meaning: a till or petty-cash box (v3)
    natural_key: cash_journal_id
    key_attributes:
      - company_code
      - currency
      - gl_account_id
      - name
      - status
    master_data: module-owned (configuration)
    reference_table: pay_cash_journal
  - id: cash_entry
    meaning: one cash receipt, expense or deposit (v3)
    natural_key:
      - cash_journal_id
      - entry_date
      - seq
    key_attributes:
      - kind (receipt | expense | deposit | difference)
      - amount
      - payment_id
      - account_id
      - tax_code
      - vat_amount
      - receipt_image_key
      - transfer_to_bank_account_id
      - journal_id
      - recorded_by
    master_data: module-owned
    reference_table: pay_cash_entry
  - id: cash_count
    meaning: a day's count and close (v3)
    natural_key:
      - cash_journal_id
      - day
    key_attributes:
      - opening
      - expected
      - counted
      - denominations_json
      - difference
      - counted_by
      - approved_by
      - closed_at
    master_data: module-owned
    reference_table: pay_cash_count
  - id: deposit_batch
    meaning: cheques paid in together on one slip (v3)
    natural_key: batch_id = "dep:<bank_account_id>:<slip_no>"
    key_attributes:
      - lifecycle
      - total
      - cheque_count
      - deposited_on
      - statement_line_id
      - kind (cheque | cash; v6)
      - "batch_ref (v6: the handing module's key, dep:<module>:<shop_id>:<bag_no>; batch_id = batch_ref for an expected batch)"
      - source_module (v6)
      - "clearing_purpose (v6: cash_in_transit)"
      - expected_on (v6)
    master_data: module-owned
    reference_table: pay_deposit_batch
  - id: card_settlement
    meaning: what a card provider owes us for one terminal batch, matched to its net credit (v6)
    natural_key: settlement_ref ("card:<module>:<shop_id>:<terminal_batch_ref>")
    key_attributes:
      - provider
      - company_code
      - currency
      - gross (NUMERIC 18,2)
      - business_date
      - transaction_count
      - lifecycle
      - statement_line_id
      - fees (NUMERIC 18,2)
      - source_module
    master_data: module-owned
    reference_table: pay_card_settlement
  - id: payout
    meaning: money we pay a person on another module's request (an expense reimbursement), approved and paid in a run (v6)
    natural_key: "payout_ref (the requester's: \"exp:<report_id>:r<revision>:pay\", v7 \"prl:<pay_run_id>:<person_id>:net[:<n>]\")"
    key_attributes:
      - payee_bp_id
      - person_id
      - company_code
      - currency
      - amount (NUMERIC 18,2)
      - due_on
      - reference
      - source_module
      - source_document
      - clearing_purpose
      - batch_ref (v7)
      - lifecycle
      - paying_bank_account_id
      - approved_by
      - approved_sub
      - run_object_key
      - payment_id
      - bank_ref
      - paid_on
      - notified_at
    master_data: module-owned
    reference_table: pay_payout
    note: "the payee's bank details are never a column here: read from hr at export into the file object only"
  - id: reminder_level
    meaning: one step of the follow-up policy
    natural_key:
      - policy_id
      - level
    key_attributes:
      - days_after_due
      - channel
      - template
      - mode (off | suggest | auto)
    master_data: module-owned (configuration)
    reference_table: pay_reminder_level
  - id: settings
    meaning: how this business takes and chases money
    natural_key: key
    key_attributes:
      - value
    master_data: module-owned (configuration)
    reference_table: pay_settings
master_data:
  owns:
    - pay_receipt
    - pay_allocation
    - pay_match_suggestion
    - pay_refund
    - pay_collection
    - pay_statement
    - pay_credit
    - pay_credit_override
    - pay_bank_account
    - pay_reminder_level
    - pay_settings
    - pay_house_bank
    - pay_default_bank_account
    - pay_bank_statement
    - pay_statement_line
    - pay_line_suggestion
    - pay_reconciliation_rule
    - pay_remittance
    - pay_dd_mandate
    - pay_dd_run
    - pay_dd_collection
    - pay_cash_journal
    - pay_cash_entry
    - pay_cash_count
    - pay_deposit_batch
  references:
    - entity: business_partner
      owner: business-partner
      how: pay_receipt.bp_id = business_partner.bp_id
      if_absent: a receipt names a customer by text, and a statement cannot be produced
    - entity: invoice
      owner: ar
      how: pay_allocation.invoice_id, totals read through invoicing's route
      if_absent: nothing to allocate to; payments without invoicing records cash only
  warning: Two places that each know "what has been paid" is how a customer is chased for an invoice they paid last week. Allocations live here and nowhere else; invoicing and sales read them.
jobs:
  - story: PAY-30
    trigger: on read
    runs: settlement and aging
    where_options:
      - derived on read
    idempotent: nothing stored
  - story: PAY-31
    trigger: daily
    runs: reminders at each customer's levels
    where_options:
      - the composition's clock worker calling the brick's tick route (--reserved 1 --max-replicas 1)
      - external scheduler
    idempotent: "keyed (bp_id, kind reminder, level, invoice_id): a second pass sends nothing further"
  - story: PAY-32
    trigger: when a receipt is recorded or imported
    runs: the match suggestion
    where_options:
      - in the same request
      - for an import: after the import commits
    idempotent: a suggestion only, keyed <payment_id>#<n>; re-running supersedes an open suggestion, never a decided one
  - story: PAY-44
    trigger: once at cut-over (test copy, then live)
    runs: the Odoo migration (migration.odoo)
    where_options:
      - a one-off job run by the composition's migration client
      - from the operator's machine
    idempotent: "migration.odoo.idempotent_on: a second run matches every row and adds nothing"
  - story: PAY-19
    trigger: on each run's collection date (the entry day)
    runs: collect_dd_run, and daily mark_mandate_dormant
    where_options:
      - the composition's clock worker calling the brick's dd route
    idempotent: receipts keyed dd:<run>:<mandate>; collecting twice adds nothing
  - story: PAY-22
    trigger: after a statement import commits
    runs: suggest_statement_match for each open line, then apply_reconciliation_rule
    where_options:
      - after the import commits
      - in the same worker as PAY-32
    idempotent: a suggestion keyed <line_id>#<n>; re-running supersedes an open one, never a decided one; an auto rule posts a line once (journal keyed on the line)
  - story: PAY-33
    trigger: when an allocation or its undo commits
    runs: post_fx_difference
    where_options:
      - in the same request
      - after the allocation commits
    idempotent: keyed alloc:<payment_id>:<invoice_id>:<seq> + event
  - story: PAY-35
    trigger: daily, by the feed provider
    runs: import_bank_statement through POST /statements/ingest
    where_options:
      - the provider's call
    idempotent: keyed on the bank's transaction id
seams:
  - story: PAY-40
    with: ar
    owner: payments
    rule: invoicing, sales-order and crm call settlement / open_items / credit_status; none of them reads pay_ tables or keeps its own balance
  - story: PAY-41
    with: ar
    owner: payments
    rule: invoicing calls allocate for credit notes and write-offs; payments calls invoicing's read route for invoice totals, currency and due dates; neither writes the other's tables
  - story: PAY-42
    with: gl
    owner: gl
    rule: payments posts through the ledger's journal route with journal_id derived from payment_id / refund_id; the bank account's gl_account_id is the debit
  - story: PAY-43
    with: payments
    owner: payments
    rule: a provider is a capability (a seam provider) calling record_receipt with its own reference; the provider never writes pay_ tables
  - story: PAY-44
    with: ar
    owner: payments
    rule: ar's migration runs first and keys invoices and credit notes by their Odoo numbers; this one allocates to those ids and never writes ar_ tables; the per-customer open balance it reconciles must equal ar's
  - story: PAY-42
    with: gl
    owner: gl
    route: POST /journal-entry/journals
    permission: gl:journal:post
    request: "{journal_id (str of the BIGINT derived from (source_document, event)), source_module: \"payments\", source_document: payment_id | refund_id | line_id | \"<cash_journal_id>:<day>:<seq>\" | \"alloc:<payment_id>:<invoice_id>:<seq>\", event: receipt | receipt_reversal | refund | bank_line | bank_line_reversal | transfer | cash_entry | cash_difference | fx | fx_reversal | deposit_match | deposit_difference | card_settlement | payout | payout_return (v6), company_code, journal_date, currency, fx_rate, memo, lines: [{purpose: receivable, bp_id, against_document (optional; the invoice_id this receipt line clears)} | {account_id: the bank account's gl_account_id} | {purpose: fx_gain | fx_loss | transfer_clearing | cash_over_short} | {account_id, tax_code, cost_centre}, debit | credit]}"
    response: "201 {journal_id, entry_no, period_id, status: posted, replay: false, lines: n}; 200 the same with replay: true; 409 {gate: period_open | journal_conflict, reason, fix}; 422 {gate: balanced | purposes_mapped | ledger_assigned | bounds, reason, fix}"
    rule: gl's GL-30 shape (the wiring commit already sends source_module, source_document, event and purposes for receipts, reversals and refunds); v3 adds the events above; v6 adds deposit_match and deposit_difference (purposes cash_in_transit, cash_over_short), card_settlement (card_clearing, card_fees with tax code E, card_claim), payout and payout_return (employee_payable); payments never writes gl_ tables
    status: aligned 2026-10-22 with gl GL-30
  - story: PAY-34
    with: gl
    owner: gl
    route: GET /accounts/{account_id}/balance?as_of=
    permission: gl:report:read
    request: path account_id (the bank account's gl_account_id); query as_of=YYYY-MM-DD, company_code (optional; the default company)
    response: 200 {account_id, company_code, as_of, balance, currency}; 404 when unknown or not visible
    rule: the book side of book_equals_bank
    status: aligned 2026-10-22 with gl GL-61
  - story: PAY-16
    with: ar
    owner: ar
    route: POST /invoices/{invoice_id}/settlement-discount
    permission: ar:invoice:settle_discount
    request: "{payment_id, received_on: YYYY-MM-DD, discount_amount}"
    response: "201 {credit_id, credit_no, net, tax, total, approved_by: terms}; 200 the same for the same payment_id; 422 {gate: within_discount_window | discount_matches_terms, reason, fix}"
    rule: "invoicing raises the credit at the invoice's rates and allocates it through POST /allocations/from-source (PAY-41); the discount window and percentage come from GET /invoice-totals, which returns settlement_discount_pct and settlement_discount_until from ar v4; this call: {payment_id, received_on, discount_amount}"
    status: aligned 2026-10-22 with ar AR-38
  - story: PAY-36
    with: ar
    owner: ar
    route: POST /payment-requests/{request_id}/settle
    permission: ar:payment_request:settle
    request: "{payment_id, received_on: YYYY-MM-DD, amount, currency}"
    response: "200 {request_id, deposit_invoice_id, invoice_no, amount, remaining, status: issued | settled}; 200 the same for the same payment_id; 422 {gate: within_request}"
    rule: "payments then allocates the receipt to deposit_invoice_id; this call: {payment_id, received_on, amount, currency}"
    status: aligned 2026-10-22 with ar AR-36
  - story: PAY-45
    with: tax
    owner: tax
    route: POST /cis/suffered
    permission: tax:cis:record_suffered
    request: "{contractor_bp_id, statement_ref, tax_month, invoice_id, payment_id, gross, materials, deduction}"
    response: "201 {recorded: true}; 200 {replay: true} for the same statement; 422 {gate: within_cis_rate, reason, fix}"
    rule: the allocation source_kind cis_deduction is written only after tax accepts
    status: aligned 2026-10-22 with tax TAX-41
  - story: PAY-46
    with: tax
    owner: payments
    route: GET /cash-basis?from=&to=
    permission: pay:receipt:read
    request: query from, to
    response: "200 {allocations: [{payment_id, invoice_id, seq, amount, currency, received_on, allocated_on, source_kind}]}"
    rule: receipt allocations in the window by received_on, plus those allocated in it for receipts of earlier windows; reversing rows included
    status: aligned 2026-10-22 with tax TAX-36
  - story: PAY-46
    with: tax
    owner: payments
    route: GET /allocations?invoice_ids=&from=
    permission: pay:receipt:read
    request: query invoice_ids=<comma-separated, at most 200>, from=YYYY-MM-DD (optional)
    response: "200 {allocations: [{source_kind, source_id, invoice_id, seq, amount, currency, allocated_on, received_on, reverses_seq}]}"
    rule: the one allocation read for tax (bad debt repayment, settlement discounts under invoice_wording) and ar (late interest, AR-39)
    status: aligned 2026-10-22 with tax TAX-35
  - story: PAY-47
    with: ar
    owner: payments
    route: GET /bank-accounts/default?company_code=&currency=
    permission: pay:receipt:read
    request: query company_code, currency
    response: "200 {bank_account_id, name, account_name, sort_code, account_number, iban, bic}; 404 {gate: receiving_account, reason, fix}"
    rule: the receiving account printed on invoices and in the UBL PaymentMeans
    status: aligned 2026-10-22 with ar AR-03
  - story: PAY-47
    with: pos
    owner: payments
    route: GET /bank-accounts/default?company_code=&currency=
    permission: pay:receipt:read
    request: query company_code, currency
    response: "200 {bank_account_id, name, account_name, sort_code, account_number, iban, bic}; 404 {gate: receiving_account, reason, fix}"
    rule: the receiving account printed on invoices and in the UBL PaymentMeans
    status: "payments v8 2026-10-26 (spec gaps wave 4): listed for pos POS-26 (the bank account a sealed cash bag is paid into; pos no longer takes bank_account_id typed in)"
  - story: PAY-27
    with: purchasing
    owner: payments
    direction: inbound
    route: GET /bank-accounts/{bank_account_id}/account-details
    permission: pay:bank_account:read_unmasked
    request: "path bank_account_id (one of our accounts with purpose pay); header: the caller's service-client identity"
    response: 200 {bank_account_id, company_code, account_name, sort_code, account_number, iban, bic, currency}; 403 for any token that is not a machine identity holding the permission; 404 when unknown or not a pay account
    rule: "the unmasked account number of OUR paying (originator) account, for purchasing's bank file (Bacs Standard 18 carries the originator's sort code and account; pain.001 the IBAN). Served ONLY to purchasing's machine identity: no person role holds pay:bank_account:read_unmasked, so a human sees the account masked everywhere. EVERY read writes a pay_bank_account_read row (bank_account_id, caller, requested_at, purpose) before the answer is returned; a read that cannot be logged is refused. Purchasing never stores the number: it asks per export. Payees' accounts stay purchasing's own (supplier_bank)"
    status: proposed 2026-10-23; replaces purchasing's temporary originator-bank-account input
  - story: PAY-48
    with: purchasing
    owner: purchasing
    route: GET /supplier-payments?bank_account_id=&status=&from=&to=
    permission: purchasing:payment:read
    request: query bank_account_id, status=sent (the candidates a statement debit is matched to)
    response: "200 {payments: [{payment_id, run_id, bp_id, bank_account_id, amount, currency, reference, status: sent | confirmed | rejected, paid_on, bills: [{bill_id, amount}]}]}"
    rule: the candidates a statement debit is matched to; payments never reads pur_ tables
    status: aligned 2026-10-22 with purchasing PUR-66
  - story: PAY-48
    with: purchasing
    owner: purchasing
    route: POST /supplier-payments/{payment_id}/confirm
    permission: purchasing:payment:confirm
    request: "{bank_ref, paid_on (YYYY-MM-DD, the statement date)}"
    response: "200 {payment_id, status: confirmed, replay: false}; 200 the same with replay: true when already confirmed; 409 {gate: payment_open, reason, fix}"
    rule: "purchasing's confirm_paid_from_bank (PUR-66): the bank confirms PUR-29's payment; purchasing allocates it to its bills and posts it"
    status: aligned 2026-10-22 with purchasing PUR-66
  - story: PAY-29
    with: purchasing
    owner: purchasing
    route: GET /bills/open
    permission: purchasing:bill:read
    request: query as_of=YYYY-MM-DD, bp_id (optional)
    response: "200 {as_of, items: [{bill_id, bill_no, bp_id, currency, total, outstanding, due_date}]}"
    rule: the payables side of the forecast (the route gl already reads, GL-36)
    status: aligned 2026-10-22 with purchasing PUR-14
  - story: PAY-29
    with: tax
    owner: tax
    route: GET /vat-returns/liabilities
    permission: tax:vat_return:read
    request: none
    response: "200 {liabilities: [{period_key, charge_type, outstanding_amount, due_date}], cis_due: [{tax_month, amount, due_date}]}"
    rule: the VAT and CIS payments due, in the forecast
    status: aligned 2026-10-22 with tax TAX-32
  - story: PAY-21
    with: tax
    owner: tax
    route: GET /vat-returns/liabilities
    permission: tax:vat_return:read
    request: none
    response: "200 {liabilities: [{period_key, charge_type, outstanding_amount, due_date}], cis_due: [{tax_month, amount, due_date}]}"
    rule: an HMRC debit on the statement is matched to the liability it pays; the posting is bank against vat_control
    status: aligned 2026-10-22 with tax TAX-32
  - story: PAY-26
    with: tax
    owner: tax
    route: POST /documents/compute
    permission: tax:document_tax:compute
    request: "{document_ref, direction (sale | purchase; default sale, so every v2 caller is unchanged), tax_point (YYYY-MM-DD), bp_id, currency, treatment_code (optional), prices_include_tax (optional), fx_rate (optional), ship_to_country (optional, v4: ISO 3166; goods lines are taxed where the ship-to receives them, services by the customer), self_billed (optional, v4: a purchase-direction invoice we issue on the supplier's behalf under a self-billing agreement), lines: [{line_no, description, category_code (the product's tax_category), supply (goods | services), amount, quantity, unit_price, pinned_from (optional; an earlier frozen document_ref), tax_code (with pinned_from)}]}"
    response: "{document_ref, status, lines: [{line_no, tax_code, rate_pct, net, vat, gross, vat_reclaimable, vat_non_reclaimable (purchase)}], summary: [{tax_code, rate_pct, net, vat, vat_due, gross}], totals: {net, vat, vat_due, gross, method, vat_reclaimable, vat_non_reclaimable}, gates: [{gate, met, reason, fix}], version}; a pinned line answers the earlier document's frozen code and rate, or the gates pinned_code / pinned_within_original"
    rule: payments sends document_ref pay_cash:<cash_journal_id>:<day>:<seq> | pay_line:<line_id> with direction purchase; reclaimable only with the receipt image kept
    status: built; aligned 2026-10-22 with tax TAX-23
  - story: PAY-27
    with: gl
    owner: gl
    route: GET /companies/{company_code}
    permission: gl:company:read
    request: path company_code
    response: "200 {company_code, name, country, functional_currency, chart_id, bp_id, financial_year_end_month, ledgers: [ledger_id]}; 404 when unknown or not visible"
    rule: a bank account names a company that exists; its functional currency is gl's
    status: aligned 2026-10-22 with gl GL-61
  - story: PAY-40
    with: gl
    owner: payments
    route: GET /settlement
    permission: pay:receipt:read
    request: query invoice_ids=<comma-separated, at most 200>, as_at=YYYY-MM-DD (optional; today)
    response: "200 {as_at, invoices: [{invoice_id, currency, total, outstanding, state, ...}], unknown: [ids]}"
    rule: "built (erp-brick-payments use_cases.settlement): outstanding, state and age per invoice, derived on read; gl reconciles the receivable control account (GL-31), re-splits it by profit centre (GL-25) and revalues open foreign-currency items (GL-48) from it; gl never reads pay_ tables"
    status: aligned 2026-10-22 with gl GL-31, GL-25, GL-48
  - story: PAY-40
    with: tax
    owner: payments
    route: GET /settlement
    permission: pay:receipt:read
    request: query invoice_ids=<comma-separated, at most 200>, as_at=YYYY-MM-DD (optional; today)
    response: "200 {as_at, invoices: [{invoice_id, currency, total, outstanding, state, ...}], unknown: [ids]}"
    rule: "built (erp-brick-payments use_cases.settlement): outstanding, state and age per invoice, derived on read; bad debt relief: the unpaid consideration at the claim date; tax never reads pay_ tables"
    status: aligned 2026-10-22 with tax TAX-34
  - story: PAY-40
    with: sales-order
    owner: payments
    route: GET /customers/{bp_id}/credit-ok
    permission: pay:credit:read
    request: path bp_id = the order's PAYER
    response: "{gate: credit_ok, met, reason, fix, hold_mode (warn | block), bp_id}"
    rule: "built (erp-brick-payments use_cases.credit_ok): the readiness gate sales-order asks at confirm, for the payer, never the sold-to; skipped for CASH orders"
    status: aligned 2026-10-22 with sales-order SO-40
  - story: PAY-40
    with: ar
    owner: payments
    route: GET /collections/aging?as_at=
    permission: pay:receipt:read
    request: query as_at=YYYY-MM-DD, company_code (optional; the default company)
    response: "200 {as_at, buckets: [{bucket, invoices: [{invoice_id, bp_id, outstanding, days_past_due, disputed}]}]}"
    rule: the aging AS AT a date (a period end) in the business's buckets, which ar's doubtful-debt provision (AR-45) reads; GET /collections/overdue (built) is today's working list and takes no date; derived on read like settlement
    status: aligned 2026-10-22 with ar AR-45
  - story: PAY-46
    with: ar
    owner: payments
    route: GET /allocations?invoice_ids=&from=
    permission: pay:receipt:read
    request: query invoice_ids=<comma-separated, at most 200>, from=YYYY-MM-DD (optional)
    response: "200 {allocations: [{source_kind, source_id, invoice_id, seq, amount, currency, allocated_on, received_on, reverses_seq}]}"
    rule: "ar's late-payment interest (AR-39): late days run to each receipt's received_on"
    status: aligned 2026-10-22 with ar AR-39
  - story: PAY-27
    with: purchasing
    owner: payments
    route: GET /bank-accounts
    permission: pay:receipt:read
    request: none
    response: "{bank_accounts: [{bank_account_id, name, currency, iban_or_sort_code_masked, gl_account_id}]}"
    rule: "built (erp-brick-payments list_bank_accounts): the accounts the company holds, which customers pay into AND supplier runs pay out of; purchasing posts a run to its gl_account_id and never writes pay_ tables"
    status: aligned 2026-10-22 with purchasing PUR-19
  - story: PAY-40
    with: ar
    owner: payments
    route: GET /settlement
    permission: pay:receipt:read
    request: query invoice_ids=<comma-separated, at most 200>, as_at=YYYY-MM-DD (optional; today)
    request_schema:
      - field: invoice_ids
        type: array
        required: true
        in: query
        items:
          - string
      - field: as_at
        type: date
        required: false
        in: query
    response: "200 {as_at, invoices: [{invoice_id, currency, total, outstanding, state, age_days, ...}], unknown: [ids]}"
    rule: "ar stores no paid flag: it asks payments for outstanding, state and age per invoice, 200 ids a call"
    status: added 2026-10-23, seam-gap PR
  - story: PAY-40
    with: ar
    owner: payments
    route: GET /customers/{bp_id}/open-items
    permission: pay:receipt:read
    request: path bp_id; query as_at=YYYY-MM-DD (optional; today)
    request_schema:
      - field: bp_id
        type: bp_id
        required: true
        in: path
      - field: as_at
        type: date
        required: false
        in: query
    response: "200 {invoices: [...], credits: [{kind: receipt | credit_note, id, amount (negative)}], balances per currency}"
    rule: "ar reads what is still unallocated on a credit note: one still listed under credits (kind credit_note) has that amount left; one fully allocated is not listed, so 0"
    status: added 2026-10-23, seam-gap PR
  - story: PAY-40
    with: ar
    owner: payments
    route: GET /customers/{bp_id}/credit-ok
    permission: pay:credit:read
    request: path bp_id = the customer
    request_schema:
      - field: bp_id
        type: bp_id
        required: true
        in: path
    response: "{gate: credit_ok, met, reason, fix, hold_mode (warn | block), bp_id}"
    rule: "ar reads the readiness gate before it raises or sends to a customer: a hold is the gate not met; no balance is read"
    status: added 2026-10-23, seam-gap PR
  - story: PAY-41
    with: ar
    owner: payments
    route: POST /allocations/from-source
    permission: pay:allocation:from_source
    request: "{source_kind: credit_note | writeoff, source_id, invoice_id, amount, seq (optional, default 1)}"
    request_schema:
      - field: source_kind
        type: string
        required: true
        values:
          - credit_note
          - writeoff
      - field: source_id
        type: string
        required: true
      - field: invoice_id
        type: invoice_id
        required: true
      - field: amount
        type: decimal
        required: true
      - field: seq
        type: integer
        required: false
    response: 200 the allocation {source_kind, source_id, invoice_id, seq, amount, allocated_on}; the same (source, invoice, seq) twice adds nothing
    rule: ar allocates an approved credit note or write-off against an invoice, so every reduction of a balance is in the one allocations table; payments dates it today itself; ar writes no pay_ table
    status: added 2026-10-23, seam-gap PR
  - story: PAY-49
    with: pos
    owner: payments
    direction: inbound
    route: POST /deposit-batches/expected
    permission: pay:deposit_batch:intake
    request: '{batch_ref: "dep:pos:<shop_id>:<bag_no>", kind: cash, bank_account_id, company_code, currency, amount (string, NUMERIC 18,2), expected_on (YYYY-MM-DD), clearing_purpose: cash_in_transit, source_module: pos}'
    request_schema:
      - field: batch_ref
        type: string
        required: true
      - field: kind
        type: string
        required: true
        values:
          - cash
      - field: bank_account_id
        type: string
        required: true
      - field: company_code
        type: company_code
        required: true
      - field: currency
        type: currency
        required: true
      - field: amount
        type: decimal
        required: true
      - field: expected_on
        type: date
        required: true
      - field: clearing_purpose
        type: string
        required: true
        values:
          - cash_in_transit
      - field: source_module
        type: string
        required: true
        values:
          - pos
    response: "201 {batch_id (= batch_ref), status: deposited, total}; 200 the same for the same batch_ref; 422 {gate: bank_account_receives | bounds, reason, fix}"
    rule: transition expect_deposit_batch (PAY-49); matched to the statement credit, posted bank / cash_in_transit (event deposit_match); a counted difference needs an approver (accept_deposit_difference, cash_over_short); pos never writes pay_ tables and payments never writes pos_ tables
    status: added 2026-10-25 (v6) for pos v1 POS-26
  - story: PAY-50
    with: pos
    owner: payments
    direction: inbound
    route: POST /card-settlements/expected
    permission: pay:card_settlement:intake
    request: '{settlement_ref: "card:pos:<shop_id>:<terminal_batch_ref>", provider, company_code, currency, gross (string, NUMERIC 18,2), business_date (YYYY-MM-DD), transaction_count (int), source_module: pos}'
    request_schema:
      - field: settlement_ref
        type: string
        required: true
      - field: provider
        type: string
        required: true
      - field: company_code
        type: company_code
        required: true
      - field: currency
        type: currency
        required: true
      - field: gross
        type: decimal
        required: true
      - field: business_date
        type: date
        required: true
      - field: transaction_count
        type: integer
        required: true
      - field: source_module
        type: string
        required: true
        values:
          - pos
    response: "201 {settlement_ref, status: expected}; 200 the same for the same settlement_ref; 422 {gate: provider_known | bounds, reason, fix}"
    rule: transition expect_card_settlement (PAY-50); the provider's credit settles one or more expected settlements whole; fees = gross - credit (card_fees, tax code E); card_clearing credited with the gross; fees above card_fee_max_pct wait for an approver; pos posted the gross to card_clearing at its day close
    status: added 2026-10-25 (v6) for pos v1 POS-26
  - story: PAY-51
    with: expenses
    owner: payments
    direction: inbound
    route: POST /payouts
    permission: pay:payout:intake
    request: "{payout_ref: \"exp:<report_id>:r<revision>:pay\", payee_bp_id (the employee's person partner), person_id, company_code, currency, amount (string, NUMERIC 18,2), due_on (YYYY-MM-DD), reference (on the bank line, at most 18 characters for Bacs), source_module: expenses, source_document: report_id, clearing_purpose: employee_payable}"
    request_schema:
      - field: payout_ref
        type: string
        required: true
      - field: payee_bp_id
        type: bp_id
        required: true
      - field: person_id
        type: person_id
        required: true
      - field: company_code
        type: company_code
        required: true
      - field: currency
        type: currency
        required: true
      - field: amount
        type: decimal
        required: true
      - field: due_on
        type: date
        required: true
      - field: reference
        type: string
        required: true
      - field: source_module
        type: string
        required: true
        values:
          - expenses
      - field: source_document
        type: string
        required: true
      - field: clearing_purpose
        type: string
        required: true
        values:
          - employee_payable
    response: "201 {payout_ref, payout_id, status: requested}; 200 the same for the same payout_ref (a different amount for a known payout_ref is refused 409); 422 {gate: payee_is_person | paying_account | bounds, reason, fix}"
    rule: transition request_payout (PAY-51); approved, exported in a payout run and confirmed paid by payments; the payee's bank account is read from hr by payments' client only, so expenses never sees it; posted employee_payable / bank (event payout)
    status: added 2026-10-25 (v6) for expenses v1 EXP-13
  - story: PAY-51
    with: expenses
    owner: payments
    direction: inbound
    route: POST /payouts/{payout_ref}/cancel
    permission: pay:payout:intake
    request: path payout_ref; body {reason}
    request_schema:
      - field: payout_ref
        type: string
        required: true
        in: path
      - field: reason
        type: string
        required: true
    response: "200 {payout_ref, status: cancelled}; a repeat answers the same; 409 {gate: not_exported, reason, fix} once the payout is in an exported run"
    rule: transition cancel_payout by system, only for a payout this caller requested; a reversed claim (expenses) cancels its payout before export, and after export waits for the payment and a recovery
    status: added 2026-10-25 (v6) for expenses v1
  - story: PAY-51
    with: payroll
    owner: payments
    direction: inbound
    route: POST /payouts/{payout_ref}/cancel
    permission: pay:payout:intake
    request: path payout_ref; body {reason}
    request_schema:
      - field: payout_ref
        type: string
        required: true
        in: path
      - field: reason
        type: string
        required: true
    response: "200 {payout_ref, status: cancelled}; a repeat answers the same; 409 {gate: not_exported, reason, fix} once the payout is in an exported run"
    rule: transition cancel_payout by system, only for a payout this caller requested; a reversed claim (expenses) cancels its payout before export, and after export waits for the payment and a recovery
    status: "payments v8 2026-10-26 (spec gaps wave 4): listed for payroll PRL-12"
  - story: PAY-51
    with: expenses
    owner: expenses
    direction: outbound
    route: POST /reports/{report_id}/paid
    permission: exp:report:mark_paid
    request: "{payout_ref, payment_id, paid_on (YYYY-MM-DD, the statement date), amount (string, NUMERIC 18,2)}"
    request_schema:
      - field: report_id
        type: string
        required: true
        in: path
      - field: payout_ref
        type: string
        required: true
      - field: payment_id
        type: payment_id
        required: true
      - field: paid_on
        type: date
        required: true
      - field: amount
        type: decimal
        required: true
    response: "200 the report, status paid; 200 the same for a repeated payout_ref; 409 {gate: amount_matches, reason, fix} for a different amount"
    rule: called after confirm_payout_paid commits, from the outbox, until it answers 2xx; source_module expenses names this route; payments never writes exp_ tables
    status: "added 2026-10-25 (v6): the caller side of expenses v1 EXP-13"
  - story: PAY-51
    with: hr
    owner: hr
    direction: outbound
    route: GET /persons/{person_id}/bank-account
    permission: hr:bank_account:read_for_payout
    request: path person_id; query on (YYYY-MM-DD, the payment date)
    request_schema:
      - field: person_id
        type: person_id
        required: true
        in: path
      - field: on
        type: date
        required: false
        in: query
    response: 200 {person_id, account_holder, sort_code | null, account_number | null, iban | null, bic | null, valid_from}; 404 when hr holds none on that date
    rule: read at export_payout_run only, written into the payment file object and nowhere else; every read audited by hr; until hr v2 serves it, export asks the cashier to type the account and a second person to confirm it (gate payee_bank_known)
    status: satisfied by hr v2 (HR-59, bank account for payout; payments' service client only, audited)
  - story: PAY-52
    with: payroll
    owner: payments
    direction: inbound
    route: POST /payouts
    permission: pay:payout:intake
    request: '{payout_ref: "prl:<pay_run_id>:<person_id>:net[:<n>]", batch_ref: pay_run_id, payee_bp_id, person_id, company_code, currency, amount (string, NUMERIC 18,2), due_on (the pay date), reference (at most 18 characters for Bacs), source_module: payroll, source_document: pay_run_id, clearing_purpose: net_pay_payable}'
    request_schema:
      - field: payout_ref
        type: string
        required: true
        pattern: "^prl:"
      - field: batch_ref
        type: string
        required: true
      - field: payee_bp_id
        type: bp_id
        required: true
      - field: person_id
        type: person_id
        required: true
      - field: company_code
        type: company_code
        required: true
      - field: currency
        type: currency
        required: true
      - field: amount
        type: decimal
        required: true
      - field: due_on
        type: date
        required: true
      - field: reference
        type: string
        required: true
      - field: source_module
        type: string
        required: true
        values:
          - payroll
      - field: source_document
        type: string
        required: true
      - field: clearing_purpose
        type: string
        required: true
        values:
          - net_pay_payable
    response: "201 {payout_ref, payout_id, status: requested | approved}; 200 the same for the same payout_ref (a different amount is refused 409); 422 {gate: payee_is_person | paying_account | bounds, reason, fix}"
    rule: transition request_payout, then approve_payout_batch (PAY-52); the batch is exported as one file per paying account and due date; posted net_pay_payable / bank (event payout); the bank details are read from hr by payments' client only
    status: added 2026-10-25 (v7) for payroll v1 PRL-12
  - story: PAY-52
    with: payroll
    owner: payroll
    direction: outbound
    route: POST /pay-runs/{pay_run_id}/payouts/{payout_ref}/paid
    permission: prl:payout:mark_paid
    request: "{status (paid | returned), payment_id, paid_on (YYYY-MM-DD), amount (string, NUMERIC 18,2), reason (for returned)}"
    request_schema:
      - field: pay_run_id
        type: string
        required: true
        in: path
      - field: payout_ref
        type: string
        required: true
        in: path
      - field: status
        type: string
        required: true
        values:
          - paid
          - returned
      - field: payment_id
        type: payment_id
        required: false
      - field: paid_on
        type: date
        required: true
      - field: amount
        type: decimal
        required: true
      - field: reason
        type: string
        required: false
    response: "200 the payslip; 200 the same for a repeat; 409 {gate: amount_matches, reason, fix}"
    rule: called after confirm_payout_paid or record_payout_return commits, from the outbox, until 2xx; source_module payroll names this route; payments never writes prl_ tables
    status: "added 2026-10-25 (v7): the caller side of payroll v1 PRL-28"
tool_names:
  - tool: load_payments_from_odoo
    route: POST /migration/odoo/run
    was: run_odoo_migration
  - tool: sign_payments_migration
    route: POST /migration/sign
    was: sign_migration
screens:
  - id: receipts
    name: Receipts
    stories:
      - PAY-01
      - PAY-02
      - PAY-03
      - PAY-04
      - PAY-05
      - PAY-06
      - PAY-07
      - PAY-14
      - PAY-16
      - PAY-17
      - PAY-28
    actors:
      - cashier
    note: a receipt shows its open suggestion beside it (accept, change, reject) and its readiness; a 'today's auto-matches' list when auto-with-review is on
  - id: refunds
    name: Refunds
    stories:
      - PAY-08
    actors:
      - cashier
      - approver
  - id: collections
    name: Collections
    stories:
      - PAY-09
      - PAY-10
      - PAY-11
    actors:
      - collector
  - id: credit
    name: Credit
    stories:
      - PAY-12
    actors:
      - approver
  - id: bank_reconciliation
    name: Bank reconciliation
    stories:
      - PAY-21
      - PAY-22
      - PAY-23
      - PAY-24
      - PAY-25
    actors:
      - cashier
      - approver
    note: "v3: per account and statement: every line with its state, the rule's or the AI's proposal (accept, change, reject), in-transit transfers, the balance checks with readiness; 'today's auto postings' for review; rules for the approver"
  - id: direct_debits
    name: Direct debits
    stories:
      - PAY-18
      - PAY-19
      - PAY-20
    actors:
      - cashier
      - approver
    note: "v3: mandates and their AUDDIS state; runs with their notices, file and approval; ARUDD and ADDACS imports"
  - id: cash_journal
    name: Cash journal
    stories:
      - PAY-26
    actors:
      - cashier
      - approver
    note: "v3: the day's entries, the count by denomination and the close with readiness"
  - id: cash_position
    name: Cash position
    stories:
      - PAY-29
    actors:
      - approver
    note: "v3: today's position and 13 weeks ahead per currency, each figure drilling to its documents"
  - id: settings
    name: Settings
    stories:
      - PAY-13
      - PAY-27
    actors:
      - approver
    note: "every config_row below: aging buckets and basis, reminder levels and mode, bank accounts, AI step modes; the controls (pay:settings:controls) in their own section with who changed each and when"
  - id: payouts
    name: Payouts
    stories:
      - PAY-51
      - PAY-52
    actors:
      - approver
      - cashier
    note: "v6: payouts requested by other modules, their approval, the payout runs and their files; the payee's bank account is never shown (masked: last four digits)"
  - id: migration
    name: Migration
    stories:
      - PAY-15
    actors:
      - approver
    note: "one-off: the migration.reconcile checks per customer and currency, Odoo vs ours vs difference, and the sign-off; read-only once signed"
permissions:
  namespace: pay
  catalog:
    - pay:receipt:read
    - pay:receipt:write
    - pay:receipt:import
    - pay:allocation:write
    - pay:refund:request
    - pay:refund:approve
    - pay:collection:write
    - pay:reminder:send
    - pay:credit:write
    - pay:settings:write
    - pay:settings:controls
    - pay:migration:sign
    - pay:collection_event:write
    - pay:statement:write
    - pay:receipt:ingest
    - pay:receipt:match
    - pay:collection:tick
    - pay:allocation:from_source
    - pay:migration:import
    - pay:statement_line:reconcile
    - pay:remittance:import
    - pay:mandate:write
    - pay:dd_run:write
    - pay:dd_run:approve
    - pay:cash_journal:write
    - pay:cash_journal:approve
    - pay:deposit_batch:write
    - pay:bank_account:write
    - pay:reconciliation_rule:write
    - pay:forecast:read
    - pay:statement:ingest
    - pay:statement_line:suggest
    - pay:dd:collect
    - pay:bank_account:read_unmasked
    - pay:payout:approve
    - pay:payout:export
    - pay:deposit_batch:intake
    - pay:card_settlement:intake
    - pay:payout:intake
  machine:
    - pay:receipt:ingest
    - pay:receipt:match
    - pay:collection:tick
    - pay:allocation:from_source
    - pay:migration:import
    - pay:statement:ingest
    - pay:statement_line:suggest
    - pay:dd:collect
    - pay:bank_account:read_unmasked
    - pay:deposit_batch:intake
    - pay:card_settlement:intake
    - pay:payout:intake
  composition_roles: "L2C leftover: the cashier holds ar:invoice:read; payments' service client holds ar:invoice:read, ar:invoice:settle_discount, ar:payment_request:settle, tax:cis:record_suffered, tax:document_tax:compute, tax:vat_return:read, purchasing:payment:read, purchasing:payment:write, purchasing:bill:read, gl:journal:post, gl:fx:read, gl:report:read; tax's and ar's service clients hold pay:receipt:read (v3); purchasing's service client holds pay:receipt:read and pay:bank_account:read_unmasked (v4)"
  decided_by: "permissions only, never a role's name: the brick is generic; a composition maps its own roles to these (the brick ships recommended defaults in roles.yaml)"
  row_visibility:
    rule: finance sees every receipt and balance; sales sees a customer's balance and hold through credit_status only
    enforced: credit_status is a separate narrow route, not a filter on the full one
    not_found_not_forbidden: true
config_rows:
  - table: pay_settings
    changes: how this business takes and chases money
    settings:
      - key: aging_buckets
        type: str
        default: 0-30,31-60,61-90,90+
        changes: the aging buckets of the overdue list, in days
      - key: age_from
        type: str
        default: due_date
        changes: "what age runs from: due_date or invoice_date"
      - key: match_tolerance
        type: str
        default: "0.00"
        changes: the difference (in the receipt's currency) a person may close as a write-off proposal (PAY-04); 0.00 = off; pay:settings:controls
      - key: credit_hold_days_overdue
        type: int
        default: 60
        changes: a customer is on hold once an invoice is this many days overdue (PAY-12); pay:settings:controls
      - key: credit_hold_over_limit
        type: bool
        default: true
        changes: whether exceeding the credit limit (pay_credit) also puts a customer on hold; pay:settings:controls
      - key: credit_hold_mode
        type: str
        default: warn
        changes: "warn | block: what sales-order does on confirm for a customer on hold (SO-22); pay:settings:controls"
      - key: statement_import_from
        type: str
        default: ""
        changes: "a date (YYYY-MM-DD): statement lines booked before it are skipped as already migrated (the Odoo cut-over); empty = import all; pay:settings:controls"
      - key: reminder_mode
        type: str
        default: suggest
        changes: "off | suggest | auto: reminders overall; suggest drafts them for the collector to send"
      - key: transfer_window_days
        type: int
        default: 5
        changes: "v3: how many days apart the two sides of a transfer between our accounts may be booked (PAY-25)"
      - key: cash_difference_threshold
        type: str
        default: "5.00"
        changes: "v3: a cash-count difference above this needs an approver who did not count (PAY-26); pay:settings:controls"
      - key: dd_notice_days
        type: int
        default: 10
        changes: "v3: working days of advance notice before a direct debit (the Direct Debit Guarantee's default; 3 where the DDI terms agree it); pay:settings:controls"
      - key: forecast_weeks
        type: int
        default: 13
        changes: "v3: how far the cash forecast looks (PAY-29)"
      - key: card_fee_max_pct
        type: str
        default: "3.00"
        changes: "v6: the card fees, as a percent of the gross, a provider's credit may imply and still be matched without an approver (PAY-50); pay:settings:controls"
      - key: payout_file_format
        type: str
        default: pain.001
        changes: "v6: pain.001 | bacs18, the payout run's file (PAY-51)"
      - key: payout_auto_approve_sources
        type: json
        default: "[]"
        changes: 'v7: source modules whose own approval payments trusts for their payout batches (e.g. ["payroll"]); pay:settings:controls'
  - table: pay_settings
    changes: "each AI step's mode (ai_steps): ai_suggest_match off | suggest | auto-with-review (exact matches only, opt-in; auto-with-review needs pay:settings:controls), ai_draft_reminder off | suggest | auto, late_payment_risk off | suggest"
  - table: pay_reminder_level
    changes: "the follow-up levels: days after due, wording, channel, mode per level; a change applies from the next daily pass, reminders already sent are unchanged"
  - table: pay_credit
    changes: "per customer: a credit limit from a date after today (a new dated row, never an edit)"
  - table: pay_credit_override
    changes: "per customer: a manual hold or early release with a reason and an end date"
  - table: pay_bank_account
    changes: the accounts we receive into and pay from, per company, with their ledger account and purposes (v3)
  - table: pay_house_bank
    changes: "v3: the banks we hold accounts with, their SUN and SEPA creditor id"
  - table: pay_default_bank_account
    changes: "v3: the receiving account per company and currency printed on invoices, from a date after today"
  - table: pay_reconciliation_rule
    changes: "v3: how recurring statement lines are treated (account, tax code, suggest or auto), from a date after today"
  - table: pay_cash_journal
    changes: "v3: the tills and petty-cash boxes, their currency and ledger account"
  - table: pay_settings
    changes: "v3: ai_suggest_statement_line off | suggest (the AI never posts)"
in_code_not_rows:
  - that outstanding is total - SUM(allocations), never a stored flag and never incremented
  - that an allocation never exceeds its source's remainder or the invoice's outstanding
  - that an undo is a reversing row; allocations are never deleted
  - that a refund needs an approver who is not the requester
  - that a credit is never applied across currencies
  - that a re-imported statement line creates nothing
  - that 'today' is the date in business_timezone; an allocation, an undo, a credit limit or an override is never dated before today; a receipt, a return or a refund payment is never dated after today
  - that a correction is a reversing row or a new row; no pay_ row is edited or deleted after it is committed (only a suggestion's status and a refund's status move forward)
  - that only pay:settings:controls changes match_tolerance, credit_hold_*, statement_import_from or ai_suggest_match auto-with-review; pay:settings:write is refused on them
  - that an AI suggestion never becomes an allocation without a person (decide_match), except an exact match in auto-with-review, which is recorded as auto and listed for review
  - that only pay:migration:import writes allocations, limits or overrides dated before today, each with origin odoo:<model>:<id>
  - that auto-with-review allocates only an EXACT match (the amount equals the named invoices' outstanding and the reference names them); anything else stays a suggestion
  - "v3: that every statement line is kept and ends matched or posted; a statement balances and follows the previous one"
  - "v3: that a rule never allocates to an invoice and auto rules need the owner's control"
  - "v3: Bacs Standard 18 and pain.008 layouts, AUDDIS, ARUDD and ADDACS codes; that a submitted run is never rebuilt"
  - "v3: that a cash day closes once and a closed day refuses entries"
  - "v3: that the realised FX difference is (receipt rate - invoice rate) x the allocated amount, posted once per allocation"
ai_steps:
  - key: ai_suggest_match
    benefit: the commonest finance chore, ticking receipts off against invoices, becomes a confirm click; the evidence is shown
    mode: suggest
    modes_allowed:
      - off
      - suggest
      - auto-with-review
    safe_for_auto: auto-with-review only for an EXACT match (amount equals the invoices' outstanding and the reference names them); a person reviews the day's auto-allocations; never auto for partial or ambiguous matches
    inputs:
      - the receipt amount
      - currency
      - payer name and reference text
      - the customer's open invoices
    output: a proposed allocation, the evidence (which reference text, which sum) and a confidence
    fallback: the receipt stays unallocated for the cashier
    eval: a year of receipts with the allocations a person made; exact-match precision (target 100%), top-1 accuracy on the rest
  - key: ai_draft_reminder
    benefit: a reminder in the tone this customer responds to, naming the right invoices and any promise made, instead of a form letter
    mode: suggest
    modes_allowed:
      - off
      - suggest
      - auto
    safe_for_auto: auto only for level 1 with the business's own approved template; later levels and any customer with a dispute stay suggest
    inputs:
      - the overdue invoices
      - the customer's history of promises and payments
      - the level's template
    output: a reminder draft and the invoices it names
    fallback: the level's template, filled in
    eval: reminders a collector edited before sending; edit distance and factual accuracy (amounts, invoice numbers)
  - key: ai_suggest_statement_line
    benefit: the lines no rule covers (a card purchase, a one-off fee, an unknown credit) get a proposed match or account with the evidence, so reconciliation is confirm clicks
    mode: suggest
    modes_allowed:
      - off
      - suggest
    safe_for_auto: false
    inputs:
      - the line's amount
      - date
      - counterparty and text
      - open receipts
      - supplier payments
      - transfers in transit
      - past postings of similar lines
    output: a proposed match or account and tax code, the evidence and a confidence
    fallback: the line stays open for the cashier
    accepted_by: "the cashier on Bank reconciliation (PAY-22): accept, change or reject; the decision is kept on the line"
    consent: pay_settings ai_suggest_statement_line; the line text and amounts go to a sovereign model, no customer data beyond the counterparty name
    audit: "pay_line_suggestion: model, evidence, confidence, decision, who, when"
    eval: a year of reconciled lines with the person's decisions; top-1 accuracy, and zero tolerance for proposing an invoice allocation
  - key: late_payment_risk
    benefit: the collector calls the customers likely to pay late first
    mode: suggest
    modes_allowed:
      - off
      - suggest
    safe_for_auto: false
    inputs:
      - the customer's payment history
      - current overdue
      - invoice size
    output: a risk score and the two reasons that drive it, shown on the overdue list
    fallback: the list sorts by age and amount
    eval: "backtest on past invoices: did the high-risk decile pay late"
    note: a ranking on the Collections screen, not a step in the signed lifecycle
tests:
  acceptance: one per story id above, named for it
  access:
    - a cashier records and allocates; cannot approve a refund
    - a requester cannot approve their own refund
    - "sales reads credit_status only: balance, hold, reason; never receipts"
    - a cashier holding pay:settings:write cannot change match_tolerance, a credit_hold_* setting, statement_import_from or turn on auto-with-review (pay:settings:controls)
    - no wildcard grant (pay:*:*) reaches a machine permission; a person's token holding pay:migration:import by role is refused
    - "v3: the preparer of a direct-debit run cannot approve it, and the person who counted cash cannot approve its difference (email AND sub)"
    - "v3: a reconciliation rule in auto mode is refused to pay:reconciliation_rule:write without pay:settings:controls"
    - "v4: GET /bank-accounts/{id}/account-details answers only purchasing's machine identity; a person token (any role, even with pay:*:*) is 403; every answered read leaves one pay_bank_account_read row"
  invariants:
    - outstanding(invoice) = total - SUM(allocations); no stored paid flag exists anywhere
    - an allocation never exceeds its source's remainder or the invoice's outstanding
    - the sum of a receipt's allocations never exceeds the receipt
    - re-importing the same statement creates no duplicate receipts
    - a reversed receipt's allocations net to zero
    - a reminder for a given invoice and level is sent once
    - amounts are never summed or applied across currencies
    - no allocation, undo, credit limit or override is dated before today (business_timezone) outside the migration; no receipt, return or refund payment is dated after today
    - a statement as at a past date re-computes to the closing balance recorded when it was sent
    - every allocation made from a suggestion names it and the person (or auto) that decided; a rejected suggestion leaves no allocation
    - "after the migration, per customer and currency: invoices - allocations - unallocated credit = Odoo's receivable residual = ar's open balance"
    - "v3: a statement's opening + lines = closing; a reconciled statement's lines are each matched or posted exactly once; no counterpart is matched to two lines"
    - "v3: a settlement discount is allocated only when received_on is within the invoice's window and equals the terms' percentage of the gross exactly"
    - "v3: a direct-debit collection becomes one receipt; an ARUDD return reverses it once; a cancelled or dormant mandate is never collected"
    - "v3: the realised FX difference posted for an allocation and its undo net to zero; none is posted for a same-rate allocation"
    - "v3: a cash day's expected balance = opening + receipts - expenses - deposits; the difference posted equals counted - expected"
    - "v3: a deposit batch's statement line is never also a receipt; its cheques are"
    - "v3: the forecast never sums across currencies; each figure drills to documents"
    - "v3: concurrent allocations of one receipt never exceed it (re-derived with SUM in a second transaction; the loser is refused 409 with the figures)"
    - "v3: 'today' is business_timezone's date for a cash day, a run's entry day and every 'after today' gate, never UTC"
    - "v3: an amount outside NUMERIC(18,2) on any new route is refused 422"
  scenarios:
    - id: SC-PAY-01
      name: one transfer, three invoices
      stories:
        - PAY-01
        - PAY-32
        - PAY-03
        - PAY-30
      given: invoices of 4,000.00, 3,500.00 and 3,000.00; a receipt of 10,000.00 with reference naming the first two
      when: the match is suggested, confirmed, and the remainder allocated to the third
      then: the first two paid, the third with 500.00 outstanding, 0.00 unallocated
    - id: SC-PAY-02
      name: the statement imported twice
      stories:
        - PAY-02
      given: a camt.053 file of 42 credit lines
      when: it is imported twice
      then: 42 receipts, not 84
    - id: SC-PAY-03
      name: bounced payment
      stories:
        - PAY-07
        - PAY-30
      given: a receipt of 1,200.00 allocated to two invoices
      when: it is reversed
      then: both invoices outstanding again; the receipt reversed; allocations net to 0.00
    - id: SC-PAY-04
      name: overpayment refunded
      stories:
        - PAY-05
        - PAY-08
      given: a receipt of 1,300.00 against an invoice of 1,000.00
      when: 300.00 is requested back, approved by another person, and paid
      then: customer credit 0.00; the same person approving their own request is refused
    - id: SC-PAY-05
      name: reminders with a promise
      stories:
        - PAY-31
        - PAY-10
      given: an invoice 7 days overdue, levels 7/14/30, mode suggest
      when: the daily pass runs twice, then the customer promises payment by day 20
      then: one level-1 draft; no level-2 on day 14 (suppressed by the promise); level 2 on day 21 if still unpaid
    - id: SC-PAY-06
      name: credit hold reaches sales-order
      stories:
        - PAY-12
        - PAY-40
      given: a hold rule of 60 days overdue
      when: an invoice reaches 61 days
      then: credit_status says hold with the reason; sales-order refuses (or warns) on confirm
    - id: SC-PAY-07
      name: no back-dating, corrections are reversals
      stories:
        - PAY-01
        - PAY-06
        - PAY-11
        - PAY-12
      given: a statement sent as at 2026-11-30 with a closing balance of 4,500.00, today 2026-12-03 in Europe/London
      when: a receipt dated 2026-12-04, an allocation dated 2026-11-29 and a credit limit from today are tried; then a wrong allocation is undone
      then: the three are refused with their readiness reasons; the undo is a reversing row dated 2026-12-03; the statement as at 2026-11-30 still re-computes to 4,500.00
    - id: SC-PAY-08
      name: the AI suggests, the cashier decides
      stories:
        - PAY-32
        - PAY-14
        - PAY-13
      given: mode suggest; three receipts with suggestions
      when: one is accepted, one changed, one rejected; the cashier then tries to switch on auto-with-review
      then: two allocations with decided_how accepted / changed citing their suggestions, one receipt unallocated; the switch is refused, the owner's is accepted and recorded
    - id: SC-PAY-10
      name: a whole statement reconciled
      stories:
        - PAY-21
        - PAY-22
        - PAY-23
        - PAY-24
        - PAY-25
        - PAY-34
        - PAY-48
      given: a November statement with customer credits, supplier payments, a transfer out, an HMRC debit, charges, interest and a paying-in
      when: it is imported twice and reconciled with rules, one AI suggestion accepted and one rejected then posted by hand
      then: one statement; every line matched or posted; the transfer in transit until the other account's statement; book equals bank; the rejected suggestion left no posting
    - id: SC-PAY-11
      name: direct-debit cycle
      stories:
        - PAY-18
        - PAY-19
        - PAY-20
      given: three mandates lodged, invoices due, a 10-day notice
      when: a run is prepared, approved by the preparer (refused) then another approver, submitted, collected twice and an ARUDD return applied
      then: three receipts allocated once; the returned one reversed with its reason; the reason-1 mandate cancelled
    - id: SC-PAY-12
      name: early settlement and CIS on receipts
      stories:
        - PAY-16
        - PAY-45
      given: an invoice with 2/10 terms and a contractor customer's net payment
      when: 1,176.00 arrives in the window and 1,600.00 with a 400.00 deduction statement
      then: the discount credit allocated and the invoice paid; the CIS deduction allocated after tax accepted it
    - id: SC-PAY-13
      name: foreign-currency receipt with a realised gain
      stories:
        - PAY-33
      given: a EUR invoice at 0.8500 and a EUR receipt at 0.8600
      when: it is allocated, unallocated and allocated again
      then: fx 10.00 gain, its reversal, then 10.00 again; the receivable in GBP is zero at the end
    - id: SC-PAY-14
      name: petty cash and cheques
      stories:
        - PAY-26
        - PAY-28
      given: a cash journal and four cheques
      when: the day is closed 1.50 short and the cheques are paid in and matched
      then: the shortage posted after another person approves it (above the threshold, or not); the batch matched to one line; a bounced cheque reverses only itself
    - id: SC-PAY-09
      name: Odoo migration reconciles to zero unexplained
      stories:
        - PAY-44
        - PAY-15
      given: an Odoo database with posted customer payments, partial and full reconciliations, two unreconciled bank lines, one customer refund and one cross-currency payment; ar migrated first
      when: the migration runs twice into a test copy and the reconciliation is opened
      then: every 'equal' check in migration.reconcile is equal per customer and currency; the cross-currency payment is the one explained difference; the second run added no rows; the owner signs
scale:
  reads: the tables reader serves 16 concurrent reads; the overdue list is one aggregate over allocations joined to invoice totals read per page
  hot_paths:
    - settlement for an invoice list page
    - the overdue list with aging
    - match suggestion over a customer's open invoices
  workers: the daily reminder pass (PAY-31), on the composition's one clock worker
  volumes: tens of thousands of receipts a year is comfortable; above that, keep a per-invoice settled amount RE-DERIVED with SUM() in a second committed transaction after each allocation, never incremented, and reconcile it nightly
platform_constraints:
  - constraint: No read-your-writes inside an open transaction.
    today: allocate, commit, then re-derive the outstanding with SUM() in a second transaction before refusing or accepting the next allocation
  - constraint: A bare INSERT of an already-committed key is rejected (23505); it is not an upsert.
    today: the statement import claims each payment_id with a plain INSERT and treats 23505 as already imported
customer_varies:
  - question: "v6: Who releases a reimbursement or other payout to a person, and how is it paid?"
    changes: PAY-51 (approve_payout, payout_file_format)
    default: a payments approver approves every payout (never the payee nor the claim's approver) and the cashier exports a pain.001 run; the standard default, chosen 2026-10-25
    typical:
      small_business: the owner approves, a weekly run
      larger_business: finance approves within the claim's own approval, a payroll-cycle run
  - question: "v6: How much may a card provider keep in fees before someone looks?"
    changes: card_fee_max_pct (PAY-50)
    default: 3.00 percent of the gross; the standard default, chosen 2026-10-25
  - question: How do customers pay you (bank transfer, card, direct debit, cash)?
    changes: pay_bank_account, the provider capabilities composed (PAY-43)
    typical:
      b2b_uk: bank transfer, reference = invoice number
      subscriptions: direct debit or card through a provider
  - question: Can you download your bank statement as a file?
    changes: PAY-02 (camt.053 or CSV)
    standard: ISO 20022 camt.053 BankToCustomerStatement, the statement message most UK and EU business banks export
  - question: Do customers pay several invoices with one transfer, or short-pay for bank charges?
    changes: allocation (PAY-03) and the tolerance (PAY-04)
  - question: Do you want reminders sent automatically, drafted for you, or do you prefer to chase by hand?
    changes: pay_settings reminder mode, pay_reminder_level
    typical:
      saas: automatic at 7, 14 and 30 days overdue, then the service pauses
      professional_services: nothing automatic, a partner calls before anything is sent
      smaller_firms: a statement once a month rather than per-invoice chasing
  - question: Should an overdue customer be stopped from ordering?
    changes: pay_settings hold rule and mode, pay_credit limits
    standard: "UK Late Payment of Commercial Debts (Interest) Act 1998: statutory interest and fixed compensation are available on late B2B payments"
  - question: Who may approve a refund?
    changes: the approver role
  - question: Which banks and accounts do you hold, for which company?
    changes: pay_house_bank, pay_bank_account, pay_default_bank_account (v3)
  - question: Do you collect by Direct Debit yourselves (a SUN) or through a bureau?
    changes: house bank SUN, dd_notice_days; a bureau is a provider capability on PAY-43 (v3)
    standard: "Bacs Direct Debit scheme rules: advance notice (10 working days unless agreed), AUDDIS lodgement, the Direct Debit Guarantee"
  - question: Do you keep petty cash or a till?
    changes: pay_cash_journal, cash_difference_threshold (v3)
  - question: Which statement lines recur every month (charges, interest, standing orders)?
    changes: pay_reconciliation_rule (v3)
variation_points:
  - node: Task_ai_suggest_match
    op: row_value
    default: suggest
    depth: 1
    guideline: warn - auto-with-review is safe only for exact matches; a wrong allocation tells a customer they owe money they paid
  - node: Task_ai_draft_reminder
    op: row_value
    default: suggest
    depth: 1
    guideline: warn - with reminders off, chasing depends on someone remembering (typical SaaS chases at 7/14/30)
  - node: Task_approve_refund
    op: narrow
    default: an approver who is not the requester
    depth: 1
    requirement: refuse - money out needs a second person; the AI-step contract forbids money-moving without approval
  - node: Task_decide_match
    op: narrow
    default: a person accepts, changes or rejects each suggestion
    depth: 1
    requirement: refuse - an AI step that allocates money without a person is allowed only as auto-with-review on exact matches, opted in under pay:settings:controls
  - node: Task_approve_dd_run
    op: narrow
    default: an approver who is not the preparer
    depth: 1
    requirement: refuse - a run takes money from customers' accounts; a wrong one costs indemnity claims
  - node: Task_suggest_statement_match
    op: on_off
    default: on, suggest
    depth: 1
    guideline: warn - without it every line no rule covers is investigated by hand
  - node: Task_apply_reconciliation_rule
    op: row_value
    default: suggest
    depth: 1
    guideline: warn - auto posts without a person; only for lines that never vary (a fixed monthly fee)
  - node: Task_settle_difference
    op: on_off
    default: off (tolerance 0.00)
    depth: 1
    guideline: warn - a tolerance above a few pounds hides real short payments
migration:
  odoo:
    order_note: "payment terms (resolved 2026-10-22, shared.yaml conventions.odoo_migration.payment_terms): sales-order writes ar's own key odoo:<payment_term id>; there is no ar.terms slice, and this slice reads terms only through ar's key"
    reads:
      - model: account.journal
        fields:
          - id
          - code
          - name
          - type
          - currency_id
          - bank_account_id.acc_number
          - default_account_id.code
        filter: type in ('bank', 'cash')
        why: the accounts customers pay into become pay_bank_account; their ledger account maps to gl
      - model: account.payment
        fields:
          - id
          - name
          - date
          - amount
          - currency_id
          - partner_id.commercial_partner_id
          - journal_id
          - payment_type
          - partner_type
          - state
          - ref
          - payment_method_line_id.code
          - move_id
          - is_reconciled
        filter: "partner_type = 'customer' and state = 'posted' (Odoo 17: state; earlier: move_id.state = 'posted')"
        why: inbound = receipts; outbound = customer refunds already paid
      - model: account.bank.statement.line
        fields:
          - id
          - date
          - amount
          - currency_id
          - payment_ref
          - partner_id.commercial_partner_id
          - journal_id
          - unique_import_id
          - is_reconciled
          - amount_residual
          - move_id
        filter: journal_id.type = 'bank' and amount > 0 and move_id.state = 'posted'
        why: credits reconciled straight to an invoice (no account.payment) are receipts too; unreconciled credits are money not yet matched
      - model: account.move.line
        fields:
          - id
          - move_id
          - move_id.move_type
          - move_id.name
          - partner_id.commercial_partner_id
          - account_id.account_type
          - date
          - date_maturity
          - debit
          - credit
          - amount_currency
          - currency_id
          - amount_residual
          - amount_residual_currency
          - reconciled
          - full_reconcile_id
        filter: account_id.account_type = 'asset_receivable' and parent_state = 'posted'
        why: the receivable lines both sides of every reconciliation sit on, and Odoo's own residual for the reconcile checks
      - model: account.partial.reconcile
        fields:
          - id
          - debit_move_id
          - credit_move_id
          - amount
          - debit_amount_currency
          - credit_amount_currency
          - debit_currency_id
          - credit_currency_id
          - max_date
          - full_reconcile_id
          - exchange_move_id
        filter: debit_move_id and credit_move_id both in the receivable lines read above
        why: "what settled what: each partial is one allocation"
      - model: account.full.reconcile
        fields:
          - id
          - partial_reconcile_ids
          - exchange_move_id
        filter: linked to a partial read above
        why: a check only (a fully reconciled set nets to zero); settlement is derived here, not stored
      - model: res.partner
        fields:
          - id
          - commercial_partner_id
          - credit_limit
          - use_partner_credit_limit
        filter: customer_rank > 0 and use_partner_credit_limit
        why: credit limits become pay_credit
      - model: account.bank.statement.line
        fields:
          - id
          - date
          - amount
          - currency_id
          - payment_ref
          - partner_id
          - journal_id
          - unique_import_id
          - is_reconciled
        filter: journal_id.type = 'bank' and not is_reconciled and date <= the cut-over (v3)
        why: unreconciled lines of either sign at the cut-over become open statement lines, so reconciliation continues
      - model: account.journal
        fields:
          - id
          - code
          - type
          - default_account_id.code
        filter: type = 'cash' (v3)
        why: cash journals
      - model: account.reconcile.model
        fields:
          - id
          - name
          - rule_type
          - match_label
          - match_label_param
          - match_amount
          - match_amount_min
          - match_amount_max
          - line_ids.account_id
          - line_ids.tax_ids
        filter: rule_type = 'writeoff_button' or 'writeoff_suggestion' (v3)
        why: Odoo's reconciliation models become rules in suggest mode
      - model: sdd.mandate
        fields:
          - id
          - name
          - partner_id
          - partner_bank_id
          - state
          - start_date
          - end_date
        filter: Odoo Enterprise account_sepa_direct_debit, where installed (v3)
        why: SEPA mandates; Bacs mandates are held by the bureau and entered from its export
    maps:
      - from: account.journal.code
        to: bank_account.bank_account_id
        rule: the journal code as the business's own code; currency_id or the company currency; acc_number masked to the last 4; default_account_id.code -> gl_account_id
      - from: account.payment (inbound)
        to: receipt
        rule: payment_id = 'odoo:account.payment:<id>'; received_on = date; amount, currency; bp_id from commercial_partner_id via business-partner's migration map; bank_reference = ref; method from payment_method_line_id.code; origin = the same key; status received
      - from: account.bank.statement.line (no account.payment behind it)
        to: receipt
        rule: payment_id = 'odoo:account.bank.statement.line:<id>'; received_on = date; remittance_text = payment_ref; bank_reference = unique_import_id; bp_id when partner_id is set, else the door decides
      - from: account.partial.reconcile
        to: allocation
        rule: "invoice side = the debit line's move (out_invoice) -> invoice_id = ar's migrated id for move_id.name; source side = the credit line's move: an account.payment or statement line -> source_kind receipt, out_refund -> credit_note (ar's id), an entry move -> writeoff (ar's migrated write-off); amount = debit_amount_currency (the invoice's currency); allocated_on = max_date; seq = rank of the partial id within that (source, invoice) pair; origin 'odoo:account.partial.reconcile:<id>'; decided_how manual"
      - from: account.payment (outbound, customer)
        to: refund
        rule: refund_id = 'odoo:account.payment:<id>'; status paid; paid_on = date; bank_ref = ref; requested_by and approved_by 'odoo' (the history did not record a second person); its partials -> allocation source_kind refund
      - from: res.partner.credit_limit
        to: credit_policy
        rule: valid_from = the cut-over date; currency = the company currency; origin 'odoo:res.partner:<id>'; 0 or unset -> no row
      - from: account.partial.reconcile.exchange_move_id
        to: (gl)
        rule: the realised FX difference is the ledger's journal (gl migration); the allocation carries the invoice-currency amount only
    door:
      - "the cut-over date: the last day Odoo's bank statements are migrated; it becomes statement_import_from (the next day), so a camt.053 that overlaps the cut-over skips the migrated lines"
      - "unreconciled bank credits: carry them as unallocated receipts (on the customer when Odoo names one, else on a suspense customer the cashier clears), or leave them in Odoo and import their statement again"
      - "cross-currency payments (a EUR payment settling a GBP invoice): carry the allocation in the invoice's currency with the receipt's fx_rate and list each as an explained difference, or settle them in Odoo before the cut-over"
      - "history depth: every posted payment, or only those touching an invoice still open at the cut-over plus the last N months (open balances reconcile either way)"
      - "follow-up levels (Enterprise account_followup): carry the level each open invoice has reached as a collection_event so reminders do not restart at level 1, or start every invoice at level 1"
      - "v3: the first statement this module reconciles per account (its opening must equal Odoo's last reconciled balance), and the reconciliation models carried as suggest-mode rules or dropped"
      - "v3: direct-debit mandates: carried from Odoo or the bureau's export, keeping each mandate's reference so Bacs sees no new instruction"
    reconcile:
      - check: open balance per customer and currency
        odoo: SUM(amount_residual_currency) of posted receivable account.move.line per commercial partner and currency at the cut-over
        ours: SUM(invoice totals) - SUM(allocations to them) - unallocated receipts - unallocated credit notes, per bp_id and currency (open_items as at the cut-over)
        must: equal
      - check: the same open balance as ar's migration
        odoo: ar's reconcile figure for the customer
        ours: this module's open balance above
        must: equal
      - check: outstanding per invoice
        odoo: account.move.amount_residual (out_invoice, posted)
        ours: settlement(invoice_id).outstanding
        must: equal
      - check: unallocated per receipt
        odoo: -amount_residual_currency of the payment's (or statement line's) receivable line
        ours: receipt amount - SUM(its allocations)
        must: equal
      - check: receipts per bank account and month
        odoo: count and SUM(amount) of posted inbound customer account.payment plus unmatched statement credits, by journal and month of date
        ours: count and SUM(amount) of pay_receipt by bank_account_id and month of received_on
        must: equal
      - check: allocated total
        odoo: SUM(debit_amount_currency) of the partials read
        ours: SUM(pay_allocation.amount) with origin odoo:account.partial.reconcile
        must: equal
      - check: paid invoices
        odoo: count of out_invoice with payment_state in ('paid', 'in_payment')
        ours: count of invoices with outstanding 0
        must: explained
      - check: refunds paid
        odoo: count and SUM(amount) of posted outbound customer account.payment
        ours: count and SUM(amount) of pay_refund status paid
        must: equal
      - check: unreconciled statement lines per bank account (v3)
        odoo: count and SUM(amount) of account.bank.statement.line not is_reconciled at the cut-over, by journal
        ours: count and SUM(amount) of open pay_statement_line by bank_account_id
        must: equal
    not_carried:
      - "draft and cancelled payments: they moved no money"
      - "Odoo's stored payment_state on invoices and the full-reconcile rows: settlement is derived here from allocations, so a stored copy would be a second answer"
      - "exchange-difference journal entries: they are the ledger's (gl migration); allocations carry the invoice-currency amount"
      - "v3: reconciled bank statement lines and their postings: history in the ledger's migration; only unreconciled lines at the cut-over come over"
      - "payment.transaction / acquirer records: no provider capability yet (PAY-43); the receipts they produced are carried"
      - "supplier payments (partner_type supplier): purchasing (M3)"
      - "follow-up e-mails and chatter: the level reached is carried if the door says so, the messages stay in Odoo's archive"
    idempotent_on: receipt payment_id / refund_id = 'odoo:<model>:<id>'; allocation (source_kind, source_id, invoice_id, seq) with seq from the Odoo partial id's rank in its pair; bank_account_id = journal code; credit_policy (bp_id, valid_from = cut-over)
reference:
  repos:
    - name: erp-brick-payments
      role: brick
      https: https://git.dodil.io/dodil/erp-brick-payments.git
      ssh: git@git.dodil.io:dodil/erp-brick-payments.git
      tag: v0.2.2
      read_first:
        - README.md
        - AGENTS.md
      clone: git clone --depth 1 --branch v0.2.2 https://git.dodil.io/dodil/erp-brick-payments.git
    - name: brick-business-partner
      role: brick
      https: https://git.dodil.io/dodil/erp-brick-business-partner.git
      ssh: git@git.dodil.io:dodil/erp-brick-business-partner.git
      tag: v0.4.0
      read_first:
        - README.md
        - AGENTS.md
      clone: git clone --depth 1 --branch v0.4.0 https://git.dodil.io/dodil/erp-brick-business-partner.git
    - name: erp-platform
      role: platform
      https: https://git.dodil.io/dodil/erp-platform.git
      ssh: git@git.dodil.io:dodil/erp-platform.git
      tag: v0.9.13
      read_first:
        - README.md
        - AGENTS.md
      clone: git clone --depth 1 --branch v0.9.13 https://git.dodil.io/dodil/erp-platform.git
  packages: []
  contracts: []
  build_guide: null
  known_gaps:
    - "no payment providers yet (card, direct debit, open banking): PAY-43 is the seam they plug into"
    - v3 designs bank reconciliation of every line, house banks, cash journals, deposit batches, remittance files, Bacs Direct Debit, the forecast and the FX posting; erp-brick-payments implements v2 and none of these yet
    - a bank feed and a direct-debit bureau are provider capabilities (PAY-35, PAY-43); none is built
    - "no supplier payments: money out to suppliers belongs to purchasing (M3); only customer refunds are here"
    - statutory late-payment interest is invoicing's (ar AR-39), not here
    - the AI-step marker in the BPMN is a task name until erp-platform draws dodil:ai / dodil:mode (M0 AI-step contract)
sap_parity:
  - sap: Incoming payment with clearing (F-28)
    ours: covered
    where: PAY-01, PAY-03, receipt, allocation
  - sap: Payment on account / unapplied cash
    ours: covered
    where: PAY-05
  - sap: Residual and partial payments
    ours: covered
    where: PAY-03, PAY-30, allocation
    why: an allocation reduces the invoice's outstanding; no residual item document
  - sap: Payment differences within tolerance (OBA3 tolerance groups, reason codes)
    ours: covered
    where: PAY-04, settings
  - sap: Reset cleared items (FBRA)
    ours: covered
    where: PAY-06
  - sap: Returned payments / direct debit returns
    ours: covered
    where: PAY-07, PAY-20
  - sap: Customer refunds (outgoing payment to a customer)
    ours: covered
    where: PAY-08, refund
  - sap: Electronic bank statement import (FF_5 / FEBAN, camt.053, MT940)
    ours: covered
    where: PAY-02, bank_statement
    why: camt.053, MT940 and CSV (v3 adds MT940)
  - sap: Automatic clearing and search-string matching of bank lines (F.13, FEBAN interpretation algorithms)
    ours: different
    where: PAY-32, PAY-14, PAY-22, PAY-23, match_suggestion, reconciliation_rule
    why: rules and the AI suggest, a person decides; auto only for exact matches or owner-controlled rules, reviewed the same day
  - sap: Payment advice notes (avis, FDA)
    ours: covered
    where: PAY-17, remittance
    why: remittance files (CSV, camt.054) propose the allocation; deductions become disputes
  - sap: Lockbox processing (FLB2)
    ours: covered
    where: PAY-17 (BAI2 lockbox), remittance
    why: a lockbox file records its receipts and their allocation proposals; UK customers mostly pay by transfer, read from the statement
  - sap: Cash discount taken on payment (SKTO)
    ours: covered
    where: PAY-16, ar AR-38, tax TAX-47
    why: taken when received within the invoice's window; the VAT corrected by invoicing's credit at the invoice's rates
  - sap: Realised exchange differences on clearing (OBA1 KDF)
    ours: covered
    where: PAY-33, PAY-42
    why: posted at allocation from the invoice's frozen rate and the receipt's rate; reversed with an undo
  - sap: House banks and bank accounts (FI12)
    ours: covered
    where: PAY-27, house_bank, bank_account, default_bank_account
    why: several accounts per company, purposes, the default receiving account per company and currency
  - sap: Dunning run (F150) with dunning levels and letters
    ours: covered
    where: PAY-31, PAY-09, reminder_level, collection_event
    why: dunning stays here; invoicing raises the interest note (ar AR-39)
  - sap: "Collections management: promises to pay, disputes (FSCM-COL, FSCM-DM)"
    ours: covered
    where: PAY-10, collection_event
  - sap: "Credit management: limits, blocks, release (FSCM-CR, UKM_BP)"
    ours: covered
    where: PAY-12, PAY-40, credit_policy, credit_override
  - sap: Customer statements (F.27)
    ours: covered
    where: PAY-11, statement
  - sap: Posting incoming payments to the ledger (bank / clearing accounts)
    ours: covered
    where: PAY-42
  - sap: Withholding tax deducted by the customer (incl. UK CIS suffered)
    ours: covered
    where: PAY-45, tax TAX-41
    why: allocated as source_kind cis_deduction once tax records it
  - sap: Down payment received against a request (F-29)
    ours: covered
    where: PAY-36, ar AR-35, AR-36
    why: a receipt settles the request into a deposit invoice and pays it
  - sap: Outgoing payment run to suppliers (F110)
    ours: different
    where: PAY-48
    why: lives in purchasing (PUR-19, PUR-15, PUR-29, PUR-16); the statement here confirms what the bank paid
  - sap: "Payment medium formats (DMEE: SEPA pain.001/pain.008, UK BACS Standard 18)"
    ours: covered
    where: PAY-02, PAY-19, purchasing PUR-59
    why: inbound camt.053 / MT940 here; Bacs Standard 18 and pain.008 for direct debits here; outbound pain.001, Bacs Standard 18 (PUR-59) or CSV in purchasing (PUR-29)
  - sap: Incoming direct debit collection (F110 receivables run, SEPA mandates, BACS AUDDIS)
    ours: covered
    where: PAY-18, PAY-19, PAY-20, dd_mandate, dd_run
    why: mandates lodged by AUDDIS, runs approved by a second person, ARUDD and ADDACS applied; a bureau plugs into PAY-43 instead
  - sap: Cash journal (FBCJ)
    ours: covered
    where: PAY-26, cash_journal, cash_entry, cash_count
    why: receipts, expenses with tax's VAT, deposits, a daily count and close
  - sap: Cheque deposit batches (FF68 / FF.5 check deposit)
    ours: covered
    where: PAY-28, deposit_batch
  - sap: Bank reconciliation of all statement lines incl. debits and fees (FEBAN post-processing)
    ours: covered
    where: PAY-21..PAY-25, PAY-34, statement_line, reconciliation_rule
    why: every line matched or posted, balance and continuity checked, book equals bank
  - sap: Bank charges and interest posting from the statement
    ours: covered
    where: PAY-24
  - sap: Internal transfers between house bank accounts (cash in transit)
    ours: covered
    where: PAY-25
    why: through a transfer clearing account, each side on its own date
  - sap: Bank communication / host-to-host connectivity (BCM, Multi-Bank Connectivity)
    ours: different
    where: PAY-35, PAY-43
    why: an open-banking feed and a direct-debit bureau are provider capabilities; outgoing payment approval and files are purchasing's
  - sap: Cash position and liquidity forecast (Cash Management FF7A/FF7B)
    ours: covered
    where: PAY-29
    why: derived on read from statements, open items, runs, bills and tax liabilities, per currency
lifecycle: https://blog.dodil.io/modules/lifecycle.md
module_pattern: https://blog.dodil.io/modules/pattern.md
design_template: https://blog.dodil.io/modules/template.md
