roykin/erp/erp_update_customer.php
2016-03-21 14:45:17 +01:00

88 lines
2.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';
// 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));
// check mail validity
$email = $record['email'];
if(!Validate::isEmail($email)) {
$emails = explode(';', $record['email']);
$email = trim($emails[0]);
if(!Validate::isEmail($email)) {
ErpTools::logError($record);
continue;
}
}
// check if user exist
if (ErpTools::alreadyExists($record['id'])) {
if (!ErpTools::validateRecordCustomer($record)) {
ErpTools::logError($record);
continue;
}
$customer = Customer::getCustomerIdByIdErp($record['id']);
if (!Validate::isLoadedObject($customer)) {
continue;
}
// 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));
}
$customer->lastname = $lastname;
$customer->firstname = $firstname;
$customer->active = (int) $record['active'];
$customer->email = $email;
$customer->company = $record['name'];
$customer->representant = $record['user_id'][1];
$address = $customer->getAdressErp();
if (!$address) {
continue;
}
$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();
}
}