// Added visits to chart
This commit is contained in:
@@ -77,23 +77,9 @@ class Dashtrends extends Module
|
||||
|
||||
public function hookDashboardData($params)
|
||||
{
|
||||
$gapi = Module::isInstalled('gapi') ? Module::getInstanceByName('gapi') : false;
|
||||
if (Validate::isLoadedObject($gapi) && $gapi->isConfigured())
|
||||
{
|
||||
$visits_score = 0;
|
||||
if ($result = $gapi->requestReportData('', 'ga:visits', $params['date_from'], $params['date_to'], null, null, 1, 1))
|
||||
$visits_score = $result[0]['metrics']['visits'];
|
||||
}
|
||||
else
|
||||
{
|
||||
$visits_score = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
|
||||
SELECT COUNT(`id_connections`)
|
||||
FROM `'._DB_PREFIX_.'connections`
|
||||
WHERE `date_add` BETWEEN "'.pSQL($params['date_from']).'" AND "'.pSQL($params['date_to']).'"
|
||||
'.Shop::addSqlRestriction(false));
|
||||
}
|
||||
|
||||
$data = array(
|
||||
// We need the following figures to calculate our stats
|
||||
$tmp_data = array(
|
||||
'visits_score' => array(),
|
||||
'orders_score' => array(),
|
||||
'total_paid_tax_excl' => array(),
|
||||
'total_discounts_tax_excl' => array(),
|
||||
@@ -103,6 +89,29 @@ class Dashtrends extends Module
|
||||
'total_purchase_price' => array()
|
||||
);
|
||||
|
||||
// The visits are retrieved from Analytics if available
|
||||
$gapi = Module::isInstalled('gapi') ? Module::getInstanceByName('gapi') : false;
|
||||
if (Validate::isLoadedObject($gapi) && $gapi->isConfigured())
|
||||
{
|
||||
if ($result = $gapi->requestReportData('ga:date', 'ga:visits', $params['date_from'], $params['date_to'], null, null, 1, 1))
|
||||
foreach ($result as $row)
|
||||
$tmp_data['visits_score'][strtotime(preg_replace('/^([0-9]{4})([0-9]{2})([0-9]{2})$/', '$1-$2-$3', $row['dimensions']['date']))] = $row['metrics']['visits'];
|
||||
}
|
||||
else
|
||||
{
|
||||
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
|
||||
SELECT
|
||||
LEFT(`date_add`, 10) as date,
|
||||
COUNT(`id_connections`) as visits_score
|
||||
FROM `'._DB_PREFIX_.'connections`
|
||||
WHERE `date_add` BETWEEN "'.pSQL($params['date_from']).'" AND "'.pSQL($params['date_to']).'"
|
||||
'.Shop::addSqlRestriction(false).'
|
||||
GROUP BY LEFT(`date_add`, 10)');
|
||||
foreach ($result as $row)
|
||||
$tmp_data['visits_score'][strtotime($row['date'])] = $row['visits_score'];
|
||||
}
|
||||
|
||||
// First we retrieve the
|
||||
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
|
||||
SELECT
|
||||
LEFT(`invoice_date`, 10) as date,
|
||||
@@ -115,7 +124,7 @@ class Dashtrends extends Module
|
||||
GROUP BY LEFT(`invoice_date`, 10)');
|
||||
foreach ($result as $row)
|
||||
foreach (array('orders_score', 'total_paid_tax_excl', 'total_discounts_tax_excl') as $var)
|
||||
$data[$var][strtotime($row['date'])] = $row[$var];
|
||||
$tmp_data[$var][strtotime($row['date'])] = $row[$var];
|
||||
|
||||
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
|
||||
SELECT
|
||||
@@ -129,7 +138,7 @@ class Dashtrends extends Module
|
||||
GROUP BY LEFT(os.`date_add`, 10)');
|
||||
foreach ($result as $row)
|
||||
foreach (array('total_credit_tax_excl', 'total_credit_shipping_tax_excl') as $var)
|
||||
$data[$var][strtotime($row['date'])] = $row[$var];
|
||||
$tmp_data[$var][strtotime($row['date'])] = $row[$var];
|
||||
|
||||
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
|
||||
SELECT
|
||||
@@ -143,42 +152,42 @@ class Dashtrends extends Module
|
||||
GROUP BY LEFT(`invoice_date`, 10)');
|
||||
foreach ($result as $row)
|
||||
foreach (array('total_product_price_tax_excl', 'total_purchase_price') as $var)
|
||||
$data[$var][strtotime($row['date'])] = $row[$var];
|
||||
$tmp_data[$var][strtotime($row['date'])] = $row[$var];
|
||||
|
||||
$from = max(strtotime(_PS_CREATION_DATE_.' 00:00:00'), strtotime($params['date_from'].' 00:00:00'));
|
||||
$to = min(time(), strtotime($params['date_to'].' 23:59:59'));
|
||||
for ($date = $from; $date <= $to; $date = strtotime('+1 day', $date))
|
||||
{
|
||||
$this->dashboard_data['sales'][$date] = 0;
|
||||
if (isset($data['total_paid_tax_excl'][$date]))
|
||||
$this->dashboard_data['sales'][$date] += $data['total_paid_tax_excl'][$date];
|
||||
if (isset($data['total_credit_tax_excl'][$date]))
|
||||
$this->dashboard_data['sales'][$date] -= $data['total_credit_tax_excl'][$date];
|
||||
if (isset($data['total_credit_shipping_tax_excl'][$date]))
|
||||
$this->dashboard_data['sales'][$date] -= $data['total_credit_shipping_tax_excl'][$date];
|
||||
if (isset($tmp_data['total_paid_tax_excl'][$date]))
|
||||
$this->dashboard_data['sales'][$date] += $tmp_data['total_paid_tax_excl'][$date];
|
||||
if (isset($tmp_data['total_credit_tax_excl'][$date]))
|
||||
$this->dashboard_data['sales'][$date] -= $tmp_data['total_credit_tax_excl'][$date];
|
||||
if (isset($tmp_data['total_credit_shipping_tax_excl'][$date]))
|
||||
$this->dashboard_data['sales'][$date] -= $tmp_data['total_credit_shipping_tax_excl'][$date];
|
||||
|
||||
$this->dashboard_data['orders'][$date] = isset($data['orders_score'][$date]) ? $data['orders_score'][$date] : 0;
|
||||
$this->dashboard_data['orders'][$date] = isset($tmp_data['orders_score'][$date]) ? $tmp_data['orders_score'][$date] : 0;
|
||||
|
||||
$this->dashboard_data['average_cart_value'][$date] = $this->dashboard_data['orders'][$date] ? $this->dashboard_data['sales'][$date] / $this->dashboard_data['orders'][$date] : 0;
|
||||
|
||||
$this->dashboard_data['visits'][$date] = 0;
|
||||
$this->dashboard_data['visits'][$date] = isset($tmp_data['visits_score'][$date]) ? $tmp_data['visits_score'][$date] : 0;
|
||||
|
||||
$this->dashboard_data['conversion_rate'][$date] = $this->dashboard_data['visits'][$date] ? $this->dashboard_data['orders'][$date] / $this->dashboard_data['visits'][$date] : 0;
|
||||
|
||||
$this->dashboard_data['net_profits'][$date] = 0;
|
||||
if (isset($data['total_product_price_tax_excl'][$date]))
|
||||
$this->dashboard_data['net_profits'][$date] += $data['total_product_price_tax_excl'][$date];
|
||||
if (isset($data['total_discounts_tax_excl'][$date]))
|
||||
$this->dashboard_data['net_profits'][$date] -= $data['total_discounts_tax_excl'][$date];
|
||||
if (isset($data['total_purchase_price'][$date]))
|
||||
$this->dashboard_data['net_profits'][$date] -= $data['total_purchase_price'][$date];
|
||||
if (isset($data['total_credit_tax_excl'][$date]))
|
||||
$this->dashboard_data['net_profits'][$date] -= $data['total_credit_tax_excl'][$date];
|
||||
if (isset($tmp_data['total_product_price_tax_excl'][$date]))
|
||||
$this->dashboard_data['net_profits'][$date] += $tmp_data['total_product_price_tax_excl'][$date];
|
||||
if (isset($tmp_data['total_discounts_tax_excl'][$date]))
|
||||
$this->dashboard_data['net_profits'][$date] -= $tmp_data['total_discounts_tax_excl'][$date];
|
||||
if (isset($tmp_data['total_purchase_price'][$date]))
|
||||
$this->dashboard_data['net_profits'][$date] -= $tmp_data['total_purchase_price'][$date];
|
||||
if (isset($tmp_data['total_credit_tax_excl'][$date]))
|
||||
$this->dashboard_data['net_profits'][$date] -= $tmp_data['total_credit_tax_excl'][$date];
|
||||
}
|
||||
$this->dashboard_data_sum['sales'] = array_sum($this->dashboard_data['sales']);
|
||||
$this->dashboard_data_sum['orders'] = array_sum($this->dashboard_data['orders']);
|
||||
$this->dashboard_data_sum['average_cart_value'] = $this->dashboard_data_sum['sales'] ? $this->dashboard_data_sum['sales'] / $this->dashboard_data_sum['orders'] : 0;
|
||||
$this->dashboard_data_sum['visits'] = 0;
|
||||
$this->dashboard_data_sum['visits'] = array_sum($this->dashboard_data['visits']);
|
||||
$this->dashboard_data_sum['conversion_rate'] = $this->dashboard_data_sum['visits'] ? $this->dashboard_data_sum['orders'] / $this->dashboard_data_sum['visits'] : 0;
|
||||
$this->dashboard_data_sum['net_profits'] = array_sum($this->dashboard_data['net_profits']);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user