1089 lines
42 KiB
JavaScript
1089 lines
42 KiB
JavaScript
// SEARCH
|
|
var contratsSearch = false;
|
|
|
|
// VIEW
|
|
var modalViewContrat = false;
|
|
var btnViewContrat = false;
|
|
var currentViewContrat = 0;
|
|
|
|
var contratsBaseURL = "contrats.php";
|
|
|
|
// ADD
|
|
var modalAddContrat = false;
|
|
var formAddContrat = false;
|
|
var btnAddContrat = false;
|
|
|
|
// EDIT
|
|
var modalEditContrat = false;
|
|
var formEditContrat = false;
|
|
var btnEditContrat = false;
|
|
var currentEditContrat = 0;
|
|
|
|
// ARCHIVE
|
|
var modalArchiveContrat = false;
|
|
var btnArchiveContrat = false;
|
|
var btnUnarchiveContrat = false;
|
|
var currentArchiveContrat = 0;
|
|
|
|
// DELETE
|
|
var modalDeleteContrat = false;
|
|
var btnDeleteContrat = false;
|
|
var currentDeleteContrat = 0;
|
|
|
|
$(document).ready( function() {
|
|
// console.log("INIT CONTRATS");
|
|
|
|
// INIT SEARCH
|
|
contratsSearch = $("#contratsSearch");
|
|
if(contratsSearch.length>0) initSearchContrats();
|
|
|
|
// INIT VIEW CONTRAT
|
|
modalViewContrat = $("#modalViewContrat");
|
|
btnViewContrat = $(".btnViewContrat");
|
|
if(modalViewContrat.length>0 && btnViewContrat.length>0) {
|
|
initViewContrat();
|
|
// VIEW REQUEST
|
|
var id = getUrlParameter("id");
|
|
if(id!==false && parseInt(id)>0) loadDatasInViewContratModal(id);
|
|
}
|
|
|
|
// INIT ADD CONTRAT
|
|
modalAddContrat = $("#modalAddContrat");
|
|
formAddContrat = $("#formAddContrat");
|
|
btnAddContrat = $("#btnAddContrat");
|
|
if(modalAddContrat.length>0 && formAddContrat.length>0 && btnAddContrat.length>0) initAddContrat();
|
|
|
|
// INIT EDIT CONTRAT
|
|
modalEditContrat = $("#modalEditContrat");
|
|
formEditContrat = $("#formEditContrat");
|
|
btnEditContrat = $(".btnEditContrat");
|
|
if(modalEditContrat.length>0 && formEditContrat.length>0 && btnEditContrat.length>0) initEditContrat();
|
|
|
|
// INIT ARCHIVE CONTRAT
|
|
modalArchiveContrat = $("#modalArchiveContrat");
|
|
btnArchiveContrat = $(".btnArchiveContrat");
|
|
btnUnarchiveContrat = $(".btnUnarchiveContrat");
|
|
if(modalArchiveContrat.length>0 && (btnArchiveContrat.length>0 || btnUnarchiveContrat.length>0)) initArchiveContrat();
|
|
|
|
// INIT DELETE CONTRAT
|
|
modalDeleteContrat = $("#modalDeleteContrat");
|
|
btnDeleteContrat = $(".btnDeleteContrat");
|
|
if(modalDeleteContrat.length>0 && btnDeleteContrat.length>0) initDeleteContrat();
|
|
});
|
|
|
|
/***** SEARCH CONTRATS *****/
|
|
function initSearchContrats() {
|
|
// console.log("INIT SEARCH CONTRATS");
|
|
// SEARCH
|
|
contratsSearch.find("input[name=search]").unbind('keypress').keypress( function(event) {
|
|
if(event.keyCode==13) searchContrat(false);
|
|
});
|
|
contratsSearch.find(".btnClearSearch").unbind('click').click( function(event) {
|
|
searchContrat(true);
|
|
});
|
|
|
|
// FILTRE GROUPE
|
|
loadValue = contratsSearch.find("select[name=groupe]").attr("load_value");
|
|
contratsSearch.find("select[name=groupe]").change(function(e) {
|
|
$(this).blur();
|
|
document.location = "?groupe="+parseInt($(this).val());
|
|
}).val(loadValue);
|
|
|
|
// FILTRE FREQUENCE
|
|
loadValue = contratsSearch.find("select[name=frequence]").attr("load_value");
|
|
contratsSearch.find("select[name=frequence]").change(function(e) {
|
|
$(this).blur();
|
|
document.location = "?frequence="+$(this).val();
|
|
}).val(loadValue);
|
|
|
|
// FILTRE PANIER
|
|
loadValue = contratsSearch.find("select[name=panier]").attr("load_value");
|
|
contratsSearch.find("select[name=panier]").change(function(e) {
|
|
$(this).blur();
|
|
document.location = "?panier="+$(this).val();
|
|
}).val(loadValue);
|
|
|
|
// FILTRE LIEU
|
|
loadValue = contratsSearch.find("select[name=lieu]").attr("load_value");
|
|
contratsSearch.find("select[name=lieu]").change(function(e) {
|
|
$(this).blur();
|
|
document.location = "?lieu="+parseInt($(this).val());
|
|
}).val(loadValue);
|
|
|
|
// FILTRE ARCHIVE
|
|
loadValue = contratsSearch.find("select[name=archive]").attr("load_value");
|
|
contratsSearch.find("select[name=archive]").change(function(e) {
|
|
$(this).blur();
|
|
document.location = "?archive="+parseInt($(this).val());
|
|
}).val(loadValue);
|
|
}
|
|
|
|
function searchContrat(clear) {
|
|
contratsSearch.find("input[name=search]").blur();
|
|
contratsSearch.find(".btnClearSearch").blur();
|
|
if(clear) document.location = "?clearSearch";
|
|
else document.location = "?search="+contratsSearch.find("input[name=search]").val();
|
|
}
|
|
|
|
/***** VIEW CONTRAT *****/
|
|
function initViewContrat() {
|
|
// console.log("INIT VIEW CONTRAT");
|
|
|
|
btnViewContrat.unbind('click').click( function(event) {
|
|
event.preventDefault();
|
|
id = parseInt($(this).attr('ref'));
|
|
if(!id>0) return;
|
|
clearViewContratModal();
|
|
currentViewContrat = id;
|
|
loadDatasInViewContratModal(id);
|
|
});
|
|
|
|
var form = modalViewContrat.find("div.formContrat");
|
|
|
|
// TABS
|
|
form.find("a.formContratTabBtn").click(function(e) {
|
|
e.preventDefault();
|
|
|
|
// BTN
|
|
if(!$(this).parent().hasClass("active")) {
|
|
form.find("ul.formContratTabs > li").removeClass('active');
|
|
$(this).parent().addClass("active");
|
|
}
|
|
|
|
// TAB
|
|
var tab = form.find("div.tab."+$(this).attr("tab"));
|
|
if(tab.length>0) {
|
|
if(tab.hasClass("active")) return;
|
|
|
|
form.find("div.formContratTabs > div.tab").removeClass("active");
|
|
tab.addClass("active");
|
|
}
|
|
});
|
|
resizeFormContrat(form);
|
|
$( window ).on( "resize", function() { resizeFormContrat(form) });
|
|
|
|
|
|
// CANCEL
|
|
modalViewContrat.on('hidden.bs.modal', function (e) {
|
|
clearViewContratModal();
|
|
currentViewContrat = 0;
|
|
});
|
|
}
|
|
|
|
function resizeFormContrat(form) {
|
|
var wh = window.innerHeight;
|
|
form.find("div.formContratTabs div.tab").css("max-height", (wh-180)+"px");
|
|
}
|
|
|
|
function loadDatasInViewContratModal(id) {
|
|
datas = {
|
|
'ref' : id,
|
|
'action' : 'getDatas'
|
|
};
|
|
$.post( contratsBaseURL, datas, function( result ) {
|
|
var datas = JSON.parse(result);
|
|
console.log(datas);
|
|
|
|
modalViewContrat.find("small.db_ref > span").html(datas.ref);
|
|
|
|
modalViewContrat.find("td.type").html(datas.type_nom);
|
|
|
|
client = datas.client_prenom+" "+datas.client_nom;
|
|
if((datas.client_tel!="" && datas.client_tel!=null) || (datas.client_email!="" && datas.client_email!=null)) {
|
|
client += " <small>(";
|
|
if(datas.client_tel!="" && datas.client_tel!=null) {
|
|
client += '<a href="tel:'+datas.client_tel+'">'+datas.client_tel+'</a>';
|
|
if(datas.client_email!="" && datas.client_email!=null) client += ' / <a href="mailto:'+datas.client_email+'">'+datas.client_email+'</a>';
|
|
}
|
|
else if(datas.client_email!="" && datas.client_email!=null) client += '<a href="mailto:'+datas.client_email+'">'+datas.client_email+'</a>';
|
|
client += ")</small>";
|
|
}
|
|
modalViewContrat.find("td.client").html(client);
|
|
modalViewContrat.find("td.tel").html((datas.client_tel!="") ? '<a href="tel:'+datas.client_tel+'">'+datas.client_tel+'</a>' : '.');
|
|
modalViewContrat.find("td.email").html((datas.client_email!="") ? '<a href="mailto:'+datas.client_email+'">'+datas.client_email+'</a>' : '.');
|
|
|
|
modalViewContrat.find("td.date").html(datas.date_print);
|
|
modalViewContrat.find("td.frequence").html(datas.frequence_print);
|
|
modalViewContrat.find("td.paniers").html(datas.nb_panier + datas.panier_type);
|
|
modalViewContrat.find("td.panier_type").html(datas.panier_type);
|
|
modalViewContrat.find("td.lieu_depot").html(datas.lieu_depot_nom);
|
|
modalViewContrat.find("td.nb_cheque").html(datas.nb_cheque);
|
|
|
|
nb_panier_livre = datas.nb_paniers_livres + " / " + datas.nb_paniers;
|
|
if(datas.np_paniers_distrib_avt_saisie>0) {
|
|
nb_panier_livre += " <small>(dont "+datas.np_paniers_distrib_avt_saisie+" livré";
|
|
if(datas.np_paniers_distrib_avt_saisie>1) nb_panier_livre += "s";
|
|
nb_panier_livre += " avant la saisie du contrat)</small>";
|
|
}
|
|
modalViewContrat.find("td.nb_panier_livre").html(nb_panier_livre);
|
|
modalViewContrat.find("td.nb_panier_restant").html(datas.nb_paniers_restants + " / " + datas.nb_paniers);
|
|
|
|
// PANIERS
|
|
$.post(contratsBaseURL, { 'ref' : id, 'action' : 'modalView_getTab', 'tab' : 'paniers' }, function(result) {
|
|
modalViewContrat.find("div.tabPaniers").html(result);
|
|
}).fail(function() { alert(srvErrorMsg+" (load modal view client - tab paniers)"); });
|
|
|
|
modalViewContrat.modal('show');
|
|
}).fail(function() { alert(srvErrorMsg+" (load modal view contrat)"); });
|
|
}
|
|
|
|
function clearViewContratModal() {
|
|
modalViewContrat.find("small.db_ref > span").html("");
|
|
|
|
modalViewContrat.find("ul.formContratTabs > li").removeClass('active hide');
|
|
modalViewContrat.find("ul.formContratTabs > li:first-child").addClass('active');
|
|
modalViewContrat.find("div.formContratTabs > div.tab").removeClass("active");
|
|
modalViewContrat.find("div.formContratTabs > div.tab:first-child").addClass("active");
|
|
|
|
modalViewContrat.find("td.nom").html("");
|
|
modalViewContrat.find("td.tel").html("");
|
|
modalViewContrat.find("td.email").html("");
|
|
|
|
modalViewContrat.find("td.date").html("");
|
|
modalViewContrat.find("td.frequence").html("");
|
|
modalViewContrat.find("td.nb_panier").html("");
|
|
modalViewContrat.find("td.panier_type").html("");
|
|
modalViewContrat.find("td.lieu_depot").html("");
|
|
modalViewContrat.find("td.nb_cheque").html("");
|
|
|
|
modalViewContrat.find("td.nb_panier_livre").html("");
|
|
modalViewContrat.find("td.nb_panier_restant").html("");
|
|
|
|
modalViewContrat.find("div.tabPaniers").html("");
|
|
}
|
|
|
|
/***** FORM CONTRAT *****/
|
|
function initFormContrat(form) {
|
|
form.on("submit", function(e) { e.preventDefault(); });
|
|
|
|
// CLIENT
|
|
form.find("input[name=client]").autocomplete({
|
|
html: true,
|
|
delay: 200,
|
|
source: function(requete, reponse) {
|
|
datas = {
|
|
'action' : 'autocomplete_list',
|
|
'search' : form.find("input[name=client]").val(),
|
|
'nohist' : true
|
|
};
|
|
$.post("clients.php", datas, function(jsonTxt) {
|
|
datas = JSON.parse(jsonTxt);
|
|
reponse(datas);
|
|
}).fail(function() { alert(srvErrorMsg+" (form contrat - client autocomplete)"); });
|
|
},
|
|
response: function( event, ui ) {
|
|
//console.log('ui',ui);
|
|
},
|
|
focus: function(event, ui) {
|
|
event.preventDefault();
|
|
},
|
|
select: function(event, ui) {
|
|
event.preventDefault();
|
|
form.find("input[name=client]").val( ui.item.value ).attr('ref', ui.item.ref).blur();
|
|
}
|
|
});
|
|
form.find("input[name=client]").keydown(function(e) {
|
|
if(e.keyCode==13) { e.preventDefault(); $(this).blur(); }
|
|
else if(parseInt($(this).attr('ref'))>0) $(this).val("").removeAttr('ref');
|
|
});
|
|
form.find("input[name=client]").on("blur", function(e) {
|
|
console.log("blured", $(this).attr('ref'), parseInt($(this).attr('ref'))>0);
|
|
if(!parseInt($(this).attr('ref'))>0) $(this).val("").removeAttr('ref');
|
|
});
|
|
|
|
// TYPE
|
|
form.find("select[name=type]").change(function(e) {
|
|
$(this).blur();
|
|
// CUSTOM
|
|
if($(this).val() == "custom") {
|
|
form.find("div.custom_type").removeClass("hide");
|
|
|
|
switchBtnContratTypeAction(form, "save_new");
|
|
switchBtnContratTypeSecondAction(form, "hide");
|
|
}
|
|
// EXISTING
|
|
else if(parseInt($(this).val())>0) {
|
|
switchBtnContratTypeAction(form, "delete");
|
|
switchBtnContratTypeSecondAction(form, "edit");
|
|
|
|
form.find("div.custom_type").addClass("hide");
|
|
|
|
sel = $(this).find("option:selected");
|
|
grp = sel.attr('grp');
|
|
freq = sel.attr('freq');
|
|
panier = parseInt( sel.attr('panier') );
|
|
nb_paniers = parseInt( sel.attr('nb_paniers') );
|
|
|
|
form.find("select[name=groupe]").val(grp);
|
|
form.find("select[name=frequence]").val(freq);
|
|
form.find("select[name=panier_type]").val(panier);
|
|
form.find("input[name=nb_paniers]").val(nb_paniers);
|
|
|
|
// BTN ACTION
|
|
switchBtnContratTypeAction(form, "delete");
|
|
|
|
// ADD BTN EDIT
|
|
|
|
// QUINZE-GROUPE
|
|
if(freq=="quinz") form.find("select[name=quinz_groupe]").parent().removeClass("hide");
|
|
else form.find("select[name=quinz_groupe]").parent().addClass("hide");
|
|
}
|
|
// CHOISIR...
|
|
else {
|
|
switchBtnContratTypeAction(form, "add");
|
|
switchBtnContratTypeSecondAction(form, "hide");
|
|
}
|
|
});
|
|
|
|
form.find("button.btnContratTypeAction").click(function(e) {
|
|
e.preventDefault();
|
|
$(this).blur();
|
|
// ADD / SAVE NEW
|
|
if($(this).hasClass("add") || $(this).hasClass("save_new")) {
|
|
addInputContratTypeName(form, "add");
|
|
form.find("div.custom_type").removeClass("hide");
|
|
|
|
switchBtnContratTypeAction(form, "save");
|
|
switchBtnContratTypeSecondAction(form, "cancel");
|
|
}
|
|
// SAVE
|
|
else if($(this).hasClass("save")) {
|
|
var ipt = form.find("input[name=contratTypeName]");
|
|
if(ipt.length>0) {
|
|
var ref = ipt.attr("ref");
|
|
|
|
var datas = {
|
|
'nom' : ipt.val(),
|
|
'groupe' : parseInt(form.find("select[name=groupe]").val()),
|
|
'frequence' : form.find("select[name=frequence]").val(),
|
|
'panier_type' : parseInt(form.find("select[name=panier_type]").val()),
|
|
'panier_type_valeur' : parseFloat(form.find("select[name=panier_type] option:selected").attr("valeur")),
|
|
'nb_paniers' : parseInt(form.find("input[name=nb_paniers]").val())
|
|
};
|
|
|
|
if(datas.nom=="" || !datas.groupe>0 || (datas.frequence!="hebdo" && datas.frequence!="quinz") || !datas.panier_type>0 || !datas.nb_paniers>0 || !datas.panier_type_valeur>0) {
|
|
alert("Merci de renseigner tous les critères du contrat !");
|
|
return;
|
|
}
|
|
|
|
datas.prix_total = datas.panier_type_valeur * datas.nb_paniers;
|
|
|
|
// ADD CONTRAT TYPE
|
|
if(ref == "new") datas.action = "add_contrats_type";
|
|
// EDIT CONTRAT TYPE
|
|
else if(parseInt(ref)>0) {
|
|
ref = parseInt(ref);
|
|
datas.action = "edit_contrats_type";
|
|
datas.type_ref = ref;
|
|
}
|
|
|
|
form.find("div.modaLoader").addClass("show");
|
|
form.find("button.btnContratTypeAction").prop("disabled", true);
|
|
|
|
$.post( contratsBaseURL, datas, function( result ) {
|
|
if(parseInt(result)>0) {
|
|
switchBtnContratTypeAction(form, "delete");
|
|
switchBtnContratTypeSecondAction(form, "edit");
|
|
removeInputContratTypeName(form);
|
|
form.find("div.custom_type").addClass("hide");
|
|
|
|
if(ref == "new") ref = parseInt(result);
|
|
|
|
// REFRESH CONTRATS TYPES LIST
|
|
$.post( contratsBaseURL, {action: "contrats_types_select_list"}, function( result ) {
|
|
form.find("select[name=type]").html(result).val(ref);
|
|
}).fail(function() { alert(srvErrorMsg+" (form contrat - add/edit contrat type - refresh list after)"); });
|
|
}
|
|
else {
|
|
console.error(result);
|
|
alert(result);
|
|
}
|
|
}).always(function() {
|
|
form.find("div.modaLoader").removeClass("show");
|
|
form.find("button.btnContratTypeAction").prop("disabled", false);
|
|
}).fail(function() { alert(srvErrorMsg+" (form contrat - add/edit contrat type)"); });
|
|
}
|
|
}
|
|
// DELETE
|
|
else if($(this).hasClass("delete")) {
|
|
var ref = parseInt(form.find("select[name=type]").val());
|
|
var nom = form.find("select[name=type] option:selected").html();
|
|
if(confirm('Etês vous sûr de vouloir supprimer le type de contrat "'+nom+'" ?')) {
|
|
var datas = { action : 'delete_contrats_type', type_ref : ref };
|
|
$.post( contratsBaseURL, datas, function( result ) {
|
|
if(parseInt(result)>0) {
|
|
// REFRESH CONTRATS TYPES LIST
|
|
$.post( contratsBaseURL, {action: "contrats_types_select_list"}, function( result ) {
|
|
form.find("select[name=type]").html(result).val("0");
|
|
}).fail(function() { alert(srvErrorMsg+" (form contrat - delete contrat type - refresh list after)"); });
|
|
}
|
|
else {
|
|
console.error(result);
|
|
alert(result);
|
|
}
|
|
}).always(function() {
|
|
form.find("div.modaLoader").removeClass("show");
|
|
form.find("button.btnContratTypeAction").prop("disabled", false);
|
|
}).fail(function() { alert(srvErrorMsg+" (form contrat - delete contrat type)"); });
|
|
}
|
|
}
|
|
});
|
|
|
|
form.find("button.btnContratTypeSecondAction").click(function(e) {
|
|
e.preventDefault();
|
|
$(this).blur();
|
|
// EDIT
|
|
if($(this).hasClass("edit")) {
|
|
addInputContratTypeName(form, "edit");
|
|
switchBtnContratTypeAction(form, "save");
|
|
switchBtnContratTypeSecondAction(form, "cancel");
|
|
form.find("div.custom_type").removeClass("hide");
|
|
}
|
|
// CANCEL
|
|
else if($(this).hasClass("cancel")) {
|
|
removeInputContratTypeName(form);
|
|
type = form.find("select[name=type]").val();
|
|
// EDIT
|
|
if(parseInt(type)>0) {
|
|
switchBtnContratTypeAction(form, "delete");
|
|
switchBtnContratTypeSecondAction(form, "edit");
|
|
form.find("div.custom_type").addClass("hide");
|
|
}
|
|
// CUSTOM
|
|
else if(type == "custom") {
|
|
switchBtnContratTypeAction(form, "save_new");
|
|
form.find("div.custom_type").removeClass("hide");
|
|
}
|
|
// ADD
|
|
else {
|
|
switchBtnContratTypeAction(form, "add");
|
|
switchBtnContratTypeSecondAction(form, "hide");
|
|
form.find("div.custom_type").addClass("hide");
|
|
}
|
|
}
|
|
});
|
|
|
|
// GROUPE
|
|
form.find("select[name=groupe]").change(function(e) { refreshPaniersTypesSelectList(form, $(this).val()); });
|
|
|
|
// FREQUENCE
|
|
form.find("select[name=frequence]").change(function(e) {
|
|
freq = $(this).val();
|
|
if(freq=="quinz") form.find("select[name=quinz_groupe]").parent().removeClass("hide");
|
|
else form.find("select[name=quinz_groupe]").parent().addClass("hide");
|
|
});
|
|
|
|
// PANIERS TYPE
|
|
form.find("select[name=panier_type]").change(function(e) { $(this).blur(); });
|
|
|
|
// LIEU DEPOT
|
|
form.find("select[name=lieu_depot]").change(function(e) {
|
|
$(this).blur();
|
|
// EXISTING
|
|
if(parseInt($(this).val())>0) {
|
|
switchBtnLieuAction(form, "delete");
|
|
switchBtnLieuSecondAction(form, "edit");
|
|
}
|
|
// CHOISIR...
|
|
else {
|
|
switchBtnLieuAction(form, "add");
|
|
switchBtnLieuSecondAction(form, "hide");
|
|
}
|
|
});
|
|
|
|
form.find("button.btnLieuAction").click(function(e) {
|
|
e.preventDefault();
|
|
$(this).blur();
|
|
// ADD / SAVE NEW
|
|
if($(this).hasClass("add") || $(this).hasClass("save_new")) {
|
|
addInputLieuName(form, "add");
|
|
|
|
switchBtnLieuAction(form, "save");
|
|
switchBtnLieuSecondAction(form, "cancel");
|
|
}
|
|
// SAVE
|
|
else if($(this).hasClass("save")) {
|
|
var ipt = form.find("input[name=lieuName]");
|
|
if(ipt.length>0) {
|
|
var ref = ipt.attr("ref");
|
|
|
|
var datas = { 'nom' : ipt.val() };
|
|
|
|
if(datas.nom=="") {
|
|
alert("Merci de renseigner le nom du lieu !");
|
|
return;
|
|
}
|
|
|
|
// ADD LIEU
|
|
if(ref == "new") datas.action = "add_lieu";
|
|
// EDIT LIEU
|
|
else if(parseInt(ref)>0) {
|
|
ref = parseInt(ref);
|
|
datas.action = "edit_lieu";
|
|
datas.lieu_ref = ref;
|
|
}
|
|
|
|
form.find("div.modaLoader").addClass("show");
|
|
form.find("button.btnLieuAction").prop("disabled", true);
|
|
|
|
$.post( contratsBaseURL, datas, function( result ) {
|
|
if(parseInt(result)>0) {
|
|
switchBtnLieuAction(form, "delete");
|
|
switchBtnLieuSecondAction(form, "edit");
|
|
removeInputLieuName(form);
|
|
|
|
if(ref == "new") ref = parseInt(result);
|
|
|
|
// REFRESH LIEUX LIST
|
|
$.post( contratsBaseURL, {action:"lieux_select_list"}, function( result ) {
|
|
form.find("select[name=lieu_depot]").html(result).val(ref);
|
|
}).fail(function() { alert(srvErrorMsg+" (form contrat - add/edit lieu - refresh list after)"); });
|
|
}
|
|
else {
|
|
console.error(result);
|
|
alert(result);
|
|
}
|
|
}).always(function() {
|
|
form.find("div.modaLoader").removeClass("show");
|
|
form.find("button.btnLieuAction").prop("disabled", false);
|
|
}).fail(function() { alert(srvErrorMsg+" (form contrat - add/edit lieu)"); });
|
|
}
|
|
}
|
|
// DELETE
|
|
else if($(this).hasClass("delete")) {
|
|
var ref = parseInt(form.find("select[name=lieu_depot]").val());
|
|
var nom = form.find("select[name=lieu_depot] option:selected").html();
|
|
if(confirm('Etês vous sûr de vouloir supprimer le lieu de dépôt "'+nom+'" ?')) {
|
|
form.find("div.modaLoader").addClass("show");
|
|
form.find("button.btnLieuAction").prop("disabled", false);
|
|
|
|
var datas = { action : 'delete_lieu', lieu_ref : ref };
|
|
$.post( contratsBaseURL, datas, function( result ) {
|
|
if(parseInt(result)>0) {
|
|
// REFRESH CONTRATS TYPES LIST
|
|
$.post( contratsBaseURL, {action: "lieux_select_list"}, function( result ) {
|
|
form.find("select[name=lieu_depot]").html(result).val("0");
|
|
}).fail(function() { alert(srvErrorMsg+" (form contrat - delete lieu - refresh list after)"); });
|
|
}
|
|
else {
|
|
console.error(result);
|
|
alert(result);
|
|
}
|
|
}).always(function() {
|
|
form.find("div.modaLoader").removeClass("show");
|
|
form.find("button.btnLieuAction").prop("disabled", false);
|
|
}).fail(function() { alert(srvErrorMsg+" (form contrat - delete lieu)"); });
|
|
}
|
|
}
|
|
});
|
|
|
|
form.find("button.btnLieuSecondAction").click(function(e) {
|
|
e.preventDefault();
|
|
$(this).blur();
|
|
// EDIT
|
|
if($(this).hasClass("edit")) {
|
|
addInputLieuName(form, "edit");
|
|
switchBtnLieuAction(form, "save");
|
|
switchBtnLieuSecondAction(form, "cancel");
|
|
}
|
|
// CANCEL
|
|
else if($(this).hasClass("cancel")) {
|
|
removeInputLieuName(form);
|
|
// EDIT
|
|
if(parseInt(form.find("select[name=lieu_depot]").val())>0) {
|
|
switchBtnLieuAction(form, "delete");
|
|
switchBtnLieuSecondAction(form, "edit");
|
|
}
|
|
// ADD
|
|
else {
|
|
switchBtnLieuAction(form, "add");
|
|
switchBtnLieuSecondAction(form, "hide");
|
|
}
|
|
}
|
|
});
|
|
|
|
// NB PANIERS
|
|
initIntInput(form.find("input[name=nb_paniers]"));
|
|
|
|
// NB CHEQUE
|
|
initIntInput(form.find("input[name=nb_cheque]"));
|
|
|
|
// NB PANIER AVANT SAISIE
|
|
initIntInput(form.find("input[name=np_paniers_distrib_avt_saisie]"));
|
|
}
|
|
|
|
function loadDatasInFormContratDatas(modal,form,id) {
|
|
datas = {
|
|
'ref' : id,
|
|
'action' : 'getDatas'
|
|
};
|
|
$.post( contratsBaseURL, datas, function( result ) {
|
|
//console.log(result);
|
|
var datas = JSON.parse(result);
|
|
console.log(datas);
|
|
|
|
form.find("input[name=client]").val(datas.client_prenom+" "+datas.client_nom).attr('ref', datas.client_ref);
|
|
|
|
if(parseInt(datas.type_ref) > 0) {
|
|
form.find("select[name=type]").val(datas.type_ref);
|
|
switchBtnContratTypeAction(form, "delete");
|
|
switchBtnContratTypeSecondAction(form, "edit");
|
|
form.find("div.custom_type").addClass("hide");
|
|
}
|
|
else {
|
|
form.find("select[name=type]").val("custom");
|
|
switchBtnContratTypeAction(form, "save_new");
|
|
switchBtnContratTypeSecondAction(form, "hide");
|
|
form.find("div.custom_type").removeClass("hide");
|
|
}
|
|
|
|
form.find("select[name=groupe]").val(datas.groupe_ref);
|
|
form.find("select[name=frequence]").val(datas.frequence);
|
|
form.find("select[name=panier_type]").val(datas.panier_type_ref);
|
|
form.find("input[name=nb_paniers]").val(datas.nb_paniers);
|
|
|
|
if(datas.frequence == "quinz") form.find("select[name=quinz_groupe]").parent().removeClass("hide");
|
|
form.find("select[name=quinz_groupe]").val(datas.quinz_groupe);
|
|
|
|
form.find("input[name=date]").val(datas.date);
|
|
|
|
if(parseInt(datas.lieu_depot_ref) > 0) {
|
|
form.find("select[name=lieu_depot]").val(datas.lieu_depot_ref);
|
|
switchBtnLieuAction(form, "delete");
|
|
switchBtnLieuSecondAction(form, "edit");
|
|
}
|
|
else {
|
|
form.find("select[name=lieu_depot]").val("0");
|
|
switchBtnLieuAction(form, "add");
|
|
switchBtnLieuSecondAction(form, "hide");
|
|
}
|
|
|
|
form.find("input[name=nb_cheque]").val(datas.nb_cheque);
|
|
form.find("input[name=np_paniers_distrib_avt_saisie]").val(datas.np_paniers_distrib_avt_saisie);
|
|
form.find("input[type=checkbox][name=force_eligible]").prop("checked",datas.force_eligible>0);
|
|
if(modal) modal.modal('show');
|
|
}).fail(function() { alert(srvErrorMsg+" (load datas form contrat)"); });
|
|
}
|
|
|
|
function clearFormContratDatas(form) {
|
|
form.find("input[name=client]").val("");
|
|
|
|
form.find("select[name=type]").val("0");
|
|
form.find("div.custom_type").addClass("hide");
|
|
removeInputContratTypeName(form);
|
|
switchBtnContratTypeAction(form, "add");
|
|
switchBtnContratTypeSecondAction(form, "hide");
|
|
|
|
form.find("select[name=groupe]").val("");
|
|
form.find("select[name=frequence]").val("hebdo");
|
|
form.find("select[name=panier_type]").val("");
|
|
form.find("input[name=nb_paniers]").val("");
|
|
|
|
form.find("select[name=quinz_groupe]").val("A").parent().addClass("hide");
|
|
form.find("input[name=date]").val(getTodayDate("yyyy-mm-dd"));
|
|
|
|
form.find("select[name=lieu_depot]").val("0");
|
|
removeInputLieuName(form);
|
|
switchBtnLieuAction(form, "add");
|
|
switchBtnLieuSecondAction(form, "hide");
|
|
|
|
form.find("input[name=nb_cheque]").val("");
|
|
form.find("input[name=np_paniers_distrib_avt_saisie]").val("");
|
|
form.find("input[type=checkbox][name=force_eligible]").prop("checked",false);
|
|
|
|
// MODALOADER
|
|
form.find("div.modaLoader").removeClass("show");
|
|
}
|
|
|
|
function refreshPaniersTypesSelectList(form, groupe) {
|
|
datas = {
|
|
'action' : 'select_list',
|
|
'groupe' : groupe
|
|
};
|
|
$.post("paniers.php", datas, function(result) {
|
|
var oldVal = parseInt( form.find("select[name=panier_type]").val() );
|
|
form.find("select[name=panier_type]").html(result);
|
|
if(oldVal>0 && form.find("select[name=panier_type] option[value="+oldVal+"]")) form.find("select[name=panier_type]").val(oldVal);
|
|
}).fail(function() { alert(srvErrorMsg+" (form contrat - refresh paniers types select list)"); });
|
|
}
|
|
|
|
// CONTRATS TYPES
|
|
|
|
function switchBtnContratTypeAction(form, action) { // ADD / SAVE / DELETE
|
|
var btn = form.find("button.btnContratTypeAction");
|
|
|
|
if(action == "add") {
|
|
btn.removeClass("save save_new delete").addClass("add").removeClass("btn-danger btn-primary").addClass("btn-default")
|
|
.find("i").removeClass("glyphicon-floppy-saved glyphicon-floppy-disk glyphicon-trash").addClass("glyphicon-plus");
|
|
}
|
|
else if(action == "save") {
|
|
btn.removeClass("add save_new delete").addClass("save").removeClass("btn-danger btn-default").addClass("btn-primary")
|
|
.find("i").removeClass("glyphicon-plus glyphicon-floppy-disk glyphicon-trash").addClass("glyphicon-floppy-saved");
|
|
}
|
|
else if(action == "save_new") {
|
|
btn.removeClass("add save delete").addClass("save_new").removeClass("btn-danger btn-primary").addClass("btn-default")
|
|
.find("i").removeClass("glyphicon-plus glyphicon-floppy-saved glyphicon-trash").addClass("glyphicon-floppy-disk");
|
|
}
|
|
else if(action == "delete") {
|
|
btn.removeClass("add save_new save").addClass("delete").removeClass("btn-default btn-primary").addClass("btn-danger")
|
|
.find("i").removeClass("glyphicon-plus glyphicon-floppy-saved glyphicon-floppy-disk").addClass("glyphicon-trash");
|
|
}
|
|
}
|
|
|
|
function switchBtnContratTypeSecondAction(form, action) { // CANCEL / EDIT
|
|
var btn = form.find("button.btnContratTypeSecondAction");
|
|
|
|
if(action == "cancel") {
|
|
btn.removeClass("edit").addClass("cancel").removeClass("btn-default").addClass("btn-danger")
|
|
.find("i").removeClass("glyphicon-edit").addClass("glyphicon-floppy-remove");
|
|
btn.parent().removeClass("hide");
|
|
}
|
|
else if(action == "edit") {
|
|
btn.removeClass("cancel").addClass("edit").removeClass("btn-danger").addClass("btn-default")
|
|
.find("i").removeClass("glyphicon-floppy-remove").addClass("glyphicon-edit");
|
|
btn.parent().removeClass("hide");
|
|
}
|
|
else if(action == "hide") {
|
|
btn.parent().addClass("hide");
|
|
}
|
|
}
|
|
|
|
function addInputContratTypeName(form, action) {
|
|
var select = form.find("select[name=type]");
|
|
var ipt = $('<input type="text" class="form-control" name="contratTypeName" placeholder="nom">').attr("action", action);
|
|
select.addClass("hide");
|
|
ipt.insertBefore(select);
|
|
if(action=="edit") {
|
|
ipt.attr("ref", select.val());
|
|
ipt.val( select.find("option:selected").html() );
|
|
}
|
|
else ipt.attr("ref", "new");
|
|
ipt.focus();
|
|
}
|
|
|
|
function removeInputContratTypeName(form) {
|
|
var ipt = form.find("input[name=contratTypeName]");
|
|
if(ipt.length>0) ipt.remove();
|
|
form.find("select[name=type]").removeClass("hide");
|
|
}
|
|
|
|
// LIEUX
|
|
|
|
function switchBtnLieuAction(form, action) { // ADD / SAVE / DELETE
|
|
var btn = form.find("button.btnLieuAction");
|
|
|
|
if(action == "add") {
|
|
btn.removeClass("save save_new delete").addClass("add").removeClass("btn-danger btn-primary").addClass("btn-default")
|
|
.find("i").removeClass("glyphicon-floppy-saved glyphicon-trash").addClass("glyphicon-plus");
|
|
}
|
|
else if(action == "save") {
|
|
btn.removeClass("add save_new delete").addClass("save").removeClass("btn-danger btn-default").addClass("btn-primary")
|
|
.find("i").removeClass("glyphicon-plus glyphicon-trash").addClass("glyphicon-floppy-saved");
|
|
}
|
|
else if(action == "delete") {
|
|
btn.removeClass("add save_new save").addClass("delete").removeClass("btn-default btn-primary").addClass("btn-danger")
|
|
.find("i").removeClass("glyphicon-plus glyphicon-floppy-saved").addClass("glyphicon-trash");
|
|
}
|
|
}
|
|
|
|
function switchBtnLieuSecondAction(form, action) { // CANCEL / EDIT
|
|
var btn = form.find("button.btnLieuSecondAction");
|
|
|
|
if(action == "cancel") {
|
|
btn.removeClass("edit").addClass("cancel").removeClass("btn-default").addClass("btn-danger")
|
|
.find("i").removeClass("glyphicon-edit").addClass("glyphicon-floppy-remove");
|
|
btn.parent().removeClass("hide");
|
|
}
|
|
else if(action == "edit") {
|
|
btn.removeClass("cancel").addClass("edit").removeClass("btn-danger").addClass("btn-default")
|
|
.find("i").removeClass("glyphicon-floppy-remove").addClass("glyphicon-edit");
|
|
btn.parent().removeClass("hide");
|
|
}
|
|
else if(action == "hide") {
|
|
btn.parent().addClass("hide");
|
|
}
|
|
}
|
|
|
|
function addInputLieuName(form, action) {
|
|
var select = form.find("select[name=lieu_depot]");
|
|
var ipt = $('<input type="text" class="form-control" name="lieuName" placeholder="nom">').attr("action", action);
|
|
select.addClass("hide");
|
|
ipt.insertBefore(select);
|
|
if(action=="edit") {
|
|
ipt.attr("ref", select.val());
|
|
ipt.val( select.find("option:selected").html() );
|
|
}
|
|
else ipt.attr("ref", "new");
|
|
ipt.focus();
|
|
|
|
ipt.keypress(function(e) {
|
|
if(e.keyCode==13) {
|
|
e.preventDefault();
|
|
form.find("button.btnLieuAction").click();
|
|
}
|
|
});
|
|
}
|
|
|
|
function removeInputLieuName(form) {
|
|
var ipt = form.find("input[name=lieuName]");
|
|
if(ipt.length>0) ipt.remove();
|
|
form.find("select[name=lieu_depot]").removeClass("hide");
|
|
}
|
|
|
|
// GET FORM DATAS
|
|
|
|
function getContratFormDatas(form) {
|
|
var datas = {
|
|
'client' : parseInt(form.find("input[name=client]").attr('ref')),
|
|
'type' : parseInt(form.find("select[name=type]").val()),
|
|
'groupe' : parseInt(form.find("select[name=groupe]").val()),
|
|
'frequence' : form.find("select[name=frequence]").val(),
|
|
'panier_type' : parseInt(form.find("select[name=panier_type]").val()),
|
|
'nb_paniers' : parseInt(form.find("input[name=nb_paniers]").val()),
|
|
'quinz_groupe' : form.find("select[name=quinz_groupe]").val(),
|
|
'date' : form.find("input[name=date]").val(),
|
|
'lieu_depot' : parseInt(form.find("select[name=lieu_depot]").val()),
|
|
'nb_cheque' : parseInt(form.find("input[name=nb_cheque]").val()),
|
|
'np_paniers_distrib_avt_saisie' : form.find("input[name=np_paniers_distrib_avt_saisie]").val(),
|
|
'force_eligible' : form.find("input[type=checkbox][name=force_eligible]").prop("checked") ? 1 : 0
|
|
};
|
|
|
|
// CHECK TYPE_EXIST
|
|
if(!datas.type>0) {
|
|
datas.type = "custom";
|
|
|
|
sel = "select[name=type] option[grp="+datas.groupe+"]";
|
|
sel+= "[freq="+datas.frequence+"]";
|
|
sel+= "[panier="+datas.panier_type+"]";
|
|
sel+= "[nb_paniers="+datas.nb_paniers+"]";
|
|
var opt = form.find(sel);
|
|
if(opt.length>0) datas.type = parseInt( opt.attr("value") );
|
|
}
|
|
|
|
return datas;
|
|
}
|
|
|
|
/***** ADD CONTRAT *****/
|
|
function initAddContrat() {
|
|
// console.log("INIT ADD CONTRAT");
|
|
|
|
btnAddContrat.unbind('click').click( function(event) {
|
|
event.preventDefault();
|
|
clearFormContratDatas(formAddContrat);
|
|
modalAddContrat.modal('show');
|
|
});
|
|
|
|
initFormContrat(formAddContrat);
|
|
|
|
// SAVE CONTRAT
|
|
$(".btnSaveAddContrat").unbind('click').click( function(event) {
|
|
event.preventDefault();
|
|
|
|
var datas = getContratFormDatas(formAddContrat);
|
|
|
|
if(!datas.client>0 || !datas.type>0 || datas.date=="" || !datas.lieu_depot>0) {
|
|
alert("ERREUR : au minimum un client, un type de contrat, une date et un lieu de dépôt doivent être renseigné !");
|
|
return;
|
|
}
|
|
|
|
datas.action = 'add';
|
|
|
|
formAddContrat.find("div.modaLoader").addClass("show");
|
|
$(".btnSaveAddContrat").prop("disabled", true);
|
|
|
|
$.post( contratsBaseURL, datas, function( result ) {
|
|
if(parseInt(result)>0) {
|
|
modalAddContrat.modal('hide');
|
|
setTimeout(function() {
|
|
document.location.reload();
|
|
},200);
|
|
}
|
|
else {
|
|
console.error(result);
|
|
alert(result);
|
|
formAddContrat.find("div.modaLoader").removeClass("show");
|
|
$(".btnSaveAddContrat").prop("disabled", false);
|
|
}
|
|
}).fail(function() {
|
|
alert(srvErrorMsg+" (add contrat)");
|
|
formAddContrat.find("div.modaLoader").removeClass("show");
|
|
$(".btnSaveAddContrat").prop("disabled", false);
|
|
});
|
|
});
|
|
|
|
// CANCEL
|
|
modalAddContrat.on('hidden.bs.modal', function (e) { clearFormContratDatas(formAddContrat); });
|
|
}
|
|
|
|
/***** EDIT CONTRAT *****/
|
|
function initEditContrat() {
|
|
// console.log("INIT EDIT CONTRAT");
|
|
|
|
btnEditContrat.unbind('click').click( function(event) {
|
|
event.preventDefault();
|
|
id = parseInt($(this).attr('ref'));
|
|
if(!id>0) return;
|
|
currentEditContrat = id;
|
|
clearFormContratDatas(formEditContrat);
|
|
loadDatasInFormContratDatas(modalEditContrat,formEditContrat,id);
|
|
});
|
|
|
|
initFormContrat(formEditContrat);
|
|
|
|
// SAVE CONTRAT
|
|
$(".btnSaveEditContrat").unbind('click').click( function(event) {
|
|
event.preventDefault();
|
|
|
|
if(!currentEditContrat>0) return;
|
|
|
|
var datas = getContratFormDatas(formEditContrat);
|
|
if(!datas.client>0 || !datas.type>0 || datas.date=="" || !datas.lieu_depot>0) {
|
|
alert("ERREUR : au minimum un client, un type de contrat, une date et un lieu de dépôt doivent être renseigné !");
|
|
return;
|
|
}
|
|
|
|
datas.action = 'edit';
|
|
datas.ref = currentEditContrat;
|
|
|
|
// console.log(datas,"EDIT datas");
|
|
|
|
formEditContrat.find("div.modaLoader").addClass("show");
|
|
$(".btnSaveEditContrat").prop("disabled", true);
|
|
|
|
$.post( contratsBaseURL, datas, function( result ) {
|
|
if(parseInt(result)>0) {
|
|
modalEditContrat.modal('hide');
|
|
setTimeout(function() {
|
|
document.location.reload();
|
|
},200);
|
|
}
|
|
else {
|
|
console.error(result);
|
|
alert(result);
|
|
formEditContrat.find("div.modaLoader").removeClass("show");
|
|
$(".btnSaveEditContrat").prop("disabled", false);
|
|
}
|
|
}).fail(function() {
|
|
alert(srvErrorMsg+" (edit contrat)");
|
|
formEditContrat.find("div.modaLoader").removeClass("show");
|
|
$(".btnSaveEditContrat").prop("disabled", false);
|
|
});
|
|
});
|
|
|
|
// CANCEL
|
|
modalEditContrat.on('hidden.bs.modal', function (e) {
|
|
clearFormContratDatas(formEditContrat);
|
|
currentEditContrat = 0;
|
|
});
|
|
}
|
|
|
|
/***** ARCHIVE CONTRAT *****/
|
|
function initArchiveContrat() {
|
|
// console.log("INIT ARCHIVE CONTRAT");
|
|
|
|
btnArchiveContrat.unbind('click').click( function(event) {
|
|
event.preventDefault();
|
|
id = parseInt($(this).attr('ref'));
|
|
if(!id>0) return;
|
|
currentArchiveContrat = id;
|
|
modalArchiveContrat.find('b.ref').html( "#"+$(this).attr('ref') );
|
|
modalArchiveContrat.find('b.name').html( $(this).attr('nom') );
|
|
modalArchiveContrat.modal('show');
|
|
});
|
|
|
|
// ARCHIVE
|
|
$("#btnArchiveContrat").unbind('click').click( function(event) {
|
|
event.preventDefault();
|
|
$(this).blur();
|
|
|
|
if(!currentArchiveContrat>0) return;
|
|
|
|
var datas = {
|
|
action : 'archive',
|
|
ref : currentArchiveContrat
|
|
};
|
|
|
|
$.post( contratsBaseURL, datas, function( result ) {
|
|
if(parseInt(result)>0) {
|
|
modalArchiveContrat.modal('hide');
|
|
setTimeout(function() { document.location.reload(); },200);
|
|
}
|
|
else {
|
|
console.error(result);
|
|
alert(result);
|
|
}
|
|
}).fail( function() { alert(srvErrorMsg+" (archive contrat)"); });
|
|
});
|
|
|
|
// CANCEL
|
|
modalArchiveContrat.on('hidden.bs.modal', function (e) {
|
|
modalArchiveContrat.find('b.date').html("");
|
|
currentArchiveContrat = 0;
|
|
});
|
|
|
|
// UNARCHIVE
|
|
btnUnarchiveContrat.unbind('click').click( function(event) {
|
|
event.preventDefault();
|
|
id = parseInt($(this).attr('ref'));
|
|
if(!id>0) return;
|
|
|
|
var datas = { action : 'unarchive', ref : id };
|
|
$.post( contratsBaseURL, datas, function( result ) {
|
|
if(parseInt(result)>0) document.location.reload();
|
|
else {
|
|
console.error(result);
|
|
alert(result);
|
|
}
|
|
}).fail( function() { alert(srvErrorMsg+" (unarchive contrat)"); });
|
|
});
|
|
|
|
}
|
|
|
|
/***** DELETE CONTRAT *****/
|
|
function initDeleteContrat() {
|
|
// console.log("INIT DELETE CONTRAT");
|
|
|
|
btnDeleteContrat.unbind('click').click( function(event) {
|
|
event.preventDefault();
|
|
id = parseInt($(this).attr('ref'));
|
|
if(!id>0) return;
|
|
currentDeleteContrat = id;
|
|
|
|
modalDeleteContrat.find('b.ref').html( "#"+$(this).attr('ref') );
|
|
modalDeleteContrat.find('b.name').html( $(this).attr('nom') );
|
|
|
|
modalDeleteContrat.modal('show');
|
|
});
|
|
|
|
// DELETE
|
|
$("#btnDeleteContrat").unbind('click').click( function(event) {
|
|
event.preventDefault();
|
|
$(this).blur();
|
|
|
|
if(!currentDeleteContrat>0) return;
|
|
|
|
var datas = {
|
|
action : 'delete',
|
|
ref : currentDeleteContrat
|
|
};
|
|
|
|
$.post( contratsBaseURL, datas, function( result ) {
|
|
if(parseInt(result)>0) {
|
|
modalEditContrat.modal('hide');
|
|
setTimeout(function() {
|
|
document.location.reload();
|
|
},200);
|
|
}
|
|
else {
|
|
console.error(result);
|
|
alert(result);
|
|
}
|
|
}).fail(function() { alert(srvErrorMsg+" (delete contrat)"); });
|
|
});
|
|
|
|
// CANCEL
|
|
modalDeleteContrat.on('hidden.bs.modal', function (e) {
|
|
modalDeleteContrat.find('b.name').html("");
|
|
currentDeleteContrat = 0;
|
|
});
|
|
} |