dolibarr 24.0.0-beta
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.
53header('Cache-Control: max-age=10800, public, must-revalidate');
54
55?>
64function addDispatchLine(index, type, mode) {
65 mode = mode || 'qtymissing'
66
67 console.log("fourn/js/lib_dispatch.js.php addDispatchLine Split line type="+type+" index="+index+" mode="+mode);
68
69 var $row0 = $("tr[name='"+type+'_0_'+index+"']");
70 var $dpopt = $row0.find('.hasDatepicker').first().datepicker('option', 'all'); // get current datepicker options to apply the same to the cloned datepickers
71 var $row = $row0.clone(true); // clone first batch line to jQuery object
72 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
73 var qtyOrdered = parseFloat($("#qty_ordered_0_"+index).val()); // Qty ordered is same for all rows
74
75 var qty = parseFloat($("#qty_"+(nbrTrs - 1)+"_"+index).val());
76 if (isNaN(qty)) {
77 qty = '';
78 }
79
80 console.log("fourn/js/lib_dispatch.js.php addDispatchLine Split line nbrTrs="+nbrTrs+" qtyOrdered="+qtyOrdered+" qty="+qty);
81
82 var qtyDispatched;
83
84 if (mode === 'lessone') {
85 qtyDispatched = parseFloat($("#qty_dispatched_0_" + index).val()) + 1;
86 }
87 else {
88 qtyDispatched = parseFloat($("#qty_dispatched_0_" + index).val()) + qty;
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_0_" + index).val()) + 1;
92 mode = 'lessone';
93 }
94 }
95 console.log("qtyDispatched=" + qtyDispatched + " qtyOrdered=" + qtyOrdered+ " qty=" + qty);
96
97 if (qty <= 1) {
98 window.alert("Remain quantity to dispatch is too low to be split");
99 } else {
100 var oldlineqty = qtyDispatched;
101 var newlineqty = qtyOrdered - qtyDispatched;
102 if (newlineqty <= 0) {
103 newlineqty = qty - 1;
104 oldlineqty = 1;
105 $("#qty_"+(nbrTrs - 1)+"_"+index).val(oldlineqty);
106 }
107
108 //replace tr suffix nbr
109 $row.html($row.html().replace(/_0_/g, "_" + nbrTrs + "_"));
110
111 // jquery's deep clone is incompatible with date pickers (the clone shares data with the original)
112 // so we destroy and rebuild the new date pickers
113 setTimeout(() => {
114 $row.find('.hasDatepicker').each((i, dp) => {
115 $(dp).removeData()
116 .removeClass('hasDatepicker');
117 $(dp).next('img.ui-datepicker-trigger').remove();
118 $(dp).datepicker($dpopt);
119 });
120 }, 0);
121
122 //create new select2 to avoid duplicate id of cloned one
123 $row.find("select[name='" + 'entrepot_' + nbrTrs + '_' + index + "']").select2();
124 // Copy selected option to new select
125 let $prevRow = $("tr[name^='" + type + "_'][name$='_" + index + "']:last")
126 let $prevEntr = Number($prevRow.find("select[name='" + 'entrepot_' + (nbrTrs-1) + '_' + index + "']").val())
127 $row.find("select[name='" + 'entrepot_' + nbrTrs + '_' + index + "']").val($prevEntr)
128 // TODO find solution to keep new tr's after page refresh
129 //clear value
130 $row.find("input[name^='qty']").val('');
131 //change name of new row
132 $row.attr('name', type + '_' + nbrTrs + '_' + index);
133 //insert new row before last row
134 $("tr[name^='" + type + "_'][name$='_" + index + "']:last").after($row);
135
136 //remove cloned select2 with duplicate id.
137 $("#s2id_entrepot_" + nbrTrs + '_' + index).detach(); // old way to find duplicated select2 component
138 $(".csswarehouse_" + nbrTrs + "_" + index + ":first-child").parent("span.selection").parent(".select2").detach();
139
140 /* Suffix of lines are: _ trs.length _ index */
141 $("#qty_"+nbrTrs+"_"+index).focus();
142 $("#qty_dispatched_0_"+index).val(oldlineqty);
143
144 //hide all buttons then show only the last one
145 $("tr[name^='" + type + "_'][name$='_" + index + "'] .splitbutton").hide();
146 $("tr[name^='" + type + "_'][name$='_" + index + "']:last .splitbutton").show();
147
148 $("#reset_" + (nbrTrs) + "_" + index).click(function (event) {
149 event.preventDefault();
150 id = $(this).attr("id");
151 id = id.split("reset_");
152 idrow = id[1];
153 idlast = $("tr[name^='" + type + "_'][name$='_" + index + "']:last .qtydispatchinput").attr("id");
154 if (idlast == $("#qty_" + idrow).attr("id")) {
155 console.log("Remove trigger for tr name = " + type + "_" + idrow);
156 $('tr[name="' + type + '_' + idrow + '"').remove();
157 $("tr[name^='" + type + "_'][name$='_" + index + "']:last .splitbutton").show();
158 } else {
159 console.log("fourn/js/lib_dispatch.js.php Reset trigger for id = qty_" + idrow);
160 $("#qty_" + idrow).val("");
161 }
162 });
163
164 if (mode === 'lessone')
165 {
166 qty = 1; // keep 1 in old line
167 $("#qty_"+(nbrTrs-1)+"_"+index).val(qty);
168 }
169 $("#qty_"+nbrTrs+"_"+index).val(newlineqty);
170 // Store arbitrary data for dispatch qty input field change event
171 $("#qty_" + (nbrTrs - 1) + "_" + index).data('qty', qty);
172 $("#qty_" + (nbrTrs - 1) + "_" + index).data('type', type);
173 $("#qty_" + (nbrTrs - 1) + "_" + index).data('index', index);
174 // Update dispatched qty when value dispatch qty input field changed
175 //$("#qty_" + (nbrTrs - 1) + "_" + index).change(this.onChangeDispatchLineQty);
176 //set focus on lot of new line (if it exists)
177 $("#lot_number_" + (nbrTrs) + "_" + index).focus();
178 //Clean bad values
179 $("tr[name^='" + type + "_'][name$='_" + index + "']:last").data("remove", "remove");
180 $("#lot_number_" + (nbrTrs) + "_" + index).val("")
181 $("#idline_" + (nbrTrs) + "_" + index).val("-1")
182 $("#qty_" + (nbrTrs) + "_" + index).data('expected', "0");
183 $("#lot_number_" + (nbrTrs) + "_" + index).removeAttr("disabled");
184 }
185}
186
196function onChangeDispatchLineQty(element) {
197 var type = $(element).data('type'),
198 qty = parseFloat($(element).data('expected')),
199 changedQty, nbrTrs, dispatchingQty, qtyOrdered, qtyDispatched;
200 id = $(element).attr("id");
201 id = id.split("_");
202 index = id[2];
203
204 if (index >= 0 && type && qty >= 0) {
205 nbrTrs = $("tr[name^='" + type + "_'][name$='_" + index + "']").length;
206 qtyChanged = parseFloat($(element).val()) - qty; // qty changed
207 qtyDispatching = parseFloat($(element).val()); // qty currently being dispatched
208 qtyOrdered = parseFloat($("#qty_ordered_0_" + index).val()); // qty ordered
209 qtyDispatched = parseFloat($("#qty_dispatched_0_" + index).val()); // qty already dispatched
210
211 console.log("onChangeDispatchLineQty qtyChanged: " + qtyChanged + " qtyDispatching: " + qtyDispatching + " qtyOrdered: " + qtyOrdered + " qtyDispatched: " + qtyDispatched);
212
213 if ((qtyChanged) <= (qtyOrdered - (qtyDispatched + qtyDispatching))) {
214 $("#qty_dispatched_0_" + index).val(qtyDispatched + qtyChanged);
215 } else {
216 /*console.log("eee");
217 $(element).val($(element).data('expected'));*/
218 }
219 $(element).data('expected', $(element).val());
220 }
221}
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:130
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:133