From 2fe0736b87e215ab823e9dbf172fe95b25b30016 Mon Sep 17 00:00:00 2001 From: fappels Date: Tue, 26 Sep 2017 12:32:50 +0200 Subject: [PATCH 1/2] Update qty dispatched on qty change Store qty dispatched and qty ordered only once per orderline --- htdocs/fourn/js/lib_dispatch.js | 55 +++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/htdocs/fourn/js/lib_dispatch.js b/htdocs/fourn/js/lib_dispatch.js index 84f74c30c54..50b6809a979 100644 --- a/htdocs/fourn/js/lib_dispatch.js +++ b/htdocs/fourn/js/lib_dispatch.js @@ -1,5 +1,5 @@ // Copyright (C) 2014 Cedric GROSS -// Copyright (C) 2015 Francis Appels +// Copyright (C) 2017 Francis Appels // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by @@ -24,7 +24,7 @@ * addDispatchLine * Adds new table row for dispatching to multiple stock locations * - * @param index int index of produt line. 0 = first product line + * @param index int index of product line. 0 = first product line * @param type string type of dispatch (batch = batch dispatch, dispatch = non batch dispatch) * @param mode string 'qtymissing' will create new line with qty missing, 'lessone' will keep 1 in old line and the rest in new one */ @@ -35,17 +35,17 @@ function addDispatchLine(index, type, mode) console.log("Split line type="+type+" index="+index+" mode="+mode); var $row = $("tr[name='"+type+'_0_'+index+"']").clone(true), // clone first batch line to jQuery object nbrTrs = $("tr[name^='"+type+"_'][name$='_"+index+"']").length, // position of line for batch - qtyOrdered = parseFloat($("#qty_ordered_"+(nbrTrs - 1)+"_"+index).val()), + qtyOrdered = parseFloat($("#qty_ordered_0_"+index).val()), qty = parseFloat($("#qty_"+(nbrTrs - 1)+"_"+index).val()), qtyDispatched; if (mode === 'lessone') { - qtyDispatched = parseFloat($("#qty_dispatched_"+(nbrTrs - 1)+"_"+index).val()) + 1; + qtyDispatched = parseFloat($("#qty_dispatched_0_"+index).val()) + 1; } else { - qtyDispatched = parseFloat($("#qty_dispatched_"+(nbrTrs - 1)+"_"+index).val()) + qty; + qtyDispatched = parseFloat($("#qty_dispatched_0_"+index).val()) + qty; } if (qtyDispatched < qtyOrdered) @@ -63,10 +63,10 @@ function addDispatchLine(index, type, mode) //insert new row before last row $("tr[name^='"+type+"_'][name$='_"+index+"']:last").after($row); //remove cloned select2 with duplicate id. - $("#s2id_entrepot_"+nbrTrs+'_'+index).detach(); + $("#s2id_entrepot_"+nbrTrs+'_'+index).detach(); /* Suffix of lines are: _ trs.length _ index */ $("#qty_"+nbrTrs+"_"+index).focus(); - $("#qty_dispatched_"+(nbrTrs)+"_"+index).val(qtyDispatched); + $("#qty_dispatched_0_"+index).val(qtyDispatched); //hide all buttons then show only the last one $("tr[name^='"+type+"_'][name$='_"+index+"'] .splitbutton").hide(); @@ -79,10 +79,47 @@ function addDispatchLine(index, type, mode) } else { - $("#qty_"+nbrTrs+"_"+index).val(qtyOrdered - qtyDispatched); + $("#qty_"+nbrTrs+"_"+index).val(qtyOrdered - qtyDispatched); + // Store arbitrary data for dispatch qty input field change event + $("#qty_"+(nbrTrs-1)+"_"+index).data('qty', qty); + $("#qty_"+(nbrTrs-1)+"_"+index).data('type', type); + $("#qty_"+(nbrTrs-1)+"_"+index).data('index', index); + // Update dispatched qty when value dispatch qty input field changed + $("#qty_"+(nbrTrs-1)+"_"+index).change(this.onChangeDispatchLineQty); } - //set focus on lot of new line (if it exists) $("#lot_number_"+(nbrTrs)+"_"+index).focus(); } +} + +/** + * onChangeDispatchLineQty + * + * event handler for dispatch qty input field + * + * element requires arbitrary data qty (value before change), type (type of dispatch) and index (index of product line) + */ + +function onChangeDispatchLineQty() { + var index = $(this).data('index'), + type = $(this).data('type'), + qty = parseFloat($(this).data('qty')), + changedQty, nbrTrs, dispatchingQty, qtyOrdered, qtyDispatched; + + if (index >= 0 && type && qty >= 0) { + nbrTrs = $("tr[name^='"+type+"_'][name$='_"+index+"']").length; + qtyChanged = parseFloat($(this).val()) - qty; // qty changed + qtyDispatching = parseFloat($("#qty_"+(nbrTrs-1)+"_"+index).val()); // qty currently being dispatched + qtyOrdered = parseFloat($("#qty_ordered_0_"+index).val()); // qty ordered + qtyDispatched = parseFloat($("#qty_dispatched_0_"+index).val()); // qty already dispatched + + // console.log("onChangeDispatchLineQty qtyChanged: " + qtyChanged + " qtyDispatching: " + qtyDispatching + " qtyOrdered: " + qtyOrdered + " qtyDispatched: "+ qtyDispatched); + + if ((qtyChanged) <= (qtyOrdered - (qtyDispatched + qtyDispatching))) { + $("#qty_dispatched_0_"+index).val(qtyDispatched + qtyChanged); + } else { + $(this).val($(this).data('qty')); + } + $(this).data('qty', $(this).val()); + } } \ No newline at end of file From d4488cb040beebd84ff0a1e2843c7e9ebea331a1 Mon Sep 17 00:00:00 2001 From: fappels Date: Tue, 26 Sep 2017 15:44:40 +0200 Subject: [PATCH 2/2] Fix for 'lessone' mode --- htdocs/fourn/js/lib_dispatch.js | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/htdocs/fourn/js/lib_dispatch.js b/htdocs/fourn/js/lib_dispatch.js index 50b6809a979..2b39c18cbb2 100644 --- a/htdocs/fourn/js/lib_dispatch.js +++ b/htdocs/fourn/js/lib_dispatch.js @@ -35,7 +35,7 @@ function addDispatchLine(index, type, mode) console.log("Split line type="+type+" index="+index+" mode="+mode); var $row = $("tr[name='"+type+'_0_'+index+"']").clone(true), // clone first batch line to jQuery object nbrTrs = $("tr[name^='"+type+"_'][name$='_"+index+"']").length, // position of line for batch - qtyOrdered = parseFloat($("#qty_ordered_0_"+index).val()), + qtyOrdered = parseFloat($("#qty_ordered_0_"+index).val()), // Qty ordered is same for all rows qty = parseFloat($("#qty_"+(nbrTrs - 1)+"_"+index).val()), qtyDispatched; @@ -74,19 +74,16 @@ function addDispatchLine(index, type, mode) if (mode === 'lessone') { - $("#qty_"+(nbrTrs)+"_"+index).val(qty-1); - $("#qty_"+(nbrTrs-1)+"_"+index).val(1); - } - else - { - $("#qty_"+nbrTrs+"_"+index).val(qtyOrdered - qtyDispatched); - // Store arbitrary data for dispatch qty input field change event - $("#qty_"+(nbrTrs-1)+"_"+index).data('qty', qty); - $("#qty_"+(nbrTrs-1)+"_"+index).data('type', type); - $("#qty_"+(nbrTrs-1)+"_"+index).data('index', index); - // Update dispatched qty when value dispatch qty input field changed - $("#qty_"+(nbrTrs-1)+"_"+index).change(this.onChangeDispatchLineQty); + qty = 1; // keep 1 in old line + $("#qty_"+(nbrTrs-1)+"_"+index).val(qty); } + $("#qty_"+nbrTrs+"_"+index).val(qtyOrdered - qtyDispatched); + // Store arbitrary data for dispatch qty input field change event + $("#qty_"+(nbrTrs-1)+"_"+index).data('qty', qty); + $("#qty_"+(nbrTrs-1)+"_"+index).data('type', type); + $("#qty_"+(nbrTrs-1)+"_"+index).data('index', index); + // Update dispatched qty when value dispatch qty input field changed + $("#qty_"+(nbrTrs-1)+"_"+index).change(this.onChangeDispatchLineQty); //set focus on lot of new line (if it exists) $("#lot_number_"+(nbrTrs)+"_"+index).focus(); } @@ -95,7 +92,9 @@ function addDispatchLine(index, type, mode) /** * onChangeDispatchLineQty * - * event handler for dispatch qty input field + * Change event handler for dispatch qty input field, + * recalculate qty dispatched when qty input has changed. + * If qty is more then qty ordered reset input qty to max qty to dispatch. * * element requires arbitrary data qty (value before change), type (type of dispatch) and index (index of product line) */ @@ -113,7 +112,7 @@ function onChangeDispatchLineQty() { qtyOrdered = parseFloat($("#qty_ordered_0_"+index).val()); // qty ordered qtyDispatched = parseFloat($("#qty_dispatched_0_"+index).val()); // qty already dispatched - // console.log("onChangeDispatchLineQty qtyChanged: " + qtyChanged + " qtyDispatching: " + qtyDispatching + " qtyOrdered: " + qtyOrdered + " qtyDispatched: "+ qtyDispatched); + console.log("onChangeDispatchLineQty qtyChanged: " + qtyChanged + " qtyDispatching: " + qtyDispatching + " qtyOrdered: " + qtyOrdered + " qtyDispatched: "+ qtyDispatched); if ((qtyChanged) <= (qtyOrdered - (qtyDispatched + qtyDispatching))) { $("#qty_dispatched_0_"+index).val(qtyDispatched + qtyChanged);