966 lines
36 KiB
JavaScript
966 lines
36 KiB
JavaScript
// FILTRES
|
|
var filtreLivraisonGroupe = false;
|
|
var filtreLivraisonArchive = false;
|
|
|
|
// VIEW
|
|
var modalViewLivraison = false;
|
|
var btnViewLivraison = false;
|
|
var currentViewLivraison = 0;
|
|
|
|
var livraisonsBaseURL = "livraisons.php";
|
|
|
|
// ADD
|
|
var modalAddLivraison = false;
|
|
var formAddLivraison = false;
|
|
var btnAddLivraison = false;
|
|
|
|
// EDIT
|
|
var modalEditLivraison = false;
|
|
var formEditLivraison = false;
|
|
var btnEditLivraison = false;
|
|
var currentEditLivraison = 0;
|
|
|
|
// ARCHIVE
|
|
var modalArchiveLivraison = false;
|
|
var btnArchiveLivraison = false;
|
|
var btnUnarchiveLivraison = false;
|
|
var currentArchiveLivraison = 0;
|
|
|
|
// DELETE
|
|
var modalDeleteLivraison = false;
|
|
var btnDeleteLivraison = false;
|
|
var currentDeleteLivraison = 0;
|
|
|
|
$(document).ready( function() {
|
|
// console.log("INIT LIVRAISONS");
|
|
|
|
// FILTRES
|
|
filtreLivraisonGroupe = $("#livraisonsFiltre select[name=groupe]");
|
|
filtreLivraisonArchive = $("#livraisonsFiltre select[name=archive]");
|
|
if(filtreLivraisonGroupe.length>0 && filtreLivraisonArchive.length>0) initFiltresLivraisons();
|
|
|
|
// INIT VIEW LIVRAISON
|
|
modalViewLivraison = $("#modalViewLivraison");
|
|
btnViewLivraison = $(".btnViewLivraison");
|
|
if(modalViewLivraison.length>0 && btnViewLivraison.length>0) {
|
|
initViewLivraison();
|
|
// VIEW REQUEST
|
|
var id = getUrlParameter("id");
|
|
if(id!==false && parseInt(id)>0) loadDatasInViewLivraisonModal(id);
|
|
}
|
|
|
|
// INIT ADD LIVRAISON
|
|
modalAddLivraison = $("#modalAddLivraison");
|
|
formAddLivraison = $("#formAddLivraison");
|
|
btnAddLivraison = $("#btnAddLivraison");
|
|
if(modalAddLivraison.length>0 && formAddLivraison.length>0 && btnAddLivraison.length>0) initAddLivraison();
|
|
|
|
// INIT EDIT LIVRAISON
|
|
modalEditLivraison = $("#modalEditLivraison");
|
|
formEditLivraison = $("#formEditLivraison");
|
|
btnEditLivraison = $(".btnEditLivraison");
|
|
if(modalEditLivraison.length>0 && formEditLivraison.length>0 && btnEditLivraison.length>0) initEditLivraison();
|
|
|
|
// INIT ARCHIVE LIVRAISON
|
|
modalArchiveLivraison = $("#modalArchiveLivraison");
|
|
btnArchiveLivraison = $(".btnArchiveLivraison");
|
|
btnUnarchiveLivraison = $(".btnUnarchiveLivraison");
|
|
if(modalArchiveLivraison.length>0 && (btnArchiveLivraison.length>0 || btnUnarchiveLivraison.length>0)) initArchiveLivraison();
|
|
|
|
// INIT DELETE LIVRAISON
|
|
modalDeleteLivraison = $("#modalDeleteLivraison");
|
|
btnDeleteLivraison = $(".btnDeleteLivraison");
|
|
if(modalDeleteLivraison.length>0 && btnDeleteLivraison.length>0) initDeleteLivraison();
|
|
});
|
|
|
|
/***** FILTRES LIVRAISONS *****/
|
|
function initFiltresLivraisons() {
|
|
// console.log("INIT FILTRES LIVRAISONS");
|
|
|
|
filtreLivraisonGroupe.change(function(e) {
|
|
$(this).blur();
|
|
document.location = "?groupe="+parseInt($(this).val());
|
|
});
|
|
|
|
filtreLivraisonArchive.change(function(e) {
|
|
$(this).blur();
|
|
document.location = "?archive="+parseInt($(this).val());
|
|
});
|
|
}
|
|
|
|
/***** VIEW LIVRAISON *****/
|
|
function initViewLivraison() {
|
|
// console.log("INIT VIEW LIVRAISON");
|
|
|
|
var form = modalViewLivraison.find("div.formLivraison");
|
|
|
|
// TABS
|
|
form.find("a.formLivraisonTabBtn").click(function(e) {
|
|
e.preventDefault();
|
|
|
|
// BTN
|
|
if(!$(this).parent().hasClass("active")) {
|
|
form.find("ul.formLivraisonTabs > 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.formLivraisonTabs > div.tab").removeClass("active");
|
|
tab.addClass("active");
|
|
}
|
|
});
|
|
resizeFormLivraison(form);
|
|
$( window ).on( "resize", function() { resizeFormLivraison(form) });
|
|
|
|
// BTN PRINT
|
|
form.find("div.btnPrint a").click(function(e) {
|
|
e.preventDefault();
|
|
url = livraisonsBaseURL+"?ref="+currentViewLivraison+"&action=getPDF&type="+$(this).attr("print_type");
|
|
window.open(url, '_blank');
|
|
});
|
|
|
|
// BTN VIEW
|
|
btnViewLivraison.unbind('click').click( function(event) {
|
|
event.preventDefault();
|
|
id = parseInt($(this).attr('ref'));
|
|
if(!id>0) return;
|
|
clearViewLivraisonModal();
|
|
currentViewLivraison = id;
|
|
loadDatasInViewLivraisonModal(id);
|
|
});
|
|
|
|
// CANCEL
|
|
modalViewLivraison.on('hidden.bs.modal', function (e) {
|
|
clearViewLivraisonModal();
|
|
currentViewLivraison = 0;
|
|
});
|
|
}
|
|
|
|
function loadDatasInViewLivraisonModal(id) {
|
|
datas = {
|
|
'ref' : id,
|
|
'action' : 'getDatas'
|
|
};
|
|
$.post( livraisonsBaseURL, datas, function( result ) {
|
|
var datas = JSON.parse(result);
|
|
|
|
modalViewLivraison.find("small.db_ref > span").html(datas.ref);
|
|
|
|
modalViewLivraison.find("td.paniers_groupe").html(datas.paniers_groupe_nom);
|
|
modalViewLivraison.find("td.date").html(datas.date_print);
|
|
modalViewLivraison.find("td.quinz_groupe").html(datas.quinz_groupe);
|
|
|
|
if(datas.paniers_contrats.length>0) {
|
|
// PANIERS
|
|
$.post(livraisonsBaseURL, { 'ref' : id, 'action' : 'modalView_getTab', 'tab' : 'paniers' }, function(result) {
|
|
modalViewLivraison.find("div.tabPaniers").html(result);
|
|
}).fail(function() { alert(srvErrorMsg+" (load modal view livraison - tab paniers)"); });
|
|
|
|
// COMPO
|
|
if(datas.legumes.length>0) {
|
|
$.post(livraisonsBaseURL, { 'ref' : id, 'action' : 'modalView_getTab', 'tab' : 'compo' }, function(result) {
|
|
modalViewLivraison.find("div.tabCompo").html(result);
|
|
}).fail(function() { alert(srvErrorMsg+" (load modal view livraison - tab compo)"); });
|
|
}
|
|
else {
|
|
modalViewLivraison.find("ul.formLivraisonTabs li[tab=tabCompo]").addClass("hide");
|
|
modalViewLivraison.find("div.btnPrint li[print_type=compo]").addClass("hide");
|
|
}
|
|
|
|
// LEGUMES
|
|
if(Object.keys(datas.total_legumes.legumes).length>0) {
|
|
$.post(livraisonsBaseURL, { 'ref' : id, 'action' : 'modalView_getTab', 'tab' : 'legumes' }, function(result) {
|
|
modalViewLivraison.find("div.tabLegumes").html(result);
|
|
}).fail(function() { alert(srvErrorMsg+" (load modal view livraison - tab legumes)"); });
|
|
}
|
|
else {
|
|
modalViewLivraison.find("ul.formLivraisonTabs li[tab=tabLegumes]").addClass("hide");
|
|
modalViewLivraison.find("div.btnPrint li[print_type=legumes]").addClass("hide");
|
|
}
|
|
}
|
|
else {
|
|
modalViewLivraison.find("ul.formLivraisonTabs li:not([tab=tabGeneral])").addClass("hide");
|
|
modalViewLivraison.find("div.btnPrint").addClass("hide");
|
|
}
|
|
|
|
modalViewLivraison.modal('show');
|
|
}).fail(function() { alert(srvErrorMsg+" (load modal view livraison)"); });
|
|
}
|
|
|
|
function clearViewLivraisonModal() {
|
|
modalViewLivraison.find("small.db_ref > span").html("");
|
|
|
|
modalViewLivraison.find("ul.formLivraisonTabs > li").removeClass('active hide');
|
|
modalViewLivraison.find("ul.formLivraisonTabs > li:first-child").addClass('active');
|
|
modalViewLivraison.find("div.formLivraisonTabs > div.tab").removeClass("active");
|
|
modalViewLivraison.find("div.formLivraisonTabs > div.tab:first-child").addClass("active");
|
|
modalViewLivraison.find("div.btnPrint").removeClass("hide").find('li').removeClass("hide");
|
|
|
|
modalViewLivraison.find("td.paniers_groupe").html("");
|
|
modalViewLivraison.find("td.date").html("");
|
|
modalViewLivraison.find("td.quinz_groupe").html("");
|
|
|
|
modalViewLivraison.find("div.tabPaniers").html("<span class='nullChild'>aucun panier</span>");
|
|
modalViewLivraison.find("div.tabCompo").html("<span class='nullChild'>aucun panier</span>");
|
|
modalViewLivraison.find("div.tabLegumes").html("<span class='nullChild'>aucun légume</span>");
|
|
|
|
modalViewLivraison.find("td.date").html("");
|
|
}
|
|
|
|
/***** FORM LIVRAISON *****/
|
|
function initFormLivraison(form) {
|
|
form.on("submit", function(e) { e.preventDefault(); });
|
|
|
|
// TABS
|
|
form.find("a.formLivraisonTabBtn").click(function(e) {
|
|
e.preventDefault();
|
|
|
|
// BTN
|
|
if(!$(this).parent().hasClass("active")) {
|
|
form.find("ul.formLivraisonTabs > 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.formLivraisonTabs > div.tab").removeClass("active");
|
|
tab.addClass("active");
|
|
}
|
|
});
|
|
|
|
// TAB GENERAL
|
|
form.find("select[name=paniers_groupe]").change(function(e) { $(this).blur(); loadLivraisonsPaniers(form, []); loadLivraisonNextGroupe(form); });
|
|
form.find("input[name=date]").blur(function(e) { loadLivraisonsPaniers(form, []); loadLivraisonNextGroupe(form); });
|
|
form.find("input[name=date]").keydown(function(e) { if(e.keyCode==13) { e.preventDefault(); $(this).blur(); } })
|
|
form.find("select[name=quinz_groupe]").change(function(e) { $(this).blur(); loadLivraisonsPaniers(form, []); });
|
|
|
|
// TAB PANIERS
|
|
form.find("thead.paniersList td.status").click(function(e) {
|
|
var allChecked = true;
|
|
form.find("tbody.paniersList input[name='status']").each(function(n,e) { if(!$(this).prop("checked")) allChecked = false; });
|
|
if(allChecked) form.find("tbody.paniersList input[name='status']").prop("checked", false);
|
|
else form.find("tbody.paniersList input[name='status']").prop("checked", true);
|
|
});
|
|
|
|
resizeFormLivraison(form);
|
|
$( window ).on( "resize", function() { resizeFormLivraison(form) });
|
|
}
|
|
|
|
function resizeFormLivraison(form) {
|
|
var wh = window.innerHeight;
|
|
form.find("div.formLivraisonTabs div.tab").css("max-height", (wh-180)+"px");
|
|
}
|
|
|
|
function loadDatasInFormLivraisonDatas(modal,form,id) {
|
|
datas = {
|
|
'ref' : id,
|
|
'action' : 'getDatas'
|
|
};
|
|
$.post(livraisonsBaseURL, datas, function(result) {
|
|
var datas = JSON.parse(result);
|
|
|
|
form.find("select[name=paniers_groupe]").val(datas.paniers_groupe_ref);
|
|
form.find("input[name=date]").val(datas.date);
|
|
form.find("select[name=quinz_groupe]").val(datas.quinz_groupe);
|
|
|
|
loadLivraisonsPaniers(form, datas.paniers_contrats);
|
|
udpateCompo(form, datas.legumes);
|
|
|
|
if(modal) modal.modal('show');
|
|
}).fail(function() { alert(srvErrorMsg+" (form livraison - load datas)"); });
|
|
}
|
|
|
|
function clearFormLivraisonDatas(form) {
|
|
form.find("ul.formLivraisonTabs > li").removeClass('active');
|
|
form.find("ul.formLivraisonTabs > li:first-child").addClass('active');
|
|
form.find("div.formLivraisonTabs > div.tab").removeClass("active");
|
|
form.find("div.formLivraisonTabs > div.tab:first-child").addClass("active");
|
|
|
|
form.find("select[name=paniers_groupe]").val("");
|
|
form.find("input[name=date]").val(getTodayDate("yyyy-mm-dd"));
|
|
form.find("select[name=quinz_groupe]").val("A");
|
|
|
|
clearPanierItems(form);
|
|
clearCompo(form);
|
|
|
|
// MODALOADER
|
|
form.find("div.modaLoader").removeClass("show");
|
|
}
|
|
|
|
// LIVRAISON - PANIERS
|
|
|
|
function loadLivraisonNextGroupe(form) {
|
|
$datas = {
|
|
'action' : 'last_quinz_groupe',
|
|
'groupe' : form.find("select[name=paniers_groupe]").val(),
|
|
'date' : form.find("input[name=date]").val()
|
|
};
|
|
$.post(livraisonsBaseURL, $datas, function(result) {
|
|
$grp = "A";
|
|
if(result == "A") $grp = "B";
|
|
else if(result != "B") return;
|
|
form.find("select[name=quinz_groupe]").val($grp);
|
|
}).fail(function() { alert(srvErrorMsg+" (form livraison - get next groupe)"); });
|
|
}
|
|
|
|
function loadLivraisonsPaniers(form, paniers) {
|
|
var oldPaniersStates = {};
|
|
form.find("div.tabPaniers table.panier tbody tr.panier").each(function(e) {
|
|
oldPaniersStates[ $(this).attr("contrat") ] = $(this).find("input[name=status]").prop("checked");
|
|
});
|
|
clearPanierItems(form);
|
|
|
|
datas = {
|
|
'action' : 'paniers_eligibles',
|
|
'paniers_groupe' : parseInt( form.find("select[name=paniers_groupe]").val() ),
|
|
'date' : form.find("input[name=date]").val(),
|
|
'quinz_groupe' : form.find("select[name=quinz_groupe]").val()
|
|
};
|
|
if(datas.paniers_groupe>0 && datas.date != "" && datas.quinz_groupe!="") {
|
|
$.post(livraisonsBaseURL, datas, function(result) {
|
|
form.find("div.tabPaniers").html(result);
|
|
$('[data-toggle="tooltip"]').tooltip();
|
|
|
|
// APPLY CB STATUS
|
|
form.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
|
|
form.find("div.tabPaniers table.panier input[name=status]").click(function(e) { $(this).blur(); updateNbPaniersType(form); });
|
|
|
|
// CHECK GROUP
|
|
form.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);
|
|
});
|
|
form.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
|
|
updateNbPaniersType(form);
|
|
|
|
}).fail(function() { alert(srvErrorMsg+" (form livraison - load paniers eligibles)"); });
|
|
}
|
|
}
|
|
|
|
function clearPanierItems(form) {
|
|
form.find("div.tabPaniers").html('<span class="nullChild">aucun panier éligible</span>');
|
|
}
|
|
|
|
function calcNbPanierType(form) {
|
|
var calc = {};
|
|
|
|
form.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 updateNbPaniersType(form) {
|
|
// CHAQUE LIEU
|
|
form.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":""));
|
|
});
|
|
|
|
udpateCompo(form);
|
|
}
|
|
|
|
// LIVRAISON - COMPO
|
|
|
|
function udpateCompo(form, paniersTypes) {
|
|
// GET PANIERS TYPES LIST
|
|
if(typeof(paniersTypes)!="object") paniersTypes = getLivraisonPaniersCompo(form);
|
|
|
|
// CLEAR COMPO
|
|
clearCompo(form);
|
|
|
|
// CREATE PANIERS
|
|
$.each(paniersTypes, function(n,e) { addPanierTypeCompo(form, e); form.find("div.tabCompo span.nullChild").addClass("hide"); });
|
|
|
|
// UPDATE TOTAL
|
|
updateLivraisonTotalPaniersLegumes(form);
|
|
}
|
|
|
|
function addPanierTypeCompo(form, datas) {
|
|
var table = $("<table class='panier'></table>")
|
|
.attr("ref", datas.ref)
|
|
.attr("form", form.attr('id'));
|
|
|
|
// HEADER
|
|
var thead = $("<thead></thead>");
|
|
var trT = $("<tr class='title'></tr>");
|
|
trT.append( $("<th class='titre' colspan='3'></th>").html(datas.nom+" <small>(x"+datas.nb+")</small>") );
|
|
trT.append( $("<th class='total' colspan='2'>TOTAL :</th>") );
|
|
trT.append( $("<th class='total montant' colspan='2'>0.00</th>") );
|
|
trT.append( $("<th class='total unite'>€</th>") );
|
|
thead.append(trT);
|
|
|
|
var trH = $("<tr class='head'></tr>");
|
|
trH.append( $("<th class='nom'>légume</th>") );
|
|
trH.append( $("<th class='tarif' colspan='2'>tarif</th>") );
|
|
trH.append( $("<th class='quantite' colspan='2'>quantité</th>") );
|
|
trH.append( $("<th class='montant' colspan='2'>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(); addPanierTypeCompoLegume(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 => { addPanierTypeCompoLegume(table, l); });
|
|
updateMontantTotalPanier(table);
|
|
}
|
|
else trNull.removeClass("hide");
|
|
|
|
form.find("div.tabCompo").append(table);
|
|
}
|
|
|
|
var legumeTarifUnitesAccronymes = { 'kg' : 'kg', 'pièce' : 'pc', 'botte' : 'bt' }
|
|
|
|
function addPanierTypeCompoLegume(panier, datas) {
|
|
panier.find("tbody tr.nullChild").addClass("hide");
|
|
|
|
/*** CREATE ***/
|
|
var trL = $("<tr class='legume editable'></tr>");
|
|
tdNom = $("<td class='nom'></td>");
|
|
tdTarif = $("<td class='tarif'>0.00</td>");
|
|
tdTarifUnit = $("<td class='tarif_unite unite'>€/kg</td>");
|
|
tdQ = $("<td class='quantite'>0.000</td>");
|
|
tdQunit = $("<td class='quantite_unite unite'>kg</td>");
|
|
tdMontant = $("<td class='montant'>0.00</td>");
|
|
tdMunit = $("<td class='montant_unite unite'>€</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(tdTarifUnit).append(tdQ).append(tdQunit).append(tdMontant).append(tdMunit).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));
|
|
tdTarifUnit.html("€/"+unite);
|
|
|
|
if(datas.tarif_unite == 'kg') tdQ.html(number_format(datas.quantite,3));
|
|
else tdQ.html(datas.quantite);
|
|
|
|
tdQ.attr("unite", datas.tarif_unite);
|
|
tdQunit.html(unite);
|
|
|
|
tdMontant.html(number_format(datas.tarif_prix * datas.quantite, 2));
|
|
}
|
|
panier.find("tbody").append(trL);
|
|
|
|
/*** INITIALIZE ***/
|
|
tdNom.dblclick(function(e) {
|
|
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);
|
|
|
|
ipt.autocomplete({
|
|
html: true,
|
|
delay: 200,
|
|
source: function(requete, reponse) {
|
|
datas = {
|
|
'action' : 'autocomplete_list',
|
|
'search' : ipt.val(),
|
|
'nohist' : true
|
|
};
|
|
$.post("legumes.php", datas, function(jsonTxt) {
|
|
reponse(JSON.parse(jsonTxt));
|
|
}).fail(function() { alert(srvErrorMsg+" (form livraison - compo - legume autocomplete)"); });;
|
|
},
|
|
focus: function(event, ui) { event.preventDefault(); },
|
|
select: function(event, ui) {
|
|
event.preventDefault();
|
|
ipt.val( ui.item.value )
|
|
.attr('ref', ui.item.ref)
|
|
.attr('tarif_ref', ui.item.tarif_ref)
|
|
.attr('tarif_prix', ui.item.tarif_prix)
|
|
.attr('tarif_unite', ui.item.tarif_unite)
|
|
.blur();
|
|
}
|
|
});
|
|
ipt.keypress(function(e) { if(parseInt($(this).attr('ref'))>0) $(this).val("").removeAttr('ref').removeAttr('tarif_ref').removeAttr('tarif_prix').removeAttr('tarif_unite'); });
|
|
|
|
ipt.blur(function(e) {
|
|
val = $(this).val();
|
|
ref = parseInt( $(this).attr("ref") );
|
|
tarif_ref = parseInt( $(this).attr("tarif_ref") );
|
|
tarif_prix = parseFloat( $(this).attr("tarif_prix") );
|
|
tarif_unite = $(this).attr("tarif_unite");
|
|
unite = legumeTarifUnitesAccronymes[tarif_unite];
|
|
|
|
if(ref>0 && tarif_ref>0) {
|
|
var td = $(this).parent(); var tr = td.parent();
|
|
|
|
tr.attr("ref", ref);
|
|
|
|
td.html( val )
|
|
.attr('ref', ref)
|
|
.attr('tarif_ref', tarif_ref)
|
|
.attr('tarif_prix', tarif_prix)
|
|
.attr('tarif_unite', tarif_unite);
|
|
|
|
tr.find("td.tarif").html(number_format(tarif_prix,2)).attr("ref", tarif_ref);
|
|
tr.find("td.tarif_unite").html("€/"+unite);
|
|
|
|
q = tr.find("td.quantite").attr('unite', tarif_unite).html();
|
|
if(tarif_unite=="kg") tr.find("td.quantite").html( number_format(q,3) );
|
|
else tr.find("td.quantite").html( parseInt(q) );
|
|
|
|
tr.find("td.quantite_unite").html(unite);
|
|
|
|
tr.updateCompoLegumeMontant();
|
|
|
|
tr.find("td.quantite").dblclick();
|
|
}
|
|
else {
|
|
beep(50, 800, null, "square");
|
|
$(this).focus();
|
|
}
|
|
});
|
|
ipt.select();
|
|
});
|
|
tdQ.dblclick(function(e) {
|
|
val = $(this).html();
|
|
unite = $(this).attr("unite");
|
|
if(unite in legumeTarifUnitesAccronymes) {
|
|
var ipt = $("<input class='editable quantite' type='text'>");
|
|
$(this).html("").append(ipt);
|
|
if(unite=="kg") {
|
|
initFloatInput(ipt);
|
|
ipt.val( parseFloat(val) );
|
|
}
|
|
else {
|
|
initIntInput(ipt);
|
|
ipt.val( parseInt(val) );
|
|
}
|
|
ipt.blur(function(e) {
|
|
var td = $(this).parent();
|
|
val = $(this).val();
|
|
if(td.attr("unite")=="kg") td.html( number_format(val, 3) );
|
|
else td.html( val );
|
|
td.parent().updateCompoLegumeMontant();
|
|
});
|
|
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");
|
|
updateMontantTotalPanier(panier);
|
|
})
|
|
|
|
/*** START ***/
|
|
if(datas == "new") tdNom.dblclick();
|
|
}
|
|
|
|
$.fn.updateCompoLegumeMontant = function() {
|
|
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();
|
|
updateMontantTotalPanier(panier);
|
|
};
|
|
|
|
function 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));
|
|
form = $("#"+panier.attr('form'));
|
|
if(form.length>0) updateLivraisonTotalPaniersLegumes(form);
|
|
}
|
|
|
|
function clearCompo(form) {
|
|
form.find("div.tabCompo table.panier").remove();
|
|
form.find("div.tabCompo span.nullChild").removeClass("hide");
|
|
}
|
|
|
|
// LIVRAISON - LEGUMES
|
|
|
|
function updateLivraisonTotalPaniersLegumes(form) {
|
|
var legumes = {};
|
|
|
|
form.find("table.legumesTotal tr.legume").remove();
|
|
form.find("table.legumesTotal tr.nullChild").removeClass('hide');
|
|
|
|
/*** CALC TOTAL LEGUMES ***/
|
|
var paniers = getLivraisonPaniersCompo(form);
|
|
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];
|
|
|
|
trL = $("<tr class='legume'></tr>");
|
|
tdNom = $("<td class='nom'></td>").html(l.nom);
|
|
tdTarif = $("<td class='tarif'></td>").html(number_format(l.tarif_prix,2));
|
|
tdTarifUnit = $("<td class='tarif_unite unite'></td>").html("€/"+unite);
|
|
tdQ = $("<td class='quantite'></td>").html( l.tarif_unite=="kg" ? number_format(l.quantite, 3) : l.quantite );
|
|
tdQunit = $("<td class='quantite_unite unite'></td>").html(unite);
|
|
tdMontant = $("<td class='montant'></td>").html(number_format((l.quantite * l.tarif_prix),2));
|
|
tdMunit = $("<td class='montant_unite unite'>€</td>");
|
|
trL.append(tdNom).append(tdTarif).append(tdTarifUnit).append(tdQ).append(tdQunit).append(tdMontant).append(tdMunit);
|
|
|
|
form.find("table.legumesTotal tbody").append(trL);
|
|
form.find("table.legumesTotal tr.nullChild").addClass('hide');
|
|
|
|
total += l.quantite * l.tarif_prix;
|
|
}
|
|
|
|
/*** MONTANT TOTAL ***/
|
|
form.find("table.legumesTotal th.total.montant").html(number_format(total, 2));
|
|
}
|
|
|
|
// GET FORM DATAS
|
|
|
|
function getLivraisonFormDatas(form) {
|
|
var datas = {
|
|
"paniers_groupe" : parseInt(form.find("select[name=paniers_groupe]").val()),
|
|
'date' : form.find("input[name=date]").val(),
|
|
'quinz_groupe' : form.find("select[name=quinz_groupe]").val(),
|
|
'paniers' : JSON.stringify(getLivraionPaniers(form)),
|
|
'paniersCompo' : JSON.stringify(getLivraisonPaniersCompo(form))
|
|
};
|
|
return datas;
|
|
}
|
|
|
|
function getLivraionPaniers(form) {
|
|
var paniers = [];
|
|
form.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 getLivraisonPaniersCompo(form) {
|
|
var paniersTypes = calcNbPanierType(form);
|
|
var legumes = {};
|
|
for(p in paniersTypes) {
|
|
legumes[p] = paniersTypes[p];
|
|
legumes[p].legumes = getLivraisonPanierCompoLegumes(form, p);
|
|
}
|
|
return legumes;
|
|
}
|
|
|
|
function getLivraisonPanierCompoLegumes(form, ref) {
|
|
var table = form.find("table.panier[ref="+ref+"]");
|
|
var legumes = [];
|
|
table.find("tr.legume").each(function(n,e) {
|
|
datas = getLivraisonPanierCompoLegumeDatas($(this));
|
|
if(typeof(datas) == "object") legumes.push(datas);
|
|
});
|
|
|
|
return legumes;
|
|
}
|
|
|
|
function getLivraisonPanierCompoLegumeDatas(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;
|
|
}
|
|
|
|
/***** ADD LIVRAISON *****/
|
|
function initAddLivraison() {
|
|
// console.log("INIT ADD LIVRAISON");
|
|
|
|
btnAddLivraison.unbind('click').click( function(event) {
|
|
event.preventDefault();
|
|
clearFormLivraisonDatas(formAddLivraison);
|
|
modalAddLivraison.modal('show');
|
|
});
|
|
|
|
initFormLivraison(formAddLivraison);
|
|
|
|
// SAVE LIVRAISON
|
|
$(".btnSaveAddLivraison").unbind('click').click( function(event) {
|
|
event.preventDefault();
|
|
|
|
var datas = getLivraisonFormDatas(formAddLivraison);
|
|
|
|
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;
|
|
}
|
|
|
|
datas.action = 'add';
|
|
|
|
formAddLivraison.find("div.modaLoader").addClass("show");
|
|
$(".btnSaveAddLivraison").prop("disabled", true);
|
|
|
|
$.post( livraisonsBaseURL, datas, function( result ) {
|
|
if(parseInt(result)>0) {
|
|
modalAddLivraison.modal('hide');
|
|
setTimeout(function() {
|
|
document.location.reload();
|
|
},200);
|
|
}
|
|
else {
|
|
console.error(result);
|
|
alert(result);
|
|
formAddLivraison.find("div.modaLoader").removeClass("show");
|
|
$(".btnSaveAddLivraison").prop("disabled", false);
|
|
}
|
|
}).fail(function() {
|
|
alert(srvErrorMsg+" (add livraison)");
|
|
formAddLivraison.find("div.modaLoader").removeClass("show");
|
|
$(".btnSaveAddLivraison").prop("disabled", false);
|
|
});
|
|
});
|
|
|
|
// CANCEL
|
|
modalAddLivraison.on('hidden.bs.modal', function (e) { clearFormLivraisonDatas(formAddLivraison); });
|
|
}
|
|
|
|
/***** EDIT LIVRAISON *****/
|
|
function initEditLivraison() {
|
|
// console.log("INIT EDIT LIVRAISON");
|
|
|
|
btnEditLivraison.unbind('click').click( function(event) {
|
|
event.preventDefault();
|
|
id = parseInt($(this).attr('ref'));
|
|
if(!id>0) return;
|
|
currentEditLivraison = id;
|
|
clearFormLivraisonDatas(formEditLivraison);
|
|
loadDatasInFormLivraisonDatas(modalEditLivraison,formEditLivraison,id);
|
|
});
|
|
|
|
initFormLivraison(formEditLivraison);
|
|
|
|
// SAVE LIVRAISON
|
|
$(".btnSaveEditLivraison").unbind('click').click( function(event) {
|
|
event.preventDefault();
|
|
|
|
if(!currentEditLivraison>0) return;
|
|
|
|
var datas = getLivraisonFormDatas(formEditLivraison);
|
|
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;
|
|
}
|
|
|
|
datas.action = 'edit';
|
|
datas.ref = currentEditLivraison;
|
|
|
|
formEditLivraison.find("div.modaLoader").addClass("show");
|
|
$(".btnSaveEditLivraison").prop("disabled", true);
|
|
|
|
$.post( livraisonsBaseURL, datas, function( result ) {
|
|
if(parseInt(result)>0) {
|
|
modalEditLivraison.modal('hide');
|
|
setTimeout(function() {
|
|
document.location.reload();
|
|
},200);
|
|
}
|
|
else {
|
|
console.error(result);
|
|
alert(result);
|
|
formEditLivraison.find("div.modaLoader").removeClass("show");
|
|
$(".btnSaveEditLivraison").prop("disabled", false);
|
|
}
|
|
}).fail( function() {
|
|
alert(srvErrorMsg+" (edit livraison)");
|
|
formEditLivraison.find("div.modaLoader").removeClass("show");
|
|
$(".btnSaveEditLivraison").prop("disabled", false);
|
|
});
|
|
});
|
|
|
|
// CANCEL
|
|
modalEditLivraison.on('hidden.bs.modal', function (e) {
|
|
clearFormLivraisonDatas(formEditLivraison);
|
|
currentEditLivraison = 0;
|
|
});
|
|
}
|
|
|
|
/***** ARCHIVE LIVRAISON *****/
|
|
function initArchiveLivraison() {
|
|
// console.log("INIT ARCHIVE LIVRAISON");
|
|
|
|
btnArchiveLivraison.unbind('click').click( function(event) {
|
|
event.preventDefault();
|
|
id = parseInt($(this).attr('ref'));
|
|
if(!id>0) return;
|
|
currentArchiveLivraison = id;
|
|
modalArchiveLivraison.find('b.date').html( $(this).attr('date') );
|
|
modalArchiveLivraison.modal('show');
|
|
});
|
|
|
|
// ARCHIVE
|
|
$("#btnArchiveLivraison").unbind('click').click( function(event) {
|
|
event.preventDefault();
|
|
$(this).blur();
|
|
|
|
if(!currentArchiveLivraison>0) return;
|
|
|
|
var datas = {
|
|
action : 'archive',
|
|
ref : currentArchiveLivraison
|
|
};
|
|
|
|
$.post( livraisonsBaseURL, datas, function( result ) {
|
|
if(parseInt(result)>0) {
|
|
modalArchiveLivraison.modal('hide');
|
|
setTimeout(function() { document.location.reload(); },200);
|
|
}
|
|
else {
|
|
console.error(result);
|
|
alert(result);
|
|
}
|
|
}).fail( function() { alert(srvErrorMsg+" (archive livraison)"); });
|
|
});
|
|
|
|
// CANCEL
|
|
modalArchiveLivraison.on('hidden.bs.modal', function (e) {
|
|
modalArchiveLivraison.find('b.date').html("");
|
|
currentArchiveLivraison = 0;
|
|
});
|
|
|
|
// UNARCHIVE
|
|
btnUnarchiveLivraison.unbind('click').click( function(event) {
|
|
event.preventDefault();
|
|
id = parseInt($(this).attr('ref'));
|
|
if(!id>0) return;
|
|
|
|
var datas = { action : 'unarchive', ref : id };
|
|
$.post( livraisonsBaseURL, datas, function( result ) {
|
|
if(parseInt(result)>0) document.location.reload();
|
|
else {
|
|
console.error(result);
|
|
alert(result);
|
|
}
|
|
}).fail( function() { alert(srvErrorMsg+" (unarchive livraison)"); });
|
|
});
|
|
|
|
}
|
|
|
|
/***** DELETE LIVRAISON *****/
|
|
function initDeleteLivraison() {
|
|
// console.log("INIT DELETE LIVRAISON");
|
|
|
|
btnDeleteLivraison.unbind('click').click( function(event) {
|
|
event.preventDefault();
|
|
id = parseInt($(this).attr('ref'));
|
|
if(!id>0) return;
|
|
currentDeleteLivraison = id;
|
|
|
|
modalDeleteLivraison.find('b.date').html( $(this).attr('date') );
|
|
|
|
modalDeleteLivraison.modal('show');
|
|
});
|
|
|
|
// DELETE
|
|
$("#btnDeleteLivraison").unbind('click').click( function(event) {
|
|
event.preventDefault();
|
|
$(this).blur();
|
|
|
|
if(!currentDeleteLivraison>0) return;
|
|
|
|
var datas = {
|
|
action : 'delete',
|
|
ref : currentDeleteLivraison
|
|
};
|
|
|
|
$.post( livraisonsBaseURL, datas, function( result ) {
|
|
if(parseInt(result)>0) {
|
|
modalEditLivraison.modal('hide');
|
|
setTimeout(function() {
|
|
document.location.reload();
|
|
},200);
|
|
}
|
|
else {
|
|
console.error(result);
|
|
alert(result);
|
|
}
|
|
}).fail( function() { alert(srvErrorMsg+" (delete livraison)"); });
|
|
});
|
|
|
|
// CANCEL
|
|
modalDeleteLivraison.on('hidden.bs.modal', function (e) {
|
|
modalDeleteLivraison.find('b.date').html("");
|
|
currentDeleteLivraison = 0;
|
|
});
|
|
} |