{
  "schema_version": "1.0.0",
  "id": "invoice-email-to-xero-bill",
  "title": "Draft a Xero bill from an invoice email",
  "category": "invoices-docs",
  "difficulty": "intermediate",
  "featured": true,
  "description": "Turn a labelled supplier invoice email into a draft Xero bill, skipping duplicates and anything the parser was unsure about.",
  "tags": [
    "invoices",
    "xero",
    "gmail",
    "accounts-payable",
    "ai-extraction"
  ],
  "apps": [
    "Gmail",
    "AI by Zapier",
    "Code by Zapier",
    "Google Sheets",
    "Filter by Zapier",
    "Xero"
  ],
  "plan": {
    "minimum": "Professional",
    "reasons": [
      "multi-step Zap: 6 steps after the trigger",
      "Filter by Zapier",
      "AI by Zapier",
      "premium app: Xero"
    ]
  },
  "tasks": {
    "typical": 5,
    "max": 5
  },
  "trigger": {
    "app": "Gmail",
    "event": "New Labeled Email",
    "kind": "polling",
    "title": "New email under Invoices/Xero",
    "why": "Fires for each email under the Invoices/Xero label. Use a Gmail filter on your main suppliers, or label by hand.",
    "config": {
      "Label": "Invoices/Xero"
    },
    "outputs": [
      "message_id",
      "thread_id",
      "from_email",
      "from_name",
      "to",
      "subject",
      "date",
      "body_plain",
      "snippet",
      "labels",
      "attachment_count",
      "attachment_names"
    ],
    "sample": "sample-trigger.json"
  },
  "steps": [
    {
      "key": "extract",
      "kind": "ai",
      "app": "AI by Zapier",
      "event": "Analyze and Return Data",
      "title": "Pull the invoice fields",
      "why": "Same extraction prompt as the sheet template, so both Zaps agree on what a field means.",
      "fields": {
        "Model": "Default (fast)",
        "Instructions": "You read supplier invoices for an accounts-payable team. From the email below, return: vendor (the company that sent the invoice), invoice_number (exactly as printed), invoice_date and due_date (YYYY-MM-DD, empty if not stated), total_text (the grand total exactly as printed, with its currency symbol or code, for example \"$1,280.00\" or \"1.280,00 EUR\"), currency (ISO code if you can tell, otherwise empty), line_summary (one short line describing what was bought), and text_found (\"yes\" if the email text actually contains the invoice figures, \"no\" if the figures are only inside an attachment you cannot read). Never invent a number. If a value is not in the text, leave it empty.",
        "Input data": "From: {{trigger.from_name}} <{{trigger.from_email}}>\nSubject: {{trigger.subject}}\n\n{{trigger.body_plain}}",
        "Output fields": "vendor, invoice_number, invoice_date, due_date, total_text, currency, line_summary, text_found"
      },
      "outputs": [
        "vendor",
        "invoice_number",
        "invoice_date",
        "due_date",
        "total_text",
        "currency",
        "line_summary",
        "text_found"
      ]
    },
    {
      "key": "money",
      "kind": "code",
      "app": "Code by Zapier",
      "event": "Run JavaScript",
      "title": "Turn the printed total into cents",
      "why": "Xero needs a plain decimal. This step returns one, and flags formats it cannot read with certainty.",
      "code": {
        "language": "javascript",
        "module": "parse-money",
        "file": "code/parse-money.js"
      },
      "input_data": {
        "amount_text": "{{extract.total_text}}",
        "currency_hint": "{{extract.currency}}"
      },
      "outputs": [
        "amount",
        "amount_fixed",
        "amount_cents",
        "currency",
        "format",
        "needs_review"
      ]
    },
    {
      "key": "seen",
      "kind": "search",
      "app": "Google Sheets",
      "event": "Lookup Spreadsheet Row",
      "title": "Check the bill log for this invoice number",
      "why": "Looks for the invoice number in the log the Zap writes, so a reminder email for the same invoice does not create a second bill.",
      "fields": {
        "Drive": "My Google Drive",
        "Spreadsheet": "Payables",
        "Worksheet": "Xero bills",
        "Lookup Column": "invoice_number",
        "Lookup Value": "{{extract.invoice_number}}",
        "Should this step be considered a \"success\" when nothing is found?": "Yes"
      },
      "outputs": [
        "row_number",
        "_zap_search_was_found_status",
        "xero_bill_id"
      ]
    },
    {
      "key": "safe",
      "kind": "filter",
      "app": "Filter by Zapier",
      "event": "Only continue if...",
      "title": "Only new invoices with a clean amount",
      "why": "Stops the run when the invoice is already logged, the amount was ambiguous, or the figures were not in the email text. Those stay under the label for a person.",
      "conditions": [
        [
          {
            "field": "{{seen._zap_search_was_found_status}}",
            "operator": "(Boolean) Is false"
          },
          {
            "field": "{{money.needs_review}}",
            "operator": "(Boolean) Is false"
          },
          {
            "field": "{{extract.text_found}}",
            "operator": "(Text) Exactly matches",
            "value": "yes"
          }
        ]
      ],
      "usually": true
    },
    {
      "key": "bill",
      "kind": "action",
      "app": "Xero",
      "event": "Create Bill",
      "title": "Create the draft bill",
      "why": "Creates the bill as a draft, never approved, so a person still approves payment in Xero.",
      "fields": {
        "Organization": "YOUR_XERO_ORGANISATION",
        "Contact": "{{extract.vendor}}",
        "Invoice Number": "{{extract.invoice_number}}",
        "Date": "{{extract.invoice_date}}",
        "Due Date": "{{extract.due_date}}",
        "Status": "Draft",
        "Currency Code": "{{money.currency}}",
        "Line Amount Types": "Tax Inclusive",
        "Line Item Description": "{{extract.line_summary}}",
        "Line Item Quantity": "1",
        "Line Item Unit Price": "{{money.amount_fixed}}",
        "Line Item Account": "YOUR_EXPENSE_ACCOUNT_CODE"
      },
      "outputs": [
        "id",
        "invoice_id",
        "url"
      ]
    },
    {
      "key": "log",
      "kind": "action",
      "app": "Google Sheets",
      "event": "Create Spreadsheet Row",
      "title": "Log the bill",
      "why": "Records the Xero bill id against the invoice number. The next run's duplicate check reads this sheet.",
      "fields": {
        "Drive": "My Google Drive",
        "Spreadsheet": "Payables",
        "Worksheet": "Xero bills",
        "Columns": {
          "invoice_number": "{{extract.invoice_number}}",
          "vendor": "{{extract.vendor}}",
          "amount": "{{money.amount_fixed}}",
          "currency": "{{money.currency}}",
          "due_date": "{{extract.due_date}}",
          "xero_bill_id": "{{bill.invoice_id}}",
          "created_at": "{{meta.human_now}}"
        }
      },
      "outputs": [
        "id",
        "row_number"
      ]
    }
  ]
}
