// Added net profits / visitor as a KPI
This commit is contained in:
@@ -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