bebeboutik/modules/privatesales_livestats/AdminPrivateSalesLiveStats.php
Srv Bebeboutik 6c0978166c add modules
2016-01-04 12:49:26 +01:00

413 lines
16 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
include_once(_PS_ROOT_DIR_.'/modules/privatesales/Sale.php');
class AdminPrivateSalesLiveStats extends AdminTab {
public function __construct() {
$this->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 = '
<style type="text/css">
.ui-datepicker .ui-datepicker-prev span, .ui-datepicker .ui-datepicker-next span {
font-size: 0px;
}
#ui-timepicker-div-day {
display: none;
}
#day_from, #day_to {
width: 80px;
text-align: right;
}
label {
float: none;
display: inline-block;
}
.std.table {
width: 100%;
}
.right {
text-align: right;
}
.center {
text-align: center;
}
.yellow {
background: yellow;
}
.orange {
background: orange;
}
.red {
background: red;
color: #ffffff !important;
}
.button {
cursor: pointer;
}
.warn td:first-child {
background: red;
color: #ffffff !important;
}
fieldset label {
width: 160px;
}
fieldset span {
display: inline-block;
margin-left: 45px;
font-weight: bold;
}
</style>
<script type="text/javascript" src="'.__PS_BASE_URI__.'modules/privatesales_livestats/jquery-ui-1.8.20.custom.min.js"></script>
<script type="text/javascript" src="'.__PS_BASE_URI__.'modules/privatesales/jquery-ui-timepicker-addon.js"></script>
<script type="text/javascript" src="'.__PS_BASE_URI__.'modules/privatesales_livestats/stupidtable.min.js"></script>';
if($iso != 'en') {
$result .= '<script type="text/javascript" src="'.__PS_BASE_URI__.'modules/privatesales_livestats/jquery-ui-'.$iso.'.js"></script>';
}
$current_url = $currentIndex.'&token='.Tools::getAdminTokenLite('AdminPrivateSalesLiveStats');
$result .= '
<script type="text/javascript">
$(document).ready(function() {
$("input[name=day_from], input[name=day_to]").datepicker({
showTime: false,
dateFormat: "yy-mm-dd",
formattedDate: "yy-mm-dd",
maxDate: new Date,
minDate: new Date(2011, 1, 1)
});
$("#main .table").tablesorter({
headers: {
2: { sorter: "shortDate", dateFormat: "yyyy-mm-dd" },
3: { sorter: "shortDate", dateFormat: "yyyy-mm-dd" }
},
usNumberFormat: false
});
$("a.show_details").live("click", function(event){
event.preventDefault();
var that = this;
var id_sale = $(this).attr("data-id");
if ($(this).hasClass("active")) {
$(".details_sale_"+id_sale).hide();
$(this).removeClass("active");
} else {
if($(this).hasClass("load")) {
$(".details_sale_"+id_sale).show();
$(that).addClass("active");
} else {
$(that).html("<img src=\"/img/loader.gif\" />");
$.ajax({
url: "ajax_vp.php",
type: "GET",
data: "id_sale=" + id_sale + "&getStats",
success: function(html) {
$(that).html("Voir");
$(that).addClass("active");
$(that).addClass("load");
$(that).parent().parent().after(html);
}
});
}
}
});
});
</script>
<fieldset>
<label for="day_from">'.$this->l('Start date:').'</label>
<input type="text" id="day_from" name="day_from" autocomplete="off" value="'.$day_from.'" />
<label for="day_from">'.$this->l('End date:').'</label>
<input type="text" id="day_to" name="day_to" autocomplete="off" value="'.$day_to.'" />
<a class="button" onclick="document.location.href = \''.preg_replace('/[\?&]day_(from|to)=[0-9]{4}-[0-9]{2}-[0-9]{2}/', '', $_SERVER['REQUEST_URI']).'&day_from=\' + $(\'#day_from\').val() + \'&day_to=\' + $(\'#day_to\').val(); return false;">'.$this->l('Submit').'</a>
<span>'.$this->l('Last update:').' '.date('Y-m-d H:i:s', Configuration::get('PRIVATESALES_LIVESTATS_DATE')).'</span>
</fieldset>
<br />
<fieldset>
<legend>'.$this->l('Filtre par langue :').'</legend>
<a class="button" onclick="document.location.href = \''.preg_replace('/[\?&]day_(from|to)=[0-9]{4}-[0-9]{2}-[0-9]{2}/', '', $current_url).'&day_from=\' + $(\'#day_from\').val() + \'&day_to=\' + $(\'#day_to\').val(); return false;">'.$this->l('Tout').'</a>
<a class="button" onclick="document.location.href = \''.preg_replace('/[\?&]day_(from|to)=[0-9]{4}-[0-9]{2}-[0-9]{2}/', '', $current_url).'&id_lang=2&day_from=\' + $(\'#day_from\').val() + \'&day_to=\' + $(\'#day_to\').val(); return false;">'.$this->l('Français').'</a>
<a class="button" onclick="document.location.href = \''.preg_replace('/[\?&]day_(from|to)=[0-9]{4}-[0-9]{2}-[0-9]{2}/', '', $current_url).'&id_lang=3&day_from=\' + $(\'#day_from\').val() + \'&day_to=\' + $(\'#day_to\').val(); return false;">'.$this->l('Espagnol').'</a>
</fieldset>
<br />
<table class="table std">
<thead>
<tr>
<th rowspan="2" style="width: 35px;">'.$this->l('ID sale').'</th>
<th rowspan="2">'.$this->l('Sale title').'</th>
<th rowspan="2" style="width: 55px;">'.$this->l('Start date').'</th>
<th rowspan="2" style="width: 55px;">'.$this->l('End date').'</th>
<th rowspan="2" style="width: 35px;">'.$this->l('Days active').'</th>
<th rowspan="2" style="width: 45px;">'.$this->l('Init. stock').'</th>
<th colspan="4">'.$this->l('Period').'</th>
<th colspan="2">'.$this->l('Total').'</th>
<th rowspan="2" style="width: 50px;">'.$this->l('Out of stock').'</th>
<th rowspan="2" style="width: 50px;">'.$this->l('Out of stock (sold)').'</th>
<th rowspan="2" style="width: 40px;">'.$this->l('Margin').'</th>
<th rowspan="2" style="width: 50px;">'.$this->l('Employee').'</th>
<th rowspan="2" style="width: 50px;">'.$this->l('Out of stock').'</th>
</tr>
<tr>
<th data-sort="int">'.$this->l('Products').'<br /><span style="font-weight: normal; display: block; text-align: center; margin-top: 5px;">{total_products_period}</span></th>
<th data-sort="float">'.$this->l('Sales (WT)').'<br /><span style="font-weight: normal; display: block; text-align: center; margin-top: 5px;">{total_sales_period}</span></th>
<th data-sort="float">'.$this->l('Cmde').'<br /></th>
<th data-sort="float">'.$this->l('Multi').'<br /></th>
<th data-sort="int">'.$this->l('Products').'<br /><span style="font-weight: normal; display: block; text-align: center; margin-top: 5px;">{total_products}</span></th>
<th data-sort="float">'.$this->l('Sales (WT)').'<br /><span style="font-weight: normal; display: block; text-align: center; margin-top: 5px;">{total_sales}</span></th>
<tr>
</thead>
<tbody>';
$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 '<p class="error">'.$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')).'</p>';
}
}
}
$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 '<p class="error">'.$this->l('Missing global data for sale #').(int) $sale['id_sale'].' ('.$sale['name'].')</p>';
}
}
}
$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 .= '
<tr'.($warn? ' class="warn" title="'.addslashes($warn).'"': '').'>
<td class="right">'.(int) $sale['id_sale'].'</td>
<td>'.$sale['name'].'</td>
<td>'.date('Y-m-d', strtotime($sale['date_start'])).'</td>
<td>'.date('Y-m-d', strtotime($sale['date_end'])).'</td>
<td class="right">'.(strtotime($sale['date_end']) > time()? ceil((time() - strtotime($sale['date_start'])) / 86400) : /*ceil((strtotime($sale['date_end']) - strtotime($sale['date_start'])) / 86400)*/'T').'</td>
<td class="right">'.number_format($global[(int) $sale['id_sale']]['init_stock'], 0, '.', ' ').'</td>
<td class="right">'.number_format($period[(int) $sale['id_sale']][0], 0, '.', ' ').'</td>
<td class="right">'.number_format($period[(int) $sale['id_sale']][1], 0, '.', ' ').'</td>
<td class="right">'.number_format($period[(int) $sale['id_sale']][2], 0, '.', ' ').'</td>
<td class="right">'.number_format((($period[(int) $sale['id_sale']][3]/$period[(int) $sale['id_sale']][2]) * 100), 0, '.', ' ') .'%'.'</td>
<td class="right">'.number_format((int) $global[(int) $sale['id_sale']]['total_products'], 0, '.', ' ').'</td>
<td class="right">'.number_format((float) $global[(int) $sale['id_sale']]['total_sales_wt'], 0, '.', ' ').'</td>
<td class="center'.($global[(int) $sale['id_sale']]['oos'] >= 30? ' red': ($global[(int) $sale['id_sale']]['oos'] >= 20? ' orange': ($global[(int) $sale['id_sale']]['oos'] >= 10? ' yellow': ''))).'">'.$global[(int) $sale['id_sale']]['oos'].' %</td>
<td class="center'.($global[(int) $sale['id_sale']]['oos_sold'] >= 30? ' red': ($global[(int) $sale['id_sale']]['oos_sold'] >= 20? ' orange': ($global[(int) $sale['id_sale']]['oos_sold'] >= 10? ' yellow': ''))).'">'.$global[(int) $sale['id_sale']]['oos_sold'].' %</td>
<td class="center">'.sprintf('%.02f', Tools::ps_round((float) $global[(int) $sale['id_sale']]['margin_rate'] * 100, 2)).' %</td>
<td class="center">'.substr($sale['firstname'], 0, 1).substr($sale['lastname'], 0, 1).'</td>
<td class="center"><a class="show_details" href="#" data-id="'.(int)$sale['id_sale'].'">Voir</a></td>
</tr>
';
$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 .= ' </tbody>
</table>
';
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);
}
}