8
n8n 中文网amn8n.com

自动通过 Outlook 发送 Square 月度销售报告

高级

这是一个Document Extraction, Multimodal AI领域的自动化工作流,包含 16 个节点。主要使用 If, Code, SplitOut, HttpRequest, ConvertToFile 等节点。 自动通过 Outlook 发送 Square 月度销售报告

前置要求
  • 可能需要目标 API 的认证凭证
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "meta": {
    "instanceId": "d6e2f2f655b1125bbcac14a4cac6d2e46c7a150e927f85fc96fdca1a6dc39e0e",
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "850021bd-91b4-4a19-9bcc-9727131ac584",
      "name": "- 检索包含 ETF ISIN 的表行",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1312,
        544
      ],
      "parameters": {
        "url": "https://connect.squareup.com/v2/locations",
        "options": {},
        "sendHeaders": true,
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        }
      },
      "credentials": {
        "httpHeaderAuth": {
          "id": "n1GRrdbh899dbLYB",
          "name": "Square Header Auth"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "3ffebacb-7d53-4fbe-8ee8-aefa4aad2473",
      "name": "- 构建 GET 请求以从 https://justetf.com 获取数据",
      "type": "n8n-nodes-base.splitOut",
      "position": [
        1536,
        544
      ],
      "parameters": {
        "include": "selectedOtherFields",
        "options": {},
        "fieldToSplitOut": "locations",
        "fieldsToInclude": "id"
      },
      "typeVersion": 1
    },
    {
      "id": "c48961b3-deef-495a-bb50-3ba3b9c5c893",
      "name": "### HTML 内容提取",
      "type": "n8n-nodes-base.if",
      "position": [
        2032,
        544
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "498f5fab-6930-4e89-9fbe-0d67671da8d2",
              "operator": {
                "type": "array",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "={{ $json.orders }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "303aa5f9-6950-4524-b166-7c57e4c3fe5c",
      "name": "- 从 just etf 网站上的 CSS 选择器中提取 HTML 内容为人类可读文本",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1792,
        544
      ],
      "parameters": {
        "url": "https://connect.squareup.com/v2/orders/search",
        "method": "POST",
        "options": {
          "batching": {
            "batch": {}
          }
        },
        "jsonBody": "={\n  \"location_ids\": [\"{{ $json.locations.id }}\"],\n  \"query\": {\n    \"filter\": {\n      \"state_filter\": {\n        \"states\": [\"COMPLETED\"]\n      },\n      \"date_time_filter\": {\n        \"created_at\": {\n          \"start_at\": \"{{ $('Get Dates From Last Month').item.json.date }}T00:00:00-05:00\",\n          \"end_at\": \"{{ $('Get Dates From Last Month').item.json.date }}T23:59:59-05:00\"\n        }\n      }\n    }\n  },\n  \"limit\": 1000,\n  \"return_entries\": false\n}",
        "sendBody": true,
        "sendHeaders": true,
        "specifyBody": "json",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        }
      },
      "credentials": {
        "httpHeaderAuth": {
          "id": "n1GRrdbh899dbLYB",
          "name": "Square Header Auth"
        }
      },
      "executeOnce": false,
      "typeVersion": 4.2
    },
    {
      "id": "b9956b5b-612f-4078-a1c3-3f01da244855",
      "name": "- 将数据附加或更新到您的表中",
      "type": "n8n-nodes-base.code",
      "position": [
        2320,
        544
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "// Date and Location Metadata\nconst date = $('Get Dates From Last Month').item.json.date;\nconst location_id = $json.orders[0].location_id || null;\nconst location_name = $('Get Square Locations').item.json.locations.find(locations => locations.id === location_id)?.name;\n\n// Our Result Variables\nlet total_money = 0;\nlet total_tax = 0;\nlet total_discount = 0;\nlet total_tip = 0;\nlet total_returns = 0;\nlet cash_rounding = 0;\n\nlet cash_tender = 0;\nlet card_tender = 0;\nlet gift_card_tender = 0;\nlet other_tender = 0;\nlet fees = 0;\n\n// Loop Through Each Order\nfor (const sale of $json.orders) {\n\n    // Add the sales, taxes, discounts and tips\n    total_money += sale.total_money?.amount || 0;\n    total_tax += sale.total_tax_money?.amount || 0;\n    total_discount += -(sale.total_discount_money?.amount || 0);\n    total_tip += sale.total_tip_money?.amount || 0;\n    if (sale.rounding_adjustment) {\n      cash_rounding += sale.rounding_adjustment.amount_money?.amount || 0;\n    }\n\n  \n    if (sale.return_amounts) {\n      // If there are returns, subtract from sales totals and add to return amount total\n      total_money -= sale.return_amounts?.total_money?.amount || 0;\n      total_tax -= sale.return_amounts?.tax_money?.amount || 0;\n      total_discount -= sale.return_amounts?.discount_money?.amount || 0;\n      total_tip -= sale.return_amounts?.tip_money?.amount || 0;\n  \n      total_returns += -(sale.return_amounts?.total_money?.amount || 0);\n      total_returns -= -(sale.return_amounts?.tax_money?.amount || 0);\n      total_returns -= -(sale.return_amounts?.tip_money?.amount || 0);\n      total_returns -= -(sale.return_amounts?.discount_money?.amount || 0);\n  \n      // If an array of refunds is provided\n      for (const refund of sale.refunds || []) {\n        const transaction_id = refund.transaction_id;\n      \n        // Look for the original sale this refund refers to\n        const original_sale = $json.orders.find(original =>\n          original.id && transaction_id && original.id.includes(transaction_id)\n        );\n      \n        if (original_sale) {\n          if (original_sale.rounding_adjustment) {\n            const amount = original_sale.rounding_adjustment.amount_money?.amount || 0;\n            cash_rounding -= amount;\n            total_returns += amount;\n          }\n      \n          if (original_sale.tenders) {\n            for (const tender of original_sale.tenders) {\n              if (tender.id === refund.tender_id) {\n                const amount = refund.amount_money?.amount || 0;\n                if (tender.type === 'CARD') card_tender -= amount;\n                else if (tender.type === 'CASH') cash_tender -= amount;\n                else if (tender.type === 'SQUARE_GIFT_CARD') gift_card_tender -= amount;\n                else other_tender -= amount;\n      \n                if (refund.processing_fee_money && tender.id === refund.tender_id) {\n                  fees -= refund.processing_fee_money.amount || 0;\n                }\n              }\n            }\n          }\n        }\n      }\n    }\n  \n    if (sale.tenders) {\n      for (const tender of sale.tenders) {\n        const amount = tender.amount_money?.amount || 0;\n        if (tender.type === 'CARD') card_tender += amount;\n        else if (tender.type === 'CASH') cash_tender += amount;\n        else if (tender.type === 'SQUARE_GIFT_CARD') gift_card_tender += amount;\n        else other_tender += amount;\n  \n        if (tender.processing_fee_money) {\n          fees -= tender.processing_fee_money.amount || 0;\n        }\n      }\n    }\n  \n}\n\n// Final computed values\nconst net_sales = total_money - total_tip - total_tax - cash_rounding;\nconst gross_sales = net_sales - total_discount - total_returns;\nconst net_total = cash_tender + card_tender + gift_card_tender + other_tender + fees;\n\nreturn {\n  json: {\n    date,\n    location_id,\n    location_name,\n    gross_sales: gross_sales / 100.0,\n    total_returns: total_returns / 100.0,\n    total_discount: total_discount / 100.0,\n    net_sales: net_sales / 100.0,\n    total_tax: total_tax / 100.0,\n    total_tip: total_tip / 100.0,\n    cash_rounding: cash_rounding / 100.0,\n    total_payments_collected: total_money / 100.0,\n    cash: cash_tender / 100.0,\n    card: card_tender / 100.0,\n    gift_card: gift_card_tender / 100.0,\n    other: other_tender / 100.0,\n    fees: fees / 100.0,\n    net_total: net_total / 100.0,\n  }\n};"
      },
      "typeVersion": 2
    },
    {
      "id": "01d6eb5f-4ec9-4ddb-9a73-410bcf44c1f1",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        704,
        352
      ],
      "parameters": {
        "color": 5,
        "height": 420,
        "content": "## 触发器"
      },
      "typeVersion": 1
    },
    {
      "id": "3b116ff4-4bef-4653-99da-9aa768e07d4c",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1232,
        352
      ],
      "parameters": {
        "color": 5,
        "width": 460,
        "height": 420,
        "content": "n8n Webhooks"
      },
      "typeVersion": 1
    },
    {
      "id": "b32a6432-7bcd-4e8e-be26-4f3454f36e84",
      "name": "便签3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1712,
        352
      ],
      "parameters": {
        "color": 5,
        "height": 420,
        "content": "聊天响应"
      },
      "typeVersion": 1
    },
    {
      "id": "31d64d79-8904-4460-83e5-43f22d5ecb7d",
      "name": "便签4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2256,
        352
      ],
      "parameters": {
        "color": 5,
        "height": 420,
        "content": "模型响应"
      },
      "typeVersion": 1
    },
    {
      "id": "33c85a0d-eefb-4ae5-b5f3-544aa2a4a6e3",
      "name": "由 Github 模型提供支持",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        784,
        544
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "months",
              "triggerAtHour": 8
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "e5a3755f-ecbb-4afa-ae23-2fdd06e1f4af",
      "name": "便签5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2512,
        352
      ],
      "parameters": {
        "color": 5,
        "height": 420,
        "content": "### 通过 N8N Webhooks 使用 LLM 模型"
      },
      "typeVersion": 1
    },
    {
      "id": "abc96e29-7cac-462c-83f0-199e7c43a954",
      "name": "1. 创建新的 OpenAI 凭据并命名为 \"n8n-webhook\"",
      "type": "n8n-nodes-base.convertToFile",
      "position": [
        2576,
        544
      ],
      "parameters": {
        "options": {
          "fileName": "=sales_report_{{ $('Schedule Trigger').item.json.timestamp }}.csv"
        },
        "binaryPropertyName": "sales_report"
      },
      "typeVersion": 1.1
    },
    {
      "id": "ef17143b-adc8-4320-8861-af7f680faf05",
      "name": "### 需要帮助?",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2768,
        352
      ],
      "parameters": {
        "color": 5,
        "height": 420,
        "content": "3. 输入 Base URL 为 \"https://<您的_n8n_网址>/webhook/github-models\""
      },
      "typeVersion": 1
    },
    {
      "id": "ccdf9712-ee33-4271-9d8f-7872c706446e",
      "name": "获取上个月日期",
      "type": "n8n-nodes-base.code",
      "position": [
        1040,
        544
      ],
      "parameters": {
        "jsCode": "const inputDate = new Date($input.first().json.timestamp);\nconst year = inputDate.getFullYear();\nconst month = inputDate.getMonth();\n\n// Get first day of previous month\nconst firstDay = new Date(year, month - 1, 1);\n\n// Get last day of previous month by setting date to 0 of current month\nconst lastDay = new Date(year, month, 0).getDate();\n\nconst output = [];\n\nfor (let day = 1; day <= lastDay; day++) {\n  const date = new Date(year, month - 1, day);\n  const formatted = date.toISOString().split('T')[0];\n  output.push({ json: { date: formatted } });\n}\n\nreturn output;\n"
      },
      "typeVersion": 2
    },
    {
      "id": "f635bdce-e732-405c-ace1-718d13d49885",
      "name": "4. 激活您的工作流!这仅适用于生产环境 webhook URL",
      "type": "n8n-nodes-base.microsoftOutlook",
      "position": [
        2832,
        544
      ],
      "webhookId": "9b21e092-ebd1-4969-b1eb-a78603ae521d",
      "parameters": {
        "subject": "Your Last Month's Square Sales Report",
        "bodyContent": "<p>Hello User,</p><p>Please see the attached report containing last month's sales!</p><p>Best,<br> An Efficient Person</p>",
        "toRecipients": "user@example.com",
        "additionalFields": {
          "attachments": {
            "attachments": [
              {
                "binaryPropertyName": "sales_report"
              }
            ]
          },
          "bodyContentType": "html"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "9a94d6da-1d97-4d24-8db2-196f65087f05",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -64,
        -32
      ],
      "parameters": {
        "width": 736,
        "height": 1440,
        "content": "## 通过 Outlook 自动发送来自 Square 的月度销售报告"
      },
      "typeVersion": 1
    }
  ],
  "pinData": {},
  "connections": {
    "Send Report": {
      "main": [
        []
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Get Dates From Last Month",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Square Locations": {
      "main": [
        [
          {
            "node": "Turn Locations Into List",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Compile Sales Reports": {
      "main": [
        [
          {
            "node": "Convert Sales Summary to CSV File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Sales from Square": {
      "main": [
        [
          {
            "node": "Ignore Locations w/o Sales",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Turn Locations Into List": {
      "main": [
        [
          {
            "node": "Get Sales from Square",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Dates From Last Month": {
      "main": [
        [
          {
            "node": "Get Square Locations",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ignore Locations w/o Sales": {
      "main": [
        [
          {
            "node": "Compile Sales Reports",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert Sales Summary to CSV File": {
      "main": [
        [
          {
            "node": "Send Report",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

高级 - 文档提取, 多模态 AI

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
高级
节点数量16
分类2
节点类型8
难度说明

适合高级用户,包含 16+ 个节点的复杂工作流

外部链接
在 n8n.io 查看

分享此工作流