2016-03-21 14:45:17 +01:00
|
|
|
<?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';
|
|
|
|
|
2016-04-22 11:48:07 +02:00
|
|
|
if (empty($_GET['token']) || $_GET['token'] !== ERP_SCRIPT_TOKEN) {
|
|
|
|
die;
|
|
|
|
}
|
2016-03-21 14:45:17 +01:00
|
|
|
|
|
|
|
// 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('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));
|
|
|
|
|
2016-04-28 10:30:23 +02:00
|
|
|
$tags = $models->execute_kw($db, $uid, $password,
|
2016-04-26 16:46:19 +02:00
|
|
|
'res.partner.category', 'read', array($record['category_id']));
|
|
|
|
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2016-03-21 14:45:17 +01:00
|
|
|
// check mail validity
|
2016-04-26 16:46:19 +02:00
|
|
|
$email = (!empty($record['email'])) ? $record['email'] : '';
|
2016-03-21 14:45:17 +01:00
|
|
|
if(!Validate::isEmail($email)) {
|
2016-04-26 16:46:19 +02:00
|
|
|
$emails = explode(';', $email);
|
2016-03-21 14:45:17 +01:00
|
|
|
$email = trim($emails[0]);
|
|
|
|
if(!Validate::isEmail($email)) {
|
2016-04-07 16:39:03 +02:00
|
|
|
ErpTools::logError($record, 'update_customer_email');
|
2016-03-21 14:45:17 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if user exist
|
|
|
|
if (ErpTools::alreadyExists($record['id'])) {
|
|
|
|
if (!ErpTools::validateRecordCustomer($record)) {
|
2016-04-07 16:39:03 +02:00
|
|
|
ErpTools::logError($record, 'update_customer_validate');
|
2016-03-21 14:45:17 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$customer = Customer::getCustomerIdByIdErp($record['id']);
|
|
|
|
if (!Validate::isLoadedObject($customer)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-04-07 16:39:03 +02:00
|
|
|
$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));
|
|
|
|
|
|
|
|
// // if contact check name else lastname firstname is company info
|
|
|
|
// if (!empty($record['child_ids'])) {
|
|
|
|
// $contact = $models->execute_kw($db, $uid, $password,
|
|
|
|
// 'res.partner', 'read', array($record['child_ids'][0]));
|
|
|
|
// $lastname = utf8_encode($contact['name']);
|
|
|
|
// $firstname = utf8_encode($contact['name']);
|
|
|
|
// } else {
|
|
|
|
// $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));
|
|
|
|
// }
|
2016-03-21 14:45:17 +01:00
|
|
|
|
|
|
|
$customer->lastname = $lastname;
|
|
|
|
$customer->firstname = $firstname;
|
|
|
|
$customer->active = (int) $record['active'];
|
|
|
|
$customer->email = $email;
|
|
|
|
$customer->company = $record['name'];
|
|
|
|
$customer->representant = $record['user_id'][1];
|
2016-04-28 10:30:23 +02:00
|
|
|
$customer->updateGroup($group_to_add_to_customer);
|
2016-03-21 14:45:17 +01:00
|
|
|
|
|
|
|
$address = $customer->getAdressErp();
|
2016-04-07 16:39:03 +02:00
|
|
|
|
2016-03-21 15:07:02 +01:00
|
|
|
// si aucune adresse pour le moment je la créé
|
2016-03-21 14:45:17 +01:00
|
|
|
if (!$address) {
|
2016-03-21 15:07:02 +01:00
|
|
|
$address = new Address();
|
|
|
|
$address->id_customer = $customer->id;
|
|
|
|
$address->alias = 'Adresse Livraison';
|
|
|
|
$address->lastname = $lastname;
|
|
|
|
$address->firstname = $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 = $record['display_name'];
|
|
|
|
$address->phone = (int) str_replace(' ', '', $record['phone']);
|
|
|
|
$address->id_country = ErpTools::getCountryIdForPresta($record['country_id'][0]);
|
|
|
|
$address->is_erp = 1;
|
|
|
|
$address->add();
|
2016-03-21 14:45:17 +01:00
|
|
|
continue;
|
2016-04-07 16:39:03 +02:00
|
|
|
} 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();
|
2016-03-21 14:45:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|