// Added expense management to profitability KPI

This commit is contained in:
Damien Metzger committed 2013-10-09 10:55:12 +02:00
1 parent e16676efaa
commit 623b8c7960
5 files changed
+58 -26

No files matched your search

@@ -22,7 +22,7 @@
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA * International Registered Trademark & Property of PrestaShop SA
*} *}
<div id="{$id|escape}" class="col-lg-3 box-stats {$color|escape}" > <{if $href}a style="display:block" href="{$href|escape}"{else}div{/if} id="{$id|escape}" class="col-lg-3 box-stats {$color|escape}" >
{if $icon}<i class="{$icon|escape}"></i>{/if} {if $icon}<i class="{$icon|escape}"></i>{/if}
{if $chart} {if $chart}
<div class="boxchart-overlay"> <div class="boxchart-overlay">
@@ -32,7 +32,7 @@
{/if} {/if}
<span class="title">{$title|escape}<br /><small>{$subtitle|escape}</small></span> <span class="title">{$title|escape}<br /><small>{$subtitle|escape}</small></span>
<span class="value">{$value|escape}</span> <span class="value">{$value|escape}</span>
</div> </{if $href}a{else}div{/if}>
{if $source != ''} {if $source != ''}
<script> <script>
+3 -1
View File
@@ -38,6 +38,7 @@ class HelperKpiCore extends Helper
public $value; public $value;
public $data; public $data;
public $source; public $source;
public $href;
public function generate() public function generate()
{ {
@@ -52,7 +53,8 @@ class HelperKpiCore extends Helper
'subtitle' => $this->subtitle, 'subtitle' => $this->subtitle,
'value' => $this->value, 'value' => $this->value,
'data' => $this->data, 'data' => $this->data,
'source' => $this->source 'source' => $this->source,
'href' => $this->href
)); ));
return $this->tpl->fetch(); return $this->tpl->fetch();
} }
@@ -33,7 +33,7 @@ class AdminDashboardControllerCore extends AdminController
parent::__construct(); parent::__construct();
if (Tools::isSubmit('profitability_conf') || Tools::isSubmit('submitOptions')) if (Tools::isSubmit('profitability_conf') || Tools::isSubmit('submitOptionsconfiguration'))
$this->fields_options = $this->getOptionFields(); $this->fields_options = $this->getOptionFields();
} }
+3 -1
View File
@@ -1253,7 +1253,8 @@ class AdminOrdersControllerCore extends AdminController
$helper = new HelperKpi(); $helper = new HelperKpi();
$helper->id = 'box-conversion-rate'; $helper->id = 'box-conversion-rate';
$helper->chart = true; $helper->icon = 'icon-sort-by-attributes-alt';
//$helper->chart = true;
$helper->color = 'color1'; $helper->color = 'color1';
$helper->title = $this->l('Conversion Rate'); $helper->title = $this->l('Conversion Rate');
$helper->subtitle = $this->l('30 days'); $helper->subtitle = $this->l('30 days');
@@ -1271,6 +1272,7 @@ class AdminOrdersControllerCore extends AdminController
$helper->color = 'color2'; $helper->color = 'color2';
$helper->title = $this->l('Abandoned Carts'); $helper->title = $this->l('Abandoned Carts');
$helper->subtitle = $this->l('Today'); $helper->subtitle = $this->l('Today');
$helper->href = $this->context->link->getAdminLink('AdminCarts');
if (ConfigurationKPI::get('ABANDONED_CARTS') !== false) if (ConfigurationKPI::get('ABANDONED_CARTS') !== false)
$helper->value = ConfigurationKPI::get('ABANDONED_CARTS'); $helper->value = ConfigurationKPI::get('ABANDONED_CARTS');
if (ConfigurationKPI::get('ABANDONED_CARTS_EXPIRE') < $time) if (ConfigurationKPI::get('ABANDONED_CARTS_EXPIRE') < $time)
+49 -21
View File
@@ -62,7 +62,7 @@ class AdminStatsControllerCore extends AdminStatsTabController
return $visitors; return $visitors;
} }
public static function getAbandonedCarts($date_from, $date_to, $granularity = false) public static function getAbandonedCarts($date_from, $date_to)
{ {
return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
SELECT COUNT(DISTINCT id_guest) SELECT COUNT(DISTINCT id_guest)
@@ -319,6 +319,33 @@ class AdminStatsControllerCore extends AdminStatsTabController
return 0; return 0;
return round($messages / $threads, 1); return round($messages / $threads, 1);
} }
public static function getExpenses($date_from, $date_to)
{
$secs_per_year = 365.25 * 86400;
$secs_per_month = 30.4375 * 86400;
$total_secs = (strtotime($date_to) - min(strtotime($date_from), time())) / 86400;
$expenses = Configuration::get('CONF_MONTHLY_FEES') * $total_secs / $secs_per_month + Configuration::get('CONF_YEARLY_FEES') * $total_secs / $secs_per_year;
$orders = Db::getInstance()->ExecuteS('
SELECT
total_paid_tax_incl / o.conversion_rate as total_paid_tax_incl,
total_shipping_tax_excl / o.conversion_rate as total_shipping_tax_excl,
module
FROM `'._DB_PREFIX_.'orders` o
WHERE `invoice_date` BETWEEN "'.pSQL($date_from).' 00:00:00" AND "'.pSQL($date_to).' 23:59:59"
'.Shop::addSqlRestriction(Shop::SHARE_ORDER, 'o'));
foreach ($orders as $order)
{
// Add flat fees for this order
$expenses += Configuration::get('CONF_ORDER_FIXED_FEES') + Configuration::get('CONF_'.strtoupper($order['module']).'_FIXED_FEE');
// Add variable fees for this order
$expenses += $order['total_paid_tax_incl'] * (Configuration::get('CONF_ORDER_VAR_FEES') + Configuration::get('CONF_'.strtoupper($order['module']).'_VAR_FEE')) / 100;
// Add shipping fees for this order
$expenses += $order['total_shipping_tax_excl'] * (100 - Configuration::get('CONF_SHIPPING_MARGIN')) / 100;
}
return $expenses;
}
public function displayAjaxGetKpi() public function displayAjaxGetKpi()
{ {
@@ -326,20 +353,20 @@ class AdminStatsControllerCore extends AdminStatsTabController
switch (Tools::getValue('kpi')) switch (Tools::getValue('kpi'))
{ {
case 'conversion_rate': case 'conversion_rate':
$visitors = AdminStatsController::getUniqueVisitors(date('Y-m-d', strtotime('-31 day')), date('Y-m-d', strtotime('-1 day')), 'day'); $visitors = AdminStatsController::getUniqueVisitors(date('Y-m-d', strtotime('-31 day')), date('Y-m-d', strtotime('-1 day')), false /*'day'*/);
$orders = AdminStatsController::getOrders(date('Y-m-d', strtotime('-31 day')), date('Y-m-d', strtotime('-1 day')), 'day'); $orders = AdminStatsController::getOrders(date('Y-m-d', strtotime('-31 day')), date('Y-m-d', strtotime('-1 day')), false /*'day'*/);
$data = array(); // $data = array();
$from = strtotime(date('Y-m-d 00:00:00', strtotime('-31 day'))); // $from = strtotime(date('Y-m-d 00:00:00', strtotime('-31 day')));
$to = strtotime(date('Y-m-d 23:59:59', strtotime('-1 day'))); // $to = strtotime(date('Y-m-d 23:59:59', strtotime('-1 day')));
for ($date = $from; $date <= $to; $date = strtotime('+1 day', $date)) // for ($date = $from; $date <= $to; $date = strtotime('+1 day', $date))
if (isset($visitors[$date]) && $visitors[$date]) // if (isset($visitors[$date]) && $visitors[$date])
$data[$date] = round(100 * ((isset($orders[$date]) && $orders[$date]) ? $orders[$date] : 0) / $visitors[$date], 2); // $data[$date] = round(100 * ((isset($orders[$date]) && $orders[$date]) ? $orders[$date] : 0) / $visitors[$date], 2);
else // else
$data[$date] = 0; // $data[$date] = 0;
$visits_sum = array_sum($visitors); $visits_sum = $visitors; //array_sum($visitors);
$orders_sum = array_sum($orders); $orders_sum = $orders; //array_sum($orders);
if ($visits_sum) if ($visits_sum)
$value = round(100 * $orders_sum / $visits_sum, 2); $value = round(100 * $orders_sum / $visits_sum, 2);
elseif ($orders_sum) elseif ($orders_sum)
@@ -348,7 +375,7 @@ class AdminStatsControllerCore extends AdminStatsTabController
$value = 0; $value = 0;
$value .= '%'; $value .= '%';
ConfigurationKPI::updateValue('CONVERSION_RATE_CHART', Tools::jsonEncode($data)); // ConfigurationKPI::updateValue('CONVERSION_RATE_CHART', Tools::jsonEncode($data));
ConfigurationKPI::updateValue('CONVERSION_RATE', $value); ConfigurationKPI::updateValue('CONVERSION_RATE', $value);
ConfigurationKPI::updateValue('CONVERSION_RATE_EXPIRE', strtotime(date('Y-m-d 00:00:00', strtotime('+1 day')))); ConfigurationKPI::updateValue('CONVERSION_RATE_EXPIRE', strtotime(date('Y-m-d 00:00:00', strtotime('+1 day'))));
break; break;
@@ -356,7 +383,7 @@ class AdminStatsControllerCore extends AdminStatsTabController
case 'abandoned_cart': 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'))); $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', $value);
ConfigurationKPI::updateValue('ABANDONED_CARTS_EXPIRE', strtotime('+10 min')); ConfigurationKPI::updateValue('ABANDONED_CARTS_EXPIRE', strtotime('+1 hour'));
break; break;
case 'installed_modules': case 'installed_modules':
@@ -547,9 +574,10 @@ class AdminStatsControllerCore extends AdminStatsTabController
case 'netprofit_visitor': case 'netprofit_visitor':
$date_from = date('Y-m-d', strtotime('-31 day')); $date_from = date('Y-m-d', strtotime('-31 day'));
$date_to = date('Y-m-d', strtotime('-1 day')); $date_to = date('Y-m-d', strtotime('-1 day'));
$visitors = AdminStatsController::getUniqueVisitors($date_from, $date_to); $total_visitors = AdminStatsController::getUniqueVisitors($date_from, $date_to);
$total_sales = AdminStatsController::getTotalSales($date_from, $date_to); $total_sales = AdminStatsController::getTotalSales($date_from, $date_to);
$total_purchase = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' $total_expenses = AdminStatsController::getExpenses($date_from, $date_to);
$total_purchases = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
SELECT SUM(od.`product_quantity` * od.`purchase_supplier_price` / `conversion_rate`) as total_purchase_price SELECT SUM(od.`product_quantity` * od.`purchase_supplier_price` / `conversion_rate`) as total_purchase_price
FROM `'._DB_PREFIX_.'orders` o FROM `'._DB_PREFIX_.'orders` o
LEFT JOIN `'._DB_PREFIX_.'order_detail` od ON o.id_order = od.id_order LEFT JOIN `'._DB_PREFIX_.'order_detail` od ON o.id_order = od.id_order
@@ -557,11 +585,11 @@ class AdminStatsControllerCore extends AdminStatsTabController
'.Shop::addSqlRestriction(Shop::SHARE_ORDER, 'o')); '.Shop::addSqlRestriction(Shop::SHARE_ORDER, 'o'));
$net_profits = $total_sales; $net_profits = $total_sales;
$net_profits -= $total_purchase; $net_profits -= $total_purchases;
// Todo : Add profitability calculation $net_profits -= $total_expenses;
if ($visitors) if ($total_visitors)
$value = Tools::displayPrice($net_profits / $visitors, $currency); $value = Tools::displayPrice($net_profits / $total_visitors, $currency);
elseif ($net_profits) elseif ($net_profits)
$value = '&infin;'; $value = '&infin;';
else else