8
n8n 中文网amn8n.com

自动通过 Gmail 发送 Square 每周销售报告

高级

这是一个CRM领域的自动化工作流,包含 16 个节点。主要使用 If, Code, Gmail, SplitOut, HttpRequest 等节点。 自动通过 Gmail 发送 Square 每周销售报告

前置要求
  • Google 账号和 Gmail API 凭证
  • 可能需要目标 API 的认证凭证
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "meta": {
    "instanceId": "d6e2f2f655b1125bbcac14a4cac6d2e46c7a150e927f85fc96fdca1a6dc39e0e",
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "3b92fca3-0835-46e4-b642-61edb8788644",
      "name": "获取 Square 位置",
      "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": "ab8b4b48-6723-439f-9800-17db32b701cd",
      "name": "将位置转换为列表",
      "type": "n8n-nodes-base.splitOut",
      "position": [
        1536,
        544
      ],
      "parameters": {
        "include": "selectedOtherFields",
        "options": {},
        "fieldToSplitOut": "locations",
        "fieldsToInclude": "id"
      },
      "typeVersion": 1
    },
    {
      "id": "67c3641a-1081-4918-b165-ca97db909844",
      "name": "忽略无销售的位置",
      "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": "fa6cf239-5408-400b-8eb4-ca803c9769be",
      "name": "从 Square 获取销售数据",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1792,
        544
      ],
      "parameters": {
        "url": "https://connect.squareup.com/v2/orders/search",
        "method": "POST",
        "options": {},
        "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 Week').item.json.date }}T00:00:00-05:00\",\n          \"end_at\": \"{{ $('Get Dates From Last Week').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"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "06f70946-ac18-4e3c-8f19-8fba4b6065f0",
      "name": "编译销售报告",
      "type": "n8n-nodes-base.code",
      "position": [
        2320,
        544
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "// Date and Location Metadata\nconst date = $('Get Dates From Last Week').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": "7d44e945-bbce-4279-a82b-78641221108f",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        704,
        352
      ],
      "parameters": {
        "color": 5,
        "height": 420,
        "content": "## 触发器"
      },
      "typeVersion": 1
    },
    {
      "id": "d12b9656-cb3a-4cd5-824e-aa450747f351",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1232,
        352
      ],
      "parameters": {
        "color": 5,
        "width": 460,
        "height": 420,
        "content": "## 获取 Square 位置并分别处理每个位置"
      },
      "typeVersion": 1
    },
    {
      "id": "6963de7f-dfdd-46cb-8c75-fd825f7af9c2",
      "name": "便签3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1712,
        352
      ],
      "parameters": {
        "color": 5,
        "height": 420,
        "content": "## 从 Square 获取销售数据"
      },
      "typeVersion": 1
    },
    {
      "id": "b186ff01-ad7c-4b82-89d7-4c11a09e2062",
      "name": "便签4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2256,
        352
      ],
      "parameters": {
        "color": 5,
        "height": 420,
        "content": "## 为每个位置编译报告"
      },
      "typeVersion": 1
    },
    {
      "id": "4682170b-f683-49a4-b7b3-7c51e93479c5",
      "name": "计划触发器",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        784,
        544
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "weeks",
              "triggerAtDay": [
                1
              ],
              "triggerAtHour": 8
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "8155eed8-b45d-4bbe-9b00-222aa1fd58f0",
      "name": "便签5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2512,
        352
      ],
      "parameters": {
        "color": 5,
        "height": 420,
        "content": "## 将 Square 销售摘要转换为 CSV 文件"
      },
      "typeVersion": 1
    },
    {
      "id": "50b292ea-d238-4444-8cf1-a8ef7d54460c",
      "name": "将销售摘要转换为 CSV 文件",
      "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": "1e277f94-a5b6-4acd-b26c-f6122c21159d",
      "name": "便签6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2784,
        352
      ],
      "parameters": {
        "color": 5,
        "height": 420,
        "content": "## 将报告发送给财务团队/经理"
      },
      "typeVersion": 1
    },
    {
      "id": "d30dfd0e-8546-485c-abb5-6a0f833442e5",
      "name": "发送报告",
      "type": "n8n-nodes-base.gmail",
      "position": [
        2864,
        544
      ],
      "webhookId": "a1026d0b-b5a8-4657-955f-975f7c1f307d",
      "parameters": {
        "sendTo": "example@user.com",
        "message": "=<p>Hello User,</p><p>Please see the attached report containing last week's sales!</p><p>Best,<br> An Efficient Person</p>",
        "options": {
          "attachmentsUi": {
            "attachmentsBinary": [
              {
                "property": "sales_report"
              }
            ]
          }
        },
        "subject": "=Your Square Sales Report for {{ $('Schedule Trigger').item.json['Readable date'].split(',')[0] }}"
      },
      "credentials": {
        "gmailOAuth2": {
          "id": "x5LsvRUYpInxYmcG",
          "name": "Rosh's Personal Email"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "77b36188-bb4d-4c0c-9aba-a723287190c5",
      "name": "获取上周日期",
      "type": "n8n-nodes-base.code",
      "position": [
        1040,
        544
      ],
      "parameters": {
        "jsCode": "const inputDate = new Date($input.first().json.timestamp); // Input date\nconst daysBefore = 7;\nconst output = [];\n\nfor (let i = daysBefore; i > 0; i--) {\n  const d = new Date(inputDate);\n  d.setDate(inputDate.getDate() - i);\n\n  const formatted = d.toISOString().split('T')[0]; // \"YYYY-MM-DD\"\n\n  output.push({ json: { date: formatted } });\n}\n\nreturn output;\n"
      },
      "typeVersion": 2
    },
    {
      "id": "18ef8f36-73aa-4ea8-a9a7-2875f35d0778",
      "name": "便签7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -64,
        -32
      ],
      "parameters": {
        "width": 736,
        "height": 1440,
        "content": "## 通过 Gmail 自动发送来自 Square 的每周销售报告"
      },
      "typeVersion": 1
    }
  ],
  "pinData": {},
  "connections": {
    "Send Report": {
      "main": [
        []
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Get Dates From Last Week",
            "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
          }
        ]
      ]
    },
    "Get Dates From Last Week": {
      "main": [
        [
          {
            "node": "Get Square Locations",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Turn Locations Into List": {
      "main": [
        [
          {
            "node": "Get Sales from Square",
            "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 导入」,粘贴配置后根据需要修改凭证设置即可。

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

高级 - 客户关系管理

需要付费吗?

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

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

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

外部链接
在 n8n.io 查看

分享此工作流