dolibarr  13.0.2
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')) define('NOREQUIRESOC', '1');
25 if (!defined('NOCSRFCHECK')) define('NOCSRFCHECK', 1);
26 if (!defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL', 1);
27 if (!defined('NOLOGIN')) define('NOLOGIN', 1);
28 if (!defined('NOREQUIREMENU')) define('NOREQUIREMENU', 1);
29 if (!defined('NOREQUIREHTML')) define('NOREQUIREHTML', 1);
30 if (!defined('NOREQUIREAJAX')) define('NOREQUIREAJAX', '1');
31 
32 session_cache_limiter('public');
33 
34 require_once '../../main.inc.php';
35 
36 // Define javascript type
37 top_httphead('text/javascript; charset=UTF-8');
38 // Important: Following code is to avoid page request by browser and PHP CPU at each Dolibarr page access.
39 if (empty($dolibarr_nocache)) header('Cache-Control: max-age=10800, public, must-revalidate');
40 else header('Cache-Control: no-cache');
41 
42 ?>
51 function addDispatchLine(index, type, mode)
52 {
53  mode = mode || 'qtymissing'
54 
55  console.log("fourn/js/lib_dispatch.js.php Split line type="+type+" index="+index+" mode="+mode);
56  var $row = $("tr[name='"+type+'_0_'+index+"']").clone(true); // clone first batch line to jQuery object
57  var nbrTrs = $("tr[name^='"+type+"_'][name$='_"+index+"']").length; // position of line for batch
58  var qtyOrdered = parseFloat($("#qty_ordered_0_"+index).val()); // Qty ordered is same for all rows
59  var qty = parseFloat($("#qty_"+(nbrTrs - 1)+"_"+index).val());
60  var qtyDispatched;
61 
62  if (mode === 'lessone')
63  {
64  qtyDispatched = parseFloat($("#qty_dispatched_0_"+index).val()) + 1;
65  }
66  else
67  {
68  qtyDispatched = parseFloat($("#qty_dispatched_0_"+index).val()) + qty;
69  // 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
70  if (qtyDispatched == qtyOrdered && qtyDispatched > 1) {
71  qtyDispatched = parseFloat($("#qty_dispatched_0_"+index).val()) + 1;
72  mode = 'lessone';
73  }
74  }
75  console.log("qtyDispatched="+qtyDispatched+" qtyOrdered="+qtyOrdered);
76 
77  if (qtyOrdered <= 1) {
78  window.alert("Quantity can't be split");
79  }
80  if (qtyDispatched < qtyOrdered)
81  {
82  //replace tr suffix nbr
83  $row.html($row.html().replace(/_0_/g,"_"+nbrTrs+"_"));
84  //create new select2 to avoid duplicate id of cloned one
85  $row.find("select[name='"+'entrepot_'+nbrTrs+'_'+index+"']").select2();
86  // TODO find solution to copy selected option to new select
87  // TODO find solution to keep new tr's after page refresh
88  //clear value
89  $row.find("input[name^='qty']").val('');
90  //change name of new row
91  $row.attr('name',type+'_'+nbrTrs+'_'+index);
92  //insert new row before last row
93  $("tr[name^='"+type+"_'][name$='_"+index+"']:last").after($row);
94 
95  //remove cloned select2 with duplicate id.
96  $("#s2id_entrepot_"+nbrTrs+'_'+index).detach(); // old way to find duplicated select2 component
97  $(".csswarehouse_"+nbrTrs+"_"+index+":first-child").parent("span.selection").parent(".select2").detach();
98 
99  /* Suffix of lines are: _ trs.length _ index */
100  $("#qty_"+nbrTrs+"_"+index).focus();
101  $("#qty_dispatched_0_"+index).val(qtyDispatched);
102 
103  //hide all buttons then show only the last one
104  $("tr[name^='"+type+"_'][name$='_"+index+"'] .splitbutton").hide();
105  $("tr[name^='"+type+"_'][name$='_"+index+"']:last .splitbutton").show();
106 
107  if (mode === 'lessone')
108  {
109  qty = 1; // keep 1 in old line
110  $("#qty_"+(nbrTrs-1)+"_"+index).val(qty);
111  }
112  $("#qty_"+nbrTrs+"_"+index).val(qtyOrdered - qtyDispatched);
113  // Store arbitrary data for dispatch qty input field change event
114  $("#qty_"+(nbrTrs-1)+"_"+index).data('qty', qty);
115  $("#qty_"+(nbrTrs-1)+"_"+index).data('type', type);
116  $("#qty_"+(nbrTrs-1)+"_"+index).data('index', index);
117  // Update dispatched qty when value dispatch qty input field changed
118  $("#qty_"+(nbrTrs-1)+"_"+index).change(this.onChangeDispatchLineQty);
119  //set focus on lot of new line (if it exists)
120  $("#lot_number_"+(nbrTrs)+"_"+index).focus();
121  }
122 }
123 
134 function onChangeDispatchLineQty() {
135  var index = $(this).data('index'),
136  type = $(this).data('type'),
137  qty = parseFloat($(this).data('qty')),
138  changedQty, nbrTrs, dispatchingQty, qtyOrdered, qtyDispatched;
139 
140  if (index >= 0 && type && qty >= 0) {
141  nbrTrs = $("tr[name^='"+type+"_'][name$='_"+index+"']").length;
142  qtyChanged = parseFloat($(this).val()) - qty; // qty changed
143  qtyDispatching = parseFloat($("#qty_"+(nbrTrs-1)+"_"+index).val()); // qty currently being dispatched
144  qtyOrdered = parseFloat($("#qty_ordered_0_"+index).val()); // qty ordered
145  qtyDispatched = parseFloat($("#qty_dispatched_0_"+index).val()); // qty already dispatched
146 
147  console.log("onChangeDispatchLineQty qtyChanged: " + qtyChanged + " qtyDispatching: " + qtyDispatching + " qtyOrdered: " + qtyOrdered + " qtyDispatched: "+ qtyDispatched);
148 
149  if ((qtyChanged) <= (qtyOrdered - (qtyDispatched + qtyDispatching))) {
150  $("#qty_dispatched_0_"+index).val(qtyDispatched + qtyChanged);
151  } else {
152  $(this).val($(this).data('qty'));
153  }
154  $(this).data('qty', $(this).val());
155  }
156 }
if(!defined('NOREQUIREMENU')) if(!function_exists("llxHeader")) top_httphead($contenttype= 'text/html', $forcenocache=0)
Show HTTP header.
Definition: main.inc.php:1214
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:105