Commit 870ff1e2 by Damien Moulard

INV: save new qty only when validate pressed, not when cancel pressed

parent 4f963d49
...@@ -58,12 +58,13 @@ function back() { ...@@ -58,12 +58,13 @@ function back() {
function get_added_qties_sum(item) { function get_added_qties_sum(item) {
let total = null; let total = null;
function add(accumulator, a) { // for array sum function add(accumulator, a) { // for array sum
result = 0; result = 0;
if (a) { if (a) {
if (item.uom_id[1] == "kg") { if (item.uom_id[1] == "kg") {
if (typeof a === "string") { if (typeof a === "string") {
a = a.replace(',','.') a = a.replace(',', '.');
} }
result = parseFloat(accumulator) + parseFloat(a); result = parseFloat(accumulator) + parseFloat(a);
result = result.toFixed(3); result = result.toFixed(3);
...@@ -71,6 +72,7 @@ function get_added_qties_sum(item) { ...@@ -71,6 +72,7 @@ function get_added_qties_sum(item) {
result = parseInt(accumulator, 10) + parseInt(a, 10); result = parseInt(accumulator, 10) + parseInt(a, 10);
} }
} }
return result; return result;
} }
if (typeof item.added_qties != "undefined" && item.added_qties.length > 0) { if (typeof item.added_qties != "undefined" && item.added_qties.length > 0) {
...@@ -82,6 +84,7 @@ function get_added_qties_sum(item) { ...@@ -82,6 +84,7 @@ function get_added_qties_sum(item) {
} }
function barcode_analyzer(chars) { function barcode_analyzer(chars) {
let barcode = chars; let barcode = chars;
if (barcode && barcode.length >=13) { if (barcode && barcode.length >=13) {
barcode = barcode.substring(barcode.length-13); barcode = barcode.substring(barcode.length-13);
} else if (barcode && barcode.length == 12 && barcode.indexOf('0') !== 0) { } else if (barcode && barcode.length == 12 && barcode.indexOf('0') !== 0) {
...@@ -108,15 +111,23 @@ function barcode_analyzer(chars) { ...@@ -108,15 +111,23 @@ function barcode_analyzer(chars) {
} }
} }
function reset_previous_value() { /**
if (editing_item !== null) { * Option to display an icon next to edition input to cancel last value.
if (typeof editing_item.added_qties !== "undefined") { * Disabled for now. Look for #reset_to_previous_qty to restore.
editing_item.qty -= editing_item.added_qties.pop(); *
} * WARNING: if you restore this functionality, update validateEdition() so
$('#edition_input').val(editing_item.qty); * canceling last value is ignored if edition_cancel is pressed.
$("#reset_to_previous_qty").hide(); */
} // function reset_previous_value() {
} // if (editing_item !== null) {
// if (typeof editing_item.added_qties !== "undefined") {
// editing_item.qty -= editing_item.added_qties.pop();
// }
// $('#edition_input').val(editing_item.qty);
// $("#reset_to_previous_qty").hide();
// }
// }
function refresh() { function refresh() {
location.reload(); location.reload();
} }
...@@ -125,6 +136,7 @@ function refresh() { ...@@ -125,6 +136,7 @@ function refresh() {
function select_product_from_bc(barcode) { function select_product_from_bc(barcode) {
var found = null, var found = null,
qty = null; qty = null;
if (isValidEAN13(barcode)) { if (isValidEAN13(barcode)) {
var scannedProduct = barcodes.get_corresponding_odoo_product(barcode); var scannedProduct = barcodes.get_corresponding_odoo_product(barcode);
...@@ -155,9 +167,13 @@ function select_product_from_bc(barcode) { ...@@ -155,9 +167,13 @@ function select_product_from_bc(barcode) {
found = e; found = e;
if (qty) { if (qty) {
let message = "Attention, ce produit a déjà été compté.\n"; let message = "Attention, ce produit a déjà été compté.\n";
message += "La quantité " + qty + " n'a pas été ajoutée !\n"; message += "La quantité " + qty + " n'a pas été ajoutée !\n";
// temporary add read qty and recorded one to added_qties to compute sum // temporary add read qty and recorded one to added_qties to compute sum
found.added_qties = [qty, found.qty] found.added_qties = [
qty,
found.qty
];
message += "Le total serait " + get_added_qties_sum(found); message += "Le total serait " + get_added_qties_sum(found);
alert(message); alert(message);
qty = null; qty = null;
...@@ -173,17 +189,20 @@ function select_product_from_bc(barcode) { ...@@ -173,17 +189,20 @@ function select_product_from_bc(barcode) {
setLineEdition(found, qty); setLineEdition(found, qty);
if (editing_origin === 'to_process') { if (editing_origin === 'to_process') {
let row = table_to_process.row($('tr#'+found.id)); let row = table_to_process.row($('tr#'+found.id));
remove_from_toProcess(row); remove_from_toProcess(row);
} else { } else {
let row = table_processed.row($('tr#'+found.id)); let row = table_processed.row($('tr#'+found.id));
remove_from_processed(row); remove_from_processed(row);
} }
} else { } else {
console.log('Code barre introuvable'); console.log('Code barre introuvable');
} }
} else if (barcode == editing_item.barcode && qty){ } else if (barcode == editing_item.barcode && qty) {
// We scan the same product as the current one // We scan the same product as the current one
let edition_input = $('#edition_input'); let edition_input = $('#edition_input');
if (typeof editing_item.added_qties == "undefined") { if (typeof editing_item.added_qties == "undefined") {
editing_item.added_qties = [edition_input.val()]; editing_item.added_qties = [edition_input.val()];
} }
...@@ -289,12 +308,38 @@ function clearLineEdition() { ...@@ -289,12 +308,38 @@ function clearLineEdition() {
$('#product_name').text(''); $('#product_name').text('');
$('#edition_input').val(''); $('#edition_input').val('');
$('#search_input').focus(); $('#search_input').focus();
$("#reset_to_previous_qty").hide(); // $("#reset_to_previous_qty").hide();
} }
// Validate product edition /**
* Validate product edition.
* Keep track of every qty change.
*/
function validateEdition() { function validateEdition() {
if (editing_item != null) { if (editing_item != null) {
const current_val = $("#edition_input").val();
// Let's verify if quantity have been changed
if (current_val != editing_item.qty) {
if (typeof editing_item.added_qties !== "undefined") {
// total may have been affected by manual typing
const total = get_added_qties_sum(editing_item);
if (current_val != total) {
// add difference in added_qties array
editing_item.added_qties.push(current_val - total);
}
} else {
// Init added_qties to take change into account
editing_item.added_qties = [
editing_item.qty,
current_val - editing_item.qty
];
}
}
editing_item.qty = current_val;
if (editProductInfo(editing_item)) { if (editProductInfo(editing_item)) {
clearLineEdition(); clearLineEdition();
} }
...@@ -377,9 +422,10 @@ function record_products_shelf_on_server(data) { ...@@ -377,9 +422,10 @@ function record_products_shelf_on_server(data) {
// call on change_shelf_btn click action // call on change_shelf_btn click action
async function open_change_shelf_modal() { async function open_change_shelf_modal() {
selected_products_for_shelf_change = []; selected_products_for_shelf_change = [];
$('.select_product_cb:checked').each(function(idx,elt){ $('.select_product_cb:checked').each(function(idx, elt) {
const row = $(elt).closest('tr'); const row = $(elt).closest('tr');
selected_products_for_shelf_change.push(table_to_process.row(row).data())
selected_products_for_shelf_change.push(table_to_process.row(row).data());
}); });
if (selected_products_for_shelf_change.length > 0) { if (selected_products_for_shelf_change.length > 0) {
/* /*
...@@ -388,6 +434,7 @@ async function open_change_shelf_modal() { ...@@ -388,6 +434,7 @@ async function open_change_shelf_modal() {
*/ */
let shelfs = await get_all_shelfs(); let shelfs = await get_all_shelfs();
if (shelfs !== null) { if (shelfs !== null) {
let modal_content = $('#templates #change_shelf_form').clone(), let modal_content = $('#templates #change_shelf_form').clone(),
shelf_selector = $('<select>').addClass('shelf_selection'), shelf_selector = $('<select>').addClass('shelf_selection'),
...@@ -395,12 +442,13 @@ async function open_change_shelf_modal() { ...@@ -395,12 +442,13 @@ async function open_change_shelf_modal() {
/* construct shelfs selector */ /* construct shelfs selector */
// first of all, sort by name // first of all, sort by name
shelfs.sort((a,b) => (a.name > b.name) ? 1 : ((b.name > a.name) ? -1 : 0)); shelfs.sort((a, b) => (a.name > b.name) ? 1 : ((b.name > a.name) ? -1 : 0));
// if ahead_shelfs_ids is not empty, put them ahead // if ahead_shelfs_ids is not empty, put them ahead
if (ahead_shelfs_ids.length > 0) { if (ahead_shelfs_ids.length > 0) {
let to_move = {}, let to_move = {},
idx = 0; idx = 0;
// find index of shelfs to move // find index of shelfs to move
shelfs.forEach((shelf) => { shelfs.forEach((shelf) => {
if (ahead_shelfs_ids.indexOf(shelf.id) > -1) { if (ahead_shelfs_ids.indexOf(shelf.id) > -1) {
to_move[shelf.id] = idx; to_move[shelf.id] = idx;
...@@ -410,27 +458,30 @@ async function open_change_shelf_modal() { ...@@ -410,27 +458,30 @@ async function open_change_shelf_modal() {
// Respecting ahead_shelfs_ids order, move shelf ahead // Respecting ahead_shelfs_ids order, move shelf ahead
// splice can not be used, since more than 1 elt could be involved // splice can not be used, since more than 1 elt could be involved
let ahead_elts = []; let ahead_elts = [];
ahead_shelfs_ids.forEach((shelf_id) => { ahead_shelfs_ids.forEach((shelf_id) => {
let shelf = shelfs[to_move[shelf_id]]; let shelf = shelfs[to_move[shelf_id]];
ahead_elts.push(shelf); ahead_elts.push(shelf);
}); });
//remove ahead elts //remove ahead elts
shelfs = shelfs.filter((item) => {return !ahead_elts.includes(item.id)}); shelfs = shelfs.filter((item) => {
return !ahead_elts.includes(item.id);
});
// put them ahead by concatenation // put them ahead by concatenation
shelfs = ahead_elts.concat(shelfs); shelfs = ahead_elts.concat(shelfs);
} }
shelfs.forEach( shelfs.forEach((shelf) => {
(shelf) => {
let option = $('<option>') let option = $('<option>')
.val(shelf.id) .val(shelf.id)
.text(shelf.name + ' (' + shelf.sort_order + ')'); .text(shelf.name + ' (' + shelf.sort_order + ')');
shelf_selector.append(option); shelf_selector.append(option);
}); });
/* add product rows */ /* add product rows */
selected_products_for_shelf_change.forEach( selected_products_for_shelf_change.forEach((product) => {
(product) => { let tr = $('<tr>').attr('data-id', product.id)
let tr = $('<tr>').attr('data-id',product.id)
.append($('<td>').text(product.name)) .append($('<td>').text(product.name))
.append($('<td>').append(shelf_selector.clone())); .append($('<td>').append(shelf_selector.clone()));
...@@ -444,19 +495,22 @@ async function open_change_shelf_modal() { ...@@ -444,19 +495,22 @@ async function open_change_shelf_modal() {
make_change = async () => { make_change = async () => {
// Prepare data to be transmitted to server to be recorded // Prepare data to be transmitted to server to be recorded
let data = []; let data = [];
$('.overlay-content table tbody tr').each(function(idx,e){
$('.overlay-content table tbody tr').each(function(idx, e) {
data.push({ data.push({
product_id : $(e).data('id'), product_id : $(e).data('id'),
shelf_id : $(e).find('select').val() shelf_id : $(e).find('select')
.val()
}); });
}); });
const update_result = await record_products_shelf_on_server(data); const update_result = await record_products_shelf_on_server(data);
if (update_result !== null) { if (update_result !== null) {
update_result.forEach( update_result.forEach((product_id) => {
(product_id) => {
remove_from_toProcess(table_to_process.row($('tr#'+product_id))); remove_from_toProcess(table_to_process.row($('tr#'+product_id)));
}); });
let message = "L'opération a bien réussi."; let message = "L'opération a bien réussi.";
if (update_result.length !== data.length) { if (update_result.length !== data.length) {
message = "L'opération a partiellement réussi.\n"; message = "L'opération a partiellement réussi.\n";
message += (data.length - update_result.length) + " produit(s) non déplacé(s)."; message += (data.length - update_result.length) + " produit(s) non déplacé(s).";
...@@ -470,14 +524,14 @@ async function open_change_shelf_modal() { ...@@ -470,14 +524,14 @@ async function open_change_shelf_modal() {
} }
); );
} }
} };
make_change(); make_change();
} }
}, },
'Changer les produits de rayons' 'Changer les produits de rayons'
); );
} else { } else {
alert("Les informations des autres rayons n'ont pas pu être récupérées.") alert("Les informations des autres rayons n'ont pas pu être récupérées.");
} }
} }
...@@ -1121,36 +1175,24 @@ function init() { ...@@ -1121,36 +1175,24 @@ function init() {
$(document.documentElement).scrollTop(scrollTo); $(document.documentElement).scrollTop(scrollTo);
*/ */
}); });
if ($(this).val().length > 0) {
let reset_icon = $("#reset_to_previous_qty"); // if ($(this).val().length > 0) {
reset_icon.show(); // let reset_icon = $("#reset_to_previous_qty");
reset_icon.off(); // reset_icon.show();
reset_icon.on("click", reset_previous_value); // reset_icon.off();
} else { // reset_icon.on("click", reset_previous_value);
$("#reset_to_previous_qty").hide(); // } else {
} // $("#reset_to_previous_qty").hide();
// }
}) })
.on('blur', function () { .on('blur', function () {
const current_val = $(this).val();
$(this).off('wheel.disableScroll'); $(this).off('wheel.disableScroll');
});
if (editing_item !== null) { $("#edition_input").keypress(function(event) {
//Let's verify if quantity have be changed // Force validation when enter pressed in edition
if (current_val != editing_item.qty) { if (event.keyCode == 13 || event.which == 13) {
if (typeof editing_item.added_qties !== "undefined") { validateEdition();
// total may have been affected by manual typing
const total = get_added_qties_sum(editing_item);
if (current_val != total) {
// add difference in added_qties array
editing_item.added_qties.push(current_val - total);
}
} else {
// Init added_qties to take change into account
editing_item.added_qties = [editing_item.qty, current_val - editing_item.qty];
}
}
editing_item.qty = current_val;
} }
}); });
...@@ -1209,11 +1251,11 @@ function init() { ...@@ -1209,11 +1251,11 @@ function init() {
} }
}); });
$(document).pos(); $(document).pos();
$(document).on('scan.pos.barcode', function(event) { $(document).on('scan.pos.barcode', function(event) {
//access `event.code` - barcode data //access `event.code` - barcode data
var barcode = event.code; var barcode = event.code;
barcode_analyzer(barcode); barcode_analyzer(barcode);
}); });
......
...@@ -357,7 +357,7 @@ var is_product_in_shelf_adding_queue_list = function(testing_pid) { ...@@ -357,7 +357,7 @@ var is_product_in_shelf_adding_queue_list = function(testing_pid) {
var printProduct = function () { var printProduct = function () {
let clicked = $(this); let clicked = $(this);
let tr_to_print = clicked.closest('tr'); let tr_to_print = clicked.closest('tr');
let barcode = tr_to_print.data('bc') let barcode = tr_to_print.data('bc');
openModal(); openModal();
try { try {
...@@ -366,8 +366,9 @@ var printProduct = function () { ...@@ -366,8 +366,9 @@ var printProduct = function () {
data: {'barcode': barcode} data: {'barcode': barcode}
}) })
.done(function(res) { .done(function(res) {
var product = res.product var product = res.product;
var product_tmpl_id = product.product_tmpl_id[0] var product_tmpl_id = product.product_tmpl_id[0];
$.ajax({ $.ajax({
url: '/products/label_print/' + product_tmpl_id url: '/products/label_print/' + product_tmpl_id
}) })
...@@ -379,9 +380,9 @@ var printProduct = function () { ...@@ -379,9 +380,9 @@ var printProduct = function () {
} else { } else {
alert('Impression lancée'); alert('Impression lancée');
} }
}) });
}) });
} catch(e) { } catch (e) {
closeModal(); closeModal();
alert('Une erreur est survenue...'); alert('Une erreur est survenue...');
} }
......
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