function synchroClient($id_customer)
{
    echo "<br/>Synchronize client : {$id_customer}<br>";
    // retrieve params
    $prefix_ref_client = Configuration::get('prefix_ref_client');
    $prefix_ref_client = accents_sans("{$prefix_ref_client}");
    $client_status = Configuration::get('client_status');
    // retrieve client data
    $donnees_customer = Db::getInstance()->GetRow("select * from " . _DB_PREFIX_ . "customer where id_customer='" . $id_customer . "'");
    //var_dump($donnees_customer);
    $id_gender = $donnees_customer['id_gender'];
    $note = $donnees_customer['note'];
    $note = accents_minuscules("{$note}");
    $birthday = $donnees_customer['birthday'];
    if ($id_gender == 9) {
        $civilite = "";
    } elseif ($id_gender == 1) {
        $civilite = "MR";
    } elseif ($id_gender == 2) {
        $civilite = "MME";
    } elseif ($id_gender == 3) {
        $civilite = "MME";
    } else {
        $civilite = "MR";
    }
    $mail = $donnees_customer['email'];
    echo "Email : {$mail}<br>";
    $dolibarr = Dolibarr::getInstance();
    // Check if already exists in Dolibarr
    $exists = $dolibarr->getUser("PSUSER-" . $id_customer);
    $client = new DolibarrThirdParty();
    $client->ref_ext = "PSUSER-" . $id_customer;
    if ($prefix_ref_client == "") {
        $client->customer_code = -1;
    } else {
        $client->customer_code = $prefix_ref_client . $id_customer;
    }
    $client->status = $client_status;
    $client->ref = $donnees_customer['firstname'] . " " . $donnees_customer['lastname'];
    $client->email = $mail;
    $client->date_modification = new DateTime('NOW');
    $client->url = _PS_BASE_URL_ . __PS_BASE_URI__;
    if ($exists["result"]->result_code == 'NOT_FOUND') {
        // Create new user
        echo "Create new user :"******"result"]->result_code . "<br/>";
        if ($result["result"]->result_code != 'OK') {
            echo "Erreur de synchronisation : " . $result["result"]->result_label;
            var_dump($result);
            return FALSE;
        }
    } else {
        // Update user
        echo "update user : "******"thirdparty"];
        $client->id = $oldClient->id;
        echo $client->id . "<br";
        $result = $dolibarr->updateUser($client);
        echo $result["result"]->result_code . "<br/>";
        if ($result["result"]->result_code != 'OK') {
            echo "Erreur de synchronisation : " . $result["result"]->result_label;
        }
    }
    if ($result["result"]->result_code == 'OK') {
        // synchronize client addresses
        if ($addresses = Db::getInstance()->ExecuteS("select * from " . _DB_PREFIX_ . "address where id_customer='" . $id_customer . "'")) {
            foreach ($addresses as $address) {
                echo "<br/> Synchronize address : ";
                $contact = new DolibarrContact();
                $contact->ref_ext = $address['id_address'];
                $contact->socid = $result["id"];
                $contact->lastname = $address['lastname'];
                $contact->firstname = $address['firstname'];
                $address1 = $address['address1'];
                $address1 = accents_majuscules("{$address1}");
                $address2 = $address['address2'];
                $address2 = accents_majuscules("{$address2}");
                $contact->address = $address1 . ' ' . $address2;
                $contact->zip = $address['postcode'];
                $contact->town = accents_majuscules($address['city']);
                $contact->note = $address['other'];
                //TODO improve country correspondance
                $id_country = $address['id_country'];
                if ($id_country == 8) {
                    $country = 1;
                    // for FRANCE
                } else {
                    $country = "";
                }
                $contact->country_id = $country;
                $phone = $address['phone'];
                $phone = tel_cacateres("{$phone}");
                $mobile = $address['phone_mobile'];
                $mobile = tel_cacateres("{$mobile}");
                $contact->phone_perso = $phone;
                $contact->phone_mobile = $mobile;
                $contact->email = $mail;
                $contact->birthday = $birthday;
                $contact->civility_id = $civilite;
                $result = $dolibarr->getContact($contact->ref_ext);
                if ($result["result"]->result_code == 'NOT_FOUND') {
                    // Create address
                    echo "<br>create address : ";
                    $result = $dolibarr->createContact($contact);
                    echo $result["result"]->result_code . "<br/>";
                    if ($result["result"]->result_code != 'OK') {
                        echo "Erreur de synchronisation address : " . $result["result"]->result_label;
                        var_dump($result);
                        return FALSE;
                    }
                } else {
                    if ($result["result"]->result_code == 'OK') {
                        // Update address
                        echo "<br>update address : ";
                        $contact->id = $result["contact"]->id;
                        // we can't update contact using it's ref_ext so we use id
                        $result = $dolibarr->updateContact($contact);
                        echo $result["result"]->result_code . "<br/>";
                        if ($result["result"]->result_code != 'OK') {
                            echo "Erreur de synchronisation address : " . $result["result"]->result_label;
                            var_dump($result);
                            return FALSE;
                        }
                    } else {
                        echo "Erreur de synchronisation address : " . $result["result"]->result_label;
                        var_dump($result);
                        return FALSE;
                    }
                }
            }
        }
    }
    return TRUE;
}
// FIN RECUP VERSIONS PRESTA *****************************************************
// RECUPERATION DES PARAMETRES ***************************************
$donnees_recup_des_bases = Db::getInstance()->GetRow("select * from P2D_param where id=1");
$serveur_doli = $donnees_recup_des_bases['serveur_doli'];
$admin_doli = $donnees_recup_des_bases['admin_doli'];
$mdp_doli = $donnees_recup_des_bases['mdp_doli'];
$base_doli = $donnees_recup_des_bases['base_doli'];
$prefix_doli = $donnees_recup_des_bases['prefix_doli'];
$libelle_port = $donnees_recup_des_bases['libelle_port'];
$chaine = $libelle_port;
$chaine = accents_sans("{$chaine}");
$libelle_port = $chaine;
$code_article_port = $donnees_recup_des_bases['code_article_port'];
$label = $donnees_recup_des_bases['prefix_ref_client'];
$chaine = $label;
$chaine = accents_sans("{$chaine}");
$label = $chaine;
$option_image = $donnees_recup_des_bases['option_image'];
$uri = $donnees_recup_des_bases['uri'];
$decremente = $donnees_recup_des_bases['decremente'];
$numero_de_commande = $donnees_recup_des_bases['numero_de_commande'];
$mail_achat = $donnees_recup_des_bases['mail_achat'];
$valide = $donnees_recup_des_bases['valide'];
$memo_id = $donnees_recup_des_bases['memo_id'];
$stock_doli = $donnees_recup_des_bases['stock_doli'];
// CALCUL DU NOMBRE DE COMMANDES A TRAITER **************************************
$nb_commandes = $donnees_recup_des_bases['nb_commandes'];
$req_max_id_commandes = "select max(id_order) from " . $prefix_presta . "orders";
$req_max_id_commandes = mysql_query($req_max_id_commandes);
$id_max_commandes = mysql_result($req_max_id_commandes, 0, "max(id_order)");
if ($nb_commandes != 0) {
function synchroProduct($id_product)
{
    echo "<br/>Synchronize product : {$id_product}<br>";
    $product_description = Configuration::get('product_description');
    $use_barcode = Configuration::get('use_barcode');
    if ($product = Db::getInstance()->GetRow("select * from " . _DB_PREFIX_ . "product where id_product = '" . $id_product . "'")) {
        // retrieve params
        $prefix_ref_product = Configuration::get('prefix_ref_product');
        $prefix_ref_product = accents_sans("{$prefix_ref_product}");
        //retrieve product data
        $prix_produit_normal_HT = $product['price'];
        $active = $product['active'];
        $reference = $product['reference'];
        $reference = produits_caract("{$reference}");
        $en_vente = $product['active'];
        $barcode = $product['ean13'];
        //$datec=$product['date_add'];
        //$tms=$product['date_upd'];
        //$weight=$product['weight'];
        // find tva rate
        $id_tax_rules_group = $product['id_tax_rules_group'];
        //var_dump($id_tax_rules_group);
        $donnees_id_tax_rules_group = Db::getInstance()->GetRow("select * from " . _DB_PREFIX_ . "tax_rule where id_tax_rules_group = '" . $id_tax_rules_group . "'");
        //var_dump($donnees_id_tax_rules_group);
        $id_tax = $donnees_id_tax_rules_group['id_tax'];
        //var_dump($id_tax);
        $donnees_tax = Db::getInstance()->GetRow("select * from " . _DB_PREFIX_ . "tax where id_tax = '" . $id_tax . "'");
        $vat_rate = $donnees_tax['rate'];
        $prix_produit_normal_HT = sprintf("%.5f", $prix_produit_normal_HT);
        //find description
        $product_data = Db::getInstance()->GetRow("select * from " . _DB_PREFIX_ . "product_lang where id_product = '" . $id_product . "' AND id_lang = '" . Context::getContext()->language->id . "'");
        if ($product_description == '0') {
            $description = $product_data['description_short'];
        } else {
            $description = $product_data['description'];
        }
        $label = $product_data['name'];
        // RECUPERATION DES DONNEES DU PRODUIT DANS LA BASE ARTICLES *********************************************
        // RECUPERATION ID IMAGE ****************************************************
        //$donnees_id_image = Db::getInstance()->GetRow("select * from ".$prefix_presta."image where id_product='".$product_id."'");
        //$id_image=$donnees_id_image['id_image'];
        // FIN RECUPERATION ID IMAGE ****************************************************
        $dolibarr = Dolibarr::getInstance();
        $product = new DolibarrProduct();
        $product->ref_ext = $prefix_ref_product . $id_product;
        $product->ref = $reference;
        $product->label = $label;
        $product->description = $description;
        $product->price_net = $prix_produit_normal_HT;
        $product->vat_rate = $vat_rate;
        if ($use_barcode == '1') {
            $product->barcode = $barcode;
            $product->barcode_type = '2';
            // 2 = ean13
        }
        // Check if already exists in Dolibarr
        $exists = $dolibarr->getProduct($prefix_ref_product . $id_product);
        if ($exists["result"]->result_code == 'NOT_FOUND') {
            // Create new product
            echo "Create new product : ";
            $result = $dolibarr->createProduct($product);
            echo $result["result"]->result_code . "<br/>";
            if ($result["result"]->result_code != 'OK') {
                echo "Erreur de synchronisation : " . $result["result"]->result_label;
                echo "<br>product : ";
                var_dump($product);
                echo "<br>result : ";
                var_dump($result);
                return FALSE;
            }
        } else {
            if ($exists["result"]->result_code == 'OK') {
                // Update product
                echo "update product : ";
                $oldProduct = $exists["product"];
                $product->id = $oldProduct->id;
                $result = $dolibarr->updateProduct($product);
                echo $result["result"]->result_code . "<br/>";
                if ($result["result"]->result_code != 'OK') {
                    if (strpos($result["result"]->result_label, 'CONSTRAINT `fk_product_barcode_type') !== FALSE) {
                        echo "Synchronisation Error : Looks like you have enabled barcode in this module but not in your Dolibarr installation.";
                    } else {
                        echo "Erreur de synchronisation : " . $result["result"]->result_label;
                        echo "<br>product : ";
                        var_dump($product);
                        echo "<br>result : ";
                        var_dump($result);
                        return FALSE;
                    }
                }
            } else {
                if (strpos($result["result"]->result_label, 'CONSTRAINT `fk_product_barcode_type') !== FALSE) {
                    echo "Synchronisation Error : Looks like you have enabled barcode in this module but not in your Dolibarr installation.";
                } else {
                    echo "Erreur de synchronisation : " . $result["result"]->result_label;
                    echo "<br>product : ";
                    var_dump($product);
                    echo "<br>result : ";
                    var_dump($result);
                    return FALSE;
                }
            }
        }
    }
    return TRUE;
}