{
  "name": "Invoice VAT calculation in Airtable",
  "nodes": [
    {
      "id": "inv-trigger",
      "name": "New invoice trigger",
      "type": "n8n-nodes-base.airtableTrigger",
      "typeVersion": 1,
      "position": [
        0,
        0
      ],
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "authentication": "airtableTokenApi",
        "baseId": {
          "__rl": true,
          "mode": "id",
          "value": "appXXXXXXXXXXXXXX"
        },
        "tableId": {
          "__rl": true,
          "mode": "id",
          "value": "tblXXXXXXXXXXXXXX"
        },
        "triggerField": "Created",
        "additionalFields": {}
      }
    },
    {
      "id": "inv-rate",
      "name": "Set VAT rate",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.5,
      "position": [
        224,
        0
      ],
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "vat-rate",
              "name": "vatRate",
              "type": "number",
              "value": "={{ $now.toISODate() >= \"2025-01-01\" ? 0.18 : 0.17 }}"
            }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      }
    },
    {
      "id": "inv-calc",
      "name": "Calculate VAT and total",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.5,
      "position": [
        448,
        0
      ],
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "invoice-number",
              "name": "InvoiceNumber",
              "type": "string",
              "value": "={{ \"INV-\" + $now.toFormat(\"yyyyMMdd\") + \"-\" + String($json.id).slice(-6).toUpperCase() }}"
            },
            {
              "id": "vat-amount",
              "name": "VatAmount",
              "type": "number",
              "value": "={{ Math.round((Number($json.fields.Amount) || 0) * $json.vatRate * 100) / 100 }}"
            },
            {
              "id": "total",
              "name": "Total",
              "type": "number",
              "value": "={{ Math.round(((Number($json.fields.Amount) || 0) * (1 + $json.vatRate)) * 100) / 100 }}"
            },
            {
              "id": "status",
              "name": "Status",
              "type": "string",
              "value": "ValidatedQueued"
            }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      }
    },
    {
      "id": "inv-update",
      "name": "Update invoice",
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 2.2,
      "position": [
        672,
        0
      ],
      "parameters": {
        "operation": "update",
        "base": {
          "__rl": true,
          "mode": "id",
          "value": "appXXXXXXXXXXXXXX"
        },
        "table": {
          "__rl": true,
          "mode": "id",
          "value": "tblXXXXXXXXXXXXXX"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "id": "={{ $json.id }}",
            "InvoiceNumber": "={{ $json.InvoiceNumber }}",
            "VatAmount": "={{ $json.VatAmount }}",
            "Total": "={{ $json.Total }}",
            "Status": "={{ $json.Status }}"
          },
          "schema": [
            {
              "id": "id",
              "displayName": "id",
              "required": false,
              "defaultMatch": true,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "InvoiceNumber",
              "displayName": "InvoiceNumber",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": false
            },
            {
              "id": "VatAmount",
              "displayName": "VatAmount",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": false
            },
            {
              "id": "Total",
              "displayName": "Total",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": false
            },
            {
              "id": "Status",
              "displayName": "Status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": false
            }
          ],
          "matchingColumns": [
            "id"
          ]
        },
        "options": {}
      }
    }
  ],
  "connections": {
    "New invoice trigger": {
      "main": [
        [
          {
            "node": "Set VAT rate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set VAT rate": {
      "main": [
        [
          {
            "node": "Calculate VAT and total",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calculate VAT and total": {
      "main": [
        [
          {
            "node": "Update invoice",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}
