dolibarr 25.0.0-alpha
lib_dispatch.js.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
3 * Copyright (C) 2026 MDW <mdeweerd@users.noreply.github.com>
4 */
5// Copyright (C) 2014 Cedric GROSS <c.gross@kreiz-it.fr>
6// Copyright (C) 2017 Francis Appels <francis.appels@z-application.com>
7//
8// This program is free software; you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation; either version 3 of the License, or
11// (at your option) any later version.
12//
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17//
18// You should have received a copy of the GNU General Public License
19// along with this program. If not, see <https://www.gnu.org/licenses/>.
20// or see https://www.gnu.org/
21
27if (!defined('NOREQUIRESOC')) {
28 define('NOREQUIRESOC', '1');
29}
30if (!defined('NOCSRFCHECK')) {
31 define('NOCSRFCHECK', 1);
32}
33if (!defined('NOTOKENRENEWAL')) {
34 define('NOTOKENRENEWAL', 1);
35}
36if (!defined('NOLOGIN')) {
37 define('NOLOGIN', 1);
38}
39if (!defined('NOREQUIREMENU')) {
40 define('NOREQUIREMENU', 1);
41}
42if (!defined('NOREQUIREHTML')) {
43 define('NOREQUIREHTML', 1);
44}
45if (!defined('NOREQUIREAJAX')) {
46 define('NOREQUIREAJAX', '1');
47}
48
49session_cache_limiter('public');
50
51require_once '../../main.inc.php';
52
61// Load translation files required by the page
62$langs->loadLangs(array("mrp"));
63
64// Define javascript type
65top_httphead('text/javascript; charset=UTF-8');
66// Important: Following code is to avoid page request by browser and PHP CPU at each Dolibarr page access.
67header('Cache-Control: max-age=10800, public, must-revalidate');
68
69?>
78function addDispatchLine(index, type, mode)
79{
80 mode = mode || 'qtymissing'
81
82 console.log("mrp/js/lib_dispatch.js.php Split line type="+type+" index="+index+" mode="+mode);
83 if (mode == 'qtymissingconsume' || mode == 'allmissingconsume') {
84 var inputId = 'qtytoconsume';
85 var warehouseId = 'idwarehouse';
86 }
87 else {
88 var inputId = 'qtytoproduce';
89 var warehouseId = 'idwarehousetoproduce';
90 }
91 var nbrTrs = $("tr[name^='"+type+"_"+index+"']").length; // position of line for batch
92 var $row = $("tr[name='"+type+'_'+index+"_1']").clone(true); // clone last batch line to jQuery object
93 var qtyOrdered = parseFloat($("#qty_ordered_"+index).val()); // Qty ordered is same for all rows
94 var qty = parseFloat($("#"+inputId+"-"+index+"-"+nbrTrs).val()); // qty entered in field to consume
95
96 console.log("#"+inputId+"-"+index+"-"+nbrTrs+" => "+qty);
97
98 var qtyDispatched;
99
100 if (mode === 'lessone')
101 {
102 qtyDispatched = parseFloat($("#qty_dispatched_"+index).val()) + 1;
103 }
104 else
105 {
106 console.log($("#qty_dispatched_"+index).val());
107 qtyDispatched = parseFloat($("#qty_dispatched_"+index).val()) + qty;
108 // 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
109 if (qtyDispatched == qtyOrdered && qtyDispatched > 1) {
110 qtyDispatched = parseFloat($("#qty_dispatched_" + index).val()) + 1;
111 }
112 if (mode == 'allmissingconsume' || mode == 'alltoproduce') {
113 var qtymax = parseFloat($($row).data('max-qty'));
114 if (qtymax === 'undefined') {
115 qtymax = 1;
116 }
117 }
118 }
119
120
121 if (mode == 'allmissingconsume' || mode == 'alltoproduce') {
122 var count = 0;
123 var qtyalreadyused = 0;
124 var error = 0;
125
126 while (qtyalreadyused < qty) {
127 //If remaining qty needed is inferior to qty asked, qtymax = qty asked - qty already used
128 if ((qtyalreadyused + qtymax) > qty) qtymax = qty - qtyalreadyused;
129 //If first line, we replace value, not add line
130 if(count === 0){
131 $("#"+inputId+"-"+index+"-"+nbrTrs).val(qtymax);
132 } else {
133 var res = addDispatchTR(qtyOrdered, qtyDispatched, index, nbrTrs, warehouseId, inputId, type, qtymax, mode, $row);
134 if(res === -1){
135 error = 1;
136 break;
137 }
138 nbrTrs++;
139 }
140 qtyalreadyused = qtyalreadyused + qtymax;
141 count++;
142 $row = $("tr[name='" + type + '_' + index + "_1']").clone(true);
143 }
144
145 if (error === 0) {
146 addDispatchTR(qtyOrdered, qtyDispatched, index, nbrTrs, warehouseId, inputId, type, '', mode, $row);
147 }
148 } else {
149 addDispatchTR(qtyOrdered, qtyDispatched, index, nbrTrs, warehouseId, inputId, type, qty, mode, $row);
150 }
151
152}
153
169function addDispatchTR(qtyOrdered, qtyDispatched, index, nbrTrs, warehouseId, inputId, type, qty, mode, $row) {
170 if (qtyOrdered <= 0) {
171 let errormsg = <?php echo "'".dol_escape_js($langs->trans('QtyCantBeSplit'))."'" ; ?>;
172 $.jnotify(errormsg, 'error', true);
173 return -1;
174 } else if (qtyDispatched >= qtyOrdered) {
175 let errormsg = <?php echo "'".dol_escape_js($langs->trans('NoRemainQtyToDispatch'))."'" ; ?>;
176 $.jnotify(errormsg, 'error', true);
177 return -1;
178 } else if (qtyDispatched < qtyOrdered) {
179 //replace tr suffix nbr
180 var re1 = new RegExp('_'+index+'_1', 'g');
181 var re2 = new RegExp('-'+index+'-1', 'g');
182 $row.html($row.html().replace(re1, '_'+index+'_'+(nbrTrs+1)));
183 $row.html($row.html().replace(re2, '-'+index+'-'+(nbrTrs+1)));
184 //create new select2 to avoid duplicate id of cloned one
185 $row.find("select[name='"+warehouseId+'-'+index+'-'+(nbrTrs+1)+"']").select2();
186 // TODO find solution to copy selected option to new select
187 // TODO find solution to keep new tr's after page refresh
188 //clear value
189 $row.find("input[id^='"+inputId+"']").val('');
190 //change name of new row
191 $row.attr('name',type+'_'+index+'_'+(nbrTrs+1));
192 //insert new row before last row
193 $("tr[name^='"+type+"_"+index+"_"+nbrTrs+"']:last").after($row);
194
195 //remove cloned select2 with duplicate id.
196 $("#s2id_entrepot_"+nbrTrs+'_'+index).detach(); // old way to find duplicated select2 component
197 $(".csswarehouse_"+index+"_"+(nbrTrs+1)+":first-child").parent("span.selection").parent(".select2").detach();
198
199 /* Suffix of lines are: index _ trs.length */
200 $("#"+inputId+"-"+index+"-"+(nbrTrs+1)).focus();
201 if ($("#"+inputId+"-"+index+"-"+(nbrTrs)).val() == 0) {
202 if(mode == 'allmissingconsume' || mode == 'alltoproduce') $("#"+inputId+"-"+index+"-"+(nbrTrs)).val(qty);
203 else $("#"+inputId+"-"+index+"-"+(nbrTrs)).val(1);
204 }
205 var totalonallines = 0;
206 for (let i = 1; i <= nbrTrs; i++) {
207 console.log(i+" = "+parseFloat($("#"+inputId+"-"+index+"-"+i).val()));
208 totalonallines = totalonallines + parseFloat($("#"+inputId+"-"+index+"-"+i).val());
209 }
210
211 if(mode != 'allmissingconsume' && mode != 'alltoproduce') {
212 if (totalonallines == qtyOrdered && qtyOrdered > 1) {
213 var prevouslineqty = $("#" + inputId + "-" + index + "-" + nbrTrs).val();
214 $("#" + inputId + "-" + index + "-" + (nbrTrs)).val(1);
215 $("#" + inputId + "-" + index + "-" + (nbrTrs + 1)).val(prevouslineqty - 1);
216 }
217 }
218 $("#qty_dispatched_"+index).val(qtyDispatched);
219
220 //hide all buttons then show only the last one
221 $("tr[name^='"+type+"_"+index+"_'] .splitbutton").hide();
222 $("tr[name^='"+type+"_"+index+"_']:last .splitbutton").show();
223
224 if (mode === 'lessone')
225 {
226 qty = 1; // keep 1 in old line
227 $("#qty_"+(nbrTrs-1)+"_"+index).val(qty);
228 }
229 // Store arbitrary data for dispatch qty input field change event
230 $("#"+inputId+"-"+index+(nbrTrs)).data('qty', qty);
231 $("#"+inputId+"-"+index+(nbrTrs)).data('type', type);
232 $("#"+inputId+"-"+index+(nbrTrs)).data('index', index);
233 if(mode == 'allmissingconsume' || mode == 'alltoproduce') {
234 let currentQtyDispatched = qtyDispatched+qty;
235 $row.find("input[id^='"+inputId+"']").val(qty);
236 }
237 }
238}
$propal type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
Definition propal.php:280
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.