998 lines
40 KiB
JavaScript
998 lines
40 KiB
JavaScript
// SETTINGS
|
|
var livraisonsConf = {
|
|
type : "livraison",
|
|
baseURL : "livraisons.php",
|
|
// LIST
|
|
list_selector : "#livraisonsList",
|
|
// FILTRES
|
|
filtres_selector : "#livraisonsSearch select",
|
|
clear_filtres_selector : "#livraisonsSearch button.btnClearFiltres",
|
|
// VIEW
|
|
view_modalSelector : "#modalViewLivraison",
|
|
view_btnSelector : ".btnViewLivraison",
|
|
// ADD
|
|
add_modalSelector : "#modalAddLivraison",
|
|
add_btnSelector : "#btnAddLivraison",
|
|
// EDIT
|
|
edit_modalSelector : "#modalEditLivraison",
|
|
edit_btnSelector : ".btnEditLivraison",
|
|
// COPY
|
|
copy_btnSelector : ".btnCopyLivraison",
|
|
// ARCHIVE
|
|
archive_modalSelector : "#modalArchiveLivraison",
|
|
archive_btnSelector : ".btnArchiveLivraison",
|
|
unarchive_btnSelector : ".btnUnarchiveLivraison",
|
|
// DELETE
|
|
delete_modalSelector : "#modalDeleteLivraison",
|
|
delete_btnSelector : ".btnDeleteLivraison",
|
|
}
|
|
|
|
var legumeTarifUnitesAccronymes = { 'kg' : 'kg', 'pièce' : 'pc', 'botte' : 'bt' }
|
|
|
|
var modalFormLivraison_copyCompoOrigin = false;
|
|
|
|
// MODALS
|
|
var modalViewLivraison = false;
|
|
var modalAddLivraison = false;
|
|
var modalEditLivraison = false;
|
|
var modalArchiveLivraison = false;
|
|
var modalDeleteLivraison = false;
|
|
|
|
$(document).ready( function() {
|
|
// LIST LOADER
|
|
initListProgressLoad($(livraisonsConf.list_selector), livraisonsConf.baseURL, livraisonsConf.type+" list");
|
|
|
|
// SEARCH / FILTRES
|
|
$(livraisonsConf.filtres_selector).initFiltreSelect();
|
|
initBtnClearFiltres( $(livraisonsConf.clear_filtres_selector) );
|
|
|
|
// VIEW
|
|
modalViewLivraison = $(livraisonsConf.view_modalSelector);
|
|
if(modalViewLivraison.length>0) {
|
|
initViewLivraison();
|
|
// VIEW REQUEST
|
|
var id = getUrlParameter("ref");
|
|
if(id!==false && parseInt(id)>0 && current_page == "livraisons") {
|
|
modalViewLivraison_clear(modalViewLivraison);
|
|
modalViewLivraison_loadDatas(modalViewLivraison, id);
|
|
}
|
|
}
|
|
|
|
// ADD
|
|
modalAddLivraison = $(livraisonsConf.add_modalSelector);
|
|
if(modalAddLivraison.length>0) initAddLivraison();
|
|
|
|
// EDIT
|
|
modalEditLivraison = $(livraisonsConf.edit_modalSelector);
|
|
if(modalEditLivraison.length>0) initEditLivraison();
|
|
|
|
// COPY
|
|
if(modalAddLivraison.length>0) initCopyLivraison();
|
|
|
|
// ARCHIVE
|
|
modalArchiveLivraison = $(livraisonsConf.archive_modalSelector);
|
|
if(modalArchiveLivraison.length>0) initArchiveLivraison();
|
|
|
|
// DELETE
|
|
modalDeleteLivraison = $(livraisonsConf.delete_modalSelector);
|
|
if(modalDeleteLivraison.length>0) initDeleteLivraison();
|
|
});
|
|
|
|
/***** MODAL VIEW *****/
|
|
function initViewLivraison() {
|
|
// INIT VIEW BTNs
|
|
var initBtnFct = function() { modalForm_initBtnView(
|
|
$(livraisonsConf.view_btnSelector), // BTNs
|
|
modalViewLivraison, // MODAL
|
|
modalViewLivraison_clear, // CLEAR FUNCTION
|
|
modalViewLivraison_loadDatas // LOAD DATAS FUNCTION
|
|
)};
|
|
registerListProgressLoadCbkFct( $("tbody.progressLoadList"), initBtnFct );
|
|
initBtnFct();
|
|
|
|
// TABS
|
|
modalForm_initTabs(modalViewLivraison);
|
|
|
|
// BTN PRINT
|
|
modalViewLivraison_initPrintBtn();
|
|
|
|
// CANCEL
|
|
modalViewLivraison.on('hidden.bs.modal', function(e) { modalViewLivraison_clear(modalViewLivraison); });
|
|
|
|
// EMPECHE LA FERMETURE SI ON EST ENTRAIN D'EDITER UN COMPLEMENT
|
|
modalViewLivraison.on('hide.bs.modal', function(e) {
|
|
if(modalViewLivraison.find("input.editable").length>0) e.preventDefault();
|
|
});
|
|
}
|
|
|
|
function modalViewLivraison_loadDatas(modal, id) {
|
|
$.post(livraisonsConf.baseURL, { 'ref' : id, 'action' : 'getDatas' }, function(jsonTxt) {
|
|
var datas = JSON.parse(jsonTxt);
|
|
|
|
modal.find("small.db_ref > span").html(datas.ref);
|
|
|
|
// PRINT EMARGEMENT PAR LIEU
|
|
if(Object.values(datas.lieux).length>1) {
|
|
for(l in datas.lieux) {
|
|
li = $('<li print_type="emargement" class="print_emargement_par_lieu"></li>');
|
|
a = $('<a href="#" target="_blank" print_type="emargement"></a></li>');
|
|
a.html("Emargement - "+datas.lieux[l].nom).attr("lieu", l);
|
|
li.append(a);
|
|
li.insertAfter(modal.find("#print_emargement"));
|
|
}
|
|
modalViewLivraison_initPrintBtn();
|
|
}
|
|
|
|
modal.find("td.groupe").html(datas.paniers_groupe_nom);
|
|
modal.find("td.date").html(datas.date_print);
|
|
modal.find("td.quinz_groupe").html(datas.quinz_groupe);
|
|
|
|
if(datas.paniers_contrats.length>0) {
|
|
// PANIERS
|
|
modalViewLivraison_loadPaniers();
|
|
|
|
// COMPO
|
|
if(datas.legumes.length>0) {
|
|
$.post(livraisonsConf.baseURL, { 'ref' : id, 'action' : 'modalView_getTab', 'tab' : 'compo' }, function(result) {
|
|
modal.find("div.tabCompo").html(result);
|
|
}).fail(function() { alert("ERREUR SERVEUR (modal view livraison - load tab compo)"); });
|
|
}
|
|
else {
|
|
modal.find("ul.modalLivraisonTabs li[tab=tabCompo]").addClass("hide");
|
|
modal.find("div.btnPrint li[print_type=compo]").addClass("hide");
|
|
}
|
|
|
|
// LEGUMES
|
|
if(Object.keys(datas.total_legumes.legumes).length>0) {
|
|
$.post(livraisonsConf.baseURL, { 'ref' : id, 'action' : 'modalView_getTab', 'tab' : 'legumes' }, function(result) {
|
|
modal.find("div.tabLegumes").html(result);
|
|
}).fail(function() { alert("ERREUR SERVEUR (modal view livraison - load tab legumes)"); });
|
|
}
|
|
else {
|
|
modal.find("ul.modalLivraisonTabs li[tab=tabLegumes]").addClass("hide");
|
|
modal.find("div.btnPrint li[print_type=legumes]").addClass("hide");
|
|
}
|
|
}
|
|
else {
|
|
modal.find("ul.modalLivraisonTabs li:not([tab=tabGeneral])").addClass("hide");
|
|
modal.find("div.btnPrint").addClass("hide");
|
|
}
|
|
|
|
modal.modal('show');
|
|
}).fail(function() { alert("("+livraisonsConf.type+" - load modal view)"); });
|
|
}
|
|
|
|
function modalViewLivraison_clear(modal) {
|
|
modal.removeAttr("view_id");
|
|
modalForm_resetTabs(modal);
|
|
|
|
modal.find("small.db_ref > span").html("");
|
|
|
|
modal.find("div.btnPrint li").removeClass("hide");
|
|
modal.find(".print_emargement_par_lieu").remove();
|
|
|
|
modal.find("td.groupe").html("");
|
|
modal.find("td.date").html("");
|
|
modal.find("td.quinz_groupe").html("");
|
|
|
|
modal.find("div.tabPaniers").html("<span class='nullChild'>aucun panier</span>");
|
|
modal.find("div.tabCompo").html("<span class='nullChild'>aucun panier</span>");
|
|
modal.find("div.tabLegumes").html("<span class='nullChild'>aucun légume</span>");
|
|
}
|
|
|
|
function modalViewLivraison_loadPaniers() {
|
|
$.post(livraisonsConf.baseURL, { 'ref' : modalViewLivraison.attr("view_id"), 'action' : 'modalView_getTab', 'tab' : 'paniers', 'complements' : 1 }, function(result) {
|
|
modalViewLivraison.find("div.tabPaniers").html(result);
|
|
|
|
// COMPLEMENT
|
|
modalViewLivraison.find("div.tabPaniers tr:not(.absent) td.complement").each(function(n,e) {
|
|
$(this).addClass("editable").dblclick(function(e) {
|
|
e.preventDefault();
|
|
$(this).parent().addClass("editing");
|
|
val = parseFloat( $(this).html() );
|
|
var ipt = $("<input class='editable complement' type='text' old_value='0'>");
|
|
$(this).html("").append(ipt);
|
|
initFloatInput(ipt);
|
|
ipt.keypress( function(event) {
|
|
if(event.keyCode==13) {
|
|
$(this).addClass("saving");
|
|
modalViewLivraison_saveComplement( $(this) );
|
|
}
|
|
})
|
|
|
|
if(val>0) ipt.val(val).attr("old_value", val);
|
|
ipt.blur(function(e) {
|
|
$(this).parent().parent().removeClass("editing");
|
|
if( $(this).hasClass("saving") ) return;
|
|
if( parseFloat($(this).attr("old_value"))>0 ) ipt.parent().html(number_format(parseFloat($(this).attr("old_value")), 2)+" €");
|
|
else ipt.parent().html(".")
|
|
});
|
|
ipt.select();
|
|
});
|
|
});
|
|
|
|
// COMPLEMENT REGLE
|
|
modalViewLivraison.find("div.tabPaniers tr:not(.absent) td.complement_regle").each(function(n,e) {
|
|
$(this).addClass("editable").dblclick(function(e) {
|
|
e.preventDefault();
|
|
$(this).parent().addClass("editing");
|
|
val = parseFloat( $(this).html() );
|
|
var ipt = $("<input class='editable complement_regle' type='text' old_value='0'>");
|
|
$(this).html("").append(ipt);
|
|
initFloatInput(ipt);
|
|
ipt.keypress( function(event) {
|
|
if(event.keyCode==13) {
|
|
$(this).addClass("saving");
|
|
modalViewLivraison_saveComplement( $(this) );
|
|
}
|
|
})
|
|
|
|
if(val>0) ipt.val(val).attr("old_value", val);
|
|
ipt.blur(function(e) {
|
|
$(this).parent().parent().removeClass("editing");
|
|
if( $(this).hasClass("saving") ) return;
|
|
if( parseFloat($(this).attr("old_value"))>0 ) ipt.parent().html(number_format(parseFloat($(this).attr("old_value")), 2)+" €");
|
|
else ipt.parent().html(".")
|
|
});
|
|
ipt.select();
|
|
});
|
|
});
|
|
|
|
}).fail(function() { alert("ERREUR SERVEUR (modal view livraison - load tab paniers)"); });
|
|
}
|
|
|
|
function modalViewLivraison_saveComplement(ipt) {
|
|
var td = ipt.parent();
|
|
var tr = td.parent();
|
|
val = parseFloat( ipt.val() );
|
|
old_value = parseFloat( ipt.attr("old_value") );
|
|
if(val>0 || val!=old_value) {
|
|
var datas = {
|
|
action : "saveComplement",
|
|
livraison : parseInt( modalViewLivraison.attr("view_id") ),
|
|
contrat : parseInt( tr.attr("contrat") )
|
|
}
|
|
if(ipt.hasClass("complement")) datas.complement = val;
|
|
else datas.complement_regle = val;
|
|
|
|
$.post(livraisonsConf.baseURL, datas, function(result) {
|
|
if(parseInt(result)==1) modalViewLivraison_loadPaniers();
|
|
else { console.error(result); alert(result); }
|
|
}).fail(function() { alert("ERREUR SERVEUR (modal view livraison - save complement)"); });
|
|
}
|
|
else td.html(".");
|
|
}
|
|
|
|
function modalViewLivraison_initPrintBtn() {
|
|
modalViewLivraison.find("div.btnPrint a").unbind("click").click(function(e) {
|
|
e.preventDefault(); $(this).blur();
|
|
id = parseInt( modalViewLivraison.attr("view_id") );
|
|
if(!id>0) return;
|
|
url = livraisonsConf.baseURL+"?ref="+id+"&action=getPDF&type="+$(this).attr("print_type");
|
|
if(isDefined($(this).attr("lieu"))) url += "&lieu="+$(this).attr("lieu");
|
|
window.open(url, '_blank');
|
|
});
|
|
}
|
|
|
|
/***** MODAL FORM ADD/EDIT *****/
|
|
function modalFormLivraison_init(modal) {
|
|
modal.find("form").preventFormValidation();
|
|
modalForm_initTabs(modal);
|
|
|
|
// TAB GENERAL
|
|
modal.find("select[name=paniers_groupe]").change(function(e) {
|
|
$(this).blur();
|
|
modalFormLivraison_loadPaniers(modal, []);
|
|
modalFormLivraison_loadNextGroupe(modal);
|
|
});
|
|
modal.find("input[name=date]").on("blur change", function(e) {
|
|
modalFormLivraison_loadPaniers(modal, []);
|
|
modalFormLivraison_loadNextGroupe(modal);
|
|
});
|
|
modal.find("input[name=date]").keydown(function(e) { if(e.keyCode==13) { e.preventDefault(); $(this).blur(); } })
|
|
modal.find("select[name=quinz_groupe]").change(function(e) {
|
|
$(this).blur();
|
|
modalFormLivraison_loadPaniers(modal, []);
|
|
});
|
|
|
|
// TAB PANIERS
|
|
modal.find("thead.paniersList td.status").click(function(e) {
|
|
var allChecked = true;
|
|
modal.find("tbody.paniersList input[name='status']").each(function(n,e) { if(!$(this).prop("checked")) allChecked = false; });
|
|
if(allChecked) modal.find("tbody.paniersList input[name='status']").prop("checked", false);
|
|
else modal.find("tbody.paniersList input[name='status']").prop("checked", true);
|
|
});
|
|
|
|
modalFormLivraison_clear(modal);
|
|
}
|
|
|
|
function modalFormLivraison_loadDatas(modal,id) {
|
|
$.post(livraisonsConf.baseURL, { 'ref' : id, 'action' : 'getDatas' }, function(jsonTxt) {
|
|
var datas = JSON.parse(jsonTxt);
|
|
|
|
modal.find("select[name=paniers_groupe]").val(datas.paniers_groupe_ref);
|
|
modal.find("input[name=date]").val(datas.date);
|
|
modal.find("select[name=quinz_groupe]").val(datas.quinz_groupe);
|
|
|
|
modalFormLivraison_loadPaniers(modal, datas.paniers_contrats);
|
|
modalFormLivraison_udpateCompo(modal, datas.legumes);
|
|
|
|
if(modal) modal.modal('show');
|
|
}).fail(function() { alert("("+livraisonsConf.type+" - load datas in modal form)"); });
|
|
}
|
|
|
|
function modalFormLivraison_clear(modal) {
|
|
modal.removeAttr("edit_id");
|
|
modal.find("div.modaLoader").removeClass("show");
|
|
modalForm_resetTabs(modal);
|
|
|
|
modal.find("select[name=paniers_groupe]").val("");
|
|
modal.find("input[name=date]").val(getTodayDate("yyyy-mm-dd"));
|
|
modal.find("select[name=quinz_groupe]").val("A");
|
|
|
|
modalFormLivraison_clearPaniers(modal);
|
|
modalFormLivraison_clearCompo(modal);
|
|
}
|
|
|
|
// LIVRAISON - PANIERS
|
|
|
|
function modalFormLivraison_loadNextGroupe(modal) {
|
|
$datas = {
|
|
'action' : 'last_quinz_groupe',
|
|
'groupe' : modal.find("select[name=paniers_groupe]").val(),
|
|
'date' : modal.find("input[name=date]").val()
|
|
};
|
|
$.post(livraisonsConf.baseURL, $datas, function(result) {
|
|
$grp = "A";
|
|
if(result == "A") $grp = "B";
|
|
else if(result != "B") return;
|
|
modal.find("select[name=quinz_groupe]").val($grp);
|
|
}).fail(function() { alert("ERREUR SERVEUR (modal form livraison - get next groupe)"); });
|
|
}
|
|
|
|
function modalFormLivraison_loadPaniers(modal, paniers) {
|
|
var oldPaniersStates = {};
|
|
modal.find("div.tabPaniers table.panier tbody tr.panier").each(function(e) {
|
|
oldPaniersStates[ $(this).attr("contrat") ] = $(this).find("input[name=status]").prop("checked");
|
|
});
|
|
modalFormLivraison_clearPaniers(modal);
|
|
|
|
datas = {
|
|
'action' : 'paniers_eligibles',
|
|
'paniers_groupe' : parseInt( modal.find("select[name=paniers_groupe]").val() ),
|
|
'date' : modal.find("input[name=date]").val(),
|
|
'quinz_groupe' : modal.find("select[name=quinz_groupe]").val()
|
|
};
|
|
if(datas.paniers_groupe>0 && datas.date != "" && datas.quinz_groupe!="") {
|
|
$.post(livraisonsConf.baseURL, datas, function(result) {
|
|
modal.find("div.tabPaniers").html(result);
|
|
$('[data-toggle="tooltip"]').tooltip();
|
|
|
|
// APPLY CB STATUS
|
|
modal.find("div.tabPaniers table.panier tbody tr.panier").each(function(n,e) {
|
|
ref = parseInt($(this).attr("contrat"));
|
|
if($(this).hasClass('absent')) return;
|
|
state = true;
|
|
if(paniers.length>0) state = paniers.includes(ref);
|
|
else if(oldPaniersStates.hasOwnProperty(ref)) state = oldPaniersStates[ref];
|
|
$(this).find("input[name=status]").prop("checked", state);
|
|
});
|
|
|
|
// INIT CHECKBOX
|
|
modal.find("div.tabPaniers table.panier input[name=status]").click(function(e) { e.stopPropagation(); $(this).blur(); modalFormLivraison_updateNbPaniersType(modal); });
|
|
|
|
// INIT TR CLICK
|
|
modal.find("div.tabPaniers table.panier tr.panier").click(function(e) { $(this).find("input[name=status]").click(); });
|
|
|
|
// CHECK GROUP
|
|
modal.find("div.tabPaniers tr.lieuTitre").click(function(e) {
|
|
var tr = $(this).parent().parent().find("tr.panier:not(.absent)");
|
|
if( tr.find("input:not(:checked)").length>0 ) tr.find("input:not(:checked)").prop("checked", true);
|
|
else tr.find("input").prop("checked", false);
|
|
});
|
|
modal.find("div.tabPaniers tr.panierTitre").click(function(e) {
|
|
var tr = $(this).parent().find("tr.panier:not(.absent)[type_ref="+$(this).attr("type_ref")+"]");
|
|
if( tr.find("input:not(:checked)").length>0 ) tr.find("input:not(:checked)").prop("checked", true);
|
|
else tr.find("input").prop("checked", false);
|
|
});
|
|
|
|
// UPDATE NB PANIERS
|
|
modalFormLivraison_updateNbPaniersType(modal);
|
|
|
|
}).fail(function() { alert("ERREUR SERVEUR (modal form livraison - load paniers eligibles)"); });
|
|
}
|
|
}
|
|
|
|
function modalFormLivraison_clearPaniers(modal) {
|
|
modal.find("div.tabPaniers").html('<span class="nullChild">aucun panier éligible</span>');
|
|
}
|
|
|
|
function modalFormLivraison_calcNbPanierType(modal) {
|
|
var calc = {};
|
|
|
|
modal.find("div.tabPaniers table.panier tbody tr.panier").each(function(e) {
|
|
if($(this).find("input[name=status]").prop("checked")) {
|
|
ref = parseInt($(this).attr("type_ref"));
|
|
nom = $(this).attr("type_nom");
|
|
|
|
if(ref in calc == false) {
|
|
calc[ref] = {
|
|
"ref" : ref,
|
|
"nom" : nom,
|
|
"nb" : 1
|
|
};
|
|
}
|
|
else calc[ref].nb += 1;
|
|
}
|
|
});
|
|
|
|
return calc;
|
|
}
|
|
|
|
function modalFormLivraison_updateNbPaniersType(modal) {
|
|
// CHAQUE LIEU
|
|
modal.find("div.tabPaniers table.panier").each(function(n,e) {
|
|
var table = $(this);
|
|
var nbTotalPaniers = 0;
|
|
// CHAQUE TYPE DE PANIER
|
|
table.find("tr.panierTitre").each(function(n,e) {
|
|
var trTitre = $(this);
|
|
var nbPaniersType = 0;
|
|
var pTypeRef = $(this).attr('type_ref');
|
|
// CHAQUE LIGNE
|
|
table.find('tr.panier[type_ref='+pTypeRef+']').each(function(n,e) {
|
|
if($(this).find("input[name=status]").prop("checked")) { nbPaniersType++; nbTotalPaniers++; }
|
|
});
|
|
trTitre.find("th > small").html("(x"+nbPaniersType+")");
|
|
});
|
|
table.find("tr.lieuTitre th.nb_total_paniers").html(nbTotalPaniers+" panier"+(nbTotalPaniers>1?"s":""));
|
|
});
|
|
|
|
modalFormLivraison_udpateCompo(modal);
|
|
}
|
|
|
|
// LIVRAISON - COMPO
|
|
|
|
function modalFormLivraison_udpateCompo(modal, paniersTypes) {
|
|
// GET PANIERS TYPES LIST
|
|
if(typeof(paniersTypes)!="object") paniersTypes = modalFormLivraison_getPaniersCompo(modal);
|
|
|
|
// CLEAR COMPO
|
|
modalFormLivraison_clearCompo(modal);
|
|
|
|
// CREATE PANIERS
|
|
$.each(paniersTypes, function(n,e) { modalFormLivraison_addPanierTypeCompo(modal, e); modal.find("div.tabCompo span.nullChild").addClass("hide"); });
|
|
|
|
// UPDATE TOTAL
|
|
modalFormLivraison_updateTotalLegumes(modal);
|
|
}
|
|
|
|
function modalFormLivraison_addPanierTypeCompo(modal, datas) {
|
|
var table = $("<table class='panier table-hover'></table>")
|
|
.attr("ref", datas.ref)
|
|
.attr("modal", modal.attr('id'));
|
|
|
|
// HEADER
|
|
var thead = $("<thead></thead>");
|
|
var trT = $("<tr class='title'></tr>");
|
|
trT.append( $("<th class='titre' colspan='2'></th>").html(datas.nom+" <small>(x"+datas.nb+")</small>") );
|
|
trT.append( $("<th class='total' colspan='2'>TOTAL :</th>") );
|
|
trT.append( $("<th class='total montant'>0.00 €</th>") );
|
|
btnCopyCompo = $("<button class='btn btn-xs btn-default glyphicon glyphicon-copy btnCopyCompo'></button>");
|
|
trT.append( $("<th class='action'></th>").append(btnCopyCompo) );
|
|
thead.append(trT);
|
|
|
|
btnCopyCompo.click(function(e) {
|
|
e.preventDefault(); $(this).blur();
|
|
if(e.clientX==0 && e.clientY==0) return;
|
|
modalFormLivraison_copyPanierTypeCompo(modal, table, $(this));
|
|
})
|
|
|
|
var trH = $("<tr class='head'></tr>");
|
|
trH.append( $("<th class='nom'>légume</th>") );
|
|
trH.append( $("<th class='tarif'>tarif</th>") );
|
|
trH.append( $("<th class='quantite' colspan='2'>quantité</th>") );
|
|
trH.append( $("<th class='montant'>montant</th>") );
|
|
btnAddLegume = $("<button class='btn btn-xs btn-info glyphicon glyphicon-plus btnAddLegume'></button>");
|
|
trH.append( $("<th class='action'></th>").append(btnAddLegume) );
|
|
thead.append(trH);
|
|
table.append(thead);
|
|
|
|
btnAddLegume.click(function(e) { e.preventDefault(); $(this).blur(); modalFormLivraison_addPanierTypeCompoLegume(modal, table, "new"); });
|
|
|
|
// LEGUMES
|
|
var tbody = $("<tbody></tbody>");
|
|
var trNull = $("<tr class='nullChild hide'><td colspan='8'>aucun légume</td></tr>");
|
|
tbody.append(trNull);
|
|
table.append(tbody);
|
|
if(datas.legumes && datas.legumes.length>0) {
|
|
datas.legumes.forEach(l => { modalFormLivraison_addPanierTypeCompoLegume(modal, table, l); });
|
|
modalFormLivraison_updateMontantTotalPanier(table);
|
|
}
|
|
else trNull.removeClass("hide");
|
|
|
|
modal.find("div.tabCompo").append(table);
|
|
}
|
|
|
|
function modalFormLivraison_addPanierTypeCompoLegume(modal, panier, datas) {
|
|
panier.find("tbody tr.nullChild").addClass("hide");
|
|
|
|
/*** CREATE ***/
|
|
var trL = $("<tr class='legume editable'></tr>");
|
|
tdNom = $("<td class='nom noselect'></td>");
|
|
tdTarif = $("<td class='tarif noselect'>0.00 €</td>");
|
|
tdQ = $("<td class='quantite noselect'>0.000</td>");
|
|
tdQunit = $("<td class='quantite_unite unite noselect'>kg</td>");
|
|
tdMontant = $("<td class='montant noselect'>0.00 €</td>");
|
|
btnDel = $("<button class='btn btn-xs btn-link glyphicon glyphicon-trash btnDeleteLegume'></button>");
|
|
tdAct = $("<td class='action'></td>").append(btnDel);
|
|
trL.append(tdNom).append(tdTarif).append(tdQ).append(tdQunit).append(tdMontant).append(tdAct);
|
|
|
|
/*** LOAD DATAS ***/
|
|
if(datas!=null && typeof(datas)=="object") {
|
|
trL.attr('ref', datas.ref);
|
|
|
|
unite = legumeTarifUnitesAccronymes[datas.tarif_unite];
|
|
|
|
tdNom.html(datas.nom)
|
|
.attr('ref', datas.ref)
|
|
.attr('tarif_ref', datas.tarif_ref)
|
|
.attr('tarif_prix', datas.tarif_prix)
|
|
.attr('tarif_unite', datas.tarif_unite);
|
|
|
|
tdTarif.html(number_format(datas.tarif_prix,2)+" €/"+unite);
|
|
|
|
tdQ.html(number_format(datas.quantite,3));
|
|
|
|
tdQ.attr("unite", datas.tarif_unite);
|
|
tdQunit.html(unite);
|
|
|
|
tdMontant.html(number_format(datas.tarif_prix * datas.quantite, 2)+" €");
|
|
|
|
modalFormLivraison_refreshPanierTypeCompoLegumeTarif(modal, panier, trL);
|
|
}
|
|
panier.find("tbody").append(trL);
|
|
|
|
/*** INITIALIZE ***/
|
|
tdNom.dblclick(function(e) {
|
|
$(this).parent().addClass("editing");
|
|
var ipt = $("<input class='editable legume' type='text'>").val($(this).html());
|
|
ipt.attr('ref', $(this).attr('ref'))
|
|
.attr('tarif_ref', $(this).attr('tarif_ref'))
|
|
.attr('tarif_prix', $(this).attr('tarif_prix'))
|
|
.attr('tarif_unite', $(this).attr('tarif_unite'));
|
|
$(this).html("").append(ipt);
|
|
|
|
initAutocompleteInput(
|
|
modal, // FORMULAIRE
|
|
ipt, // INPUT
|
|
"legumes.php", // URL
|
|
'autocomplete_list', // REQUEST ACTION
|
|
"(modal form livraison - compo legume autocomplete)", // ERROR CONTEXT
|
|
// REQUEST DATAS
|
|
function(modal, ipt, datas) {
|
|
datas.groupe = modal.find("select[name=paniers_groupe]").val();
|
|
datas.date = modal.find("input[name=date]").val();
|
|
return datas;
|
|
},
|
|
// SELECT
|
|
function(modal, ipt, datas) {
|
|
ipt.val( datas.value )
|
|
.attr('ref', datas.ref)
|
|
.attr('tarif_ref', datas.tarif_ref)
|
|
.attr('tarif_prix', datas.tarif_prix)
|
|
.attr('tarif_unite', datas.tarif_unite)
|
|
.blur();
|
|
},
|
|
// CLEAR
|
|
function(modal, ipt) {
|
|
ipt.val("")
|
|
.removeAttr('ref')
|
|
.removeAttr('tarif_ref')
|
|
.removeAttr('tarif_prix')
|
|
.removeAttr('tarif_unite');
|
|
},
|
|
// BLUR
|
|
function(modal, ipt) {
|
|
val = ipt.val();
|
|
ref = parseInt( ipt.attr("ref") );
|
|
tarif_ref = parseInt( ipt.attr("tarif_ref") );
|
|
tarif_prix = parseFloat( ipt.attr("tarif_prix") );
|
|
tarif_unite = ipt.attr("tarif_unite");
|
|
unite = legumeTarifUnitesAccronymes[tarif_unite];
|
|
|
|
if(ref>0 && tarif_ref>0) {
|
|
var td = ipt.parent(); var tr = td.parent();
|
|
tr.attr("ref", ref);
|
|
td.html( val ).attr('ref', ref);
|
|
modalFormLivraison_loadPanierTypeCompoLegumeTarif(tr, tarif_ref, tarif_prix, tarif_unite);
|
|
tr.find("td.quantite").dblclick();
|
|
}
|
|
else {
|
|
beep(50, 800, null, "square");
|
|
ipt.focus();
|
|
}
|
|
}
|
|
);
|
|
ipt.select();
|
|
});
|
|
tdQ.dblclick(function(e) {
|
|
val = $(this).html();
|
|
unite = $(this).attr("unite");
|
|
if(unite in legumeTarifUnitesAccronymes) {
|
|
$(this).parent().addClass("editing");
|
|
var ipt = $("<input class='editable quantite' type='number' step='0.1'>");
|
|
$(this).html("").append(ipt);
|
|
initFloatInput(ipt);
|
|
ipt.val( parseFloat(val) );
|
|
ipt.blur(function(e) {
|
|
var td = $(this).parent();
|
|
val = parseFloat( $(this).val() );
|
|
td.html( number_format(val, 3) );
|
|
td.parent().removeClass("editing").updateCompoLegumeMontant(true);
|
|
});
|
|
ipt.select();
|
|
}
|
|
});
|
|
btnDel.click(function(e) {
|
|
e.preventDefault();
|
|
trL.remove();
|
|
if(panier.find("tbody tr.legume").length==0) panier.find("tbody tr.nullChild").removeClass("hide");
|
|
modalFormLivraison_updateMontantTotalPanier(panier);
|
|
})
|
|
|
|
/*** START ***/
|
|
if(datas == "new") tdNom.dblclick();
|
|
}
|
|
|
|
function modalFormLivraison_loadPanierTypeCompoLegumeTarif(row, tarif_ref, tarif_prix, tarif_unite) {
|
|
unite = legumeTarifUnitesAccronymes[tarif_unite];
|
|
|
|
row.find("td.nom").attr('tarif_ref', tarif_ref)
|
|
.attr('tarif_prix', tarif_prix)
|
|
.attr('tarif_unite', tarif_unite);
|
|
|
|
row.find("td.tarif").html(number_format(tarif_prix,2)+" €/"+unite).attr("ref", tarif_ref);
|
|
|
|
q = row.find("td.quantite").attr('unite', tarif_unite).html();
|
|
if(tarif_unite=="kg") row.find("td.quantite").html( number_format(q,3) );
|
|
else row.find("td.quantite").html( parseInt(q) );
|
|
|
|
row.find("td.quantite_unite").html(unite);
|
|
|
|
row.updateCompoLegumeMontant(false);
|
|
}
|
|
|
|
function modalFormLivraison_refreshPanierTypeCompoLegumeTarif(modal, panier, row) {
|
|
return;
|
|
var datas = {
|
|
ref : parseInt(row.attr('ref')),
|
|
action : 'getTarif',
|
|
groupe : modal.find("select[name=paniers_groupe]").val(),
|
|
date : modal.find("input[name=date]").val()
|
|
}
|
|
$.post("legumes.php", datas, function(result) {
|
|
tarif = JSON.parse(result);
|
|
tdNom = row.find("td.nom");
|
|
if(typeof(tarif)=== "object" && tdNom.length>0) {
|
|
if(parseInt(tarif.ref) != parseInt(tdNom.attr('tarif_ref'))) modalFormLivraison_loadPanierTypeCompoLegumeTarif(row, tarif.ref, tarif.prix, tarif.unite);
|
|
}
|
|
}).fail(function() { alert("ERREUR SERVEUR (modal form livraison - get next groupe)"); });
|
|
}
|
|
|
|
function modalFormLivraison_copyPanierTypeCompo(modal, panier, btn) {
|
|
// COPY
|
|
if(!modalFormLivraison_copyCompoOrigin>0) {
|
|
var compo = modalFormLivraison_getPanierCompoLegumes(modal, panier.attr('ref'));
|
|
if(!compo.length>0) return;
|
|
modalFormLivraison_copyCompoOrigin = panier.attr("ref");
|
|
modal.find(".btnCopyCompo").removeClass("btn-default").addClass("btn-warning")
|
|
.removeClass("glyphicon-copy").addClass("glyphicon-paste");
|
|
btn.removeClass("btn-warning").addClass("btn-info")
|
|
.removeClass("glyphicon-paste").addClass("glyphicon-remove");
|
|
}
|
|
// PASTE
|
|
else {
|
|
if(modalFormLivraison_copyCompoOrigin != panier.attr("ref")) {
|
|
var oldCompo = modalFormLivraison_getPanierCompoLegumes(modal, panier.attr('ref'));
|
|
if(oldCompo.length>0) { if(!confirm("Etês-vous sûr de vouloir remplacer le contenu de ce panier ?")) return; }
|
|
var legumes = modalFormLivraison_getPanierCompoLegumes(modal, modalFormLivraison_copyCompoOrigin);
|
|
// CLEAR
|
|
panier.find("tbody").children().remove();
|
|
// PASTE
|
|
legumes.forEach(l => { modalFormLivraison_addPanierTypeCompoLegume(modal, panier, l); });
|
|
modalFormLivraison_updateMontantTotalPanier(panier);
|
|
}
|
|
|
|
modalFormLivraison_copyCompoOrigin = false;
|
|
modal.find(".btnCopyCompo").removeClass("btn-warning").removeClass("btn-info").addClass("btn-default")
|
|
.removeClass("glyphicon-paste").removeClass("glyphicon-remove").addClass("glyphicon-copy");
|
|
}
|
|
}
|
|
|
|
$.fn.updateCompoLegumeMontant = function(focus) {
|
|
q = parseFloat($(this).find("td.quantite").html());
|
|
t = parseFloat($(this).find("td.tarif").html());
|
|
$(this).find("td.montant").html( number_format(q * t, 2)+" €" );
|
|
|
|
// UPDATE TOTAL
|
|
panier = $(this).parent().parent();
|
|
modalFormLivraison_updateMontantTotalPanier(panier);
|
|
|
|
if(focus) panier.find(".btnAddLegume").focus();
|
|
};
|
|
|
|
function modalFormLivraison_updateMontantTotalPanier(panier) {
|
|
var total = 0;
|
|
panier.find("tr.legume td.montant").each(function(n,e) { total += parseFloat($(this).html()); })
|
|
panier.find("th.total.montant").html(number_format(total, 2)+" €");
|
|
modal = $("#"+panier.attr('modal'));
|
|
if(modal.length>0) modalFormLivraison_updateTotalLegumes(modal);
|
|
}
|
|
|
|
function modalFormLivraison_clearCompo(modal) {
|
|
modal.find("div.tabCompo table.panier").remove();
|
|
modal.find("div.tabCompo span.nullChild").removeClass("hide");
|
|
}
|
|
|
|
// LIVRAISON - LEGUMES
|
|
|
|
function modalFormLivraison_updateTotalLegumes(modal) {
|
|
var legumes = {};
|
|
|
|
modal.find("table.legumesTotal tr.legume").remove();
|
|
modal.find("table.legumesTotal tr.nullChild").removeClass('hide');
|
|
|
|
/*** CALC TOTAL LEGUMES ***/
|
|
var paniers = modalFormLivraison_getPaniersCompo(modal);
|
|
for(r in paniers) {
|
|
p = paniers[r];
|
|
for (const l of p.legumes) {
|
|
if(l.ref in legumes) legumes[l.ref].quantite += l.quantite * p.nb;
|
|
else {
|
|
legumes[l.ref] = l;
|
|
legumes[l.ref].quantite = l.quantite * p.nb;
|
|
}
|
|
}
|
|
}
|
|
|
|
/*** CREATE ***/
|
|
var total = 0.0;
|
|
for(ref in legumes) {
|
|
l = legumes[ref];
|
|
unite = legumeTarifUnitesAccronymes[l.tarif_unite];
|
|
number_format(l.quantite,2);
|
|
trL = $("<tr class='legume'></tr>");
|
|
tdNom = $("<td class='nom noselect'></td>").html(l.nom);
|
|
tdTarif = $("<td class='tarif noselect'></td>").html(number_format(l.tarif_prix,2)+" €/"+unite);
|
|
tdQ = $("<td class='quantite noselect'></td>").html( round_number(l.quantite,3)+" "+unite );
|
|
tdMontant = $("<td class='montant noselect'></td>").html(number_format((l.quantite * l.tarif_prix),2)+" €");
|
|
trL.append(tdNom).append(tdTarif).append(tdQ).append(tdMontant);
|
|
|
|
modal.find("table.legumesTotal tbody").append(trL);
|
|
modal.find("table.legumesTotal tr.nullChild").addClass('hide');
|
|
|
|
total += l.quantite * l.tarif_prix;
|
|
}
|
|
|
|
/*** MONTANT TOTAL ***/
|
|
modal.find("table.legumesTotal th.total.montant").html(number_format(total, 2)+" €");
|
|
}
|
|
|
|
// GET FORM DATAS
|
|
|
|
function modalFormLivraison_getDatas(modal) {
|
|
var datas = {
|
|
"paniers_groupe" : parseInt(modal.find("select[name=paniers_groupe]").val()),
|
|
'date' : modal.find("input[name=date]").val(),
|
|
'quinz_groupe' : modal.find("select[name=quinz_groupe]").val(),
|
|
'paniers' : JSON.stringify( modalFormLivraison_getPaniers(modal) ),
|
|
'paniersCompo' : JSON.stringify( modalFormLivraison_getPaniersCompo(modal) )
|
|
};
|
|
return datas;
|
|
}
|
|
|
|
function modalFormLivraison_getPaniers(modal) {
|
|
var paniers = [];
|
|
modal.find("div.tabPaniers table.panier tbody tr.panier").each(function(e) {
|
|
if($(this).find("input[name=status]").prop("checked")) paniers.push(parseInt($(this).attr("contrat")));
|
|
});
|
|
return paniers;
|
|
}
|
|
|
|
function modalFormLivraison_getPaniersCompo(modal) {
|
|
var paniersTypes = modalFormLivraison_calcNbPanierType(modal);
|
|
var legumes = {};
|
|
for(p in paniersTypes) {
|
|
legumes[p] = paniersTypes[p];
|
|
legumes[p].legumes = modalFormLivraison_getPanierCompoLegumes(modal, p);
|
|
}
|
|
return legumes;
|
|
}
|
|
|
|
function modalFormLivraison_getPanierCompoLegumes(modal, ref) {
|
|
var table = modal.find("table.panier[ref="+ref+"]");
|
|
var legumes = [];
|
|
table.find("tr.legume").each(function(n,e) {
|
|
datas = modalFormLivraison_getPanierCompoLegumeDatas($(this));
|
|
if(typeof(datas) == "object") legumes.push(datas);
|
|
});
|
|
|
|
return legumes;
|
|
}
|
|
|
|
function modalFormLivraison_getPanierCompoLegumeDatas(tr) {
|
|
ref = parseInt( tr.attr('ref') );
|
|
if(ref>0) {
|
|
tdNom = tr.find("td.nom");
|
|
return {
|
|
'ref' : ref,
|
|
'nom' : tdNom.html(),
|
|
'tarif_ref' : parseInt( tdNom.attr("tarif_ref") ),
|
|
'tarif_prix' : parseFloat( tdNom.attr("tarif_prix") ),
|
|
'tarif_unite' : tdNom.attr("tarif_unite"),
|
|
'quantite' : parseFloat( tr.find("td.quantite").html() )
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function modalFormLivraison_checkDatas(datas) {
|
|
if(!datas.paniers_groupe>0 ||datas.date=="" || datas.quinz_groupe=="") {
|
|
alert("ERREUR : au minimum un groupe de contrats, une date et un groupe bi-hebdo doivent être renseigné !");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/***** ADD *****/
|
|
function initAddLivraison() {
|
|
// INIT FORM
|
|
modalFormLivraison_init(modalAddLivraison);
|
|
|
|
// INIT ADD BTN
|
|
modalForm_initBtnAdd(
|
|
$(livraisonsConf.add_btnSelector), // BTN ADD
|
|
modalAddLivraison, // MODAL
|
|
modalFormLivraison_clear // CLEAR FORM FUNCTION
|
|
);
|
|
|
|
// INIT SAVE BTN
|
|
modalForm_initBtnSaveAdd(
|
|
modalAddLivraison.find(".btnSave"), // BTN SAVE
|
|
livraisonsConf.type, // ADD TYPE
|
|
modalAddLivraison, // MODAL
|
|
modalFormLivraison_getDatas, // GET FORM DATAS FUNCTION
|
|
modalFormLivraison_checkDatas, // CHECK FORM DATAS FUNCTION
|
|
livraisonsConf.baseURL // SAVE URL
|
|
);
|
|
|
|
// CANCEL
|
|
modalAddLivraison.on('hidden.bs.modal', function (e) { modalFormLivraison_clear(modalAddLivraison); });
|
|
}
|
|
|
|
/***** COPY *****/
|
|
function initCopyLivraison() {
|
|
// INIT COPY BTNs
|
|
var initBtnFct = function() { modalForm_initBtnCopy(
|
|
$(livraisonsConf.copy_btnSelector), // BTNs
|
|
modalAddLivraison, // MODAL
|
|
modalFormLivraison_clear, // CLEAR FUNCTION
|
|
modalFormLivraison_loadDatas // LOAD DATAS FUNCTION
|
|
)};
|
|
registerListProgressLoadCbkFct( $("tbody.progressLoadList"), initBtnFct );
|
|
initBtnFct();
|
|
}
|
|
|
|
/***** EDIT *****/
|
|
function initEditLivraison() {
|
|
// INIT FORM
|
|
modalFormLivraison_init(modalEditLivraison);
|
|
|
|
// INIT EDIT BTNs
|
|
var initBtnFct = function() { modalForm_initBtnEdit(
|
|
$(livraisonsConf.edit_btnSelector), // BTNs
|
|
modalEditLivraison, // MODAL
|
|
modalFormLivraison_clear, // CLEAR FUNCTION
|
|
modalFormLivraison_loadDatas // LOAD DATAS FUNCTION
|
|
)};
|
|
registerListProgressLoadCbkFct( $("tbody.progressLoadList"), initBtnFct );
|
|
initBtnFct();
|
|
|
|
// INIT SAVE EDIT BTN
|
|
modalForm_initBtnSaveEdit(
|
|
modalEditLivraison.find(".btnSave"), // BTN SAVE
|
|
livraisonsConf.type, // EDIT TYPE
|
|
modalEditLivraison, // MODAL
|
|
modalFormLivraison_getDatas, // GET FORM DATAS FUNCTION
|
|
modalFormLivraison_checkDatas, // CHECK FORM DATAS FUNCTION
|
|
livraisonsConf.baseURL // SAVE URL
|
|
);
|
|
|
|
// CANCEL
|
|
modalEditLivraison.on('hidden.bs.modal', function (e) { modalFormLivraison_clear(modalEditLivraison); });
|
|
}
|
|
|
|
/***** ARCHIVE *****/
|
|
function initArchiveLivraison() {
|
|
// INIT ARCHIVE BTNs
|
|
var initBtnFct = function() { modalForm_initBtnArchive(
|
|
$(livraisonsConf.archive_btnSelector), // BTNs
|
|
modalArchiveLivraison, // MODAL
|
|
modalArchiveLivraison_clear, // CLEAR MODAL FUNCTION
|
|
modalArchiveLivraison_loadDatas // LOAD DATAS FUNCTION
|
|
)};
|
|
registerListProgressLoadCbkFct( $("tbody.progressLoadList"), initBtnFct );
|
|
initBtnFct();
|
|
|
|
// INIT UNARCHIVE BTNs
|
|
var initUnBtnFct = function() { modalForm_initBtnUnarchive(
|
|
$(livraisonsConf.unarchive_btnSelector), // BTNs
|
|
livraisonsConf.type, // DELETE TYPE
|
|
modalArchiveLivraison, // MODAL
|
|
livraisonsConf.baseURL // SAVE URL
|
|
)};
|
|
registerListProgressLoadCbkFct( $("tbody.progressLoadList"), initUnBtnFct );
|
|
initUnBtnFct();
|
|
|
|
// INIT SAVE DELETE BTN
|
|
modalForm_initBtnSaveArchive(
|
|
modalArchiveLivraison.find(".btnSave"), // BTN SAVE
|
|
livraisonsConf.type, // DELETE TYPE
|
|
modalArchiveLivraison, // MODAL
|
|
livraisonsConf.baseURL // SAVE URL
|
|
);
|
|
|
|
// CANCEL
|
|
modalArchiveLivraison.on('hidden.bs.modal', function (e) { modalArchiveLivraison_clear(); });
|
|
}
|
|
|
|
function modalArchiveLivraison_loadDatas(btn, id) {
|
|
modalArchiveLivraison.find('b.groupe').html( btn.attr('groupe') );
|
|
modalArchiveLivraison.find('b.date').html( btn.attr('date') );
|
|
modalArchiveLivraison.modal('show');
|
|
}
|
|
|
|
function modalArchiveLivraison_clear() {
|
|
modalArchiveLivraison.removeAttr("archive_id");
|
|
modalArchiveLivraison.find('b.groupe').html("");
|
|
modalArchiveLivraison.find('b.date').html("");
|
|
}
|
|
|
|
/***** DELETE *****/
|
|
function initDeleteLivraison() {
|
|
// INIT DELETE BTNs
|
|
var initBtnFct = function() { modalForm_initBtnDelete(
|
|
$(livraisonsConf.delete_btnSelector), // BTNs
|
|
modalDeleteLivraison, // MODAL
|
|
modalDeleteLivraison_clear, // CLEAR MODAL FUNCTION
|
|
modalDeleteLivraison_loadDatas // LOAD DATAS FUNCTION
|
|
)};
|
|
registerListProgressLoadCbkFct( $("tbody.progressLoadList"), initBtnFct );
|
|
initBtnFct();
|
|
|
|
// INIT SAVE DELETE BTN
|
|
modalForm_initBtnSaveDelete(
|
|
modalDeleteLivraison.find(".btnSave"), // BTN SAVE
|
|
livraisonsConf.type, // DELETE TYPE
|
|
modalDeleteLivraison, // MODAL
|
|
false, // GET FORM DATAS FUNCTION
|
|
false, // CHECK FORM DATAS FUNCTION
|
|
livraisonsConf.baseURL // SAVE URL
|
|
);
|
|
|
|
// CANCEL
|
|
modalDeleteLivraison.on('hidden.bs.modal', function (e) { modalDeleteLivraison_clear(); });
|
|
}
|
|
|
|
function modalDeleteLivraison_loadDatas(btn, id) {
|
|
modalDeleteLivraison.find('b.groupe').html( btn.attr('groupe') );
|
|
modalDeleteLivraison.find('b.date').html( btn.attr('date') );
|
|
modalDeleteLivraison.modal('show');
|
|
}
|
|
|
|
function modalDeleteLivraison_clear() {
|
|
modalDeleteLivraison.removeAttr("delete_id");
|
|
modalDeleteLivraison.find('b.groupe').html("");
|
|
modalDeleteLivraison.find('b.date').html("");
|
|
} |