From b80b363a2e0b6c2871925e1da17d3c853b51f237 Mon Sep 17 00:00:00 2001 From: Damien Metzger Date: Wed, 25 Sep 2013 15:24:19 +0200 Subject: [PATCH] [*] BO : Added Customer Service KPIs --- .../admin/AdminCustomerThreadsController.php | 47 ++++++++++++++ .../admin/AdminCustomersController.php | 2 +- controllers/admin/AdminStatsController.php | 64 +++++++++++++++++++ 3 files changed, 112 insertions(+), 1 deletion(-) diff --git a/controllers/admin/AdminCustomerThreadsController.php b/controllers/admin/AdminCustomerThreadsController.php index 5d8d37fa1..6e40422d3 100644 --- a/controllers/admin/AdminCustomerThreadsController.php +++ b/controllers/admin/AdminCustomerThreadsController.php @@ -466,6 +466,53 @@ class AdminCustomerThreadsControllerCore extends AdminController die; } + public function renderKpis() + { + $time = time(); + $kpis = array(); + + /* The data generation is located in AdminStatsControllerCore */ + + $helper = new HelperKpi(); + $helper->id = 'box-pending-messages'; + $helper->icon = 'icon-envelope'; + $helper->color = 'color1'; + $helper->title = $this->l('Pending Messages'); + if (ConfigurationKPI::get('PENDING_MESSAGES') !== false) + $helper->value = ConfigurationKPI::get('PENDING_MESSAGES'); + if (ConfigurationKPI::get('PENDING_MESSAGES_EXPIRE') < $time) + $helper->source = $this->context->link->getAdminLink('AdminStats').'&ajax=1&action=getKpi&kpi=pending_messages'; + $kpis[] = $helper->generate(); + + $helper = new HelperKpi(); + $helper->id = 'box-age'; + $helper->icon = 'icon-time'; + $helper->color = 'color2'; + $helper->title = $this->l('Average Response Time'); + $helper->subtitle = $this->l('30 days'); + if (ConfigurationKPI::get('AVG_MSG_RESPONSE_TIME') !== false) + $helper->value = ConfigurationKPI::get('AVG_MSG_RESPONSE_TIME'); + if (ConfigurationKPI::get('AVG_MSG_RESPONSE_TIME_EXPIRE') < $time) + $helper->source = $this->context->link->getAdminLink('AdminStats').'&ajax=1&action=getKpi&kpi=avg_msg_response_time'; + $kpis[] = $helper->generate(); + + $helper = new HelperKpi(); + $helper->id = 'box-messages-per-thread'; + $helper->icon = 'icon-copy'; + $helper->color = 'color3'; + $helper->title = $this->l('Messages per Thread'); + $helper->subtitle = $this->l('30 day'); + if (ConfigurationKPI::get('MESSAGES_PER_THREAD') !== false) + $helper->value = ConfigurationKPI::get('MESSAGES_PER_THREAD'); + if (ConfigurationKPI::get('MESSAGES_PER_THREAD_EXPIRE') < $time) + $helper->source = $this->context->link->getAdminLink('AdminStats').'&ajax=1&action=getKpi&kpi=messages_per_thread'; + $kpis[] = $helper->generate(); + + $helper = new HelperKpiRow(); + $helper->kpis = $kpis; + return $helper->generate(); + } + public function renderView() { if (!$id_customer_thread = (int)Tools::getValue('id_customer_thread')) diff --git a/controllers/admin/AdminCustomersController.php b/controllers/admin/AdminCustomersController.php index d46175767..df5ec99bc 100644 --- a/controllers/admin/AdminCustomersController.php +++ b/controllers/admin/AdminCustomersController.php @@ -525,7 +525,7 @@ class AdminCustomersControllerCore extends AdminController { $customer->id_shop = $this->context->shop->id; } - + public function renderKpis() { $time = time(); diff --git a/controllers/admin/AdminStatsController.php b/controllers/admin/AdminStatsController.php index 928ced0fd..39ad801e4 100644 --- a/controllers/admin/AdminStatsController.php +++ b/controllers/admin/AdminStatsController.php @@ -229,6 +229,52 @@ class AdminStatsControllerCore extends AdminStatsTabController return round((time() - strtotime($value)) / 86400 / 365, 1); } + public static function getPendingMessages() + { + return CustomerThread::getTotalCustomerThreads('status LIKE "%pending%" OR status = "open"'); + } + + public static function getAverageMessageResponseTime($date_from, $date_to) + { + $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' + SELECT MIN(cm1.date_add) as question, MIN(cm2.date_add) as reply + FROM `'._DB_PREFIX_.'customer_message` cm1 + INNER JOIN `'._DB_PREFIX_.'customer_message` cm2 ON (cm1.id_customer_thread = cm2.id_customer_thread AND cm1.date_add < cm2.date_add) + WHERE cm1.`date_add` BETWEEN "'.pSQL($date_from).' 00:00:00" AND "'.pSQL($date_to).' 23:59:59" + AND cm1.id_employee = 0 AND cm2.id_employee != 0 + GROUP BY cm1.id_customer_thread'); + $total_questions = $total_replies = $threads = 0; + foreach ($result as $row) + { + ++$threads; + $total_questions += strtotime($row['question']); + $total_replies += strtotime($row['reply']); + } + if (!$threads) + return 0; + return round(($total_replies - $total_questions) / $threads / 3600, 1); + } + + public static function getMessagesPerThread($date_from, $date_to) + { + $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' + SELECT COUNT(*) as messages + FROM `'._DB_PREFIX_.'customer_thread` ct + LEFT JOIN `'._DB_PREFIX_.'customer_message` cm ON (ct.id_customer_thread = cm.id_customer_thread) + WHERE ct.`date_add` BETWEEN "'.pSQL($date_from).' 00:00:00" AND "'.pSQL($date_to).' 23:59:59" + AND status = "closed" + GROUP BY ct.id_customer_thread'); + $threads = $messages = 0; + foreach ($result as $row) + { + ++$threads; + $messages += $row['messages']; + } + if (!$threads) + return 0; + return round($messages / $threads, 1); + } + public function displayAjaxGetKpi() { $currency = new Currency(Configuration::get('PS_CURRENCY_DEFAULT')); @@ -317,6 +363,24 @@ class AdminStatsControllerCore extends AdminStatsTabController ConfigurationKPI::updateValue('AVG_CUSTOMER_AGE_EXPIRE', strtotime('+1 day')); break; + case 'pending_messages': + $value = (int)AdminStatsController::getPendingMessages(); + ConfigurationKPI::updateValue('PENDING_MESSAGES', $value); + ConfigurationKPI::updateValue('PENDING_MESSAGES_EXPIRE', strtotime('+5 min')); + break; + + case 'avg_msg_response_time': + $value = sprintf($this->l('%.1f hours'), AdminStatsController::getAverageMessageResponseTime(date('Y-m-d', strtotime('-31 day')), date('Y-m-d', strtotime('-1 day')))); + ConfigurationKPI::updateValue('AVG_MSG_RESPONSE_TIME', $value); + ConfigurationKPI::updateValue('AVG_MSG_RESPONSE_TIME_EXPIRE', strtotime('+4 hour')); + break; + + case 'messages_per_thread': + $value = round(AdminStatsController::getMessagesPerThread(date('Y-m-d', strtotime('-31 day')), date('Y-m-d', strtotime('-1 day'))), 1); + ConfigurationKPI::updateValue('MESSAGES_PER_THREAD', $value); + ConfigurationKPI::updateValue('MESSAGES_PER_THREAD_EXPIRE', strtotime('+12 hour')); + break; + case 'newsletter_registrations': $value = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' SELECT COUNT(*)