{
  "schema_version": "1.0.0",
  "id": "invoice-approval-over-limit",
  "title": "Ask for approval on invoices over a limit",
  "category": "invoices-docs",
  "difficulty": "intermediate",
  "featured": false,
  "description": "Log every invoice, and post the ones over 5,000 to an approvals channel with the amount, due date, and a link to the email.",
  "tags": [
    "invoices",
    "approvals",
    "slack",
    "google-sheets",
    "gmail"
  ],
  "apps": [
    "Gmail",
    "AI by Zapier",
    "Code by Zapier",
    "Google Sheets",
    "Filter by Zapier",
    "Slack"
  ],
  "plan": {
    "minimum": "Professional",
    "reasons": [
      "multi-step Zap: 5 steps after the trigger",
      "Filter by Zapier",
      "AI by Zapier"
    ]
  },
  "tasks": {
    "typical": 3,
    "max": 4
  },
  "trigger": {
    "app": "Gmail",
    "event": "New Labeled Email",
    "kind": "polling",
    "title": "New email under the Invoices label",
    "why": "Fires for each email under the Invoices label.",
    "config": {
      "Label": "Invoices"
    },
    "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": "Reads vendor, number, dates, and the total as printed.",
      "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": "The limit check compares integer cents, so \"5.000,00 EUR\" is not read as five.",
      "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": "log",
      "kind": "action",
      "app": "Google Sheets",
      "event": "Create Spreadsheet Row",
      "title": "Log the invoice",
      "why": "Every invoice is logged, approved or not.",
      "fields": {
        "Drive": "My Google Drive",
        "Spreadsheet": "Payables",
        "Worksheet": "Invoices",
        "Columns": {
          "received_at": "{{trigger.date}}",
          "vendor": "{{extract.vendor}}",
          "invoice_number": "{{extract.invoice_number}}",
          "amount": "{{money.amount_fixed}}",
          "currency": "{{money.currency}}",
          "due_date": "{{extract.due_date}}",
          "approval": "not needed unless posted"
        }
      },
      "outputs": [
        "id",
        "row_number"
      ]
    },
    {
      "key": "large",
      "kind": "filter",
      "app": "Filter by Zapier",
      "event": "Only continue if...",
      "title": "Only invoices at or over the limit",
      "why": "Most invoices stop here. 499999 cents is the line, so 5,000.00 and above continue.",
      "conditions": [
        [
          {
            "field": "{{money.amount_cents}}",
            "operator": "(Number) Greater than",
            "value": "499999"
          }
        ]
      ],
      "usually": false
    },
    {
      "key": "ask",
      "kind": "action",
      "app": "Slack",
      "event": "Send Channel Message",
      "title": "Post it for approval",
      "why": "Gives the approver everything needed to decide from Slack.",
      "fields": {
        "Channel": "#invoice-approvals",
        "Message Text": ":money_with_wings: Approval needed: {{extract.vendor}} invoice {{extract.invoice_number}} for *{{money.amount_fixed}} {{money.currency}}*, due {{extract.due_date}}. What: {{extract.line_summary}}. Sheet row {{log.row_number}}. Email: https://mail.google.com/mail/u/0/#all/{{trigger.message_id}}",
        "Send as a bot?": "Yes",
        "Bot Name": "Zap alerts",
        "Include a link to this Zap?": "No"
      },
      "outputs": [
        "ts",
        "permalink"
      ]
    }
  ]
}
