lib_dispatch.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright (C) 2014 Cedric GROSS <c.gross@kreiz-it.fr>
  2. // Copyright (C) 2017 Francis Appels <francis.appels@z-application.com>
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. // or see http://www.gnu.org/
  17. //
  18. // \file htdocs/core/js/lib_dispatch.js
  19. // \brief File that include javascript functions used dispatch.php
  20. //
  21. /**
  22. * addDispatchLine
  23. * Adds new table row for dispatching to multiple stock locations
  24. *
  25. * @param index int index of product line. 0 = first product line
  26. * @param type string type of dispatch (batch = batch dispatch, dispatch = non batch dispatch)
  27. * @param mode string 'qtymissing' will create new line with qty missing, 'lessone' will keep 1 in old line and the rest in new one
  28. */
  29. function addDispatchLine(index, type, mode)
  30. {
  31. mode = mode || 'qtymissing'
  32. console.log("fourn/js/lib_dispatch.js Split line type="+type+" index="+index+" mode="+mode);
  33. var $row = $("tr[name='"+type+'_0_'+index+"']").clone(true), // clone first batch line to jQuery object
  34. nbrTrs = $("tr[name^='"+type+"_'][name$='_"+index+"']").length, // position of line for batch
  35. qtyOrdered = parseFloat($("#qty_ordered_0_"+index).val()), // Qty ordered is same for all rows
  36. qty = parseFloat($("#qty_"+(nbrTrs - 1)+"_"+index).val()),
  37. qtyDispatched;
  38. if (mode === 'lessone')
  39. {
  40. qtyDispatched = parseFloat($("#qty_dispatched_0_"+index).val()) + 1;
  41. }
  42. else
  43. {
  44. qtyDispatched = parseFloat($("#qty_dispatched_0_"+index).val()) + qty;
  45. }
  46. if (qtyDispatched < qtyOrdered)
  47. {
  48. //replace tr suffix nbr
  49. $row.html($row.html().replace(/_0_/g,"_"+nbrTrs+"_"));
  50. //create new select2 to avoid duplicate id of cloned one
  51. $row.find("select[name='"+'entrepot_'+nbrTrs+'_'+index+"']").select2();
  52. // TODO find solution to copy selected option to new select
  53. // TODO find solution to keep new tr's after page refresh
  54. //clear value
  55. $row.find("input[name^='qty']").val('');
  56. //change name of new row
  57. $row.attr('name',type+'_'+nbrTrs+'_'+index);
  58. //insert new row before last row
  59. $("tr[name^='"+type+"_'][name$='_"+index+"']:last").after($row);
  60. //remove cloned select2 with duplicate id.
  61. $("#s2id_entrepot_"+nbrTrs+'_'+index).detach(); // old way to find duplicated select2 component
  62. $(".csswarehouse_"+nbrTrs+"_"+index+":first-child").parent("span.selection").parent(".select2").detach();
  63. /* Suffix of lines are: _ trs.length _ index */
  64. $("#qty_"+nbrTrs+"_"+index).focus();
  65. $("#qty_dispatched_0_"+index).val(qtyDispatched);
  66. //hide all buttons then show only the last one
  67. $("tr[name^='"+type+"_'][name$='_"+index+"'] .splitbutton").hide();
  68. $("tr[name^='"+type+"_'][name$='_"+index+"']:last .splitbutton").show();
  69. if (mode === 'lessone')
  70. {
  71. qty = 1; // keep 1 in old line
  72. $("#qty_"+(nbrTrs-1)+"_"+index).val(qty);
  73. }
  74. $("#qty_"+nbrTrs+"_"+index).val(qtyOrdered - qtyDispatched);
  75. // Store arbitrary data for dispatch qty input field change event
  76. $("#qty_"+(nbrTrs-1)+"_"+index).data('qty', qty);
  77. $("#qty_"+(nbrTrs-1)+"_"+index).data('type', type);
  78. $("#qty_"+(nbrTrs-1)+"_"+index).data('index', index);
  79. // Update dispatched qty when value dispatch qty input field changed
  80. $("#qty_"+(nbrTrs-1)+"_"+index).change(this.onChangeDispatchLineQty);
  81. //set focus on lot of new line (if it exists)
  82. $("#lot_number_"+(nbrTrs)+"_"+index).focus();
  83. }
  84. }
  85. /**
  86. * onChangeDispatchLineQty
  87. *
  88. * Change event handler for dispatch qty input field,
  89. * recalculate qty dispatched when qty input has changed.
  90. * If qty is more then qty ordered reset input qty to max qty to dispatch.
  91. *
  92. * element requires arbitrary data qty (value before change), type (type of dispatch) and index (index of product line)
  93. */
  94. function onChangeDispatchLineQty() {
  95. var index = $(this).data('index'),
  96. type = $(this).data('type'),
  97. qty = parseFloat($(this).data('qty')),
  98. changedQty, nbrTrs, dispatchingQty, qtyOrdered, qtyDispatched;
  99. if (index >= 0 && type && qty >= 0) {
  100. nbrTrs = $("tr[name^='"+type+"_'][name$='_"+index+"']").length;
  101. qtyChanged = parseFloat($(this).val()) - qty; // qty changed
  102. qtyDispatching = parseFloat($("#qty_"+(nbrTrs-1)+"_"+index).val()); // qty currently being dispatched
  103. qtyOrdered = parseFloat($("#qty_ordered_0_"+index).val()); // qty ordered
  104. qtyDispatched = parseFloat($("#qty_dispatched_0_"+index).val()); // qty already dispatched
  105. console.log("onChangeDispatchLineQty qtyChanged: " + qtyChanged + " qtyDispatching: " + qtyDispatching + " qtyOrdered: " + qtyOrdered + " qtyDispatched: "+ qtyDispatched);
  106. if ((qtyChanged) <= (qtyOrdered - (qtyDispatched + qtyDispatching))) {
  107. $("#qty_dispatched_0_"+index).val(qtyDispatched + qtyChanged);
  108. } else {
  109. $(this).val($(this).data('qty'));
  110. }
  111. $(this).data('qty', $(this).val());
  112. }
  113. }