Merge pull request #1065 from shreesh-webkul/gli-2071

Added: option to view frequent customer in backoffice customer page
This commit is contained in:
Rohit Jain
2024-08-05 12:48:31 +05:30
committed by GitHub
3 changed files with 32 additions and 18 deletions

View File

@@ -133,7 +133,7 @@ class AdminCustomerPreferencesControllerCore extends AdminController
'PS_KPI_FREQUENT_CUSTOMER_NB_ORDERS' => array(
'title' => $this->l('Number of orders to use to calculate frequent customers'),
'hint' => $this->l('Set the number of orders to use to calculate frequent customers.'),
'validation' => 'isUnsignedInt',
'validation' => 'isInt',
'cast' => 'intval',
'type' => 'text',
'class' => 'fixed-width-xxl',
@@ -173,6 +173,14 @@ class AdminCustomerPreferencesControllerCore extends AdminController
);
}
public function beforeUpdateOptions()
{
$fieldData = $this->fields_options['customer_kpi']['fields']['PS_KPI_FREQUENT_CUSTOMER_NB_ORDERS'];
if (!Tools::getValue('PS_KPI_FREQUENT_CUSTOMER_NB_ORDERS')) {
$this->errors[] = sprintf(Tools::displayError('field %s must be greater than 0.'), $fieldData['title']);;
}
}
/**
* Update PS_B2B_ENABLE and enables / disables the associated tabs
* @param $value integer Value of option

View File

@@ -79,6 +79,9 @@ class AdminCustomersControllerCore extends AdminController
$this->_join = 'LEFT JOIN '._DB_PREFIX_.'gender_lang gl ON (a.id_gender = gl.id_gender AND gl.id_lang = '.(int)$this->context->language->id.')';
$this->_join .= ' LEFT JOIN '._DB_PREFIX_.'group_lang grl ON (a.id_default_group = grl.id_group AND grl.id_lang = '.(int)$this->context->language->id.')';
$this->_join .= ' LEFT JOIN '._DB_PREFIX_.'orders o ON (a.id_customer = o.id_customer)';
$this->_group = 'GROUP BY a.`id_customer`';
$this->_use_found_rows = false;
$this->fields_list = array(
'id_customer' => array(
@@ -123,6 +126,7 @@ class AdminCustomersControllerCore extends AdminController
),
'total_orders' => array(
'title' => $this->l('Number of orders'),
'type' => 'range',
'optional' => true,
'visible_default' => true,
'havingFilter' => true,
@@ -163,6 +167,7 @@ class AdminCustomersControllerCore extends AdminController
'date_add' => array(
'title' => $this->l('Registration'),
'type' => 'date',
'filter_key' => 'a!date_add',
'align' => 'text-right'
),
'connect' => array(
@@ -175,6 +180,12 @@ class AdminCustomersControllerCore extends AdminController
'title' => $this->l('Banned'),
'type' => 'bool',
'displayed' => false,
),
'order_date' => array(
'title' => $this->l('Order date'),
'type' => 'date',
'filter_key' => 'o!date_add',
'displayed' => false
)
));
@@ -183,19 +194,8 @@ class AdminCustomersControllerCore extends AdminController
parent::__construct();
$this->_select = '
a.date_add, gl.name as title, grl.name as default_group_name, (
SELECT SUM(total_paid_real / conversion_rate)
FROM '._DB_PREFIX_.'orders o
WHERE o.id_customer = a.id_customer
'.Shop::addSqlRestriction(Shop::SHARE_ORDER, 'o').'
AND o.valid = 1
) as total_spent, (
SELECT COUNT(o.`id_order`)
FROM '._DB_PREFIX_.'orders o
WHERE o.id_customer = a.id_customer
'.Shop::addSqlRestriction(Shop::SHARE_ORDER, 'o').'
AND o.valid = 1
) as total_orders, (
a.date_add, gl.name as title, grl.name as default_group_name, COUNT(o.`id_order`) as total_orders,
o.`date_add` as order_date, SUM(total_paid_real / conversion_rate) as total_spent, (
SELECT c.date_add FROM '._DB_PREFIX_.'guest g
LEFT JOIN '._DB_PREFIX_.'connections c ON c.id_guest = g.id_guest
WHERE g.id_customer = a.id_customer
@@ -707,7 +707,8 @@ class AdminCustomersControllerCore extends AdminController
$helper->icon = 'icon-star';
$helper->color = 'color2';
$helper->title = $this->l('Total Frequent Customers', null, null, false);
$helper->subtitle = $this->l('All Time', null, null, false);
$helper->subtitle = $this->l('1 year', null, null, false);
$helper->href = $this->context->link->getAdminLink('AdminCustomers').'&submitFiltercustomer=1&customerFilter_total_orders%5B0%5D='.Configuration::get('PS_KPI_FREQUENT_CUSTOMER_NB_ORDERS').'&customerFilter_o%21date_add%5B0%5D='.date('Y-m-d', strtotime('-365 day')).'&customerFilter_o%21date_add%5B1%5D='.date('Y-m-d');
$helper->source = $this->context->link->getAdminLink('AdminStats').'&ajax=1&action=getKpi&kpi=total_frequent_customers';
$helper->tooltip = $this->l('The total number of frequent customers in given period of time.', null, null, false);
$kpis[] = $helper;

View File

@@ -910,7 +910,12 @@ class AdminStatsControllerCore extends AdminStatsTabController
case 'total_frequent_customers':
$nbOrdersFrequentCustomers = Configuration::get('PS_KPI_FREQUENT_CUSTOMER_NB_ORDERS');
$value = AdminStatsController::getTotalFrequentCustomers($nbOrdersFrequentCustomers, 0);
$value = AdminStatsController::getTotalFrequentCustomers(
date('Y-m-d', strtotime('-365 day')),
date('Y-m-d'),
$nbOrdersFrequentCustomers,
0
);
break;
@@ -1829,7 +1834,7 @@ class AdminStatsControllerCore extends AdminStatsTabController
return $result;
}
public static function getTotalFrequentCustomers($nbOrders = 5, $idHotel = null)
public static function getTotalFrequentCustomers($dateFrom, $dateTo, $nbOrders = 5, $idHotel = null)
{
$sql = 'SELECT COUNT(t.`id_customer`)
FROM (
@@ -1840,7 +1845,7 @@ class AdminStatsControllerCore extends AdminStatsTabController
WHERE hbd.`id_order` = o.`id_order` LIMIT 1
) AS id_hotel
FROM `'._DB_PREFIX_.'orders` o
WHERE o.`valid` = 1
WHERE o.`valid` = 1 AND o.`date_add` BETWEEN "'.pSQL($dateFrom).' 00:00:00" AND "'.pSQL($dateTo).' 23:59:59"
GROUP BY o.`id_customer`
HAVING 1 '.(!is_null($idHotel) ? HotelBranchInformation::addHotelRestriction($idHotel) : '').'
) AS t