{
  "id": "bUnFjkLevdnP1zo7",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Finance Monthly Report with AI Insight",
  "tags": [],
  "nodes": [
    {
      "id": "c9b7416f-8164-4869-8c8a-3ae49f868334",
      "name": "1. Get Finance Transactions (Google Sheets)",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        380,
        140
      ],
      "parameters": {
        "range": "FinanceSummary!A:E",
        "options": {},
        "sheetId": "<YOUR_GOOGLE_SHEET_ID_HERE>"
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "description": "Reads monthly finance transaction data from the specified Google Sheet.",
      "typeVersion": 2
    },
    {
      "id": "6b2dbe34-e3f0-4ca3-b5ca-3a6c3e027ad2",
      "name": "2. Filter Previous Month Transactions",
      "type": "n8n-nodes-base.function",
      "position": [
        600,
        140
      ],
      "parameters": {
        "functionCode": "// Filter transactions for the previous month\nconst rows = items.map(item => item.json);\n\nconst now = new Date();\nconst lastMonthStart = new Date(now.getFullYear(), now.getMonth() - 1, 1); // First day of previous month\nconst lastMonthEnd = new Date(now.getFullYear(), now.getMonth(), 0);   // Last day of previous month\n\nconst filtered = rows.filter(row => {\n  // Ensure 'Date' column exists and is a valid date format\n  if (!row.Date) return false;\n  const date = new Date(row.Date); \n  return date >= lastMonthStart && date <= lastMonthEnd;\n});\n\nreturn filtered.map(row => ({ json: row }));"
      },
      "description": "Filters the raw data to only include transactions from the previous full month.",
      "typeVersion": 1
    },
    {
      "id": "869519eb-0879-4f0d-8a21-4035b69df097",
      "name": "3. Generate AI Financial Insights",
      "type": "n8n-nodes-base.openAi",
      "position": [
        820,
        140
      ],
      "parameters": {
        "model": "gpt-4",
        "prompt": "You are a finance assistant. Analyze the following transaction data. Calculate the total income and total expense for the period. Then, provide 3 concise bullet-point financial insights based on the data. Structure your output clearly:\n\nTotal Income: [Amount]\nTotal Expense: [Amount]\nKey Insights:\n- [Insight 1]\n- [Insight 2]\n- [Insight 3]\n\nTransactions:\n{{JSON.stringify($json, null, 2)}}",
        "options": {},
        "requestOptions": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "description": "Uses OpenAI (GPT-4) to generate a summary of income, expenses, and key financial insights.",
      "typeVersion": 1
    },
    {
      "id": "d8d4588b-9761-4bef-a427-ff3282c1b12e",
      "name": "4. Send Monthly Finance Report Email",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1040,
        140
      ],
      "parameters": {
        "subject": "Monthly Finance Summary and AI Insights",
        "additionalFields": {}
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "description": "Composes and sends the monthly finance report email, including transaction details and AI insights.",
      "typeVersion": 1
    },
    {
      "id": "f7d8accf-7532-4289-b564-4d2e566ff83b",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        160,
        140
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "months"
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "9931672d-4382-490b-8ed8-68f84aba39c2",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        60,
        80
      ],
      "parameters": {
        "color": 3,
        "width": 1220,
        "height": 240,
        "content": "## Flow\n"
      },
      "typeVersion": 1
    },
    {
      "id": "31db7ae7-de61-4b2e-b38b-89d33bf0ee5c",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1300,
        80
      ],
      "parameters": {
        "color": 4,
        "width": 680,
        "height": 1680,
        "content": "# Workflow Documentation: Finance Monthly Report with AI Insight\n\n## Problem\n\nMany businesses struggle with manual, time-consuming financial reporting processes. This often leads to:\n* Delayed access to critical financial insights.\n* Lack of proactive decision-making due to retrospective data analysis.\n* Inaccurate or inconsistent reports due to human error in data compilation.\n* Underutilization of financial data for strategic planning.\n\n## Solution\n\nThis n8n workflow automates the monthly financial reporting process by:\n1.  **Extracting Data:** Automatically pulls transaction data from Google Sheets.\n2.  **Filtering & Processing:** Filters the data to focus on the previous month's transactions.\n3.  **AI-Powered Insights:** Utilizes OpenAI (GPT-4) to analyze filtered data and generate a summary of income, expenses, along with key financial insights.\n4.  **Automated Delivery:** Compiles the raw data (previous month's transactions) and the AI-generated insights into a well-formatted email and sends it to the designated recipients.\n\nThis ensures timely, accurate, and insightful financial reporting with minimal manual effort.\n\n## For Who\n\nThis workflow is ideal for:\n* **Small to Medium-sized Businesses (SMBs):** Who need regular financial oversight but lack dedicated finance teams or extensive ERP systems.\n* **Startup Founders:** Needing quick and reliable financial summaries to monitor burn rate and performance.\n* **Finance Managers/Accountants:** Looking to automate routine reporting tasks to focus on more strategic analysis.\n* **E-commerce Businesses:** To quickly understand monthly revenue and expenditure trends.\n\n## Scope\n\n* **Input Data Source:** Google Sheets (specifically configured for finance transaction data with 'Date', 'Category', 'Description', 'Amount', 'Type' columns).\n* **AI Model:** OpenAI's GPT-4 (requires an OpenAI API Key).\n* **Output Medium:** Email (via Gmail integration).\n* **Frequency:** Designed to run monthly (configurable via Cron trigger).\n* **Output Content:** Previous month's transaction table, total income, total expense, and 3 AI-generated financial insights.\n\n## How to Set Up\n\n1.  **Import Workflow:** Import this JSON into your n8n instance.\n2.  **Configure Google Sheets Node:**\n    * Connect your Google Sheets account credential.\n    * Update `sheetId` to your specific Google Sheet ID.\n    * Ensure your sheet contains relevant columns (e.g., Date, Category, Description, Amount, Type) in the 'FinanceSummary' tab or adjust the `range` if your sheet name/range differs.\n3.  **Configure OpenAI Node:**\n    * Connect your OpenAI API Key credential.\n    * (Optional) Adjust the prompt for different insights or output formats.\n4.  **Configure Gmail Node:**\n    * Connect your Gmail account credential.\n    * Update `fromEmail` and `toEmail` with your desired sender and recipient addresses.\n5.  **Add Cron Trigger Node:**\n    * Add a `Cron` node as the first step.\n    * Configure it to run on the desired schedule (e.g., 'every month', 'on the 1st day of the month').\n6.  **Activate Workflow:** Save and activate the workflow.\n\n**Important Notes:**\n* Ensure your Google Sheet data format is consistent for accurate filtering and AI analysis.\n* Monitor your OpenAI token usage to manage costs.\n* Consider adding error handling (e.g., sending an alert email if the workflow fails)."
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "fbd3aed3-1704-40c9-a85c-55cfec0e33bc",
  "connections": {
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "1. Get Finance Transactions (Google Sheets)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "3. Generate AI Financial Insights": {
      "main": [
        [
          {
            "node": "4. Send Monthly Finance Report Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "2. Filter Previous Month Transactions": {
      "main": [
        [
          {
            "node": "3. Generate AI Financial Insights",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "1. Get Finance Transactions (Google Sheets)": {
      "main": [
        [
          {
            "node": "2. Filter Previous Month Transactions",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}