{
  "id": "FWqoxDXJ4tpzAA7Q",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "REAL-TIME PROCUREMENT SPEND INTELLIGENCE DASHBOARD",
  "tags": [],
  "nodes": [
    {
      "id": "18c99019-29cf-47bf-b971-6d27cd9bb8fe",
      "name": "Fetch Department Budgets",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        -64,
        224
      ],
      "parameters": {
        "options": {},
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": 604781030,
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1A8fW4VPSC5Z-h-bXW_wGBHQaHIAUpjqJP8oNHcCsVzo/edit#gid=604781030",
          "cachedResultName": "department_budgets"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1A8fW4VPSC5Z-h-bXW_wGBHQaHIAUpjqJP8oNHcCsVzo",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1A8fW4VPSC5Z-h-bXW_wGBHQaHIAUpjqJP8oNHcCsVzo/edit?usp=drivesdk",
          "cachedResultName": "Procurement Spend Intelligence System"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "executeOnce": false,
      "typeVersion": 4.7
    },
    {
      "id": "c22078dc-5ba2-457c-9ee2-f8abe78a3736",
      "name": "Spend Intelligence Engine",
      "type": "n8n-nodes-base.code",
      "position": [
        384,
        128
      ],
      "parameters": {
        "jsCode": "const departmentMap = {};\n\n// Current day of month for forecasting\nconst currentDay = new Date().getDate();\n\nfor (const item of items) {\n\n  const row = item.json;\n  const dept = row.department;\n\n  // Initialize department aggregation\n  if (!departmentMap[dept]) {\n\n    departmentMap[dept] = {\n      department: dept,\n      monthly_budget: row.monthly_budget,\n      warning_threshold: row.warning_threshold,\n      critical_threshold: row.critical_threshold,\n      budget_owner: row.budget_owner,\n\n      total_spend: 0,\n      transaction_count: 0,\n      vendors: new Set(),\n      pending_transactions: 0\n    };\n  }\n\n  // Aggregate values\n  departmentMap[dept].total_spend += row.amount;\n  departmentMap[dept].transaction_count += 1;\n\n  // Unique vendors\n  departmentMap[dept].vendors.add(row.vendor);\n\n  // Pending transactions\n  if (row.payment_status === \"Pending\") {\n    departmentMap[dept].pending_transactions += 1;\n  }\n}\n\n// Final calculations\nconst output = [];\n\nfor (const dept in departmentMap) {\n\n  const data = departmentMap[dept];\n\n  const budgetUtilization =\n    (data.total_spend / data.monthly_budget) * 100;\n\n  const remainingBudget =\n    data.monthly_budget - data.total_spend;\n\n  const avgDailySpend =\n    data.total_spend / currentDay;\n\n  const projectedMonthlySpend =\n    avgDailySpend * 30;\n\n  const forecastDaysRemaining =\n    avgDailySpend > 0\n      ? Math.max(Math.floor(remainingBudget / avgDailySpend),0)\n      : null;\n\n  // Risk classification\n  let riskLevel = \"SAFE\";\n\n  if (budgetUtilization >= data.critical_threshold) {\n    riskLevel = \"CRITICAL\";\n  }\n  else if (budgetUtilization >= data.warning_threshold) {\n    riskLevel = \"WARNING\";\n  }\n\n  output.push({\n    json: {\n\n      department: data.department,\n      budget_owner: data.budget_owner,\n\n      total_spend: data.total_spend,\n      monthly_budget: data.monthly_budget,\n\n      budget_utilization:\n        Number(budgetUtilization.toFixed(2)),\n\n      remaining_budget:\n        remainingBudget,\n\n      avg_daily_spend:\n        Number(avgDailySpend.toFixed(2)),\n\n      projected_monthly_spend:\n        Number(projectedMonthlySpend.toFixed(2)),\n\n      forecast_days_remaining:\n        forecastDaysRemaining,\n\n      transaction_count:\n        data.transaction_count,\n\n      unique_vendors:\n        data.vendors.size,\n\n      pending_transactions:\n        data.pending_transactions,\n\n      risk_level:\n        riskLevel,\n\n      generated_at:\n        new Date().toISOString()\n    }\n  });\n}\n\nreturn output;\n"
      },
      "typeVersion": 2
    },
    {
      "id": "c8ac44b3-a2aa-4b99-be08-031ba793e623",
      "name": "Groq Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGroq",
      "position": [
        680,
        352
      ],
      "parameters": {
        "model": "llama-3.1-8b-instant",
        "options": {
          "temperature": 0.3,
          "maxTokensToSample": 500
        }
      },
      "credentials": {
        "groqApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "bc50ea42-1acb-4cca-9748-134d7a310528",
      "name": "Generate Executive Dashboard Metrics",
      "type": "n8n-nodes-base.code",
      "position": [
        960,
        128
      ],
      "parameters": {
        "jsCode": "const spendData = $('Spend Intelligence Engine').all();\n\nconst aiData = items;\n\nlet totalSpend = 0;\nlet totalProjectedSpend = 0;\n\nlet criticalAlerts = 0;\nlet warningAlerts = 0;\n\nlet highestSpendDept = null;\nlet highestSpendValue = 0;\n\nlet topRiskDept = null;\n\nconst departmentSummaries = [];\n\n// Loop through all departments\nfor (let i = 0; i < spendData.length; i++) {\n\n  const spend =\n    spendData[i].json;\n\n  const ai =\n    aiData[i].json;\n\n  // Organization totals\n  totalSpend += spend.total_spend || 0;\n\n  totalProjectedSpend +=\n    spend.projected_monthly_spend || 0;\n\n  // Highest spend department\n  if ((spend.total_spend || 0) > highestSpendValue) {\n\n    highestSpendValue =\n      spend.total_spend;\n\n    highestSpendDept =\n      spend.department;\n  }\n\n  // Risk tracking\n  if (spend.risk_level === \"CRITICAL\") {\n\n    criticalAlerts += 1;\n\n    if (!topRiskDept) {\n      topRiskDept = spend.department;\n    }\n  }\n\n  if (spend.risk_level === \"WARNING\") {\n    warningAlerts += 1;\n  }\n\n  // Department summaries\n  departmentSummaries.push({\n\n    department:\n      spend.department,\n\n    budget_owner:\n      spend.budget_owner,\n\n    total_spend:\n      spend.total_spend,\n\n    monthly_budget:\n      spend.monthly_budget,\n\n    budget_utilization:\n      spend.budget_utilization,\n\n    remaining_budget:\n      spend.remaining_budget,\n\n    projected_monthly_spend:\n      spend.projected_monthly_spend,\n\n    forecast_days_remaining:\n      spend.forecast_days_remaining,\n\n    pending_transactions:\n      spend.pending_transactions,\n\n    unique_vendors:\n      spend.unique_vendors,\n\n    risk_level:\n      spend.risk_level,\n\n    ai_summary:\n      ai.output\n  });\n}\n\n// Organization risk\nlet overallRisk = \"SAFE\";\n\nif (criticalAlerts > 0) {\n\n  overallRisk = \"CRITICAL\";\n\n} else if (warningAlerts > 0) {\n\n  overallRisk = \"WARNING\";\n}\n\n// Final executive dashboard object\nreturn [\n  {\n    json: {\n\n      generated_at:\n        new Date().toISOString(),\n\n      organization_summary: {\n\n        total_organizational_spend:\n          Number(totalSpend.toFixed(2)),\n\n        total_projected_spend:\n          Number(totalProjectedSpend.toFixed(2)),\n\n        highest_spend_department:\n          highestSpendDept,\n\n        highest_department_spend:\n          highestSpendValue,\n\n        critical_alerts:\n          criticalAlerts,\n\n        warning_alerts:\n          warningAlerts,\n\n        top_risk_department:\n          topRiskDept,\n\n        overall_procurement_risk:\n          overallRisk,\n\n        department_count:\n          spendData.length\n      },\n\n      department_summaries:\n        departmentSummaries\n    }\n  }\n];"
      },
      "typeVersion": 2
    },
    {
      "id": "9bd87f9c-3f0c-4839-b78b-139804928516",
      "name": "Transform Dashboard For Sheets",
      "type": "n8n-nodes-base.code",
      "position": [
        1408,
        128
      ],
      "parameters": {
        "jsCode": "const dashboard = items[0].json;\n\nconst results = [];\n\n/*\n========================================\nEXECUTIVE SUMMARY RECORD\n========================================\n*/\n\nresults.push({\n  json: {\n\n    record_type:\n      \"executive_summary\",\n\n    generated_at:\n      dashboard.generated_at,\n\n    total_organizational_spend:\n      dashboard.organization_summary.total_organizational_spend,\n\n    total_projected_spend:\n      dashboard.organization_summary.total_projected_spend,\n\n    highest_spend_department:\n      dashboard.organization_summary.highest_spend_department,\n\n    highest_department_spend:\n      dashboard.organization_summary.highest_department_spend,\n\n    critical_alerts:\n      dashboard.organization_summary.critical_alerts,\n\n    warning_alerts:\n      dashboard.organization_summary.warning_alerts,\n\n    top_risk_department:\n      dashboard.organization_summary.top_risk_department,\n\n    overall_procurement_risk:\n      dashboard.organization_summary.overall_procurement_risk,\n\n    department_count:\n      dashboard.organization_summary.department_count\n  }\n});\n\n/*\n========================================\nDEPARTMENT INSIGHT RECORDS\n========================================\n*/\n\nfor (const dept of dashboard.department_summaries) {\n\n  results.push({\n    json: {\n\n      record_type:\n        \"department_insight\",\n\n      generated_at:\n        dashboard.generated_at,\n\n      department:\n        dept.department,\n\n      budget_owner:\n        dept.budget_owner,\n\n      total_spend:\n        dept.total_spend,\n\n      monthly_budget:\n        dept.monthly_budget,\n\n      budget_utilization:\n        dept.budget_utilization,\n\n      remaining_budget:\n        dept.remaining_budget,\n\n      projected_monthly_spend:\n        dept.projected_monthly_spend,\n\n      forecast_days_remaining:\n        dept.forecast_days_remaining,\n\n      pending_transactions:\n        dept.pending_transactions,\n\n      unique_vendors:\n        dept.unique_vendors,\n\n      risk_level:\n        dept.risk_level,\n\n      ai_summary:\n        dept.ai_summary\n    }\n  });\n}\n\n/*\n========================================\nRISK ALERT RECORDS\n========================================\n*/\n\nfor (const dept of dashboard.department_summaries) {\n\n  if (\n    dept.risk_level === \"CRITICAL\" ||\n    dept.risk_level === \"WARNING\"\n  ) {\n\n    results.push({\n      json: {\n\n        record_type:\n          \"risk_alert\",\n\n        generated_at:\n          dashboard.generated_at,\n\n        department:\n          dept.department,\n\n        risk_level:\n          dept.risk_level,\n\n        budget_utilization:\n          dept.budget_utilization,\n\n        projected_monthly_spend:\n          dept.projected_monthly_spend,\n\n        remaining_budget:\n          dept.remaining_budget,\n\n        ai_summary:\n          dept.ai_summary\n      }\n    });\n  }\n}\n\nreturn results;"
      },
      "typeVersion": 2
    },
    {
      "id": "47fc4c10-43b5-45c8-af50-7e474d1d4176",
      "name": "Store Executive Summary",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        1856,
        -64
      ],
      "parameters": {
        "columns": {
          "value": {},
          "schema": [
            {
              "id": "record_type",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "record_type",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "generated_at",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "generated_at",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "total_organizational_spend",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "total_organizational_spend",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "total_projected_spend",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "total_projected_spend",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "highest_spend_department",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "highest_spend_department",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "highest_department_spend",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "highest_department_spend",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "critical_alerts",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "critical_alerts",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "warning_alerts",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "warning_alerts",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "top_risk_department",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "top_risk_department",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "overall_procurement_risk",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "overall_procurement_risk",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "department_count",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "department_count",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "autoMapInputData",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": 125344822,
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1A8fW4VPSC5Z-h-bXW_wGBHQaHIAUpjqJP8oNHcCsVzo/edit#gid=125344822",
          "cachedResultName": "Executive_Summary"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1A8fW4VPSC5Z-h-bXW_wGBHQaHIAUpjqJP8oNHcCsVzo",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1A8fW4VPSC5Z-h-bXW_wGBHQaHIAUpjqJP8oNHcCsVzo/edit?usp=drivesdk",
          "cachedResultName": "Procurement Spend Intelligence System"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "97afd0c2-5c6e-4b23-920d-55be5789d2e8",
      "name": "Store Department Insights",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        1856,
        128
      ],
      "parameters": {
        "columns": {
          "value": {},
          "schema": [
            {
              "id": "record_type",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "record_type",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "generated_at",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "generated_at",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "department",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "department",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "budget_owner",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "budget_owner",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "total_spend",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "total_spend",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "monthly_budget",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "monthly_budget",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "budget_utilization",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "budget_utilization",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "remaining_budget",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "remaining_budget",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "projected_monthly_spend",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "projected_monthly_spend",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "forecast_days_remaining",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "forecast_days_remaining",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "pending_transactions",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "pending_transactions",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "unique_vendors",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "unique_vendors",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "risk_level",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "risk_level",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "ai_summary",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "ai_summary",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "autoMapInputData",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": 1772699852,
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1A8fW4VPSC5Z-h-bXW_wGBHQaHIAUpjqJP8oNHcCsVzo/edit#gid=1772699852",
          "cachedResultName": "Department_Insights"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1A8fW4VPSC5Z-h-bXW_wGBHQaHIAUpjqJP8oNHcCsVzo",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1A8fW4VPSC5Z-h-bXW_wGBHQaHIAUpjqJP8oNHcCsVzo/edit?usp=drivesdk",
          "cachedResultName": "Procurement Spend Intelligence System"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "a25ae3bb-78aa-4a13-9846-efe7899cc5f2",
      "name": "Store Risk Alerts",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        1856,
        320
      ],
      "parameters": {
        "columns": {
          "value": {},
          "schema": [
            {
              "id": "record_type",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "record_type",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "generated_at",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "generated_at",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "department",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "department",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "risk_level",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "risk_level",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "budget_utilization",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "budget_utilization",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "projected_monthly_spend",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "projected_monthly_spend",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "remaining_budget",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "remaining_budget",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "ai_summary",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "ai_summary",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "autoMapInputData",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": 1236756798,
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1A8fW4VPSC5Z-h-bXW_wGBHQaHIAUpjqJP8oNHcCsVzo/edit#gid=1236756798",
          "cachedResultName": "Risk_Alerts_Dashboard"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1A8fW4VPSC5Z-h-bXW_wGBHQaHIAUpjqJP8oNHcCsVzo",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1A8fW4VPSC5Z-h-bXW_wGBHQaHIAUpjqJP8oNHcCsVzo/edit?usp=drivesdk",
          "cachedResultName": "Procurement Spend Intelligence System"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "54bda381-5af2-4d02-b1b6-5f78780c1126",
      "name": "Route Dashboard Records",
      "type": "n8n-nodes-base.switch",
      "position": [
        1632,
        112
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "version": 3,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "dc6b5917-ba61-4afb-87d7-7831279fa037",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.record_type }}",
                    "rightValue": "executive_summary"
                  }
                ]
              }
            },
            {
              "conditions": {
                "options": {
                  "version": 3,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "d978daf8-a886-47e1-980f-0ec4b723c97d",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.record_type }}",
                    "rightValue": "department_insight"
                  }
                ]
              }
            },
            {
              "conditions": {
                "options": {
                  "version": 3,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "8eb23df3-904f-4c3b-a2c5-d2c8cf5102c0",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.record_type }}",
                    "rightValue": "risk_alert"
                  }
                ]
              }
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3.4
    },
    {
      "id": "0dd986f8-3530-4c49-9e40-ec7d6e141646",
      "name": "No Operation, do nothing",
      "type": "n8n-nodes-base.noOp",
      "position": [
        2384,
        416
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "75918064-40ea-4e2a-83d2-56ed735d9211",
      "name": "Procurement Spend Scheduler",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -288,
        128
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 15
            }
          ]
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "eee8fea9-b4f0-4e15-90a1-b84d3bd8de28",
      "name": "Fetch Procurement Transactions",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        -64,
        32
      ],
      "parameters": {
        "options": {},
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=0",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1A8fW4VPSC5Z-h-bXW_wGBHQaHIAUpjqJP8oNHcCsVzo/edit#gid=0",
          "cachedResultName": "raw_spend_data"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1A8fW4VPSC5Z-h-bXW_wGBHQaHIAUpjqJP8oNHcCsVzo",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1A8fW4VPSC5Z-h-bXW_wGBHQaHIAUpjqJP8oNHcCsVzo/edit?usp=drivesdk",
          "cachedResultName": "Procurement Spend Intelligence System"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "00f4c3bf-3001-4f7d-99c0-73834c258b5e",
      "name": "Merge Spend & Budget Data",
      "type": "n8n-nodes-base.merge",
      "position": [
        160,
        128
      ],
      "parameters": {
        "mode": "combine",
        "options": {},
        "fieldsToMatchString": "department"
      },
      "typeVersion": 3.2
    },
    {
      "id": "1214c0a7-f7f5-4af4-8ae1-c369a13e5a28",
      "name": "AI Procurement Risk Analyzer",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        608,
        128
      ],
      "parameters": {
        "text": "=You are a senior enterprise procurement intelligence advisor.\n\nAnalyze this department's procurement spend metrics and generate executive-level business insights.\n\nDepartment: {{$json.department}}\n\nMetrics:\n- Total Spend: {{$json.total_spend}}\n- Monthly Budget: {{$json.monthly_budget}}\n- Budget Utilization: {{$json.budget_utilization}}%\n- Remaining Budget: {{$json.remaining_budget}}\n- Projected Monthly Spend: {{$json.projected_monthly_spend}}\n- Forecast Days Remaining: {{$json.forecast_days_remaining}}\n- Pending Transactions: {{$json.pending_transactions}}\n- Unique Vendors: {{$json.unique_vendors}}\n- Risk Level: {{$json.risk_level}}\n\nRisk Evaluation Rules:\n- Below 75% utilization = financially stable\n- 75% to 89% = warning level\n- 90% or above = critical\n- Do not exaggerate risks for SAFE departments\n- Recommendations must align with actual risk level\n\nGenerate output in this exact structure:\n\nExecutive Summary:\n(short summary)\n\nRisk Analysis:\n(main risk explanation)\n\nRecommendation:\n(procurement action)\n\nOptimization Advice:\n(cost optimization suggestion)\n\nKeep response concise, professional, and executive-friendly.",
        "options": {
          "systemMessage": "You are a senior enterprise procurement intelligence advisor.\n\nYour role:\n- Analyze procurement spending risks\n- Identify budget anomalies\n- Recommend procurement actions\n- Suggest cost optimization opportunities\n\nAlways produce:\n- concise\n- executive-level\n- structured\n- professional responses\n\nAvoid markdown.\nAvoid bullet overload.\nKeep insights actionable."
        },
        "promptType": "define"
      },
      "typeVersion": 3.1
    },
    {
      "id": "908fca0d-420a-4bad-9e49-32d3fc891bb0",
      "name": "Critical Risk Detected",
      "type": "n8n-nodes-base.if",
      "position": [
        2144,
        320
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 3,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "67f149c4-0f4e-416f-956e-3e761b2cfd1e",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.risk_level }}",
              "rightValue": "CRITICAL"
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "6984eef2-70f9-4ced-b1d1-6a9d1d8c0427",
      "name": "Send Critical Procurement Alert",
      "type": "n8n-nodes-base.gmail",
      "position": [
        2384,
        224
      ],
      "parameters": {
        "sendTo": "user@example.com",
        "message": "=<h2>Critical Procurement Risk Detected</h2>\n\n<p><strong>Department:</strong> {{ $json.department }}</p>\n\n<p><strong>Risk Level:</strong> {{ $json.risk_level }}</p>\n\n<p><strong>Budget Utilization:</strong> {{ $json.budget_utilization }}%</p>\n\n<p><strong>Projected Monthly Spend:</strong> ${{ $json.projected_monthly_spend }}</p>\n\n<p><strong>Remaining Budget:</strong> ${{ $json.remaining_budget }}</p>\n\n<hr>\n\n<h3>AI Procurement Analysis</h3>\n\n<pre>{{ $json.ai_summary }}</pre>\n\n<hr>\n\n<p>Generated Automatically by Procurement Spend Intelligence Workflow</p>",
        "options": {
          "appendAttribution": false
        },
        "subject": "=CRITICAL PROCUREMENT ALERT \u2014 {{ $json.department }}"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "2eac3ed7-8915-4f91-a010-749a5fcd1bb9",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1008,
        -704
      ],
      "parameters": {
        "width": 656,
        "height": 720,
        "content": "## REAL-TIME PROCUREMENT SPEND INTELLIGENCE DASHBOARD\n\nThis workflow automates procurement spend monitoring, executive reporting, AI-driven risk analysis, and real-time budget escalation alerts using Google Sheets, AI analysis, and Gmail notifications.\n\n\n### How it Works:\n1. Scheduler triggers workflow execution automatically.\n2. Procurement transactions and department budgets are fetched from Google Sheets.\n3. Spend Intelligence Engine aggregates departmental spend, calculates budget utilization, projected spend, risk levels, vendor insights, and forecast metrics.\n4. AI Procurement Risk Analyzer generates executive-level procurement risk summaries and optimization recommendations.\n5. Executive dashboard metrics are generated for organization-wide visibility.\n6. Dashboard records are transformed and routed into dedicated reporting sheets:\n   - Executive Summary\n   - Department Insights\n   - Risk Alerts\n7. Critical procurement risks automatically trigger escalation emails to procurement stakeholders.\n\n\n### Setup Steps:\n- Connect Google Sheets credentials\n- Configure Gmail authentication\n- Update spreadsheet IDs and sheet names\n- Configure schedule frequency\n- Update escalation recipients if required\n\nThis workflow is production-ready and designed for scalable procurement analytics automation.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "06296b0e-6c6f-470a-968e-67fec25ec60f",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -336,
        -208
      ],
      "parameters": {
        "color": 7,
        "width": 432,
        "height": 784,
        "content": "## DATA INGESTION LAYER\n\nFetches procurement transactions and budget allocations from Google Sheets on a scheduled interval for real-time spend monitoring."
      },
      "typeVersion": 1
    },
    {
      "id": "ec885335-fa19-479f-ac70-ac2a37cc9948",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        104,
        -208
      ],
      "parameters": {
        "color": 7,
        "width": 416,
        "height": 784,
        "content": "## SPEND INTELLIGENCE ENGINE\n\nMerges transaction and budget datasets, calculates utilization metrics, projected spend, vendor analytics, and procurement risk levels."
      },
      "typeVersion": 1
    },
    {
      "id": "5b33f85a-eb07-405d-ba85-41ee9d33c094",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2096,
        -208
      ],
      "parameters": {
        "color": 7,
        "width": 544,
        "height": 784,
        "content": "## REAL-TIME RISK ESCALATION\n\nDetects critical procurement risks and automatically sends executive escalation alerts through Gmail notifications."
      },
      "typeVersion": 1
    },
    {
      "id": "41db2ed5-1545-489e-b831-650123396bb0",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        528,
        -208
      ],
      "parameters": {
        "color": 7,
        "width": 384,
        "height": 784,
        "content": "## AI PROCUREMENT ANALYSIS\n\nUses Groq LLM to generate executive summaries, procurement risk explanations, optimization strategies, and actionable recommendations.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "765acbec-bc3f-44ca-b003-1c5ac603450f",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1568,
        -208
      ],
      "parameters": {
        "color": 7,
        "width": 512,
        "height": 784,
        "content": "## REPORTING & AUDIT STORAGE\n\nRoutes processed records into dedicated Google Sheets for executive summaries, department insights, and procurement risk tracking."
      },
      "typeVersion": 1
    },
    {
      "id": "27f69f4b-120a-4abb-ba99-ffe33f438017",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        926,
        -208
      ],
      "parameters": {
        "color": 7,
        "width": 628,
        "height": 784,
        "content": "## EXECUTIVE DASHBOARD GENERATION\n\nAdds workflow metadata, reporting context, and execution tagging for standardized executive reporting and audit readiness.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "4601f87c-af09-4be4-b220-5834460ad4ad",
      "name": "Prepare Dashboard Metadata",
      "type": "n8n-nodes-base.set",
      "position": [
        1184,
        128
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "170847df-5792-4815-bea7-d27d06ac308e",
              "name": "workflow_name",
              "type": "string",
              "value": "Procurement Spend Intelligence Dashboard"
            },
            {
              "id": "e4f08598-96b1-40fe-b55c-c0bc075a124f",
              "name": "workflow_version",
              "type": "string",
              "value": "v1.0"
            },
            {
              "id": "f23322b5-f63d-4685-b59f-0e64754c3980",
              "name": "environment",
              "type": "string",
              "value": "Production"
            },
            {
              "id": "8a4b9229-b21c-43b5-9c5e-13c55950ed5f",
              "name": "generated_by",
              "type": "string",
              "value": "n8n Automation Engine"
            },
            {
              "id": "be38cd45-7246-4606-b7b8-8e398bfa25ef",
              "name": "report_type",
              "type": "string",
              "value": "Real-Time Procurement Intelligence"
            }
          ]
        },
        "includeOtherFields": true
      },
      "typeVersion": 3.4
    }
  ],
  "active": false,
  "settings": {
    "binaryMode": "separate",
    "executionOrder": "v1"
  },
  "versionId": "c84e7adf-ec3b-478e-ba09-f7fa5e828926",
  "connections": {
    "Groq Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Procurement Risk Analyzer",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Store Risk Alerts": {
      "main": [
        [
          {
            "node": "Critical Risk Detected",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Critical Risk Detected": {
      "main": [
        [
          {
            "node": "Send Critical Procurement Alert",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No Operation, do nothing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Route Dashboard Records": {
      "main": [
        [
          {
            "node": "Store Executive Summary",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Store Department Insights",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Store Risk Alerts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Department Budgets": {
      "main": [
        [
          {
            "node": "Merge Spend & Budget Data",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Merge Spend & Budget Data": {
      "main": [
        [
          {
            "node": "Spend Intelligence Engine",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Spend Intelligence Engine": {
      "main": [
        [
          {
            "node": "AI Procurement Risk Analyzer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Dashboard Metadata": {
      "main": [
        [
          {
            "node": "Transform Dashboard For Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Procurement Spend Scheduler": {
      "main": [
        [
          {
            "node": "Fetch Procurement Transactions",
            "type": "main",
            "index": 0
          },
          {
            "node": "Fetch Department Budgets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Procurement Risk Analyzer": {
      "main": [
        [
          {
            "node": "Generate Executive Dashboard Metrics",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Procurement Transactions": {
      "main": [
        [
          {
            "node": "Merge Spend & Budget Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Transform Dashboard For Sheets": {
      "main": [
        [
          {
            "node": "Route Dashboard Records",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Executive Dashboard Metrics": {
      "main": [
        [
          {
            "node": "Prepare Dashboard Metadata",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}