{
  "name": "Analyse Charges Fixes",
  "nodes": [
    {
      "parameters": {
        "path": "analyse-compta",
        "method": "POST",
        "responseMode": "lastNode"
      },
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        100,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://app.pennylane.com/api/analytic_tags",
        "method": "GET",
        "authentication": "headerAuth",
        "headerParameters": [
          {
            "name": "Authorization",
            "value": "Bearer VOTRE_TOKEN_API"
          }
        ]
      },
      "name": "get_tags",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        300,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const tagName = $json[\"body\"][\"category_name\"].toLowerCase();\nconst tags = items[0].json.data;\nconst found = tags.find(tag => tag.attributes.name.toLowerCase() === tagName);\nif (!found) throw new Error(\"Tag non trouv\u00e9 : \" + tagName);\nreturn [{ json: { tag_id: found.id } }];"
      },
      "name": "find_tag_id",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        500,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const year = parseInt($json[\"body\"][\"year\"]);\nconst month = parseInt($json[\"body\"][\"month\"]);\nconst start = new Date(year, month - 1, 1);\nconst end = new Date(year, month, 0);\nconst startPrev = new Date(year, month - 2, 1);\nconst endPrev = new Date(year, month - 1, 0);\nfunction format(d) { return d.toISOString().split('T')[0]; }\nreturn [{ json: { mois_demande_start: format(start), mois_demande_end: format(end), mois_avant_start: format(startPrev), mois_avant_end: format(endPrev), tag_id: $json[\"tag_id\"] } }];"
      },
      "name": "build_dates",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        700,
        300
      ]
    },
    {
      "parameters": {
        "url": "={{`https://app.pennylane.com/api/reports/analytics?start_date=${$json[\"mois_demande_start\"]}&end_date=${$json[\"mois_demande_end\"]}&tag_ids[]=${$json[\"tag_id\"]}`}}",
        "method": "GET",
        "authentication": "headerAuth",
        "headerParameters": [
          {
            "name": "Authorization",
            "value": "Bearer VOTRE_TOKEN_API"
          }
        ]
      },
      "name": "get_montant_1",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        900,
        200
      ]
    },
    {
      "parameters": {
        "url": "={{`https://app.pennylane.com/api/reports/analytics?start_date=${$json[\"mois_avant_start\"]}&end_date=${$json[\"mois_avant_end\"]}&tag_ids[]=${$json[\"tag_id\"]}`}}",
        "method": "GET",
        "authentication": "headerAuth",
        "headerParameters": [
          {
            "name": "Authorization",
            "value": "Bearer VOTRE_TOKEN_API"
          }
        ]
      },
      "name": "get_montant_2",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        900,
        400
      ]
    },
    {
      "parameters": {
        "functionCode": "const montant_1 = items[0].json.data[0].attributes.total;\nconst montant_2 = items[1].json.data[0].attributes.total;\nconst variation = montant_2 === 0 ? 0 : ((montant_1 - montant_2) / montant_2) * 100;\nreturn [{ json: { montant: montant_1, variation: variation.toFixed(1), interpretation: `Les charges fixes sont de ${montant_1.toLocaleString()} \u20ac pour ce mois, soit une ${variation >= 0 ? 'hausse' : 'baisse'} de ${Math.abs(variation).toFixed(1)} % par rapport au mois pr\u00e9c\u00e9dent.` } }];"
      },
      "name": "analyse",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1100,
        300
      ]
    },
    {
      "parameters": {
        "responseMode": "lastNode",
        "responseData": "firstEntryJson",
        "options": {}
      },
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        1300,
        300
      ]
    }
  ],
  "connections": {
    "Webhook": [
      {
        "node": "get_tags",
        "type": "main",
        "index": 0
      }
    ],
    "get_tags": [
      {
        "node": "find_tag_id",
        "type": "main",
        "index": 0
      }
    ],
    "find_tag_id": [
      {
        "node": "build_dates",
        "type": "main",
        "index": 0
      }
    ],
    "build_dates": [
      {
        "node": "get_montant_1",
        "type": "main",
        "index": 0
      },
      {
        "node": "get_montant_2",
        "type": "main",
        "index": 0
      }
    ],
    "get_montant_1": [
      {
        "node": "analyse",
        "type": "main",
        "index": 0
      }
    ],
    "get_montant_2": [
      {
        "node": "analyse",
        "type": "main",
        "index": 1
      }
    ],
    "analyse": [
      {
        "node": "Respond to Webhook",
        "type": "main",
        "index": 0
      }
    ]
  }
}