150 lines
5.9 KiB
PHP
150 lines
5.9 KiB
PHP
<?php
|
|
error_reporting(E_ALL);
|
|
ini_set('display_errors', 1);
|
|
|
|
// Vendors
|
|
require_once 'ripcord/ripcord.php';
|
|
require '../config/config.inc.php';
|
|
require 'ErpTools.php';
|
|
require 'config.php';
|
|
|
|
if (empty($_GET['token']) || $_GET['token'] !== ERP_SCRIPT_TOKEN) {
|
|
die;
|
|
}
|
|
|
|
// date de diff pour la création de client
|
|
$date_since_import = new DateTime();
|
|
$date_since_import->modify('-1 day');
|
|
|
|
// Get All Customers with email and is customer to create if no exist or update info
|
|
$models = ripcord::client("$url/xmlrpc/2/object");
|
|
$ids_customer_update = $models->execute_kw($db, $uid, $password,
|
|
'res.partner', 'search', array(
|
|
array(
|
|
array('email', '!=', ''),
|
|
array('is_company', '=', true),
|
|
array('customer', '=', true),
|
|
array('__last_update', '>=', $date_since_import->format('Y-m-d H:i:s')),
|
|
)
|
|
)
|
|
);
|
|
|
|
foreach ($ids_customer_update as $id_customer) {
|
|
// get info company
|
|
$record = $models->execute_kw($db, $uid, $password,
|
|
'res.partner', 'read', array($id_customer));
|
|
|
|
$other_addresses = $models->execute_kw($db, $uid, $password,
|
|
'res.partner', 'read', array($record['child_ids']));
|
|
|
|
$tags = $models->execute_kw($db, $uid, $password,
|
|
'res.partner.category', 'read', array($record['category_id']));
|
|
|
|
// echo '<pre>'; var_dump($record); echo '</pre>';
|
|
// die;
|
|
|
|
$group_to_add_to_customer = array();
|
|
foreach ($tags as $key => $tag) {
|
|
$group = '';
|
|
if (!Group::getByName($tag['name'], 1)) {
|
|
$group = new Group();
|
|
$group->name = array(1 => $tag['name']);
|
|
$group->reduction = 0;
|
|
$group->price_display_method = PS_TAX_EXC;
|
|
$group->save();
|
|
} else {
|
|
$group = Group::getByName($tag['name'], 1);
|
|
}
|
|
$group_to_add_to_customer[] = $group->id;
|
|
}
|
|
$group_to_add_to_customer[] = 3;
|
|
$group_to_add_to_customer = array_unique($group_to_add_to_customer);
|
|
|
|
// check mail validity
|
|
$email = (!empty($record['email'])) ? $record['email'] : '';
|
|
if (!Validate::isEmail($email)) {
|
|
$emails = explode(';', $email);
|
|
$email = trim($emails[0]);
|
|
if(!Validate::isEmail($email)) {
|
|
ErpTools::logError($record, 'update_customer_email');
|
|
continue;
|
|
}
|
|
}
|
|
$record['phone'] = ($record['phone']) ? $record['phone'] : '0123456789';
|
|
|
|
// check if user exist
|
|
if (ErpTools::alreadyExists($record['id'])) {
|
|
if (!ErpTools::validateRecordCustomer($record)) {
|
|
ErpTools::logError($record, 'update_customer_validate');
|
|
continue;
|
|
}
|
|
|
|
$customer = Customer::getCustomerIdByIdErp($record['id']);
|
|
if (!Validate::isLoadedObject($customer)) {
|
|
continue;
|
|
}
|
|
|
|
$lastname = utf8_decode(substr((isset($record['display_name']) ? $record['display_name'] : $record['name'] ) , 0, 32));
|
|
$firstname = utf8_decode(substr((isset($record['display_name']) ? $record['display_name'] : $record['name'] ) , 0, 32));
|
|
|
|
$customer->lastname = $lastname;
|
|
$customer->firstname = $firstname;
|
|
$customer->active = (int) $record['active'];
|
|
|
|
// Si on modifie l'EMAIL il faut l'informer
|
|
$customer->email = $email;
|
|
$customer->company = utf8_encode($record['name']);
|
|
$customer->representant = $record['user_id'][1];
|
|
|
|
$customer->update();
|
|
$customer->updateGroup($group_to_add_to_customer);
|
|
$address = $customer->getAdressErp();
|
|
|
|
// si aucune adresse pour le moment je la créé
|
|
if (!$address) {
|
|
$address = new Address();
|
|
$address->id_customer = $customer->id;
|
|
$address->alias = 'Adresse Livraison';
|
|
$address->lastname = utf8_encode($lastname);
|
|
$address->firstname = utf8_encode($firstname);
|
|
$address->address1 = utf8_encode($record['street']);
|
|
$address->address2 = utf8_encode($record['street2']);
|
|
$address->city = utf8_encode($record['city']);
|
|
$address->postcode = $record['zip'];
|
|
$address->company = utf8_encode($record['display_name']);
|
|
$address->phone = (int) str_replace(' ', '', $record['phone']);
|
|
$address->id_country = ErpTools::getCountryIdForPresta($record['country_id'][0]);
|
|
$address->is_erp = 1;
|
|
$address->id_erp = $record['id'];
|
|
$address->add();
|
|
continue;
|
|
} else {
|
|
$address->address1 = utf8_encode($record['street']);
|
|
$address->address2 = utf8_encode($record['street2']);
|
|
$address->city = utf8_encode($record['city']);
|
|
$address->postcode = $record['zip'];
|
|
$address->id_country = ErpTools::getCountryIdForPresta($record['country_id'][0]);
|
|
$address->update();
|
|
}
|
|
|
|
foreach ($other_addresses as $key => $other_address) {
|
|
$other_address['phone'] = ($other_address['phone']) ? $other_address['phone'] : $record['phone'];
|
|
$address = Address::getByErp($other_address['id']);
|
|
$address->id_customer = $customer->id;
|
|
$address->alias = utf8_encode($other_address['name']);
|
|
$address->lastname = utf8_encode($lastname);
|
|
$address->firstname = utf8_encode($firstname);
|
|
$address->address1 = utf8_encode($other_address['street']);
|
|
$address->address2 = utf8_encode($other_address['street2']);
|
|
$address->city = utf8_encode($other_address['city']);
|
|
$address->postcode = $other_address['zip'];
|
|
$address->company = utf8_encode($other_address['display_name']);
|
|
$address->phone = (int) str_replace(' ', '', $other_address['phone']);
|
|
$address->id_country = ErpTools::getCountryIdForPresta($other_address['country_id'][0]);
|
|
$address->is_erp = 1;
|
|
$address->id_erp = $other_address['id'];
|
|
$address->save();
|
|
}
|
|
}
|
|
}
|