diff --git a/admin/themes/default/template/controllers/addresses/helpers/form/form.tpl b/admin/themes/default/template/controllers/addresses/helpers/form/form.tpl index e46200e15..cb9d740f7 100644 --- a/admin/themes/default/template/controllers/addresses/helpers/form/form.tpl +++ b/admin/themes/default/template/controllers/addresses/helpers/form/form.tpl @@ -47,50 +47,47 @@ - {else} - - -
- - -
- {/if} + function resetCustomerRelatedAddressFields() { + $('input[name=firstname]').val(''); + $('input[name=lastname]').val(''); + $('input[name=company]').val(''); + $('input[name=id_customer]').val(''); + } + {else} {$smarty.block.parent} {/if} diff --git a/classes/Customer.php b/classes/Customer.php index a8c4c3609..4a55fa55d 100644 --- a/classes/Customer.php +++ b/classes/Customer.php @@ -951,4 +951,19 @@ class CustomerCore extends ObjectModel $sql_filter .= Shop::addSqlRestriction(Shop::SHARE_CUSTOMER, 'main'); return parent::getWebserviceObjectList($sql_join, $sql_filter, $sql_sort, $sql_limit); } + + public static function getAllCustomers($withAddress = null, $active = null, $deleted = null) + { + return Db::getInstance()->executeS( + 'SELECT c.`firstname`, c.`lastname`, c.`id_customer`, c.`email`, a.`id_address` + FROM `'._DB_PREFIX_.'customer` c + LEFT JOIN `'._DB_PREFIX_.'address` a + ON a.`id_customer` = c.`id_customer` AND a.`deleted`=0 + WHERE 1 '. + (!is_null($deleted) ? ' AND c.`deleted` = '.(int) $deleted : ' '). + (!is_null($active) ? ' AND c.`active` = '.(int) $active: ' ' ). + (!is_null($withAddress) ? (($withAddress) ? ' AND a.`id_address` !='.(int) 0: ' AND ISNULL(a.`id_address`)'): ' ' ) + ); + } + } diff --git a/controllers/admin/AdminAddressesController.php b/controllers/admin/AdminAddressesController.php index 6e052f39b..a963d6709 100644 --- a/controllers/admin/AdminAddressesController.php +++ b/controllers/admin/AdminAddressesController.php @@ -112,18 +112,41 @@ class AdminAddressesControllerCore extends AdminController public function renderForm() { + $customerSelectField =array( + 'type' => 'text_customer', + 'label' => $this->l('Customer'), + 'name' => 'id_customer', + 'required' => false, + ); + if ($this->loadObject(true) + && !$this->object->id + ) { + $customerEmails = array(array( + 'email' => $this->l('Select Customer'), + 'id_customer' => 0, + 'id_address'=> 0 + )); + $customerEmails = array_merge($customerEmails, Customer::getAllCustomers(false, null, 0)); + $customerSelectField = array( + 'type' => 'select', + 'label' => $this->l('Customer'), + 'name' => 'id_customer', + 'class' => 'chosen', + 'options' => array( + 'query' => $customerEmails, + 'id' => 'id_customer', + 'name' => 'email' + ), + ); + } + $this->fields_form = array( 'legend' => array( 'title' => $this->l('Addresses'), 'icon' => 'icon-envelope-alt' ), 'input' => array( - array( - 'type' => 'text_customer', - 'label' => $this->l('Customer'), - 'name' => 'id_customer', - 'required' => false, - ), + $customerSelectField, array( 'type' => 'text', 'label' => $this->l('Identification Number'), @@ -190,7 +213,6 @@ class AdminAddressesControllerCore extends AdminController $token_customer = Tools::getAdminToken('AdminCustomers'.(int)(Tab::getIdFromClassName('AdminCustomers')).(int)$this->context->employee->id); } - $this->fields_value['email'] = Tools::getValue('email'); $this->tpl_form_vars = array( 'customer' => (isset($customer) && $this->object->id) ? $customer : null, 'tokenCustomer' => isset($token_customer) ? $token_customer : null @@ -503,21 +525,23 @@ class AdminAddressesControllerCore extends AdminController return $out; } - /** - * Method called when an ajax request is made + /** + * Method called when an ajax request is made to load the customer info for the selected email * @see AdminController::postProcess() */ - public function ajaxProcess() + public function ajaxProcessLoadCustomer() { - if (Tools::isSubmit('email')) { - $email = pSQL(Tools::getValue('email')); - $customer = Customer::searchByName($email); - if (!empty($customer)) { - $customer = $customer['0']; - echo json_encode(array('infos' => pSQL($customer['firstname']).'_'.pSQL($customer['lastname']).'_'.pSQL($customer['company']).'_'.pSQL($customer['id_customer']))); - } + $response = array('status' => false); + if (Tools::getValue('id_customer') + && Validate::isLoadedObject($objCustomer = new Customer(Tools::getValue('id_customer'))) + ) { + $response['status'] = true; + $response['firstname'] = $objCustomer->firstname; + $response['lastname'] = $objCustomer->lastname; + $response['company'] = $objCustomer->company; } - die; + + $this->ajaxDie(json_encode($response)); } /**