getRow(' SELECT date_format(o.`date_add`, \''.$format.'\') as date, SUM(o.`total_products` / c.conversion_rate) as totalht, SUM(o.`total_paid` / c.conversion_rate) as totalttc FROM `'._DB_PREFIX_.'orders` o LEFT JOIN `'._DB_PREFIX_.'currency` c ON o.id_currency = c.id_currency WHERE o.valid = 1 AND o.`date_add` BETWEEN '.$dateBetween.' GROUP BY date_format(o.`date_add`, \''.$format.'\') ORDER BY totalht '.$order); } public static function getRecords($dateBetween) { $xtrems = array(); $xtrems['bestmonth'] = self::recordQuery($dateBetween, '%Y-%m', 'DESC'); $xtrems['worstmonth'] = self::recordQuery($dateBetween, '%Y-%m', 'ASC'); $xtrems['bestday'] = self::recordQuery($dateBetween, '%Y-%m-%d', 'DESC'); $xtrems['worstday'] = self::recordQuery($dateBetween, '%Y-%m-%d', 'ASC'); if ($xtrems['bestmonth']) return $xtrems; } public static function getSales($dateBetween) { $result = Db::getInstance()->getRow(' SELECT COUNT(DISTINCT o.`id_order`) as orders, SUM(o.`total_paid` / c.conversion_rate) as ttc, SUM(o.`total_products` / c.conversion_rate) as ht FROM `'._DB_PREFIX_.'orders` o INNER JOIN `'._DB_PREFIX_.'currency` c ON o.id_currency = c.id_currency WHERE o.valid = 1 AND o.`invoice_date` BETWEEN '.$dateBetween); $products = Db::getInstance()->getRow(' SELECT COUNT(od.`id_order_detail`) as products FROM `'._DB_PREFIX_.'orders` o LEFT JOIN `'._DB_PREFIX_.'order_detail` od ON o.`id_order` = od.`id_order` WHERE o.valid = 1 AND o.`invoice_date` BETWEEN '.$dateBetween); $xtrems = Db::getInstance()->getRow(' SELECT MAX(`total_products`) as maxht, MIN(`total_products`) as minht, MAX(`total_paid`) as maxttc, MIN(`total_paid`) as minttc FROM ( SELECT o.`total_paid` / c.conversion_rate as total_paid, o.`total_products` / c.conversion_rate as total_products 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 o.`id_order` = od.`id_order` WHERE o.valid = 1 AND o.`invoice_date` BETWEEN '.$dateBetween.') records'); return array_merge($result, array_merge($xtrems, $products)); } public static function getCarts($dateBetween) { return Db::getInstance()->getRow(' SELECT AVG(cartsum) as average, MAX(cartsum) as highest, MIN(cartsum) as lowest FROM ( SELECT SUM( ((1+t.rate/100) * p.`price` + IF(cp.id_product_attribute IS NULL OR cp.id_product_attribute = 0, 0, ( SELECT IFNULL(pa.price, 0) FROM '._DB_PREFIX_.'product_attribute pa WHERE pa.id_product = cp.id_product AND pa.id_product_attribute = cp.id_product_attribute ))) * cp.quantity) / cu.conversion_rate as cartsum FROM `'._DB_PREFIX_.'cart` c LEFT JOIN `'._DB_PREFIX_.'currency` cu ON c.id_currency = cu.id_currency LEFT JOIN `'._DB_PREFIX_.'cart_product` cp ON c.`id_cart` = cp.`id_cart` LEFT JOIN `'._DB_PREFIX_.'product` p ON p.`id_product` = cp.`id_product` LEFT JOIN `'._DB_PREFIX_.'tax` t ON p.`id_tax` = t.`id_tax` WHERE c.`date_upd` BETWEEN '.$dateBetween.' GROUP BY c.`id_cart` ) carts'); } public function display() { global $cookie; $currency = Currency::getCurrency(Configuration::get('PS_CURRENCY_DEFAULT')); $language = Language::getLanguage(intval($cookie->id_lang)); $iso = $language['iso_code']; $dateBetween = ModuleGraph::getDateBetween(); $sales = self::getSales($dateBetween); $carts = self::getCarts($dateBetween); $records = self::getRecords($dateBetween); echo '
'; $this->displayCalendar(); $this->displayMenu(false); $this->displaySearch(); echo '
'.$this->l('Help').'

'.$this->l('Use the calendar on the left to select the time period.').'

'.$this->l('All available statistic modules are displayed in the Navigation list beneath the calendar.').'

'.$this->l('In the Settings sub-tab, you can also customize the Stats tab to fit your needs and resources, change the graph rendering engine, and adjust the database settings.').'



'.$this->l('Sales').'
'.$this->l('Total placed orders').''.$sales['orders'].'
'.$this->l('Total products sold').''.$sales['products'].'
'.$this->l('total paid with tax').' '.$this->l('total products not incl. tax').'
'.$this->l('Sales turnover').' '.Tools::displayPrice($sales['ttc'], $currency).' '.Tools::displayPrice($sales['ht'], $currency).'
'.$this->l('Largest order').' '.Tools::displayPrice($sales['maxttc'], $currency).' '.Tools::displayPrice($sales['maxht'], $currency).'
'.$this->l('Smallest order').' '.Tools::displayPrice($sales['minttc'], $currency).' '.Tools::displayPrice($sales['minht'], $currency).'


'.$this->l('Carts').'
'.$this->l('Products total all tax inc.').'
'.$this->l('Average cart').' '.Tools::displayPrice($carts['average'], $currency).'
'.$this->l('Largest cart').' '.Tools::displayPrice($carts['highest'], $currency).'
'.$this->l('Smallest cart').' '.Tools::displayPrice($carts['lowest'], $currency).'
'; if (strtolower($cookie->stats_granularity) != 'd' AND $records AND is_array($records)) { echo '

'.$this->l('Records').' '; if (strtolower($cookie->stats_granularity) == 'y') echo ' '; echo '
'.$this->l('date').' '.$this->l('with tax').' '.$this->l('only products not incl. tax').'
'.$this->l('Best month').' '.$this->displayDate($records['bestmonth']['date'], $iso).' '.Tools::displayPrice($records['bestmonth']['totalttc'], $currency).' '.Tools::displayPrice($records['bestmonth']['totalht'], $currency).'
'.$this->l('Worst month').' '.$this->displayDate($records['worstmonth']['date'], $iso).' '.Tools::displayPrice($records['worstmonth']['totalttc'], $currency).' '.Tools::displayPrice($records['worstmonth']['totalht'], $currency).'
'.$this->l('Best day').' '.$this->displayDate($records['bestday']['date'], $iso).' '.Tools::displayPrice($records['bestday']['totalttc'], $currency).' '.Tools::displayPrice($records['bestday']['totalht'], $currency).'
'.$this->l('Worst day').' '.$this->displayDate($records['worstday']['date'], $iso).' '.Tools::displayPrice($records['worstday']['totalttc'], $currency).' '.Tools::displayPrice($records['worstday']['totalht'], $currency).'
'; } echo '
'; } static public function displayDate($date, $iso) { $tmpTab = explode('-', $date); if (strtolower($iso == 'fr')) return (isset($tmpTab[2]) ? ($tmpTab[2].'-') : '').$tmpTab[1].'-'.$tmpTab[0]; else return $tmpTab[0].'-'.$tmpTab[1].(isset($tmpTab[2]) ? ('-'.$tmpTab[2]) : ''); } } ?>