{
  "schema_version": "1.0.0",
  "id": "duplicate-invoice-check",
  "title": "Catch a duplicate invoice before it is logged",
  "category": "invoices-docs",
  "difficulty": "intermediate",
  "featured": false,
  "description": "Build one key from vendor, invoice number, and amount, look it up in the log, and either log the invoice or flag the duplicate.",
  "tags": [
    "invoices",
    "duplicates",
    "gmail",
    "google-sheets",
    "accounts-payable"
  ],
  "apps": [
    "Gmail",
    "AI by Zapier",
    "Code by Zapier",
    "Google Sheets",
    "Paths by Zapier",
    "Slack"
  ],
  "plan": {
    "minimum": "Professional",
    "reasons": [
      "multi-step Zap: 8 steps after the trigger",
      "Paths by Zapier",
      "AI by Zapier"
    ]
  },
  "tasks": {
    "typical": 5,
    "max": 6
  },
  "trigger": {
    "app": "Gmail",
    "event": "New Labeled Email",
    "kind": "polling",
    "title": "New email under the Invoices label",
    "why": "Fires for each invoice email under the 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, and total from the email text.",
      "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 key uses cents so \"$1,280.00\" and \"1280\" match.",
      "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": "key",
      "kind": "code",
      "app": "Code by Zapier",
      "event": "Run JavaScript",
      "title": "Build the duplicate key",
      "why": "Normalises the vendor (\"Harbor Goods Ltd.\" and \"HARBOR GOODS\" match) and the number (\"INV-001842\", \"#1842\") into one key.",
      "code": {
        "language": "javascript",
        "module": "invoice-key",
        "file": "code/invoice-key.js"
      },
      "input_data": {
        "vendor": "{{extract.vendor}}",
        "invoice_number": "{{extract.invoice_number}}",
        "amount_cents": "{{money.amount_cents}}"
      },
      "outputs": [
        "key",
        "vendor_slug",
        "number_clean",
        "complete"
      ]
    },
    {
      "key": "seen",
      "kind": "search",
      "app": "Google Sheets",
      "event": "Lookup Spreadsheet Row",
      "title": "Look the key up in the log",
      "why": "Searches the key column the new-invoice path writes.",
      "fields": {
        "Drive": "My Google Drive",
        "Spreadsheet": "Payables",
        "Worksheet": "Invoice log",
        "Lookup Column": "invoice_key",
        "Lookup Value": "{{key.key}}",
        "Should this step be considered a \"success\" when nothing is found?": "Yes"
      },
      "outputs": [
        "row_number",
        "_zap_search_was_found_status",
        "logged_at",
        "gmail_link"
      ]
    },
    {
      "key": "route",
      "kind": "paths",
      "app": "Paths by Zapier",
      "event": "Split into paths",
      "title": "New invoice or duplicate",
      "why": "One path per answer from the lookup.",
      "paths": [
        {
          "name": "New invoice",
          "conditions": [
            [
              {
                "field": "{{seen._zap_search_was_found_status}}",
                "operator": "(Boolean) Is false"
              }
            ]
          ],
          "usually": true,
          "steps": [
            {
              "key": "log",
              "kind": "action",
              "app": "Google Sheets",
              "event": "Create Spreadsheet Row",
              "title": "Log the invoice with its key",
              "why": "Writes the key so the next copy of this invoice is caught.",
              "fields": {
                "Drive": "My Google Drive",
                "Spreadsheet": "Payables",
                "Worksheet": "Invoice log",
                "Columns": {
                  "invoice_key": "{{key.key}}",
                  "logged_at": "{{trigger.date}}",
                  "vendor": "{{extract.vendor}}",
                  "invoice_number": "{{key.number_clean}}",
                  "amount": "{{money.amount_fixed}}",
                  "currency": "{{money.currency}}",
                  "due_date": "{{extract.due_date}}",
                  "gmail_link": "https://mail.google.com/mail/u/0/#all/{{trigger.message_id}}"
                }
              },
              "outputs": [
                "id",
                "row_number"
              ]
            }
          ]
        },
        {
          "name": "Duplicate",
          "conditions": [
            [
              {
                "field": "{{seen._zap_search_was_found_status}}",
                "operator": "(Boolean) Is true"
              }
            ]
          ],
          "usually": false,
          "steps": [
            {
              "key": "tag",
              "kind": "action",
              "app": "Gmail",
              "event": "Add Label to Email",
              "title": "Label the email as a duplicate",
              "why": "Keeps the copy out of the payment run without deleting it.",
              "fields": {
                "Message": "{{trigger.message_id}}",
                "Labels": "Invoices/Duplicate"
              },
              "outputs": [
                "id"
              ]
            },
            {
              "key": "warn",
              "kind": "action",
              "app": "Slack",
              "event": "Send Channel Message",
              "title": "Tell AP about the duplicate",
              "why": "Links the first copy so a person can confirm in seconds.",
              "fields": {
                "Channel": "#accounts-payable",
                "Message Text": "Possible duplicate: {{extract.vendor}} invoice {{key.number_clean}} for {{money.amount_fixed}} {{money.currency}}. First logged {{seen.logged_at}} (row {{seen.row_number}}): {{seen.gmail_link}}",
                "Send as a bot?": "Yes",
                "Bot Name": "Zap alerts",
                "Include a link to this Zap?": "No"
              },
              "outputs": [
                "ts",
                "permalink"
              ]
            }
          ]
        }
      ]
    }
  ]
}
