From 2c84f098217d98ff7c778450607d35c8c7869afb Mon Sep 17 00:00:00 2001 From: "vishalsingh.qlo325" Date: Wed, 10 Jul 2024 15:29:40 +0530 Subject: [PATCH] Optimized function getCustomers() for easy understanding --- .../addresses/helpers/form/form.tpl | 17 ++++------- classes/Customer.php | 30 +++++++++++-------- .../admin/AdminAddressesController.php | 3 +- 3 files changed, 24 insertions(+), 26 deletions(-) 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 fbcda13d1..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,22 +29,15 @@ {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}) + {/if} diff --git a/classes/Customer.php b/classes/Customer.php index 645c80ae6..a1ef3279e 100644 --- a/classes/Customer.php +++ b/classes/Customer.php @@ -294,33 +294,37 @@ class CustomerCore extends ObjectModel } /** - * Return customers list + * Returns a list of customers. * - * @param null|bool $only_active Returns customers by active status - * @param null|bool $havingAddress Returns customers having | not having address - * @param null|bool $deleted Returns customers by deleted status - * @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, $havingAddress = null, $deleted = null) + public static function getCustomers($active = null, $deleted = null, $havingAddress = null) { - $sqlSelect = 'SELECT c.`id_customer`, c.`email`, c.`firstname`, c.`lastname` '; - $sqlFrom = ' FROM `'._DB_PREFIX_.'customer` c'; + $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($only_active) ? ' AND c.`active` = '.(int) $only_active: ' ' ). + (!is_null($active) ? ' AND c.`active` = '.(int) $active: ' ' ). (!is_null($deleted) ? ' AND c.`deleted` = '.(int) $deleted : ' '); - $sqlOrderBY = 'ORDER BY c.`id_customer` ASC'; + $sqlOrderBy = 'ORDER BY c.`id_customer` ASC'; + $sqlGroupBy = 'GROUP BY c.`id_customer`'; if (!is_null($havingAddress)) { - $sqlFrom .= ' LEFT JOIN `'._DB_PREFIX_.'address` a + $sqlJoin .= ' LEFT JOIN `'._DB_PREFIX_.'address` a ON a.`id_customer` = c.`id_customer` AND a.`deleted`=0'; - $sqlWhere .= (($havingAddress) ? ' AND a.`id_address` !='.(int) 0: ' AND ISNULL(a.`id_address`)'); + $sqlWhere .= (($havingAddress) ? ' AND a.`id_address` IS NOT NULL': ' AND a.`id_address` IS NULL'); } - $sql = $sqlSelect .' '. $sqlFrom.' '. $sqlWhere.' '.$sqlOrderBY; + $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) * diff --git a/controllers/admin/AdminAddressesController.php b/controllers/admin/AdminAddressesController.php index daf71152d..5f6bb472d 100644 --- a/controllers/admin/AdminAddressesController.php +++ b/controllers/admin/AdminAddressesController.php @@ -127,11 +127,12 @@ class AdminAddressesControllerCore extends AdminController 'id_customer' => 0, 'id_address'=> 0 )); - $customerEmails = array_merge($customerEmails, Customer::getCustomers(null, false, 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,