{
  "name": "Syscom \u279c Odoo 17 Sync (con im\u00e1genes)",
  "active": false,
  "nodes": [
    {
      "id": 1,
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        100,
        200
      ]
    },
    {
      "id": 2,
      "name": "Get Syscom Token",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 2,
      "position": [
        300,
        200
      ],
      "parameters": {
        "url": "https://developers.syscom.mx/oauth/token",
        "method": "POST",
        "contentType": "form-urlencoded",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "client_id",
              "value": "={{$env.SYSCOM_CLIENT_ID}}"
            },
            {
              "name": "client_secret",
              "value": "={{$env.SYSCOM_CLIENT_SECRET}}"
            },
            {
              "name": "grant_type",
              "value": "client_credentials"
            }
          ]
        },
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/x-www-form-urlencoded"
            }
          ]
        }
      }
    },
    {
      "id": 3,
      "name": "Init Page",
      "type": "n8n-nodes-base.set",
      "typeVersion": 2,
      "position": [
        500,
        200
      ],
      "parameters": {
        "values": {
          "number": [
            {
              "name": "page",
              "value": 1
            },
            {
              "name": "per_page",
              "value": 2
            }
          ]
        }
      }
    },
    {
      "id": 4,
      "name": "HTTP Syscom Products",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 2,
      "position": [
        700,
        200
      ],
      "parameters": {
        "url": "https://developers.syscom.mx/api/v1/productos",
        "method": "GET",
        "sendQuery": true,
        "queryParametersUi": {
          "parameter": [
            {
              "name": "per_page",
              "value": "={{$node[\"Init Page\"].json.per_page}}"
            },
            {
              "name": "page",
              "value": "={{$node[\"Init Page\"].json.page}}"
            }
          ]
        },
        "sendHeaders": true,
        "headerParametersUi": {
          "parameter": [
            {
              "name": "Authorization",
              "value": "=Bearer {{$node[\"Get Syscom Token\"].json.access_token}}"
            }
          ]
        }
      }
    },
    {
      "id": 5,
      "name": "SplitInBatches",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 1,
      "position": [
        900,
        200
      ],
      "parameters": {
        "batchSize": 50
      }
    },
    {
      "id": 6,
      "name": "HTTP Imagen",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 2,
      "position": [
        1100,
        140
      ],
      "parameters": {
        "url": "={{$json.img_portada}}",
        "responseFormat": "binary",
        "dataPropertyName": "data"
      }
    },
    {
      "id": 7,
      "name": "Binary \u2192 Base64",
      "type": "n8n-nodes-base.moveBinaryData",
      "typeVersion": 1,
      "position": [
        1100,
        260
      ],
      "parameters": {
        "mode": "binaryToJson",
        "sourceKey": "data",
        "destinationKey": "data"
      }
    },
    {
      "id": 8,
      "name": "Map Syscom \u279c Odoo",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1300,
        200
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "functionCode": "// margen y cuentas contables\nconst GASTO = { CCTV:5101, INCENDIO:5102, POS:5103, ACCESO:5104, REDES:5105, AUDIOVIDEO:5106 };\nconst MARGEN = 0.7;\nreturn items.map(i => {\n  const p = i.json;\n  const costo = parseFloat(p.precios?.precio_descuento || 0);\n  const venta = +(costo / MARGEN).toFixed(2);\n  const fam = (p.familia || '').toUpperCase();\n  return {\n    json: {\n      sku: p.sku || p.modelo,\n      name: p.titulo,\n      standard_price: costo,\n      list_price: venta,\n      qty_available: p.total_existencia,\n      property_account_income_id: 4010102,\n      property_account_expense_id: GASTO[fam] || 5100,\n      unspsc_code: p.clave_unidad_sat,\n      image_1920: i.json.data\n    }\n  };\n});"
      }
    },
    {
      "id": 9,
      "name": "search_read",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 2,
      "position": [
        1500,
        200
      ],
      "parameters": {
        "url": "https://{{$env.ODOO_DOMAIN}}/jsonrpc",
        "method": "POST",
        "sendHeaders": true,
        "headerParametersUi": {
          "parameter": [
            {
              "name": "Content-Type",
              "value": "application/json"
            },
            {
              "name": "X-Api-Key",
              "value": "={{$credentials.ODOO_API_KEY}}"
            }
          ]
        },
        "specifyBody": "json",
        "jsonBody": "={\"jsonrpc\":\"2.0\",\"method\":\"call\",\"params\":{\"service\":\"object\",\"method\":\"execute_kw\",\"args\":[\"{{$env.ODOO_DB}}\",{{$env.ODOO_UID}},\"{{$credentials.ODOO_API_KEY}}\",\"product.template\",\"search_read\",[[[\"default_code\",\"=\",$json.sku]],[\"id\"]]]}}}"
      }
    },
    {
      "id": 10,
      "name": "\u00bfExiste?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        1700,
        200
      ],
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{$json.result.length}}",
              "operation": "largerEqual",
              "value2": 1
            }
          ]
        }
      }
    },
    {
      "id": 11,
      "name": "create",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 2,
      "position": [
        1900,
        100
      ],
      "parameters": {
        "url": "https://{{$env.ODOO_DOMAIN}}/jsonrpc",
        "method": "POST",
        "sendHeaders": true,
        "headerParametersUi": {
          "parameter": [
            {
              "name": "Content-Type",
              "value": "application/json"
            },
            {
              "name": "X-Api-Key",
              "value": "={{$credentials.ODOO_API_KEY}}"
            }
          ]
        },
        "specifyBody": "json",
        "jsonBody": "={\"jsonrpc\":\"2.0\",\"method\":\"call\",\"params\":{\"service\":\"object\",\"method\":\"execute_kw\",\"args\":[\"{{$env.ODOO_DB}}\",{{$env.ODOO_UID}},\"{{$credentials.ODOO_API_KEY}}\",\"product.template\",\"create\",[{$json}]]}}}"
      }
    },
    {
      "id": 12,
      "name": "write",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 2,
      "position": [
        1900,
        300
      ],
      "parameters": {
        "url": "https://{{$env.ODOO_DOMAIN}}/jsonrpc",
        "method": "POST",
        "sendHeaders": true,
        "headerParametersUi": {
          "parameter": [
            {
              "name": "Content-Type",
              "value": "application/json"
            },
            {
              "name": "X-Api-Key",
              "value": "={{$credentials.ODOO_API_KEY}}"
            }
          ]
        },
        "specifyBody": "json",
        "jsonBody": "={\"jsonrpc\":\"2.0\",\"method\":\"call\",\"params\":{\"service\":\"object\",\"method\":\"execute_kw\",\"args\":[\"{{$env.ODOO_DB}}\",{{$env.ODOO_UID}},\"{{$credentials.ODOO_API_KEY}}\",\"product.template\",\"write\",[[$json.result[0].id],$json]]}}}"
      }
    },
    {
      "id": 13,
      "name": "Next page?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        900,
        50
      ],
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{$node[\"HTTP Syscom Products\"].json.data.length}}",
              "operation": "larger",
              "value2": 0
            }
          ]
        }
      }
    },
    {
      "id": 14,
      "name": "Inc page",
      "type": "n8n-nodes-base.set",
      "typeVersion": 2,
      "position": [
        700,
        50
      ],
      "parameters": {
        "values": {
          "number": [
            {
              "name": "page",
              "value": "={{$node[\"Init Page\"].json.page + 1}}"
            }
          ]
        }
      }
    }
  ],
  "connections": {
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "Get Syscom Token",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Syscom Token": {
      "main": [
        [
          {
            "node": "Init Page",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Init Page": {
      "main": [
        [
          {
            "node": "HTTP Syscom Products",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Syscom Products": {
      "main": [
        [
          {
            "node": "SplitInBatches",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Next page?",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "SplitInBatches": {
      "main": [
        [
          {
            "node": "HTTP Imagen",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Imagen": {
      "main": [
        [
          {
            "node": "Binary \u2192 Base64",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Binary \u2192 Base64": {
      "main": [
        [
          {
            "node": "Map Syscom \u279c Odoo",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Map Syscom \u279c Odoo": {
      "main": [
        [
          {
            "node": "search_read",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "search_read": {
      "main": [
        [
          {
            "node": "\u00bfExiste?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\u00bfExiste?": {
      "main": [
        [
          {
            "node": "write",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "create",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Next page?": {
      "main": [
        [
          {
            "node": "Inc page",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Inc page": {
      "main": [
        [
          {
            "node": "HTTP Syscom Products",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}