{
 "name": "Reporte diario de ventas · Mercado Pago → Telegram",
 "nodes": [
  {
   "id": "90440b9d-7be9-4a57-8747-fae2b6979d80",
   "name": "Nota 898c",
   "type": "n8n-nodes-base.stickyNote",
   "typeVersion": 1,
   "position": [
    -80,
    -360
   ],
   "parameters": {
    "content": "## Reporte diario de ventas\nTodos los días a las 21 suma los pagos aprobados del día en Mercado Pago, los compara con ayer y te manda por Telegram el total, la cantidad de pagos, el ticket promedio, las comisiones, el neto y el detalle por medio de pago.\n\n**Credenciales:** una «Header Auth» `Authorization: Bearer <access token>` de Mercado Pago y Telegram. Hasta 300 pagos por día (para más, se pagina con `offset`).",
    "width": 820,
    "height": 240,
    "color": 6
   }
  },
  {
   "id": "ff50b2ed-b87e-4143-838e-6933587d1a85",
   "name": "Todos los días a las 21",
   "type": "n8n-nodes-base.scheduleTrigger",
   "typeVersion": 1.2,
   "position": [
    0,
    0
   ],
   "parameters": {
    "rule": {
     "interval": [
      {
       "field": "cronExpression",
       "expression": "0 21 * * *"
      }
     ]
    }
   }
  },
  {
   "id": "e23a3087-99be-4a76-a541-1502a5f58714",
   "name": "Probar ahora",
   "type": "n8n-nodes-base.manualTrigger",
   "typeVersion": 1,
   "position": [
    0,
    160
   ],
   "parameters": {}
  },
  {
   "id": "75ec8919-2422-437e-bb00-e331dd675d99",
   "name": "Configuración",
   "type": "n8n-nodes-base.set",
   "typeVersion": 3.4,
   "position": [
    240,
    80
   ],
   "parameters": {
    "assignments": {
     "assignments": [
      {
       "id": "7f6d7df6-1ca5-4d7a-ab54-d6deef543458",
       "name": "negocio",
       "value": "Tu Negocio",
       "type": "string"
      },
      {
       "id": "b80c429b-ddb7-466f-8348-ffd0d90d44bd",
       "name": "moneda",
       "value": "ARS",
       "type": "string"
      },
      {
       "id": "020c6200-b183-4505-8632-c75d04787cc1",
       "name": "zonaHoraria",
       "value": -3,
       "type": "number"
      },
      {
       "id": "d65fac34-073d-40a3-89b7-356165e10d9c",
       "name": "telegramChatId",
       "value": "TU_CHAT_ID",
       "type": "string"
      }
     ]
    },
    "includeOtherFields": true,
    "include": "all",
    "options": {}
   }
  },
  {
   "id": "d3bb811d-3931-43ba-a69e-035d1b68421d",
   "name": "Pagos de hoy",
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2,
   "position": [
    480,
    80
   ],
   "parameters": {
    "method": "GET",
    "url": "https://api.mercadopago.com/v1/payments/search",
    "authentication": "genericCredentialType",
    "genericAuthType": "httpHeaderAuth",
    "options": {},
    "sendQuery": true,
    "queryParameters": {
     "parameters": [
      {
       "name": "status",
       "value": "approved"
      },
      {
       "name": "range",
       "value": "date_approved"
      },
      {
       "name": "sort",
       "value": "date_approved"
      },
      {
       "name": "criteria",
       "value": "desc"
      },
      {
       "name": "limit",
       "value": "300"
      },
      {
       "name": "begin_date",
       "value": "={{ (() => { const z = $('Configuración').first().json.zonaHoraria, d = new Date(Date.now() + z * 3600e3 - 0 * 86400e3); return d.toISOString().slice(0, 10) + 'T00:00:00.000' + (z < 0 ? '-' : '+') + String(Math.abs(z)).padStart(2, '0') + ':00'; })() }}"
      },
      {
       "name": "end_date",
       "value": "={{ (() => { const z = $('Configuración').first().json.zonaHoraria, d = new Date(Date.now() + z * 3600e3 - 0 * 86400e3); return d.toISOString().slice(0, 10) + 'T23:59:59.999' + (z < 0 ? '-' : '+') + String(Math.abs(z)).padStart(2, '0') + ':00'; })() }}"
      }
     ]
    }
   }
  },
  {
   "id": "3a12b4e0-0cc2-4106-a674-f34e4c60df52",
   "name": "Pagos de ayer",
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2,
   "position": [
    720,
    80
   ],
   "parameters": {
    "method": "GET",
    "url": "https://api.mercadopago.com/v1/payments/search",
    "authentication": "genericCredentialType",
    "genericAuthType": "httpHeaderAuth",
    "options": {},
    "sendQuery": true,
    "queryParameters": {
     "parameters": [
      {
       "name": "status",
       "value": "approved"
      },
      {
       "name": "range",
       "value": "date_approved"
      },
      {
       "name": "sort",
       "value": "date_approved"
      },
      {
       "name": "criteria",
       "value": "desc"
      },
      {
       "name": "limit",
       "value": "300"
      },
      {
       "name": "begin_date",
       "value": "={{ (() => { const z = $('Configuración').first().json.zonaHoraria, d = new Date(Date.now() + z * 3600e3 - 1 * 86400e3); return d.toISOString().slice(0, 10) + 'T00:00:00.000' + (z < 0 ? '-' : '+') + String(Math.abs(z)).padStart(2, '0') + ':00'; })() }}"
      },
      {
       "name": "end_date",
       "value": "={{ (() => { const z = $('Configuración').first().json.zonaHoraria, d = new Date(Date.now() + z * 3600e3 - 1 * 86400e3); return d.toISOString().slice(0, 10) + 'T23:59:59.999' + (z < 0 ? '-' : '+') + String(Math.abs(z)).padStart(2, '0') + ':00'; })() }}"
      }
     ]
    }
   }
  },
  {
   "id": "e306fc67-e4e6-4652-bca2-962d10c084b3",
   "name": "Armar el resumen",
   "type": "n8n-nodes-base.code",
   "typeVersion": 2,
   "position": [
    960,
    80
   ],
   "parameters": {
    "jsCode": "// ── Nodo «Armar el resumen» (Code) ──\n// Entrada: los pagos aprobados de hoy y de ayer (búsqueda de la API de Mercado Pago). Salida: el texto del reporte.\nconst cfg = $('Configuración').first().json;\nconst hoy = $('Pagos de hoy').first().json.results || [], ayer = $('Pagos de ayer').first().json.results || [];\nconst dinero = (n) => new Intl.NumberFormat('es-AR', { style: 'currency', currency: cfg.moneda, maximumFractionDigits: 0 }).format(n);\nconst total = (l) => l.reduce((s, p) => s + (Number(p.transaction_amount) || 0), 0);\nconst MEDIOS = { account_money: 'Dinero en cuenta', credit_card: 'Tarjeta de crédito', debit_card: 'Tarjeta de débito', bank_transfer: 'Transferencia', ticket: 'Efectivo', digital_currency: 'Cuotas sin tarjeta' };\nconst porMedio = {};\nfor (const p of hoy) { const m = MEDIOS[p.payment_type_id] || p.payment_type_id || 'Otro'; porMedio[m] = (porMedio[m] || 0) + (Number(p.transaction_amount) || 0); }\nconst tHoy = total(hoy), tAyer = total(ayer), variacion = tAyer ? Math.round((tHoy - tAyer) / tAyer * 100) : null;\nconst comision = hoy.reduce((s, p) => s + (p.fee_details || []).reduce((x, f) => x + (Number(f.amount) || 0), 0), 0);\nconst lineas = [\n  `<b>Resumen de ${cfg.negocio}</b> · ${new Date(Date.now() + cfg.zonaHoraria * 3600e3).toISOString().slice(0, 10).split('-').reverse().join('/')}`,\n  `Cobrado: <b>${dinero(tHoy)}</b> en ${hoy.length} ${hoy.length === 1 ? 'pago' : 'pagos'}${variacion === null ? '' : ` (${variacion >= 0 ? '+' : ''}${variacion}% contra ayer)`}`,\n  hoy.length ? `Ticket promedio: ${dinero(tHoy / hoy.length)} · Comisiones: ${dinero(comision)} · Neto: ${dinero(tHoy - comision)}` : 'Hoy no entraron pagos.',\n  ...Object.entries(porMedio).sort((a, b) => b[1] - a[1]).map(([m, v]) => `• ${m}: ${dinero(v)}`),\n];\nreturn [{ json: { texto: lineas.join('\\n'), total: tHoy, pagos: hoy.length } }];\n"
   }
  },
  {
   "id": "30449c1b-7130-4b1c-b3a1-f398e7e2f3d4",
   "name": "Mandar por Telegram",
   "type": "n8n-nodes-base.telegram",
   "typeVersion": 1.2,
   "position": [
    1200,
    80
   ],
   "parameters": {
    "chatId": "={{ $('Configuración').first().json.telegramChatId }}",
    "text": "={{ $json.texto }}",
    "additionalFields": {
     "parse_mode": "HTML",
     "appendAttribution": false
    }
   }
  }
 ],
 "settings": {
  "executionOrder": "v1",
  "timezone": "America/Argentina/Buenos_Aires"
 },
 "pinData": {},
 "connections": {
  "Todos los días a las 21": {
   "main": [
    [
     {
      "node": "Configuración",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Probar ahora": {
   "main": [
    [
     {
      "node": "Configuración",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Configuración": {
   "main": [
    [
     {
      "node": "Pagos de hoy",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Pagos de hoy": {
   "main": [
    [
     {
      "node": "Pagos de ayer",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Pagos de ayer": {
   "main": [
    [
     {
      "node": "Armar el resumen",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Armar el resumen": {
   "main": [
    [
     {
      "node": "Mandar por Telegram",
      "type": "main",
      "index": 0
     }
    ]
   ]
  }
 }
}