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  var $row0 = $("tr[name='"+type+'_0_'+index+"']");
74  var $dpopt = $row0.find('.hasDatepicker').first().datepicker('option', 'all'); // get current datepicker options to apply the same to the cloned datepickers
75  var $row = $row0.clone(true); // clone first batch line to jQuery object
76  var nbrTrs = $("tr[name^='"+type+"_'][name$='_"+index+"']").length; // position of line for batch
77  var qtyOrdered = parseFloat($("#qty_ordered_0_"+index).val()); // Qty ordered is same for all rows
78  var qty = parseFloat($("#qty_"+(nbrTrs - 1)+"_"+index).val());
79  var qtyDispatched;
80 
81  if (mode === 'lessone')
82  {
83  qtyDispatched = parseFloat($("#qty_dispatched_0_"+index).val()) + 1;
84  }
85  else
86  {
87  qtyDispatched = parseFloat($("#qty_dispatched_0_"+index).val()) + qty;
88  // 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
89  if (qtyDispatched == qtyOrdered && qtyDispatched > 1) {
90  qtyDispatched = parseFloat($("#qty_dispatched_0_"+index).val()) + 1;
91  mode = 'lessone';
92  }
93  }
94  console.log("qtyDispatched="+qtyDispatched+" qtyOrdered="+qtyOrdered);
95 
96  if (qtyDispatched >= qtyOrdered || qtyOrdered <= 1) {
97  window.alert("Remain quantity to dispatch is too low to be split");
98  } else if (qtyDispatched < qtyOrdered) {
99  //replace tr suffix nbr
100  $row.html($row.html().replace(/_0_/g,"_"+nbrTrs+"_"));
101 
102  // jquery's deep clone is incompatible with date pickers (the clone shares data with the original)
103  // so we destroy and rebuild the new date pickers
104  setTimeout(() => {
105  $row.find('.hasDatepicker').each((i, dp) => {
106  $(dp).removeData()
107  .removeClass('hasDatepicker');
108  $(dp).next('img.ui-datepicker-trigger').remove();
109  $(dp).datepicker($dpopt);
110  });
111  }, 0);
112 
113  //create new select2 to avoid duplicate id of cloned one
114  $row.find("select[name='"+'entrepot_'+nbrTrs+'_'+index+"']").select2();
115  // TODO find solution to copy selected option to new select
116  // TODO find solution to keep new tr's after page refresh
117  //clear value
118  $row.find("input[name^='qty']").val('');
119  //change name of new row
120  $row.attr('name',type+'_'+nbrTrs+'_'+index);
121  //insert new row before last row
122  $("tr[name^='"+type+"_'][name$='_"+index+"']:last").after($row);
123 
124  //remove cloned select2 with duplicate id.
125  $("#s2id_entrepot_"+nbrTrs+'_'+index).detach(); // old way to find duplicated select2 component
126  $(".csswarehouse_"+nbrTrs+"_"+index+":first-child").parent("span.selection").parent(".select2").detach();
127 
128  /* Suffix of lines are: _ trs.length _ index */
129  $("#qty_"+nbrTrs+"_"+index).focus();
130  $("#qty_dispatched_0_"+index).val(qtyDispatched);
131 
132  //hide all buttons then show only the last one
133  $("tr[name^='"+type+"_'][name$='_"+index+"'] .splitbutton").hide();
134  $("tr[name^='"+type+"_'][name$='_"+index+"']:last .splitbutton").show();
135 
136  if (mode === 'lessone')
137  {
138  qty = 1; // keep 1 in old line
139  $("#qty_"+(nbrTrs-1)+"_"+index).val(qty);
140  }
141  $("#qty_"+nbrTrs+"_"+index).val(qtyOrdered - qtyDispatched);
142  // Store arbitrary data for dispatch qty input field change event
143  $("#qty_"+(nbrTrs-1)+"_"+index).data('qty', qty);
144  $("#qty_"+(nbrTrs-1)+"_"+index).data('type', type);
145  $("#qty_"+(nbrTrs-1)+"_"+index).data('index', index);
146  // Update dispatched qty when value dispatch qty input field changed
147  $("#qty_"+(nbrTrs-1)+"_"+index).change(this.onChangeDispatchLineQty);
148  //set focus on lot of new line (if it exists)
149  $("#lot_number_"+(nbrTrs)+"_"+index).focus();
150  }
151 }
152 
163 function onChangeDispatchLineQty() {
164  var index = $(this).data('index'),
165  type = $(this).data('type'),
166  qty = parseFloat($(this).data('qty')),
167  changedQty, nbrTrs, dispatchingQty, qtyOrdered, qtyDispatched;
168 
169  if (index >= 0 && type && qty >= 0) {
170  nbrTrs = $("tr[name^='"+type+"_'][name$='_"+index+"']").length;
171  qtyChanged = parseFloat($(this).val()) - qty; // qty changed
172  qtyDispatching = parseFloat($("#qty_"+(nbrTrs-1)+"_"+index).val()); // qty currently being dispatched
173  qtyOrdered = parseFloat($("#qty_ordered_0_"+index).val()); // qty ordered
174  qtyDispatched = parseFloat($("#qty_dispatched_0_"+index).val()); // qty already dispatched
175 
176  console.log("onChangeDispatchLineQty qtyChanged: " + qtyChanged + " qtyDispatching: " + qtyDispatching + " qtyOrdered: " + qtyOrdered + " qtyDispatched: "+ qtyDispatched);
177 
178  if ((qtyChanged) <= (qtyOrdered - (qtyDispatched + qtyDispatching))) {
179  $("#qty_dispatched_0_"+index).val(qtyDispatched + qtyChanged);
180  } else {
181  $(this).val($(this).data('qty'));
182  }
183  $(this).data('qty', $(this).val());
184  }
185 }
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