// Added net profits / visitor as a KPI
This commit is contained in:
@@ -33,12 +33,6 @@
|
||||
<span class="title">Conversion Rate <br/><small>30 days</small></span>
|
||||
<span class="value">1.89%</span>
|
||||
</div>
|
||||
|
||||
<div id="box-orders" class="col-lg-3 box-stats color4">
|
||||
<i class="icon-user"></i>
|
||||
<span class="title">Net Profit per visitor <br/><small>30 days</small></span>
|
||||
<span class="value">$2.12</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -1244,6 +1244,8 @@ class AdminOrdersControllerCore extends AdminController
|
||||
$time = time();
|
||||
$kpis = array();
|
||||
|
||||
/* The data generation is located in AdminStatsControllerCore */
|
||||
|
||||
$helper = new HelperKpi();
|
||||
$helper->id = 'box-carts';
|
||||
$helper->icon = 'icon-shopping-cart';
|
||||
@@ -1267,6 +1269,18 @@ class AdminOrdersControllerCore extends AdminController
|
||||
if (Configuration::get('PS_KPI_AVG_ORDER_VALUE_EXPIRE') < $time)
|
||||
$helper->source = $this->context->link->getAdminLink('AdminStats').'&ajax=1&action=getKpi&kpi=average_order_value';
|
||||
$kpis[] = $helper->generate();
|
||||
|
||||
$helper = new HelperKpi();
|
||||
$helper->id = 'box-net-profit-visitor';
|
||||
$helper->icon = 'icon-user';
|
||||
$helper->color = 'color4';
|
||||
$helper->title = $this->l('Net Profit per Visitor ');
|
||||
$helper->subtitle = $this->l('30 days');
|
||||
if (Configuration::get('PS_KPI_NETPROFIT_VISITOR') !== false)
|
||||
$helper->value = Configuration::get('PS_KPI_NETPROFIT_VISITOR');
|
||||
if (Configuration::get('PS_KPI_NETPROFIT_VISITOR_EXPIRE') < $time)
|
||||
$helper->source = $this->context->link->getAdminLink('AdminStats').'&ajax=1&action=getKpi&kpi=netprofit_visitor';
|
||||
$kpis[] = $helper->generate();
|
||||
|
||||
$helper = new HelperKpiRow();
|
||||
$helper->kpis = $kpis;
|
||||
|
||||
@@ -53,6 +53,63 @@ class AdminStatsControllerCore extends AdminStatsTabController
|
||||
Configuration::updateValue('PS_KPI_AVG_ORDER_VALUE', $value);
|
||||
Configuration::updateValue('PS_KPI_AVG_ORDER_VALUE_EXPIRE', strtotime(date('Y-m-d 00:00:00', strtotime('+1 day'))));
|
||||
break;
|
||||
case 'netprofit_visitor':
|
||||
|
||||
$gapi = Module::isInstalled('gapi') ? Module::getInstanceByName('gapi') : false;
|
||||
if (Validate::isLoadedObject($gapi) && $gapi->isConfigured())
|
||||
{
|
||||
$visitors = 0;
|
||||
if ($result = $gapi->requestReportData('', 'ga:visitors', date('Y-m-d', strtotime('-31 day')), date('Y-m-d', strtotime('-1 day')), null, null, 1, 1))
|
||||
$visitors = $result[0]['metrics']['visitors'];
|
||||
}
|
||||
else
|
||||
{
|
||||
$visitors = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
|
||||
SELECT COUNT(DISTINCT id_guest)
|
||||
FROM `'._DB_PREFIX_.'connections`
|
||||
WHERE `date_add` 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(false).'');
|
||||
}
|
||||
|
||||
$row_products = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
|
||||
SELECT
|
||||
SUM(od.`total_price_tax_excl` / `conversion_rate`) as total_product_price_tax_excl,
|
||||
SUM(od.`product_quantity` * od.`purchase_supplier_price` / `conversion_rate`) as total_purchase_price
|
||||
FROM `'._DB_PREFIX_.'orders` o
|
||||
LEFT JOIN `'._DB_PREFIX_.'order_detail` od ON o.id_order = od.id_order
|
||||
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(Shop::SHARE_ORDER, 'o'));
|
||||
extract($row_products);
|
||||
|
||||
$total_discounts_tax_excl = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
|
||||
SELECT SUM(`total_discounts_tax_excl` / `conversion_rate`) as total_discounts_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(Shop::SHARE_ORDER));
|
||||
|
||||
$total_credit_tax_excl = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
|
||||
SELECT SUM(os.`amount` / o.`conversion_rate`) as total_credit_tax_excl
|
||||
FROM `'._DB_PREFIX_.'orders` o
|
||||
LEFT JOIN `'._DB_PREFIX_.'order_slip` os ON o.id_order = os.id_order
|
||||
WHERE os.`date_add` 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(Shop::SHARE_ORDER, 'o'));
|
||||
|
||||
$net_profits = 0;
|
||||
$net_profits += $total_product_price_tax_excl;
|
||||
$net_profits -= $total_discounts_tax_excl;
|
||||
$net_profits -= $total_purchase_price;
|
||||
$net_profits -= $total_credit_tax_excl;
|
||||
|
||||
if ($visitors)
|
||||
$value = Tools::displayPrice($net_profits / $visitors, $currency);
|
||||
elseif ($net_profits)
|
||||
$value = '∞';
|
||||
else
|
||||
$value = Tools::displayPrice(0, $currency);
|
||||
|
||||
Configuration::updateValue('PS_KPI_NETPROFIT_VISITOR', $value);
|
||||
Configuration::updateValue('PS_KPI_NETPROFIT_VISITOR_EXPIRE', strtotime(date('Y-m-d 00:00:00', strtotime('+1 day'))));
|
||||
break;
|
||||
default:
|
||||
$value = false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user