Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added: option to view frequent customer in backoffice customer page #1065

Merged
merged 11 commits into from
Aug 5, 2024
Merged
10 changes: 9 additions & 1 deletion controllers/admin/AdminCustomerPreferencesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function __construct()
'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',
Expand Down Expand Up @@ -173,6 +173,14 @@ public function __construct()
);
}

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
Expand Down
29 changes: 15 additions & 14 deletions controllers/admin/AdminCustomersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public function __construct()

$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(
Expand Down Expand Up @@ -123,6 +126,7 @@ public function __construct()
),
'total_orders' => array(
'title' => $this->l('Number of orders'),
'type' => 'range',
'optional' => true,
'visible_default' => true,
'havingFilter' => true,
Expand Down Expand Up @@ -163,6 +167,7 @@ public function __construct()
'date_add' => array(
'title' => $this->l('Registration'),
'type' => 'date',
'filter_key' => 'a!date_add',
'align' => 'text-right'
),
'connect' => array(
Expand All @@ -175,6 +180,12 @@ public function __construct()
'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
)
));

Expand All @@ -183,19 +194,8 @@ public function __construct()
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
Expand Down Expand Up @@ -707,7 +707,8 @@ public function renderKpis()
$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;
Expand Down
11 changes: 8 additions & 3 deletions controllers/admin/AdminStatsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,12 @@ public function getLatestKpiValue($kpi)
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;

Expand Down Expand Up @@ -1829,7 +1834,7 @@ public static function getDisabledRoomsForDiscreteDates($dateFrom, $dateTo = nul
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 (
Expand All @@ -1840,7 +1845,7 @@ public static function getTotalFrequentCustomers($nbOrders = 5, $idHotel = null)
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
Expand Down