* @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) */ if (!defined('_PS_VERSION_')) { exit; } class statsforecast extends Module { private $html = ''; private $t1 = 0; private $t2 = 0; private $t3 = 0; private $t4 = 0; private $t5 = 0; private $t6 = 0; private $t7 = 0; private $t8 = 0; public function __construct() { $this->name = 'statsforecast'; $this->tab = 'administration'; $this->version = '2.1.0'; $this->author = 'PrestaShop'; $this->need_instance = 0; parent::__construct(); $this->displayName = $this->trans('Stats Dashboard', [], 'Modules.Statsforecast.Admin'); $this->description = $this->trans('Enrich your stats, add a summary of all your current statistics on your back office.', [], 'Modules.Statsforecast.Admin'); $this->ps_versions_compliancy = ['min' => '1.7.6.0', 'max' => _PS_VERSION_]; } public function install() { return parent::install() && $this->registerHook('displayAdminStatsModules'); } public function getContent() { Tools::redirectAdmin($this->context->link->getAdminLink('AdminStats', true, [], ['module' => $this->name])); } public function hookDisplayAdminStatsModules() { $ru = $this->context->link->getAdminLink('AdminStats', true, [], ['module' => $this->name]); $db = Db::getInstance(); if (!isset($this->context->cookie->stats_granularity)) { $this->context->cookie->stats_granularity = 10; } if (Tools::isSubmit('submitIdZone')) { $this->context->cookie->stats_id_zone = (int) Tools::getValue('stats_id_zone'); } if (Tools::isSubmit('submitGranularity')) { $this->context->cookie->stats_granularity = Tools::getValue('stats_granularity'); } $currency = $this->context->currency; $employee = $this->context->employee; if (defined('_PS_CREATION_DATE_')) { $from = max( strtotime(_PS_CREATION_DATE_ . ' 00:00:00'), strtotime($employee->stats_date_from . ' 00:00:00') ); } else { $from = strtotime($employee->stats_date_from . ' 00:00:00'); } $to = strtotime($employee->stats_date_to . ' 23:59:59'); $to2 = min(time(), $to); $interval = ($to - $from) / 60 / 60 / 24; $interval2 = ($to2 - $from) / 60 / 60 / 24; $prop30 = $interval / $interval2; $interval_avg = 1; if ($this->context->cookie->stats_granularity == 7) { $interval_avg = $interval2 / 30; } if ($this->context->cookie->stats_granularity == 4) { $interval_avg = $interval2 / 365; } if ($this->context->cookie->stats_granularity == 10) { $interval_avg = $interval2; } if ($this->context->cookie->stats_granularity == 42) { $interval_avg = $interval2 / 7; } $data_table = []; if ($this->context->cookie->stats_granularity == 10) { for ($i = $from; $i <= $to2; $i = strtotime('+1 day', $i)) { $data_table[date('Y-m-d', $i)] = [ 'fix_date' => date('Y-m-d', $i), 'countOrders' => 0, 'countProducts' => 0, 'totalSales' => 0, ]; } } $date_from_gadd = ($this->context->cookie->stats_granularity != 42 ? 'LEFT(date_add, ' . (int) $this->context->cookie->stats_granularity . ')' : 'IFNULL(MAKEDATE(YEAR(date_add),DAYOFYEAR(date_add)-WEEKDAY(date_add)), CONCAT(YEAR(date_add),"-01-01*"))'); $date_from_ginvoice = ($this->context->cookie->stats_granularity != 42 ? 'LEFT(invoice_date, ' . (int) $this->context->cookie->stats_granularity . ')' : 'IFNULL(MAKEDATE(YEAR(invoice_date),DAYOFYEAR(invoice_date)-WEEKDAY(invoice_date)), CONCAT(YEAR(invoice_date),"-01-01*"))'); $result = $db->query(' SELECT ' . $date_from_ginvoice . ' as fix_date, COUNT(*) as countOrders, SUM((SELECT SUM(od.product_quantity) FROM ' . _DB_PREFIX_ . 'order_detail od WHERE o.id_order = od.id_order)) as countProducts, SUM(o.total_paid_tax_excl / o.conversion_rate) as totalSales FROM ' . _DB_PREFIX_ . 'orders o WHERE o.valid = 1 AND o.invoice_date BETWEEN ' . ModuleGraph::getDateBetween() . ' ' . Shop::addSqlRestriction(Shop::SHARE_ORDER, 'o') . ' GROUP BY ' . $date_from_ginvoice); while ($row = $db->nextRow($result)) { $data_table[$row['fix_date']] = $row; } $this->html .= '
| ' . $this->trans('Visits', [], 'Admin.Shopparameters.Feature') . ' | ' . $this->trans('Registrations', [], 'Admin.Shopparameters.Feature') . ' | ' . $this->trans('Placed orders', [], 'Modules.Statsforecast.Admin') . ' | ' . $this->trans('Bought items', [], 'Modules.Statsforecast.Admin') . ' | ' . $this->trans('Percentage of registrations', [], 'Modules.Statsforecast.Admin') . ' | ' . $this->trans('Percentage of orders', [], 'Modules.Statsforecast.Admin') . ' | ' . $this->trans('Revenue', [], 'Modules.Statsforecast.Admin') . ' | |
|---|---|---|---|---|---|---|---|
| ' . $row['fix_date'] . ' | ' . $visits_today . ' | ' . (int) $row['registrations'] . ' | ' . (int) $row['countOrders'] . ' | ' . (int) $row['countProducts'] . ' | ' . ($visits_today ? round(100 * (int) $row['registrations'] / $visits_today, 2) . ' %' : '-') . ' | ' . ($visits_today ? round(100 * (int) $row['countOrders'] / $visits_today, 2) . ' %' : '-') . ' | ' . $this->context->getCurrentLocale()->formatPrice($row['totalSales'], $currency->iso_code) . ' |
| ' . $this->trans('Visits', [], 'Admin.Shopparameters.Feature') . ' | ' . $this->trans('Registrations', [], 'Admin.Shopparameters.Feature') . ' | ' . $this->trans('Placed orders', [], 'Modules.Statsforecast.Admin') . ' | ' . $this->trans('Bought items', [], 'Modules.Statsforecast.Admin') . ' | ' . $this->trans('Percentage of registrations', [], 'Modules.Statsforecast.Admin') . ' | ' . $this->trans('Percentage of orders', [], 'Modules.Statsforecast.Admin') . ' | ' . $this->trans('Revenue', [], 'Modules.Statsforecast.Admin') . ' | |
| ' . $this->trans('Total', [], 'Admin.Global') . ' | ' . (int) $this->t1 . ' | ' . (int) $this->t2 . ' | ' . (int) $this->t3 . ' | ' . (int) $this->t4 . ' | -- | -- | ' . $this->context->getCurrentLocale()->formatPrice($this->t8, $currency->iso_code) . ' |
| ' . $this->trans('Average', [], 'Modules.Statsforecast.Admin') . ' | ' . (int) ($this->t1 / $interval_avg) . ' | ' . (int) ($this->t2 / $interval_avg) . ' | ' . (int) ($this->t3 / $interval_avg) . ' | ' . (int) ($this->t4 / $interval_avg) . ' | ' . ($this->t1 ? round(100 * $this->t2 / $this->t1, 2) . ' %' : '-') . ' | ' . ($this->t1 ? round(100 * $this->t3 / $this->t1, 2) . ' %' : '-') . ' | ' . $this->context->getCurrentLocale()->formatPrice($this->t8 / $interval_avg, $currency->iso_code) . ' |
| ' . $this->trans('Forecast', [], 'Modules.Statsforecast.Admin') . ' | ' . (int) ($this->t1 * $prop30) . ' | ' . (int) ($this->t2 * $prop30) . ' | ' . (int) ($this->t3 * $prop30) . ' | ' . (int) ($this->t4 * $prop30) . ' | -- | -- | ' . $this->context->getCurrentLocale()->formatPrice($this->t8 * $prop30, $currency->iso_code) . ' |
|
' . $this->trans('Visitors', [], 'Admin.Shopparameters.Feature') . ' ' . $visitors . ' |
' . round(100 * $customers / max(1, $visitors), 2) . ' % |
' . $this->trans('Accounts', [], 'Modules.Statsforecast.Admin') . ' ' . $customers . ' |
' . round(100 * $fullcarts / max(1, $customers), 2) . ' % |
' . $this->trans('Full carts', [], 'Modules.Statsforecast.Admin') . ' ' . $fullcarts . ' |
' . round(100 * $orders / max(1, $fullcarts), 2) . ' % |
' . $this->trans('Orders', [], 'Admin.Global') . ' ' . $orders . ' |
' . $this->trans('Registered visitors', [], 'Modules.Statsforecast.Admin') . ' |
' . round(100 * $orders / max(1, $customers), 2) . ' % |
' . $this->trans('Orders', [], 'Admin.Global') . ' |
' . $this->trans('Visitors', [], 'Admin.Shopparameters.Feature') . ' |
' . round(100 * $orders / max(1, $visitors), 2) . ' % |
' . $this->trans('Orders', [], 'Admin.Global') . ' |
||||
|
' . round(100 * $carts / max(1, $visitors)) . ' % |
' . $this->trans('Carts', [], 'Admin.Global') . ' ' . $carts . ' |
' . round(100 * $fullcarts / max(1, $carts)) . ' % |
' . $this->trans('A simple statistical calculation lets you know the monetary value of your visitors:', [], 'Modules.Statsforecast.Admin') . '
' . $this->trans('On average, each visitor places an order for this amount:', [], 'Modules.Statsforecast.Admin') . ' ' . $this->context->getCurrentLocale()->formatPrice($ca['ventil']['total'] / max(1, $visitors), $currency->iso_code) . '.
' . $this->trans('On average, each registered visitor places an order for this amount:', [], 'Modules.Statsforecast.Admin') . ' ' . $this->context->getCurrentLocale()->formatPrice($ca['ventil']['total'] / max(1, $customers), $currency->iso_code) . '.
| ' . $this->trans('Module', [], 'Modules.Statsforecast.Admin') . ' | ' . $this->trans('Orders', [], 'Admin.Global') . ' | ' . $this->trans('Sales', [], 'Admin.Global') . ' | ' . $this->trans('Average cart value', [], 'Modules.Statsforecast.Admin') . ' |
|---|---|---|---|
| ' . $payment['payment_method'] . ' | ' . (int) $payment['nb'] . ' | ' . $this->context->getCurrentLocale()->formatPrice($payment['total'], $currency->iso_code) . ' | ' . $this->context->getCurrentLocale()->formatPrice($payment['total'] / (int) $payment['nb'], $currency->iso_code) . ' |
| ' . $this->trans('Category', [], 'Admin.Catalog.Feature') . ' | ' . $this->trans('Products sold', [], 'Modules.Statsforecast.Admin') . ' | ' . $this->trans('Sales', [], 'Admin.Global') . ' | ' . $this->trans('Percentage of products sold', [], 'Modules.Statsforecast.Admin') . ' | ' . $this->trans('Percentage of sales', [], 'Modules.Statsforecast.Admin') . ' | ' . $this->trans('Average price', [], 'Admin.Global') . ' |
|---|---|---|---|---|---|
| ' . (empty($catrow['name']) ? $this->trans('Unknown', [], 'Admin.Shopparameters.Feature') : $catrow['name']) . ' | ' . $catrow['orderQty'] . ' | ' . $this->context->getCurrentLocale()->formatPrice($catrow['orderSum'], $currency->iso_code) . ' | ' . number_format((100 * $catrow['orderQty'] / $this->t4), 1, '.', ' ') . '% | ' . ((int) $ca['ventil']['total'] ? number_format((100 * $catrow['orderSum'] / $ca['ventil']['total']), 1, '.', ' ') : '0') . '% | ' . $this->context->getCurrentLocale()->formatPrice($catrow['priveAvg'], $currency->iso_code) . ' |
| ' . $this->trans('Language', [], 'Admin.Global') . ' | ' . $this->trans('Sales', [], 'Admin.Global') . ' | ' . $this->trans('Percentage', [], 'Admin.Global') . ' | ' . $this->trans('Growth', [], 'Modules.Statsforecast.Admin') . ' | |
|---|---|---|---|---|
| ' . $ophone . ' | ' . $this->context->getCurrentLocale()->formatPrice((int) $amount, $currency->iso_code) . ' | ' . ((int) $ca['ventil']['total'] ? number_format((100 * (int) $amount / $ca['ventil']['total']), 1, '.', ' ') . '%' : '-') . ' | ' . (($percent > 0 || $percent == '∞') ? ' ' : ' ') . ' |
' . (($percent > 0 || $percent == '∞') ? '+' : '') . $percent . '% |
| ' . $this->trans('Zone', [], 'Admin.Global') . ' | ' . $this->trans('Orders', [], 'Admin.Global') . ' | ' . $this->trans('Sales', [], 'Admin.Global') . ' | ' . $this->trans('Percentage of orders', [], 'Modules.Statsforecast.Admin') . ' | ' . $this->trans('Percentage of sales', [], 'Modules.Statsforecast.Admin') . ' |
|---|---|---|---|---|
| ' . (isset($zone['name']) ? $zone['name'] : $this->trans('Undefined', [], 'Admin.Shopparameters.Feature')) . ' | ' . (int) ($zone['nb']) . ' | ' . $this->context->getCurrentLocale()->formatPrice($zone['total'], $currency->iso_code) . ' | ' . ($ca['ventil']['nb'] ? number_format((100 * $zone['nb'] / $ca['ventil']['nb']), 1, '.', ' ') : '0') . '% | ' . ((int) $ca['ventil']['total'] ? number_format((100 * $zone['total'] / $ca['ventil']['total']), 1, '.', ' ') : '0') . '% |
| ' . $this->trans('Currency', [], 'Admin.Global') . ' | ' . $this->trans('Orders', [], 'Admin.Global') . ' | ' . $this->trans('Sales (converted)', [], 'Modules.Statsforecast.Admin') . ' | ' . $this->trans('Percentage of orders', [], 'Modules.Statsforecast.Admin') . ' | ' . $this->trans('Percentage of sales', [], 'Modules.Statsforecast.Admin') . ' |
|---|---|---|---|---|
| ' . $currency_row['name'] . ' | ' . (int) ($currency_row['nb']) . ' | ' . $this->context->getCurrentLocale()->formatPrice($currency_row['total'], $currency->iso_code) . ' | ' . ($ca['ventil']['nb'] ? number_format((100 * $currency_row['nb'] / $ca['ventil']['nb']), 1, '.', ' ') : '0') . '% | ' . ((int) $ca['ventil']['total'] ? number_format((100 * $currency_row['total'] / $ca['ventil']['total']), 1, '.', ' ') : '0') . '% |
| ' . $this->trans('Group', [], 'Admin.Global') . ' | ' . $this->trans('Attribute', [], 'Admin.Global') . ' | ' . $this->trans('Quantity of products sold', [], 'Modules.Statsforecast.Admin') . ' |
|---|---|---|
| ' . $attribut['gname'] . ' | ' . $attribut['aname'] . ' | ' . (int) ($attribut['total']) . ' |