{
  "active": false,
  "activeVersion": null,
  "activeVersionId": null,
  "connections": {
    "Pr\u00e9parer donn\u00e9es": {
      "main": [
        [
          {
            "node": "Copy file",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Remplacer balises": {
      "main": [
        [
          {
            "node": "Exporter PDF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Exporter PDF": {
      "main": [
        [
          {
            "node": "Uploader Drive",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Uploader Drive": {
      "main": [
        [
          {
            "node": "Update a database page",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "R\u00e9cup\u00e9rer Bail": {
      "main": [
        [
          {
            "node": "Locataire",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Locataire": {
      "main": [
        [
          {
            "node": "Logement",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Logement": {
      "main": [
        [
          {
            "node": "Pr\u00e9parer donn\u00e9es",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Copy file": {
      "main": [
        [
          {
            "node": "Remplacer balises",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update a database page": {
      "main": [
        [
          {
            "node": "Create a draft",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When clicking \u2018Execute workflow\u2019": {
      "main": [
        [
          {
            "node": "Get many database pages",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get many database pages": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [],
        [
          {
            "node": "R\u00e9cup\u00e9rer Bail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create a draft": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "createdAt": "2025-11-28T13:59:08.499Z",
  "id": "8hoocGPnyaRiZGb0",
  "isArchived": true,
  "meta": null,
  "name": "[Geston locative] G\u00e9n\u00e9ration quittance - Statut Pay\u00e9",
  "nodes": [
    {
      "parameters": {
        "jsCode": "// ---------- Dates & Mois ----------\nconst now = new Date();\nconst moisCourant = new Date(now.getFullYear(), now.getMonth(), 1);\nconst dernierJour = new Date(now.getFullYear(), now.getMonth() + 1, 0);\n\nconst mois = [\n  'janvier','f\u00e9vrier','mars','avril','mai','juin',\n  'juillet','ao\u00fbt','septembre','octobre','novembre','d\u00e9cembre'\n];\n\n// Mois actuel (ex: \"Novembre\")\nconst moisActuel = mois[now.getMonth()].charAt(0).toUpperCase() + mois[now.getMonth()].slice(1);\n\n// ---------- Utilitaires ----------\nconst getRichText = (prop) =>\n  prop?.rich_text?.[0]?.plain_text ??\n  prop?.rich_text?.[0]?.text?.content ??\n  '';\n\nconst getTitle = (prop) =>\n  prop?.title?.[0]?.plain_text ??\n  prop?.title?.[0]?.text?.content ??\n  '';\n\nconst formatMontant = (val) => (Number(val) || 0).toFixed(2).replace('.', ',');\n\n// ---------- Entr\u00e9es n8n ----------\nconst bailPage       = $node[\"R\u00e9cup\u00e9rer Bail\"]?.json ?? {};\nconst locatairePage  = $node[\"Locataire\"]?.json ?? {};\n\n// Logement : accepte soit items bruts Notion, soit items .json\nconst items = $input.all();\nconst logementPage = (Array.isArray(items) && items.length > 0)\n  ? (items[0].json ?? items[0])\n  : {};\n\n// ---------- Proprio ----------\nconst nomProprietaire = 'Huguet Camille';\nconst adresseProprietaire = '8 rue Leyla Zana 93000 Bobigny';\n\n// ---------- Dates texte ----------\nconst dateDebut = `1er ${mois[moisCourant.getMonth()]} ${moisCourant.getFullYear()}`;\nconst dateFin = `${dernierJour.getDate()} ${mois[moisCourant.getMonth()]} ${moisCourant.getFullYear()}`;\nconst dateSignature = `${now.getDate()} ${mois[now.getMonth()]} ${now.getFullYear()}`;\n\n// ---------- Montants depuis Notion (nouvelle structure .properties) ----------\nconst p = bailPage?.properties ?? {};\nconst loyerNumber   = p?.[\"Montant du loyer HC\"]?.number;\nconst chargesNumber = p?.[\"Montant des charges\"]?.number;\n\n// Fallback sur anciennes cl\u00e9s si tu en as encore besoin\nconst montantLoyer   = (typeof loyerNumber === 'number' ? loyerNumber : bailPage.property_montant_du_loyer_hc || 0);\nconst montantCharges = (typeof chargesNumber === 'number' ? chargesNumber : bailPage.property_montant_des_charges || 0);\nconst montantTotal   = (Number(montantLoyer) || 0) + (Number(montantCharges) || 0);\n\n// ---------- Liens Notion ----------\nconst lienBail = p?.[\"lien du bail\"]?.rich_text?.[0]?.text?.content || '';\n\n// ---------- Locataire (nouvelle structure .properties) ----------\nconst locProps = locatairePage?.properties ?? {};\nconst nomLocataire = getTitle(locProps?.Name);\nconst adresseLocataire = getRichText(locProps?.Address);\nconst lienStockage = locProps?.[\"Lien de stockage des documents\"]?.url || '';\n\n// ---------- Logement (nouvelle structure fournie) ----------\nconst logProps = logementPage?.properties ?? {};\nconst adresseLogement = getRichText(logProps?.Adresse);\nconst nomLogement = getTitle(logProps?.Name);\n\n// ---------- Sortie ----------\nreturn [\n  {\n    json: {\n      nom_proprietaire: nomProprietaire,\n      adresse_proprietaire: adresseProprietaire,\n\n      nom_logement: nomLogement,\n      adresse_logement: adresseLogement,\n\n      nom_locataire: nomLocataire,\n      adresse_locataire: adresseLocataire,\n\n      montant_total:   formatMontant(montantTotal),\n      montant_loyer:   formatMontant(montantLoyer),\n      montant_charges: formatMontant(montantCharges),\n\n      date_debut: dateDebut,\n      date_fin: dateFin,\n      date_signature: dateSignature,\n\n      lien_stockage: lienStockage,\n      lien_bail: lienBail,\n\n      mois_actuel: moisActuel,\n      // Conserve l'underscore si tu l'utilises en nom de fichier\n      mois_annee: `${mois[moisCourant.getMonth()]}_${moisCourant.getFullYear()}`\n    }\n  }\n];\n"
      },
      "id": "57be69fc-2759-4fca-ac95-e009527c510d",
      "name": "Pr\u00e9parer donn\u00e9es",
      "type": "n8n-nodes-base.code",
      "position": [
        880,
        176
      ],
      "typeVersion": 2
    },
    {
      "parameters": {
        "operation": "update",
        "documentURL": "={{ $json.id }}",
        "simple": false,
        "actionsUi": {
          "actionFields": [
            {
              "action": "replaceAll",
              "text": "nom_proprietaire",
              "replaceText": "={{ $('Pr\u00e9parer donn\u00e9es').item.json.nom_proprietaire }}"
            },
            {
              "action": "replaceAll",
              "text": "nom_locataire",
              "replaceText": "={{ $('Pr\u00e9parer donn\u00e9es').item.json.nom_locataire }}"
            },
            {
              "action": "replaceAll",
              "text": "adresse_locataire",
              "replaceText": "={{ $('Pr\u00e9parer donn\u00e9es').item.json.adresse_locataire }}"
            },
            {
              "action": "replaceAll",
              "text": "date_debut",
              "replaceText": "={{ $('Pr\u00e9parer donn\u00e9es').item.json.date_debut }}"
            },
            {
              "action": "replaceAll",
              "text": "date_fin",
              "replaceText": "={{ $('Pr\u00e9parer donn\u00e9es').item.json.date_fin }}"
            },
            {
              "action": "replaceAll",
              "text": "montant_loyer",
              "replaceText": "={{ $('Pr\u00e9parer donn\u00e9es').item.json.montant_loyer }}"
            },
            {
              "action": "replaceAll",
              "text": " montant_charges",
              "replaceText": "={{ $('Pr\u00e9parer donn\u00e9es').item.json.montant_charges }}"
            },
            {
              "action": "replaceAll",
              "text": "date_signature",
              "replaceText": "={{ $('Pr\u00e9parer donn\u00e9es').item.json.date_signature }}"
            },
            {
              "action": "replaceAll",
              "text": "montant_total",
              "replaceText": "={{ $('Pr\u00e9parer donn\u00e9es').item.json.montant_total }}"
            },
            {
              "action": "replaceAll",
              "text": "adresse_logement",
              "replaceText": "={{ $('Pr\u00e9parer donn\u00e9es').item.json.adresse_logement }}"
            }
          ]
        }
      },
      "id": "5c6056c9-cc1a-4de3-97ef-100b4144e111",
      "name": "Remplacer balises",
      "type": "n8n-nodes-base.googleDocs",
      "position": [
        1328,
        176
      ],
      "typeVersion": 2
    },
    {
      "parameters": {
        "url": "=https://www.googleapis.com/drive/v3/files/{{ $('Copy file').item.json.id }}/export?mimeType=application/pdf",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "googleDriveOAuth2Api",
        "options": {}
      },
      "id": "260f3404-75f1-44d3-a7b9-16768c32c61d",
      "name": "Exporter PDF",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1552,
        176
      ],
      "typeVersion": 4.2
    },
    {
      "parameters": {
        "name": "=Quittance loyer {{ $('Locataire').item.json.properties.Name.title[0].text.content }} {{ $('Pr\u00e9parer donn\u00e9es').item.json.mois_actuel }} 2025",
        "driveId": {
          "__rl": true,
          "mode": "list",
          "value": "My Drive"
        },
        "folderId": {
          "__rl": true,
          "value": "={{ $('Locataire').item.json.properties['Lien de stockage des documents'].url }}",
          "mode": "url"
        },
        "options": {}
      },
      "id": "34c812ea-e9a0-46db-8dfa-053250b50b1c",
      "name": "Uploader Drive",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        1776,
        176
      ],
      "typeVersion": 3
    },
    {
      "parameters": {
        "resource": "databasePage",
        "operation": "get",
        "pageId": {
          "__rl": true,
          "value": "={{ $json.properties.bail.relation[0].id }}",
          "mode": "id"
        },
        "simple": false
      },
      "type": "n8n-nodes-base.notion",
      "typeVersion": 2.2,
      "position": [
        48,
        176
      ],
      "id": "dec57f67-b872-4583-afba-18a6c8043773",
      "name": "R\u00e9cup\u00e9rer Bail",
      "credentials": {
        "notionApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "resource": "databasePage",
        "operation": "get",
        "pageId": {
          "__rl": true,
          "value": "={{ $json.properties.Locataires.relation[0].id }}",
          "mode": "id"
        },
        "simple": false
      },
      "type": "n8n-nodes-base.notion",
      "typeVersion": 2.2,
      "position": [
        208,
        176
      ],
      "id": "c07c1bc1-6c33-4692-8dd2-898f6518700f",
      "name": "Locataire",
      "credentials": {
        "notionApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "resource": "databasePage",
        "operation": "get",
        "pageId": {
          "__rl": true,
          "value": "={{ $('R\u00e9cup\u00e9rer Bail').item.json.properties.Logement.relation[0].id }}",
          "mode": "id"
        },
        "simple": false
      },
      "type": "n8n-nodes-base.notion",
      "typeVersion": 2.2,
      "position": [
        432,
        176
      ],
      "id": "dc1a558c-2d66-4bfa-bc70-c8c6eb299910",
      "name": "Logement",
      "credentials": {
        "notionApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "copy",
        "fileId": {
          "__rl": true,
          "value": "1ZXbJIoVjvae2ghwT10cqdryO9WHaLJYvizVImeLwsEk",
          "mode": "list",
          "cachedResultName": "Mod\u00e8le quittance de loyer ",
          "cachedResultUrl": "https://docs.google.com/document/d/1ZXbJIoVjvae2ghwT10cqdryO9WHaLJYvizVImeLwsEk/edit?usp=drivesdk"
        },
        "name": "=Quittance loyer {{ $('Locataire').item.json.properties.Name.title[0].text.content }} {{ $json.mois_actuel }} 2025",
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        1104,
        176
      ],
      "id": "668695e9-4428-4b57-8dbe-7b8ed1dfb557",
      "name": "Copy file"
    },
    {
      "parameters": {
        "resource": "draft",
        "subject": "={{ $('Uploader Drive').item.json.name }}",
        "message": "=Bonjour,\n\nSuite au r\u00e8glement du loyer au {{ $('Logement').item.json.properties.Adresse.rich_text[0].text.content }}, je vous prie de trouver la quittance du mois de {{ $('Pr\u00e9parer donn\u00e9es').item.json.mois_actuel }} :  {{ $('Uploader Drive').item.json.webContentLink }}\n\nEn vous souhaitant bonne r\u00e9ception,\n\nCordialement,\n\nCamille HUGUET",
        "options": {
          "sendTo": "={{ $('Locataire').item.json.properties.Email.rich_text[0].text.content }}"
        }
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        2224,
        176
      ],
      "id": "1b5eb803-c46b-4e84-8f72-b718e55540d1",
      "name": "Create a draft"
    },
    {
      "parameters": {
        "resource": "databasePage",
        "operation": "update",
        "pageId": {
          "__rl": true,
          "value": "={{ $('Get many database pages').item.json.id }}",
          "mode": "id"
        },
        "simple": false,
        "propertiesUi": {
          "propertyValues": [
            {
              "key": "Lien quittance|url",
              "urlValue": "={{ $json.webViewLink }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.notion",
      "typeVersion": 2.2,
      "position": [
        2000,
        176
      ],
      "id": "cefa494b-9efe-4dbd-bd18-c763530151b6",
      "name": "Update a database page",
      "credentials": {
        "notionApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {},
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        -464,
        176
      ],
      "id": "658a64b9-ad5b-4f06-a785-37911fc1edae",
      "name": "When clicking \u2018Execute workflow\u2019"
    },
    {
      "parameters": {
        "resource": "databasePage",
        "operation": "getAll",
        "databaseId": {
          "__rl": true,
          "value": "29c02d84-a064-807b-8f5f-f10110646245",
          "mode": "list",
          "cachedResultName": "Paiement des loyers",
          "cachedResultUrl": "https://www.notion.so/29c02d84a064807b8f5ff10110646245"
        },
        "limit": 5,
        "simple": false,
        "options": {}
      },
      "type": "n8n-nodes-base.notion",
      "typeVersion": 2.2,
      "position": [
        -240,
        176
      ],
      "id": "a067dafe-18e6-4b02-9ed9-48847ec59a04",
      "name": "Get many database pages",
      "credentials": {
        "notionApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        0,
        0
      ],
      "id": "63e12a7e-e90c-48a0-8368-831c186206c0",
      "name": "Loop Over Items"
    }
  ],
  "origin": "n8n",
  "repo": {
    "owner": "kamiyechung",
    "name": "Sauvegarde-n8n"
  },
  "settings": {
    "executionOrder": "v1"
  },
  "shared": [
    {
      "updatedAt": "2025-11-28T13:59:08.499Z",
      "createdAt": "2025-11-28T13:59:08.499Z",
      "role": "workflow:owner",
      "workflowId": "8hoocGPnyaRiZGb0",
      "projectId": "ou1B4ZLA3LmPCUHk"
    }
  ],
  "staticData": null,
  "tags": [
    {
      "updatedAt": "2025-11-26T21:50:45.750Z",
      "createdAt": "2025-11-26T21:50:45.750Z",
      "id": "bVdv5uZuvSz9pfPC",
      "name": "OK"
    },
    {
      "updatedAt": "2025-11-26T21:50:45.756Z",
      "createdAt": "2025-11-26T21:50:45.756Z",
      "id": "TfKV0Tw6506gk5QE",
      "name": "Gestion locative"
    }
  ],
  "triggerCount": 0,
  "updatedAt": "2025-11-28T13:59:17.125Z",
  "versionId": "19e70c27-c7f2-4d80-bd30-352425b131cf"
}