{
  "nodes": [
    {
      "id": "Schedule Trigger",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "parameters": {
        "rule": {
          "interval": [
            {}
          ]
        }
      },
      "position": [
        780,
        540
      ],
      "typeVersion": 1
    },
    {
      "id": "Get Square Locations",
      "name": "Get Square Locations",
      "type": "n8n-nodes-base.httpRequest",
      "parameters": {
        "url": "https://connect.squareup.com/v2/locations",
        "options": {},
        "sendHeaders": true,
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        }
      },
      "position": [
        1040,
        540
      ],
      "typeVersion": 4.2
    },
    {
      "id": "Turn Locations Into List",
      "name": "Turn Locations Into List",
      "type": "n8n-nodes-base.splitOut",
      "parameters": {
        "include": "selectedOtherFields",
        "options": {},
        "fieldToSplitOut": "locations",
        "fieldsToInclude": "id"
      },
      "position": [
        1260,
        540
      ],
      "typeVersion": 1
    },
    {
      "id": "Get Sales from Square",
      "name": "Get Sales from Square",
      "type": "n8n-nodes-base.httpRequest",
      "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\": \"{ { $('Schedule Trigger').item.json.timestamp.toDateTime().minus(1, 'days').format('yyyy-MM-dd') } }T00:00:00-05:00\",\n          \"end_at\": \"{ { $('Schedule Trigger').item.json.timestamp.toDateTime().minus(1, 'days').format('yyyy-MM-dd') } }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"
            }
          ]
        }
      },
      "position": [
        1520,
        540
      ],
      "typeVersion": 4.2
    },
    {
      "id": "Ignore Locations w/o Sales",
      "name": "Ignore Locations w/o Sales",
      "type": "n8n-nodes-base.if",
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "Condition1",
              "operator": {
                "type": "array",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "={ { $json.orders } }",
              "rightValue": ""
            }
          ]
        }
      },
      "position": [
        1760,
        540
      ],
      "typeVersion": 2.2
    },
    {
      "id": "Compile Sales Reports",
      "name": "Compile Sales Reports",
      "type": "n8n-nodes-base.code",
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "// Date and Location Metadata\nconst date = $('Schedule Trigger').item.json['Readable date'].split(',')[0];\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    // 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    if (sale.return_amounts) {\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      total_returns += -(sale.return_amounts?.total_money?.amount || 0);\n    }\n}\nconst net_sales = total_money - total_tip - total_tax - cash_rounding;\nconst gross_sales = net_sales - total_discount - total_returns;\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  }\n};"
      },
      "position": [
        2040,
        540
      ],
      "typeVersion": 2
    },
    {
      "id": "Upload Sales to Google Sheets",
      "name": "Upload Sales to Google Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "parameters": {
        "columns": {
          "value": {
            "Date": "={ { $json.date } }",
            "Location": "={ { $json.location_name } }",
            "Gross Sales": "={ { $json.gross_sales } }",
            "Total Returns": "={ { $json.total_returns } }",
            "Total Discount": "={ { $json.total_discount } }",
            "Net Sales": "={ { $json.net_sales } }",
            "Taxes": "={ { $json.total_tax } }",
            "Tips": "={ { $json.total_tip } }",
            "Cash Rounding": "={ { $json.cash_rounding } }",
            "Total Money": "={ { $json.total_payments_collected } }"
          },
          "attemptToConvertTypes": false,
          "operation": "append",
          "sheetName": "Sales Data",
          "documentId": "your_google_sheet_id"
        },
        "options": {}
      },
      "position": [
        2300,
        540
      ],
      "typeVersion": 4.5
    }
  ],
  "connections": {
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Get Square Locations",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Square Locations": {
      "main": [
        [
          {
            "node": "Turn Locations Into List",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Turn Locations Into List": {
      "main": [
        [
          {
            "node": "Get Sales from Square",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Sales from Square": {
      "main": [
        [
          {
            "node": "Ignore Locations w/o Sales",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ignore Locations w/o Sales": {
      "main": [
        [
          {
            "node": "Compile Sales Reports",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Compile Sales Reports": {
      "main": [
        [
          {
            "node": "Upload Sales to Google Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}