{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "7db65121-c146-4d70-8dac-7affb58032cb",
      "name": "Send Order to Suplier",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1648,
        176
      ],
      "parameters": {
        "sendTo": "={{ $json.supplier_email }}",
        "message": "={{ $json.body }}",
        "options": {
          "appendAttribution": false
        },
        "subject": "={{ $json.subject }}",
        "emailType": "text"
      },
      "typeVersion": 2.1
    },
    {
      "id": "0f9634e3-6688-4081-aeae-438109366bd3",
      "name": "Switch Email & PO",
      "type": "n8n-nodes-base.switch",
      "position": [
        1328,
        352
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "outputKey": "Email",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "6167fcaf-6603-44b3-971f-0d4157015fae",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.type }}",
                    "rightValue": "email"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Purchase Order",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "97d1cb23-55d5-45de-afcb-89c71ba38ec1",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.type }}",
                    "rightValue": "po"
                  }
                ]
              },
              "renameOutput": true
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3.2
    },
    {
      "id": "5e9b4876-de98-42b5-be19-7cb8dffd5f74",
      "name": "Generate Email & PO",
      "type": "n8n-nodes-base.code",
      "position": [
        1136,
        352
      ],
      "parameters": {
        "jsCode": "const supplierOrders = {};\nconst orderDate = new Date().toISOString().split(\"T\")[0];\nconst poNumber = `PO-${Date.now()}`;\n\n// Group by supplier\nfor (const item of items) {\n  const { sku, name, supplier, supplier_email, reorder_needed, order_qty, available, threshold } = item.json;\n\n  if (reorder_needed) {\n    if (!supplierOrders[supplier_email]) {\n      supplierOrders[supplier_email] = {\n        supplier,\n        supplierEmail: supplier_email,\n        items: []\n      };\n    }\n\n    supplierOrders[supplier_email].items.push({\n      sku,\n      name,\n      orderQty: order_qty,\n      available,\n      threshold\n    });\n  }\n}\n\nconst results = [];\n\nfor (const supplierEmail in supplierOrders) {\n  const order = supplierOrders[supplierEmail];\n  const itemList = order.items.map(i =>\n    `- ${i.name} (SKU: ${i.sku})\\n   Order Qty: ${i.orderQty}`\n  ).join(\"\\n\");\n\n  // Email\n  const emailSubject = `Purchase Order ${poNumber} - ${order.supplier}`;\n  const emailBody = `Dear ${order.supplier},\\n\\n` +\n    `We would like to place the following order:\\n\\n${itemList}\\n\\n` +\n    `PO Number: ${poNumber}\\nOrder Date: ${orderDate}\\n\\n` +\n    `Please confirm availability and expected delivery.\\n\\n` +\n    `Best regards,\\nProcurement Team`;\n\n  results.push({\n    json: {\n      type: \"email\",\n      supplier: order.supplier,\n      supplier_email: order.supplierEmail,\n      subject: emailSubject,\n      body: emailBody\n    }\n  });\n\n  // PO Rows (for Sheet)\n  for (const i of order.items) {\n    results.push({\n      json: {\n        type: \"po\",\n        po_number: poNumber,\n        supplier: order.supplier,\n        supplier_email: order.supplierEmail,\n        sku: i.sku,\n        item_name: i.name,\n        available: i.available,\n        threshold: i.threshold,\n        order_qty: i.orderQty,\n        order_date: orderDate\n      }\n    });\n  }\n}\n\nreturn results;\n"
      },
      "typeVersion": 2
    },
    {
      "id": "348e2d77-ba5e-4c48-b2b4-6a298277b29d",
      "name": "Order Handling",
      "type": "n8n-nodes-base.code",
      "position": [
        928,
        352
      ],
      "parameters": {
        "jsCode": "const results = [];\n\nfor (const item of items) {\n  const sku = item.json.sku;\n  const name = item.json.Name;\n  const supplier = item.json.supplier;\n  const supplierEmail = item.json.supplier_email;\n\n  const available = Number(item.json[\"Available Qty\"]) || 0;\n  const threshold = Number(item.json[\"Min. Threshold\"]) || 0;\n  const minOrder = Number(item.json[\"Min Order\"]) || 0;\n  const maxOrder = Number(item.json[\"Max Order\"]) || 0;\n\n  let reorderNeeded = false;\n  let orderQty = 0;\n\n  // Check if reorder needed\n  if (available < threshold) {\n    reorderNeeded = true;\n\n    // Basic calculation: how much short\n    orderQty = threshold - available;\n\n    // Ensure reorderQty >= Min Order\n    if (orderQty < minOrder) {\n      orderQty = minOrder;\n    }\n\n    // Ensure reorderQty <= Max Order\n    if (orderQty > maxOrder) {\n      orderQty = maxOrder;\n    }\n  }\n\n  results.push({\n    json: {\n      sku,\n      name,\n      supplier,\n      supplier_email: supplierEmail,\n      available,\n      threshold,\n      minOrder,\n      maxOrder,\n      reorder_needed: reorderNeeded,\n      order_qty: orderQty\n    }\n  });\n}\n\nreturn results;\n"
      },
      "typeVersion": 2
    },
    {
      "id": "d226a550-b568-436f-92fe-ccfc51f9d66b",
      "name": "No Operation, do nothing",
      "type": "n8n-nodes-base.noOp",
      "position": [
        2352,
        240
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "af031a3b-8e21-44f1-887c-029a7deafcc5",
      "name": "Loop Over Items",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        1632,
        352
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "5242396e-9946-42a2-9f1c-446eab2e9ced",
      "name": "Trigger Everyday",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        512,
        352
      ],
      "parameters": {
        "rule": {
          "interval": [
            {}
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "ba237185-bc76-4f4a-b221-d1ce16d52249",
      "name": "Get Low Stock Product",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        720,
        352
      ],
      "parameters": {
        "options": {},
        "filtersUI": {
          "values": [
            {
              "lookupValue": "Low Stock",
              "lookupColumn": "Inventory Status"
            }
          ]
        },
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=0",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/15bLKtbrz1Ic7vJQZ7iIDxkL4rV9mLBpMWdTCZwehGUw/edit#gid=0",
          "cachedResultName": "Inventory_Stock"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "15bLKtbrz1Ic7vJQZ7iIDxkL4rV9mLBpMWdTCZwehGUw",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/15bLKtbrz1Ic7vJQZ7iIDxkL4rV9mLBpMWdTCZwehGUw/edit?usp=drivesdk",
          "cachedResultName": "Inventory Management"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "510e966f-e067-414d-ad62-adff4b391e41",
      "name": "Get Purchase Order",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        1888,
        368
      ],
      "parameters": {
        "options": {},
        "filtersUI": {
          "values": [
            {
              "lookupValue": "={{ $json.sku }}",
              "lookupColumn": "SKU"
            },
            {
              "lookupValue": "In Progress",
              "lookupColumn": "Order Status"
            }
          ]
        },
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": 1455016343,
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/15bLKtbrz1Ic7vJQZ7iIDxkL4rV9mLBpMWdTCZwehGUw/edit#gid=1455016343",
          "cachedResultName": "Purchase Order"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "15bLKtbrz1Ic7vJQZ7iIDxkL4rV9mLBpMWdTCZwehGUw",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/15bLKtbrz1Ic7vJQZ7iIDxkL4rV9mLBpMWdTCZwehGUw/edit?usp=drivesdk",
          "cachedResultName": "Inventory Management"
        }
      },
      "typeVersion": 4.7,
      "alwaysOutputData": true
    },
    {
      "id": "af60df87-c6c7-442b-879f-67ce6d0a07a9",
      "name": "Check Record Exists Or Not",
      "type": "n8n-nodes-base.if",
      "position": [
        2128,
        368
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "f68f512d-48ad-44f6-bbcf-36646c290250",
              "operator": {
                "type": "string",
                "operation": "exists",
                "singleValue": true
              },
              "leftValue": "={{ $json['Order Status'] }}",
              "rightValue": 1
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "ebf5adbf-73d5-49c6-abfb-1e074f969dce",
      "name": "Store a Purchase Order",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        2368,
        464
      ],
      "parameters": {
        "columns": {
          "value": {
            "SKU": "={{ $('Generate Email & PO').item.json.sku }}",
            "Supplier": "={{ $('Generate Email & PO').item.json.supplier }}",
            "Available": "={{ $('Generate Email & PO').item.json.available }}",
            "Item Name": "={{ $('Generate Email & PO').item.json.item_name }}",
            "Order Qty": "={{ $('Switch Email & PO').item.json.order_qty }}",
            "PO Number": "={{ $('Generate Email & PO').item.json.po_number }}",
            "Order Date": "={{ $('Generate Email & PO').item.json.order_date }}",
            "Order Status": "In Progress",
            "Supplier Email": "={{ $('Generate Email & PO').item.json.supplier_email }}"
          },
          "schema": [
            {
              "id": "PO Number",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "PO Number",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Supplier",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Supplier",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Supplier Email",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Supplier Email",
              "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": "Available",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Available",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Order Qty",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Order Qty",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Order Date",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Order Date",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Order Status",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Order Status",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": 1455016343,
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/15bLKtbrz1Ic7vJQZ7iIDxkL4rV9mLBpMWdTCZwehGUw/edit#gid=1455016343",
          "cachedResultName": "Purchase Order"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "15bLKtbrz1Ic7vJQZ7iIDxkL4rV9mLBpMWdTCZwehGUw",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/15bLKtbrz1Ic7vJQZ7iIDxkL4rV9mLBpMWdTCZwehGUw/edit?usp=drivesdk",
          "cachedResultName": "Inventory Management"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "88439e45-5488-4649-a91c-34902479127d",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        656,
        176
      ],
      "parameters": {
        "color": 7,
        "width": 876,
        "height": 424,
        "content": "## Collect Low stock products\n- Code node checks product stock meets the minimum threshold or not.\n- Email created based on new stock and product details."
      },
      "typeVersion": 1
    },
    {
      "id": "9aec0f0a-4923-4b38-8863-55b9dbdff0a7",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -112,
        224
      ],
      "parameters": {
        "width": 578,
        "height": 288,
        "content": "## How it works\nThis workflow automates inventory procurement by checking stock levels daily, calculating reorder quantities, and sending purchase orders to suppliers via email while logging them to Google Sheets.\n\n## Setup steps\n1. Connect your Google Sheets account (use the sample sheet as template)\n2. Connect your Gmail account for sending supplier emails\n3. Update the Google Sheet document ID if using your own sheet\n4. Adjust the schedule trigger timing as needed\n5. Test with a few low-stock items before going live"
      },
      "typeVersion": 1
    },
    {
      "id": "235522da-4a84-42b0-beb0-12d20fe147ea",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1552,
        80
      ],
      "parameters": {
        "color": 7,
        "width": 1072,
        "height": 608,
        "content": "## Data Processing\n- If already record created then skip otherwise add new record in spreadsheets."
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "Order Handling": {
      "main": [
        [
          {
            "node": "Generate Email & PO",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [],
        [
          {
            "node": "Get Purchase Order",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Trigger Everyday": {
      "main": [
        [
          {
            "node": "Get Low Stock Product",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch Email & PO": {
      "main": [
        [
          {
            "node": "Send Order to Suplier",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Purchase Order": {
      "main": [
        [
          {
            "node": "Check Record Exists Or Not",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Email & PO": {
      "main": [
        [
          {
            "node": "Switch Email & PO",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Low Stock Product": {
      "main": [
        [
          {
            "node": "Order Handling",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Store a Purchase Order": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "No Operation, do nothing": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Record Exists Or Not": {
      "main": [
        [
          {
            "node": "No Operation, do nothing",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Store a Purchase Order",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}