var location_document = $(location).attr('pathname').split("/"); var location_page = location_document[location_document.length -2] + "/" + location_document[location_document.length -1]; $(document).on('change', '#select-grillesTarif', retournerGrilleTarif); $(document).on('click', '#ajouter-ligne', ajouterLigne); $(document).on('click', '#ajouter-ligne-creer', ajouterLigneCreer); $(document).on('click', '#modifier-prix-produit', updateTarifProduit); $(document).on("change", '#select-revendeurs', retournerListeGrilleTarif); $(document).on('click', '#btn-reset', resetGrilleTarif); console.log(location_page); /************************************************************* GRILLE TARIF *************************************************************/ function retournerGrilleTarif() { if ($("#select-grillesTarif option:selected").val() != 0) { $("#tarifs-grilletarif").load("GrilleTarif", {"idGrilleTarif": $("#select-grillesTarif option:selected").val()}); } else { $("#tarifs-grilletarif").html(""); } } function retournerListeGrilleTarif() { var reqGrilleTarif = $.ajax({ url: "api/Tarif/GrilleTarif/getGrilleTarif/"+$("#select-produits").val()+"/"+$("#select-revendeurs").val(), method: "GET", contentType: 'application/json', datatype: "json", }); reqGrilleTarif.done(function(data) { if ($("#grilleTarif-revendeur").html() != "") $("#grilleTarif-revendeur").remove(); $("#revendeurs-resultat").append("

Grille tarif liée pour cette ville : " + data.libelle + "

"); }); reqGrilleTarif.fail(function(data) { $("#revendeurs-resultat").append("

Pas de grille de tarif pour ce produit et ce revendeur

"); }); var request = $.ajax({ url: "api/Tarif/GrilleTarif/Liste", method: "GET", contentType: 'application/json', datatype: "json", }); request.done(function( data ) { var html = []; var options = []; html.push(""); options.push(""); $.each(JSON.parse(JSON.stringify(data)), function(i, obj) { options.push(""); }); html.push(""); html.push("
"); $("#liste-grilletarif").html(html); }); request.fail(function( jqXHR, textStatus ) { $("#liste-grilletarif").html("Impossible d'afficher la liste de grille de tarif"); }); } function createGrilleTarif() { /*Créer un tableau JSON a partir de la grille tarif*/ var tarifs = new Array(); $("#table-grilleTarif").find("tbody tr").each(function() { if ($(this).find("input").val() != "") { var tarifTransport = {}; tarifTransport["libelle"] = $(this).find("#tarif-transport-libelle").val(); tarifTransport["km_min"] = parseInt($(this).find("#tarif-transport-kmmini").val()); tarifTransport["km_max"] = parseInt($(this).find("#tarif-transport-kmmaxi").val()); tarifTransport["prix_ht"] = $(this).find("#tarif-transport-prixht").val().replace(",","."); tarifs.push(tarifTransport); } }); var jsonArray = JSON.stringify(tarifs); /*Envoyer le tableau JSON en Ajax au serveur qui va mettre à jour les valeurs*/ var request = $.ajax({ type: "POST", url: "api/Tarif/GrilleTarif/Creer/"+$("#creer-grille-tarif > div > input").val(), contentType: 'application/json', data: jsonArray, datatype: "json", }); /*En cas de succès, on renvoie un message au client*/ request.done(function(response) { window.scrollTo(0, 0); $("#creer-grille-tarif").parent().prepend(''); setTimeout( function() { $("#ajax_succes").slideUp("slow", function(){ $("#ajax_succes").remove(); }); }, 5000); }); /*En cas d'erreur, on renvoie un message au client avec l'erreur*/ request.fail(function(jqXHR, status, err) { $("#creer-grille-tarif").parent().prepend(''); setTimeout( function() { $("#ajax_error").slideUp("slow", function(){ $("#ajax_error").remove(); }); }, 5000); }); } /************************************************************* TARIF TRANSPORT *************************************************************/ function deleteTarifTransport(elem) { var res = confirm("Etes-vous sur de vouloir supprimer ?"); if (confirm == false) { return; } else { var tr = elem.parentElement.parentElement; /*Envoyer le tableau JSON en Ajax au serveur qui va mettre à jour les valeurs*/ var request = $.ajax({ type: "DELETE", url: "api/Tarif/"+$(tr).attr("id"), contentType: 'application/json', datatype: "json", }); /*En cas de succès, on renvoie un message au client*/ request.done(function(response) { /* On insert la valeur du tarif dans le tableau */ tr.remove(); /* On affiche le message de création du tarif */ $("#gestion-tarifs").parent().prepend(''); setTimeout( function() { $("#ajax_succes").slideUp("slow", function(){ $("#ajax_succes").remove(); }); }, 5000); }); /*En cas d'erreur, on renvoie un message au client avec l'erreur*/ request.fail(function(jqXHR, status) { $("#gestion-tarifs").parent().prepend(''); setTimeout( function() { $("#ajax_error").slideUp("slow", function(){ $("#ajax_error").remove(); }); }, 5000); }); } } function updateTarifTransport(elem) { var tr = elem.parentElement.parentElement; var tarifTransport = {}; tarifTransport["id"] = $(tr).attr("id"); tarifTransport["libelle"] = $(tr).find("#tarif-transport-libelle").val(); tarifTransport["km_min"] = $(tr).find("#tarif-transport-kmmini").val(); tarifTransport["km_max"] = $(tr).find("#tarif-transport-kmmaxi").val(); tarifTransport["prix_ht"] = $(tr).find("#tarif-transport-prixht").val().replace(",","."); tarifTransport["id_grille_tarif"] = $("#select-grillesTarif option:selected").val(); /*Envoyer le tableau JSON en Ajax au serveur qui va mettre à jour les valeurs*/ var request = $.ajax({ type: "PUT", url: "api/Tarif/"+$(tr).attr("id"), contentType: 'application/json', data: JSON.stringify(tarifTransport), datatype: "json", }); /*En cas de succès, on renvoie un message au client*/ request.done(function(response) { $("#gestion-tarifs").parent().prepend(''); setTimeout( function() { $("#ajax_succes").slideUp("slow", function(){ $("#ajax_succes").remove(); }); }, 5000); }); /*En cas d'erreur, on renvoie un message au client avec l'erreur*/ request.fail(function(jqXHR, status) { $("#gestion-tarifs").parent().prepend(''); setTimeout( function() { $("#ajax_error").slideUp("slow", function(){ $("#ajax_error").remove(); }); }, 5000); }); } /************************************************************* TARIF PRODUIT *************************************************************/ function updateTarifProduit() { var idProduit = $("#select-produits option:selected").val(); var idRevendeur = $("#select-revendeurs option:selected").val(); var nouveauPrix = $("#produit-revendeur-prixht").val(); var request = $.ajax({ type: "PUT", url: "api/Tarif/Update-Prix/"+idProduit+"/"+idRevendeur+"/"+nouveauPrix, contentType: 'application/json', datatype: "json", }); /*En cas de succès, on renvoie une vue jsp */ request.done(function(reponse) { $("#produit-info").parent().prepend(''); setTimeout( function() { $("#ajax_succes").slideUp("slow", function(){ $("#ajax_succes").remove(); }); }, 5000); }); /*En cas d'erreur, on renvoie un message au client avec l'erreur*/ request.fail(function(jqXHR, status) { $("#produit-info").parent().prepend(''); setTimeout( function() { $("#ajax_error").slideUp("slow", function(){ $("#ajax_error").remove(); }); }, 5000); }); } /************************************************************* HELPERS *************************************************************/ function ajouterLigne(e) { addLigne("modifier"); var dest = $("#table-grilleTarif").parent(); $(e.target).parent().appendTo(dest); } function ajouterLigneCreer(e) { addLigne("creer"); var dest = $("#table-grilleTarif").parent(); $(e.target).parent().appendTo(dest); } function addLigne(action) { if (action == "modifier") { $("#table-grilleTarif").append(""+ "-"+ ""+ ""+ ""+ ""+ ""+ ""); } else if (action == "creer") { $("#table-grilleTarif").append(""+ ""+ ""+ ""+ ""+ ""); } } function resetGrilleTarif() { $("#table-grilleTarif").find("input").val(""); } function comparer(index) { return function(a, b) { var valA = getCellValue(a, index), valB = getCellValue(b, index) return $.isNumeric(valA) && $.isNumeric(valB) ? valA - valB : valA.toString().localeCompare(valB) } } function getCellValue(row, index){ return $(row).children('td').eq(index).text() }