lib_dispatch.js.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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/mrp/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. {
  64. mode = mode || 'qtymissing'
  65. console.log("fourn/js/lib_dispatch.js.php Split line type="+type+" index="+index+" mode="+mode);
  66. if(mode == 'qtymissingconsume' || mode == 'allmissingconsume') {
  67. var inputId = 'qtytoconsume';
  68. var warehouseId = 'idwarehouse';
  69. }
  70. else {
  71. var inputId = 'qtytoproduce';
  72. var warehouseId = 'idwarehousetoproduce';
  73. }
  74. var nbrTrs = $("tr[name^='"+type+"_"+index+"']").length; // position of line for batch
  75. var $row = $("tr[name='"+type+'_'+index+"_1']").clone(true); // clone last batch line to jQuery object
  76. var qtyOrdered = parseFloat($("#qty_ordered_"+index).val()); // Qty ordered is same for all rows
  77. var qty = parseFloat($("#"+inputId+"-"+index+"-"+nbrTrs).val());
  78. var qtyDispatched;
  79. if (mode === 'lessone')
  80. {
  81. qtyDispatched = parseFloat($("#qty_dispatched_"+index).val()) + 1;
  82. }
  83. else
  84. {
  85. qtyDispatched = parseFloat($("#qty_dispatched_"+index).val()) + qty;
  86. console.log($("#qty_dispatched_"+index).val());
  87. // 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
  88. if (qtyDispatched == qtyOrdered && qtyDispatched > 1) {
  89. qtyDispatched = parseFloat($("#qty_dispatched_" + index).val()) + 1;
  90. }
  91. if(mode == 'allmissingconsume' || mode == 'alltoproduce') {
  92. var qtymax = parseFloat($($row).data('max-qty'));
  93. if(qtymax === 'undefined') qtymax = 1;
  94. }
  95. }
  96. if(mode == 'allmissingconsume' || mode == 'alltoproduce') {
  97. var count = 0;
  98. var qtyalreadyused = 0;
  99. var error = 0;
  100. while (qtyalreadyused < qty) {
  101. //If remaining qty needed is inferior to qty asked, qtymax = qty asked - qty already used
  102. if ((qtyalreadyused + qtymax) > qty) qtymax = qty - qtyalreadyused;
  103. //If first line, we replace value, not add line
  104. if(count === 0){
  105. $("#"+inputId+"-"+index+"-"+nbrTrs).val(qtymax);
  106. } else {
  107. var res = addDispatchTR(qtyOrdered, qtyDispatched, index, nbrTrs, warehouseId, inputId, type, qtymax, mode, $row);
  108. if(res === -1){
  109. error = 1;
  110. break;
  111. }
  112. nbrTrs++;
  113. }
  114. qtyalreadyused = qtyalreadyused + qtymax;
  115. count++;
  116. $row = $("tr[name='" + type + '_' + index + "_1']").clone(true);
  117. }
  118. if(error === 0) {
  119. addDispatchTR(qtyOrdered, qtyDispatched, index, nbrTrs, warehouseId, inputId, type, '', mode, $row);
  120. }
  121. }
  122. else addDispatchTR(qtyOrdered, qtyDispatched, index, nbrTrs, warehouseId, inputId, type, qty, mode, $row)
  123. }
  124. /**
  125. * addDispatchTR
  126. * Adds new table row for dispatching to multiple stock locations or multiple lot/serial
  127. *
  128. * @param qtyOrdered double
  129. * @param qtyDispatched double
  130. * @param index int
  131. * @param nbrTrs int
  132. * @param warehouseId int
  133. * @param inputId int
  134. * @param type string
  135. * @param qty double
  136. * @param mode string
  137. * @param $row object
  138. */
  139. function addDispatchTR(qtyOrdered, qtyDispatched, index, nbrTrs, warehouseId, inputId, type, qty, mode, $row) {
  140. if (qtyOrdered <= 1) {
  141. window.alert("Quantity can't be split");
  142. return -1;
  143. } else if (qtyDispatched >= qtyOrdered) {
  144. window.alert("No remain qty to dispatch");
  145. return -1;
  146. } else if (qtyDispatched < qtyOrdered) {
  147. //replace tr suffix nbr
  148. var re1 = new RegExp('_'+index+'_1', 'g');
  149. var re2 = new RegExp('-'+index+'-1', 'g');
  150. $row.html($row.html().replace(re1, '_'+index+'_'+(nbrTrs+1)));
  151. $row.html($row.html().replace(re2, '-'+index+'-'+(nbrTrs+1)));
  152. //create new select2 to avoid duplicate id of cloned one
  153. $row.find("select[name='"+warehouseId+'-'+index+'-'+(nbrTrs+1)+"']").select2();
  154. // TODO find solution to copy selected option to new select
  155. // TODO find solution to keep new tr's after page refresh
  156. //clear value
  157. $row.find("input[id^='"+inputId+"']").val('');
  158. //change name of new row
  159. $row.attr('name',type+'_'+index+'_'+(nbrTrs+1));
  160. //insert new row before last row
  161. $("tr[name^='"+type+"_"+index+"_"+nbrTrs+"']:last").after($row);
  162. //remove cloned select2 with duplicate id.
  163. $("#s2id_entrepot_"+nbrTrs+'_'+index).detach(); // old way to find duplicated select2 component
  164. $(".csswarehouse_"+index+"_"+(nbrTrs+1)+":first-child").parent("span.selection").parent(".select2").detach();
  165. /* Suffix of lines are: index _ trs.length */
  166. $("#"+inputId+"-"+index+"-"+(nbrTrs+1)).focus();
  167. if ($("#"+inputId+"-"+index+"-"+(nbrTrs)).val() == 0) {
  168. if(mode == 'allmissingconsume' || mode == 'alltoproduce') $("#"+inputId+"-"+index+"-"+(nbrTrs)).val(qty);
  169. else $("#"+inputId+"-"+index+"-"+(nbrTrs)).val(1);
  170. }
  171. var totalonallines = 0;
  172. for (let i = 1; i <= nbrTrs; i++) {
  173. console.log(i+" = "+parseFloat($("#"+inputId+"-"+index+"-"+i).val()));
  174. totalonallines = totalonallines + parseFloat($("#"+inputId+"-"+index+"-"+i).val());
  175. }
  176. if(mode != 'allmissingconsume' && mode != 'alltoproduce') {
  177. if (totalonallines == qtyOrdered && qtyOrdered > 1) {
  178. var prevouslineqty = $("#" + inputId + "-" + index + "-" + nbrTrs).val();
  179. $("#" + inputId + "-" + index + "-" + (nbrTrs)).val(1);
  180. $("#" + inputId + "-" + index + "-" + (nbrTrs + 1)).val(prevouslineqty - 1);
  181. }
  182. }
  183. $("#qty_dispatched_"+index).val(qtyDispatched);
  184. //hide all buttons then show only the last one
  185. $("tr[name^='"+type+"_"+index+"_'] .splitbutton").hide();
  186. $("tr[name^='"+type+"_"+index+"_']:last .splitbutton").show();
  187. if (mode === 'lessone')
  188. {
  189. qty = 1; // keep 1 in old line
  190. $("#qty_"+(nbrTrs-1)+"_"+index).val(qty);
  191. }
  192. // Store arbitrary data for dispatch qty input field change event
  193. $("#"+inputId+"-"+index+(nbrTrs)).data('qty', qty);
  194. $("#"+inputId+"-"+index+(nbrTrs)).data('type', type);
  195. $("#"+inputId+"-"+index+(nbrTrs)).data('index', index);
  196. if(mode == 'allmissingconsume' || mode == 'alltoproduce') {
  197. let currentQtyDispatched = qtyDispatched+qty;
  198. $row.find("input[id^='"+inputId+"']").val(qty);
  199. }
  200. }
  201. }