{
  "nodes": [
    {
      "id": "c1e329c7-7a4b-4f43-a4de-4837bdc45a3b",
      "name": "Gmail Trigger",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        4000,
        544
      ],
      "parameters": {
        "simple": false,
        "filters": {
          "sender": "user@example.com"
        },
        "options": {
          "downloadAttachments": true
        },
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "fc9966a6-7d22-4bf5-80d8-28aeb12d8d64",
      "name": "Extract from File",
      "type": "n8n-nodes-base.extractFromFile",
      "position": [
        4224,
        544
      ],
      "parameters": {
        "options": {},
        "operation": "pdf",
        "binaryPropertyName": "attachment_0"
      },
      "typeVersion": 1
    },
    {
      "id": "333f965f-f0c5-420d-b211-ae8216f60e32",
      "name": "Structured Output Parser",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        4584,
        768
      ],
      "parameters": {
        "jsonSchemaExample": "{\n  \"order_id\": \"\",\n  \"sku\": \"\",\n  \"customer_name\": \"\",\n  \"email\": \"\",\n  \"phone\": \"\",\n  \"item_name\": \"\",\n  \"quantity\": \"\",\n  \"price\": \"\",\n  \"total\": \"\"\n}"
      },
      "typeVersion": 1.3
    },
    {
      "id": "fe863840-1c1f-4857-a4bd-388fbf86e124",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        4456,
        768
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "86023db0-7c7e-4ff2-9a96-bf002453223f",
      "name": "Send a message",
      "type": "n8n-nodes-base.gmail",
      "position": [
        5696,
        448
      ],
      "parameters": {
        "sendTo": "user@example.com",
        "message": "=<!DOCTYPE html>\n<html>\n<head>\n  <style>\n    body {\n      font-family: Arial, sans-serif;\n      color: #333333;\n      line-height: 1.6;\n    }\n    .container {\n      padding: 20px;\n      background-color: #f9f9f9;\n    }\n    .header {\n      font-size: 18px;\n      font-weight: bold;\n      margin-bottom: 15px;\n    }\n    .section {\n      margin-bottom: 15px;\n      background-color: #ffffff;\n      padding: 15px;\n      border-radius: 5px;\n      border: 1px solid #dddddd;\n    }\n    .section h3 {\n      margin: 0 0 10px 0;\n      font-size: 16px;\n      color: #555555;\n    }\n    .section p {\n      margin: 5px 0;\n    }\n    .footer {\n      font-size: 14px;\n      color: #777777;\n      margin-top: 20px;\n    }\n  </style>\n</head>\n<body>\n  <div class=\"container\">\n    <div class=\"header\">\n      Purchase Requisition Request\n    </div>\n    \n    <div class=\"section\">\n      <h3>Item Details</h3>\n      <p><strong>Item Name:</strong> {{ $json['item name'] }}</p>\n      <p><strong>SKU / Item Code:</strong> {{ $json['SKU / Item Code'] }}</p>\n      <p><strong>Current Quantity:</strong> {{ $json['Current Quantity'] }}</p>\n      <p><strong>Requested Quantity:</strong> {{ $json['Quantity Requested'] }}</p>\n      <p><strong>Priority:</strong> {{ $json['Priority'] }}</p>\n    </div>\n    \n    <div class=\"section\">\n      <h3>Request Summary</h3>\n      <p>This item is required to ensure uninterrupted operations and maintain inventory levels. Kindly prioritize this requisition and process it at the earliest convenience.</p>\n      <p>Please let me know if any additional information or approval is required to expedite this request.</p>\n    </div>\n    \n    <div class=\"footer\">\n      Thank you for your attention.<br>\n    </div>\n  </div>\n</body>\n</html>\n",
        "options": {
          "appendAttribution": false
        },
        "subject": "=Purchase Requisition Request \u2013 {{ $json['item name'] }} / {{ $json['SKU / Item Code'] }}"
      },
      "typeVersion": 2.1
    },
    {
      "id": "d33dc38e-679b-4d06-918c-aad95669d1cd",
      "name": "Extract Invoice Record",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        4448,
        544
      ],
      "parameters": {
        "text": "={{ $json.text }}",
        "options": {},
        "hasOutputParser": true
      },
      "typeVersion": 2.2
    },
    {
      "id": "d094de77-6ec7-4271-a36f-d4ec3446ee74",
      "name": "Sales Order Creation",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        4800,
        544
      ],
      "parameters": {
        "columns": {
          "value": {
            "SKU": "={{ $json.output.sku }}",
            "Email": "={{ $json.output.email }}",
            "Phone": "={{ $json.output.phone }}",
            "Price": "={{ $json.output.price }}",
            "Total": "={{ $json.output.total }}",
            "Order id": "={{ $json.output.order_id }}",
            "Quantity": "={{ $json.output.quantity }}",
            "Item Name": "={{ $json.output.item_name }}",
            "Customer Name": "={{ $json.output.customer_name }}"
          },
          "schema": [
            {
              "id": "Order id",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Order id",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "SKU",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "SKU",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Customer Name",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Customer Name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Email",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Email",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Phone",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Phone",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Item Name",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Item Name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Quantity",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Quantity",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Price",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Price",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Total",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Total",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Customer order"
        },
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "1X_iuREMFdiefIF_ukCwTBUMXGURwR1mC-RMl1hztssw"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "f9bc4f56-be77-4298-a16c-831f5c8ce827",
      "name": "Stock & Shortage Check",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        5056,
        544
      ],
      "parameters": {
        "options": {},
        "filtersUI": {
          "values": [
            {
              "lookupValue": "={{ $json['Item Name'] }}",
              "lookupColumn": "Item Name"
            }
          ]
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Product invetory"
        },
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "1X_iuREMFdiefIF_ukCwTBUMXGURwR1mC-RMl1hztssw"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "a288c427-f713-4d61-89d3-e9519bfea2f0",
      "name": "If Negative",
      "type": "n8n-nodes-base.if",
      "position": [
        5248,
        544
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "760d94b6-2836-4058-8a0d-6dcd149ad4b9",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json['Stock '] }}",
              "rightValue": "out of Stock"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "df07b6d3-d71e-41b7-8f74-69d441f82696",
      "name": "Create Purchase Requisition",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        5472,
        448
      ],
      "parameters": {
        "columns": {
          "value": {
            "Priority": "High",
            "item name": "={{ $json['Item Name'] }}",
            "Request Date": "={{$now.format('yyyy-MM-dd')}}",
            "SKU / Item Code": "={{ $('Sales Order Creation').item.json.SKU }}",
            "Current Quantity": "={{ $json['Stock '] }}",
            "Quantity Requested": "={{ $('Sales Order Creation').item.json.Quantity }}"
          },
          "schema": [
            {
              "id": "Request Date",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Request Date",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "item name",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "item name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "SKU / Item Code",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "SKU / Item Code",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Current Quantity",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Current Quantity",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Quantity Requested",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Quantity Requested",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Priority",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Priority",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Purchase Requisition"
        },
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "1X_iuREMFdiefIF_ukCwTBUMXGURwR1mC-RMl1hztssw"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "544667d2-f251-40f3-99fb-db9ff32df73e",
      "name": "Create  Work  Order",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        5472,
        640
      ],
      "parameters": {
        "columns": {
          "value": {
            "SKU ": "={{ $('Sales Order Creation').item.json.SKU }}",
            "order id": "={{ $('Sales Order Creation').item.json['Order id'] }}",
            "item name": "={{ $('Sales Order Creation').item.json['Item Name'] }}",
            "Customer name": "={{ $('Sales Order Creation').item.json['Customer Name'] }}",
            "Planned  Due Date": "={{ new Date(new Date().getTime() + 3*24*60*60*1000).toISOString().split('T')[0] }}",
            "Quantity to Produce": "={{ $('Sales Order Creation').item.json.Quantity }}"
          },
          "schema": [
            {
              "id": "order id",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "order id",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "SKU ",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "SKU ",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "item name",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "item name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Customer name",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Customer name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Quantity to Produce",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Quantity to Produce",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Planned  Due Date",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Planned  Due Date",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Work Order Create"
        },
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "1X_iuREMFdiefIF_ukCwTBUMXGURwR1mC-RMl1hztssw"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "125d0e86-a4d6-4ee4-809f-d42e1b4d0488",
      "name": "Send Order Summary",
      "type": "n8n-nodes-base.slack",
      "position": [
        5696,
        640
      ],
      "parameters": {
        "text": "=*\ud83d\udccb Work Order Summary*\n\n\u2022 *Order ID:* {{ $json['order id'] }}\n  - SKU: {{ $json['SKU '] }}\n  - Item Name: {{ $json['item name'] }}\n  - Customer: {{ $json['Customer name'] }}\n  - Quantity to Produce: {{ $json['Quantity to Produce'] }}\n  - Planned Due Date: {{ $json['Planned  Due Date'] }}",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "id",
          "value": "C08QEDHAYSZ"
        },
        "otherOptions": {}
      },
      "typeVersion": 2.3
    },
    {
      "id": "9950f0af-fa1a-4965-bd7c-2b5ddc3a5ee6",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        3456,
        336
      ],
      "parameters": {
        "width": 450,
        "height": 496,
        "content": "## How it works\nThis workflow automates order procurement by monitoring Gmail for invoice PDFs. When an invoice arrives, AI extracts order details and checks inventory levels. If stock is sufficient, a work order is created and sent to Slack. If stock is low, a purchase requisition is generated and emailed to procurement.\n\n## Setup steps\n1. **Connect Gmail** \u2013 Add OAuth credentials to Gmail Trigger and Send nodes\n2. **Connect OpenAI** \u2013 Add API key to OpenAI Chat Model\n3. **Connect Google Sheets** \u2013 Add OAuth credentials and update the spreadsheet ID in all Google Sheets nodes (or use the sample sheet linked below)\n4. **Connect Slack** \u2013 Add access token and select your channel\n5. **Update email addresses** \u2013 Set sender filter in Gmail Trigger and recipient in Send a message node\n6. **Test** \u2013 Send a sample invoice PDF to trigger the workflow\n\n\ud83d\udcca Sample Sheet: https://docs.google.com/spreadsheets/d/1X_iuREMFdiefIF_ukCwTBUMXGURwR1mC-RMl1hztssw"
      },
      "typeVersion": 1
    },
    {
      "id": "2ebf1cf1-b5ef-4b79-8b9f-9da78c0d1868",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        3936,
        336
      ],
      "parameters": {
        "color": 7,
        "width": 1032,
        "height": 620,
        "content": "## Invoice Processing\nExtracts order data from PDF attachments using AI to structure customer info, SKU, quantity, and pricing."
      },
      "typeVersion": 1
    },
    {
      "id": "ef95cae2-e583-4a2c-8333-61e5a710bdb0",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        4992,
        336
      ],
      "parameters": {
        "color": 7,
        "width": 896,
        "height": 608,
        "content": "## Check stock availability or not\n- If available, then create a work order and notify via Slack.\n- In not available, then create a Purchase Requisition and send an email to the procurement team."
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "If Negative": {
      "main": [
        [
          {
            "node": "Create Purchase Requisition",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Create  Work  Order",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail Trigger": {
      "main": [
        [
          {
            "node": "Extract from File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract from File": {
      "main": [
        [
          {
            "node": "Extract Invoice Record",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Extract Invoice Record",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Create  Work  Order": {
      "main": [
        [
          {
            "node": "Send Order Summary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sales Order Creation": {
      "main": [
        [
          {
            "node": "Stock & Shortage Check",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Invoice Record": {
      "main": [
        [
          {
            "node": "Sales Order Creation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Stock & Shortage Check": {
      "main": [
        [
          {
            "node": "If Negative",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser": {
      "ai_outputParser": [
        [
          {
            "node": "Extract Invoice Record",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Create Purchase Requisition": {
      "main": [
        [
          {
            "node": "Send a message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}