Serveur preprod f0c0c48223 first push
2016-04-14 16:14:31 +02:00

262 lines
7.3 KiB
PHP

<?php
class AdminPrivatesalesStatsController extends ModuleAdminController
{
public function __construct()
{
$this->bootstrap = true;
parent::__construct();
}
public function setMedia()
{
parent::setMedia();
$this->addJS(_MODULE_DIR_.$this->module->name.'/js/admin.js');
$this->addCSS(_MODULE_DIR_.$this->module->name.'/css/admin.css', 'all');
$this->addJqueryUI(array('ui.widget', 'ui.slider', 'ui.datepicker'));
$this->addJS(_MODULE_DIR_.$this->module->name.'/js/jquery.ui.timepicker.min.js');
$this->addCSS(_MODULE_DIR_.$this->module->name.'/js/dataTable/css/jquery.dataTables.css', 'all');
$this->addJS(_MODULE_DIR_.$this->module->name.'/js/dataTable/src/jquery.dataTables.min.js');
}
public function initStatsState()
{
if(!Tools::getValue('state') || !Validate::isLoadedObject($this->stats_state = new PrivateSalesStatsStates(Tools::getValue('state'))))
throw new PrestaShopException(Tools::displayError('There is stats state'));
return true;
}
public function initPrivateSale()
{
if(!Tools::getValue('sale') || !Validate::isLoadedObject($this->private_sale = new SaleCoreExtend(Tools::getValue('sale'), $this->context->language->id)))
throw new PrestaShopException(Tools::displayError('There is an invalide private sale'));
return true;
}
public function displayAjaxError($string = 'error')
{
$array = array(
'hasError' => true,
'error' => $string
);
die ( json_encode($array) );
}
public function displayAjaxStatsGlobal()
{
$array_date = array(
'date_order_from', 'date_order_to'
//'date_sale_from', 'date_sale_to',
);
$export = Tools::getValue('export') == 1 ? true : false;
foreach($array_date as $date) {
if(Tools::getValue('check_by_date') == 1)
{
$$date = DateTime::createFromFormat('d/m/Y', Tools::getValue($date));
}
else
$$date = null;
}
if(Tools::getValue('order') != '' && in_array(Tools::getValue('orderby'), array('ASC', 'DESC')))
{
$orderby = Tools::getValue('order');
$ordered = Tools::getValue('orderby');
}
else
$ordered = $orderby = null;
$sales = MakeStats::getAllSales($date_sale_from, $date_sale_to);
$sales_stats = MakeStats::getStatsStatic($sales, 'global', $date_order_from, $date_order_to, $orderby, $ordered);
$total_payment_type = MakeStats::getStatsPaymentType($date_order_from, $date_order_to);
$total_global = MakeStats::getStatsGlobal($date_order_from, $date_order_to);
if($export === true)
{
$tpl = $this->createTemplate('sale_export/global.tpl');
$tpl->assign(array(
'sales' => $sales,
'sales_stats' => $sales_stats
));
$name = $this->l('Sales');
if($date_order_from !== null && $date_order_to !== null)
$name .= ' '.sprintf($this->l('BETWEEN %s and %s'), $date_order_from, $date_order_to);
MakeFile::makeCSV($tpl->fetch(), $name);
}
else
{
$tpl = $this->createTemplate('sale_stats/sale_stats_global.tpl');
$tpl->assign(array(
'orderby' => $orderby,
'ordered' => $ordered,
'sales' => $sales,
'sales_stats' => $sales_stats,
'export' => $export,
'export_link' => $_SERVER['REQUEST_URI'].'&export=1',
'total_payment_type' => $total_payment_type,
'total_global' => $total_global
));
die( json_encode($tpl->fetch()) );
}
}
public function displayAjaxStats()
{
try{
$this->initPrivateSale();
$type = Tools::getValue('type');
$array_date = array('date_by_sale_from', 'date_by_sale_to');
$export = Tools::getValue('export') == 1 ? true : false;
foreach($array_date as $date) {
if(Tools::getValue('check_by_date') == 1)
{
$$date = DateTime::createFromFormat('d/m/Y', Tools::getValue($date));
}
else
$$date = null;
}
if(Tools::getValue('order') != '' && in_array(Tools::getValue('orderby'), array('ASC', 'DESC')))
{
$orderby = Tools::getValue('order');
$ordered = Tools::getValue('orderby');
}
else
$ordered = $orderby = null;
$stats = MakeStats::getStats($this->private_sale, $type, $date_by_sale_from, $date_by_sale_to, $orderby, $ordered);
if($export === true)
{
$tpl = $this->createTemplate('sale_export/'.$type.'.tpl');
$tpl->assign(array(
'stats' => $stats
));
$name = '';
$name .= $this->l('Sale').' '.$this->private_sale->loadCategory()->name[Context::getContext()->language->id];
switch($type)
{
case 'product': $name .= ' '.$this->l('Products sell'); break;
case 'order': $name .= ' '.$this->l('Day by day');break;
}
if($date_by_sale_from !== null && $date_by_sale_to !== null)
$name .= ' '.sprintf($this->l('BETWEEN %s and %s'), $date_by_sale_from->format('d/m/y'), $date_by_sale_to->format('d/m/y'));
MakeFile::makeCSV($tpl->fetch(), $name);
}
else
{
$tpl = $this->createTemplate('sale_stats/sale_stats_'.$type.'.tpl');
$tpl->assign(array(
'orderby' => $orderby,
'ordered' => $ordered,
'stats' => $stats,
'export' => $export,
'export_link' => $_SERVER['REQUEST_URI'].'&export=1'
));
die( json_encode($tpl->fetch()) );
}
}
catch(PrestashopException $e) {
$this->displayAjaxError($e->getMessage());
}
}
public function displayAjaxStatsLine()
{
try{
$this->initPrivateSale();
$this->initStatsState();
$date_from = new DateTime($this->private_sale->date_start);
$date_to = new DateTime(date('Y-m-d H:i:s'));
$stats = $this->private_sale->getStats($this->stats_state, $date_from, $date_to);
$tpl = $this->createTemplate('sale_stats_line.tpl');
$tpl->assign(array(
'stats_block' => $stats
));
die( json_encode($tpl->fetch()) );
}
catch(PrestashopException $e) {
$this->displayAjaxError($e->getMessage());
}
}
public function run()
{
if(Tools::getValue('run_token') == 'fdsKFUYZPQZJVH585445fggkiJFU4448585')
{
set_time_limit(6000);
$sales = SaleCoreExtend::getSales('all', 0, false, false, false);
foreach($sales as $sale)
MakeStats::pushSale($sale);
if(!Tools::getIsset('full'))
{
$date_to = new DateTime();
$date_from = clone($date_to);
$date_from->modify('-1 month');
}
else
$date_to = $date_from = null;
MakeStats::run($date_from, $date_to);
Configuration::updateValue('PRIVATESALES_EXPORT_LAST_UPDATE', date('Y-m-d H:i:s'));
if(count(MakeStats::$errors) > 0)
{
$this->errors = MakeStats::$errors;
$this->warning[] = Tools::displayError('An error occurred during the update at least one sale');
}
else
$this->confirmations[] = Tools::displayError('Update correctly executed');
}
parent::run();
}
public function display()
{
$last_update_date = new DateTime(Configuration::get('PRIVATESALES_EXPORT_LAST_UPDATE'));
$date_now = new DateTime(date('Y-m-d H:i:s'));
$interval = $date_now->diff($last_update_date);
$last_update_minutes = $interval->format('%i');
$this->context->smarty->assign(array(
'date_sale_from' => '01/01/2012',
'date_sale_to' => date('d/m/Y'),
'date_order_from' => '01/01/'.date('Y'),
'date_order_to' => date('d/m/Y'),
'date_by_sale_from' => date('d/m/Y'),
'date_by_sale_to' => date('d/m/Y'),
'last_update_minutes' => $last_update_minutes
));
$this->setTemplate('index.tpl');
parent::display();
}
}