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