Commit 7078ba9e by Damien Moulard Committed by Alexis Aoun

linting

parent 9f41ce36
...@@ -5,23 +5,27 @@ var suppliers_list = [], ...@@ -5,23 +5,27 @@ var suppliers_list = [],
/** /**
* Add a supplier to the selected suppliers list. * Add a supplier to the selected suppliers list.
* *
* @returns -1 if validation failed, void otherwise * @returns -1 if validation failed, 0 otherwise
*/ */
function add_supplier() { function add_supplier() {
const user_input = $("#supplier_input").val(); const user_input = $("#supplier_input").val();
// Check if user input is a valid supplier // Check if user input is a valid supplier
const supplier = suppliers_list.find(s => s.display_name === user_input) const supplier = suppliers_list.find(s => s.display_name === user_input);
if (supplier === undefined) { if (supplier === undefined) {
alert("Le fournisseur renseigné n'est pas valide.\n" alert("Le fournisseur renseigné n'est pas valide.\n"
+ "Veuillez sélectionner un fournisseur dans la liste déroulante."); + "Veuillez sélectionner un fournisseur dans la liste déroulante.");
return -1; return -1;
} }
const supplier_selected = selected_suppliers.find(s => s.display_name === user_input) const supplier_selected = selected_suppliers.find(s => s.display_name === user_input);
if (supplier_selected !== undefined) { if (supplier_selected !== undefined) {
alert("Ce fournisseur est déjà sélectionné."); alert("Ce fournisseur est déjà sélectionné.");
return -1; return -1;
} }
...@@ -30,6 +34,7 @@ function add_supplier() { ...@@ -30,6 +34,7 @@ function add_supplier() {
selected_suppliers.push(supplier); selected_suppliers.push(supplier);
let url = "/orders/get_supplier_products"; let url = "/orders/get_supplier_products";
url += "?sid=" + encodeURIComponent(supplier.id); url += "?sid=" + encodeURIComponent(supplier.id);
// Fetch supplier products // Fetch supplier products
...@@ -42,7 +47,7 @@ function add_supplier() { ...@@ -42,7 +47,7 @@ function add_supplier() {
success: function(data) { success: function(data) {
save_supplier_products(supplier, data.res.products); save_supplier_products(supplier, data.res.products);
update_display(); update_display();
$("#supplier_input").val("") $("#supplier_input").val("");
closeModal(); closeModal();
}, },
error: function(data) { error: function(data) {
...@@ -56,24 +61,26 @@ function add_supplier() { ...@@ -56,24 +61,26 @@ function add_supplier() {
alert('Erreur lors de la récupération des produits, réessayer plus tard.'); alert('Erreur lors de la récupération des produits, réessayer plus tard.');
} }
}); });
return 0;
} }
/** /**
* Remove a supplier from the selected list & its associated products * Remove a supplier from the selected list & its associated products
* *
* @param {int} supplier_id * @param {int} supplier_id
*/ */
function remove_supplier(supplier_id) { function remove_supplier(supplier_id) {
// Remove from suppliers list // Remove from suppliers list
selected_suppliers = selected_suppliers.filter(supplier => supplier.id != supplier_id) selected_suppliers = selected_suppliers.filter(supplier => supplier.id != supplier_id);
// Remove the supplier from the products suppliers list // Remove the supplier from the products suppliers list
for (const i in products) { for (const i in products) {
products[i].suppliers = products[i].suppliers.filter(supplier => supplier.id != supplier_id) products[i].suppliers = products[i].suppliers.filter(supplier => supplier.id != supplier_id);
} }
// Remove products only associated to this product // Remove products only associated to this product
products = products.filter(product => product.suppliers.length > 0) products = products.filter(product => product.suppliers.length > 0);
update_display(); update_display();
} }
...@@ -82,9 +89,9 @@ function remove_supplier(supplier_id) { ...@@ -82,9 +89,9 @@ function remove_supplier(supplier_id) {
* When products are fetched, save them and the relation with the supplier. * When products are fetched, save them and the relation with the supplier.
* If product already saved, add the supplier to its suppliers list. * If product already saved, add the supplier to its suppliers list.
* Else, add product with supplier. * Else, add product with supplier.
* *
* @param {object} supplier * @param {object} supplier
* @param {array} new_products * @param {array} new_products
*/ */
function save_supplier_products(supplier, new_products) { function save_supplier_products(supplier, new_products) {
for (np of new_products) { for (np of new_products) {
...@@ -92,19 +99,19 @@ function save_supplier_products(supplier, new_products) { ...@@ -92,19 +99,19 @@ function save_supplier_products(supplier, new_products) {
if (index === -1) { if (index === -1) {
np.suppliers = [{ ...supplier }]; np.suppliers = [{ ...supplier }];
products.push(np) products.push(np);
} else { } else {
products[index].suppliers.push({ ...supplier }) products[index].suppliers.push({ ...supplier });
} }
} }
} }
/** /**
* Save the quantity set for a product/supplier * Save the quantity set for a product/supplier
* *
* @param {int} prod_id * @param {int} prod_id
* @param {int} supplier_id * @param {int} supplier_id
* @param {float} val * @param {float} val
*/ */
function save_product_supplier_qty(prod_id, supplier_id, val) { function save_product_supplier_qty(prod_id, supplier_id, val) {
for (const i in products) { for (const i in products) {
...@@ -121,9 +128,9 @@ function save_product_supplier_qty(prod_id, supplier_id, val) { ...@@ -121,9 +128,9 @@ function save_product_supplier_qty(prod_id, supplier_id, val) {
/** /**
* Look in the 'suppliers' property of a product * Look in the 'suppliers' property of a product
* *
* @param {object} product * @param {object} product
* @param {object} supplier * @param {object} supplier
* @returns boolean * @returns boolean
*/ */
function is_product_related_to_supplier(product, supplier) { function is_product_related_to_supplier(product, supplier) {
...@@ -143,21 +150,25 @@ function supplier_column_name(supplier) { ...@@ -143,21 +150,25 @@ function supplier_column_name(supplier) {
*/ */
function display_suppliers() { function display_suppliers() {
let supplier_container = $("#suppliers_container"); let supplier_container = $("#suppliers_container");
$("#suppliers_container").empty(); $("#suppliers_container").empty();
for (supplier of selected_suppliers) { for (supplier of selected_suppliers) {
let template = $("#templates #supplier_pill") let template = $("#templates #supplier_pill");
template.find(".supplier_name").text(supplier.display_name); template.find(".supplier_name").text(supplier.display_name);
template.find(".remove_supplier_icon").attr('id', `remove_supplier_${supplier.id}`) template.find(".remove_supplier_icon").attr('id', `remove_supplier_${supplier.id}`);
supplier_container.append(template.html()); supplier_container.append(template.html());
} }
$(".remove_supplier_icon").on("click", function(e) { $(".remove_supplier_icon").on("click", function() {
const el_id = $(this).attr('id').split('_'); const el_id = $(this).attr('id')
.split('_');
const supplier_id = el_id[el_id.length-1]; const supplier_id = el_id[el_id.length-1];
let modal_remove_supplier = $('#templates #modal_remove_supplier'); let modal_remove_supplier = $('#templates #modal_remove_supplier');
modal_remove_supplier.find(".supplier_name").text(supplier.display_name); modal_remove_supplier.find(".supplier_name").text(supplier.display_name);
openModal( openModal(
...@@ -165,9 +176,9 @@ function display_suppliers() { ...@@ -165,9 +176,9 @@ function display_suppliers() {
() => { () => {
remove_supplier(supplier_id); remove_supplier(supplier_id);
}, },
'Valider', 'Valider'
); );
}) });
} }
/* DATATABLE */ /* DATATABLE */
...@@ -182,7 +193,7 @@ function prepare_datatable_data() { ...@@ -182,7 +193,7 @@ function prepare_datatable_data() {
let item = { let item = {
id: product.id, id: product.id,
name: product.name name: product.name
} };
// If product not related to supplier : false ; else null (qty to be set) or qty // If product not related to supplier : false ; else null (qty to be set) or qty
for (product_supplier of product.suppliers) { for (product_supplier of product.suppliers) {
...@@ -213,7 +224,7 @@ function prepare_datatable_columns() { ...@@ -213,7 +224,7 @@ function prepare_datatable_columns() {
}, },
{ {
data: "name", data: "name",
title: "Produit", title: "Produit"
} }
]; ];
...@@ -227,11 +238,13 @@ function prepare_datatable_columns() { ...@@ -227,11 +238,13 @@ function prepare_datatable_columns() {
if (data === false) { if (data === false) {
return "X"; return "X";
} else { } else {
const input_id = `product_${full.id}_supplier_${supplier.id}_qty_input` const input_id = `product_${full.id}_supplier_${supplier.id}_qty_input`;
return `<input type="number" class="product_qty_input" id=${input_id} value=${data}>`
return `<input type="number" class="product_qty_input" id=${input_id} value=${data}>`;
} }
} }
}) });
} }
return columns; return columns;
...@@ -244,6 +257,7 @@ function display_products() { ...@@ -244,6 +257,7 @@ function display_products() {
// Empty datatable if already exists // Empty datatable if already exists
if (products.length == 0) { if (products.length == 0) {
$('.main').hide(); $('.main').hide();
return -1; return -1;
} }
...@@ -264,7 +278,7 @@ function display_products() { ...@@ -264,7 +278,7 @@ function display_products() {
"asc" "asc"
] ]
], ],
dom: 'lrtip', // TODO: change DOM display? dom: 'lrtip', // TODO: change DOM display?
iDisplayLength: 100, iDisplayLength: 100,
language: {url : '/static/js/datatables/french.json'} language: {url : '/static/js/datatables/french.json'}
}); });
...@@ -273,16 +287,20 @@ function display_products() { ...@@ -273,16 +287,20 @@ function display_products() {
// Save value on inputs change // Save value on inputs change
$('#products_table').on('input', 'tbody td .product_qty_input', function () { $('#products_table').on('input', 'tbody td .product_qty_input', function () {
let val = parseFloat($(this).val()) let val = parseFloat($(this).val());
// If value is a number // If value is a number
if (!isNaN(val)) { if (!isNaN(val)) {
const id_split = $(this).attr('id').split('_') const id_split = $(this).attr('id')
.split('_');
const prod_id = id_split[1]; const prod_id = id_split[1];
const supplier_id = id_split[3]; const supplier_id = id_split[3];
save_product_supplier_qty(prod_id, supplier_id, val); save_product_supplier_qty(prod_id, supplier_id, val);
} }
}); });
return 0;
} }
/** /**
...@@ -309,7 +327,7 @@ $(document).ready(function() { ...@@ -309,7 +327,7 @@ $(document).ready(function() {
suppliers_list = data.res; suppliers_list = data.res;
// Set up autocomplete on supplier input // Set up autocomplete on supplier input
$( "#supplier_input" ).autocomplete({ $("#supplier_input").autocomplete({
source: suppliers_list.map(a => a.display_name) source: suppliers_list.map(a => a.display_name)
}); });
...@@ -330,7 +348,7 @@ $(document).ready(function() { ...@@ -330,7 +348,7 @@ $(document).ready(function() {
$("#supplier_form").on("submit", function(e) { $("#supplier_form").on("submit", function(e) {
e.preventDefault(); e.preventDefault();
add_supplier(); add_supplier();
}) });
// TODO: on click on 'X' change to input // TODO: on click on 'X' change to input
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment