Skip to Content
Menu
This question has been flagged
357 Views

Hello, I am creating an automation so that I can fill in the name, opportunity, value, email and phone number through an external page. When I click on register, the system should automatically send this data to Odoo, create the Lead and automatically convert the lead into an opportunity. But it is not working, it creates the lead, but the opportunity is never created. My code is this:


<?php

require_once("verifica_login.php");


if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    

    ACCESS INFORM


    $nome_cliente     = $_POST['nome_cliente'];

    $nome_negociacao  = $_POST['nome_negociacao'];

    $valor_previsto   = floatval($_POST['valor_previsto']);

    $email            = $_POST['email'];

    $telefone         = $_POST['telefone'];


    // === Autenticação ===

    $common_request = xmlrpc_encode_request("authenticate", [$db, $username, $password, []]);

    $context = stream_context_create(['http' => [

        'method'  => "POST",

        'header'  => "Content-Type: text/xml",

        'content' => $common_request

    ]]);

    $response = file_get_contents("$url/xmlrpc/2/common", false, $context);

    $uid = xmlrpc_decode($response);

    if (!$uid) {

        die("❌ Falha na autenticação com o Odoo.");

    }


    $object_url = "$url/xmlrpc/2/object";


    // === Buscar cliente existente pelo e-mail ===

    $params = [$db, $uid, $password, 'res.partner', 'search_read', [[['email', '=', $email]]], ['fields' => ['id'], 'limit' => 1]];

    $request = xmlrpc_encode_request("execute_kw", $params);

    $context['http']['content'] = $request;

    $response = file_get_contents($object_url, false, $context);

    $partner_data = xmlrpc_decode($response);

    $partner_id = $partner_data[0]['id'] ?? false;


    // === Buscar time de vendas ===

    $params = [$db, $uid, $password, 'crm.team', 'search_read', [[]], ['fields' => ['id'], 'limit' => 1]];

    $request = xmlrpc_encode_request("execute_kw", $params);

    $context['http']['content'] = $request;

    $response = file_get_contents($object_url, false, $context);

    $team_data = xmlrpc_decode($response);

    $team_id = $team_data[0]['id'] ?? null;

    if (!$team_id) {

        die("❌ Nenhum time de vendas encontrado.");

    }


    // === Buscar usuário "Matheus Okazaki" ===

    $params = [$db, $uid, $password, 'res.users', 'search_read', [[['name', '=', 'Matheus Okazaki']]], ['fields' => ['id'], 'limit' => 1]];

    $request = xmlrpc_encode_request("execute_kw", $params);

    $context['http']['content'] = $request;

    $response = file_get_contents($object_url, false, $context);

    $user_data = xmlrpc_decode($response);

    $user_id = $user_data[0]['id'] ?? $uid;


    // === Buscar estágio "Lead Novo" ===

    $params = [$db, $uid, $password, 'crm.stage', 'search_read', [[['name', '=', 'Lead Novo']]], ['fields' => ['id'], 'limit' => 1]];

    $request = xmlrpc_encode_request("execute_kw", $params);

    $context['http']['content'] = $request;

    $response = file_get_contents($object_url, false, $context);

    $stage_data = xmlrpc_decode($response);

    $stage_id = $stage_data[0]['id'] ?? null;

    if (!$stage_id) {

        die("❌ Etapa 'Lead Novo' não encontrada.");

    }


    // === Criar oportunidade diretamente ===

    $opportunity_data = [

        'type' => 'opportunity',

        'name' => $nome_negociacao,

        'contact_name' => $nome_cliente,

        'email_from' => $email,

        'phone' => $telefone,

        'expected_revenue' => $valor_previsto,

        'team_id' => $team_id,

        'user_id' => $user_id,

        'priority' => '2',

        'stage_id' => $stage_id,

        'partner_id' => $partner_id

    ];


    $params = [$db, $uid, $password, 'crm.lead', 'create', [$opportunity_data]];

    $request = xmlrpc_encode_request("execute_kw", $params);

    $context['http']['content'] = $request;

    $response = file_get_contents($object_url, false, $context);

    $opportunity_id = xmlrpc_decode($response);


    if (!is_int($opportunity_id)) {

        echo "<p style='color:red;'> Erro ao criar a oportunidade:</p><pre>";

        print_r($opportunity_id);

        echo "</pre>";

        exit;

    }


    echo "<p style='color:green;'> Oportunidade criada com sucesso! ID: $opportunity_id</p>";


    // === Adicionar nota na oportunidade ===

    $params = [$db, $uid, $password, 'crm.lead', 'message_post', [

        $opportunity_id,

        [

            'body' => 'Nova oportunidade criada',

            'message_type' => 'comment',

        ]

    ]];

    $request = xmlrpc_encode_request("execute_kw", $params);

    $context['http']['content'] = $request;

    file_get_contents($object_url, false, $context);

}

?>


<!-- Formulário HTML -->

<!DOCTYPE html>

<html lang="pt-br">

<head>

    <meta charset="UTF-8">

    <title>Cadastrar Oportunidade no Odoo CRM</title>

</head>

<body>

    <h2>Cadastro de Nova Oportunidade</h2>

    <form method="POST" action="">

        <label>Nome do Cliente:</label><br>

        <input type="text" name="nome_cliente" required><br><br>


        <label>Nome da Negociação:</label><br>

        <input type="text" name="nome_negociacao" required><br><br>


        <label>Valor Previsto (R$):</label><br>

        <input type="number" step="0.01" name="valor_previsto" required><br><br>


        <label>Email:</label><br>

        <input type="email" name="email" required><br><br>


        <label>Telefone:</label><br>

        <input type="text" name="telefone" required><br><br>


        <button type="submit">Cadastrar</button>

    </form>

</body>

</html>


any idea what it's wrong? what should I do exactly?


Avatar
Discard

You should probably try to trim your source down to what is actually relevant to the question and format it so it can be read... You mean to say that
$params = [$db, $uid, $password, 'crm.lead', 'create', [$opportunity_data]];
does not create an crm.lead of type 'opportunity' but of type 'lead'?

Related Posts Replies Views Activity
0
Apr 22
1954
0
Jun 20
6102
1
May 25
94
0
May 25
377
2
May 25
685