{
  "id": "MwrrVy5R0JruerRI",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Customer Payment Reminder Scheduler",
  "tags": [],
  "nodes": [
    {
      "id": "dd929e51-c8f9-475a-abf7-4656e701998b",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -272,
        -112
      ],
      "parameters": {
        "width": 480,
        "height": 896,
        "content": "## Customer Payment Reminder Scheduler\n\n### How it works\n\n1. The workflow is triggered daily at 9 AM.\n2. It fetches unpaid invoices from a Microsoft SQL database.\n3. The workflow checks if there are any unpaid invoices to process.\n4. Each unpaid invoice is processed to determine how many days until due and to route reminders accordingly.\n5. Reminder emails are prepared and sent at different stages based on conditions.\n6. Finally, a summary report of all reminders sent is generated and emailed to the finance team.\n\n### Setup steps\n\n- [ ] Connect to Microsoft SQL database\n- [ ] Set up Gmail credentials\n- [ ] Configure email templates for reminders\n\n### Customization\n\nCustomize the email templates for better engagement."
      },
      "typeVersion": 1
    },
    {
      "id": "5c533614-e061-44b6-970c-5e8132fe4384",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        288,
        368
      ],
      "parameters": {
        "color": 7,
        "height": 304,
        "content": "## Schedule daily trigger\n\nTrigger the workflow at a set time."
      },
      "typeVersion": 1
    },
    {
      "id": "83f1d8a6-4a95-4f3f-b1ca-76170d9e0dde",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        560,
        368
      ],
      "parameters": {
        "color": 7,
        "height": 304,
        "content": "## Fetch unpaid invoices\n\nRetrieve unpaid invoices from the database."
      },
      "typeVersion": 1
    },
    {
      "id": "2d5f8e17-17d8-4bc7-ad9d-521969ca6b2d",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        832,
        368
      ],
      "parameters": {
        "color": 7,
        "height": 304,
        "content": "## Check unpaid invoices\n\nDetermine if there are unpaid invoices to process."
      },
      "typeVersion": 1
    },
    {
      "id": "ec8fa507-8d5f-4ea8-af03-6519404047b1",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1104,
        272
      ],
      "parameters": {
        "color": 7,
        "height": 304,
        "content": "## Process each invoice\n\nSplit invoices into batches for processing."
      },
      "typeVersion": 1
    },
    {
      "id": "e2065e05-1391-4bd8-a4e2-b6198bd6881b",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1104,
        608
      ],
      "parameters": {
        "color": 7,
        "height": 304,
        "content": "## No invoices to process\n\nHandle cases with no unpaid invoices."
      },
      "typeVersion": 1
    },
    {
      "id": "ff1e2ee5-65dc-4c92-8132-c48aa807b905",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1184,
        -112
      ],
      "parameters": {
        "color": 7,
        "height": 352,
        "content": "## Calculate days until due\n\nCalculate how many days are left until each invoice is due."
      },
      "typeVersion": 1
    },
    {
      "id": "b60548ba-2e8c-4901-ba73-0fcfbeddfa5d",
      "name": "Sticky Note7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1408,
        464
      ],
      "parameters": {
        "color": 7,
        "height": 448,
        "content": "## Route to reminder stage\n\nDetermine which stage of reminder to process based on conditions."
      },
      "typeVersion": 1
    },
    {
      "id": "77cac75e-f91c-4a0a-a2f2-1aca8e54b090",
      "name": "Sticky Note8",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1632,
        96
      ],
      "parameters": {
        "color": 7,
        "width": 1088,
        "height": 272,
        "content": "## Prepare and send 7 days before\n\nPrepare and send reminders for invoices due in 7 days."
      },
      "typeVersion": 1
    },
    {
      "id": "d83614ff-6aa2-4999-9093-c1cf7d582097",
      "name": "Sticky Note9",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1680,
        400
      ],
      "parameters": {
        "color": 7,
        "width": 1088,
        "height": 272,
        "content": "## Prepare and send 3 days before\n\nPrepare and send reminders for invoices due in 3 days."
      },
      "typeVersion": 1
    },
    {
      "id": "80662059-056b-4e60-94f0-bc9751f5bc54",
      "name": "Sticky Note10",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1680,
        704
      ],
      "parameters": {
        "color": 7,
        "width": 1088,
        "height": 272,
        "content": "## Prepare and send due today\n\nPrepare and send reminders for invoices due today."
      },
      "typeVersion": 1
    },
    {
      "id": "f6d267b6-4e52-4a97-8e37-e0c7b75d53f8",
      "name": "Sticky Note11",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1680,
        1008
      ],
      "parameters": {
        "color": 7,
        "width": 1088,
        "height": 272,
        "content": "## Prepare and send 1 day overdue\n\nPrepare and send reminders for invoices overdue by 1 day."
      },
      "typeVersion": 1
    },
    {
      "id": "a2960ca6-6d49-4041-b83a-1acd3d9c4206",
      "name": "Sticky Note12",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1680,
        1312
      ],
      "parameters": {
        "color": 7,
        "width": 1088,
        "height": 272,
        "content": "## Prepare and send 7 days overdue\n\nPrepare and send reminders for invoices overdue by 7 days."
      },
      "typeVersion": 1
    },
    {
      "id": "ac1f88d0-1844-4813-8b17-5724643c580f",
      "name": "Sticky Note13",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1632,
        1616
      ],
      "parameters": {
        "color": 7,
        "width": 1088,
        "height": 272,
        "content": "## Prepare and send 15 days overdue\n\nPrepare and send reminders for invoices overdue by 15 days."
      },
      "typeVersion": 1
    },
    {
      "id": "5e697089-f4ac-4eac-9a3b-3998bdb515f1",
      "name": "Sticky Note14",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2800,
        304
      ],
      "parameters": {
        "color": 7,
        "height": 448,
        "content": "## Merge all stages\n\nMerge all results from reminders into a single output."
      },
      "typeVersion": 1
    },
    {
      "id": "ffc1e989-c5ec-457d-9006-1258b9c3ae88",
      "name": "Sticky Note15",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        3072,
        416
      ],
      "parameters": {
        "color": 7,
        "width": 864,
        "height": 272,
        "content": "## Generate summary\n\nAggregate and generate a summary of all reminders sent."
      },
      "typeVersion": 1
    },
    {
      "id": "45640612-47e2-4e3d-b665-84fe2a1fd734",
      "name": "Daily at 9 AM",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        336,
        496
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtHour": 9
            }
          ]
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "dab9e230-04bc-4465-8bc7-a4ed472ddda1",
      "name": "Fetch Unpaid Invoices",
      "type": "n8n-nodes-base.microsoftSql",
      "position": [
        608,
        496
      ],
      "parameters": {
        "query": "SELECT InvoiceID, CustomerEmail, CustomerName, InvoiceNumber, Amount, DueDate, ReminderStage, LastReminderDate FROM Invoices WHERE PaymentStatus = 'Unpaid' ORDER BY DueDate",
        "options": {},
        "operation": "executeQuery"
      },
      "credentials": {
        "microsoftSql": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "88454393-9aae-4d58-b126-87b690c47c8c",
      "name": "Has Unpaid Invoices?",
      "type": "n8n-nodes-base.if",
      "position": [
        880,
        496
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 1,
            "leftValue": "",
            "caseSensitive": false,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "operator": {
                "type": "number",
                "operation": "gt"
              },
              "leftValue": "={{ $input.all().length }}",
              "rightValue": 0
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "c5e8c320-664b-4ddd-93f6-0c39d190904c",
      "name": "Merge All Stages",
      "type": "n8n-nodes-base.merge",
      "position": [
        2848,
        464
      ],
      "parameters": {
        "numberInputs": 6
      },
      "typeVersion": 3.2
    },
    {
      "id": "e5db6fdf-a1af-496b-b215-3219ea8b608f",
      "name": "Calculate Days Until Due",
      "type": "n8n-nodes-base.dateTime",
      "position": [
        1232,
        80
      ],
      "parameters": {
        "endDate": "={{ $json.DueDate }}",
        "options": {},
        "operation": "getTimeBetweenDates",
        "startDate": "={{ $now.toISO() }}"
      },
      "typeVersion": 2
    },
    {
      "id": "7c57e471-8e9d-4e81-b7b5-31a6a82cca4c",
      "name": "Route to Reminder Stage",
      "type": "n8n-nodes-base.switch",
      "position": [
        1456,
        624
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "outputKey": "7 Days Before",
              "conditions": {
                "options": {
                  "version": 3,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "fcf160a0-0ffb-4ead-b1e4-aa64ef2f41b8",
                    "operator": {
                      "type": "number",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.day }}",
                    "rightValue": 7
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "3 Days Before",
              "conditions": {
                "options": {
                  "version": 3,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "9f002c8d-f890-4974-9565-129a97f17960",
                    "operator": {
                      "type": "number",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.day }}",
                    "rightValue": 3
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Due Today",
              "conditions": {
                "options": {
                  "version": 3,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "53e15a97-fc0f-4cc6-a4e6-e4c75e8d0ca7",
                    "operator": {
                      "type": "number",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.day }}",
                    "rightValue": 0
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "1 Day Overdue",
              "conditions": {
                "options": {
                  "version": 3,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "6c7dbf9b-027c-4b1e-a8d2-a7303094ebb9",
                    "operator": {
                      "type": "number",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.day }}",
                    "rightValue": -1
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "7 Days Overdue",
              "conditions": {
                "options": {
                  "version": 3,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "d8afcebf-29bf-41e1-a90b-e0b10fc8ba78",
                    "operator": {
                      "type": "number",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.day }}",
                    "rightValue": -7
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "15 Days Overdue",
              "conditions": {
                "options": {
                  "version": 3,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "786ba84a-0b3b-4842-b2be-8db9d2a2c05f",
                    "operator": {
                      "type": "number",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.day }}",
                    "rightValue": -15
                  }
                ]
              },
              "renameOutput": true
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3.4
    },
    {
      "id": "97f23b48-51cb-40ed-b263-4b16d9dfc243",
      "name": "Already Sent 7 Before?",
      "type": "n8n-nodes-base.if",
      "position": [
        1680,
        208
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 3,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "or",
          "conditions": [
            {
              "id": "73053db0-fcc0-4c19-ab57-2933ed8b418f",
              "operator": {
                "type": "string",
                "operation": "isEmpty"
              },
              "leftValue": "={{ $json.ReminderStage }}",
              "rightValue": ""
            },
            {
              "id": "7f8091bf-fb05-4ce9-990c-8b190200295f",
              "operator": {
                "type": "string",
                "operation": "notEquals"
              },
              "leftValue": "={{ $json.ReminderStage }}",
              "rightValue": "7_DAYS_BEFORE"
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "30d1247a-b26d-4d6a-9cbf-f73a247a10d9",
      "name": "Prepare Email 7 Before",
      "type": "n8n-nodes-base.set",
      "position": [
        1904,
        208
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "stage",
              "name": "stage",
              "type": "string",
              "value": "7_DAYS_BEFORE"
            },
            {
              "id": "subject",
              "name": "subject",
              "type": "string",
              "value": "=Reminder: Invoice {{ $json.InvoiceNumber }} due in 7 days"
            },
            {
              "id": "message",
              "name": "message",
              "type": "string",
              "value": "=Dear {{ $json.CustomerName }},\n\nThis is a friendly reminder that invoice {{ $json.InvoiceNumber }} for ${{ $json.Amount }} is due on {{ $json.DueDate }}.\n\nPlease ensure payment is made by the due date.\n\nThank you!"
            }
          ]
        },
        "includeOtherFields": true
      },
      "typeVersion": 3.4
    },
    {
      "id": "ed2b3eff-8d30-4d13-8d17-c7532ef6e524",
      "name": "Send Email 7 Before",
      "type": "n8n-nodes-base.gmail",
      "position": [
        2128,
        208
      ],
      "parameters": {
        "sendTo": "={{ $json.CustomerEmail }}",
        "message": "={{ $json.message }}",
        "options": {},
        "subject": "={{ $json.subject }}",
        "emailType": "text"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "edaac184-e1a1-427f-a58d-0ba5d4d24fc3",
      "name": "Update DB 7 Before",
      "type": "n8n-nodes-base.microsoftSql",
      "position": [
        2352,
        208
      ],
      "parameters": {
        "query": "UPDATE Invoices SET ReminderStage = '{{ $(\"Prepare Email 7 Before\").item.json.stage }}', LastReminderDate = '{{ $now.toFormat(\"yyyy-MM-dd\") }}' WHERE InvoiceID = {{ $(\"Prepare Email 7 Before\").item.json.InvoiceID }}",
        "options": {},
        "operation": "executeQuery"
      },
      "credentials": {
        "microsoftSql": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "7dcfccc7-d726-4b08-8a54-d6a8b2669f01",
      "name": "Log Audit 7 Before",
      "type": "n8n-nodes-base.microsoftSql",
      "position": [
        2576,
        208
      ],
      "parameters": {
        "query": "INSERT INTO AuditLog (InvoiceID, Action, ReminderStage, Timestamp) VALUES ({{ $(\"Prepare Email 7 Before\").item.json.InvoiceID }}, 'REMINDER_SENT', '{{ $(\"Prepare Email 7 Before\").item.json.stage }}', '{{ $now.toISO() }}')",
        "options": {},
        "operation": "executeQuery"
      },
      "credentials": {
        "microsoftSql": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "1ea28460-04c4-4bcb-8b9c-9fd3759d0027",
      "name": "Already Sent 3 Before?",
      "type": "n8n-nodes-base.if",
      "position": [
        1728,
        512
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 3,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "bc35deff-b765-4b77-bf9b-df680e742fe4",
              "operator": {
                "type": "string",
                "operation": "notEquals"
              },
              "leftValue": "={{ $json.ReminderStage }}",
              "rightValue": "3_DAYS_BEFORE"
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "46fc5cc7-7bd3-4cae-b090-621f8d29969f",
      "name": "Prepare Email 3 Before",
      "type": "n8n-nodes-base.set",
      "position": [
        1952,
        512
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "stage",
              "name": "stage",
              "type": "string",
              "value": "3_DAYS_BEFORE"
            },
            {
              "id": "subject",
              "name": "subject",
              "type": "string",
              "value": "=Reminder: Invoice {{ $json.InvoiceNumber }} due in 3 days"
            },
            {
              "id": "message",
              "name": "message",
              "type": "string",
              "value": "=Dear {{ $json.CustomerName }},\n\nInvoice {{ $json.InvoiceNumber }} for ${{ $json.Amount }} is due in 3 days ({{ $json.DueDate }}).\n\nPlease arrange payment soon.\n\nThank you!"
            }
          ]
        },
        "includeOtherFields": true
      },
      "typeVersion": 3.4
    },
    {
      "id": "19d0cdda-d53a-4d1f-9709-b26dee146365",
      "name": "Send Email 3 Before",
      "type": "n8n-nodes-base.gmail",
      "position": [
        2176,
        512
      ],
      "parameters": {
        "sendTo": "={{ $json.CustomerEmail }}",
        "message": "={{ $json.message }}",
        "options": {},
        "subject": "={{ $json.subject }}",
        "emailType": "text"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "bdb23aad-8135-457b-b666-fd32f8c1b117",
      "name": "Update DB 3 Before",
      "type": "n8n-nodes-base.microsoftSql",
      "position": [
        2400,
        512
      ],
      "parameters": {
        "query": "UPDATE Invoices SET ReminderStage = '{{ $(\"Prepare Email 3 Before\").item.json.stage }}', LastReminderDate = '{{ $now.toFormat(\"yyyy-MM-dd\") }}' WHERE InvoiceID = {{ $(\"Prepare Email 3 Before\").item.json.InvoiceID }}",
        "options": {},
        "operation": "executeQuery"
      },
      "credentials": {
        "microsoftSql": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "1e8aa5eb-4f71-4ad7-8679-7c43a40f4f82",
      "name": "Log Audit 3 Before",
      "type": "n8n-nodes-base.microsoftSql",
      "position": [
        2624,
        512
      ],
      "parameters": {
        "query": "INSERT INTO AuditLog (InvoiceID, Action, ReminderStage, Timestamp) VALUES ({{ $(\"Prepare Email 3 Before\").item.json.InvoiceID }}, 'REMINDER_SENT', '{{ $(\"Prepare Email 3 Before\").item.json.stage }}', '{{ $now.toISO() }}')",
        "options": {},
        "operation": "executeQuery"
      },
      "credentials": {
        "microsoftSql": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "3b7e2dda-3618-470e-9e9e-5e4c37b94b58",
      "name": "Already Sent Due Today?",
      "type": "n8n-nodes-base.if",
      "position": [
        1728,
        816
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 1,
            "leftValue": "",
            "caseSensitive": false,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "operator": {
                "type": "string",
                "operation": "notEquals"
              },
              "leftValue": "={{ $json.ReminderStage }}",
              "rightValue": "DUE_TODAY"
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "f01335da-174e-444e-8ad2-ce066adcabd4",
      "name": "Prepare Email Due Today",
      "type": "n8n-nodes-base.set",
      "position": [
        1952,
        816
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "stage",
              "name": "stage",
              "type": "string",
              "value": "DUE_TODAY"
            },
            {
              "id": "subject",
              "name": "subject",
              "type": "string",
              "value": "=Payment Due Today: Invoice {{ $json.InvoiceNumber }}"
            },
            {
              "id": "message",
              "name": "message",
              "type": "string",
              "value": "=Dear {{ $json.CustomerName }},\n\nInvoice {{ $json.InvoiceNumber }} for ${{ $json.Amount }} is due TODAY ({{ $json.DueDate }}).\n\nPlease submit payment immediately to avoid late fees.\n\nThank you!"
            }
          ]
        },
        "includeOtherFields": true
      },
      "typeVersion": 3.4
    },
    {
      "id": "40820aa7-92d6-4f5c-a80c-2189b3191dff",
      "name": "Send Email Due Today",
      "type": "n8n-nodes-base.gmail",
      "position": [
        2176,
        816
      ],
      "parameters": {
        "sendTo": "={{ $json.CustomerEmail }}",
        "message": "={{ $json.message }}",
        "options": {},
        "subject": "={{ $json.subject }}",
        "emailType": "text"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "638ee997-5a62-4277-b4ed-e972b4b085f1",
      "name": "Update DB Due Today",
      "type": "n8n-nodes-base.microsoftSql",
      "position": [
        2400,
        816
      ],
      "parameters": {
        "query": "UPDATE Invoices SET ReminderStage = '{{ $(\"Prepare Email Due Today\").item.json.stage }}', LastReminderDate = '{{ $now.toFormat(\"yyyy-MM-dd\") }}' WHERE InvoiceID = {{ $(\"Prepare Email Due Today\").item.json.InvoiceID }}",
        "options": {},
        "operation": "executeQuery"
      },
      "credentials": {
        "microsoftSql": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "438a3b88-131f-44a8-830f-af8adbf9e484",
      "name": "Log Audit Due Today",
      "type": "n8n-nodes-base.microsoftSql",
      "position": [
        2624,
        816
      ],
      "parameters": {
        "query": "INSERT INTO AuditLog (InvoiceID, Action, ReminderStage, Timestamp) VALUES ({{ $(\"Prepare Email Due Today\").item.json.InvoiceID }}, 'REMINDER_SENT', '{{ $(\"Prepare Email Due Today\").item.json.stage }}', '{{ $now.toISO() }}')",
        "options": {},
        "operation": "executeQuery"
      },
      "credentials": {
        "microsoftSql": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "b8e7703f-2983-4316-9453-0c4419c0def6",
      "name": "Already Sent 1 Overdue?",
      "type": "n8n-nodes-base.if",
      "position": [
        1728,
        1120
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 1,
            "leftValue": "",
            "caseSensitive": false,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "operator": {
                "type": "string",
                "operation": "notEquals"
              },
              "leftValue": "={{ $json.ReminderStage }}",
              "rightValue": "1_DAY_OVERDUE"
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "fef0a652-90f5-4ec4-b17f-301970110502",
      "name": "Prepare Email 1 Overdue",
      "type": "n8n-nodes-base.set",
      "position": [
        1952,
        1120
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "stage",
              "name": "stage",
              "type": "string",
              "value": "1_DAY_OVERDUE"
            },
            {
              "id": "subject",
              "name": "subject",
              "type": "string",
              "value": "=OVERDUE: Invoice {{ $json.InvoiceNumber }} - Payment Required"
            },
            {
              "id": "message",
              "name": "message",
              "type": "string",
              "value": "=Dear {{ $json.CustomerName }},\n\nInvoice {{ $json.InvoiceNumber }} for ${{ $json.Amount }} is now 1 day overdue (due date: {{ $json.DueDate }}).\n\nPlease submit payment immediately to avoid additional late fees.\n\nThank you!"
            }
          ]
        },
        "includeOtherFields": true
      },
      "typeVersion": 3.4
    },
    {
      "id": "eb90a86d-8e83-463c-a81a-6cfcfd3322ad",
      "name": "Send Email 1 Overdue",
      "type": "n8n-nodes-base.gmail",
      "position": [
        2176,
        1120
      ],
      "parameters": {
        "sendTo": "={{ $json.CustomerEmail }}",
        "message": "={{ $json.message }}",
        "options": {},
        "subject": "={{ $json.subject }}",
        "emailType": "text"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "7db2b065-c468-43ae-a8b2-ac13e01211e5",
      "name": "Update DB 1 Overdue",
      "type": "n8n-nodes-base.microsoftSql",
      "position": [
        2400,
        1120
      ],
      "parameters": {
        "query": "UPDATE Invoices SET ReminderStage = '{{ $(\"Prepare Email 1 Overdue\").item.json.stage }}', LastReminderDate = '{{ $now.toFormat(\"yyyy-MM-dd\") }}' WHERE InvoiceID = {{ $(\"Prepare Email 1 Overdue\").item.json.InvoiceID }}",
        "options": {},
        "operation": "executeQuery"
      },
      "credentials": {
        "microsoftSql": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "ee1353e1-fad2-4a2f-9d42-8e33f01424a3",
      "name": "Log Audit 1 Overdue",
      "type": "n8n-nodes-base.microsoftSql",
      "position": [
        2624,
        1120
      ],
      "parameters": {
        "query": "INSERT INTO AuditLog (InvoiceID, Action, ReminderStage, Timestamp) VALUES ({{ $(\"Prepare Email 1 Overdue\").item.json.InvoiceID }}, 'REMINDER_SENT', '{{ $(\"Prepare Email 1 Overdue\").item.json.stage }}', '{{ $now.toISO() }}')",
        "options": {},
        "operation": "executeQuery"
      },
      "credentials": {
        "microsoftSql": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "eeb25af0-1aeb-4306-a9d2-25431536584f",
      "name": "Already Sent 7 Overdue?",
      "type": "n8n-nodes-base.if",
      "position": [
        1728,
        1424
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 1,
            "leftValue": "",
            "caseSensitive": false,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "operator": {
                "type": "string",
                "operation": "notEquals"
              },
              "leftValue": "={{ $json.ReminderStage }}",
              "rightValue": "7_DAYS_OVERDUE"
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "e6178cdd-e1c6-4b80-be0d-78127a6edd25",
      "name": "Prepare Email 7 Overdue",
      "type": "n8n-nodes-base.set",
      "position": [
        1952,
        1424
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "stage",
              "name": "stage",
              "type": "string",
              "value": "7_DAYS_OVERDUE"
            },
            {
              "id": "subject",
              "name": "subject",
              "type": "string",
              "value": "=URGENT: Invoice {{ $json.InvoiceNumber }} - 7 Days Overdue"
            },
            {
              "id": "message",
              "name": "message",
              "type": "string",
              "value": "=Dear {{ $json.CustomerName }},\n\nInvoice {{ $json.InvoiceNumber }} for ${{ $json.Amount }} is now 7 days overdue (due date: {{ $json.DueDate }}).\n\nImmediate payment is required. Please contact us if there are any issues.\n\nThank you!"
            }
          ]
        },
        "includeOtherFields": true
      },
      "typeVersion": 3.4
    },
    {
      "id": "09ef0575-1f66-46ee-8f9b-f5523c90c31f",
      "name": "Send Email 7 Overdue",
      "type": "n8n-nodes-base.gmail",
      "position": [
        2176,
        1424
      ],
      "parameters": {
        "sendTo": "={{ $json.CustomerEmail }}",
        "message": "={{ $json.message }}",
        "options": {},
        "subject": "={{ $json.subject }}",
        "emailType": "text"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "9b2e99a8-59be-4a79-a1e5-fe51cedef17d",
      "name": "Update DB 7 Overdue",
      "type": "n8n-nodes-base.microsoftSql",
      "position": [
        2400,
        1424
      ],
      "parameters": {
        "query": "UPDATE Invoices SET ReminderStage = '{{ $(\"Prepare Email 7 Overdue\").item.json.stage }}', LastReminderDate = '{{ $now.toFormat(\"yyyy-MM-dd\") }}' WHERE InvoiceID = {{ $(\"Prepare Email 7 Overdue\").item.json.InvoiceID }}",
        "options": {},
        "operation": "executeQuery"
      },
      "credentials": {
        "microsoftSql": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "31b021c5-21fe-409c-8146-ed791b65ff2f",
      "name": "Log Audit 7 Overdue",
      "type": "n8n-nodes-base.microsoftSql",
      "position": [
        2624,
        1424
      ],
      "parameters": {
        "query": "INSERT INTO AuditLog (InvoiceID, Action, ReminderStage, Timestamp) VALUES ({{ $(\"Prepare Email 7 Overdue\").item.json.InvoiceID }}, 'REMINDER_SENT', '{{ $(\"Prepare Email 7 Overdue\").item.json.stage }}', '{{ $now.toISO() }}')",
        "options": {},
        "operation": "executeQuery"
      },
      "credentials": {
        "microsoftSql": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "f4c59914-5103-433f-9192-44c3fa27857a",
      "name": "Already Sent 15 Overdue?",
      "type": "n8n-nodes-base.if",
      "position": [
        1680,
        1728
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 1,
            "leftValue": "",
            "caseSensitive": false,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "operator": {
                "type": "string",
                "operation": "notEquals"
              },
              "leftValue": "={{ $json.ReminderStage }}",
              "rightValue": "15_DAYS_OVERDUE"
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "8824a669-e78f-4f2f-b3f2-4a4f1ed11704",
      "name": "Prepare Email 15 Overdue",
      "type": "n8n-nodes-base.set",
      "position": [
        1904,
        1728
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "stage",
              "name": "stage",
              "type": "string",
              "value": "15_DAYS_OVERDUE"
            },
            {
              "id": "subject",
              "name": "subject",
              "type": "string",
              "value": "=FINAL NOTICE: Invoice {{ $json.InvoiceNumber }} - 15 Days Overdue"
            },
            {
              "id": "message",
              "name": "message",
              "type": "string",
              "value": "=Dear {{ $json.CustomerName }},\n\nThis is a FINAL NOTICE. Invoice {{ $json.InvoiceNumber }} for ${{ $json.Amount }} is now 15 days overdue (due date: {{ $json.DueDate }}).\n\nPayment must be received immediately or your account may be sent to collections.\n\nThank you!"
            }
          ]
        },
        "includeOtherFields": true
      },
      "typeVersion": 3.4
    },
    {
      "id": "b9d12e42-6eb2-44bb-b088-f935b703cdc0",
      "name": "Send Email 15 Overdue",
      "type": "n8n-nodes-base.gmail",
      "position": [
        2128,
        1728
      ],
      "parameters": {
        "sendTo": "={{ $json.CustomerEmail }}",
        "message": "={{ $json.message }}",
        "options": {},
        "subject": "={{ $json.subject }}",
        "emailType": "text"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "a981bb98-5e3a-4702-82fa-8659a59152f6",
      "name": "Update DB 15 Overdue",
      "type": "n8n-nodes-base.microsoftSql",
      "position": [
        2352,
        1728
      ],
      "parameters": {
        "query": "UPDATE Invoices SET ReminderStage = '{{ $(\"Prepare Email 15 Overdue\").item.json.stage }}', LastReminderDate = '{{ $now.toFormat(\"yyyy-MM-dd\") }}' WHERE InvoiceID = {{ $(\"Prepare Email 15 Overdue\").item.json.InvoiceID }}",
        "options": {},
        "operation": "executeQuery"
      },
      "credentials": {
        "microsoftSql": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "020d82ed-e3d2-4328-bb84-4563e8f9f95d",
      "name": "Log Audit 15 Overdue",
      "type": "n8n-nodes-base.microsoftSql",
      "position": [
        2576,
        1728
      ],
      "parameters": {
        "query": "INSERT INTO AuditLog (InvoiceID, Action, ReminderStage, Timestamp) VALUES ({{ $(\"Prepare Email 15 Overdue\").item.json.InvoiceID }}, 'REMINDER_SENT', '{{ $(\"Prepare Email 15 Overdue\").item.json.stage }}', '{{ $now.toISO() }}')",
        "options": {},
        "operation": "executeQuery"
      },
      "credentials": {
        "microsoftSql": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "a4058cbc-de7c-4f35-b27e-483bfdc56df7",
      "name": "Process Each Invoice",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        1152,
        400
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "91b63edc-829f-437e-89d2-9dbf0b374cd8",
      "name": "No Invoices to Process",
      "type": "n8n-nodes-base.set",
      "position": [
        1152,
        736
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "message",
              "name": "message",
              "type": "string",
              "value": "No unpaid invoices found"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "eff43d91-4552-411e-ba8b-6b20b5edb78d",
      "name": "Aggregate Summary",
      "type": "n8n-nodes-base.aggregate",
      "position": [
        3120,
        528
      ],
      "parameters": {
        "options": {},
        "aggregate": "aggregateAllItemData",
        "destinationFieldName": "processedInvoices"
      },
      "typeVersion": 1
    },
    {
      "id": "41bdc652-c58e-4bd6-95f1-69b50f5d37aa",
      "name": "Generate Summary Report",
      "type": "n8n-nodes-base.set",
      "position": [
        3344,
        528
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "totalProcessed",
              "name": "totalProcessed",
              "type": "number",
              "value": "={{ $json.processedInvoices.length }}"
            },
            {
              "id": "reportDate",
              "name": "reportDate",
              "type": "string",
              "value": "={{ $now.toFormat(\"yyyy-MM-dd\") }}"
            },
            {
              "id": "subject",
              "name": "subject",
              "type": "string",
              "value": "=Payment Reminder Summary - {{ $now.toFormat(\"MMMM d, yyyy\") }}"
            }
          ]
        },
        "includeOtherFields": true
      },
      "typeVersion": 3.4
    },
    {
      "id": "a1e04024-7c83-4ab4-89b4-dffebe764167",
      "name": "Create HTML Report",
      "type": "n8n-nodes-base.html",
      "position": [
        3568,
        528
      ],
      "parameters": {
        "html": "<html>\n<head><style>body { font-family: Arial, sans-serif; } table { border-collapse: collapse; width: 100%; } th, td { border: 1px solid #ddd; padding: 8px; text-align: left; } th { background-color: #4CAF50; color: white; }</style></head>\n<body>\n<h2>Payment Reminder Summary - {{ $json.reportDate }}</h2>\n<p><strong>Total Invoices Processed:</strong> {{ $json.totalProcessed }}</p>\n<table>\n<tr><th>Invoice ID</th><th>Reminder Stage</th></tr>\n{{ $json.processedInvoices.map(inv => \"<tr><td>\" + inv.InvoiceID + \"</td><td>\" + inv.stage + \"</td></tr>\").join(\"\") }}\n</table>\n</body>\n</html>"
      },
      "typeVersion": 1.2
    },
    {
      "id": "af2e4353-d430-4dff-b7f6-005f4c4dd183",
      "name": "Send Summary to Finance",
      "type": "n8n-nodes-base.gmail",
      "position": [
        3792,
        528
      ],
      "parameters": {
        "sendTo": "user@example.com",
        "message": "={{ $json.html }}",
        "options": {},
        "subject": "={{ $(\"Generate Summary Report\").item.json.subject }}"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.2
    }
  ],
  "active": false,
  "settings": {
    "binaryMode": "separate",
    "availableInMCP": false,
    "executionOrder": "v1"
  },
  "versionId": "e2e44012-5d68-4a94-9e60-10871cfeb370",
  "nodeGroups": [],
  "connections": {
    "Daily at 9 AM": {
      "main": [
        [
          {
            "node": "Fetch Unpaid Invoices",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge All Stages": {
      "main": [
        [
          {
            "node": "Aggregate Summary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate Summary": {
      "main": [
        [
          {
            "node": "Generate Summary Report",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create HTML Report": {
      "main": [
        [
          {
            "node": "Send Summary to Finance",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Log Audit 3 Before": {
      "main": [
        [
          {
            "node": "Merge All Stages",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Log Audit 7 Before": {
      "main": [
        [
          {
            "node": "Merge All Stages",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update DB 3 Before": {
      "main": [
        [
          {
            "node": "Log Audit 3 Before",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update DB 7 Before": {
      "main": [
        [
          {
            "node": "Log Audit 7 Before",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Log Audit 1 Overdue": {
      "main": [
        [
          {
            "node": "Merge All Stages",
            "type": "main",
            "index": 3
          }
        ]
      ]
    },
    "Log Audit 7 Overdue": {
      "main": [
        [
          {
            "node": "Merge All Stages",
            "type": "main",
            "index": 4
          }
        ]
      ]
    },
    "Log Audit Due Today": {
      "main": [
        [
          {
            "node": "Merge All Stages",
            "type": "main",
            "index": 2
          }
        ]
      ]
    },
    "Send Email 3 Before": {
      "main": [
        [
          {
            "node": "Update DB 3 Before",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Email 7 Before": {
      "main": [
        [
          {
            "node": "Update DB 7 Before",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update DB 1 Overdue": {
      "main": [
        [
          {
            "node": "Log Audit 1 Overdue",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update DB 7 Overdue": {
      "main": [
        [
          {
            "node": "Log Audit 7 Overdue",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update DB Due Today": {
      "main": [
        [
          {
            "node": "Log Audit Due Today",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has Unpaid Invoices?": {
      "main": [
        [
          {
            "node": "Process Each Invoice",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No Invoices to Process",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Log Audit 15 Overdue": {
      "main": [
        [
          {
            "node": "Merge All Stages",
            "type": "main",
            "index": 5
          }
        ]
      ]
    },
    "Process Each Invoice": {
      "main": [
        [
          {
            "node": "Merge All Stages",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Calculate Days Until Due",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Email 1 Overdue": {
      "main": [
        [
          {
            "node": "Update DB 1 Overdue",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Email 7 Overdue": {
      "main": [
        [
          {
            "node": "Update DB 7 Overdue",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Email Due Today": {
      "main": [
        [
          {
            "node": "Update DB Due Today",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update DB 15 Overdue": {
      "main": [
        [
          {
            "node": "Log Audit 15 Overdue",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Unpaid Invoices": {
      "main": [
        [
          {
            "node": "Has Unpaid Invoices?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Email 15 Overdue": {
      "main": [
        [
          {
            "node": "Update DB 15 Overdue",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Already Sent 3 Before?": {
      "main": [
        [
          {
            "node": "Prepare Email 3 Before",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Already Sent 7 Before?": {
      "main": [
        [
          {
            "node": "Prepare Email 7 Before",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Email 3 Before": {
      "main": [
        [
          {
            "node": "Send Email 3 Before",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Email 7 Before": {
      "main": [
        [
          {
            "node": "Send Email 7 Before",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Already Sent 1 Overdue?": {
      "main": [
        [
          {
            "node": "Prepare Email 1 Overdue",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Already Sent 7 Overdue?": {
      "main": [
        [
          {
            "node": "Prepare Email 7 Overdue",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Already Sent Due Today?": {
      "main": [
        [
          {
            "node": "Prepare Email Due Today",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Summary Report": {
      "main": [
        [
          {
            "node": "Create HTML Report",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Email 1 Overdue": {
      "main": [
        [
          {
            "node": "Send Email 1 Overdue",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Email 7 Overdue": {
      "main": [
        [
          {
            "node": "Send Email 7 Overdue",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Email Due Today": {
      "main": [
        [
          {
            "node": "Send Email Due Today",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Route to Reminder Stage": {
      "main": [
        [
          {
            "node": "Already Sent 7 Before?",
            "type": "main",
            "index": 0
          },
          {
            "node": "Process Each Invoice",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Already Sent 3 Before?",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Already Sent Due Today?",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Already Sent 1 Overdue?",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Already Sent 7 Overdue?",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Already Sent 15 Overdue?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Already Sent 15 Overdue?": {
      "main": [
        [
          {
            "node": "Prepare Email 15 Overdue",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calculate Days Until Due": {
      "main": [
        [
          {
            "node": "Route to Reminder Stage",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Email 15 Overdue": {
      "main": [
        [
          {
            "node": "Send Email 15 Overdue",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}