* @copyright 2007-2014 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
if (!defined('_PS_VERSION_'))
exit;
class StatsCheckUp extends Module
{
private $html = '';
public function __construct()
{
$this->name = 'statscheckup';
$this->tab = 'analytics_stats';
$this->version = '1.3.1';
$this->author = 'PrestaShop';
$this->need_instance = 0;
parent::__construct();
$this->displayName = $this->l('Catalog evaluation');
$this->description = $this->l('Adds a quick evaluation of your catalog quality to the Stats dashboard.');
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
}
public function install()
{
$confs = array(
'CHECKUP_DESCRIPTIONS_LT' => 100,
'CHECKUP_DESCRIPTIONS_GT' => 400,
'CHECKUP_IMAGES_LT' => 1,
'CHECKUP_IMAGES_GT' => 2,
'CHECKUP_SALES_LT' => 1,
'CHECKUP_SALES_GT' => 2,
'CHECKUP_STOCK_LT' => 1,
'CHECKUP_STOCK_GT' => 3
);
foreach ($confs as $confname => $confdefault)
if (!Configuration::get($confname))
Configuration::updateValue($confname, (int)$confdefault);
return (parent::install() && $this->registerHook('AdminStatsModules'));
}
public function hookAdminStatsModules()
{
if (Tools::isSubmit('submitCheckup'))
{
$confs = array(
'CHECKUP_DESCRIPTIONS_LT',
'CHECKUP_DESCRIPTIONS_GT',
'CHECKUP_IMAGES_LT',
'CHECKUP_IMAGES_GT',
'CHECKUP_SALES_LT',
'CHECKUP_SALES_GT',
'CHECKUP_STOCK_LT',
'CHECKUP_STOCK_GT'
);
foreach ($confs as $confname)
Configuration::updateValue($confname, (int)Tools::getValue($confname));
echo '
'.$this->l('Configuration updated').'
';
}
if (Tools::isSubmit('submitCheckupOrder'))
{
$this->context->cookie->checkup_order = (int)Tools::getValue('submitCheckupOrder');
echo ' '.$this->l('Configuration updated').'
';
}
if (!isset($this->context->cookie->checkup_order))
$this->context->cookie->checkup_order = 1;
$db = Db::getInstance(_PS_USE_SQL_SLAVE_);
$employee = Context::getContext()->employee;
$prop30 = ((strtotime($employee->stats_date_to.' 23:59:59') - strtotime($employee->stats_date_from.' 00:00:00')) / 60 / 60 / 24) / 30;
// Get languages
$sql = 'SELECT l.*
FROM '._DB_PREFIX_.'lang l'
.Shop::addSqlAssociation('lang', 'l');
$languages = $db->executeS($sql);
$array_colors = array(
0 => '',
1 => '',
2 => ''
);
$token_products = Tools::getAdminToken('AdminProducts'.(int)Tab::getIdFromClassName('AdminProducts').(int)Context::getContext()->employee->id);
$divisor = 4;
$totals = array('products' => 0, 'active' => 0, 'images' => 0, 'sales' => 0, 'stock' => 0);
foreach ($languages as $language)
{
$divisor++;
$totals['description_'.$language['iso_code']] = 0;
}
$order_by = 'p.id_product';
if ($this->context->cookie->checkup_order == 2)
$order_by = 'pl.name';
else if ($this->context->cookie->checkup_order == 3)
$order_by = 'nbSales DESC';
// Get products stats
$sql = 'SELECT p.id_product, product_shop.active, pl.name, (
SELECT COUNT(*)
FROM '._DB_PREFIX_.'image i
'.Shop::addSqlAssociation('image', 'i').'
WHERE i.id_product = p.id_product
) as nbImages, (
SELECT SUM(od.product_quantity)
FROM '._DB_PREFIX_.'orders o
LEFT JOIN '._DB_PREFIX_.'order_detail od ON o.id_order = od.id_order
WHERE od.product_id = p.id_product
AND o.invoice_date BETWEEN '.ModuleGraph::getDateBetween().'
'.Shop::addSqlRestriction(Shop::SHARE_ORDER, 'o').'
) as nbSales,
IFNULL(stock.quantity, 0) as stock
FROM '._DB_PREFIX_.'product p
'.Shop::addSqlAssociation('product', 'p').'
'.Product::sqlStock('p', 0).'
LEFT JOIN '._DB_PREFIX_.'product_lang pl
ON (p.id_product = pl.id_product AND pl.id_lang = '.(int)$this->context->language->id.Shop::addSqlRestrictionOnLang('pl').')
ORDER BY '.$order_by;
$result = $db->executeS($sql);
if (!$result)
return $this->l('No product was found.');
$array_conf = array(
'DESCRIPTIONS' => array('name' => $this->l('Descriptions'), 'text' => $this->l('chars (without HTML)')),
'IMAGES' => array('name' => $this->l('Images'), 'text' => $this->l('images')),
'SALES' => array('name' => $this->l('Sales'), 'text' => $this->l('orders / month')),
'STOCK' => array('name' => $this->l('Available quantity for sale'), 'text' => $this->l('items'))
);
$this->html = '
'
.$this->displayName.'
'.$this->l('ID').' |
'.$this->l('Item').' |
'.$this->l('Active').' | ';
foreach ($languages as $language)
$this->html .= ''.$this->l('Desc.').' ('.Tools::strtoupper($language['iso_code']).') | ';
$this->html .= '
'.$this->l('Images').' |
'.$this->l('Sales').' |
'.$this->l('Available quantity for sale').' |
'.$this->l('Global').' |
';
foreach ($result as $row)
{
$totals['products']++;
$scores = array(
'active' => ($row['active'] ? 2 : 0),
'images' => ($row['nbImages'] < Configuration::get('CHECKUP_IMAGES_LT') ? 0 : ($row['nbImages'] > Configuration::get('CHECKUP_IMAGES_GT') ? 2 : 1)),
'sales' => (($row['nbSales'] * $prop30 < Configuration::get('CHECKUP_SALES_LT')) ? 0 : (($row['nbSales'] * $prop30 > Configuration::get('CHECKUP_SALES_GT')) ? 2 : 1)),
'stock' => (($row['stock'] < Configuration::get('CHECKUP_STOCK_LT')) ? 0 : (($row['stock'] > Configuration::get('CHECKUP_STOCK_GT')) ? 2 : 1)),
);
$totals['active'] += (int)$scores['active'];
$totals['images'] += (int)$scores['images'];
$totals['sales'] += (int)$scores['sales'];
$totals['stock'] += (int)$scores['stock'];
$descriptions = $db->executeS('
SELECT l.iso_code, pl.description
FROM '._DB_PREFIX_.'product_lang pl
LEFT JOIN '._DB_PREFIX_.'lang l
ON pl.id_lang = l.id_lang
WHERE id_product = '.(int)$row['id_product'].Shop::addSqlRestrictionOnLang('pl'));
foreach ($descriptions as $description)
{
if (isset($description['iso_code']) && isset($description['description']))
$row['desclength_'.$description['iso_code']] = Tools::strlen(strip_tags($description['description']));
if (isset($description['iso_code']))
{
$scores['description_'.$description['iso_code']] = ($row['desclength_'.$description['iso_code']] < Configuration::get('CHECKUP_DESCRIPTIONS_LT') ? 0 : ($row['desclength_'.$description['iso_code']] > Configuration::get('CHECKUP_DESCRIPTIONS_GT') ? 2 : 1));
$totals['description_'.$description['iso_code']] += $scores['description_'.$description['iso_code']];
}
}
$scores['average'] = array_sum($scores) / $divisor;
$scores['average'] = ($scores['average'] < 1 ? 0 : ($scores['average'] > 1.5 ? 2 : 1));
$this->html .= '
'.$row['id_product'].' |
'.Tools::substr($row['name'], 0, 42).' |
'.$array_colors[$scores['active']].' | ';
foreach ($languages as $language)
if (isset($row['desclength_'.$language['iso_code']]))
$this->html .= ''.(int)$row['desclength_'.$language['iso_code']].' '.$array_colors[$scores['description_'.$language['iso_code']]].' | ';
else
$this->html .= '0 '.$array_colors[0].' | ';
$this->html .= '
'.(int)$row['nbImages'].' '.$array_colors[$scores['images']].' |
'.(int)$row['nbSales'].' '.$array_colors[$scores['sales']].' |
'.(int)$row['stock'].' '.$array_colors[$scores['stock']].' |
'.$array_colors[$scores['average']].' |
';
}
$this->html .= '';
$totals['active'] = $totals['active'] / $totals['products'];
$totals['active'] = ($totals['active'] < 1 ? 0 : ($totals['active'] > 1.5 ? 2 : 1));
$totals['images'] = $totals['images'] / $totals['products'];
$totals['images'] = ($totals['images'] < 1 ? 0 : ($totals['images'] > 1.5 ? 2 : 1));
$totals['sales'] = $totals['sales'] / $totals['products'];
$totals['sales'] = ($totals['sales'] < 1 ? 0 : ($totals['sales'] > 1.5 ? 2 : 1));
$totals['stock'] = $totals['stock'] / $totals['products'];
$totals['stock'] = ($totals['stock'] < 1 ? 0 : ($totals['stock'] > 1.5 ? 2 : 1));
foreach ($languages as $language)
{
$totals['description_'.$language['iso_code']] = $totals['description_'.$language['iso_code']] / $totals['products'];
$totals['description_'.$language['iso_code']] = ($totals['description_'.$language['iso_code']] < 1 ? 0 : ($totals['description_'.$language['iso_code']] > 1.5 ? 2 : 1));
}
$totals['average'] = array_sum($totals) / $divisor;
$totals['average'] = ($totals['average'] < 1 ? 0 : ($totals['average'] > 1.5 ? 2 : 1));
$this->html .= '
|
'.$this->l('Active').' | ';
foreach ($languages as $language)
$this->html .= ''.$this->l('Desc.').' ('.Tools::strtoupper($language['iso_code']).') | ';
$this->html .= '
'.$this->l('Images').' |
'.$this->l('Sales').' |
'.$this->l('Available quantity for sale').' |
'.$this->l('Global').' |
|
'.$array_colors[$totals['active']].' | ';
foreach ($languages as $language)
$this->html .= ''.$array_colors[$totals['description_'.$language['iso_code']]].' | ';
$this->html .= '
'.$array_colors[$totals['images']].' |
'.$array_colors[$totals['sales']].' |
'.$array_colors[$totals['stock']].' |
'.$array_colors[$totals['average']].' |
';
return $this->html;
}
}