Enhance customer search for discount

This commit is contained in:
Michael RICOIS 2018-02-26 12:20:35 +01:00
parent b9016d4131
commit 8288c15333

View File

@ -125,22 +125,43 @@ if (isset($_GET['ajaxDiscountCustomers']))
$currentIndex = 'index.php?tab=AdminDiscounts';
$jsonArray = array();
$filter = Tools::getValue('filter');
if (Validate::isBool_Id($filter))
$filterWithIdCustomer = false;
if (strpos($filter, '_')) {
$filterArray = explode('_', $filter);
$customers = Db::getInstance()->ExecuteS('
SELECT `id_customer`, `email`, CONCAT(`lastname`, \' \', `firstname`) as name
FROM `'._DB_PREFIX_.'customer`
WHERE `deleted` = 0 AND is_guest = 0
AND '.(Validate::isUnsignedInt($filter) ? '`id_customer` = '.(int)($filter) : '(`email` LIKE "%'.pSQL($filter).'%"
'.((Validate::isBool_Id($filter) AND $filterArray[0] == 0) ? 'OR `id_customer` = '.(int)($filterArray[1]) : '').'
'.(Validate::isUnsignedInt($filter) ? '`id_customer` = '.(int)($filter) : '').'
OR CONCAT(`firstname`, \' \', `lastname`) LIKE "%'.pSQL($filter).'%"
OR CONCAT(`lastname`, \' \', `firstname`) LIKE "%'.pSQL($filter).'%")').'
ORDER BY CONCAT(`lastname`, \' \', `firstname`) ASC
LIMIT 50');
$filter = $filterArray[1];
$filterWithIdCustomer = true;
}
// Filter
if ($filterWithIdCustomer === true) {
$customerSql = 'SELECT `id_customer`, `email`, CONCAT(`lastname`, \' \', `firstname`) as name
FROM `'._DB_PREFIX_.'customer`
WHERE `deleted` = 0 AND is_guest = 0 AND `id_customer` = '.(int)($filter).'
ORDER BY CONCAT(`lastname`, \' \', `firstname`) ASC
LIMIT 50';
}
// Detect email address
elseif (strpos($filter, '@')) {
$customerSql = 'SELECT `id_customer`, `email`, CONCAT(`lastname`, \' \', `firstname`) as name
FROM `'._DB_PREFIX_.'customer`
WHERE `deleted` = 0 AND is_guest = 0 AND `email` LIKE "%'.pSQL($filter).'%"
ORDER BY CONCAT(`lastname`, \' \', `firstname`) ASC
LIMIT 50';
}
// Default
else {
$customerSql = 'SELECT `id_customer`, `email`, CONCAT(`lastname`, \' \', `firstname`) as name
FROM `'._DB_PREFIX_.'customer`
WHERE `deleted` = 0 AND is_guest = 0 AND (
CONCAT(`firstname`, \' \', `lastname`) LIKE "%'.pSQL($filter).'%"
OR CONCAT(`lastname`, \' \', `firstname`) LIKE "%'.pSQL($filter).'%"
)
ORDER BY CONCAT(`lastname`, \' \', `firstname`) ASC
LIMIT 50';
}
$customers = Db::getInstance()->ExecuteS($customerSql);
$groups = Db::getInstance()->ExecuteS('
SELECT g.`id_group`, gl.`name`
FROM `'._DB_PREFIX_.'group` g
@ -150,15 +171,19 @@ if (isset($_GET['ajaxDiscountCustomers']))
ORDER BY gl.`name` ASC
LIMIT 50');
// JSON
$json = '{"customers" : ';
foreach ($customers AS $customer)
foreach ($customers AS $customer) {
$jsonArray[] = '{"value":"0_'.(int)($customer['id_customer']).'", "text":"'.addslashes($customer['name']).' ('.addslashes($customer['email']).')"}';
}
$json .= '['.implode(',', $jsonArray).'],
"groups" : ';
$jsonArray = array();
foreach ($groups AS $group)
foreach ($groups AS $group) {
$jsonArray[] = '{"value":"1_'.(int)($group['id_group']).'", "text":"'.addslashes($group['name']).'"}';
}
$json .= '['.implode(',', $jsonArray).']}';
die($json);
}
@ -705,4 +730,3 @@ if (Tools::isSubmit('getChildrenCategories') && Tools::getValue('id_category_par
$children_categories = Category::getChildrenWithNbSelectedSubCat(Tools::getValue('id_category_parent'), Tools::getValue('selectedCat'), $cookie->id_lang);
die(Tools::jsonEncode($children_categories));
}