3
0
Fork 0
mirror of https://github.com/nanoy42/coope synced 2024-12-25 16:33:47 +00:00
coope/staticfiles/manage.js

218 lines
6.2 KiB
JavaScript
Raw Normal View History

2018-11-22 21:52:15 +00:00
total = 0
2018-10-05 22:03:02 +00:00
products = []
2018-11-27 08:07:12 +00:00
menus = []
2019-01-22 19:27:18 +00:00
cotisations = []
2018-10-05 22:03:02 +00:00
paymentMethod = null
balance = 0
username = ""
2019-01-22 19:27:18 +00:00
id_user = 0
listPintes = []
nbPintes = 0;
use_pinte_monitoring = false;
function get_config(){
2018-12-23 11:06:16 +00:00
res = $.get("../preferences/getConfig", function(data){
use_pinte_monitoring = data.use_pinte_monitoring;
});
}
2018-10-05 22:03:02 +00:00
2018-12-23 22:55:27 +00:00
function get_product(id){
res = $.get("getProduct/" + id, function(data){
nbPintes += data.nb_pintes;
2019-06-23 14:43:23 +00:00
add_product(data.pk, data.name, data.amount, data.needQuantityButton);
2018-10-05 22:03:02 +00:00
});
}
2018-12-23 22:55:27 +00:00
function get_menu(id){
res = $.get("getMenu/" + id, function(data){
nbPintes += data.nb_pintes;
2019-06-23 14:43:23 +00:00
add_menu(data.pk, data.name, data.amount, data.needQuantityButton);
2018-11-27 08:07:12 +00:00
});
}
2019-01-22 19:27:18 +00:00
function get_cotisation(id){
res = $.get("../preferences/getCotisation/" + id, function(data){
add_cotisation(data.pk, "", data.duration, data.amount, data.needQuantityButton);
});
}
2019-06-23 14:43:23 +00:00
function add_product(pk, name, amount, needQuantityButton){
2018-10-05 22:03:02 +00:00
exist = false
2019-01-22 19:27:18 +00:00
index = -1;
2018-10-05 22:03:02 +00:00
for(k=0;k < products.length; k++){
if(products[k].pk == pk){
exist = true
index = k
}
}
if(needQuantityButton){
quantity = parseInt(window.prompt("Quantité ?",""));
}else{
quantity = 1;
}
if(quantity == null || !Number.isInteger(quantity)){
quantity = 1;
}
2018-10-05 22:03:02 +00:00
if(exist){
products[index].quantity += quantity;
2018-10-05 22:03:02 +00:00
}else{
2019-06-23 14:43:23 +00:00
products.push({"pk": pk, "name": name, "amount": amount, "quantity": quantity});
2018-10-05 22:03:02 +00:00
}
generate_html()
}
2019-06-23 14:43:23 +00:00
function add_menu(pk, name, amount){
2018-11-27 08:07:12 +00:00
exist = false;
index = -1;
for(k=0; k < menus.length; k++){
if(menus[k].pk == pk){
exist = true;
index = k;
}
}
if(exist){
menus[index].quantity += 1;
}else{
2019-06-23 14:43:23 +00:00
menus.push({"pk": pk, "name": name, "amount": amount, "quantity":1});
2018-11-27 08:07:12 +00:00
}
generate_html();
}
2019-06-23 14:43:23 +00:00
function add_cotisation(pk, duration, amount){
2019-01-22 19:27:18 +00:00
exist = false;
index = -1;
for(k=0; k < cotisations.length; k++){
if(cotisations[k].pk == pk){
exist = true;
index = k;
}
}
if(exist){
cotisations[index].quantity += 1;
}else{
2019-06-23 14:43:23 +00:00
cotisations.push({"pk": pk, "duration": duration, "amount": amount, "quantity":1});
2019-01-22 19:27:18 +00:00
}
generate_html();
}
2018-10-05 22:03:02 +00:00
function generate_html(){
html = "";
2019-01-22 19:27:18 +00:00
for(k=0;k<cotisations.length;k++){
cotisation = cotisations[k];
html += '<tr><td></td><td>Cotisation ' + String(cotisation.duration) + ' jours</td><td>' + String(cotisation.amount) + ' €</td><td><input type="number" data-target="' + String(k) + '" onChange="updateCotisationInput(this)" value="' + String(cotisation.quantity) + '"/></td><td>' + String(Number((cotisation.quantity * cotisation.amount).toFixed(2))) + ' €</td></tr>';
}
2018-10-05 22:03:02 +00:00
for(k=0;k<products.length;k++){
product = products[k]
2019-06-23 14:43:23 +00:00
html += '<tr><td>' + product.name + '</td><td>' + String(product.amount) + ' €</td><td><input type="number" data-target="' + String(k) + '" onChange="updateInput(this)" value="' + String(product.quantity) + '"/></td><td>' + String(Number((product.quantity * product.amount).toFixed(2))) + ' €</td></tr>';
2018-11-27 08:07:12 +00:00
}
for(k=0; k<menus.length;k++){
menu = menus[k]
2019-06-23 14:43:23 +00:00
html += '<tr><td>' + menu.name + '</td><td>' + String(menu.amount) + ' €</td><td><input type="number" data-target="' + String(k) + '" onChange="updateMenuInput(this)" value="' + String(menu.quantity) + '"/></td><td>' + String(Number((menu.quantity * menu.amount).toFixed(2))) + ' €</td></tr>';
2018-10-05 22:03:02 +00:00
}
$("#items").html(html)
2018-11-27 08:07:12 +00:00
updateTotal();
2018-10-05 22:03:02 +00:00
}
function updateTotal(){
total = 0
for(k=0;k<products.length;k++){
2018-11-27 08:07:12 +00:00
total += products[k].quantity * products[k].amount;
}
for(k=0; k<menus.length;k++){
total += menus[k].quantity * menus[k].amount;
2018-10-05 22:03:02 +00:00
}
2019-01-22 19:27:18 +00:00
for(k=0; k<cotisations.length;k++){
total += cotisations[k].quantity * cotisations[k].amount;
}
2018-12-02 18:50:15 +00:00
$("#totalAmount").text(String(Number(total.toFixed(2))) + "€")
2018-10-05 22:03:02 +00:00
totalAfter = balance - total
2018-12-02 18:50:15 +00:00
$("#totalAfter").text(String(Number(totalAfter.toFixed(2))) + "€")
2018-10-05 22:03:02 +00:00
}
2018-11-27 08:07:12 +00:00
function updateInput(a){
quantity = parseInt(a.value)
k = parseInt(a.getAttribute("data-target"))
products[k].quantity = quantity
generate_html()
}
function updateMenuInput(a){
quantity = parseInt(a.value);
k = parseInt(a.getAttribute("data-target"));
menus[k].quantity = quantity;
generate_html();
}
2019-01-22 19:27:18 +00:00
function updateCotisationInput(a){
quantity = parseInt(a.value);
k = parseInt(a.getAttribute("data-target"));
cotisations[k].quantity = quantity;
generate_html();
}
2018-10-05 22:03:02 +00:00
$(document).ready(function(){
2019-01-22 19:27:18 +00:00
$(".cotisation-hidden").hide();
get_config();
2018-12-23 22:55:27 +00:00
2018-10-05 22:03:02 +00:00
$(".product").click(function(){
product = get_product($(this).attr('target'));
});
2018-12-23 22:55:27 +00:00
2018-11-27 08:07:12 +00:00
$(".menu").click(function(){
menu = get_menu($(this).attr('target'));
2018-12-23 22:55:27 +00:00
});
2019-01-22 19:27:18 +00:00
$(".cotisation").click(function(){
cotisation = get_cotisation($(this).attr('target'));
});
2018-10-05 22:03:02 +00:00
$("#id_client").on('change', function(){
2019-01-22 19:27:18 +00:00
id_user = $("#id_client").val();
$.get("/users/getUser/" + id_user, function(data){
2018-10-05 22:03:02 +00:00
balance = data.balance;
username = data.username;
2019-01-22 19:27:18 +00:00
is_adherent = data.is_adherent;
2018-10-05 22:03:02 +00:00
$("#balance").html(balance + "€");
2019-01-22 19:27:18 +00:00
if(!is_adherent){
$(".cotisation-hidden").show();
}
2018-10-05 22:03:02 +00:00
updateTotal();
}).fail(function(){
alert("Une erreur inconnue est survenue");
window.location.reload()
});
});
2018-12-23 22:55:27 +00:00
$("#id_product").on('change', function(){
product = get_product(parseInt($("#id_product").val()));
});
2018-11-22 21:52:15 +00:00
$(".pay_button").click(function(){
2018-12-23 11:06:16 +00:00
if(use_pinte_monitoring){
message = "Il reste " + nbPintes.toString() + " pintes à renseigner. Numéro de la pinte ?"
while(nbPintes > 0){
id_pinte = window.prompt(message,"");
if(id_pinte == null){
return;
}else{
2018-12-23 11:06:16 +00:00
id_pinte = parseInt(id_pinte);
if(!Number.isInteger(id_pinte) || id_pinte < 0){
message = "Numéro incorrect. Il reste " + nbPintes.toString() + " pintes à renseigner. Numéro de la pinte ?";
}else{
listPintes.push(id_pinte)
nbPintes -= 1;
message = "Il reste " + nbPintes.toString() + " pintes à renseigner. Numéro de la pinte ?"
}
}
}
}
2019-01-22 19:27:18 +00:00
$.post("order", {"user":id_user, "paymentMethod": $(this).attr('data-payment'), "order_length": products.length + menus.length + cotisations.length, "order": JSON.stringify(products), "amount": total, "menus": JSON.stringify(menus), "listPintes": JSON.stringify(listPintes), "cotisations": JSON.stringify(cotisations)}, function(data){
2018-11-22 21:52:15 +00:00
alert(data);
location.reload();
}).fail(function(data){
2018-11-27 08:07:12 +00:00
alert("Impossible d'effectuer la transaction. Veuillez contacter le trésorier ou le président");
2018-11-22 21:52:15 +00:00
location.reload();
});
});
2018-10-05 22:03:02 +00:00
});