dolibarr 23.0.3
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
24if (!defined('NOREQUIRESOC')) {
25 define('NOREQUIRESOC', '1');
26}
27if (!defined('NOCSRFCHECK')) {
28 define('NOCSRFCHECK', 1);
29}
30if (!defined('NOTOKENRENEWAL')) {
31 define('NOTOKENRENEWAL', 1);
32}
33if (!defined('NOLOGIN')) {
34 define('NOLOGIN', 1);
35}
36if (!defined('NOREQUIREMENU')) {
37 define('NOREQUIREMENU', 1);
38}
39if (!defined('NOREQUIREHTML')) {
40 define('NOREQUIREHTML', 1);
41}
42if (!defined('NOREQUIREAJAX')) {
43 define('NOREQUIREAJAX', '1');
44}
45
46session_cache_limiter('public');
47
48require_once '../../main.inc.php';
49
50// Define javascript type
51top_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.
53if (empty($dolibarr_nocache)) {
54 header('Cache-Control: max-age=10800, public, must-revalidate');
55} else {
56 header('Cache-Control: no-cache');
57}
58
59?>
68function addDispatchLine(index, type, mode) {
69 mode = mode || 'qtymissing'
70
71 console.log("expedition/js/lib_dispatch.js.php addDispatchLine Split line type="+type+" index="+index+" mode="+mode);
72
73 var lineId = '';
74 var typeArr = type.split('-');
75 if (typeArr.length > 0) {
76 lineId = typeArr[1];
77 }
78 var $row0 = $("tr[name='"+type+'_0_'+index+"']");
79 var $dpopt = $row0.find('.hasDatepicker').first().datepicker('option', 'all'); // get current datepicker options to apply the same to the cloned datepickers
80 var $row = $row0.clone(true); // clone first batch line to jQuery object
81 var nbrTrs = $("tr[name^='"+type+"_'][name$='_"+index+"']").length; // count nb of tr line with attribute name that starts with 'batch_' or 'dispatch_', and end with _index
82 var qtyOrdered = parseFloat($("#qty_ordered_0_"+index).val()); // Qty ordered is same for all rows
83
84 var qty = parseFloat($("#qty"+lineId+"_"+(nbrTrs - 1)+"_"+index).val());
85 if (isNaN(qty)) {
86 qty = '';
87 }
88
89 console.log("expedition/js/lib_dispatch.js.php addDispatchLine Split line="+lineId+" nbrTrs="+nbrTrs+" qtyOrdered="+qtyOrdered+" qty="+qty);
90
91 var qtyDispatched;
92
93 if (mode === 'lessone') {
94 qtyDispatched = parseFloat($("#qty_dispatched_0_" + index).val()) + 1;
95 }
96 else {
97 qtyDispatched = parseFloat($("#qty_dispatched_0_" + index).val()) + qty;
98 // 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
99 if (qtyDispatched == qtyOrdered && qtyDispatched > 1) {
100 qtyDispatched = parseFloat($("#qty_dispatched_0_" + index).val()) + 1;
101 mode = 'lessone';
102 }
103 }
104 console.log("expedition/js/lib_dispatch.js.php qtyDispatched=" + qtyDispatched + " qtyOrdered=" + qtyOrdered+ " qty=" + qty);
105
106 if (qty <= 1) {
107 window.alert("Remain quantity to dispatch is too low to be split");
108 } else {
109 var oldlineqty = qtyDispatched;
110 var newlineqty = qtyOrdered - qtyDispatched;
111 if (newlineqty <= 0) {
112 newlineqty = qty - 1;
113 oldlineqty = 1;
114 $("#qty"+lineId+"_"+(nbrTrs - 1)+"_"+index).val(oldlineqty);
115 }
116
117 //replace tr suffix nbr
118 $row.html($row.html().replace(/_0_/g, "_" + nbrTrs + "_"));
119
120 // jquery's deep clone is incompatible with date pickers (the clone shares data with the original)
121 // so we destroy and rebuild the new date pickers
122 setTimeout(() => {
123 $row.find('.hasDatepicker').each((i, dp) => {
124 $(dp).removeData()
125 .removeClass('hasDatepicker');
126 $(dp).next('img.ui-datepicker-trigger').remove();
127 $(dp).datepicker($dpopt);
128 });
129 }, 0);
130
131 // create new select2 to avoid duplicate id of cloned one for warehouse
132 $row.find("select[name='"+'entrepot'+lineId+'_'+nbrTrs+'_'+index+"']").select2();
133 // create new select2 to avoid duplicate id of cloned one for lot / serial number
134 $row.find("select[name='"+'lot_number'+lineId+'_'+nbrTrs+'_'+index+"']").select2();
135 // TODO find solution to copy selected option to new select
136 // TODO find solution to keep new tr's after page refresh
137 //clear value
138 $row.find("input[name^='qty']").val('');
139 //change name of new row
140 $row.attr('name', type+'_'+nbrTrs+'_'+index);
141 //insert new row before last row
142 $("tr[name^='"+type+"_'][name$='_"+index+"']:last").after($row);
143
144 // remove cloned select2 with duplicate id for warehouse
145 $("#s2id_entrepot"+lineId+"_"+nbrTrs+'_'+index).detach(); // old way to find duplicated select2 component
146 $(".csswarehouse"+lineId+"_"+nbrTrs+"_"+index + ":first-child").parent("span.selection").parent(".select2").detach();
147
148 // remove cloned select2 with duplicate id for lot / serial number
149 $("#s2id_lot_number"+lineId+"_"+nbrTrs+'_'+index).detach(); // old way to find duplicated select2 component
150 $(".csslotnumber"+lineId+"_"+nbrTrs+"_"+index + ":first-child").parent("span.selection").parent(".select2").detach();
151
152 /* Suffix of lines are: _ trs.length _ index */
153 $("#qty"+lineId+"_"+nbrTrs+"_"+index).focus();
154 $("#qty_dispatched_0_"+index).val(oldlineqty);
155
156 //hide all buttons then show only the last one
157 $("tr[name^='"+type+"_'][name$='_"+index+"'] .splitbutton").hide();
158 $("tr[name^='"+type+"_'][name$='_"+index+"']:last .splitbutton").show();
159
160 $("#reset"+lineId+"_"+(nbrTrs)+"_"+index).click(function (event) {
161 event.preventDefault();
162 id = $(this).attr("id");
163 id = id.split("reset"+lineId+"_");
164 idrow = id[1];
165 idlast = $("tr[name^='"+type+"_'][name$='_"+index+"']:last .qtydispatchinput").attr("id");
166 if (idlast == $("#qty"+lineId+"_"+idrow).attr("id")) {
167 console.log("expedition/js/lib_dispatch.js.php Remove trigger for tr name = "+type+"_"+idrow);
168 $('tr[name="'+type+'_'+idrow+'"').remove();
169 $("tr[name^='"+type+"_'][name$='_"+index+"']:last .splitbutton").show();
170 } else {
171 console.log("expedition/js/lib_dispatch.js.php Reset trigger for id = qty_"+idrow);
172 $("#qty"+lineId+"_"+idrow).val("");
173 }
174 });
175
176 if (mode === 'lessone')
177 {
178 qty = 1; // keep 1 in old line
179 $("#qty"+lineId+"_"+(nbrTrs-1)+"_"+index).val(qty);
180 }
181 $("#qty"+lineId+"_"+nbrTrs+"_"+index).val(newlineqty);
182 // Store arbitrary data for dispatch qty input field change event
183 $("#qty"+lineId+"_" + (nbrTrs - 1) + "_" + index).data('qty', qty);
184 $("#qty"+lineId+"_" + (nbrTrs - 1) + "_" + index).data('type', type);
185 $("#qty"+lineId+"_" + (nbrTrs - 1) + "_" + index).data('index', index);
186 // Update dispatched qty when value dispatch qty input field changed
187 //$("#qty_" + (nbrTrs - 1) + "_" + index).change(this.onChangeDispatchLineQty);
188 //set focus on lot of new line (if it exists)
189 $("#lot_number"+lineId+"_"+(nbrTrs)+"_"+index).focus();
190 //Clean bad values
191 $("tr[name^='"+type+"_'][name$='_"+index + "']:last").data("remove", "remove");
192 $("#lot_number_"+(nbrTrs) + "_"+index).val("")
193 $("#idline"+lineId+"_"+(nbrTrs)+"_"+index).val("-1")
194 $("#qty"+lineId+"_"+(nbrTrs)+"_"+index).data('expected', "0");
195 //$("input[type='hidden']#lot_number_" + (nbrTrs) + "_" + index).remove();
196 $("#lot_number"+lineId+"_"+(nbrTrs)+"_"+index).removeAttr("disabled");
197 }
198}
199
209function onChangeDispatchLineQty(element) {
210 var type = $(element).data('type'),
211 qty = parseFloat($(element).data('expected')),
212 changedQty, nbrTrs, dispatchingQty, qtyOrdered, qtyDispatched;
213 id = $(element).attr("id");
214 id = id.split("_");
215 index = id[2];
216
217 if (index >= 0 && type && qty >= 0) {
218 nbrTrs = $("tr[name^='"+type+"_'][name$='_"+index+"']").length;
219 qtyChanged = parseFloat($(element).val()) - qty; // qty changed
220 qtyDispatching = parseFloat($(element).val()); // qty currently being dispatched
221 qtyOrdered = parseFloat($("#qty_ordered_0_"+index).val()); // qty ordered
222 qtyDispatched = parseFloat($("#qty_dispatched_0_"+index).val()); // qty already dispatched
223
224 console.log("onChangeDispatchLineQty qtyChanged: "+qtyChanged+" qtyDispatching: "+qtyDispatching+" qtyOrdered: "+qtyOrdered+" qtyDispatched: "+qtyDispatched);
225
226 if ((qtyChanged) <= (qtyOrdered - (qtyDispatched + qtyDispatching))) {
227 $("#qty_dispatched_0_" + index).val(qtyDispatched + qtyChanged);
228 } else {
229 /*console.log("eee");
230 $(element).val($(element).data('expected'));*/
231 }
232 $(element).data('expected', $(element).val());
233 }
234}
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
if(preg_match('/(crypted|dolcrypt):/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
Definition repair.php:125