dolibarr 21.0.0-alpha
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// Load translation files required by the page
51$langs->loadLangs(array("mrp"));
52
53// Define javascript type
54top_httphead('text/javascript; charset=UTF-8');
55// Important: Following code is to avoid page request by browser and PHP CPU at each Dolibarr page access.
56if (empty($dolibarr_nocache)) {
57 header('Cache-Control: max-age=10800, public, must-revalidate');
58} else {
59 header('Cache-Control: no-cache');
60}
61
62?>
71function addDispatchLine(index, type, mode)
72{
73 mode = mode || 'qtymissing'
74
75 console.log("mrp/js/lib_dispatch.js.php Split line type="+type+" index="+index+" mode="+mode);
76 if(mode == 'qtymissingconsume' || mode == 'allmissingconsume') {
77 var inputId = 'qtytoconsume';
78 var warehouseId = 'idwarehouse';
79 }
80 else {
81 var inputId = 'qtytoproduce';
82 var warehouseId = 'idwarehousetoproduce';
83 }
84 var nbrTrs = $("tr[name^='"+type+"_"+index+"']").length; // position of line for batch
85 var $row = $("tr[name='"+type+'_'+index+"_1']").clone(true); // clone last batch line to jQuery object
86 var qtyOrdered = parseFloat($("#qty_ordered_"+index).val()); // Qty ordered is same for all rows
87 var qty = parseFloat($("#"+inputId+"-"+index+"-"+nbrTrs).val());
88 var qtyDispatched;
89
90 if (mode === 'lessone')
91 {
92 qtyDispatched = parseFloat($("#qty_dispatched_"+index).val()) + 1;
93 }
94 else
95 {
96 qtyDispatched = parseFloat($("#qty_dispatched_"+index).val()) + qty;
97 console.log($("#qty_dispatched_"+index).val());
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_" + index).val()) + 1;
101
102 }
103 if(mode == 'allmissingconsume' || mode == 'alltoproduce') {
104 var qtymax = parseFloat($($row).data('max-qty'));
105 if(qtymax === 'undefined') qtymax = 1;
106 }
107 }
108
109
110 if(mode == 'allmissingconsume' || mode == 'alltoproduce') {
111 var count = 0;
112 var qtyalreadyused = 0;
113 var error = 0;
114
115 while (qtyalreadyused < qty) {
116 //If remaining qty needed is inferior to qty asked, qtymax = qty asked - qty already used
117 if ((qtyalreadyused + qtymax) > qty) qtymax = qty - qtyalreadyused;
118 //If first line, we replace value, not add line
119 if(count === 0){
120 $("#"+inputId+"-"+index+"-"+nbrTrs).val(qtymax);
121 } else {
122 var res = addDispatchTR(qtyOrdered, qtyDispatched, index, nbrTrs, warehouseId, inputId, type, qtymax, mode, $row);
123 if(res === -1){
124 error = 1;
125 break;
126 }
127 nbrTrs++;
128 }
129 qtyalreadyused = qtyalreadyused + qtymax;
130 count++;
131 $row = $("tr[name='" + type + '_' + index + "_1']").clone(true);
132 }
133
134 if(error === 0) {
135 addDispatchTR(qtyOrdered, qtyDispatched, index, nbrTrs, warehouseId, inputId, type, '', mode, $row);
136 }
137 }
138 else addDispatchTR(qtyOrdered, qtyDispatched, index, nbrTrs, warehouseId, inputId, type, qty, mode, $row)
139
140}
141
157function addDispatchTR(qtyOrdered, qtyDispatched, index, nbrTrs, warehouseId, inputId, type, qty, mode, $row) {
158 if (qtyOrdered <= 1) {
159 let errormsg = '<?php echo dol_escape_js($langs->trans('QtyCantBeSplit')); ?>';
160 $.jnotify(errormsg, 'error', true);
161 return -1;
162 } else if (qtyDispatched >= qtyOrdered) {
163 let errormsg = '<?php echo dol_escape_js($langs->trans('NoRemainQtyToDispatch')); ?>';
164 $.jnotify(errormsg, 'error', true);
165 return -1;
166 } else if (qtyDispatched < qtyOrdered) {
167 //replace tr suffix nbr
168 var re1 = new RegExp('_'+index+'_1', 'g');
169 var re2 = new RegExp('-'+index+'-1', 'g');
170 $row.html($row.html().replace(re1, '_'+index+'_'+(nbrTrs+1)));
171 $row.html($row.html().replace(re2, '-'+index+'-'+(nbrTrs+1)));
172 //create new select2 to avoid duplicate id of cloned one
173 $row.find("select[name='"+warehouseId+'-'+index+'-'+(nbrTrs+1)+"']").select2();
174 // TODO find solution to copy selected option to new select
175 // TODO find solution to keep new tr's after page refresh
176 //clear value
177 $row.find("input[id^='"+inputId+"']").val('');
178 //change name of new row
179 $row.attr('name',type+'_'+index+'_'+(nbrTrs+1));
180 //insert new row before last row
181 $("tr[name^='"+type+"_"+index+"_"+nbrTrs+"']:last").after($row);
182
183 //remove cloned select2 with duplicate id.
184 $("#s2id_entrepot_"+nbrTrs+'_'+index).detach(); // old way to find duplicated select2 component
185 $(".csswarehouse_"+index+"_"+(nbrTrs+1)+":first-child").parent("span.selection").parent(".select2").detach();
186
187 /* Suffix of lines are: index _ trs.length */
188 $("#"+inputId+"-"+index+"-"+(nbrTrs+1)).focus();
189 if ($("#"+inputId+"-"+index+"-"+(nbrTrs)).val() == 0) {
190 if(mode == 'allmissingconsume' || mode == 'alltoproduce') $("#"+inputId+"-"+index+"-"+(nbrTrs)).val(qty);
191 else $("#"+inputId+"-"+index+"-"+(nbrTrs)).val(1);
192 }
193 var totalonallines = 0;
194 for (let i = 1; i <= nbrTrs; i++) {
195 console.log(i+" = "+parseFloat($("#"+inputId+"-"+index+"-"+i).val()));
196 totalonallines = totalonallines + parseFloat($("#"+inputId+"-"+index+"-"+i).val());
197 }
198
199 if(mode != 'allmissingconsume' && mode != 'alltoproduce') {
200 if (totalonallines == qtyOrdered && qtyOrdered > 1) {
201 var prevouslineqty = $("#" + inputId + "-" + index + "-" + nbrTrs).val();
202 $("#" + inputId + "-" + index + "-" + (nbrTrs)).val(1);
203 $("#" + inputId + "-" + index + "-" + (nbrTrs + 1)).val(prevouslineqty - 1);
204 }
205 }
206 $("#qty_dispatched_"+index).val(qtyDispatched);
207
208 //hide all buttons then show only the last one
209 $("tr[name^='"+type+"_"+index+"_'] .splitbutton").hide();
210 $("tr[name^='"+type+"_"+index+"_']:last .splitbutton").show();
211
212 if (mode === 'lessone')
213 {
214 qty = 1; // keep 1 in old line
215 $("#qty_"+(nbrTrs-1)+"_"+index).val(qty);
216 }
217 // Store arbitrary data for dispatch qty input field change event
218 $("#"+inputId+"-"+index+(nbrTrs)).data('qty', qty);
219 $("#"+inputId+"-"+index+(nbrTrs)).data('type', type);
220 $("#"+inputId+"-"+index+(nbrTrs)).data('index', index);
221 if(mode == 'allmissingconsume' || mode == 'alltoproduce') {
222 let currentQtyDispatched = qtyDispatched+qty;
223 $row.find("input[id^='"+inputId+"']").val(qty);
224 }
225 }
226}
227
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:137