roykin/erp/erp_update_customer.php

165 lines
6.6 KiB
PHP
Raw Normal View History

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', '!=', ''),
2016-07-04 11:57:59 +02:00
array('is_company', '=', true),
2016-03-21 14:45:17 +01:00
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-07-04 11:57:59 +02:00
$other_addresses = $models->execute_kw($db, $uid, $password,
'res.partner', 'read', array($record['child_ids']));
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']));
2016-07-04 11:57:59 +02:00
// echo '<pre>'; var_dump($record); echo '</pre>';
// die;
2016-04-26 16:46:19 +02:00
$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);
}
2016-11-24 11:38:53 +01:00
$group_to_add_to_customer[] = $group->id;
2016-04-26 16:46:19 +02:00
}
2016-11-24 11:38:53 +01:00
$group_to_add_to_customer[] = 3;
$group_to_add_to_customer = array_unique($group_to_add_to_customer);
2016-04-26 16:46:19 +02:00
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-11-24 11:38:53 +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;
}
}
// 12611 - get rid of unwanted characters in phone number (eg "01 02 03 04 05 (PIERRE)" found in ERP)
if (isset($record['phone']) && !empty($record['phone'])) {
$record['phone'] = preg_replace('/[^\+0-9\. \(\)\-]+/', '', $record['phone']);
$record['phone'] = str_replace('()', '', $record['phone']);
}
else {
$record['phone'] = '0123456789';
}
2016-03-21 14:45:17 +01:00
// check if user exist
if (ErpTools::alreadyExists($record['id'])) {
2016-11-24 11:38:53 +01:00
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-11-24 11:38:53 +01:00
$lastname = utf8_decode(substr((isset($record['display_name']) ? $record['display_name'] : $record['name'] ) , 0, 32));
2016-04-07 16:39:03 +02:00
$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'];
2016-10-21 11:37:19 +02:00
// Si on modifie l'EMAIL il faut l'informer
2016-03-21 14:45:17 +01:00
$customer->email = $email;
2016-10-21 11:37:19 +02:00
$customer->company = utf8_encode($record['name']);
2016-03-21 14:45:17 +01:00
$customer->representant = $record['user_id'][1];
2016-10-21 11:37:19 +02:00
2016-11-24 11:38:53 +01:00
$customer->update();
2016-04-28 10:30:23 +02:00
$customer->updateGroup($group_to_add_to_customer);
2016-11-24 11:38:53 +01:00
$address = $customer->getAdressErp();
2016-10-21 11:37:19 +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';
2016-10-21 11:37:19 +02:00
$address->lastname = utf8_encode($lastname);
$address->firstname = utf8_encode($firstname);
2016-03-21 15:07:02 +01:00
$address->address1 = utf8_encode($record['street']);
$address->address2 = utf8_encode($record['street2']);
$address->city = utf8_encode($record['city']);
$address->postcode = $record['zip'];
2016-10-21 11:37:19 +02:00
$address->company = utf8_encode($record['display_name']);
2016-03-21 15:07:02 +01:00
$address->phone = (int) str_replace(' ', '', $record['phone']);
$address->id_country = ErpTools::getCountryIdForPresta($record['country_id'][0]);
$address->is_erp = 1;
2016-07-04 11:57:59 +02:00
$address->id_erp = $record['id'];
2016-03-21 15:07:02 +01:00
$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
}
2016-07-04 11:57:59 +02:00
foreach ($other_addresses as $key => $other_address) {
//$other_address['phone'] = ($other_address['phone']) ? $other_address['phone'] : $record['phone'];
if (isset($other_address['phone']) && !empty($other_address['phone'])) {
$other_address['phone'] = preg_replace('/[^\+0-9\. \(\)\-]+/', '', $other_address['phone']);
$other_address['phone'] = str_replace('()', '', $other_address['phone']);
}
else {
$other_address['phone'] = $record['phone'];
}
2016-07-04 11:57:59 +02:00
$address = Address::getByErp($other_address['id']);
$address->id_customer = $customer->id;
2016-10-21 11:37:19 +02:00
$address->alias = utf8_encode($other_address['name']);
$address->lastname = utf8_encode($lastname);
$address->firstname = utf8_encode($firstname);
2016-07-04 11:57:59 +02:00
$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'];
2016-10-21 11:37:19 +02:00
$address->company = utf8_encode($other_address['display_name']);
2016-07-04 11:57:59 +02:00
$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();
}
2016-03-21 14:45:17 +01:00
}
}