dolibarr 20.0.2
codeinit.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2014-2022 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2018 Ferran Marcet <fmarcet@2byte.es>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
26// Load Dolibarr environment
27require '../main.inc.php';
28require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
29require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
30
31// Load translation files required by the page
32$langs->loadLangs(array('admin', 'members', 'errors', 'other'));
33
34// Choice of print year or current year.
35$now = dol_now();
36$year = dol_print_date($now, '%Y');
37$month = dol_print_date($now, '%m');
38$day = dol_print_date($now, '%d');
39$forbarcode = GETPOST('forbarcode');
40$fk_barcode_type = GETPOST('fk_barcode_type');
41$eraseallproductbarcode = GETPOST('eraseallproductbarcode');
42$eraseallthirdpartybarcode = GETPOST('eraseallthirdpartybarcode');
43
44$action = GETPOST('action', 'aZ09');
45
46$producttmp = new Product($db);
47$thirdpartytmp = new Societe($db);
48
49$modBarCodeProduct = '';
50$modBarCodeThirdparty = '';
51
52$maxperinit = !getDolGlobalString('BARCODE_INIT_MAX') ? 1000 : $conf->global->BARCODE_INIT_MAX;
53
54// Security check (enable the most restrictive one)
55//if ($user->socid > 0) accessforbidden();
56//if ($user->socid > 0) $socid = $user->socid;
57if (!isModEnabled('barcode')) {
58 accessforbidden('Module not enabled');
59}
60//restrictedArea($user, 'barcode');
61if (empty($user->admin)) {
62 accessforbidden('Must be admin');
63}
64
65
66/*
67 * Actions
68 */
69
70// Define barcode template for third-party
71if (getDolGlobalString('BARCODE_THIRDPARTY_ADDON_NUM')) {
72 $dirbarcodenum = array_merge(array('/core/modules/barcode/'), $conf->modules_parts['barcode']);
73
74 foreach ($dirbarcodenum as $dirroot) {
75 $dir = dol_buildpath($dirroot, 0);
76
77 $handle = @opendir($dir);
78 if (is_resource($handle)) {
79 while (($file = readdir($handle)) !== false) {
80 if (preg_match('/^mod_barcode_thirdparty_.*php$/', $file)) {
81 $file = substr($file, 0, dol_strlen($file) - 4);
82
83 try {
84 dol_include_once($dirroot.$file.'.php');
85 } catch (Exception $e) {
86 dol_syslog($e->getMessage(), LOG_ERR);
87 }
88
89 $modBarCodeThirdparty = new $file();
90 '@phan-var-force ModeleNumRefBarCode $module';
91 break;
92 }
93 }
94 closedir($handle);
95 }
96 }
97}
98
99if ($action == 'initbarcodethirdparties' && $user->hasRight('societe', 'lire')) {
100 if (!is_object($modBarCodeThirdparty)) {
101 $error++;
102 setEventMessages($langs->trans("NoBarcodeNumberingTemplateDefined"), null, 'errors');
103 }
104
105 if (!$error) {
106 $thirdpartystatic = new Societe($db);
107
108 $db->begin();
109
110 $nbok = 0;
111 if (!empty($eraseallthirdpartybarcode)) {
112 $sql = "UPDATE ".MAIN_DB_PREFIX."societe";
113 $sql .= " AND entity IN (".getEntity('societe').")";
114 $sql .= " SET barcode = NULL";
115 $resql = $db->query($sql);
116 if ($resql) {
117 setEventMessages($langs->trans("AllBarcodeReset"), null, 'mesgs');
118 } else {
119 $error++;
120 dol_print_error($db);
121 }
122 } else {
123 $sql = "SELECT rowid";
124 $sql .= " FROM ".MAIN_DB_PREFIX."societe";
125 $sql .= " WHERE barcode IS NULL or barcode = ''";
126 $sql .= " AND entity IN (".getEntity('societe').")";
127 $sql .= $db->order("datec", "ASC");
128 $sql .= $db->plimit($maxperinit);
129
130 dol_syslog("codeinit", LOG_DEBUG);
131 $resql = $db->query($sql);
132 if ($resql) {
133 $num = $db->num_rows($resql);
134
135 $i = 0;
136 $nbok = $nbtry = 0;
137 while ($i < min($num, $maxperinit)) {
138 $obj = $db->fetch_object($resql);
139 if ($obj) {
140 $thirdpartystatic->id = $obj->rowid;
141 $nextvalue = $modBarCodeThirdparty->getNextValue($thirdpartystatic, '');
142
143 $result = $thirdpartystatic->setValueFrom('barcode', $nextvalue, '', '', 'text', '', $user, 'THIRDPARTY_MODIFY');
144
145 $nbtry++;
146 if ($result > 0) {
147 $nbok++;
148 }
149 }
150
151 $i++;
152 }
153 } else {
154 $error++;
155 dol_print_error($db);
156 }
157
158 if (!$error) {
159 setEventMessages($langs->trans("RecordsModified", $nbok), null, 'mesgs');
160 }
161 }
162
163 if (!$error) {
164 //$db->rollback();
165 $db->commit();
166 } else {
167 $db->rollback();
168 }
169 }
170
171 $action = '';
172}
173
174// Define barcode template for products
175if (getDolGlobalString('BARCODE_PRODUCT_ADDON_NUM')) {
176 $dirbarcodenum = array_merge(array('/core/modules/barcode/'), $conf->modules_parts['barcode']);
177
178 foreach ($dirbarcodenum as $dirroot) {
179 $dir = dol_buildpath($dirroot, 0);
180
181 $handle = @opendir($dir);
182 if (is_resource($handle)) {
183 while (($file = readdir($handle)) !== false) {
184 if (preg_match('/^mod_barcode_product_.*php$/', $file)) {
185 $file = substr($file, 0, dol_strlen($file) - 4);
186
187 if ($file == getDolGlobalString('BARCODE_PRODUCT_ADDON_NUM')) {
188 try {
189 dol_include_once($dirroot.$file.'.php');
190 } catch (Exception $e) {
191 dol_syslog($e->getMessage(), LOG_ERR);
192 }
193
194 $modBarCodeProduct = new $file();
195 '@phan-var-force ModeleNumRefBarCode $module';
196 break;
197 }
198 }
199 }
200 closedir($handle);
201 }
202 }
203}
204
205if ($action == 'initbarcodeproducts' && $user->hasRight('produit', 'lire')) {
206 if (!is_object($modBarCodeProduct)) {
207 $error++;
208 setEventMessages($langs->trans("NoBarcodeNumberingTemplateDefined"), null, 'errors');
209 }
210
211 if (!$error) {
212 $productstatic = new Product($db);
213
214 $db->begin();
215
216 $nbok = 0;
217 if (!empty($eraseallproductbarcode)) {
218 $sql = "UPDATE ".MAIN_DB_PREFIX."product";
219 $sql .= " SET barcode = NULL";
220 $sql .= " WHERE entity IN (".getEntity('product').")";
221 $resql = $db->query($sql);
222 if ($resql) {
223 setEventMessages($langs->trans("AllBarcodeReset"), null, 'mesgs');
224 } else {
225 $error++;
226 dol_print_error($db);
227 }
228 } else {
229 $sql = "SELECT rowid, ref, fk_product_type";
230 $sql .= " FROM ".MAIN_DB_PREFIX."product";
231 $sql .= " WHERE barcode IS NULL or barcode = ''";
232 $sql .= " AND entity IN (".getEntity('product').")";
233 $sql .= $db->order("datec", "ASC");
234 $sql .= $db->plimit($maxperinit);
235
236 dol_syslog("codeinit", LOG_DEBUG);
237 $resql = $db->query($sql);
238 if ($resql) {
239 $num = $db->num_rows($resql);
240
241 $i = 0;
242 $nbok = $nbtry = 0;
243 while ($i < min($num, $maxperinit)) {
244 $obj = $db->fetch_object($resql);
245 if ($obj) {
246 $productstatic->id = $obj->rowid;
247 $productstatic->ref = $obj->ref;
248 $productstatic->type = $obj->fk_product_type;
249 $nextvalue = $modBarCodeProduct->getNextValue($productstatic, '');
250
251 //print 'Set value '.$nextvalue.' to product '.$productstatic->id." ".$productstatic->ref." ".$productstatic->type."<br>\n";
252 $result = $productstatic->setValueFrom('barcode', $nextvalue, '', '', 'text', '', $user, 'PRODUCT_MODIFY');
253
254 $nbtry++;
255 if ($result > 0) {
256 $nbok++;
257 }
258 }
259
260 $i++;
261 }
262 } else {
263 $error++;
264 dol_print_error($db);
265 }
266
267 if (!$error) {
268 setEventMessages($langs->trans("RecordsModified", $nbok), null, 'mesgs');
269 }
270 }
271
272 if (!$error) {
273 //$db->rollback();
274 $db->commit();
275 } else {
276 $db->rollback();
277 }
278 }
279
280 $action = '';
281}
282
283
284/*
285 * View
286 */
287
288$form = new Form($db);
289
290llxHeader('', $langs->trans("MassBarcodeInit"), '', '', 0, 0, '', '', '', 'mod-barcode page-codeinit');
291
292print load_fiche_titre($langs->trans("MassBarcodeInit"), '', 'title_setup.png');
293print '<br>';
294
295print '<span class="opacitymedium">'.$langs->trans("MassBarcodeInitDesc").'</span><br>';
296print '<br>';
297
298//print img_picto('','puce').' '.$langs->trans("PrintsheetForOneBarCode").'<br>';
299//print '<br>';
300
301print '<br>';
302
303
304
305// Example 1 : Adding jquery code
306print '<script type="text/javascript">
307function confirm_erase() {
308 return confirm("'.dol_escape_js($langs->trans("ConfirmEraseAllCurrentBarCode")).'");
309}
310</script>';
311
312
313// For thirdparty
314if (isModEnabled('societe')) {
315 print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
316 print '<input type="hidden" name="mode" value="label">';
317 print '<input type="hidden" name="action" value="initbarcodethirdparties">';
318 print '<input type="hidden" name="token" value="'.newToken().'">';
319 $nbthirdpartyno = $nbthirdpartytotal = 0;
320
321 print load_fiche_titre($langs->trans("BarcodeInitForThirdparties"), '', 'company');
322
323 print '<br>'."\n";
324 $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."societe where barcode IS NULL or barcode = ''";
325 $resql = $db->query($sql);
326 if ($resql) {
327 $obj = $db->fetch_object($resql);
328 $nbthirdpartyno = $obj->nb;
329 } else {
330 dol_print_error($db);
331 }
332
333 $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."societe";
334 $sql .= " WHERE entity IN (".getEntity('societe').")";
335 $resql = $db->query($sql);
336 if ($resql) {
337 $obj = $db->fetch_object($resql);
338 $nbthirdpartytotal = $obj->nb;
339 } else {
340 dol_print_error($db);
341 }
342
343 print $langs->trans("CurrentlyNWithoutBarCode", $nbthirdpartyno, $nbthirdpartytotal, $langs->transnoentitiesnoconv("ThirdParties"))."\n";
344
345 $disabledthirdparty = $disabledthirdparty1 = 0;
346
347 if (is_object($modBarCodeThirdparty)) {
348 print '<br>'.$langs->trans("BarCodeNumberManager").": ";
349 $objthirdparty = new Societe($db);
350 print '<b>'.(isset($modBarCodeThirdparty->name) ? $modBarCodeThirdparty->name : $modBarCodeThirdparty->nom).'</b> - '.$langs->trans("NextValue").': <b>'.$modBarCodeThirdparty->getNextValue($objthirdparty).'</b><br>';
351 $disabledthirdparty = 0;
352 print '<br>';
353 } else {
354 $disabledthirdparty = 1;
355 $titleno = $langs->trans("NoBarcodeNumberingTemplateDefined");
356 print '<div class="warning">'.$langs->trans("NoBarcodeNumberingTemplateDefined");
357 print '<br><a href="'.DOL_URL_ROOT.'/admin/barcode.php">'.$langs->trans("ToGenerateCodeDefineAutomaticRuleFirst").'</a>';
358 print '</div>';
359 }
360 if (empty($nbthirdpartyno)) {
361 $disabledthirdparty1 = 1;
362 }
363
364 $moretagsthirdparty1 = (($disabledthirdparty || $disabledthirdparty1) ? ' disabled title="'.dol_escape_htmltag($titleno).'"' : '');
365 print '<br><input class="button button-add" type="submit" id="submitformbarcodethirdpartygen" value="'.$langs->trans("InitEmptyBarCode", $nbthirdpartyno).'"'.$moretagsthirdparty1.'>';
366 $moretagsthirdparty2 = (($nbthirdpartyno == $nbthirdpartytotal) ? ' disabled' : '');
367 print ' &nbsp; ';
368 print '<input type="submit" class="button butActionDelete" name="eraseallthirdpartybarcode" id="eraseallthirdpartybarcode" value="'.$langs->trans("EraseAllCurrentBarCode").'"'.$moretagsthirdparty2.' onClick="return confirm_erase();">';
369 print '<br><br><br><br>';
370 print '</form>';
371}
372
373
374// For products
375if (isModEnabled('product') || isModEnabled('service')) {
376 print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
377 print '<input type="hidden" name="mode" value="label">';
378 print '<input type="hidden" name="action" value="initbarcodeproducts">';
379 print '<input type="hidden" name="token" value="'.newToken().'">';
380
381 $nbproductno = $nbproducttotal = 0;
382
383 print load_fiche_titre($langs->trans("BarcodeInitForProductsOrServices"), '', 'product');
384 print '<br>'."\n";
385
386 $sql = "SELECT count(rowid) as nb, fk_product_type, datec";
387 $sql .= " FROM ".MAIN_DB_PREFIX."product";
388 $sql .= " WHERE barcode IS NULL OR barcode = ''";
389 $sql .= " AND entity IN (".getEntity('product').")";
390 $sql .= " GROUP BY fk_product_type, datec";
391 $sql .= " ORDER BY datec";
392 $resql = $db->query($sql);
393 if ($resql) {
394 $num = $db->num_rows($resql);
395
396 $i = 0;
397 while ($i < $num) {
398 $obj = $db->fetch_object($resql);
399 $nbproductno += $obj->nb;
400
401 $i++;
402 }
403 } else {
404 dol_print_error($db);
405 }
406
407 $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."product";
408 $sql .= " WHERE entity IN (".getEntity('product').")";
409 $resql = $db->query($sql);
410 if ($resql) {
411 $obj = $db->fetch_object($resql);
412 $nbproducttotal = $obj->nb;
413 } else {
414 dol_print_error($db);
415 }
416
417 print $langs->trans("CurrentlyNWithoutBarCode", $nbproductno, $nbproducttotal, $langs->transnoentitiesnoconv("ProductsOrServices"))."\n";
418
419 $disabledproduct = $disabledproduct1 = 0;
420
421 if (is_object($modBarCodeProduct)) {
422 print '<br>'.$langs->trans("BarCodeNumberManager").": ";
423 $objproduct = new Product($db);
424 print '<b>'.(isset($modBarCodeProduct->name) ? $modBarCodeProduct->name : $modBarCodeProduct->nom).'</b> - '.$langs->trans("NextValue").': <b>'.$modBarCodeProduct->getNextValue($objproduct).'</b><br>';
425 $disabledproduct = 0;
426 print '<br>';
427 } else {
428 $disabledproduct = 1;
429 $titleno = $langs->trans("NoBarcodeNumberingTemplateDefined");
430 print '<br><div class="warning">'.$langs->trans("NoBarcodeNumberingTemplateDefined");
431 print '<br><a href="'.DOL_URL_ROOT.'/admin/barcode.php">'.$langs->trans("ToGenerateCodeDefineAutomaticRuleFirst").'</a>';
432 print '</div>';
433 }
434 if (empty($nbproductno)) {
435 $disabledproduct1 = 1;
436 }
437
438 //print '<input type="checkbox" id="erasealreadyset" name="erasealreadyset"> '.$langs->trans("ResetBarcodeForAllRecords").'<br>';
439 $moretagsproduct1 = (($disabledproduct || $disabledproduct1) ? ' disabled title="'.dol_escape_htmltag($titleno).'"' : '');
440 print '<input type="submit" class="button" name="submitformbarcodeproductgen" id="submitformbarcodeproductgen" value="'.$langs->trans("InitEmptyBarCode", min($maxperinit, $nbproductno)).'"'.$moretagsproduct1.'>';
441 $moretagsproduct2 = (($nbproductno == $nbproducttotal) ? ' disabled' : '');
442 print ' &nbsp; ';
443 print '<input type="submit" class="button butActionDelete" name="eraseallproductbarcode" id="eraseallproductbarcode" value="'.$langs->trans("EraseAllCurrentBarCode").'"'.$moretagsproduct2.' onClick="return confirm_erase();">';
444 print '<br><br><br><br>';
445 print '</form>';
446}
447
448
449print load_fiche_titre($langs->trans("BarCodePrintsheet"), '', 'generic');
450print '<br>'."\n";
451print $langs->trans("ClickHereToGoTo").' : <a href="'.DOL_URL_ROOT.'/barcode/printsheet.php">'.$langs->trans("BarCodePrintsheet").'</a>';
452
453
454
455print '<br>';
456
457// End of page
458llxFooter();
459$db->close();
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader()
Empty header.
Definition wrapper.php:55
llxFooter()
Empty footer.
Definition wrapper.php:69
Class to manage generation of HTML components Only common components must be here.
Class to manage products or services.
Class to manage third parties objects (customers, suppliers, prospects...)
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_now($mode='auto')
Return date for now.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.