From 799efcba3fea65523bc6210038f1973b2ecad5ec Mon Sep 17 00:00:00 2001 From: Damien Metzger Date: Tue, 24 Sep 2013 15:31:52 +0200 Subject: [PATCH] [*] BO : Customers KPIs --- classes/Configuration.php | 42 +++++------ .../admin/AdminCustomersController.php | 60 ++++++++++++++++ controllers/admin/AdminStatsController.php | 72 +++++++++++++++++++ modules/dashactivity/dashactivity.php | 2 +- 4 files changed, 154 insertions(+), 22 deletions(-) diff --git a/classes/Configuration.php b/classes/Configuration.php index 56eb4f072..ae7f7d2f0 100644 --- a/classes/Configuration.php +++ b/classes/Configuration.php @@ -61,7 +61,7 @@ class ConfigurationCore extends ObjectModel ); /** @var array Configuration cache */ - protected static $_CONF; + protected static $_cache = array(); /** @var array Vars types */ protected static $types = array(); @@ -109,7 +109,7 @@ class ConfigurationCore extends ObjectModel */ public static function loadConfiguration() { - self::$_CONF = array(); + self::$_cache[self::$definition['table']] = array(); $sql = 'SELECT c.`name`, cl.`id_lang`, IF(cl.`id_lang` IS NULL, c.`value`, cl.`value`) AS value, c.id_shop_group, c.id_shop FROM `'._DB_PREFIX_.bqSQL(self::$definition['table']).'` c LEFT JOIN `'._DB_PREFIX_.bqSQL(self::$definition['table']).'_lang` cl ON (c.`'.bqSQL(self::$definition['primary']).'` = cl.`'.bqSQL(self::$definition['primary']).'`)'; @@ -120,19 +120,19 @@ class ConfigurationCore extends ObjectModel { $lang = ($row['id_lang']) ? $row['id_lang'] : 0; self::$types[$row['name']] = ($lang) ? 'lang' : 'normal'; - if (!isset(self::$_CONF[$lang])) - self::$_CONF[$lang] = array( + if (!isset(self::$_cache[self::$definition['table']][$lang])) + self::$_cache[self::$definition['table']][$lang] = array( 'global' => array(), 'group' => array(), 'shop' => array(), ); if ($row['id_shop']) - self::$_CONF[$lang]['shop'][$row['id_shop']][$row['name']] = $row['value']; + self::$_cache[self::$definition['table']][$lang]['shop'][$row['id_shop']][$row['name']] = $row['value']; else if ($row['id_shop_group']) - self::$_CONF[$lang]['group'][$row['id_shop_group']][$row['name']] = $row['value']; + self::$_cache[self::$definition['table']][$lang]['group'][$row['id_shop_group']][$row['name']] = $row['value']; else - self::$_CONF[$lang]['global'][$row['name']] = $row['value']; + self::$_cache[self::$definition['table']][$lang]['global'][$row['name']] = $row['value']; } } @@ -149,10 +149,10 @@ class ConfigurationCore extends ObjectModel return false; // If conf if not initialized, try manual query - if (!self::$_CONF) + if (!isset(self::$_cache[self::$definition['table']])) { Configuration::loadConfiguration(); - if (!self::$_CONF) + if (!self::$_cache[self::$definition['table']]) return Db::getInstance()->getValue('SELECT `value` FROM `'._DB_PREFIX_.bqSQL(self::$definition['table']).'` WHERE `name` = "'.pSQL($key).'"'); } $id_lang = (int)$id_lang; @@ -161,15 +161,15 @@ class ConfigurationCore extends ObjectModel if ($id_shop_group === null) $id_shop_group = Shop::getContextShopGroupID(true); - if (!isset(self::$_CONF[$id_lang])) + if (!isset(self::$_cache[self::$definition['table']][$id_lang])) $id_lang = 0; if ($id_shop && Configuration::hasKey($key, $id_lang, null, $id_shop)) - return self::$_CONF[$id_lang]['shop'][$id_shop][$key]; + return self::$_cache[self::$definition['table']][$id_lang]['shop'][$id_shop][$key]; elseif ($id_shop_group && Configuration::hasKey($key, $id_lang, $id_shop_group)) - return self::$_CONF[$id_lang]['group'][$id_shop_group][$key]; + return self::$_cache[self::$definition['table']][$id_lang]['group'][$id_shop_group][$key]; elseif (Configuration::hasKey($key, $id_lang)) - return self::$_CONF[$id_lang]['global'][$key]; + return self::$_cache[self::$definition['table']][$id_lang]['global'][$key]; return false; } @@ -232,10 +232,10 @@ class ConfigurationCore extends ObjectModel { $id_lang = (int)$id_lang; if ($id_shop) - return isset(self::$_CONF[$id_lang]['shop'][$id_shop]) && array_key_exists($key, self::$_CONF[$id_lang]['shop'][$id_shop]); + return isset(self::$_cache[self::$definition['table']][$id_lang]['shop'][$id_shop]) && array_key_exists($key, self::$_cache[self::$definition['table']][$id_lang]['shop'][$id_shop]); elseif ($id_shop_group) - return isset(self::$_CONF[$id_lang]['group'][$id_shop_group]) && array_key_exists($key, self::$_CONF[$id_lang]['group'][$id_shop_group]); - return isset(self::$_CONF[$id_lang]['global']) && array_key_exists($key, self::$_CONF[$id_lang]['global']); + return isset(self::$_cache[self::$definition['table']][$id_lang]['group'][$id_shop_group]) && array_key_exists($key, self::$_cache[self::$definition['table']][$id_lang]['group'][$id_shop_group]); + return isset(self::$_cache[self::$definition['table']][$id_lang]['global']) && array_key_exists($key, self::$_cache[self::$definition['table']][$id_lang]['global']); } /** @@ -262,11 +262,11 @@ class ConfigurationCore extends ObjectModel foreach ($values as $lang => $value) { if ($id_shop) - self::$_CONF[$lang]['shop'][$id_shop][$key] = $value; + self::$_cache[self::$definition['table']][$lang]['shop'][$id_shop][$key] = $value; else if ($id_shop_group) - self::$_CONF[$lang]['group'][$id_shop_group][$key] = $value; + self::$_cache[self::$definition['table']][$lang]['group'][$id_shop_group][$key] = $value; else - self::$_CONF[$lang]['global'][$key] = $value; + self::$_cache[self::$definition['table']][$lang]['global'][$key] = $value; } } @@ -401,7 +401,7 @@ class ConfigurationCore extends ObjectModel DELETE FROM `'._DB_PREFIX_.bqSQL(self::$definition['table']).'` WHERE `name` = "'.pSQL($key).'"'); - self::$_CONF = null; + self::$_cache[self::$definition['table']] = null; return ($result && $result2); } @@ -429,7 +429,7 @@ class ConfigurationCore extends ObjectModel DELETE FROM `'._DB_PREFIX_.bqSQL(self::$definition['table']).'_lang` WHERE `'.bqSQL(self::$definition['primary']).'` = '.(int)$id); - self::$_CONF = null; + self::$_cache[self::$definition['table']] = null; } /** diff --git a/controllers/admin/AdminCustomersController.php b/controllers/admin/AdminCustomersController.php index adc92569a..d4682767b 100644 --- a/controllers/admin/AdminCustomersController.php +++ b/controllers/admin/AdminCustomersController.php @@ -525,6 +525,66 @@ class AdminCustomersControllerCore extends AdminController { $customer->id_shop = $this->context->shop->id; } + + public function renderKpis() + { + $time = time(); + $kpis = array(); + + /* The data generation is located in AdminStatsControllerCore */ + + $helper = new HelperKpi(); + $helper->id = 'box-gender'; + $helper->icon = 'icon-male'; + $helper->color = 'color1'; + $helper->title = $this->l('Customers'); + $helper->subtitle = $this->l('All Time'); + if (ConfigurationKPI::get('CUSTOMER_MAIN_GENDER') !== false) + $helper->value = ConfigurationKPI::get('CUSTOMER_MAIN_GENDER'); + if (ConfigurationKPI::get('CUSTOMER_MAIN_GENDER_EXPIRE') < $time) + $helper->source = $this->context->link->getAdminLink('AdminStats').'&ajax=1&action=getKpi&kpi=customer_main_gender'; + $kpis[] = $helper->generate(); + + $helper = new HelperKpi(); + $helper->id = 'box-age'; + $helper->icon = 'icon-calendar'; + $helper->color = 'color2'; + $helper->title = $this->l('Average Age'); + $helper->subtitle = $this->l('All Time'); + if (ConfigurationKPI::get('CUSTOMER_AGE') !== false) + $helper->value = ConfigurationKPI::get('CUSTOMER_AGE'); + if (ConfigurationKPI::get('CUSTOMER_AGE_EXPIRE') < $time) + $helper->source = $this->context->link->getAdminLink('AdminStats').'&ajax=1&action=getKpi&kpi=customer_age'; + $kpis[] = $helper->generate(); + + $helper = new HelperKpi(); + $helper->id = 'box-orders'; + $helper->icon = 'icon-retweet'; + $helper->color = 'color3'; + $helper->title = $this->l('Orders per Customer'); + $helper->subtitle = $this->l('All Time'); + if (ConfigurationKPI::get('ORDERS_PER_CUSTOMER') !== false) + $helper->value = ConfigurationKPI::get('ORDERS_PER_CUSTOMER'); + if (ConfigurationKPI::get('ORDERS_PER_CUSTOMER_EXPIRE') < $time) + $helper->source = $this->context->link->getAdminLink('AdminStats').'&ajax=1&action=getKpi&kpi=orders_per_customer'; + $kpis[] = $helper->generate(); + + $helper = new HelperKpi(); + $helper->id = 'box-newsletter'; + $helper->icon = 'icon-envelope'; + $helper->color = 'color4'; + $helper->title = $this->l('Newsletter Registrations'); + $helper->subtitle = $this->l('All Time'); + if (ConfigurationKPI::get('NEWSLETTER_REGISTRATIONS') !== false) + $helper->value = ConfigurationKPI::get('NEWSLETTER_REGISTRATIONS'); + if (ConfigurationKPI::get('NEWSLETTER_REGISTRATIONS_EXPIRE') < $time) + $helper->source = $this->context->link->getAdminLink('AdminStats').'&ajax=1&action=getKpi&kpi=newsletter_registrations'; + $kpis[] = $helper->generate(); + + $helper = new HelperKpiRow(); + $helper->kpis = $kpis; + return $helper->generate(); + } public function renderView() { diff --git a/controllers/admin/AdminStatsController.php b/controllers/admin/AdminStatsController.php index d7cb3d385..e3e9df6bc 100644 --- a/controllers/admin/AdminStatsController.php +++ b/controllers/admin/AdminStatsController.php @@ -141,6 +141,78 @@ class AdminStatsControllerCore extends AdminStatsTabController ConfigurationKPI::updateValue('EMPTY_CATS_EXPIRE', strtotime('+2 hour')); break; + case 'customer_main_gender': + $row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' + SELECT SUM(IF(g.id_gender IS NOT NULL, 1, 0)) as total, SUM(IF(type = 0, 1, 0)) as male, SUM(IF(type = 1, 1, 0)) as female, SUM(IF(type = 2, 1, 0)) as neutral + FROM `'._DB_PREFIX_.'customer` c + '.Shop::addSqlAssociation('customer', 'c').' + LEFT JOIN `'._DB_PREFIX_.'gender` g ON c.id_gender = g.id_gender + WHERE c.active = 1'); + if (!$row['total']) + $value = $this->l('No customers'); + elseif ($row['male'] > $row['female'] && $row['male'] > $row['neutral']) + $value = sprintf($this->l('%d%% Men Customers'), round(100 * $row['male'] / $row['total'])); + elseif ($row['female'] > $row['male'] && $row['female'] > $row['neutral']) + $value = sprintf($this->l('%d%% Women Customers'), round(100 * $row['female'] / $row['total'])); + else + $value = sprintf($this->l('%d%% Neutral Customers'), round(100 * $row['neutral'] / $row['total'])); + + ConfigurationKPI::updateValue('CUSTOMER_MAIN_GENDER', $value); + ConfigurationKPI::updateValue('CUSTOMER_MAIN_GENDER_EXPIRE', strtotime('+1 day')); + break; + + case 'customer_age': + $value = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' + SELECT AVG(birthday) + FROM `'._DB_PREFIX_.'customer` + '.Shop::addSqlAssociation('customer').' + WHERE active = 1 + AND birthday IS NOT NULL AND birthday != "0000-00-00"'); + $value = sprintf($this->l('%.1f years'), round((time() - strtotime($value)) / 86400 / 365, 1)); + + ConfigurationKPI::updateValue('CUSTOMER_AGE', $value); + ConfigurationKPI::updateValue('CUSTOMER_AGE_EXPIRE', strtotime('+1 day')); + break; + + case 'newsletter_registrations': + $value = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' + SELECT COUNT(*) + FROM `'._DB_PREFIX_.'customer` + WHERE newsletter = 1 + '.Shop::addSqlRestriction(Shop::SHARE_ORDER)); + if (Module::isInstalled('blocknewsletter')) + { + $value += Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' + SELECT COUNT(*) + FROM `'._DB_PREFIX_.'newsletter` + WHERE active = 1 + '.Shop::addSqlRestriction(Shop::SHARE_ORDER)); + } + + ConfigurationKPI::updateValue('NEWSLETTER_REGISTRATIONS', $value); + ConfigurationKPI::updateValue('NEWSLETTER_REGISTRATIONS_EXPIRE', strtotime('+6 hour')); + break; + + case 'orders_per_customer': + $value = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' + SELECT COUNT(*) + FROM `'._DB_PREFIX_.'customer` + '.Shop::addSqlAssociation('customer').' + WHERE active = 1'); + if ($value) + { + $orders = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' + SELECT COUNT(*) + FROM `'._DB_PREFIX_.'orders` + '.Shop::addSqlAssociation('orders').' + WHERE valid = 1'); + $value = round($orders / $value, 2); + } + + ConfigurationKPI::updateValue('ORDERS_PER_CUSTOMER', $value); + ConfigurationKPI::updateValue('ORDERS_PER_CUSTOMER_EXPIRE', strtotime('+1 day')); + break; + case 'average_order_value': $row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' SELECT diff --git a/modules/dashactivity/dashactivity.php b/modules/dashactivity/dashactivity.php index 87f4071c5..9955efdcb 100644 --- a/modules/dashactivity/dashactivity.php +++ b/modules/dashactivity/dashactivity.php @@ -198,7 +198,7 @@ class Dashactivity extends Module $product_reviews = 0; if (Module::isInstalled('productcomments')) { - $new_registrations += Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' + $product_reviews += Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' SELECT COUNT(*) FROM `'._DB_PREFIX_.'product_comment` pc LEFT JOIN `'._DB_PREFIX_.'product` p ON (pc.id_product = p.id_product)