roykin/erp/erp_add_customer.php

177 lines
7.3 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
require 'config.php';
2016-04-22 11:48:07 +02:00
if (empty($_GET['token']) || $_GET['token'] !== ERP_SCRIPT_TOKEN) {
die;
}
2016-03-18 10:53:04 +01:00
// date de diff pour la création de client
2016-03-21 10:13:36 +01:00
$date_since_import = new DateTime();
2016-10-21 11:37:19 +02:00
$date_since_import->modify('-2 day');
2016-03-18 10:53:04 +01:00
2016-03-21 14:45:17 +01:00
// Get All Customers with email and is company
2016-03-18 10:53:04 +01:00
$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 14:45:17 +01:00
array('is_company', '=', true),
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-10-21 11:37:19 +02:00
// $ids_customer = array(7185);
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-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']));
$group_to_add_to_customer = array();
foreach ($tags as $key => $tag) {
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;
}
2016-03-21 10:13:36 +01:00
// check mail validity
2016-03-21 15:05:13 +01:00
if(empty($record['email'])) {
continue;
2016-04-07 16:39:03 +02:00
}
// multiple email / explode
2016-03-21 10:13:36 +01:00
$email = $record['email'];
if(!Validate::isEmail($email)) {
$emails = explode(';', $record['email']);
$email = trim($emails[0]);
if(!Validate::isEmail($email)) {
2016-04-07 16:39:03 +02:00
ErpTools::logError($record, 'add_customer_email');
2016-03-21 10:13:36 +01:00
continue;
}
}
2016-10-21 11:37:19 +02:00
// ppp($id_customer.' loop');
// var_dump(ErpTools::alreadyExists($record['id']));
// var_dump(Customer::getCustomersByEmail($email));
2016-03-21 10:13:36 +01:00
// check if user not exist and email not register
if (!ErpTools::alreadyExists($record['id'])
&& !Customer::getCustomersByEmail($email)) {
2016-04-28 10:30:23 +02:00
2016-10-21 11:37:19 +02:00
$lastname = substr((isset($record['display_name']) ? $record['display_name'] : $record['name'] ) , 0, 32);
$firstname = substr((isset($record['display_name']) ? $record['display_name'] : $record['name'] ) , 0, 32);
$record['phone'] = ($record['phone']) ? $record['phone'] : '0123456789';
2016-03-21 15:05:13 +01:00
2016-03-21 10:13:36 +01:00
// if contact check name else lastname firstname is company info
2016-04-07 16:39:03 +02:00
// 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-10-21 11:37:19 +02:00
//
2016-04-28 10:30:23 +02:00
2016-03-21 14:45:17 +01:00
if (!ErpTools::validateRecordCustomer($record)) {
2016-10-21 11:37:19 +02:00
ErpTools::logError($record, 'add_customer_validate');
p($record);
2016-03-21 14:45:17 +01:00
continue;
}
2016-10-21 11:37:19 +02:00
$password_generate = Tools::passwdGen(6);
2016-04-07 16:39:03 +02:00
2016-03-21 10:13:36 +01:00
try {
// add Customer
$customer = new Customer();
2016-10-21 11:37:19 +02:00
$customer->lastname = utf8_encode($lastname);
$customer->firstname = utf8_encode($firstname);
2016-03-21 14:45:17 +01:00
$customer->active = (int) $record['active'];
2016-03-21 10:13:36 +01:00
$customer->email = $email;
2016-10-21 11:37:19 +02:00
$customer->passwd = Tools::encrypt($password_generate);
$customer->company = utf8_encode( (isset($record['display_name']) ? $record['display_name'] : $record['name'] ) );
2016-03-21 10:13:36 +01:00
$customer->id_erp = $record['id'];
2016-04-07 16:39:03 +02:00
$customer->representant = (isset($record['user_id'][1]) ? $record['user_id'][1] : '' );
2016-03-21 10:13:36 +01:00
// If customer add with success create Address
if ($customer->add()) {
2016-04-26 16:46:19 +02:00
$customer->addGroups($group_to_add_to_customer);
2016-03-21 10:13:36 +01:00
$address = new Address();
$address->id_customer = $customer->id;
2016-03-21 14:45:17 +01:00
$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 10:13:36 +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 14:45:17 +01:00
$address->phone = (int) str_replace(' ', '', $record['phone']);
2016-03-21 10:13:36 +01:00
$address->id_country = ErpTools::getCountryIdForPresta($record['country_id'][0]);
2016-03-21 14:45:17 +01:00
$address->is_erp = 1;
2016-07-04 11:57:59 +02:00
$address->id_erp = $record['id'];
2016-03-21 10:13:36 +01:00
$address->add();
2016-10-21 11:37:19 +02:00
$mail = Mail::Send(
1,
'account',
Mail::l('Welcome!'),
array(
'{firstname}' => $customer->firstname,
'{lastname}' => $customer->lastname,
'{email}' => $customer->email,
'{passwd}' => $password_generate
),
array($customer->email, 'gerald@roykin.fr'),
$customer->firstname.' '.$customer->lastname
);
var_dump("mail sent : ".$mail.' '.$customer->email);
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'];
$address = new Address();
$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->add();
}
2016-10-21 11:37:19 +02:00
2016-03-21 10:13:36 +01:00
}
} catch (Exception $exception) {
2016-10-21 11:37:19 +02:00
$msg = $exception->getMessage();
2016-04-07 16:39:03 +02:00
ErpTools::logError($record, 'add_customer');
2016-03-21 10:13:36 +01:00
}
}
2016-03-18 10:53:04 +01:00
}