lib_dispatch.js.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. // Load translation files required by the page
  46. $langs->loadLangs(array("mrp"));
  47. // Define javascript type
  48. top_httphead('text/javascript; charset=UTF-8');
  49. // Important: Following code is to avoid page request by browser and PHP CPU at each Dolibarr page access.
  50. if (empty($dolibarr_nocache)) {
  51. header('Cache-Control: max-age=10800, public, must-revalidate');
  52. } else {
  53. header('Cache-Control: no-cache');
  54. }
  55. ?>
  56. /**
  57. * addDispatchLine
  58. * Adds new table row for dispatching to multiple stock locations or multiple lot/serial
  59. *
  60. * @param index int index of product line. 0 = first product line
  61. * @param type string type of dispatch (batch = batch dispatch, dispatch = non batch dispatch)
  62. * @param mode string 'qtymissing' will create new line with qty missing, 'lessone' will keep 1 in old line and the rest in new one
  63. */
  64. function addDispatchLine(index, type, mode)
  65. {
  66. mode = mode || 'qtymissing'
  67. console.log("mrp/js/lib_dispatch.js.php Split line type="+type+" index="+index+" mode="+mode);
  68. if(mode == 'qtymissingconsume' || mode == 'allmissingconsume') {
  69. var inputId = 'qtytoconsume';
  70. var warehouseId = 'idwarehouse';
  71. }
  72. else {
  73. var inputId = 'qtytoproduce';
  74. var warehouseId = 'idwarehousetoproduce';
  75. }
  76. var nbrTrs = $("tr[name^='"+type+"_"+index+"']").length; // position of line for batch
  77. var $row = $("tr[name='"+type+'_'+index+"_1']").clone(true); // clone last batch line to jQuery object
  78. var qtyOrdered = parseFloat($("#qty_ordered_"+index).val()); // Qty ordered is same for all rows
  79. var qty = parseFloat($("#"+inputId+"-"+index+"-"+nbrTrs).val());
  80. var qtyDispatched;
  81. if (mode === 'lessone')
  82. {
  83. qtyDispatched = parseFloat($("#qty_dispatched_"+index).val()) + 1;
  84. }
  85. else
  86. {
  87. qtyDispatched = parseFloat($("#qty_dispatched_"+index).val()) + qty;
  88. console.log($("#qty_dispatched_"+index).val());
  89. // 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
  90. if (qtyDispatched == qtyOrdered && qtyDispatched > 1) {
  91. qtyDispatched = parseFloat($("#qty_dispatched_" + index).val()) + 1;
  92. }
  93. if(mode == 'allmissingconsume' || mode == 'alltoproduce') {
  94. var qtymax = parseFloat($($row).data('max-qty'));
  95. if(qtymax === 'undefined') qtymax = 1;
  96. }
  97. }
  98. if(mode == 'allmissingconsume' || mode == 'alltoproduce') {
  99. var count = 0;
  100. var qtyalreadyused = 0;
  101. var error = 0;
  102. while (qtyalreadyused < qty) {
  103. //If remaining qty needed is inferior to qty asked, qtymax = qty asked - qty already used
  104. if ((qtyalreadyused + qtymax) > qty) qtymax = qty - qtyalreadyused;
  105. //If first line, we replace value, not add line
  106. if(count === 0){
  107. $("#"+inputId+"-"+index+"-"+nbrTrs).val(qtymax);
  108. } else {
  109. var res = addDispatchTR(qtyOrdered, qtyDispatched, index, nbrTrs, warehouseId, inputId, type, qtymax, mode, $row);
  110. if(res === -1){
  111. error = 1;
  112. break;
  113. }
  114. nbrTrs++;
  115. }
  116. qtyalreadyused = qtyalreadyused + qtymax;
  117. count++;
  118. $row = $("tr[name='" + type + '_' + index + "_1']").clone(true);
  119. }
  120. if(error === 0) {
  121. addDispatchTR(qtyOrdered, qtyDispatched, index, nbrTrs, warehouseId, inputId, type, '', mode, $row);
  122. }
  123. }
  124. else addDispatchTR(qtyOrdered, qtyDispatched, index, nbrTrs, warehouseId, inputId, type, qty, mode, $row)
  125. }
  126. /**
  127. * addDispatchTR
  128. * Adds new table row for dispatching to multiple stock locations or multiple lot/serial
  129. *
  130. * @param qtyOrdered double
  131. * @param qtyDispatched double
  132. * @param index int
  133. * @param nbrTrs int
  134. * @param warehouseId int
  135. * @param inputId int
  136. * @param type string
  137. * @param qty double
  138. * @param mode string
  139. * @param $row object
  140. */
  141. function addDispatchTR(qtyOrdered, qtyDispatched, index, nbrTrs, warehouseId, inputId, type, qty, mode, $row) {
  142. if (qtyOrdered <= 1) {
  143. let errormsg = '<?php echo dol_escape_js($langs->trans('QtyCantBeSplit')); ?>';
  144. $.jnotify(errormsg, 'error', true);
  145. return -1;
  146. } else if (qtyDispatched >= qtyOrdered) {
  147. let errormsg = '<?php echo dol_escape_js($langs->trans('NoRemainQtyToDispatch')); ?>;
  148. $.jnotify(errormsg, 'error', true);
  149. return -1;
  150. } else if (qtyDispatched < qtyOrdered) {
  151. //replace tr suffix nbr
  152. var re1 = new RegExp('_'+index+'_1', 'g');
  153. var re2 = new RegExp('-'+index+'-1', 'g');
  154. $row.html($row.html().replace(re1, '_'+index+'_'+(nbrTrs+1)));
  155. $row.html($row.html().replace(re2, '-'+index+'-'+(nbrTrs+1)));
  156. //create new select2 to avoid duplicate id of cloned one
  157. $row.find("select[name='"+warehouseId+'-'+index+'-'+(nbrTrs+1)+"']").select2();
  158. // TODO find solution to copy selected option to new select
  159. // TODO find solution to keep new tr's after page refresh
  160. //clear value
  161. $row.find("input[id^='"+inputId+"']").val('');
  162. //change name of new row
  163. $row.attr('name',type+'_'+index+'_'+(nbrTrs+1));
  164. //insert new row before last row
  165. $("tr[name^='"+type+"_"+index+"_"+nbrTrs+"']:last").after($row);
  166. //remove cloned select2 with duplicate id.
  167. $("#s2id_entrepot_"+nbrTrs+'_'+index).detach(); // old way to find duplicated select2 component
  168. $(".csswarehouse_"+index+"_"+(nbrTrs+1)+":first-child").parent("span.selection").parent(".select2").detach();
  169. /* Suffix of lines are: index _ trs.length */
  170. $("#"+inputId+"-"+index+"-"+(nbrTrs+1)).focus();
  171. if ($("#"+inputId+"-"+index+"-"+(nbrTrs)).val() == 0) {
  172. if(mode == 'allmissingconsume' || mode == 'alltoproduce') $("#"+inputId+"-"+index+"-"+(nbrTrs)).val(qty);
  173. else $("#"+inputId+"-"+index+"-"+(nbrTrs)).val(1);
  174. }
  175. var totalonallines = 0;
  176. for (let i = 1; i <= nbrTrs; i++) {
  177. console.log(i+" = "+parseFloat($("#"+inputId+"-"+index+"-"+i).val()));
  178. totalonallines = totalonallines + parseFloat($("#"+inputId+"-"+index+"-"+i).val());
  179. }
  180. if(mode != 'allmissingconsume' && mode != 'alltoproduce') {
  181. if (totalonallines == qtyOrdered && qtyOrdered > 1) {
  182. var prevouslineqty = $("#" + inputId + "-" + index + "-" + nbrTrs).val();
  183. $("#" + inputId + "-" + index + "-" + (nbrTrs)).val(1);
  184. $("#" + inputId + "-" + index + "-" + (nbrTrs + 1)).val(prevouslineqty - 1);
  185. }
  186. }
  187. $("#qty_dispatched_"+index).val(qtyDispatched);
  188. //hide all buttons then show only the last one
  189. $("tr[name^='"+type+"_"+index+"_'] .splitbutton").hide();
  190. $("tr[name^='"+type+"_"+index+"_']:last .splitbutton").show();
  191. if (mode === 'lessone')
  192. {
  193. qty = 1; // keep 1 in old line
  194. $("#qty_"+(nbrTrs-1)+"_"+index).val(qty);
  195. }
  196. // Store arbitrary data for dispatch qty input field change event
  197. $("#"+inputId+"-"+index+(nbrTrs)).data('qty', qty);
  198. $("#"+inputId+"-"+index+(nbrTrs)).data('type', type);
  199. $("#"+inputId+"-"+index+(nbrTrs)).data('index', index);
  200. if(mode == 'allmissingconsume' || mode == 'alltoproduce') {
  201. let currentQtyDispatched = qtyDispatched+qty;
  202. $row.find("input[id^='"+inputId+"']").val(qty);
  203. }
  204. }
  205. }