{
  "workflow_id": "bills_to_spreadsheet",
  "description": "Reads every bill in a folder you choose, has the AI on this PC pick out the vendor, invoice number, date and total, and adds one row per bill to bills.xlsx on your Desktop. Files that are not bills, and bills already in the sheet, get no row. Nothing leaves this PC.",
  "tasks": [
    {
      "id": "each_bill",
      "trigger": "manual",
      "action_type": "for_each",
      "target": "{{input.bills_folder}}",
      "payload": {
        "match": ".txt,.pdf,.docx",
        "steps": [
          {
            "id": "read",
            "action_type": "file_op",
            "target": "{{item}}",
            "payload": {
              "operation": "read_document"
            },
            "output": {
              "store_as": "bill"
            }
          },
          {
            "id": "pick_fields",
            "action_type": "ai_call",
            "target": "local",
            "payload": {
              "system": "You read documents and reply with JSON only.",
              "prompt": "Read this document and reply with ONLY one JSON object: {\"is_bill\":\"yes or no\",\"vendor\":\"...\",\"invoice_number\":\"...\",\"date\":\"...\",\"total\":\"...\"}. Set is_bill to yes if this is a bill, invoice, statement, or receipt showing a total amount or charge (a receipt number or statement number counts as invoice_number; copy each value as it appears; total as a number without a currency sign). If it is not a bill, invoice, or receipt, set is_bill to no and leave the rest empty.\n\nDOCUMENT:\n{{bill}}",
              "ask_times": 3
            },
            "output": {
              "store_as": "fields"
            }
          },
          {
            "id": "add_to_sheet",
            "action_type": "file_op",
            "target": "{{desktop_dir}}/bills.xlsx",
            "payload": {
              "operation": "add_row",
              "headers": [
                "Vendor",
                "Invoice number",
                "Date",
                "Total",
                "File"
              ],
              "values": [
                "{{fields.vendor}}",
                "{{fields.invoice_number}}",
                "{{fields.date}}",
                "{{fields.total}}",
                "{{item_name}}"
              ],
              "unique_by": [
                "Vendor",
                "Invoice number"
              ],
              "must_appear_in": "{{bill}}",
              "check_columns": [
                "Vendor",
                "Invoice number",
                "Total"
              ]
            },
            "run_if": "{{fields.is_bill}} == yes"
          }
        ]
      }
    }
  ]
}
