$name_parts = $this->openbay->splitName((string)$order->address->name);
$user = array();
$user['fname'] = $name_parts['firstname'];
$user['lname'] = $name_parts['surname'];
/** get the iso2 code from the data and pull out the correct country for the details. */
if(!empty($order->address->iso2)){
$country_qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "country` WHERE `iso_code_2` = '".$this->db->escape($order->address->iso2)."'");
}
if(!empty($country_qry->num_rows)){
$user['country'] = $country_qry->row['name'];
$user['country_id'] = $country_qry->row['country_id'];
}else{
$user['country'] = (string)$order->address->iso2;
$user['country_id'] = '';
}
$user['email'] = (string)$order->user->email;
$user['id'] = $this->openbay->getUserByEmail($user['email']);
if($user['id'] != false){
$this->db->query("UPDATE `" . DB_PREFIX . "customer` SET
`firstname` = '" . $this->db->escape($name_parts['firstname']) . "',
`lastname` = '" . $this->db->escape($name_parts['surname']) . "',
`telephone` = '" . str_replace(array(' ', '+', '-'), '', $this->db->escape($order->address->phone))."',
`status` = '1'
WHERE `customer_id` = '" . (int)$user['id'] . "'");
}else{
$this->db->query("INSERT INTO `" . DB_PREFIX . "customer` SET
`store_id` = '" . (int)$this->config->get('config_store_id') . "',
`firstname` = '" . $this->db->escape($name_parts['firstname']) . "',
`lastname` = '" . $this->db->escape($name_parts['surname']) . "',
`email` = '" . $this->db->escape($user['email']) . "',
`telephone` = '" . str_replace(array(' ', '+', '-'), '', $this->db->escape($order->address->phone))."',
`password` = '" . $this->db->escape(md5($order->user->userid)) . "',
`newsletter` = '0',
`customer_group_id` = '" . (int)$this->config->get('openbay_def_customer_grp') . "',
`approved` = '1',
`status` = '1',
`date_added` = NOW()");
$user['id'] = $this->db->getLastId();
}
return $user;