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 ec102910d..9fd0f69e6 100644
--- a/admin/themes/default/template/controllers/addresses/helpers/form/form.tpl
+++ b/admin/themes/default/template/controllers/addresses/helpers/form/form.tpl
@@ -29,31 +29,29 @@
{if $input.name == 'vat_number'}
{/if}
-
- {if $input.type == 'text_customer' && !isset($customer)}
-
- {else}
- {$smarty.block.parent}
- {/if}
+ {$smarty.block.parent}
{/block}
-{block name="field"}
+{block name="input"}
{if $input.type == 'text_customer'}
{if isset($customer)}
-
+
+ {$customer->lastname} {$customer->firstname} ({$customer->email})
+
- {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..a1ef3279e 100644
--- a/classes/Customer.php
+++ b/classes/Customer.php
@@ -294,21 +294,37 @@ class CustomerCore extends ObjectModel
}
/**
- * Return customers list
+ * Returns a list of customers.
*
- * @param null|bool $only_active Returns only active customers when true
- * @return array Customers
+ * @param null|bool $active Optional. Filter customers by their active status. If null, no filter is applied.
+ * @param null|bool $deleted Optional. Filter customers by their deleted status. If null, no filter is applied.
+ * @param null|bool $havingAddress Optional. Filter customers having|not having address. If true, only customers having an address are returned.
+ * If false, only customers without an address are returned. If null, no filter is applied.
+ * @return array List of customers matching the specified criteria.
*/
- public static function getCustomers($only_active = null)
+ public static function getCustomers($active = null, $deleted = null, $havingAddress = null)
{
- $sql = 'SELECT `id_customer`, `email`, `firstname`, `lastname`
- FROM `'._DB_PREFIX_.'customer`
- WHERE 1 '.Shop::addSqlRestriction(Shop::SHARE_CUSTOMER).
- ($only_active ? ' AND `active` = 1' : '').'
- ORDER BY `id_customer` ASC';
+ $sqlSelect = 'SELECT c.`id_customer`, c.`email`, c.`firstname`, c.`lastname`';
+ $sqlFrom = 'FROM `'._DB_PREFIX_.'customer` c';
+ $sqlJoin = '';
+ $sqlWhere = 'WHERE 1 '.Shop::addSqlRestriction(Shop::SHARE_CUSTOMER).
+ (!is_null($active) ? ' AND c.`active` = '.(int) $active: ' ' ).
+ (!is_null($deleted) ? ' AND c.`deleted` = '.(int) $deleted : ' ');
+ $sqlOrderBy = 'ORDER BY c.`id_customer` ASC';
+ $sqlGroupBy = 'GROUP BY c.`id_customer`';
+
+ if (!is_null($havingAddress)) {
+ $sqlJoin .= ' LEFT JOIN `'._DB_PREFIX_.'address` a
+ ON a.`id_customer` = c.`id_customer` AND a.`deleted`=0';
+ $sqlWhere .= (($havingAddress) ? ' AND a.`id_address` IS NOT NULL': ' AND a.`id_address` IS NULL');
+ }
+
+ $sql = $sqlSelect .' '. $sqlFrom.' '.$sqlJoin.' '.$sqlWhere.' '.$sqlGroupBy.' '.$sqlOrderBy;
+
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
}
+
/**
* Return customer instance from its e-mail (optionnaly check password)
*
@@ -951,4 +967,5 @@ class CustomerCore extends ObjectModel
$sql_filter .= Shop::addSqlRestriction(Shop::SHARE_CUSTOMER, 'main');
return parent::getWebserviceObjectList($sql_join, $sql_filter, $sql_sort, $sql_limit);
}
+
}
diff --git a/controllers/admin/AdminAddressesController.php b/controllers/admin/AdminAddressesController.php
index 83a646cb6..5f6bb472d 100644
--- a/controllers/admin/AdminAddressesController.php
+++ b/controllers/admin/AdminAddressesController.php
@@ -112,18 +112,43 @@ class AdminAddressesControllerCore extends AdminController
public function renderForm()
{
+ $customerField =array(
+ 'type' => 'text_customer',
+ 'label' => $this->l('Customer'),
+ 'name' => 'id_customer',
+ 'required' => false,
+ );
+ if ($this->loadObject(true)
+ && !$this->object->id
+ && !Tools::getValue('liteDisplaying')
+ ) {
+ $customerEmails = array(array(
+ 'email' => $this->l('Select Customer'),
+ 'id_customer' => 0,
+ 'id_address'=> 0
+ ));
+ $customerEmails = array_merge($customerEmails, Customer::getCustomers(null, 0, 0));
+ $customerField = array(
+ 'type' => 'select',
+ 'label' => $this->l('Customer'),
+ 'name' => 'id_customer',
+ 'required' => true,
+ '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,
- ),
+ $customerField,
array(
'type' => 'text',
'label' => $this->l('Identification Number'),
@@ -191,9 +216,8 @@ class AdminAddressesControllerCore extends AdminController
}
$this->tpl_form_vars = array(
- 'customer' => isset($customer) ? $customer : null,
- 'tokenCustomer' => isset($token_customer) ? $token_customer : null,
- 'back_url' => urldecode(Tools::getValue('back'))
+ 'customer' => (isset($customer) && ($this->object->id || Tools::getValue('liteDisplaying'))) ? $customer : null,
+ 'tokenCustomer' => isset($token_customer) ? $token_customer : null
);
// Order address fields depending on country format
@@ -503,7 +527,7 @@ class AdminAddressesControllerCore extends AdminController
return $out;
}
- /**
+ /**
* Method called when an ajax request is made
* @see AdminController::postProcess()
*/