bebeboutik/modules/braderie/AdminBraderie.php
2018-01-02 10:28:59 +01:00

507 lines
26 KiB
PHP

<?php
if(!defined('_PS_VERSION_')) {
exit;
}
require_once(PS_ADMIN_DIR . '/helpers/HelperFormBootstrap.php');
class AdminBraderie extends AdminTab
{
private $_html = '';
private $error = '';
public function display()
{
global $cookie;
$helperForm = new HelperFormBootstrap();
$helperForm->_select2 = true;
$id_category = Tools::getValue('id_category', 1);
$id_product = Tools::getValue('id_product', false);
$db = Db::getInstance();
$sql_category = '
SELECT c.`id_category`, cl.`name` FROM `'._DB_PREFIX_.'category` c
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON c.`id_category` = cl.`id_category`
WHERE c.`id_parent` = 1
AND cl.`id_lang` = '.(int) $cookie->id_lang.'
ORDER BY c.`id_category` DESC';
$categories = $db->ExecuteS($sql_category);
$ids_categories = array();
foreach ($categories as $key => $category) {
$ids_categories[] = (int) $category["id_category"];
}
$small_titles = array();
foreach (Db::getInstance()->executeS('
SELECT ex.`value`, ps.`id_category`
FROM `'._DB_PREFIX_.'privatesale_extrafield_sale` ex
LEFT JOIN `'._DB_PREFIX_.'privatesale` ps on ex.`id_sale` = ps.`id_sale`
LEFT JOIN `'._DB_PREFIX_.'category` c on c.`id_category` = ps.`id_category`
WHERE ex.`id_field` = 1
AND ex.`id_lang` = '. (int) $cookie->id_lang.'
AND c.`id_category` IN ('.implode(',', $ids_categories).')
') as $key => $data) {
$small_titles[(int) $data['id_category']] = $data['value'];
}
$this->_html .= $helperForm->renderStyle();
$this->_html .= $this->_buildHtmlCategoryBlock($categories, $small_titles);
$helperForm->_js .= '
<script type="text/javascript">
function addCaption(img, title){
return img+\'<div class="caption"><h5 class="text-center">\'+title+\'</h5><p class="text-center"><button type="button" class="btn btn-default" id="check_button">Vérifier</button></p></div>\';
}
$(function() {
var $token = "'.Tools::getAdminToken('AdminCatalog'.(int)(Tab::getIdFromClassName('AdminCatalog')).(int)($cookie->id_employee)).'";
var $product_id = 0;
$("#sale").select2({
allowClear: true
});
$(\'#sale\').change(function() {
$.ajax({
type: \'POST\',
url: \''._MODULE_DIR_.'braderie/ajax.php?action=getCategories'.'\',
dataType: \'json\',
data: { sale: $(this).val() },
success: function(response) {
$(\'#category\').empty();
for (var i in response) {
var html = \'<option value="\'+ response[i].id_category +\'">\'+ response[i].id_category +\' - \'+ response[i].name +\'</option>\';
$(\'#category\').append(html);
}
$(".chosen-select").trigger("chosen:updated");
$("#divider, #category_input_group, #ean_input_group, #storage_input_group, #button_input_group, #show_current_category_button, #quantity_input_group").show();
$("#error").empty();
$("#error").hide();
},
error: function(xhr) {
$("#ean_from_product").empty();
$("#ean_from_product").hide();
$("#error").empty().append(JSON.parse(xhr.responseText));
$("#error").show();
}
});
});
$("#category").change(function(event) {
$("#show_current_category_button").show();
$("#ean_input_group #input_ean").focus();
});
$("#input_ean").change(function(event) {
$("#loader").show();
$.ajax({
type: \'POST\',
url: \''._MODULE_DIR_.'braderie/ajax.php?action=getProductId'.'\',
dataType: \'json\',
data: { ean: $(this).val() },
success: function(response) {
$("#loader").hide();
if (response.name == null) {
$("#error").empty().append("'.$this->l('Aucun produit n\'a été trouvé. Vérifier le code EAN').'");
$("#error").show();
$("#ean_from_product").empty();
$("#ean_from_product").hide();
return false;
}
$product_id = response.id_product;
$attibute_id = response.id_product_attribute;
if (response.id_image == null) {
image = \'\';
} else {
image = \'<img src="'._THEME_PROD_DIR_.'\'+response.img_path+\'-product_carre.jpg" width="200px"/>\';
}
if (response.attribute_name) {
var caption = addCaption(image,response.name+" - "+response.attribute_name);
$("#ean_from_product").empty().append(caption);
$("#ean_from_product").show();
} else {
var caption = addCaption(image,response.name);
$("#ean_from_product").empty().append(caption);
$("#ean_from_product").show();
}
if ($attibute_id) {
$("#hidden_attribute_id").val($attibute_id);
} else {
$("#hidden_attribute_id").val(0);
}
$("#hidden_product_id").val($product_id);
if(response.comments != null){
tinymce.activeEditor.setContent(response.comments);
} else {
tinymce.activeEditor.setContent("");
}
$("#comment_input_group").show();
$("#error").empty();
$("#error").hide();
$("#input_storage").focus();
},
error: function(xhr) {
$("#loader").hide();
$("#ean_from_product").empty();
$("#ean_from_product").hide();
$("#input_comment").val("");
$("#comment_input_group").hide();
$("#error").empty().append(JSON.parse(xhr.responseText));
$("#error").show();
}
});
});
$(document).on("click", "#check_button", function(event) {
if ($product_id == undefined || $product_id == 0) {
$("#error").empty().append("'.$this->l('Aucun produit n\'a été trouvé. Vérifier le code EAN').'");
$("#error").show();
return false;
}
if ($token.length == 0) {
$("#error").empty().append("'.$this->l('Un problème de sécurité est survenu. Merci de réinitialisé la page').'");
$("#error").show();
return false;
}
window.open("?tab=AdminCatalog&updateproduct&id_product=" + $product_id + "&token=" + $token);
return false;
});
$("#input_storage").change(function(event) {
event.preventDefault();
if ($product_id == undefined || $product_id == 0) {
$("#error").empty().append("'.$this->l('Aucun produit n\'a été trouvé. Vérifier le code EAN').'");
$("#error").show();
return false;
}
var emplacement = $("#input_storage").val();
/* {rack}{etagere} ou {rack}{etagere}{carton} */
if (emplacement.length == 0 || emplacement>995) {
$("#error").empty().append("'.$this->l('L\'emplacement n\'est pas renseigné ou est mal renseigné').'");
$("#error").show();
return false;
}
if ($("#input_quantity").val().length == 0) {
$("#error").empty().append("'.$this->l('La quantité n\'est pas renseignée').'");
$("#error").show();
return false;
}
$("#hidden_product_id").val($product_id);
$("#loader").show();
$.ajax({
type: \'POST\',
url: \''._MODULE_DIR_.'braderie/ajax.php?action=addToSellout'.'\',
dataType: \'json\',
data: {
parent_category: $("#sale").val(),
category: $("#category").val(),
ean: $("#input_ean").val(),
storage: $("#input_storage").val(),
product: $("#hidden_product_id").val(),
quantity: $("#input_quantity").val(),
attribute: $("#hidden_attribute_id").val(),
comment: tinymce.activeEditor.getContent()
},
success: function(response) {
$("#loader").hide();
$("#error").empty().append(response);
$("#error").show();
$("#input_ean, #input_storage").val("");
$("#comment_input_group").hide();
$("#input_quantity").val(1);
$("#ean_from_product").empty();
$("#ean_from_product").hide();
$("#input_ean").focus();
},
error: function(xhr) {
$("#loader").hide();
$("#error").empty().append(JSON.parse(xhr.responseText));
$("#error").show();
}
});
return false;
});
// $("#formBraderie").submit(function(){
// return false;
// });
$("#add_button").click(function(event) {
event.preventDefault();
return false;
if ($product_id == undefined || $product_id == 0) {
$("#error").empty().append("'.$this->l('Aucun produit n\'a été trouvé. Vérifier le code EAN').'");
$("#error").show();
return false;
}
var emplacement = $("#input_storage").val();
if (emplacement.length == 0 || emplacement>505) {
$("#error").empty().append("'.$this->l('L\'emplacement n\'est pas renseigné ou est mal renseigné').'");
$("#error").show();
return false;
}
if ($("#input_quantity").val().length == 0) {
$("#error").empty().append("'.$this->l('La quantité n\'est pas renseignée').'");
$("#error").show();
return false;
}
$("#hidden_product_id").val($product_id);
$("#loader").show();
$.ajax({
type: \'POST\',
url: \''._MODULE_DIR_.'braderie/ajax.php?action=addToSellout'.'\',
dataType: \'json\',
data: {
parent_category: $("#sale").val(),
category: $("#category").val(),
ean: $("#input_ean").val(),
storage: $("#input_storage").val(),
product: $("#hidden_product_id").val(),
quantity: $("#input_quantity").val(),
attribute: $("#hidden_attribute_id").val()
},
success: function(response) {
$("#loader").hide();
$("#error").empty().append(response);
$("#error").show();
$("#input_ean, #input_storage").val("");
$("#ean_from_product").empty();
},
error: function(xhr) {
$("#loader").hide();
$("#error").empty().append(JSON.parse(xhr.responseText));
$("#error").show();
}
});
return false;
});
$("#show_current_category_button").click(function() {
$category = $("#category").val();
if ($category.length == 0) {
$("#error").empty().append("'.$this->l('Catégorie invalide').'");
$("#error").show();
return false;
}
window.open("?tab=AdminCatalog&viewcategory&id_category=" + $category + "&token=" + $token, "_blank");
return false;
});
});
</script>';
$this->_html .= $helperForm->renderScript();
$iso = Language::getIsoById((int)($cookie->id_lang));
$isoTinyMCE = (file_exists(_PS_ROOT_DIR_.'/js/tiny_mce/langs/'.$iso.'.js') ? $iso : 'en');
$ad = dirname($_SERVER["PHP_SELF"]);
$this->_html.= '
<script type="text/javascript">
var iso = \''.$isoTinyMCE.'\' ;
var pathCSS = \''._THEME_CSS_DIR_.'\' ;
var ad = \''.$ad.'\' ;
</script>
<script type="text/javascript" src="'.__PS_BASE_URI__.'js/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript" src="'.__PS_BASE_URI__.'js/tinymce.inc.js"></script>
<script type="text/javascript">
// other_textarea different config
$(function() {
tinymce.init({
mode : "specific_textareas",
theme : "advanced",
skin:"cirkuit",
editor_selector : "other_textarea",
editor_deselector : "noEditor",
plugins : "safari,pagebreak,style,table,advimage,advlink,inlinepopups,media,contextmenu,paste,fullscreen,xhtmlxtras,preview",
// Theme options
theme_advanced_buttons1 : "newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,,|,forecolor,backcolor",
theme_advanced_buttons3 : "",
theme_advanced_buttons4 : "",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_statusbar_location : "bottom",
theme_advanced_resizing : false,
content_css : pathCSS+"global.css",
document_base_url : ad,
width: "100",
height: "200",
font_size_style_values : "8pt, 10pt, 12pt, 14pt, 18pt, 24pt, 36pt",
elements : "nourlconvert,ajaxfilemanager",
file_browser_callback : "ajaxfilemanager",
entity_encoding: "raw",
convert_urls : false,
language : iso,
extended_valid_elements: "iframe[src,width,height,frameborder,marginwidth,marginheight],script[type,src]",
});
});
</script>';
echo $this->_html;
}
private function _buildHtmlCategoryBlock($categories, $small_titles)
{
global $cookie;
$token = Tools::getAdminToken('AdminBraderie'.(int)(Tab::getIdFromClassName('AdminBraderie')).(int)($cookie->id_employee));
$html = '
<div class="col-md-6 col-md-offset-3 text-center" style="padding:5px;margin-bottom: 10px;background: #504d8b;color: #fff; display:none" id="error"></div>
<div class="panel">
<div class="panel-title">
<h2><span class="anticon anticon-price-tags"></span> '.$this->l('Braderie').'</h2>
<div class="clearfix"></div>
</div>
<div class="panel-content">
<div class="row">
<div class="col-md-5">
<form id="formBraderie" action="?tab=AdminBraderie&token='.$token.'" method="POST">
<div class="form-group" id="ean_input_group" style="display: none;">
<div class="input-group">
<span class="input-group-addon"><i class="anticon anticon-barcode"></i></span>
<input type="text" class="form-control" id="input_ean" name="ean" placeholder="Sélection par EAN..."/>
</div>
</div>
<div id="divider" class="ln_solid" style="display: none;"></div>
<div class="form-group">
<label>'.$this->l('Ventes').' : </label>
<select class="form-control input-sm select2" id="sale" name="sale">
<option value="">Choisir une vente</option>';
foreach ($categories as $key => $value) {
$html .= '<option value="'.$value['id_category'].'">'.$value['id_category'].' - '.$value['name'].(!empty($small_titles[(int)$value['id_category']])?' - '.$small_titles[(int)$value['id_category']]:'').'</option>';
}
$html .= ' </select>
</div>
<div class="form-group" id="category_input_group" style="display: none;">
<label>'.$this->l('Catégorie').' : </label>
<div class="input-group">
<select class="form-control input-sm select2" id="category" name="category">
<option value="">Choisir une vente</option>
</select>
<span class="input-group-btn">
<a href="?tab=AdminCatalog" class="btn btn-default btn-sm" id="show_current_category_button" style="margin-left: 20px; display: none"><i class="glyphicon glyphicon-search"></i> '.$this->l('Voir').'</a>
</span>
</div>
</div>
<div class="form-group" id="quantity_input_group" style="display: none;">
<label >'.$this->l('Quantité').' : </label>
<input class="form-control" type="number" name="quantity" id="input_quantity" value="1">
</div>
<div class="form-group" id="storage_input_group" style="display: none;">
<label>'.$this->l('Emplacement').' : </label>
<input class="form-control" type="text" name="storage" id="input_storage">
</div>
<div class="form-group" id="comment_input_group" style="display: none;">
<label>'.$this->l('Commentaires').' : </label>
<textarea class="other_textarea form-control" cols="49" rows="3" id="input_comment" name="input_comment"></textarea>
</div>
<div class="form-group text-left" id="button_input_group" style="display: none;">
<input type="submit" class="btn btn-primary" name="adminbraderie" value="'.$this->l('Ajouter').'" id="add_button">
</div>
<input type="hidden" name="product" id="hidden_product_id">
<input type="hidden" name="attribute" id="hidden_attribute_id">
</form>
</div>
<div class="col-md-5 col-md-offset-2">
<div style="">
<div class="div-title">
<h4 style=""><span class="anticon anticon-target"></span> Produit</h4>
<div class="clearfix"></div>
</div>
<div class="col-md-6 col-md-offset-3 text-center" style="padding:5px;margin-bottom: 10px;color: #fff; display:none" id="loader"><img src="../img/loader_payment.gif" /></div>
<div class="thumbnail" id="ean_from_product" style="display:none;"></div>
</div>
</div>
</div>
</div>
</div>';
// $html = '
// <style>
// .chosen-container { width: 315px !important; }
// .button-container input { margin: 10px 25px; }
// #ean_from_product {
// background-color: rgba(86,84,133,0.7);
// color:#fff;
// margin-top: 5px;
// }
// #ean_from_product p{width: 330px;text-align: center;}
// #ean_from_product p,#ean_from_product img{
// display:inline-block;
// vertical-align:middle;
// margin: 5px;
// }
// #error, #ean_from_product { margin-left: 205px; width: 445px;}
// #error { font-weight: bold; margin-bottom: 20px; color: #CC0000; }
// .margin-form {padding: 0 0 1em 10px;}
// </style>
// <fieldset>
// <legend>'.$this->l('Braderie').'</legend>
// <form id="formBraderie" action="?tab=AdminBraderie&token='.$token.'" method="POST">';
// if ($this->error) {
// $html .= '<p class="margin-form" id="error">'.$this->error.'</p>';
// } else {
// $html .= '<p class="margin-form" id="error">'.$this->error.'</p>';
// }
// $html .='<div class="margin-form" id="ean_input_group" style="display: none;">
// <label>'.$this->l('Code EAN').' : </label>
// <input type="text" name="ean" id="input_ean">
// <div id="ean_from_product"></div>
// </div>
// <hr id="divider" style="display: none;">';
// $html .= '<div class="margin-form">
// <label>'.$this->l('Ventes').' : </label>
// <select class="chosen-select" id="sale" name="sale">
// <option value=""></option>';
// foreach ($categories as $key => $value) {
// $html .= '<option value="'.$value['id_category'].'">'.$value['id_category'].' - '.$value['name'].(!empty($small_titles[(int)$value['id_category']])?' - '.$small_titles[(int)$value['id_category']]:'').'</option>';
// }
// $html .= ' </select>
// </div>
// <div class="margin-form" id="category_input_group" style="display: none;">
// <label>'.$this->l('Catégories').' : </label>
// <select class="chosen-select" id="category" name="category">
// <option value=""></option>
// </select>
// <a href="?tab=AdminCatalog" class="button" id="show_current_category_button" style="margin-left: 20px; display: none">'.$this->l('Voir cette catégorie').'</a>
// </div>
// <div class="margin-form" id="quantity_input_group" style="display: none;">
// <label>'.$this->l('Quantité').' : </label>
// <input type="number" name="quantity" id="input_quantity" value="1">
// </div>
// <div class="margin-form" id="storage_input_group" style="display: none;">
// <label>'.$this->l('Emplacement').' : </label>
// <input type="text" name="storage" id="input_storage">
// </div>
// <div class="margin-form" id="comment_input_group" style="display: none;">
// <label>'.$this->l('Commentaires').' : </label>
// <textarea class="other_textarea" cols="49" rows="3" id="input_comment" name="input_comment"></textarea>
// </div>
// <div class="margin-form button-container" id="button_input_group" style="display: none;">
// <input type="submit" class="button" name="adminbraderie" value="'.$this->l('Ajouter').'" style="margin-left: 200px;" id="add_button">
// <input type="submit" class="button" id="check_button" value="'.$this->l('Vérifier').'">
// </div>
// <input type="hidden" name="product" id="hidden_product_id">
// <input type="hidden" name="attribute" id="hidden_attribute_id">
// </form>
// </fieldset>';
return $html;
}
}