{
  "name": "Daily Revenue & Remaining Report",
  "nodes": [
    {
      "parameters": {},
      "id": "manual-trigger",
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        260,
        260
      ]
    },
    {
      "parameters": {
        "host": "DESKTOP-0RJ1A8T\\NADINE",
        "database": "Sougui_DWH",
        "user": "",
        "password": "",
        "ssl": false,
        "queries": [
          {
            "query": "SELECT CAST(SUM(CAST(reste_du AS FLOAT)) AS DECIMAL(15,2)) as total_reste FROM Fact_Achat WHERE CAST(CONVERT(DATE, GETDATE()) AS DATE) = CAST(CONVERT(DATE, GETDATE()) AS DATE)"
          }
        ]
      },
      "id": "sql-reste-du",
      "name": "SQL - Get Reste_du",
      "type": "n8n-nodes-base.mssql",
      "typeVersion": 2.0,
      "position": [
        450,
        250
      ]
    },
    {
      "parameters": {
        "host": "DESKTOP-0RJ1A8T\\NADINE",
        "database": "Sougui_DWH",
        "user": "",
        "password": "",
        "ssl": false,
        "queries": [
          {
            "query": "SELECT CAST(SUM(CAST(Total_TTC AS FLOAT)) AS DECIMAL(15,2)) as ca_b2b FROM Fact_V_B2B UNION ALL SELECT CAST(SUM(CAST(Montant_total_de_la_commande AS FLOAT)) AS DECIMAL(15,2)) as ca_b2c FROM Fact_V_B2C"
          }
        ]
      },
      "id": "sql-ca",
      "name": "SQL - Get Chiffre Affaire",
      "type": "n8n-nodes-base.mssql",
      "typeVersion": 2.0,
      "position": [
        450,
        400
      ]
    },
    {
      "parameters": {
        "functionCode": "const reste = $input.item.json.total_reste || 0;\\nconst ca_b2b = $input.all()[1].json.ca_b2b || 0;\\nconst ca_b2c = $input.all()[1].json.ca_b2c || 0;\\nconst total_ca = ca_b2b + ca_b2c;\\nconst today = new Date().toLocaleDateString('fr-FR');\\n\\nreturn {\\n  date: today,\\n  reste_du: reste.toLocaleString('fr-FR', {minimumFractionDigits: 2, maximumFractionDigits: 2}),\\n  ca_total: total_ca.toLocaleString('fr-FR', {minimumFractionDigits: 2, maximumFractionDigits: 2}),\\n  ca_b2b: ca_b2b.toLocaleString('fr-FR', {minimumFractionDigits: 2, maximumFractionDigits: 2}),\\n  ca_b2c: ca_b2c.toLocaleString('fr-FR', {minimumFractionDigits: 2, maximumFractionDigits: 2})\\n}"
      },
      "id": "format-data",
      "name": "Format Data",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2.0,
      "position": [
        650,
        325
      ]
    },
    {
      "parameters": {
        "fromEmail": "your-email@gmail.com",
        "toEmail": "nadinechiha50@gmail.com",
        "subject": "\ud83d\udcca Rapport Quotidien - Chiffre d'Affaire & Reste D\u00fb",
        "emailType": "html",
        "htmlEmail": "<html><body>Rapport quotidien - {{ $node[\"Format Data\"].json.date }}</body></html>"
      },
      "id": "send-email",
      "name": "Send Email",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 1.0,
      "position": [
        850,
        325
      ]
    }
  ],
  "connections": {
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "SQL - Get Reste_du",
            "type": "main",
            "index": 0
          },
          {
            "node": "SQL - Get Chiffre Affaire",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SQL - Get Reste_du": {
      "main": [
        [
          {
            "node": "Format Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SQL - Get Chiffre Affaire": {
      "main": [
        [
          {
            "node": "Format Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Data": {
      "main": [
        [
          {
            "node": "Send Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {},
  "versionId": "1"
}