{
  "name": "Rastreador Automático de Gastos e Ingresos",
  "flow": [
    {
      "id": 1,
      "module": "gmail:watchEmails",
      "version": 2,
      "parameters": {
        "account": "{{GMAIL_CONNECTION}}",
        "label": "INBOX",
        "criteria": "from:(banco OR bank OR factura OR recibo OR notificacion) subject:(transacción OR compra OR depósito OR transferencia OR pago OR factura)"
      },
      "mapper": {},
      "metadata": {
        "designer": { "x": 0, "y": 0 },
        "restore": {},
        "expect": [
          { "name": "account", "type": "text", "label": "Connection" }
        ]
      }
    },
    {
      "id": 2,
      "module": "util:TextParser",
      "version": 1,
      "parameters": {},
      "mapper": {
        "text": "{{1.subject}} {{1.textBody}}",
        "pattern": "\\$([\\d.,]+)|USD\\s*([\\d.,]+)|([\\d.,]+)\\s*(USD|dólares|dolares)"
      },
      "metadata": {
        "designer": { "x": 300, "y": 0 },
        "restore": {},
        "expect": [
          { "name": "text", "type": "text", "label": "Text" },
          { "name": "pattern", "type": "text", "label": "Pattern" }
        ]
      }
    },
    {
      "id": 3,
      "module": "util:TextParser",
      "version": 1,
      "parameters": {},
      "mapper": {
        "text": "{{1.subject}} {{1.textBody}}",
        "pattern": "(\\d{1,2}[/\\-]\\d{1,2}[/\\-]\\d{2,4})"
      },
      "metadata": {
        "designer": { "x": 300, "y": 200 },
        "restore": {},
        "expect": [
          { "name": "text", "type": "text", "label": "Text" },
          { "name": "pattern", "type": "text", "label": "Pattern" }
        ]
      }
    },
    {
      "id": 4,
      "module": "builtin:BasicRouter",
      "version": 1,
      "parameters": {},
      "mapper": {},
      "routes": [
        {
          "label": "Ingreso",
          "filter": {
            "conditions": [
              { "a": "{{lowercase(1.subject + \" \" + 1.textBody)}}", "o": "text:matches", "b": "(depósito|deposito|abono|ingreso|venta|cobro|transferencia recibida)" }
            ]
          },
          "flow": [5]
        },
        {
          "label": "Gasto",
          "filter": {
            "conditions": [
              { "a": "{{lowercase(1.subject + \" \" + 1.textBody)}}", "o": "text:matches", "b": "(compra|pago|débito|debito|retiro|cargo|factura de)" }
            ]
          },
          "flow": [6]
        }
      ],
      "metadata": {
        "designer": { "x": 600, "y": 0 },
        "restore": {},
        "expect": []
      }
    },
    {
      "id": 5,
      "module": "google-sheets:addRow",
      "version": 2,
      "parameters": {
        "spreadsheetId": "{{SPREADSHEET_CONTABILIDAD_ID}}",
        "sheetId": "Ingresos",
        "includeHeaders": true
      },
      "mapper": {
        "values": {
          "0": "{{ifempty(3.match; formatDate(now; \"DD/MM/YYYY\"))}}",
          "1": "Ingreso",
          "2": "{{2.match}}",
          "3": "{{substring(1.subject; 0; 100)}}",
          "4": "{{if(lowercase(1.textBody) contains \"venta\"; \"Ventas\"; if(lowercase(1.textBody) contains \"transferencia\"; \"Transferencias\"; \"Otros Ingresos\"))}}",
          "5": "{{1.from}}",
          "6": "Automático - Gmail"
        }
      },
      "metadata": {
        "designer": { "x": 900, "y": -100 },
        "restore": {
          "spreadsheetId": { "label": "Contabilidad" }
        },
        "expect": [
          { "name": "spreadsheetId", "type": "text", "label": "Spreadsheet ID" }
        ]
      }
    },
    {
      "id": 6,
      "module": "google-sheets:addRow",
      "version": 2,
      "parameters": {
        "spreadsheetId": "{{SPREADSHEET_CONTABILIDAD_ID}}",
        "sheetId": "Gastos",
        "includeHeaders": true
      },
      "mapper": {
        "values": {
          "0": "{{ifempty(3.match; formatDate(now; \"DD/MM/YYYY\"))}}",
          "1": "Gasto",
          "2": "{{2.match}}",
          "3": "{{substring(1.subject; 0; 100)}}",
          "4": "{{if(lowercase(1.textBody) contains \"luz\"; \"Servicios Básicos\"; if(lowercase(1.textBody) contains \"agua\"; \"Servicios Básicos\"; if(lowercase(1.textBody) contains \"internet\"; \"Servicios Básicos\"; if(lowercase(1.textBody) contains \"materia\"; \"Materia Prima\"; if(lowercase(1.textBody) contains \"sueldo\"; \"Nómina\"; \"Otros Gastos\")))))}}",
          "5": "{{1.from}}",
          "6": "Automático - Gmail",
          "7": "{{formatNumber(parseFloat(2.match) * 0.15; 2)}}"
        }
      },
      "metadata": {
        "designer": { "x": 900, "y": 100 },
        "restore": {
          "spreadsheetId": { "label": "Contabilidad" }
        },
        "expect": [
          { "name": "spreadsheetId", "type": "text", "label": "Spreadsheet ID" }
        ]
      }
    },
    {
      "id": 7,
      "module": "builtin:BasicSchedule",
      "version": 1,
      "parameters": {
        "interval": "monthly",
        "day": 28,
        "time": "18:00"
      },
      "mapper": {},
      "metadata": {
        "designer": { "x": 0, "y": 400 },
        "restore": {},
        "expect": []
      }
    },
    {
      "id": 8,
      "module": "google-docs:createFromTemplate",
      "version": 1,
      "parameters": {
        "templateId": "{{PLANTILLA_RESUMEN_MENSUAL_ID}}",
        "folderId": "{{CARPETA_CONTABILIDAD_ID}}",
        "name": "Resumen_Mensual_{{formatDate(now; \"YYYY-MM\")}}"
      },
      "mapper": {
        "replacements": {
          "{{MES}}": "{{formatDate(now; \"MMMM YYYY\")}}",
          "{{TOTAL_INGRESOS}}": "${{SUMA_INGRESOS}}",
          "{{TOTAL_GASTOS}}": "${{SUMA_GASTOS}}",
          "{{UTILIDAD_BRUTA}}": "${{SUMA_INGRESOS - SUMA_GASTOS}}",
          "{{IVA_VENTAS}}": "${{IVA_VENTAS_TOTAL}}",
          "{{IVA_COMPRAS}}": "${{IVA_COMPRAS_TOTAL}}",
          "{{IVA_A_PAGAR}}": "${{IVA_VENTAS_TOTAL - IVA_COMPRAS_TOTAL}}"
        }
      },
      "metadata": {
        "designer": { "x": 300, "y": 400 },
        "restore": {},
        "expect": [
          { "name": "templateId", "type": "text", "label": "Template ID" }
        ]
      }
    }
  ],
  "metadata": {
    "instant": true,
    "version": 1,
    "scenario": {
      "roundtrips": 1,
      "maxErrors": 3,
      "autoCommit": true,
      "autoCommitTriggerLast": true,
      "sequential": false
    },
    "designer": {
      "orphans": []
    },
    "zone": "us1.make.com"
  }
}