{
  "name": "10 - Automated Financial Reporting Dashboard",
  "nodes": [
    {
      "id": "node-1",
      "name": "Schedule - Every Monday 8AM",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.1,
      "position": [240, 300],
      "parameters": {
        "rule": { "interval": [{ "field": "cronExpression", "expression": "0 8 * * 1" }] }
      }
    },
    {
      "id": "node-2",
      "name": "Stripe - Get Weekly Charges",
      "type": "n8n-nodes-base.stripe",
      "typeVersion": 1,
      "position": [480, 160],
      "credentials": { "stripeApi": { "id": "YOUR_STRIPE_CREDENTIAL_ID", "name": "Stripe account" } },
      "parameters": {
        "resource": "charge",
        "operation": "getAll",
        "returnAll": false,
        "limit": 100
      }
    },
    {
      "id": "node-3",
      "name": "Stripe - Get Customers",
      "type": "n8n-nodes-base.stripe",
      "typeVersion": 1,
      "position": [480, 300],
      "credentials": { "stripeApi": { "id": "YOUR_STRIPE_CREDENTIAL_ID", "name": "Stripe account" } },
      "parameters": {
        "resource": "customer",
        "operation": "getAll",
        "returnAll": false,
        "limit": 100
      }
    },
    {
      "id": "node-4",
      "name": "QuickBooks - Get Expenses",
      "type": "n8n-nodes-base.quickbooks",
      "typeVersion": 1,
      "position": [480, 440],
      "credentials": { "quickBooksOAuth2Api": { "id": "YOUR_QB_CREDENTIAL_ID", "name": "QuickBooks account" } },
      "parameters": {
        "resource": "purchase",
        "operation": "getAll",
        "returnAll": false,
        "limit": 100
      }
    },
    {
      "id": "node-5",
      "name": "Code - Calculate KPIs",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [720, 300],
      "parameters": {
        "jsCode": "const charges = $('Stripe - Get Weekly Charges').all().map(i => i.json);\nconst customers = $('Stripe - Get Customers').all().map(i => i.json);\nconst purchases = $('QuickBooks - Get Expenses').all().map(i => i.json);\n\nconst grossRevenue = charges\n  .filter(c => c.paid === true)\n  .reduce((sum, c) => sum + (c.amount || 0), 0) / 100;\n\nconst totalRefunded = charges\n  .reduce((sum, c) => sum + (c.amount_refunded || 0), 0) / 100;\n\nconst netRevenue = grossRevenue - totalRefunded;\n\nconst totalExpenses = purchases\n  .reduce((sum, p) => sum + parseFloat(p.TotalAmt || 0), 0);\n\nconst grossProfit = netRevenue - totalExpenses;\nconst profitMargin = netRevenue > 0 ? ((grossProfit / netRevenue) * 100).toFixed(1) : 0;\n\nconst recurringCharges = charges.filter(c => c.invoice);\nconst estimatedMRR = recurringCharges.reduce((s, c) => s + c.amount, 0) / 100;\n\nreturn [{\n  json: {\n    report_period: `Week of ${new Date().toLocaleDateString('en-US', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' })}`,\n    gross_revenue: grossRevenue.toFixed(2),\n    total_refunded: totalRefunded.toFixed(2),\n    net_revenue: netRevenue.toFixed(2),\n    total_expenses: totalExpenses.toFixed(2),\n    gross_profit: grossProfit.toFixed(2),\n    profit_margin_pct: profitMargin,\n    total_customers: customers.length,\n    total_transactions: charges.filter(c => c.paid).length,\n    estimated_mrr: estimatedMRR.toFixed(2),\n    estimated_arr: (estimatedMRR * 12).toFixed(2),\n    generated_at: new Date().toISOString()\n  }\n}];"
      }
    },
    {
      "id": "node-6",
      "name": "OpenAI - Write AI Commentary",
      "type": "n8n-nodes-base.openAi",
      "typeVersion": 1.8,
      "position": [960, 300],
      "credentials": { "openAiApi": { "id": "YOUR_OPENAI_CREDENTIAL_ID", "name": "OpenAI account" } },
      "parameters": {
        "resource": "chat",
        "operation": "complete",
        "modelId": { "__rl": true, "value": "gpt-4o", "mode": "list", "cachedResultName": "gpt-4o" },
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "You are a CFO writing a concise weekly financial commentary. Be analytical, highlight risks and wins. Write 3-4 sentences."
            },
            {
              "role": "user",
              "content": "=Write a financial commentary for: Period={{ $json.report_period }}, Gross Revenue=${{ $json.gross_revenue }}, Net Revenue=${{ $json.net_revenue }}, Expenses=${{ $json.total_expenses }}, Gross Profit=${{ $json.gross_profit }}, Margin={{ $json.profit_margin_pct }}%, Customers={{ $json.total_customers }}, MRR=${{ $json.estimated_mrr }}, ARR=${{ $json.estimated_arr }}"
            }
          ]
        },
        "options": { "temperature": 0.4 }
      }
    },
    {
      "id": "node-7",
      "name": "Code - Build Report Text",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [1200, 300],
      "parameters": {
        "jsCode": "const kpis = $('Code - Calculate KPIs').first().json;\nconst aiComment = $input.first().json.message?.content || 'Commentary unavailable';\nconst d = '='.repeat(44);\nconst report = `WEEKLY FINANCIAL REPORT\\n${kpis.report_period}\\n${d}\\n\\n💰 REVENUE\\n  Gross Revenue:    $${kpis.gross_revenue}\\n  Refunds:          $${kpis.total_refunded}\\n  Net Revenue:      $${kpis.net_revenue}\\n\\n💸 EXPENSES & PROFIT\\n  Total Expenses:   $${kpis.total_expenses}\\n  Gross Profit:     $${kpis.gross_profit}\\n  Profit Margin:    ${kpis.profit_margin_pct}%\\n\\n📈 RECURRING\\n  Est. MRR:         $${kpis.estimated_mrr}\\n  Est. ARR:         $${kpis.estimated_arr}\\n\\n👥 CUSTOMERS\\n  Total:            ${kpis.total_customers}\\n  Transactions:     ${kpis.total_transactions}\\n\\n💡 AI COMMENTARY\\n${aiComment}\\n\\n${d}\\nGenerated: ${kpis.generated_at}`;\nreturn [{ json: { ...kpis, ai_commentary: aiComment, full_report: report } }];"
      }
    },
    {
      "id": "node-8",
      "name": "Google Sheets - Log KPIs",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [1440, 180],
      "credentials": { "googleSheetsOAuth2Api": { "id": "YOUR_GOOGLE_SHEETS_CREDENTIAL_ID", "name": "Google Sheets account" } },
      "parameters": {
        "resource": "sheet",
        "operation": "appendOrUpdate",
        "documentId": { "__rl": true, "value": "YOUR_GOOGLE_SPREADSHEET_ID", "mode": "id" },
        "sheetName": { "__rl": true, "value": "Weekly_KPIs", "mode": "name" },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Period": "={{ $json.report_period }}",
            "Gross Revenue": "={{ $json.gross_revenue }}",
            "Net Revenue": "={{ $json.net_revenue }}",
            "Total Expenses": "={{ $json.total_expenses }}",
            "Gross Profit": "={{ $json.gross_profit }}",
            "Profit Margin %": "={{ $json.profit_margin_pct }}",
            "Est MRR": "={{ $json.estimated_mrr }}",
            "Est ARR": "={{ $json.estimated_arr }}",
            "Customers": "={{ $json.total_customers }}",
            "Transactions": "={{ $json.total_transactions }}",
            "Generated At": "={{ $json.generated_at }}"
          }
        },
        "options": {}
      }
    },
    {
      "id": "node-9",
      "name": "Gmail - Email Report to Leadership",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [1440, 420],
      "credentials": { "gmailOAuth2": { "id": "YOUR_GMAIL_CREDENTIAL_ID", "name": "Gmail account" } },
      "parameters": {
        "resource": "message",
        "operation": "send",
        "sendTo": "cfo@yourcompany.com, ceo@yourcompany.com",
        "subject": "=📈 Weekly Financial Report — {{ $json.report_period }}",
        "message": "={{ $json.full_report }}",
        "options": {}
      }
    },
    {
      "id": "node-10",
      "name": "HTTP - Post Digest to Slack",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [1680, 300],
      "parameters": {
        "method": "POST",
        "url": "YOUR_SLACK_WEBHOOK_URL",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"blocks\": [\n    { \"type\": \"header\", \"text\": { \"type\": \"plain_text\", \"text\": \"📈 Weekly Financial Snapshot\" } },\n    {\n      \"type\": \"section\",\n      \"fields\": [\n        { \"type\": \"mrkdwn\", \"text\": \"*Net Revenue*\\n${{ $json.net_revenue }}\" },\n        { \"type\": \"mrkdwn\", \"text\": \"*Gross Profit*\\n${{ $json.gross_profit }}\" },\n        { \"type\": \"mrkdwn\", \"text\": \"*Profit Margin*\\n{{ $json.profit_margin_pct }}%\" },\n        { \"type\": \"mrkdwn\", \"text\": \"*Est. MRR*\\n${{ $json.estimated_mrr }}\" },\n        { \"type\": \"mrkdwn\", \"text\": \"*Customers*\\n{{ $json.total_customers }}\" },\n        { \"type\": \"mrkdwn\", \"text\": \"*Transactions*\\n{{ $json.total_transactions }}\" }\n      ]\n    },\n    { \"type\": \"section\", \"text\": { \"type\": \"mrkdwn\", \"text\": \"*AI Commentary:* {{ $json.ai_commentary }}\" } },\n    { \"type\": \"context\", \"elements\": [{ \"type\": \"mrkdwn\", \"text\": \"_Full report emailed to leadership. Google Sheets dashboard updated._\" }] }\n  ]\n}"
      }
    }
  ],
  "connections": {
    "Schedule - Every Monday 8AM": {
      "main": [[
        { "node": "Stripe - Get Weekly Charges", "type": "main", "index": 0 },
        { "node": "Stripe - Get Customers", "type": "main", "index": 0 },
        { "node": "QuickBooks - Get Expenses", "type": "main", "index": 0 }
      ]]
    },
    "Stripe - Get Weekly Charges": {
      "main": [[{ "node": "Code - Calculate KPIs", "type": "main", "index": 0 }]]
    },
    "Stripe - Get Customers": {
      "main": [[{ "node": "Code - Calculate KPIs", "type": "main", "index": 0 }]]
    },
    "QuickBooks - Get Expenses": {
      "main": [[{ "node": "Code - Calculate KPIs", "type": "main", "index": 0 }]]
    },
    "Code - Calculate KPIs": {
      "main": [[{ "node": "OpenAI - Write AI Commentary", "type": "main", "index": 0 }]]
    },
    "OpenAI - Write AI Commentary": {
      "main": [[{ "node": "Code - Build Report Text", "type": "main", "index": 0 }]]
    },
    "Code - Build Report Text": {
      "main": [[
        { "node": "Google Sheets - Log KPIs", "type": "main", "index": 0 },
        { "node": "Gmail - Email Report to Leadership", "type": "main", "index": 0 }
      ]]
    },
    "Gmail - Email Report to Leadership": {
      "main": [[{ "node": "HTTP - Post Digest to Slack", "type": "main", "index": 0 }]]
    }
  },
  "settings": { "executionOrder": "v1" },
  "active": false,
  "tags": [{ "name": "Portfolio" }, { "name": "Finance" }, { "name": "AI" }]
}
