{
  "name": "Email Order to Procurement Automation",
  "nodes": [
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "simple": false,
        "filters": {
          "sender": "team@forgentsystems.com"
        },
        "options": {
          "downloadAttachments": true
        }
      },
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1.3,
      "position": [
        -960,
        -128
      ],
      "id": "e74f8c1a-9bdc-45d3-bdd7-32da13116719",
      "name": "Gmail Trigger",
      "credentials": {
        "gmailOAuth2": {
          "id": "LBIbVskaBzVhN0SE",
          "name": "Gmail account"
        }
      }
    },
    {
      "parameters": {
        "operation": "pdf",
        "binaryPropertyName": "attachment_0",
        "options": {}
      },
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1,
      "position": [
        -784,
        -128
      ],
      "id": "c7d5c8c8-a884-4a99-b1b0-74a54b4f0def",
      "name": "Extract from File"
    },
    {
      "parameters": {
        "jsonSchemaExample": "{\n  \"order_id\": \"\",\n  \"sku\": \"\",\n  \"customer_name\": \"\",\n  \"email\": \"\",\n  \"phone\": \"\",\n  \"item_name\": \"\",\n  \"quantity\": \"\",\n  \"price\": \"\",\n  \"total\": \"\"\n}"
      },
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "typeVersion": 1.3,
      "position": [
        -480,
        96
      ],
      "id": "d1fae6b5-52fe-45f7-aed0-2154d927b78d",
      "name": "Structured Output Parser"
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "value": "gpt-4o",
          "mode": "list",
          "cachedResultName": "gpt-4o"
        },
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.2,
      "position": [
        -624,
        96
      ],
      "id": "0111cb9e-aba6-47ef-b93e-0c14a212c7c2",
      "name": "OpenAI Chat Model",
      "credentials": {
        "openAiApi": {
          "id": "UFoXLJTyZlrIERe4",
          "name": "OpenAI account"
        }
      }
    },
    {
      "parameters": {
        "sendTo": "team@forgentsystems.com",
        "subject": "=Purchase Requisition Request – {{ $json['item name'] }} / {{ $json['SKU / Item Code'] }}",
        "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
        }
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        336,
        -256
      ],
      "id": "a5dfe6d0-9f5c-4ddc-9a65-a932737293bc",
      "name": "Send a message",
      "webhookId": "5400a110-2133-49d4-8f0a-c701b5e1d5c8",
      "credentials": {
        "gmailOAuth2": {
          "id": "LBIbVskaBzVhN0SE",
          "name": "Gmail account"
        }
      }
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=I will provide multiple invoice records in plain text. For each record, generate a structured output in the following format:\n\n{\n  \"order_id\": \"generate a unique random number for each record\",\n  \"sku\": \"\",\n  \"customer_name\": \"\",\n  \"email\": \"\",\n  \"phone\": \"\",\n  \"item_name\": \"\",\n  \"quantity\": number,\n  \"price\": number,\n  \"total\": number  // calculate as quantity × price\n}\n\nReturn the output as a table or a structured list, one record per line. Ensure the Order ID is unique for each record.  \nHere are the invoice records: \n{{ $json.text }}",
        "hasOutputParser": true,
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 2.2,
      "position": [
        -624,
        -128
      ],
      "id": "421419b0-bbce-4302-a9ff-100ab6f4b50d",
      "name": "Extract Invoice Record"
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "1X_iuREMFdiefIF_ukCwTBUMXGURwR1mC-RMl1hztssw",
          "mode": "list",
          "cachedResultName": "Order tracking"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Customer order"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Order id": "={{ $json.output.order_id }}",
            "SKU": "={{ $json.output.sku }}",
            "Customer Name": "={{ $json.output.customer_name }}",
            "Email": "={{ $json.output.email }}",
            "Phone": "={{ $json.output.phone }}",
            "Item Name": "={{ $json.output.item_name }}",
            "Quantity": "={{ $json.output.quantity }}",
            "Price": "={{ $json.output.price }}",
            "Total": "={{ $json.output.total }}"
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "Order id",
              "displayName": "Order id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "SKU",
              "displayName": "SKU",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Customer Name",
              "displayName": "Customer Name",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Email",
              "displayName": "Email",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Phone",
              "displayName": "Phone",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Item Name",
              "displayName": "Item Name",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Quantity",
              "displayName": "Quantity",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Price",
              "displayName": "Price",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Total",
              "displayName": "Total",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        -352,
        -128
      ],
      "id": "0d55ca39-7541-4aaa-bf41-707a8808abd3",
      "name": "Sales Order Creation",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "9rJzpmHDDKlqEEe9",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "1X_iuREMFdiefIF_ukCwTBUMXGURwR1mC-RMl1hztssw",
          "mode": "list",
          "cachedResultName": "Order tracking"
        },
        "sheetName": {
          "__rl": true,
          "value": 1401516918,
          "mode": "list",
          "cachedResultName": "Product inventory"
        },
        "filtersUI": {
          "values": [
            {
              "lookupColumn": "Item Name",
              "lookupValue": "={{ $json['Item Name'] }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        -176,
        -128
      ],
      "id": "42de3125-0308-4990-b4b8-faa2ccf3050b",
      "name": "Stock & Shortage Check",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "9rJzpmHDDKlqEEe9",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "760d94b6-2836-4058-8a0d-6dcd149ad4b9",
              "leftValue": "={{ $json['Stock '] }}",
              "rightValue": "out of Stock",
              "operator": {
                "type": "string",
                "operation": "equals",
                "name": "filter.operator.equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        -16,
        -128
      ],
      "id": "5522959f-5bbf-4707-a9e1-085536d1fade",
      "name": "If Negative"
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "1X_iuREMFdiefIF_ukCwTBUMXGURwR1mC-RMl1hztssw",
          "mode": "list",
          "cachedResultName": "Order tracking"
        },
        "sheetName": {
          "__rl": true,
          "value": 1963817265,
          "mode": "list",
          "cachedResultName": "Purchase Requisition"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Request Date": "={{$now.format('yyyy-MM-dd')}}",
            "item name": "={{ $json['Item Name'] }}",
            "SKU / Item Code": "={{ $('Sales Order Creation').item.json.SKU }}",
            "Quantity Requested": "={{ $('Sales Order Creation').item.json.Quantity }}",
            "Priority": "High",
            "Current Quantity": "={{ $json['Stock '] }}"
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "Request Date",
              "displayName": "Request Date",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "item name",
              "displayName": "item name",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "SKU / Item Code",
              "displayName": "SKU / Item Code",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Current Quantity",
              "displayName": "Current Quantity",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Quantity Requested",
              "displayName": "Quantity Requested",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Priority",
              "displayName": "Priority",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        160,
        -256
      ],
      "id": "5c05b6a4-4160-4c4a-9449-0486639a5ff9",
      "name": "Create Purchase Requisition",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "9rJzpmHDDKlqEEe9",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "1X_iuREMFdiefIF_ukCwTBUMXGURwR1mC-RMl1hztssw",
          "mode": "list",
          "cachedResultName": "Order tracking"
        },
        "sheetName": {
          "__rl": true,
          "value": 444044674,
          "mode": "list",
          "cachedResultName": "Work Order Create"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "order id": "={{ $('Sales Order Creation').item.json['Order id'] }}",
            "SKU ": "={{ $('Sales Order Creation').item.json.SKU }}",
            "item name": "={{ $('Sales Order Creation').item.json['Item Name'] }}",
            "Quantity to Produce": "={{ $('Sales Order Creation').item.json.Quantity }}",
            "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] }}\n"
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "order id",
              "displayName": "order id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "SKU ",
              "displayName": "SKU ",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "item name",
              "displayName": "item name",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Customer name",
              "displayName": "Customer name",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Quantity to Produce",
              "displayName": "Quantity to Produce",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Planned  Due Date",
              "displayName": "Planned  Due Date",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        144,
        0
      ],
      "id": "2cec292e-5eaf-4784-b75b-ea5cc0f08158",
      "name": "Create  Work  Order",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "9rJzpmHDDKlqEEe9",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "select": "channel",
        "channelId": {
          "__rl": true,
          "value": "C08QEDHAYSZ",
          "mode": "list",
          "cachedResultName": "all-forgent-systems"
        },
        "text": "=*📋 Work Order Summary*\n\n• *Order ID:* {{ $json['order id'] }}\n  - SKU: {{ $json['SKU '] }}\n  - Item Name: {{ $json['item name'] }}\n  - Customer: {{ $json['item name'] }}\n  - Quantity to Produce: {{ $json['Quantity to Produce'] }}\n  - Planned Due Date: {{ $json['Planned  Due Date'] }}",
        "otherOptions": {}
      },
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.3,
      "position": [
        336,
        0
      ],
      "id": "71b09eb1-d1bd-4bad-8c07-c2fb68e6e06a",
      "name": "Send Order Summary",
      "webhookId": "3090f385-b221-412e-8ce9-c8d2d59538dc",
      "credentials": {
        "slackApi": {
          "id": "R4nnpqtlL26ynf4r",
          "name": "Slack account"
        }
      }
    },
    {
      "parameters": {
        "content": "## Data Setup\nCreate your own spreadsheet with these tabs/columns, then select it in the Google Sheets nodes:\n- **Order tracking → Customer order**: `Order id`, `SKU`, `Customer Name`, `Email`, `Phone`, `Item Name`, `Quantity`, `Price`, `Total`\n- **Order tracking → Product inventory**: `Item Name`\n- **Order tracking → Purchase Requisition**: `Request Date`, `item name`, `SKU / Item Code`, `Current Quantity`, `Quantity Requested`, `Priority`\n- **Order tracking → Work Order Create**: `order id`, `SKU`, `item name`, `Customer name`, `Quantity to Produce`, `Planned  Due Date`",
        "height": 144,
        "width": 320
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -960,
        -320
      ],
      "typeVersion": 1,
      "id": "c83dc0ef-1604-46fd-aa02-f3d52fd33e9d",
      "name": "Sticky Note"
    }
  ],
  "pinData": {},
  "connections": {
    "Gmail Trigger": {
      "main": [
        [
          {
            "node": "Extract from File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract from File": {
      "main": [
        [
          {
            "node": "Extract Invoice Record",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser": {
      "ai_outputParser": [
        [
          {
            "node": "Extract Invoice Record",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Extract Invoice Record",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Send a message": {
      "main": [
        []
      ]
    },
    "Extract Invoice Record": {
      "main": [
        [
          {
            "node": "Sales Order Creation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sales Order Creation": {
      "main": [
        [
          {
            "node": "Stock & Shortage Check",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Stock & Shortage Check": {
      "main": [
        [
          {
            "node": "If Negative",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If Negative": {
      "main": [
        [
          {
            "node": "Create Purchase Requisition",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Create  Work  Order",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Purchase Requisition": {
      "main": [
        [
          {
            "node": "Send a message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create  Work  Order": {
      "main": [
        [
          {
            "node": "Send Order Summary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "530b24d3-1c24-43e0-a94a-93d5370ffaf4",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "f1c26778ec36097a9722a22c47c5e9c01f00b8862dadd0121ec118026a370fa6"
  },
  "id": "6nEPDbAfjB1RShba",
  "tags": []
}