{
  "id": "pzCuU0IAT1KsqT0O",
  "createdAt": "2023-07-13T16:55:17.968Z",
  "updatedAt": "2024-01-18T15:45:52.157Z",
  "name": "[Oscar] GetPurchases",
  "active": false,
  "nodes": [
    {
      "parameters": {
        "method": "POST",
        "url": "https://cumplo.fapro.app/api/v1.0/login",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "email",
              "value": "api@cumplo.com"
            },
            {
              "name": "password",
              "value": "TKB6Thdmt2WacP3B8z9h"
            }
          ]
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            },
            {
              "name": "Accept",
              "value": "application/json"
            }
          ]
        },
        "options": {}
      },
      "id": "4de0f2fc-8530-4820-8979-289935689ca5",
      "name": "LogginFapro",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -3380,
        880
      ],
      "typeVersion": 3
    },
    {
      "parameters": {
        "url": "https://cumplo.fapro.app/api/v1.0/company/connected",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "authorization",
              "value": "=Bearer {{ $json[\"data\"][\"accessToken\"] }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            },
            {
              "name": "Accept",
              "value": "application/json"
            }
          ]
        },
        "options": {}
      },
      "id": "d9817397-1003-47cb-b52f-1f054bc3de53",
      "name": "GetBusinessLogged",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -3160,
        880
      ],
      "typeVersion": 3
    },
    {
      "parameters": {
        "jsCode": "// Loop over input items and add a new field\n// called 'myNewField' to the JSON of each one\n//input all, el input es uno solo, el input all trae todo los inputs de ese nodo, en este caso es uno. s epuede tener varios items.\n\nvar data = []\nfor (const item of $input.all()) {\n  \n  data = item.json.data.companies.map(\n    (account) => {\n      return {\n        empresa:{\n      rut: account.rut.substring(0, account.rut.length -2),\n          dv: account.rut.substring(account.rut.length,account.rut.length-1 ),\n          rutdv:account.rut.substring(0, account.rut.length -2)+account.rut.substring(account.rut.length,account.rut.length-1 ),\n       rznSocial: account.razon_social.toUpperCase()\n       }\n      }\n    })\n}\n\nreturn data;"
      },
      "id": "7cb1898d-80d0-465a-affe-fa256e967ec7",
      "name": "Code",
      "type": "n8n-nodes-base.code",
      "position": [
        -2940,
        880
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "number": [
            {
              "name": "rut",
              "value": "={{ parseInt($json[\"empresa\"][\"rut\"]) }}"
            }
          ],
          "string": [
            {
              "name": "RznSocial",
              "value": "={{ $json[\"empresa\"][\"rznSocial\"] }}"
            },
            {
              "name": "dv",
              "value": "={{ $json[\"empresa\"][\"dv\"] }}"
            },
            {
              "name": "rutdv",
              "value": "={{ $json[\"empresa\"][\"rutdv\"] }}"
            }
          ]
        },
        "options": {}
      },
      "id": "dda4f90b-82aa-48d1-a66b-b0e250a9f6d3",
      "name": "BussinessActive",
      "type": "n8n-nodes-base.set",
      "position": [
        -2740,
        500
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "jsCode": "const registeredBusiness = $('RegisteredBusiness').all()\nconst moment = require('moment')\ndata = []\nLastDay = moment().subtract(1, 'day').format('YYYY-MM-DD');\nCurrentDay = moment().format('YYYY-MM-DD')\nconsole.log(LastDay)\nfor(var i = 0 ; i < registeredBusiness.length;i ++){ \n     const empresas= registeredBusiness[i]\n    const empresa = empresas.json.Rut \n    \n      let caca = {\n        empresa:{\n        rut:  empresa,\n        fecha_inicial: LastDay,\n        fecha_final: LastDay}}\n  data.push(caca)\n\n    \n  }\nreturn data\n"
      },
      "id": "c315fe5a-6c91-40d4-b2c9-cc904ba1131b",
      "name": "Code4",
      "type": "n8n-nodes-base.code",
      "position": [
        -2240,
        500
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "number": [
            {
              "name": "Rut",
              "value": "={{ $json[\"rut\"] }}"
            }
          ]
        },
        "options": {}
      },
      "id": "0f382eaa-f03e-42a2-bdac-bf0b213681a7",
      "name": "RegisteredBusiness",
      "type": "n8n-nodes-base.set",
      "position": [
        -2480,
        500
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "operation": "notEqual",
              "value2": "={{ $json[\"data\"][\"facturas_compras\"].length }}"
            }
          ]
        }
      },
      "id": "1e1249ac-79fb-474e-8c93-7373f981d95e",
      "name": "IF1",
      "type": "n8n-nodes-base.if",
      "position": [
        -1200,
        260
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "jsCode": "const rut = $('Split In Batches').first()\n\nconst rutPagador = rut.json.empresa.rut\n\nvar current = new Date().toLocaleString()\nconsole.log('prueba', rutPagador)\n\nlet data = []\nfor (const item of $input.all()) {\n  \n  data = item.json.data.facturas_compras.map(\n    (account) => {\n      let hasCreditNote;\n      if (typeof account.notas_credito_asociadas === 'string'){\n          hasCreditNote = account.notas_credito_asociadas;\n      } else {\n          hasCreditNote = 'Existen Notas de creditos asociadas'; // o cualquier otro valor predeterminado\n      }\n      return {\n        identifier: \n          `${rutPagador}_${account.rut_emisor}_${account.folio}_${account.tipo_documento}`,\n        payerIdentifier: `${rutPagador}`,\n        clientIdentifier: account.rut_emisor,\n        clientName: account.razon_social_emisor,\n        folio:account.folio,\n        invoiceAmount: account.monto_total_factura,\n        documentType: account.tipo_documento,\n        invoiceDate: account.fecha_documento,\n        invoiceReceptionDate: account.fecha_recepcion_sii,\n        cesionStatus: account.estado_cesion,\n        hasCreditNote: hasCreditNote,       \n        eventoReceptor: account.evento_receptor,\n\n      }\n    })\n}\n\nreturn data;"
      },
      "id": "3eecdf80-5d95-4c32-9871-c96e6ced38b8",
      "name": "Code5",
      "type": "n8n-nodes-base.code",
      "position": [
        -880,
        460
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "table": {
          "__rl": true,
          "mode": "list",
          "value": "Purchases",
          "cachedResultName": "Purchases"
        },
        "columns": "identifier,payerIdentifier,clientIdentifier,clientName,folio,invoiceAmount,documentType,invoiceDate,invoiceReceptionDate,cesionStatus,hasCreditNote,eventoReceptor",
        "options": {
          "ignore": true
        }
      },
      "id": "fbdb367d-7e76-4601-8ef4-943a264377a5",
      "name": "MySQL1",
      "type": "n8n-nodes-base.mySql",
      "position": [
        -520,
        460
      ],
      "typeVersion": 1,
      "credentials": {
        "mySql": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "options": {}
      },
      "id": "0c55f4a3-6677-40dc-a3c6-1a40b383b92f",
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "position": [
        -700,
        460
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "options": {}
      },
      "id": "8809a094-9dd2-4299-b750-e4c9410c2beb",
      "name": "Set1",
      "type": "n8n-nodes-base.set",
      "position": [
        -360,
        460
      ],
      "executeOnce": true,
      "typeVersion": 1
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {}
      },
      "id": "244f08f3-2e45-4a3f-861a-579ea119d7cc",
      "name": "Split In Batches",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        -1900,
        280
      ],
      "typeVersion": 2
    },
    {
      "parameters": {
        "url": "https://cumplo.fapro.app/api/v1.0/company/list_docs_purchases",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $node[\"LogginFapro\"].json[\"data\"][\"accessToken\"] }}"
            }
          ]
        },
        "sendBody": true,
        "contentType": "raw",
        "rawContentType": "application/json",
        "body": "={{ $json.empresa }}",
        "options": {}
      },
      "id": "c399d9da-cd14-455e-a413-42dee6b38209",
      "name": "Getpurchase",
      "type": "n8n-nodes-base.httpRequest",
      "maxTries": 2,
      "position": [
        -1520,
        260
      ],
      "retryOnFail": true,
      "typeVersion": 3
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtHour": 3
            }
          ]
        }
      },
      "id": "a0d91b45-4e15-4c5b-ac0c-d31656a9f66d",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -3660,
        880
      ],
      "typeVersion": 1.1
    }
  ],
  "connections": {
    "IF1": {
      "main": [
        [
          {
            "node": "Code5",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Split In Batches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "MySQL1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code": {
      "main": [
        [
          {
            "node": "BussinessActive",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set1": {
      "main": [
        [
          {
            "node": "Split In Batches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code4": {
      "main": [
        [
          {
            "node": "Split In Batches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code5": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MySQL1": {
      "main": [
        [
          {
            "node": "Set1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Getpurchase": {
      "main": [
        [
          {
            "node": "IF1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "LogginFapro": {
      "main": [
        [
          {
            "node": "GetBusinessLogged",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "BussinessActive": {
      "main": [
        [
          {
            "node": "RegisteredBusiness",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "LogginFapro",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split In Batches": {
      "main": [
        [
          {
            "node": "Getpurchase",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GetBusinessLogged": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "RegisteredBusiness": {
      "main": [
        [
          {
            "node": "Code4",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "saveDataErrorExecution": "none",
    "saveDataSuccessExecution": "none",
    "saveManualExecutions": false,
    "callerPolicy": "workflowsFromSameOwner"
  },
  "staticData": {
    "node:Schedule Trigger": {
      "recurrencyRules": []
    }
  },
  "versionId": "8a0393eb-d0c0-4fc2-8bf8-d766a3e0d04a",
  "triggerCount": 1,
  "tags": []
}