public function displayAjaxGetKpi() { $currency = new Currency(Configuration::get('PS_CURRENCY_DEFAULT')); switch (Tools::getValue('kpi')) { case 'conversion_rate': $visitors = AdminStatsController::getVisits(true, date('Y-m-d', strtotime('-31 day')), date('Y-m-d', strtotime('-1 day')), false); $orders = AdminStatsController::getOrders(date('Y-m-d', strtotime('-31 day')), date('Y-m-d', strtotime('-1 day')), false); // $data = array(); // $from = strtotime(date('Y-m-d 00:00:00', strtotime('-31 day'))); // $to = strtotime(date('Y-m-d 23:59:59', strtotime('-1 day'))); // for ($date = $from; $date <= $to; $date = strtotime('+1 day', $date)) // if (isset($visitors[$date]) && $visitors[$date]) // $data[$date] = round(100 * ((isset($orders[$date]) && $orders[$date]) ? $orders[$date] : 0) / $visitors[$date], 2); // else // $data[$date] = 0; $visits_sum = $visitors; //array_sum($visitors); $orders_sum = $orders; //array_sum($orders); if ($visits_sum) { $value = round(100 * $orders_sum / $visits_sum, 2); } elseif ($orders_sum) { $value = '∞'; } else { $value = 0; } $value .= '%'; // ConfigurationKPI::updateValue('CONVERSION_RATE_CHART', Tools::jsonEncode($data)); ConfigurationKPI::updateValue('CONVERSION_RATE', $value); ConfigurationKPI::updateValue('CONVERSION_RATE_EXPIRE', strtotime(date('Y-m-d 00:00:00', strtotime('+1 day')))); break; case 'abandoned_cart': $value = AdminStatsController::getAbandonedCarts(date('Y-m-d H:i:s', strtotime('-2 day')), date('Y-m-d H:i:s', strtotime('-1 day'))); ConfigurationKPI::updateValue('ABANDONED_CARTS', $value); ConfigurationKPI::updateValue('ABANDONED_CARTS_EXPIRE', strtotime('+1 hour')); break; case 'installed_modules': $value = AdminStatsController::getInstalledModules(); ConfigurationKPI::updateValue('INSTALLED_MODULES', $value); ConfigurationKPI::updateValue('INSTALLED_MODULES_EXPIRE', strtotime('+2 min')); break; case 'disabled_modules': $value = AdminStatsController::getDisabledModules(); ConfigurationKPI::updateValue('DISABLED_MODULES', $value); ConfigurationKPI::updateValue('DISABLED_MODULES_EXPIRE', strtotime('+2 min')); break; case 'update_modules': $value = AdminStatsController::getModulesToUpdate(); ConfigurationKPI::updateValue('UPDATE_MODULES', $value); ConfigurationKPI::updateValue('UPDATE_MODULES_EXPIRE', strtotime('+2 min')); break; case 'percent_product_stock': $value = AdminStatsController::getPercentProductStock(); ConfigurationKPI::updateValue('PERCENT_PRODUCT_STOCK', $value); ConfigurationKPI::updateValue('PERCENT_PRODUCT_STOCK_EXPIRE', strtotime('+4 hour')); break; case 'product_avg_gross_margin': $value = AdminStatsController::getProductAverageGrossMargin(); ConfigurationKPI::updateValue('PRODUCT_AVG_GROSS_MARGIN', $value); ConfigurationKPI::updateValue('PRODUCT_AVG_GROSS_MARGIN_EXPIRE', strtotime('+6 hour')); break; case 'disabled_categories': $value = AdminStatsController::getDisabledCategories(); ConfigurationKPI::updateValue('DISABLED_CATEGORIES', $value); ConfigurationKPI::updateValue('DISABLED_CATEGORIES_EXPIRE', strtotime('+2 hour')); break; case 'disabled_products': $value = AdminStatsController::getDisabledProducts(); ConfigurationKPI::updateValue('DISABLED_PRODUCTS', $value); ConfigurationKPI::updateValue('DISABLED_PRODUCTS_EXPIRE', strtotime('+2 hour')); break; case '8020_sales_catalog': $value = AdminStatsController::get8020SalesCatalog(date('Y-m-d', strtotime('-1 month')), date('Y-m-d')); $value = sprintf($this->l('%d%% of your Catalog'), $value); ConfigurationKPI::updateValue('8020_SALES_CATALOG', $value); ConfigurationKPI::updateValue('8020_SALES_CATALOG_EXPIRE', strtotime('+12 hour')); break; case 'empty_categories': $value = AdminStatsController::getEmptyCategories(); ConfigurationKPI::updateValue('EMPTY_CATEGORIES', $value); ConfigurationKPI::updateValue('EMPTY_CATEGORIES_EXPIRE', strtotime('+2 hour')); break; case 'customer_main_gender': $value = AdminStatsController::getCustomerMainGender(); if ($value === false) { $value = $this->l('No customers', null, null, false); } elseif ($value['type'] == 'female') { $value = sprintf($this->l('%d%% Women Customers', null, null, false), $value['value']); } elseif ($value['type'] == 'male') { $value = sprintf($this->l('%d%% Men Customers', null, null, false), $value['value']); } else { $value = sprintf($this->l('%d%% Neutral Customers', null, null, false), $value['value']); } ConfigurationKPI::updateValue('CUSTOMER_MAIN_GENDER', array($this->context->language->id => $value)); ConfigurationKPI::updateValue('CUSTOMER_MAIN_GENDER_EXPIRE', array($this->context->language->id => strtotime('+1 day'))); break; case 'avg_customer_age': $value = sprintf($this->l('%d years', null, null, false), AdminStatsController::getAverageCustomerAge(), 1); ConfigurationKPI::updateValue('AVG_CUSTOMER_AGE', $value); 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', null, null, false), 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(*) 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 'enabled_languages': $value = Language::countActiveLanguages(); ConfigurationKPI::updateValue('ENABLED_LANGUAGES', $value); ConfigurationKPI::updateValue('ENABLED_LANGUAGES_EXPIRE', strtotime('+1 min')); break; case 'frontoffice_translations': $themes = Theme::getThemes(); $languages = Language::getLanguages(); $total = $translated = 0; foreach ($themes as $theme) { foreach ($languages as $language) { $kpi_key = substr(strtoupper($theme->name . '_' . $language['iso_code']), 0, 16); $total += ConfigurationKPI::get('TRANSLATE_TOTAL_' . $kpi_key); $translated += ConfigurationKPI::get('TRANSLATE_DONE_' . $kpi_key); } } $value = 0; if ($translated) { $value = round(100 * $translated / $total, 1); } $value .= '%'; ConfigurationKPI::updateValue('FRONTOFFICE_TRANSLATIONS', $value); ConfigurationKPI::updateValue('FRONTOFFICE_TRANSLATIONS_EXPIRE', strtotime('+2 min')); break; case 'main_country': if (!($row = AdminStatsController::getMainCountry(date('Y-m-d', strtotime('-30 day')), date('Y-m-d')))) { $value = $this->l('No orders', null, null, false); } else { $country = new Country($row['id_country'], $this->context->language->id); $value = sprintf($this->l('%d%% %s', null, null, false), $row['orders'], $country->name); } ConfigurationKPI::updateValue('MAIN_COUNTRY', array($this->context->language->id => $value)); ConfigurationKPI::updateValue('MAIN_COUNTRY_EXPIRE', array($this->context->language->id => strtotime('+1 day'))); break; case 'orders_per_customer': $value = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' SELECT COUNT(*) FROM `' . _DB_PREFIX_ . 'customer` c WHERE active = 1 ' . Shop::addSqlRestriction()); if ($value) { $orders = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' SELECT COUNT(*) FROM `' . _DB_PREFIX_ . 'orders` o WHERE valid = 1 ' . Shop::addSqlRestriction()); $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 COUNT(`id_order`) as orders, SUM(`total_paid_tax_excl` / `conversion_rate`) as total_paid_tax_excl FROM `' . _DB_PREFIX_ . 'orders` WHERE `invoice_date` BETWEEN "' . pSQL(date('Y-m-d', strtotime('-31 day'))) . ' 00:00:00" AND "' . pSQL(date('Y-m-d', strtotime('-1 day'))) . ' 23:59:59" ' . Shop::addSqlRestriction()); $value = Tools::displayPrice($row['orders'] ? $row['total_paid_tax_excl'] / $row['orders'] : 0, $currency); ConfigurationKPI::updateValue('AVG_ORDER_VALUE', $value); ConfigurationKPI::updateValue('AVG_ORDER_VALUE_EXPIRE', strtotime(date('Y-m-d 00:00:00', strtotime('+1 day')))); break; case 'netprofit_visitor': $date_from = date('Y-m-d', strtotime('-31 day')); $date_to = date('Y-m-d', strtotime('-1 day')); $total_visitors = AdminStatsController::getVisits(true, $date_from, $date_to); $net_profits = AdminStatsController::getTotalSales($date_from, $date_to); $net_profits -= AdminStatsController::getExpenses($date_from, $date_to); $net_profits -= AdminStatsController::getPurchases($date_from, $date_to); if ($total_visitors) { $value = Tools::displayPrice($net_profits / $total_visitors, $currency); } elseif ($net_profits) { $value = '∞'; } else { $value = Tools::displayPrice(0, $currency); } ConfigurationKPI::updateValue('NETPROFIT_VISITOR', $value); ConfigurationKPI::updateValue('NETPROFIT_VISITOR_EXPIRE', strtotime(date('Y-m-d 00:00:00', strtotime('+1 day')))); break; case 'products_per_category': $products = AdminStatsController::getTotalProducts(); $categories = AdminStatsController::getTotalCategories(); $value = round($products / $categories); ConfigurationKPI::updateValue('PRODUCTS_PER_CATEGORY', $value); ConfigurationKPI::updateValue('PRODUCTS_PER_CATEGORY_EXPIRE', strtotime('+1 hour')); break; case 'top_category': if (!($id_category = AdminStatsController::getBestCategory(date('Y-m-d', strtotime('-1 month')), date('Y-m-d')))) { $value = $this->l('No category', null, null, false); } else { $category = new Category($id_category, $this->context->language->id); $value = $category->name; } ConfigurationKPI::updateValue('TOP_CATEGORY', array($this->context->language->id => $value)); ConfigurationKPI::updateValue('TOP_CATEGORY_EXPIRE', array($this->context->language->id => strtotime('+1 day'))); break; default: $value = false; } if ($value !== false) { $array = array('value' => $value); if (isset($data)) { $array['data'] = $data; } die(Tools::jsonEncode($array)); } die(Tools::jsonEncode(array('has_errors' => true))); }