dolibarr  17.0.4
lib_dispatch.js.php
Go to the documentation of this file.
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 
24 if (!defined('NOREQUIRESOC')) {
25  define('NOREQUIRESOC', '1');
26 }
27 if (!defined('NOCSRFCHECK')) {
28  define('NOCSRFCHECK', 1);
29 }
30 if (!defined('NOTOKENRENEWAL')) {
31  define('NOTOKENRENEWAL', 1);
32 }
33 if (!defined('NOLOGIN')) {
34  define('NOLOGIN', 1);
35 }
36 if (!defined('NOREQUIREMENU')) {
37  define('NOREQUIREMENU', 1);
38 }
39 if (!defined('NOREQUIREHTML')) {
40  define('NOREQUIREHTML', 1);
41 }
42 if (!defined('NOREQUIREAJAX')) {
43  define('NOREQUIREAJAX', '1');
44 }
45 
46 session_cache_limiter('public');
47 
48 require_once '../../main.inc.php';
49 
50 // Define javascript type
51 top_httphead('text/javascript; charset=UTF-8');
52 // Important: Following code is to avoid page request by browser and PHP CPU at each Dolibarr page access.
53 if (empty($dolibarr_nocache)) {
54  header('Cache-Control: max-age=10800, public, must-revalidate');
55 } else {
56  header('Cache-Control: no-cache');
57 }
58 
59 ?>
68 function addDispatchLine(index, type, mode)
69 {
70  mode = mode || 'qtymissing'
71 
72  console.log("fourn/js/lib_dispatch.js.php Split line type="+type+" index="+index+" mode="+mode);
73  if(mode == 'qtymissingconsume' || mode == 'allmissingconsume') {
74  var inputId = 'qtytoconsume';
75  var warehouseId = 'idwarehouse';
76  }
77  else {
78  var inputId = 'qtytoproduce';
79  var warehouseId = 'idwarehousetoproduce';
80  }
81  var nbrTrs = $("tr[name^='"+type+"_"+index+"']").length; // position of line for batch
82  var $row = $("tr[name='"+type+'_'+index+"_1']").clone(true); // clone last batch line to jQuery object
83  var qtyOrdered = parseFloat($("#qty_ordered_"+index).val()); // Qty ordered is same for all rows
84  var qty = parseFloat($("#"+inputId+"-"+index+"-"+nbrTrs).val());
85  var qtyDispatched;
86 
87  if (mode === 'lessone')
88  {
89  qtyDispatched = parseFloat($("#qty_dispatched_"+index).val()) + 1;
90  }
91  else
92  {
93  qtyDispatched = parseFloat($("#qty_dispatched_"+index).val()) + qty;
94  console.log($("#qty_dispatched_"+index).val());
95  // 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
96  if (qtyDispatched == qtyOrdered && qtyDispatched > 1) {
97  qtyDispatched = parseFloat($("#qty_dispatched_" + index).val()) + 1;
98 
99  }
100  if(mode == 'allmissingconsume' || mode == 'alltoproduce') {
101  var qtymax = parseFloat($($row).data('max-qty'));
102  if(qtymax === 'undefined') qtymax = 1;
103  }
104  }
105 
106 
107  if(mode == 'allmissingconsume' || mode == 'alltoproduce') {
108  var count = 0;
109  var qtyalreadyused = 0;
110  var error = 0;
111 
112  while (qtyalreadyused < qty) {
113  //If remaining qty needed is inferior to qty asked, qtymax = qty asked - qty already used
114  if ((qtyalreadyused + qtymax) > qty) qtymax = qty - qtyalreadyused;
115  //If first line, we replace value, not add line
116  if(count === 0){
117  $("#"+inputId+"-"+index+"-"+nbrTrs).val(qtymax);
118  } else {
119  var res = addDispatchTR(qtyOrdered, qtyDispatched, index, nbrTrs, warehouseId, inputId, type, qtymax, mode, $row);
120  if(res === -1){
121  error = 1;
122  break;
123  }
124  nbrTrs++;
125  }
126  qtyalreadyused = qtyalreadyused + qtymax;
127  count++;
128  $row = $("tr[name='" + type + '_' + index + "_1']").clone(true);
129  }
130 
131  if(error === 0) {
132  addDispatchTR(qtyOrdered, qtyDispatched, index, nbrTrs, warehouseId, inputId, type, '', mode, $row);
133  }
134  }
135  else addDispatchTR(qtyOrdered, qtyDispatched, index, nbrTrs, warehouseId, inputId, type, qty, mode, $row)
136 
137 }
138 
154 function addDispatchTR(qtyOrdered, qtyDispatched, index, nbrTrs, warehouseId, inputId, type, qty, mode, $row) {
155  if (qtyOrdered <= 1) {
156  window.alert("Quantity can't be split");
157  return -1;
158  } else if (qtyDispatched >= qtyOrdered) {
159  window.alert("No remain qty to dispatch");
160  return -1;
161  } else if (qtyDispatched < qtyOrdered) {
162  //replace tr suffix nbr
163  var re1 = new RegExp('_'+index+'_1', 'g');
164  var re2 = new RegExp('-'+index+'-1', 'g');
165  $row.html($row.html().replace(re1, '_'+index+'_'+(nbrTrs+1)));
166  $row.html($row.html().replace(re2, '-'+index+'-'+(nbrTrs+1)));
167  //create new select2 to avoid duplicate id of cloned one
168  $row.find("select[name='"+warehouseId+'-'+index+'-'+(nbrTrs+1)+"']").select2();
169  // TODO find solution to copy selected option to new select
170  // TODO find solution to keep new tr's after page refresh
171  //clear value
172  $row.find("input[id^='"+inputId+"']").val('');
173  //change name of new row
174  $row.attr('name',type+'_'+index+'_'+(nbrTrs+1));
175  //insert new row before last row
176  $("tr[name^='"+type+"_"+index+"_"+nbrTrs+"']:last").after($row);
177 
178  //remove cloned select2 with duplicate id.
179  $("#s2id_entrepot_"+nbrTrs+'_'+index).detach(); // old way to find duplicated select2 component
180  $(".csswarehouse_"+index+"_"+(nbrTrs+1)+":first-child").parent("span.selection").parent(".select2").detach();
181 
182  /* Suffix of lines are: index _ trs.length */
183  $("#"+inputId+"-"+index+"-"+(nbrTrs+1)).focus();
184  if ($("#"+inputId+"-"+index+"-"+(nbrTrs)).val() == 0) {
185  if(mode == 'allmissingconsume' || mode == 'alltoproduce') $("#"+inputId+"-"+index+"-"+(nbrTrs)).val(qty);
186  else $("#"+inputId+"-"+index+"-"+(nbrTrs)).val(1);
187  }
188  var totalonallines = 0;
189  for (let i = 1; i <= nbrTrs; i++) {
190  console.log(i+" = "+parseFloat($("#"+inputId+"-"+index+"-"+i).val()));
191  totalonallines = totalonallines + parseFloat($("#"+inputId+"-"+index+"-"+i).val());
192  }
193 
194  if(mode != 'allmissingconsume' && mode != 'alltoproduce') {
195  if (totalonallines == qtyOrdered && qtyOrdered > 1) {
196  var prevouslineqty = $("#" + inputId + "-" + index + "-" + nbrTrs).val();
197  $("#" + inputId + "-" + index + "-" + (nbrTrs)).val(1);
198  $("#" + inputId + "-" + index + "-" + (nbrTrs + 1)).val(prevouslineqty - 1);
199  }
200  }
201  $("#qty_dispatched_"+index).val(qtyDispatched);
202 
203  //hide all buttons then show only the last one
204  $("tr[name^='"+type+"_"+index+"_'] .splitbutton").hide();
205  $("tr[name^='"+type+"_"+index+"_']:last .splitbutton").show();
206 
207  if (mode === 'lessone')
208  {
209  qty = 1; // keep 1 in old line
210  $("#qty_"+(nbrTrs-1)+"_"+index).val(qty);
211  }
212  // Store arbitrary data for dispatch qty input field change event
213  $("#"+inputId+"-"+index+(nbrTrs)).data('qty', qty);
214  $("#"+inputId+"-"+index+(nbrTrs)).data('type', type);
215  $("#"+inputId+"-"+index+(nbrTrs)).data('index', index);
216  if(mode == 'allmissingconsume' || mode == 'alltoproduce') {
217  let currentQtyDispatched = qtyDispatched+qty;
218  $row.find("input[id^='"+inputId+"']").val(qty);
219  }
220  }
221 }
222 
if(!defined('NOREQUIREMENU')) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
Definition: main.inc.php:1440
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:119