{
  "name": "Rodopi Dent - Finance ADD",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "finance-add",
        "responseMode": "responseNode",
        "options": {
          "allowedOrigins": "*"
        }
      },
      "id": "webhook-finance-add",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "const body = $input.first().json.body;\n\n// Required fields\nconst required = ['date', 'type', 'amount'];\nconst missing = required.filter(field => body[field] === undefined && body[field] !== 0);\n\nif (missing.length > 0) {\n  return [{\n    json: {\n      success: false,\n      error: `\u041b\u0438\u043f\u0441\u0432\u0430\u0449\u0438 \u043f\u043e\u043b\u0435\u0442\u0430: ${missing.join(', ')}`,\n      valid: false\n    }\n  }];\n}\n\n// Validate type\nconst validTypes = ['income', 'expense'];\nif (!validTypes.includes(body.type)) {\n  return [{\n    json: {\n      success: false,\n      error: 'type \u0442\u0440\u044f\u0431\u0432\u0430 \u0434\u0430 \u0435 income \u0438\u043b\u0438 expense',\n      valid: false\n    }\n  }];\n}\n\n// Generate unique ID\nconst id = 'fin_' + Date.now().toString(36) + Math.random().toString(36).substr(2, 6);\n\n// Amount: positive for income, negative for expense\nlet amount = parseFloat(body.amount);\nif (body.type === 'expense' && amount > 0) {\n  amount = -amount;\n}\n\nreturn [{\n  json: {\n    id: id,\n    date: body.date,\n    type: body.type,\n    amount: amount,\n    description: body.description || '',\n    paymentMethod: body.paymentMethod || 'cash',\n    category: body.category || (body.type === 'income' ? 'private' : 'other'),\n    patientName: body.patientName || '',\n    procedureCode: body.procedureCode || '',\n    procedureName: body.procedureName || '',\n    nhifAmount: parseFloat(body.nhifAmount) || 0,\n    patientAmount: parseFloat(body.patientAmount) || 0,\n    serviceCategory: body.serviceCategory || '',\n    remainingPayment: parseFloat(body.remainingPayment) || 0,\n    createdAt: new Date().toISOString(),\n    valid: true\n  }\n}];"
      },
      "id": "prepare-record",
      "name": "Prepare Record",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        0
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $json.valid }}",
              "value2": true
            }
          ]
        }
      },
      "id": "is-valid",
      "name": "Is Valid?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        440,
        0
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "1hv4XAfHhScA40Bm1kQ3I-Ih4SJuCBpOJxTOYDNb167g"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Finances"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "id": "={{ $json.id }}",
            "date": "={{ $json.date }}",
            "type": "={{ $json.type }}",
            "patientName": "={{ $json.patientName }}",
            "category": "={{ $json.category }}",
            "procedureCode": "={{ $json.procedureCode }}",
            "procedureName": "={{ $json.procedureName }}",
            "nhifAmount": "={{ $json.nhifAmount }}",
            "patientAmount": "={{ $json.patientAmount }}",
            "amount": "={{ $json.amount }}",
            "description": "={{ $json.description }}",
            "paymentMethod": "={{ $json.paymentMethod }}",
            "createdAt": "={{ $json.createdAt }}",
            "serviceCategory": "={{ $json.serviceCategory }}",
            "remainingPayment": "={{ $json.remainingPayment }}"
          }
        },
        "options": {}
      },
      "id": "save-finance",
      "name": "Save Finance",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        660,
        -50
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={\n  \"success\": true,\n  \"message\": \"\u0417\u0430\u043f\u0438\u0441\u044a\u0442 \u0435 \u0434\u043e\u0431\u0430\u0432\u0435\u043d\",\n  \"id\": \"{{ $('Prepare Record').first().json.id }}\",\n  \"amount\": {{ $('Prepare Record').first().json.amount }}\n}",
        "options": {
          "responseHeaders": {
            "entries": [
              {
                "name": "Access-Control-Allow-Origin",
                "value": "*"
              }
            ]
          }
        }
      },
      "id": "respond-success",
      "name": "Respond Success",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        880,
        -50
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={\n  \"success\": false,\n  \"error\": \"{{ $json.error }}\"\n}",
        "options": {
          "responseCode": "400",
          "responseHeaders": {
            "entries": [
              {
                "name": "Access-Control-Allow-Origin",
                "value": "*"
              }
            ]
          }
        }
      },
      "id": "respond-error",
      "name": "Respond Error",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        660,
        100
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Prepare Record",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Record": {
      "main": [
        [
          {
            "node": "Is Valid?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Valid?": {
      "main": [
        [
          {
            "node": "Save Finance",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Respond Error",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Save Finance": {
      "main": [
        [
          {
            "node": "Respond Success",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "tags": [
    {
      "name": "Rodopi Dent"
    }
  ]
}