total = 0 products = [] menus = [] cotisations = [] paymentMethod = null balance = 0 username = "" id_user = 0 listPintes = [] nbPintes = 0; use_pinte_monitoring = false; function get_config(){ res = $.get("../preferences/getConfig", function(data){ use_pinte_monitoring = data.use_pinte_monitoring; }); } function get_product(id){ res = $.get("getProduct/" + id, function(data){ nbPintes += data.nb_pintes; add_product(data.pk, data.barcode, data.name, data.amount, data.needQuantityButton); }); } function get_menu(id){ res = $.get("getMenu/" + id, function(data){ nbPintes += data.nb_pintes; add_menu(data.pk, data.barcode, data.name, data.amount, data.needQuantityButton); }); } function get_cotisation(id){ res = $.get("../preferences/getCotisation/" + id, function(data){ add_cotisation(data.pk, "", data.duration, data.amount, data.needQuantityButton); }); } function add_product(pk, barcode, name, amount, needQuantityButton){ exist = false index = -1; 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; } if(exist){ products[index].quantity += quantity; }else{ products.push({"pk": pk, "barcode": barcode, "name": name, "amount": amount, "quantity": quantity}); } generate_html() } function add_menu(pk, barcode, name, amount){ 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{ menus.push({"pk": pk, "barcode": barcode, "name": name, "amount": amount, "quantity":1}); } generate_html(); } function add_cotisation(pk, barcode, duration, amount){ 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{ cotisations.push({"pk": pk, "barcode": barcode, "duration": duration, "amount": amount, "quantity":1}); } generate_html(); } function generate_html(){ html = ""; for(k=0;k' + String(cotisation.amount) + ' €' + String(Number((cotisation.quantity * cotisation.amount).toFixed(2))) + ' €'; } for(k=0;k' + product.name + '' + String(product.amount) + ' €' + String(Number((product.quantity * product.amount).toFixed(2))) + ' €'; } for(k=0; k' + menu.name + '' + String(menu.amount) + ' €' + String(Number((menu.quantity * menu.amount).toFixed(2))) + ' €'; } $("#items").html(html) updateTotal(); } function updateTotal(){ total = 0 for(k=0;k 0){ id_pinte = window.prompt(message,""); if(id_pinte == null){ return; }else{ 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 ?" } } } } $.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){ alert(data); location.reload(); }).fail(function(data){ alert("Impossible d'effectuer la transaction. Veuillez contacter le trésorier ou le président"); location.reload(); }); }); });