{
  "name": "02 Late payment reminder",
  "nodes": [
    {
      "id": "ddf220b9-a941-4182-bf61-a3887f5dc363",
      "name": "Every morning 8:00",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        300
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 8 * * 1-5"
            }
          ]
        }
      }
    },
    {
      "id": "a8a904df-b01b-4624-bc64-28e34f494955",
      "name": "May this flow run?",
      "type": "n8n-nodes-base.executeWorkflow",
      "typeVersion": 1.2,
      "position": [
        240,
        300
      ],
      "parameters": {
        "source": "database",
        "workflowId": {
          "__rl": true,
          "mode": "list",
          "value": "",
          "cachedResultName": "10 Kill switch and daily cap"
        },
        "workflowInputs": {
          "mappingMode": "defineBelow",
          "value": {
            "flow": "payment-reminder",
            "count": 0
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "flow",
              "displayName": "flow",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "count",
              "displayName": "count",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": true
            }
          ]
        },
        "options": {}
      },
      "notes": "Reads on/off and the daily cap from the switches table",
      "notesInFlow": true
    },
    {
      "id": "f43c4d74-754b-4c34-8cad-c08487a64d05",
      "name": "On?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        480,
        300
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "a2",
              "leftValue": "={{ $json.allowed }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      }
    },
    {
      "id": "fcfac0b1-a579-44fe-99de-0041611de4b1",
      "name": "Open invoices",
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1,
      "position": [
        720,
        300
      ],
      "parameters": {
        "resource": "row",
        "operation": "get",
        "dataTableId": {
          "__rl": true,
          "mode": "list",
          "value": "",
          "cachedResultName": "invoices"
        },
        "matchType": "allConditions",
        "filters": {
          "conditions": [
            {
              "keyName": "status",
              "condition": "eq",
              "keyValue": "open"
            }
          ]
        },
        "returnAll": true
      }
    },
    {
      "id": "910886da-97fe-4c86-8197-68776dc635a9",
      "name": "Overdue and due for a reminder",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        960,
        300
      ],
      "parameters": {
        "jsCode": "// 1st reminder 1 day after the due date, 2nd after 14 days, then no more automatic reminders\nconst day = 24*3600*1000, now = Date.now();\nconst out = [];\nfor (const i of $input.all()) {\n  const f = i.json; const overdue = Math.floor((now - new Date(f.due_date).getTime())/day);\n  const n = f.reminders || 0;\n  const since = f.last_reminded ? Math.floor((now - new Date(f.last_reminded).getTime())/day) : 999;\n  if (overdue >= 1 && n === 0) out.push({json: {...f, overdue, round: 1}});\n  else if (overdue >= 14 && n === 1 && since >= 7) out.push({json: {...f, overdue, round: 2}});\n}\nreturn out;"
      }
    },
    {
      "id": "d46f6752-f0d7-4c50-974f-5fb4fe862e4c",
      "name": "Send reminder",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2.1,
      "position": [
        1200,
        300
      ],
      "parameters": {
        "fromEmail": "info@example.com",
        "toEmail": "={{ $json.client_email }}",
        "subject": "={{ $json.round === 1 ? 'Reminder' : 'Second reminder' }}: invoice {{ $json.invoice_number }}",
        "emailFormat": "text",
        "text": "=Hi {{ $json.client }},\n\nInvoice {{ $json.invoice_number }} for ${{ $json.amount.toFixed(2) }} was due on {{ DateTime.fromISO($json.due_date).setLocale('en-US').toFormat('MMMM d') }} and we haven't seen the payment yet. If it's already on its way, you can ignore this email.\n\nBest regards",
        "options": {
          "appendAttribution": false
        }
      }
    },
    {
      "id": "60830ee8-c120-4b5c-8d1d-cc446e216532",
      "name": "Update counter",
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1,
      "position": [
        1440,
        300
      ],
      "parameters": {
        "resource": "row",
        "operation": "update",
        "dataTableId": {
          "__rl": true,
          "mode": "list",
          "value": "",
          "cachedResultName": "invoices"
        },
        "matchType": "allConditions",
        "filters": {
          "conditions": [
            {
              "keyName": "id",
              "condition": "eq",
              "keyValue": "={{ $('Overdue and due for a reminder').item.json.id }}"
            }
          ]
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "reminders": "={{ $('Overdue and due for a reminder').item.json.round }}",
            "last_reminded": "={{ $now.toISO() }}"
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "reminders",
              "displayName": "reminders",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": true
            },
            {
              "id": "last_reminded",
              "displayName": "last_reminded",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "dateTime",
              "canBeUsedToMatch": true
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "9729408f-e8b8-46a4-9c6e-121c677ee71e",
      "name": "Test manually",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        560
      ],
      "parameters": {}
    },
    {
      "id": "4733533f-3912-42f6-85c7-105f08b963b9",
      "name": "Read me",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -40,
        -160
      ],
      "parameters": {
        "content": "## Late payment reminder\nTested on November 11, 2026, with n8n 2.47.3.\n\n**Fill in:** an SMTP account, the *invoices* table (invoice_number, client, client_email, amount, due_date, status, reminders, last_reminded), and pick template 10 in 'May this flow run?'.\n\nRound 1: one day after the due date. Round 2: after 14 days. After that, you pick up the phone.",
        "height": 220,
        "width": 520,
        "color": 7
      }
    }
  ],
  "connections": {
    "Every morning 8:00": {
      "main": [
        [
          {
            "node": "May this flow run?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "May this flow run?": {
      "main": [
        [
          {
            "node": "On?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "On?": {
      "main": [
        [
          {
            "node": "Open invoices",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Open invoices": {
      "main": [
        [
          {
            "node": "Overdue and due for a reminder",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Overdue and due for a reminder": {
      "main": [
        [
          {
            "node": "Send reminder",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send reminder": {
      "main": [
        [
          {
            "node": "Update counter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Test manually": {
      "main": [
        [
          {
            "node": "May this flow run?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  }
}