lang = TRUE; parent::__construct(); } public function displayForm($isMainTab=TRUE) { global $currentIndex, $cookie; parent::displayForm(); } public function display() { global $cookie, $currentIndex; $iso = Language::getIsoById((int)($cookie->id_lang)); $day_from = Tools::getValue('day_from', date('Y-m-d')); if(!preg_match('/^20[0-1][0-9]-[0-1][0-9]-[0-3][0-9]$/', $day_from)) { $day_from = date('Y-m-d'); } $day_to = Tools::getValue('day_to', date('Y-m-d')); if(!preg_match('/^20[0-1][0-9]-[0-1][0-9]-[0-3][0-9]$/', $day_to)) { $day_to = date('Y-m-d'); } if(strtotime($day_to) < strtotime($day_from)) { $day_to = $day_from; } $result = ' '; if($iso != 'en') { $result .= ''; } $current_url = $currentIndex.'&token='.Tools::getAdminTokenLite('AdminPrivateSalesLiveStats'); $result .= '
'.$this->l('Submit').' '.$this->l('Last update:').' '.date('Y-m-d H:i:s', Configuration::get('PRIVATESALES_LIVESTATS_DATE')).'

'.$this->l('Filtre par langue :').' '.$this->l('Tout').' '.$this->l('Français').' '.$this->l('Espagnol').'

'; $filter_lang = false; $langs_allow = array( 'fr' => 2, 'es' => 3 ); if (Tools::getValue('id_lang') && in_array(Tools::getValue('id_lang'), $langs_allow) ) { $langs = array(Tools::getValue('id_lang')); $filter_lang = true; } else { $langs = $langs_allow; } $iso = array_search(Tools::getValue('id_lang'), $langs_allow); $sales = array(); foreach(Db::getInstance()->ExecuteS(' SELECT p.*, l.`name`, e.`firstname`, e.`lastname` FROM `'._DB_PREFIX_.'privatesale` p LEFT JOIN `'._DB_PREFIX_.'category_lang` l ON p.`id_category` = l.`id_category` LEFT JOIN `'._DB_PREFIX_.'privatesale_group` g ON g.`id_sale` = p.`id_sale` LEFT JOIN `'._DB_PREFIX_.'employee` e ON p.`id_employee` = e.`id_employee` '. (($filter_lang) ? 'RIGHT JOIN `'._DB_PREFIX_.'privatesale_site_version` v ON v.`id_sale` = p.`id_sale` AND v.`version` = "'.$iso.'"' : '') .' WHERE ((CAST(p.`date_start` AS DATE) >= "'.pSQL($day_from).'" AND CAST(p.`date_start` AS DATE) <= "'.pSQL($day_to).'") OR (CAST(p.`date_end` AS DATE) >= "'.pSQL($day_from).'" AND CAST(p.`date_end` AS DATE) <= "'.pSQL($day_to).'") OR (CAST(p.`date_start` AS DATE) <= "'.pSQL($day_from).'" AND CAST(p.`date_end` AS DATE) >= "'.pSQL($day_to).'")) AND l.`id_lang` = '.(int) $cookie->id_lang.' AND g.`id_group` = 1 AND ((p.`date_end` > NOW() AND p.`enabled` = 1) OR p.`date_end` <= NOW() ) GROUP BY p.`id_sale` ORDER BY p.`date_start` DESC ') AS $row) { $sales[(int) $row['id_sale']] = $row; } $period = array(); foreach ($langs as $iso => $id_lang) { $cache = array(); foreach(Db::getInstance()->ExecuteS(' SELECT * FROM `'._DB_PREFIX_.'privatesale_livestats` WHERE `day` >= "'.pSQL($day_from).'" AND `day` <= "'.pSQL($day_to).'" AND `id_sale` IN ('.implode(', ', array_keys($sales)).') AND `id_lang` = '.(int) $id_lang.' ') as $row) { if(!isset($cache[(int) $row['id_sale']])) { $cache[(int) $row['id_sale']] = array(); } $cache[(int) $row['id_sale']][strtotime($row['day'])] = $row; } foreach($sales as $sale) { if (!is_array($period[(int) $sale['id_sale']])) { $period[(int) $sale['id_sale']] = array(0, 0); } for($i=0, $l=ceil((strtotime($day_to) - strtotime($day_from) + 10) / 86400) - 1; $i <= $l; $i++) { if(isset($cache[(int) $sale['id_sale']][strtotime(date('Y-m-d', strtotime($day_from.' + '.$i.' day')))])) { $period[(int) $sale['id_sale']][0] += $cache[(int) $sale['id_sale']][strtotime(date('Y-m-d', strtotime($day_from.' + '.$i.' day')))]['total_products']; $period[(int) $sale['id_sale']][1] += $cache[(int) $sale['id_sale']][strtotime(date('Y-m-d', strtotime($day_from.' + '.$i.' day')))]['total_sales_wt']; $period[(int) $sale['id_sale']][2] += $cache[(int) $sale['id_sale']][strtotime(date('Y-m-d', strtotime($day_from.' + '.$i.' day')))]['total_orders']; $period[(int) $sale['id_sale']][3] += $cache[(int) $sale['id_sale']][strtotime(date('Y-m-d', strtotime($day_from.' + '.$i.' day')))]['total_multi']; } else { // test si data manquante uniquement sur la langue FR (2) if(strtotime($sale['date_start']) < strtotime(date('Y-m-d', strtotime($day_from.' + '.$i.' day'))) && strtotime($sale['date_end']) > strtotime(date('Y-m-d', strtotime($day_from.' + '.$i.' day'))) && $id_lang != 3) { echo '

'.$this->l('Missing data for sale #').(int) $sale['id_sale'].' ('.$sale['name'].') '.$this->l('on date').' '.date('Y-m-d', strtotime($day_from.' + '.$i.' day')).'

'; } } } $query = Db::getInstance()->getRow(' SELECT * FROM `'._DB_PREFIX_.'privatesale_livestats_sale` WHERE `id_sale` = '.(int) $sale['id_sale'].' '); if($query) { $global[(int) $sale['id_sale']] = $query; } else { echo '

'.$this->l('Missing global data for sale #').(int) $sale['id_sale'].' ('.$sale['name'].')

'; } } } $total_products = 0; $total_products_period = 0; $total_sales = 0.0; $total_sales_period = 0.0; $total_order_period = 0; $total_multi_period = 0; foreach($sales as $sale) { $warn = FALSE; if(!isset($global[(int) $sale['id_sale']])) { $global[(int) $sale['id_sale']] = array( 'init_stock' => 0, 'total_products' => 0, 'total_sales_wt' => 0, 'oos' => 0, 'oos_sold' => 0, 'warn' => 0, 'id_sale' => (int) $sale['id_sale'], ); } // surcharge if filter if ($filter_lang) { $id_lang_filter = Tools::getValue('id_lang'); $query_lang = Db::getInstance()->getRow(' SELECT `total_products`, `total_sales_wt` FROM `'._DB_PREFIX_.'privatesale_livestats_sale_lang` WHERE `id_sale` = '.(int) $sale['id_sale'].' AND `id_lang` = '.(int) $id_lang_filter.' '); $global[(int) $sale['id_sale']]['total_products'] = $query_lang['total_products']; $global[(int) $sale['id_sale']]['total_sales_wt'] = $query_lang['total_sales_wt']; } if($global[(int) $sale['id_sale']]['warning'] == 1) { $warn = $this->l('More than 20% of at least 5% of products stocks sold in less than 8 hours'); } elseif($global[(int) $sale['id_sale']]['warning'] == 2) { $warn = $this->l('More than 30% of at least 15% of products stocks sold in less than 29 hours'); } elseif($global[(int) $sale['id_sale']]['warning'] == 3) { $warn = $this->l('More than 40% of at least 20% of products stocks sold in less than 53 hours'); } elseif($global[(int) $sale['id_sale']]['warning'] == 4) { $warn = $this->l('More than 50% of at least 25% of products stocks sold'); } $result .= ' '; $total_products += (int) $global[(int) $sale['id_sale']]['total_products']; $total_products_period += $period[(int) $sale['id_sale']][0]; $total_sales += (float) $global[(int) $sale['id_sale']]['total_sales_wt']; $total_sales_period += $period[(int) $sale['id_sale']][1]; $total_order_period += $period[(int) $sale['id_sale']][2]; $total_multi_period += $period[(int) $sale['id_sale']][3]; } $result .= '
'.$this->l('ID sale').' '.$this->l('Sale title').' '.$this->l('Start date').' '.$this->l('End date').' '.$this->l('Days active').' '.$this->l('Init. stock').' '.$this->l('Period').' '.$this->l('Total').' '.$this->l('Out of stock').' '.$this->l('Out of stock (sold)').' '.$this->l('Margin').' '.$this->l('Employee').' '.$this->l('Out of stock').'
'.$this->l('Products').'
{total_products_period}
'.$this->l('Sales (WT)').'
{total_sales_period}
'.$this->l('Cmde').'
'.$this->l('Multi').'
'.$this->l('Products').'
{total_products}
'.$this->l('Sales (WT)').'
{total_sales}
'.(int) $sale['id_sale'].' '.$sale['name'].' '.date('Y-m-d', strtotime($sale['date_start'])).' '.date('Y-m-d', strtotime($sale['date_end'])).' '.(strtotime($sale['date_end']) > time()? ceil((time() - strtotime($sale['date_start'])) / 86400) : /*ceil((strtotime($sale['date_end']) - strtotime($sale['date_start'])) / 86400)*/'T').' '.number_format($global[(int) $sale['id_sale']]['init_stock'], 0, '.', ' ').' '.number_format($period[(int) $sale['id_sale']][0], 0, '.', ' ').' '.number_format($period[(int) $sale['id_sale']][1], 0, '.', ' ').' '.number_format($period[(int) $sale['id_sale']][2], 0, '.', ' ').' '.number_format((($period[(int) $sale['id_sale']][3]/$period[(int) $sale['id_sale']][2]) * 100), 0, '.', ' ') .'%'.' '.number_format((int) $global[(int) $sale['id_sale']]['total_products'], 0, '.', ' ').' '.number_format((float) $global[(int) $sale['id_sale']]['total_sales_wt'], 0, '.', ' ').' '.$global[(int) $sale['id_sale']]['oos'].' % '.$global[(int) $sale['id_sale']]['oos_sold'].' % '.sprintf('%.02f', Tools::ps_round((float) $global[(int) $sale['id_sale']]['margin_rate'] * 100, 2)).' % '.substr($sale['firstname'], 0, 1).substr($sale['lastname'], 0, 1).' Voir
'; echo str_replace(array( '{total_products_period}', '{total_sales_period}', '{total_products}', '{total_sales}', '{total_order_period}', '{total_multi_period}', ), array( number_format($total_products_period, 0, '.', ' '), number_format($total_sales_period, 0, '.', ' '), number_format($total_products, 0, '.', ' '), number_format($total_sales, 0, '.', ' '), number_format($total_order_period, 0, '.', ' '), number_format( ($total_multi_period/$total_order_period*100) , 0, '.', ' ') .'%', ), $result); } }