name = 'statssales';
$this->tab = 'Stats';
$this->version = 1.0;
parent::__construct();
$this->displayName = $this->l('Sales and orders');
$this->description = $this->l('Display the sales evolution and orders by statuses');
}
public function install()
{
return (parent::install() AND $this->registerHook('AdminStatsModules'));
}
public function hookAdminStatsModules($params)
{
global $cookie;
$totals = $this->getTotals();
$currency = new Currency(Configuration::get('PS_CURRENCY_DEFAULT'));
$this->_html = '
';
return $this->_html;
}
private function getTotals()
{
$result0 = Db::getInstance()->getRow('
SELECT COUNT(o.`id_order`) as allOrderCount, SUM(o.`total_paid_real` / c.conversion_rate) as orderSum
FROM `'._DB_PREFIX_.'orders` o
LEFT JOIN `'._DB_PREFIX_.'currency` c ON o.id_currency = c.id_currency
'.(intval(Tools::getValue('id_country')) ? 'LEFT JOIN `'._DB_PREFIX_.'address` a ON o.id_address_delivery = a.id_address' : '').'
WHERE o.`invoice_date` BETWEEN '.ModuleGraph::getDateBetween().'
'.(intval(Tools::getValue('id_country')) ? 'AND a.id_country = '.intval(Tools::getValue('id_country')) : ''));
$result1 = Db::getInstance()->getRow('
SELECT COUNT(o.`id_order`) as orderCount, SUM(o.`total_paid_real` / c.conversion_rate) as orderSum
FROM `'._DB_PREFIX_.'orders` o
LEFT JOIN `'._DB_PREFIX_.'currency` c ON o.id_currency = c.id_currency
'.(intval(Tools::getValue('id_country')) ? 'LEFT JOIN `'._DB_PREFIX_.'address` a ON o.id_address_delivery = a.id_address' : '').'
WHERE o.valid = 1
'.(intval(Tools::getValue('id_country')) ? 'AND a.id_country = '.intval(Tools::getValue('id_country')) : '').'
AND o.`invoice_date` BETWEEN '.ModuleGraph::getDateBetween());
$result2 = Db::getInstance()->getRow('
SELECT SUM(od.product_quantity) as products
FROM `'._DB_PREFIX_.'orders` o
LEFT JOIN `'._DB_PREFIX_.'order_detail` od ON od.`id_order` = o.`id_order`
'.(intval(Tools::getValue('id_country')) ? 'LEFT JOIN `'._DB_PREFIX_.'address` a ON o.id_address_delivery = a.id_address' : '').'
WHERE o.valid = 1
'.(intval(Tools::getValue('id_country')) ? 'AND a.id_country = '.intval(Tools::getValue('id_country')) : '').'
AND o.`invoice_date` BETWEEN '.ModuleGraph::getDateBetween());
return array_merge(array_merge($result0, $result1), $result2);
}
public function setOption($options, $layers = 1)
{
list($this->_option, $this->id_country) = explode('-', $options);
switch ($this->_option)
{
case 1:
$this->_titles['main'][0] = $this->l('Number of orders and products ordered');
$this->_titles['main'][1] = $this->l('Orders');
$this->_titles['main'][2] = $this->l('Orders (valid)');
$this->_titles['main'][3] = $this->l('Products (valid)');
break;
case 2:
$currency = new Currency(Configuration::get('PS_CURRENCY_DEFAULT'));
$this->_titles['main'] = $this->l('Sales in').' '.$currency->iso_code;
break;
case 3:
$this->_titles['main'] = $this->l('Percentage of orders by status');
break;
}
}
protected function getData($layers)
{
if ($this->_option == 3)
return $this->getStatesData();
$this->_query0 = '
SELECT o.`invoice_date`, o.`total_paid_real` / c.conversion_rate AS total_paid_real, SUM(od.product_quantity) as product_quantity
FROM `'._DB_PREFIX_.'orders` o
LEFT JOIN `'._DB_PREFIX_.'currency` c ON o.id_currency = c.id_currency
LEFT JOIN `'._DB_PREFIX_.'order_detail` od ON od.`id_order` = o.`id_order`
'.(intval($this->id_country) ? 'LEFT JOIN `'._DB_PREFIX_.'address` a ON o.id_address_delivery = a.id_address' : '').'
'.(intval($this->id_country) ? 'WHERE a.id_country = '.intval($this->id_country).' AND ' : 'WHERE ').'
o.`invoice_date` BETWEEN ';
$this->_query = '
SELECT o.`invoice_date`, o.`total_paid_real` / c.conversion_rate AS total_paid_real, SUM(od.product_quantity) as product_quantity
FROM `'._DB_PREFIX_.'orders` o
LEFT JOIN `'._DB_PREFIX_.'currency` c ON o.id_currency = c.id_currency
LEFT JOIN `'._DB_PREFIX_.'order_detail` od ON od.`id_order` = o.`id_order`
'.(intval($this->id_country) ? 'LEFT JOIN `'._DB_PREFIX_.'address` a ON o.id_address_delivery = a.id_address' : '').'
WHERE o.valid = 1
'.(intval($this->id_country) ? 'AND a.id_country = '.intval($this->id_country) : '').'
AND o.`invoice_date` BETWEEN ';
$this->_query2 = ' GROUP BY o.id_order';
$this->setDateGraph($layers, true);
}
protected function setYearValues($layers)
{
$result = Db::getInstance()->ExecuteS($this->_query.$this->getDate().$this->_query2);
foreach ($result AS $row)
if ($this->_option == 1)
{
$this->_values[1][intval(substr($row['invoice_date'], 5, 2))] += 1;
$this->_values[2][intval(substr($row['invoice_date'], 5, 2))] += $row['product_quantity'];
}
else
$this->_values[intval(substr($row['invoice_date'], 5, 2))] += $row['total_paid_real'];
if ($this->_option == 1)
{
$result = Db::getInstance()->ExecuteS($this->_query0.$this->getDate().$this->_query2);
foreach ($result AS $row)
$this->_values[0][intval(substr($row['invoice_date'], 5, 2))] += 1;
}
}
protected function setMonthValues($layers)
{
$result = Db::getInstance()->ExecuteS($this->_query.$this->getDate().$this->_query2);
foreach ($result AS $row)
if ($this->_option == 1)
{
$this->_values[1][intval(substr($row['invoice_date'], 8, 2))] += 1;
$this->_values[2][intval(substr($row['invoice_date'], 8, 2))] += $row['product_quantity'];
}
else
$this->_values[intval(substr($row['invoice_date'], 8, 2))] += $row['total_paid_real'];
if ($this->_option == 1)
{
$result = Db::getInstance()->ExecuteS($this->_query0.$this->getDate().$this->_query2);
foreach ($result AS $row)
$this->_values[0][intval(substr($row['invoice_date'], 8, 2))] += 1;
}
}
protected function setDayValues($layers)
{
$result = Db::getInstance()->ExecuteS($this->_query.$this->getDate().$this->_query2);
foreach ($result AS $row)
if ($this->_option == 1)
{
$this->_values[1][intval(substr($row['invoice_date'], 11, 2))] += 1;
$this->_values[2][intval(substr($row['invoice_date'], 11, 2))] += $row['product_quantity'];
}
else
$this->_values[intval(substr($row['invoice_date'], 11, 2))] += $row['total_paid_real'];
if ($this->_option == 1)
{
$result = Db::getInstance()->ExecuteS($this->_query0.$this->getDate().$this->_query2);
foreach ($result AS $row)
$this->_values[0][intval(substr($row['invoice_date'], 11, 2))] += 1;
}
}
private function getStatesData()
{
$result = Db::getInstance()->ExecuteS('
SELECT osl.`name`, COUNT(oh.`id_order`) as total
FROM `'._DB_PREFIX_.'order_state` os
LEFT JOIN `'._DB_PREFIX_.'order_state_lang` osl ON (os.`id_order_state` = osl.`id_order_state` AND osl.`id_lang` = '.intval($this->getLang()).')
LEFT JOIN `'._DB_PREFIX_.'order_history` oh ON os.`id_order_state` = oh.`id_order_state`
LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = oh.`id_order`
'.(intval($this->id_country) ? 'LEFT JOIN `'._DB_PREFIX_.'address` a ON o.id_address_delivery = a.id_address' : '').'
WHERE oh.`id_order_history` = (
SELECT ios.`id_order_history`
FROM `'._DB_PREFIX_.'order_history` ios
WHERE ios.`id_order` = oh.`id_order`
ORDER BY ios.`date_add` DESC, oh.`id_order_history` DESC
LIMIT 1
)
'.(intval($this->id_country) ? 'AND a.id_country = '.intval($this->id_country) : '').'
AND o.`date_add` BETWEEN '.ModuleGraph::getDateBetween().'
GROUP BY oh.`id_order_state`');
foreach ($result as $row)
{
$this->_values[] = $row['total'];
$this->_legend[] = $row['name'];
}
}
}
?>