bebeboutik/modules/privatesales_logistique/AdminPrivateSalesLogistique.php
2016-04-06 13:09:54 +02:00

616 lines
26 KiB
PHP
Executable File

<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
include_once(_PS_ROOT_DIR_.'/modules/privatesales_logistique/classes/OrderForm.php');
include_once(_PS_ROOT_DIR_.'/modules/privatesales_logistique/classes/DateManager.php');
include_once(_PS_ROOT_DIR_.'/modules/privatesales_logistique/classes/MakeStats.php');
include_once(_PS_ROOT_DIR_.'/modules/privatesales_logistique/classes/SaleStats.php');
/**
* Description of AdminPrivateSalesLogistique
*
* @author Pierre
*/
class AdminPrivateSalesLogistique extends AdminTab {
public $category;
public $html = '';
public function __construct()
{
parent::__construct();
}
public function _postProcess()
{
if(Tools::isSubmit('changeCategory'))
{
$this->category = new Category(Tools::getValue("id"));
}
if(Tools::getValue("actionform") == 'delete')
{
if(Tools::getValue('idorderform') !== false && Validate::isLoadedObject($order_form = new OrderForm((int)Tools::getValue('idorderform'))))
{
if($order_form->delete())
echo 'Suppression effectuée';
else
echo 'Erreur durant la suppression';
}
else
echo 'Mauvais id de bon de commande';
exit;
}
}
public function _displayHeader()
{
global $currentIndex;
$this->html .= '
<link rel="stylesheet" type="text/css" href="'.__PS_BASE_URI__.'modules/privatesales_logistique/css/privatesales_logistique.css" />
<script type="text/javascript" src="'.__PS_BASE_URI__.'modules/privatesales_logistique/js/jquery-ui-1.8.20.custom.min.js"></script>
<script type="text/javascript" src="'.__PS_BASE_URI__.'modules/privatesales_logistique/js/jquery-ui-timepicker-addon.js"></script>
<script type="text/javascript" src="'.__PS_BASE_URI__.'modules/privatesales_logistique/js/filtre.js"></script>
<style type="text/css">
@import url("'.__PS_BASE_URI__.'modules/privatesales/jquery-ui-1.8.20.custom.css");
#exportorderform { line-height:25px; }
</style>';
$this->html .= '<script>
jQuery(function () {
$("#category").change( function(){
current_location = "'.$currentIndex.'&token='.Tools::getAdminTokenLite('AdminPrivateSalesLogistique').'";
document.location.href = current_location + "&changeCategory&id=" + $("#category").val();
});
});
$(document).ready(function(){
$("#hasDatepicker_1, #hasDatepicker_2").datetimepicker({
prevText:"",
nextText:"",
showSecond: true,
timeFormat: "hh:mm:ss",
dateFormat: "dd/mm/yy",
addSliderAccess: true,
showButtonPanel: false
});
$("#exportorderform").click(function(){
var export_last = $("#export_last").is(":checked"), req;
req = "&id="+$("#category").val();
if(export_last) {
req += "&last"
}
else {
var from = $("#hasDatepicker_1").val();
var to = $("#hasDatepicker_2").val();
req += "&from="+from+"&to="+to;
}
var type = $("#export_form input[name=export_type]:checked").val();
if(type == 1 || type == 2 || type == 4) {
req += "&type="+type;
req += "&lang="+$("input[name=lang]:checked").val();
$(this)
.attr("href", "../modules/privatesales_logistique/exportOrderForm.php?action=getOrderForm"+req)
.attr("target", "_blank");
return true;
} else {
var from = $("#hasDatepicker_1").val();
var to = $("#hasDatepicker_2").val();
req += "&from="+from+"&to="+to;
req += "&type="+type;
var url_dest = "'.$currentIndex.'&token='.Tools::getAdminTokenLite('AdminPrivateSalesLogistique').'&action=getStats";
$(this)
.attr("href", url_dest+req)
.attr("target", "_blank");
return true;
}
});
$("#toggle_form_infos").click(function(){
$("#form_infos").toggle();
});
});
</script>';
}
public function _displayContent()
{
global $cookie;
//$categories = CategoryCore::getCategories($cookie->id_lang,FALSE);
$categories = Category::getCategories($cookie->id_lang, FALSE, true, ' AND c.id_parent = 1', ' ORDER BY c.id_category DESC ', ' LIMIT 500');
$ids = array();
foreach ($categories as $key => $category) {
foreach ($category as $key => $data) {
$ids[] = $data['infos']['id_category'];
}
}
$extrafields = Category::getSalesInfos($ids);
krsort($categories[1]);
$this->html .= '<fieldset>
<legend><img src="../img/admin/tab-tools.gif" alt="" />'.$this->l('Inventory Tracking').'</legend>
<h3>Ventes :</h3>';
if(count($categories) > 0)
{
$this->html .= '<select id="category" name="category" size="5" style="width:400px;height:150px;vertical-align:top;">';
foreach($categories[1] as $categorie)
{
$select = "";
if($this->category->id == $categorie['infos']['id_category'])
$select = "selected='selected'";
$this->html .= "<option ".$select." value='".$categorie['infos']['id_category']."'>".$categorie['infos']['name']." - ".$extrafields[$categorie['infos']['id_category']]['sales'][1].' - '.$categorie['infos']['id_category']."</option>";
}
$this->html .= '</select> - Filtre : <input type="text" value="" id="filter_sale" size="50"><br/><br/>';
}
else
$this->html .= 'Aucune vente';
if(!empty($this->category))
{
$this->html .= $this->getFormOrderForm($this->category);
$this->html .= '<br/><br/>';
}
if( Tools::getIsset('action')
&& Tools::getValue('action') == 'getStats')
{
$this->html .= $this->getStatsArray();
$this->html .= '<br/><br/>';
}
}
public function display()
{
global $currentIndex;
$this->_postProcess();
$this->_displayHeader();
$this->_displayContent();
echo $this->html;
}
public function getStatsArray()
{
global $cookie;
if (!Tools::getIsset('id')) {
return false;
}
// GET INFO FOR STAT
$date_from = new DateManager(Tools::getValue('from'));
$date_to = new DateManager(Tools::getValue('to'));
$date_from->setLangue(strtoupper($lang_id));
$date_to->setLangue(strtoupper($lang_id));
$id_category = Tools::getValue('id');
$sale = Sale::getSaleFromCategory($id_category);
$category = new Category($id_category, $cookie->id_lang);
$products = MakeStats::getProductsByCat($id_category);
$arrayProduct = array();
foreach($products as $product)
$array_product[$product['id_product']] = $arrayProduct[$product['id_product']] = new Product($product['id_product']);
$order_states = explode(',', Configuration::get('PS_IT_OF_ORDER_STATES'));
$lines = MakeStats::getOrdersByIdsProduct(array_keys($arrayProduct), $order_states, $date_from->getDate('SQL'), $date_to->getDate('SQL'));
// for stats
$lines_tmp = $lines;
$lines = MakeStats::getArrayLines($lines);
$stats = new SaleStats($sale, $category);
$stats->loadData();
$total_sale_product = array();
$total_bdc_ht = 0;
foreach ($lines_tmp as $key => $order_line) {
$id_product = $order_line['product_id'];
$id_product_attribute = $order_line['product_attribute_id'];
$current_product = $array_product[$id_product];
if($id_product_attribute != 0) {
$combination = new Combination($id_product_attribute);
if($combination->wholesale_price == 0) {
$wholesale_price = $current_product->wholesale_price;
} else {
$wholesale_price = $combination->wholesale_price;
}
} else {
$wholesale_price = $current_product->wholesale_price;
}
$quantity_to_cmd = $order_line['product_quantity'] - $order_line['product_quantity_reinjected'];
$total_sale_ht += $quantity_to_cmd * $wholesale_price;
if (!is_array($total_sale_product[(int)$id_product]) ){
$total_sale_product[(int)$id_product] = array();
$total_sale_product[(int)$id_product]['total_ht'] = 0;
$total_sale_product[(int)$id_product]['quantiy'] = 0;
$total_sale_product[(int)$id_product]['name'] = $current_product->name[2];
}
$total_sale_product[(int)$id_product]['total_ht'] += ($wholesale_price * $quantity_to_cmd);
$total_sale_product[(int)$id_product]['quantiy'] += $quantity_to_cmd;
}
$stats->addToStats('total_bdc_ht', $total_sale_ht);
$bestsales = MakeStats::orderMultiArray($total_sale_product, 'total_ht');
$bestsales_extract = array_slice($bestsales, 0, 3);
$i = 1;
foreach ($bestsales_extract as $key => $bestsalesProduct) {
$stats->addToStats('bestsale_'.$i.'_product', $bestsalesProduct['name']);
$stats->addToStats('bestsale_'.$i.'_quantity', $bestsalesProduct['quantiy']);
$stats->addToStats('bestsale_'.$i.'_ca', $bestsalesProduct['total_ht']);
$i++;
}
$stats->loadDataForSubCategory();
$html = '
<style>
.red {
color :red
}
.stats td {
padding: 5px 10px;
}
.stats tr.grey {
background: #f1f1f1
}
.stats tr.blue {
background: #81c4f3;
color: #fff;
}
</style>
<fieldset style="font-size:1em;">
<legend>'.$this->l('Stats').' :</legend>';
$html.= '<h2>Rapport de vente : '.$stats->data_stats['name'].'</h2>';
$html.= '<table class="stats" style="width:45%; float:left">';
$html.= '<tr>';
$html.= '<td>'.$stats->lang['date_start'].'</td>';
$html.= '<td align="right">'.$stats->data_stats['date_start'].'</td>';
$html.= '</tr>';
$html.= '<tr>';
$html.= '<td>'.$stats->lang['date_end'].'</b></td>';
$html.= '<td align="right">'.$stats->data_stats['date_end'].'</td>';
$html.= '</tr>';
$html.= '<tr class="grey">';
$html.= '<td>'.$stats->lang['duration'].'</b></td>';
$html.= '<td align="right">'.$stats->data_stats['duration'].'</td>';
$html.= '</tr>';
$html.= '<tr class="blue">
<td colspan=2>STATS VENTES</td>
</tr>';
$html.= '<tr>';
$html.= '<td>'.$stats->lang['nb_ref'].'</td>';
$html.= '<td align="right">'.$stats->data_stats['nb_ref'].'</td>';
$html.= '</tr>';
$html.= '<tr>';
$html.= '<td>'.$stats->lang['nb_quantity_start'].'</td>';
$html.= '<td align="right">'.$stats->data_stats['nb_quantity_start'].'</td>';
$html.= '</tr>';
$html.= '<tr class="grey">';
$html.= '<td>'.$stats->lang['nb_order_simple'].'</td>';
$html.= '<td align="right">'.$stats->data_stats['nb_order_simple'].'</td>';
$html.= '</tr>';
$html.= '<tr class="grey">';
$html.= '<td>'.$stats->lang['nb_order_multi'].'</td>';
$html.= '<td align="right">'.$stats->data_stats['nb_order_multi'].'</td>';
$html.= '</tr>';
$html.= '<tr>';
$html.= '<td>'.$stats->lang['nb_order_total'].'</td>';
$html.= '<td align="right">'.$stats->data_stats['nb_order_total'].'</td>';
$html.= '</tr>';
$html.= '<tr>';
$html.= '<td>'.$stats->lang['taux_multi'].'</td>';
$html.= '<td align="right">'.$stats->data_stats['taux_multi'].'</td>';
$html.= '</tr>';
$html.= '<tr>';
$html.= '<td class="red">'.$stats->lang['quantity_sale'].'</td>';
$html.= '<td class="red" align="right">'.$stats->data_stats['quantity_sale'].'</td>';
$html.= '<tr class="grey">';
$html.= '<td class="red">'.$stats->lang['percent_sale'].'</td>';
$html.= '<td class="red" align="right">'.$stats->data_stats['percent_sale'].'</td>';
$html.= '</tr>';
$html.= '</tr>';
$html.= '<tr>';
$html.= '<td class="red">'.$stats->lang['percent_out_of_stock'].'</td>';
$html.= '<td class="red" align="right">'.$stats->data_stats['percent_out_of_stock'].'</td>';
$html.= '</tr>';
$html.= '<tr>';
$html.= '<td>'.$stats->lang['ca_ttc'].'</td>';
$html.= '<td align="right">'.Tools::displayPrice($stats->data_stats['ca_ttc']).'</td>';
$html.= '</tr>';
$html.= '<tr>';
$html.= '<tr>';
$html.= '<td class="red">'.$stats->lang['total_bdc_ht'].'</td>';
$html.= '<td class="red" align="right">'.Tools::displayPrice($stats->data_stats['total_bdc_ht']).'</td>';
$html.= '</tr>';
$html.= '<tr class="grey">';
$html.= '<td>'.$stats->lang['marge'].'</td>';
$html.= '<td align="right">'.$stats->data_stats['marge'].'</td>';
$html.= '</tr>';
$html.= '<tr class="blue">
<td colspan=2>BEST SELLERS</td>
</tr>';
for ($i=1; $i <= 3; $i++) {
$html.= '<tr class="grey">';
$html.= '<td>'.$stats->lang['bestsale_'.$i.'_product'].'</td>';
$html.= '<td align="right">'.$stats->data_stats['bestsale_'.$i.'_product'].'</td>';
$html.= '</tr>';
$html.= '<tr>';
$html.= '<td>'.$stats->lang['bestsale_'.$i.'_quantity'].'</td>';
$html.= '<td align="right">'.$stats->data_stats['bestsale_'.$i.'_quantity'].'</td>';
$html.= '</tr>';
$html.= '<tr>';
$html.= '<td>'.$stats->lang['bestsale_'.$i.'_ca'].'</td>';
$html.= '<td align="right">'.Tools::displayPrice($stats->data_stats['bestsale_'.$i.'_ca']).'</td>';
$html.= '</tr>';
}
$html.= '</table>';
$html.= '<table class="stats" style="width:45%; float:right">';
$html.= '<tr class="blue">';
$html.= '<td colspan="2">SOUS CATEGORIES</td>';
$html.= '</tr>';
$i = 0;
foreach ($stats->data_subcategories as $key => $subcategories) {
foreach ($subcategories as $key_2 => $subcategory) {
if ($i % 4 == 0) {
$html.= '<tr class="grey">';
} else {
$html.= '<tr>';
}
$html.= '<td>'.$stats->lang[$key_2].'</td>';
if ($key_2 == 'total_amount_wholesale_price') {
$html.= '<td>'.Tools::displayPrice($subcategory).'</td>';
} else {
$html.= '<td>'.$subcategory.'</td>';
}
$html.= '</tr>';
$i++;
}
}
$html.= '</table>
</fieldset>';
return $html;
}
public function getFormOrderForm($category)
{
global $link, $cookie;
require_once(_PS_ROOT_DIR_.'/modules/privatesales/Sale.php');
$sale = Sale::getSaleFromCategory($category->id);
$date = new DateTime($sale->date_start);
$date_end = new DateTime($sale->date_end);
$date_end->modify('+2 hour');
$date_first_of_month = $date->format('d/m/Y H:i:s');
$date_now = $date_end->format('d/m/Y H:i:s');
$export_list = OrderForm::getByCategorie($category->id);
if($export_list && count($export_list) > 0)
{
$old_date = OrderForm::getLastByCategory($category->id);
$old_date = new DateManager($old_date);
}
$html = '
<script type="text/javascript">
$(function(){
$(document).ready(function(){
$(".deleteOrderForm").click(function(){
var elem = $(this);
var id_order_form = $(this).attr("data-order-form");
if(confirm("'.$this->l('Êtes vous sur de vouloir supprimer ce bon de commande ?').'")) {
var link = "'.html_entity_decode(urldecode($link->getLanguageLinkAdmin($cookie->id_lang))).'&actionform=delete&idorderform="+id_order_form;
var popupnow = window.open(link, "deleteOrderForm"+id_order_form, "height=500, width=500, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no");
elem.parents("tr").first().remove();
setTimeout(function(){
popupnow.close();
}, 1000);
}
return false;
});
});
});
</script>
<fieldset style="font-size:1em;" id="export_form">
<legend>'.$this->l('Export').' :</legend>
<div style="width:450px;float:left;">
<div>
<h4>'.$this->l('Date d\'import').'</h4>
Du <input type="text" value="'.$date_first_of_month.'" size="20" id="hasDatepicker_1" style="text-align:center;">
<br />
Au <input type="text" value="'.$date_now.'" size="20" id="hasDatepicker_2" style="text-align:center;">
'.( count($export_list) > 0 ? '<br />
Ou
<br />
<input type="checkbox" id="export_last" value="1"> '.$this->l('depuis la génération du dernier bon').' : '.$old_date->getDate('STR') : '').'
<br />
<h4>'.$this->l('Selection du type de fichier').'</h4>
<input type="radio" name="export_type" value="1" checked> '.$this->l('Bon de commande').'
<br />
<input type="radio" name="export_type" value="2"> '.$this->l('Etat de la vente').'
<br />
<input type="radio" name="export_type" value="3"> '.$this->l('Stats de la vente').'
<br />
<input type="radio" name="export_type" value="4"> '.$this->l('BDC Logistique').'
<h4>'.$this->l('Selection de la langue').'</h4>
<input type="radio" name="lang" value="fr" checked> '.$this->l('Français').'
<br />
<input type="radio" name="lang" value="en"> '.$this->l('Anglais').'
';
$html .= '<br /><br />
<a href="" class="button" id="exportorderform">'.$this->l('Obtenir le fichier').'</a>
</div>';
$html .= '
</div>
<div style="width:300px;float:left;padding-left:30px">';
$html .= $this->getFormOrderFormInfo($category);
$html .= '
</div>
<div class="clear">
<br /><br />';
if($export_list && count($export_list) > 0)
{
$html .= '
<table class="table" cellspacing="0" cellpadding="0">
<thead><tr><th>'.$this->l('ID').'</th><th>'.$this->l('Date de d\'export').'</th><th>'.$this->l('A partir de').'</th><th>'.$this->l('Jusqu\'a').'</th><th style="text-align:center;">'.$this->l('Exporter à nouveau').'</th></tr></thead>
<tbody>';
foreach($export_list as $export)
{
$html .='<tr>
<td>'.$export['id_order_form'].'</td>
<td>'.$export['date_add'].'</td>
<td>'.$export['from'].'</td><td>'.$export['to'].'</td>
<td style="text-align:center;">';
// <a href="../modules/privatesales_logistique/exportOrderForm.php?action=getOrderForm&id_exportorderform='.$export['id_order_form'].'&type=1" title="'.$this->l('Exporter le bon de commande').'"><img src="../img/tmp/tab_mini_AdminSuppliers.gif"></a>
$html .='
<a href="../modules/privatesales_logistique/exportOrderForm.php?action=getOrderForm&id_exportorderform='.$export['id_order_form'].'&type=1&lang=fr" title="'.$this->l('Exporter le bon de commande').' FR">FR <img src="../img/admin/delivery.gif"></a>
<a href="../modules/privatesales_logistique/exportOrderForm.php?action=getOrderForm&id_exportorderform='.$export['id_order_form'].'&type=1&lang=en" title="'.$this->l('Exporter le bon de commande').' EN">EN <img src="../img/admin/delivery.gif"></a>
<a href="#" title="'.$this->l('Supprimer cette extraction').'" class="deleteOrderForm" data-order-form="'.$export['id_order_form'].'" ><img src="/img/admin/delete.gif"></a>
</td></tr>';
}
$html .= '</tbody>
</table>';
}
else
$html .= '<br /><br />'.$this->l('Il n\'y a pas d\'anciens exports de bon de commande');
$html .= '
</div>
</fieldset>';
return $html;
}
public function getFormOrderFormInfo()
{
global $cookie;
$html = '
<div id="toggle_form_infos" style="cursor:pointer;text-align:center;" class="button">Informations sur le bon de commande</div>';
$array_lang = array(
'fr', 'en'
);
$array = array(
'PS_IT_OF_ORGANISATION' => array( 'form_organisation', 'Organisation :' ),
'PS_IT_OF_SLOGAN' => array( 'form_slogan', 'Slogan :' ),
'PS_IT_OF_EMAIL' => array( 'form_email', 'Email :' ),
'PS_IT_OF_TVA' => array( 'form_tva', 'TVA :' ),
'PS_IT_OF_ADDRESS' => array( 'form_address', 'Adresse partie 1 :' ),
'PS_IT_OF_ADDRESS_2' => array( 'form_address_2', 'Adresse partie 2 :' ),
'PS_IT_OF_CP_CITY' => array( 'form_cp_city', 'CP et Ville :' ),
'PS_IT_OF_NUMBER_PHONE' => array( 'form_numero_tel', 'Numéro de téléphone :' ),
'PS_IT_OF_BAS_PAGE' => array( 'form_bas_page', 'Informations bas de page :', true ),
'PS_IT_OF_BAS_PAGE_BIS' => array( 'form_bas_page_bis', 'Informations bas de page Bis:', true ),
);
if(Tools::isSubmit('submitFormInfos'))
{
foreach($array_lang as $lang)
{
foreach($array as $k => $arr)
{
if(isset($arr[2]))
$html_ = true;
else $html_ = false;
Configuration::updateValue($k.'_'.$lang, Tools::getValue($arr[0].'_'.$lang), $html_);
}
}
Configuration::updateValue('PS_IT_OF_ORDER_STATES', implode(',', Tools::getValue('states', array() )) );
$html .= '<div class="conf">'.$this->l('Informations Sauvegardé').'</div>';
}
$html .= '
<form method="post" action="#" id="form_infos" style="display:none;">';
foreach($array_lang as $lang)
{
$html .= '<div><strong>'.$lang.' :</strong></div>';
foreach($array as $k => $arr)
{
${$arr[0].'_'.$lang} = Configuration::get($k.'_'.$lang);
$html .= '
<br />
<div>
'.$arr[1].' <br />
'.(isset($arr[2])
? '<textarea name="'.$arr[0].'_'.$lang.'" size="50">'.${$arr[0].'_'.$lang}.'</textarea>'
: '<input type="text" value="'.${$arr[0].'_'.$lang}.'" name="'.$arr[0].'_'.$lang.'" size="50">' ).'
</div>';
}
}
$order_state = OrderState::getOrderStates($cookie->id_lang);
$order_state_selected = explode(',', Configuration::get('PS_IT_OF_ORDER_STATES'));
$html .= '
<br />
<div>
'.$this->l('Statut des commandes que vous souhaitez traiter').' : <br /><br />
';
foreach($order_state as $state)
$html .= '<label for="order_state_'.$state['id_order_state'].'" style="float:none;padding:0;font-weight:normal;display:block;cursor:pointer;text-align:left;width:auto;">
<input type="checkbox" value="'.$state['id_order_state'].'" '.(in_array($state['id_order_state'], $order_state_selected) ? 'checked' : '').' id="order_state_'.$state['id_order_state'].'" name="states[]"> '.$state['name'].'
</label>';
$html .= '
</div>
<br />
<input type="submit" value="'.$this->l('Sauvegarder').'" name="submitFormInfos" class="button">
</form>';
return $html;
}
}
?>