version: urirun.flow.v1
source:
  nl: "Photograph a receipt, parse it to JSON, and turn it into a KSeF-ready invoice draft (net/VAT/gross)."
task:
  id: receipt-to-invoice
  title: "Receipt (paragon) → invoice draft"
steps:
  # 1. camera scans the receipt: crop to the sheet, deskew, OCR, parse items/total/NIP/date.
  - id: parse_receipt
    uri: camera://host/receipt/query/parse
    payload:
      output_dir: "~/.urirun/camera-scans/receipt-invoice"
      target: "receipt"
      deskew: true
      lang: "pol+eng"
    runs_on: node

  # 2. bridge into the invoice/KSeF flow: derive net+VAT from the gross at 23% and emit an
  #    invoice draft. Pass step 1's JSON (items/total/nip/date/currency) as receipt_json.
  #    The draft's net/vat/gross + NIP are what invoice://host/ksef/folder/register consumes.
  - id: invoice_draft
    uri: invoice://host/receipt/query/draft
    payload:
      receipt_json: "${parse_receipt.result.value}"
      vat_rate: 23
    runs_on: node
    depends_on:
      - parse_receipt

  # 3. build the KSeF FA(2) XML draft from the invoice draft and write it to disk. This is
  #    what the ksef:// API connector submits. (Validate against the official FA(2) XSD first.)
  - id: ksef_xml
    uri: invoice://host/ksef/query/build
    payload:
      draft_json: "${invoice_draft.result.value.draft}"
      number: "FV/PAR/2026/001"
      output_path: "~/.urirun/camera-scans/receipt-invoice/faktura.xml"
    runs_on: node
    depends_on:
      - invoice_draft

  # 4. validate the FA(2) XML. With KSEF_FA2_XSD set (official schema from crd.gov.pl) this
  #    is a full XSD check; otherwise structural + arithmetic. Gate real submission on valid=true.
  - id: ksef_validate
    uri: invoice://host/ksef/query/validate
    payload:
      path: "~/.urirun/camera-scans/receipt-invoice/faktura.xml"
      xsd_path: "${env:KSEF_FA2_XSD}"
    runs_on: node
    depends_on:
      - ksef_xml
