privilegedemarque/modules/privatesales_tools/controllers/admin/AdminPrivatesalesLogistique.php

300 lines
8.2 KiB
PHP
Raw Permalink Normal View History

2016-04-14 16:14:31 +02:00
<?php
// if(!class_exists('PHPExcel_Autoloader'))
// require(_PS_MODULE_DIR_.'privatesales_tools/classes/PHPExcel.php');
class AdminPrivatesalesLogistiqueController extends ModuleAdminController
{
static $states;
static $array_bdc_infos = array('shop_address', 'shop_bottom_content', 'shop_phone');
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 display()
{
$infos_bdc = array();
foreach(self::$array_bdc_infos as $info)
{
$infos_bdc[$info] = Configuration::get('BDC_'.strtoupper($info), null, null, $this->context->shop->id);
}
$this->context->smarty->assign($infos_bdc);
$this->context->smarty->assign(array(
'date_sale_from' => date('d/m/Y'),
'date_sale_to' => date('d/m/Y'),
));
$this->setTemplate('index.tpl');
parent::display();
}
public function initProcess()
{
parent::initProcess();
if(Tools::getValue('action') == 'extractPrivateSale')
$this->action = 'extractPrivateSale';
}
public function displayAjaxError($string = 'error')
{
$array = array(
'hasError' => true,
'error' => $string
);
die ( json_encode($array) );
}
public function processExtractPrivateSale()
{
try{
$this->initPrivateSale();
$type = 1;
$this->makeExport($this->private_sale, $type);
$this->context->smarty->assign(array(
'privatesales' => $this->private_sale
));
}
catch(PrestaShopException $e) {
$this->errors[] = $e->getMessage();
}
}
public function displayAjaxExportForm()
{
try{
$this->initPrivateSale();
$date_from = new DateTime($this->private_sale->date_start);
$date_to = new DateTime(date('Y-m-d H:i:s'));
$tpl = $this->createTemplate('sale_export.tpl');
$tpl->assign(array(
'date_from' => $date_from,
'date_to' => $date_to,
'sale' => $this->private_sale,
'list' => $this->getListExport($this->private_sale),
'states' => self::getAllStatus()
));
die( json_encode(array('content' => $tpl->fetch())) );
}
catch(PrestashopException $e) {
$this->displayAjaxError($e->getMessage());
}
}
public function displayAjaxSales()
{
if(Tools::getValue('order') != '' && in_array(Tools::getValue('orderby'), array('ASC', 'DESC')))
{
$orderby = Tools::getValue('order');
$ordered = Tools::getValue('orderby');
}
else
$ordered = $orderby = null;
$array_date = array('date_sale_from', 'date_sale_to');
foreach($array_date as $date)
if(Tools::getValue('check_by_date') == 1)
$$date = DateTime::createFromFormat('d/m/Y', Tools::getValue($date));
else
$$date = null;
$sales = MakeStats::getAllSales($date_sale_from, $date_sale_to, $orderby, $ordered);
$tpl = $this->createTemplate('sales.tpl');
$tpl->assign(array(
'orderby' => $orderby,
'ordered' => $ordered,
'sales' => $sales
));
die( json_encode(array('content' => $tpl->fetch())) );
}
public function displayAjaxBDCUpdate()
{
if(!Tools::isSubmit('submitBDCInfos'))
$this->displayAjaxError(Tools::displayError('Formulary not submited'));
try {
foreach(self::$array_bdc_infos as $info)
{
Configuration::updateValue('BDC_'.strtoupper($info), Tools::getValue($info), false, null, Context::getContext()->shop->id);
}
} catch(Exception $e) {
$this->displayAjaxError($e->getMessage());
}
die(json_encode(array()));
}
public function makeExport(SaleCoreExtend $sale, $type = 1)
{
try {
$order_states = Tools::getValue('order_states');
$format = 'd/m/Y H:i:s';
$date_from = DateTime::createFromFormat($format, Tools::getValue('date_from', '01/01/2013 00:00:00') );
$date_to = DateTime::createFromFormat($format, Tools::getValue('date_to', date('d/m/Y H:i:s') ) );
$lines = MakeStats::getSaleExport($sale, $order_states, $type, $date_from, $date_to);
$private_sales_export = new PrivateSalesExport();
$private_sales_export->id_sale = $sale->id;
$private_sales_export->status = implode(',', $order_states);
$private_sales_export->from = $date_from->format('Y-m-d H:i:s');
$private_sales_export->to = $date_to->format('Y-m-d H:i:s');
$private_sales_export->save();
MakeFile::makeFileByType($sale, $lines, $type, $private_sales_export);
exit;
}
catch(Exception $e) {
$this->errors[] = $e->getMessage();
}
}
public function getListExport($privatesale)
{
if(!Validate::isLoadedObject($privatesale))
return false;
$fields_list = array(
'id_privatesales_export' => array(
'title' => 'ID',
'align' => 'center',
'width' => 25,
'remove_onclick' => true
),
'from' => array(
'title' => $this->l('From'),
'width' => 80,
'remove_onclick' => true,
'callback_object' => $this,
'callback' => 'getDateFr'
),
'to' => array(
'title' => $this->l('To'),
'width' => 80,
'remove_onclick' => true,
'callback_object' => $this,
'callback' => 'getDateFr'
),
'status' => array(
'title' => $this->l('Status'),
'width' => 100,
'remove_onclick' => true,
'callback_object' => $this,
'callback' => 'getStatusName',
'align' => 'center'
),
'date_add' => array(
'title' => $this->l('Date export'),
'width' => 50,
'remove_onclick' => true,
'callback_object' => $this,
'callback' => 'getDateFr'
),
'id_sale' => array(
'title' => $this->l('Download'),
'remove_onclick' => true,
'callback_object' => $this,
'callback' => 'getDownloadLink',
'align' => 'center'
)
);
$helperList = $this->initHelperList();
$helperList->shopLinkType = false;
$helperList->title = array($this->l('Old Export'));
$helperList->show_toolbar = false;
$helperList->identifier = 'id_privatesales_export';
$list = $privatesale->getExport();
return $content = $helperList->generateList($list, $fields_list);
}
public function initHelperList()
{
$helperList = new HelperList();
$helperList->token = Tools::getValue("token");
return $helperList;
}
public static function getAllStatus()
{
if(self::$states === null)
{
self::$states = OrderState::getOrderStates(Context::getContext()->language->id);
}
return self::$states;
}
public static function getStatusName($value)
{
$states = self::getAllStatus();
$string = '';
foreach(explode(',', $value) as $val)
{
foreach($states as $state)
if($state['id_order_state'] == $val)
{
$string .= ($string != '' ? '<br />' : '').$state['name'];
break;
}
}
if($string == '')
return '-';
return '<span data-original-title="'.$string.'" data-toggle="tooltip" class="label-tooltip">Voir</span>';
}
public static function getDateFr($value)
{
$date = new DateTime($value);
return $date->format('d/m/Y H:i:s'); //strftime("%A %d %B %Y", $date->getTimestamp()).' à '.$date->format('H').'H'.$date->format('i');
}
public static function getDownloadLink($value, $line)
{
return '<a href="'.PrivateSalesExport::getFileStatic($line['id_privatesales_export'], false).'" target="_blank"><i class="icon-download"></i></a>';
}
}