lib_dispatch.js.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. // Copyright (C) 2014 Cedric GROSS <c.gross@kreiz-it.fr>
  3. // Copyright (C) 2017 Francis Appels <francis.appels@z-application.com>
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. // or see https://www.gnu.org/
  18. /**
  19. * \file htdocs/expedition/js/lib_dispatch.js.php
  20. * \brief File that include javascript functions used for dispatching qty/stock/lot
  21. */
  22. if (!defined('NOREQUIRESOC')) {
  23. define('NOREQUIRESOC', '1');
  24. }
  25. if (!defined('NOCSRFCHECK')) {
  26. define('NOCSRFCHECK', 1);
  27. }
  28. if (!defined('NOTOKENRENEWAL')) {
  29. define('NOTOKENRENEWAL', 1);
  30. }
  31. if (!defined('NOLOGIN')) {
  32. define('NOLOGIN', 1);
  33. }
  34. if (!defined('NOREQUIREMENU')) {
  35. define('NOREQUIREMENU', 1);
  36. }
  37. if (!defined('NOREQUIREHTML')) {
  38. define('NOREQUIREHTML', 1);
  39. }
  40. if (!defined('NOREQUIREAJAX')) {
  41. define('NOREQUIREAJAX', '1');
  42. }
  43. session_cache_limiter('public');
  44. require_once '../../main.inc.php';
  45. // Define javascript type
  46. top_httphead('text/javascript; charset=UTF-8');
  47. // Important: Following code is to avoid page request by browser and PHP CPU at each Dolibarr page access.
  48. if (empty($dolibarr_nocache)) {
  49. header('Cache-Control: max-age=10800, public, must-revalidate');
  50. } else {
  51. header('Cache-Control: no-cache');
  52. }
  53. ?>
  54. /**
  55. * addDispatchLine
  56. * Adds new table row for dispatching to multiple stock locations or multiple lot/serial
  57. *
  58. * @param index int index of product line. 0 = first product line
  59. * @param type string type of dispatch ('batch' = batch dispatch, 'dispatch' = non batch dispatch)
  60. * @param mode string 'qtymissing' will create new line with qty missing, 'lessone' will keep 1 in old line and the rest in new one
  61. */
  62. function addDispatchLine(index, type, mode) {
  63. mode = mode || 'qtymissing'
  64. console.log("expedition/js/lib_dispatch.js.php addDispatchLine Split line type="+type+" index="+index+" mode="+mode);
  65. var $row0 = $("tr[name='"+type+'_0_'+index+"']");
  66. var $dpopt = $row0.find('.hasDatepicker').first().datepicker('option', 'all'); // get current datepicker options to apply the same to the cloned datepickers
  67. var $row = $row0.clone(true); // clone first batch line to jQuery object
  68. var nbrTrs = $("tr[name^='"+type+"_'][name$='_"+index+"']").length; // count nb of tr line with attribute name that starts with 'batch_' or 'dispatch_', and end with _index
  69. var qtyOrdered = parseFloat($("#qty_ordered_0_"+index).val()); // Qty ordered is same for all rows
  70. var qty = parseFloat($("#qty_"+(nbrTrs - 1)+"_"+index).val());
  71. if (isNaN(qty)) {
  72. qty = '';
  73. }
  74. console.log("expedition/js/lib_dispatch.js.php addDispatchLine Split line nbrTrs="+nbrTrs+" qtyOrdered="+qtyOrdered+" qty="+qty);
  75. var qtyDispatched;
  76. if (mode === 'lessone') {
  77. qtyDispatched = parseFloat($("#qty_dispatched_0_" + index).val()) + 1;
  78. }
  79. else {
  80. qtyDispatched = parseFloat($("#qty_dispatched_0_" + index).val()) + qty;
  81. // If user did not reduced the qty to dispatch on old line, we keep only 1 on old line and the rest on new line
  82. if (qtyDispatched == qtyOrdered && qtyDispatched > 1) {
  83. qtyDispatched = parseFloat($("#qty_dispatched_0_" + index).val()) + 1;
  84. mode = 'lessone';
  85. }
  86. }
  87. console.log("expedition/js/lib_dispatch.js.php qtyDispatched=" + qtyDispatched + " qtyOrdered=" + qtyOrdered+ " qty=" + qty);
  88. if (qty <= 1) {
  89. window.alert("Remain quantity to dispatch is too low to be split");
  90. } else {
  91. var oldlineqty = qtyDispatched;
  92. var newlineqty = qtyOrdered - qtyDispatched;
  93. if (newlineqty <= 0) {
  94. newlineqty = qty - 1;
  95. oldlineqty = 1;
  96. $("#qty_"+(nbrTrs - 1)+"_"+index).val(oldlineqty);
  97. }
  98. //replace tr suffix nbr
  99. $row.html($row.html().replace(/_0_/g, "_" + nbrTrs + "_"));
  100. // jquery's deep clone is incompatible with date pickers (the clone shares data with the original)
  101. // so we destroy and rebuild the new date pickers
  102. setTimeout(() => {
  103. $row.find('.hasDatepicker').each((i, dp) => {
  104. $(dp).removeData()
  105. .removeClass('hasDatepicker');
  106. $(dp).next('img.ui-datepicker-trigger').remove();
  107. $(dp).datepicker($dpopt);
  108. });
  109. }, 0);
  110. //create new select2 to avoid duplicate id of cloned one
  111. $row.find("select[name='" + 'entrepot_' + nbrTrs + '_' + index + "']").select2();
  112. // TODO find solution to copy selected option to new select
  113. // TODO find solution to keep new tr's after page refresh
  114. //clear value
  115. $row.find("input[name^='qty']").val('');
  116. //change name of new row
  117. $row.attr('name', type + '_' + nbrTrs + '_' + index);
  118. //insert new row before last row
  119. $("tr[name^='" + type + "_'][name$='_" + index + "']:last").after($row);
  120. //remove cloned select2 with duplicate id.
  121. $("#s2id_entrepot_" + nbrTrs + '_' + index).detach(); // old way to find duplicated select2 component
  122. $(".csswarehouse_" + nbrTrs + "_" + index + ":first-child").parent("span.selection").parent(".select2").detach();
  123. /* Suffix of lines are: _ trs.length _ index */
  124. $("#qty_"+nbrTrs+"_"+index).focus();
  125. $("#qty_dispatched_0_"+index).val(oldlineqty);
  126. //hide all buttons then show only the last one
  127. $("tr[name^='" + type + "_'][name$='_" + index + "'] .splitbutton").hide();
  128. $("tr[name^='" + type + "_'][name$='_" + index + "']:last .splitbutton").show();
  129. $("#reset_" + (nbrTrs) + "_" + index).click(function (event) {
  130. event.preventDefault();
  131. id = $(this).attr("id");
  132. id = id.split("reset_");
  133. idrow = id[1];
  134. idlast = $("tr[name^='" + type + "_'][name$='_" + index + "']:last .qtydispatchinput").attr("id");
  135. if (idlast == $("#qty_" + idrow).attr("id")) {
  136. console.log("expedition/js/lib_dispatch.js.php Remove trigger for tr name = " + type + "_" + idrow);
  137. $('tr[name="' + type + '_' + idrow + '"').remove();
  138. $("tr[name^='" + type + "_'][name$='_" + index + "']:last .splitbutton").show();
  139. } else {
  140. console.log("expedition/js/lib_dispatch.js.php Reset trigger for id = qty_" + idrow);
  141. $("#qty_" + idrow).val("");
  142. }
  143. });
  144. if (mode === 'lessone')
  145. {
  146. qty = 1; // keep 1 in old line
  147. $("#qty_"+(nbrTrs-1)+"_"+index).val(qty);
  148. }
  149. $("#qty_"+nbrTrs+"_"+index).val(newlineqty);
  150. // Store arbitrary data for dispatch qty input field change event
  151. $("#qty_" + (nbrTrs - 1) + "_" + index).data('qty', qty);
  152. $("#qty_" + (nbrTrs - 1) + "_" + index).data('type', type);
  153. $("#qty_" + (nbrTrs - 1) + "_" + index).data('index', index);
  154. // Update dispatched qty when value dispatch qty input field changed
  155. //$("#qty_" + (nbrTrs - 1) + "_" + index).change(this.onChangeDispatchLineQty);
  156. //set focus on lot of new line (if it exists)
  157. $("#lot_number_" + (nbrTrs) + "_" + index).focus();
  158. //Clean bad values
  159. $("tr[name^='" + type + "_'][name$='_" + index + "']:last").data("remove", "remove");
  160. $("#lot_number_" + (nbrTrs) + "_" + index).val("")
  161. $("#idline_" + (nbrTrs) + "_" + index).val("-1")
  162. $("#qty_" + (nbrTrs) + "_" + index).data('expected', "0");
  163. //$("input[type='hidden']#lot_number_" + (nbrTrs) + "_" + index).remove();
  164. $("#lot_number_" + (nbrTrs) + "_" + index).removeAttr("disabled");
  165. }
  166. }
  167. /**
  168. * onChangeDispatchLineQty
  169. *
  170. * Change event handler for dispatch qty input field,
  171. * recalculate qty dispatched when qty input has changed.
  172. * If qty is more than qty ordered reset input qty to max qty to dispatch.
  173. *
  174. * element requires arbitrary data qty (value before change), type (type of dispatch) and index (index of product line)
  175. */
  176. function onChangeDispatchLineQty(element) {
  177. var type = $(element).data('type'),
  178. qty = parseFloat($(element).data('expected')),
  179. changedQty, nbrTrs, dispatchingQty, qtyOrdered, qtyDispatched;
  180. id = $(element).attr("id");
  181. id = id.split("_");
  182. index = id[2];
  183. if (index >= 0 && type && qty >= 0) {
  184. nbrTrs = $("tr[name^='" + type + "_'][name$='_" + index + "']").length;
  185. qtyChanged = parseFloat($(element).val()) - qty; // qty changed
  186. qtyDispatching = parseFloat($(element).val()); // qty currently being dispatched
  187. qtyOrdered = parseFloat($("#qty_ordered_0_" + index).val()); // qty ordered
  188. qtyDispatched = parseFloat($("#qty_dispatched_0_" + index).val()); // qty already dispatched
  189. console.log("onChangeDispatchLineQty qtyChanged: " + qtyChanged + " qtyDispatching: " + qtyDispatching + " qtyOrdered: " + qtyOrdered + " qtyDispatched: " + qtyDispatched);
  190. if ((qtyChanged) <= (qtyOrdered - (qtyDispatched + qtyDispatching))) {
  191. $("#qty_dispatched_0_" + index).val(qtyDispatched + qtyChanged);
  192. } else {
  193. /*console.log("eee");
  194. $(element).val($(element).data('expected'));*/
  195. }
  196. $(element).data('expected', $(element).val());
  197. }
  198. }