{
  "name": "afip-sdk-zoho-arca-invoice",
  "nodes": [
    {
      "parameters": {
        "path": "zoho-sales-order",
        "httpMethod": "POST",
        "responseMode": "onReceived"
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        0
      ],
      "id": "zoho-sales-order-webhook-6a7ac724",
      "name": "Zoho sales order Webhook"
    },
    {
      "parameters": {
        "method": "GET",
        "url": "https://www.zohoapis.com/inventory/v1/salesorders/{{$json.salesorder_id}}?organization_id={{$env.ZOHO_ORGANIZATION_ID}}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        240,
        0
      ],
      "id": "get-zoho-sales-order-780bc1aa",
      "name": "Get Zoho sales order"
    },
    {
      "parameters": {
        "jsCode": "const sale = $('Get Zoho sales order').first().json.salesorder;\nconst customer = sale.customer || {};\nreturn [{ json: {\n  externalId: 'zoho:salesorder:' + sale.salesorder_id,\n  zohoId: sale.salesorder_id,\n  documentType: customer.custom_field_hash?.cf_cuit ? 80 : 99,\n  documentNumber: customer.custom_field_hash?.cf_cuit || 0,\n  customerName: customer.company_name || customer.customer_name,\n  totalAmount: Number(sale.total),\n  netAmount: Number(sale.sub_total),\n  vatAmount: Number(sale.tax_total || 0),\n  items: (sale.line_items || []).map((item) => ({ description: item.name, quantity: Number(item.quantity), unitPrice: Number(item.rate), vatRate: Number(item.tax_percentage || 21) }))\n}}];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        480,
        0
      ],
      "id": "normalize-fiscal-data-84487f2c",
      "name": "Normalize fiscal data"
    },
    {
      "parameters": {
        "additionalFields": {},
        "parametersJson": "{\n  \"environment\": \"dev\",\n  \"tax_id\": \"20409378472\",\n  \"wsid\": \"wsfe\",\n  \"force_create\": false\n}"
      },
      "type": "n8n-nodes-afipsdk.afipSdk",
      "typeVersion": 1,
      "position": [
        720,
        -120
      ],
      "id": "get-arca-authorization-068b8ff5",
      "name": "Get ARCA authorization"
    },
    {
      "parameters": {
        "operation": "makeRequest",
        "parametersJson": "={\n  \"environment\": \"dev\",\n  \"method\": \"FECompUltimoAutorizado\",\n  \"wsid\": \"wsfe\",\n  \"params\": {\n    \"Auth\": {\n      \"Token\": \"{{ $json.token }}\",\n      \"Sign\": \"{{ $json.sign }}\",\n      \"Cuit\": \"20409378472\"\n    },\n    \"PtoVta\": 1,\n    \"CbteTipo\": {{ $('Normalize fiscal data').item.json.voucherType || 11 }}\n  }\n}"
      },
      "type": "n8n-nodes-afipsdk.afipSdk",
      "typeVersion": 1,
      "position": [
        960,
        -120
      ],
      "id": "get-last-voucher-number-69a711d6",
      "name": "Get last voucher number"
    },
    {
      "parameters": {
        "operation": "makeRequest",
        "parametersJson": "={\n  \"environment\": \"dev\",\n  \"method\": \"FECAESolicitar\",\n  \"wsid\": \"wsfe\",\n  \"params\": {\n    \"Auth\": {\n      \"Token\": \"{{ $('Get ARCA authorization').item.json.token }}\",\n      \"Sign\": \"{{ $('Get ARCA authorization').item.json.sign }}\",\n      \"Cuit\": \"20409378472\"\n    },\n    \"FeCAEReq\": {\n      \"FeCabReq\": {\n        \"CantReg\": 1,\n        \"PtoVta\": 1,\n        \"CbteTipo\": {{ $('Normalize fiscal data').item.json.voucherType || 11 }}\n      },\n      \"FeDetReq\": {\n        \"FECAEDetRequest\": {\n          \"Concepto\": 1,\n          \"DocTipo\": {{ $('Normalize fiscal data').item.json.documentType }},\n          \"DocNro\": {{ $('Normalize fiscal data').item.json.documentNumber }},\n          \"CbteDesde\": {{ $json.FECompUltimoAutorizadoResult.CbteNro + 1 }},\n          \"CbteHasta\": {{ $json.FECompUltimoAutorizadoResult.CbteNro + 1 }},\n          \"CbteFch\": {{ new Date().toISOString().slice(0, 10).replace(/-/g, \"\") }},\n          \"ImpTotal\": {{ $('Normalize fiscal data').item.json.totalAmount }},\n          \"ImpTotConc\": 0,\n          \"ImpNeto\": {{ $('Normalize fiscal data').item.json.netAmount }},\n          \"ImpOpEx\": 0,\n          \"ImpIVA\": {{ $('Normalize fiscal data').item.json.vatAmount }},\n          \"ImpTrib\": 0,\n          \"MonId\": \"PES\",\n          \"MonCotiz\": 1,\n          \"CondicionIVAReceptorId\": 5\n        }\n      }\n    }\n  }\n}"
      },
      "type": "n8n-nodes-afipsdk.afipSdk",
      "typeVersion": 1,
      "position": [
        1200,
        0
      ],
      "id": "create-arca-invoice-c704cf50",
      "name": "Create ARCA invoice"
    },
    {
      "parameters": {
        "resource": "pdf",
        "parametersJson": "={\n  \"template_id\": \"{{ $env.AFIP_SDK_INVOICE_TEMPLATE_ID }}\",\n  \"file_name\": \"{{ $('Normalize fiscal data').item.json.externalId }}.pdf\",\n  \"data\": {\n    \"emitter\": {\n      \"cuit\": \"20409378472\",\n      \"point_of_sale\": 1\n    },\n    \"receiver\": {\n      \"name\": \"{{ $('Normalize fiscal data').item.json.customerName }}\",\n      \"document_type\": {{ $('Normalize fiscal data').item.json.documentType }},\n      \"document_number\": \"{{ $('Normalize fiscal data').item.json.documentNumber }}\"\n    },\n    \"voucher\": {\n      \"type\": {{ $('Normalize fiscal data').item.json.voucherType || 11 }},\n      \"number\": {{ $('Get last voucher number').item.json.FECompUltimoAutorizadoResult.CbteNro + 1 }},\n      \"cae\": \"{{ $('Create ARCA invoice').item.json.FECAESolicitarResult.FeDetResp.FECAEDetResponse.CAE }}\",\n      \"cae_expiration\": \"{{ $('Create ARCA invoice').item.json.FECAESolicitarResult.FeDetResp.FECAEDetResponse.CAEFchVto }}\",\n      \"date\": \"{{ new Date().toISOString().slice(0, 10) }}\"\n    },\n    \"amounts\": {\n      \"net\": {{ $('Normalize fiscal data').item.json.netAmount }},\n      \"vat\": {{ $('Normalize fiscal data').item.json.vatAmount }},\n      \"total\": {{ $('Normalize fiscal data').item.json.totalAmount }}\n    },\n    \"items\": {{ JSON.stringify($('Normalize fiscal data').item.json.items || []) }}\n  }\n}"
      },
      "type": "n8n-nodes-afipsdk.afipSdk",
      "typeVersion": 1,
      "position": [
        1440,
        0
      ],
      "id": "generate-invoice-pdf-zoho-arca-invoice",
      "name": "Generate invoice PDF"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "name": "externalId",
              "value": "={{ $('Normalize fiscal data').item.json.externalId }}",
              "type": "string"
            },
            {
              "name": "cae",
              "value": "={{ $('Create ARCA invoice').item.json.FECAESolicitarResult.FeDetResp.FECAEDetResponse.CAE }}",
              "type": "string"
            },
            {
              "name": "invoiceNumber",
              "value": "={{ $('Get last voucher number').item.json.FECompUltimoAutorizadoResult.CbteNro + 1 }}",
              "type": "number"
            },
            {
              "name": "caeExpiration",
              "value": "={{ $('Create ARCA invoice').item.json.FECAESolicitarResult.FeDetResp.FECAEDetResponse.CAEFchVto }}",
              "type": "string"
            },
            {
              "name": "pdfUrl",
              "value": "={{ $json.file }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1680,
        0
      ],
      "id": "prepare-invoice-result-3353af3b",
      "name": "Prepare invoice result"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://www.zohoapis.com/inventory/v1/salesorders/{{$json.zohoId}}?organization_id={{$env.ZOHO_ORGANIZATION_ID}}",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "external_id",
              "value": "={{ $json.externalId }}"
            },
            {
              "name": "cae",
              "value": "={{ $json.cae }}"
            },
            {
              "name": "invoice_number",
              "value": "={{ $json.invoiceNumber }}"
            },
            {
              "name": "pdf_url",
              "value": "={{ $json.pdfUrl }}"
            },
            {
              "name": "cae_expiration",
              "value": "={{ $json.caeExpiration }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1920,
        0
      ],
      "id": "update-zoho-custom-fields-ad903cb3",
      "name": "Update Zoho custom fields"
    }
  ],
  "pinData": {},
  "connections": {
    "Zoho sales order Webhook": {
      "main": [
        [
          {
            "node": "Get Zoho sales order",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Zoho sales order": {
      "main": [
        [
          {
            "node": "Normalize fiscal data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize fiscal data": {
      "main": [
        [
          {
            "node": "Get ARCA authorization",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get ARCA authorization": {
      "main": [
        [
          {
            "node": "Get last voucher number",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get last voucher number": {
      "main": [
        [
          {
            "node": "Create ARCA invoice",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create ARCA invoice": {
      "main": [
        [
          {
            "node": "Generate invoice PDF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare invoice result": {
      "main": [
        [
          {
            "node": "Update Zoho custom fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate invoice PDF": {
      "main": [
        [
          {
            "node": "Prepare invoice result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "template",
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "tags": []
}
