roykin/erp/erp_add_customer.php

174 lines
5.8 KiB
PHP
Raw Normal View History

2016-03-18 10:53:04 +01:00
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Vendors
require_once 'ripcord/ripcord.php';
require '../config/config.inc.php';
2016-03-21 10:13:36 +01:00
require 'ErpTools.php';
2016-03-18 10:53:04 +01:00
// load config Odoo
require 'config.php';
// date de diff pour la création de client
2016-03-21 10:13:36 +01:00
$date_since_import = new DateTime();
$date_since_import->modify('-1 month');
2016-03-18 10:53:04 +01:00
// 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 = $models->execute_kw($db, $uid, $password,
'res.partner', 'search', array(
array(
array('email', '!=', ''),
2016-03-21 10:13:36 +01:00
array('customer', '=', true),
array('create_date', '>=', $date_since_import->format('Y-m-d H:i:s')),
2016-03-18 10:53:04 +01:00
)
)
);
2016-03-21 10:13:36 +01:00
/*
2016-03-18 10:53:04 +01:00
foreach ($ids_customer as $id_customer) {
2016-03-21 10:13:36 +01:00
// get info company
2016-03-18 10:53:04 +01:00
$record = $models->execute_kw($db, $uid, $password,
'res.partner', 'read', array($id_customer));
2016-03-21 10:13:36 +01:00
// check mail validity
$email = $record['email'];
if(!Validate::isEmail($email)) {
$emails = explode(';', $record['email']);
$email = trim($emails[0]);
if(!Validate::isEmail($email)) {
continue;
}
}
// check if user not exist and email not register
if (!ErpTools::alreadyExists($record['id'])
&& !Customer::getCustomersByEmail($email)) {
// 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));
}
try {
// add Customer
$customer = new Customer();
$customer->lastname = $lastname;
$customer->firstname = $firstname;
$customer->active = (int) $record['active'];;
$customer->email = $email;
$customer->passwd = Tools::encrypt('antadis78');
$customer->company = $record['name'];
$customer->id_erp = $record['id'];
$customer->representant = $record['user_id'][1];
// If customer add with success create Address
if ($customer->add()) {
$address = new Address();
$address->id_customer = $customer->id;
$address->alias = 'Mon adresse';
$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 = str_replace(' ', '', $record['phone']);
$address->id_country = ErpTools::getCountryIdForPresta($record['country_id'][0]);
$address->add();
}
} catch (Exception $exception) {
echo $exception->getMessage();
p($customer);
p($address);
}
}
}
*/
/**********************************************
*************** UPDATE CLIENT ****************
*********************************************/
// 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')),
//array('create_date', '<=', $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)) {
continue;
}
}
// check if user exist
if (ErpTools::alreadyExists($record['id'])) {
$customer = Customer::getCustomerIdByIdErp($record['id']);
p($customer);
die;
// 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-18 10:53:04 +01:00
// add Customer
2016-03-21 10:13:36 +01:00
/*$customer = new Customer();
$customer->lastname = $lastname;
$customer->firstname = $firstname;
2016-03-18 10:53:04 +01:00
$customer->active = (int) $record['active'];;
2016-03-21 10:13:36 +01:00
$customer->email = $email;
2016-03-18 10:53:04 +01:00
$customer->passwd = Tools::encrypt('antadis78');
$customer->company = $record['name'];
$customer->id_erp = $record['id'];
$customer->representant = $record['user_id'][1];
2016-03-21 10:13:36 +01:00
*
*/
}
2016-03-18 10:53:04 +01:00
}