dolibarr 23.0.3
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 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
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 */
20
27// Load Dolibarr environment
28require '../main.inc.php';
29require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
30require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
31
40// Load translation files required by the page
41$langs->loadLangs(array('admin', 'members', 'errors', 'other'));
42
43// Choice of print year or current year.
44$now = dol_now();
45$year = dol_print_date($now, '%Y');
46$month = dol_print_date($now, '%m');
47$day = dol_print_date($now, '%d');
48$eraseallproductbarcode = GETPOST('eraseallproductbarcode');
49$eraseallthirdpartybarcode = GETPOST('eraseallthirdpartybarcode');
50
51$action = GETPOST('action', 'aZ09');
52
53$modBarCodeProduct = '';
54$modBarCodeThirdparty = '';
55
56$maxperinit = getDolGlobalInt('BARCODE_INIT_MAX', 1000);
57
58// Security check (enable the most restrictive one)
59//if ($user->socid > 0) accessforbidden();
60//if ($user->socid > 0) $socid = $user->socid;
61if (!isModEnabled('barcode')) {
62 accessforbidden('Module not enabled');
63}
64//restrictedArea($user, 'barcode');
65if (empty($user->admin)) {
66 accessforbidden('Must be admin');
67}
68
69
70/*
71 * Actions
72 */
73
74$error = 0;
75
76// Define barcode template for third-party
77if (getDolGlobalString('BARCODE_THIRDPARTY_ADDON_NUM')) {
78 $dirbarcodenum = array_merge(array('/core/modules/barcode/'), $conf->modules_parts['barcode']);
79
80 foreach ($dirbarcodenum as $dirroot) {
81 $dir = dol_buildpath($dirroot, 0);
82
83 $handle = @opendir($dir);
84 if (is_resource($handle)) {
85 while (($file = readdir($handle)) !== false) {
86 if (preg_match('/^mod_barcode_thirdparty_.*php$/', $file)) {
87 $file = substr($file, 0, dol_strlen($file) - 4);
88
89 try {
90 dol_include_once($dirroot.$file.'.php');
91 } catch (Exception $e) {
92 dol_syslog($e->getMessage(), LOG_ERR);
93 }
94
95 $modBarCodeThirdparty = new $file();
96 '@phan-var-force ModeleNumRefBarCode $modBarCodeThirdparty';
97 break;
98 }
99 }
100 closedir($handle);
101 }
102 }
103}
104
105if ($action == 'initbarcodethirdparties' && $user->hasRight('societe', 'lire')) {
106 if (!is_object($modBarCodeThirdparty)) {
107 $error++;
108 setEventMessages($langs->trans("NoBarcodeNumberingTemplateDefined"), null, 'errors');
109 }
110
111 if (!$error) {
112 $thirdpartystatic = new Societe($db);
113
114 $db->begin();
115
116 $nbok = 0;
117 if (!empty($eraseallthirdpartybarcode)) {
118 $sql = "UPDATE ".MAIN_DB_PREFIX."societe";
119 $sql .= " SET barcode = NULL";
120 $sql .= " WHERE barcode IS NOT NULL";
121 $sql .= " AND entity IN (".getEntity('societe').")";
122 $resql = $db->query($sql);
123 if ($resql) {
124 setEventMessages($langs->trans("AllBarcodeReset"), null, 'mesgs');
125 } else {
126 $error++;
127 dol_print_error($db);
128 }
129 } else {
130 $sql = "SELECT rowid";
131 $sql .= " FROM ".MAIN_DB_PREFIX."societe";
132 $sql .= " WHERE barcode IS NULL or barcode = ''";
133 $sql .= " AND entity IN (".getEntity('societe').")";
134 $sql .= $db->order("datec", "ASC");
135 $sql .= $db->plimit($maxperinit);
136
137 dol_syslog("codeinit", LOG_DEBUG);
138 $resql = $db->query($sql);
139 if ($resql) {
140 $num = $db->num_rows($resql);
141
142 $i = 0;
143 $nbok = $nbtry = 0;
144 while ($i < min($num, $maxperinit)) {
145 $obj = $db->fetch_object($resql);
146 if ($obj) {
147 $thirdpartystatic->id = $obj->rowid;
148 $nextvalue = $modBarCodeThirdparty->getNextValue($thirdpartystatic, '');
149
150 $result = $thirdpartystatic->setValueFrom('barcode', $nextvalue, '', null, 'text', '', $user, 'THIRDPARTY_MODIFY');
151
152 $nbtry++;
153 if ($result > 0) {
154 $nbok++;
155 }
156 }
157
158 $i++;
159 }
160 } else {
161 $error++;
162 dol_print_error($db);
163 }
164
165 if (!$error) {
166 setEventMessages($langs->trans("RecordsModified", $nbok), null, 'mesgs');
167 }
168 }
169
170 if (!$error) {
171 //$db->rollback();
172 $db->commit();
173 } else {
174 $db->rollback();
175 }
176 }
177
178 $action = '';
179}
180
181// Define barcode template for products
182if (getDolGlobalString('BARCODE_PRODUCT_ADDON_NUM')) {
183 $dirbarcodenum = array_merge(array('/core/modules/barcode/'), $conf->modules_parts['barcode']);
184
185 foreach ($dirbarcodenum as $dirroot) {
186 $dir = dol_buildpath($dirroot, 0);
187
188 $handle = @opendir($dir);
189 if (is_resource($handle)) {
190 while (($file = readdir($handle)) !== false) {
191 if (preg_match('/^mod_barcode_product_.*php$/', $file)) {
192 $file = substr($file, 0, dol_strlen($file) - 4);
193
194 if ($file == getDolGlobalString('BARCODE_PRODUCT_ADDON_NUM')) {
195 try {
196 dol_include_once($dirroot.$file.'.php');
197 } catch (Exception $e) {
198 dol_syslog($e->getMessage(), LOG_ERR);
199 }
200
201 $modBarCodeProduct = new $file();
202 '@phan-var-force ModeleNumRefBarCode $modBarCodeProduct';
203 break;
204 }
205 }
206 }
207 closedir($handle);
208 }
209 }
210}
211
212if ($action == 'initbarcodeproducts' && $user->hasRight('produit', 'lire')) {
213 if (!is_object($modBarCodeProduct)) {
214 $error++;
215 setEventMessages($langs->trans("NoBarcodeNumberingTemplateDefined"), null, 'errors');
216 }
217
218 if (!$error) {
219 $productstatic = new Product($db);
220
221 $db->begin();
222
223 $nbok = 0;
224 if (!empty($eraseallproductbarcode)) {
225 $sql = "UPDATE ".MAIN_DB_PREFIX."product";
226 $sql .= " SET barcode = NULL";
227 $sql .= " WHERE barcode IS NOT NULL AND entity IN (".getEntity('product').")";
228 $resql = $db->query($sql);
229 if ($resql) {
230 setEventMessages($langs->trans("AllBarcodeReset"), null, 'mesgs');
231 } else {
232 $error++;
233 dol_print_error($db);
234 }
235 } else {
236 $sql = "SELECT rowid, ref, fk_product_type";
237 $sql .= " FROM ".MAIN_DB_PREFIX."product";
238 $sql .= " WHERE barcode IS NULL or barcode = ''";
239 $sql .= " AND entity IN (".getEntity('product').")";
240 $sql .= $db->order("datec", "ASC");
241 $sql .= $db->plimit($maxperinit);
242
243 dol_syslog("codeinit", LOG_DEBUG);
244 $resql = $db->query($sql);
245 if ($resql) {
246 $num = $db->num_rows($resql);
247
248 $i = 0;
249 $nbok = $nbtry = 0;
250 while ($i < min($num, $maxperinit)) {
251 $obj = $db->fetch_object($resql);
252 if ($obj) {
253 $productstatic->id = $obj->rowid;
254 $productstatic->ref = $obj->ref;
255 $productstatic->type = $obj->fk_product_type;
256 $nextvalue = $modBarCodeProduct->getNextValue($productstatic, '');
257
258 //print 'Set value '.$nextvalue.' to product '.$productstatic->id." ".$productstatic->ref." ".$productstatic->type."<br>\n";
259 $result = $productstatic->setValueFrom('barcode', $nextvalue, '', null, 'text', '', $user, 'PRODUCT_MODIFY');
260
261 $nbtry++;
262 if ($result > 0) {
263 $nbok++;
264 }
265 }
266
267 $i++;
268 }
269 } else {
270 $error++;
271 dol_print_error($db);
272 }
273
274 if (!$error) {
275 setEventMessages($langs->trans("RecordsModified", $nbok), null, 'mesgs');
276 }
277 }
278
279 if (!$error) {
280 //$db->rollback();
281 $db->commit();
282 } else {
283 $db->rollback();
284 }
285 }
286
287 $action = '';
288}
289
290
291/*
292 * View
293 */
294
295llxHeader('', $langs->trans("MassBarcodeInit"), '', '', 0, 0, '', '', '', 'mod-barcode page-codeinit');
296
297if (!GETPOST('dol_openinpopup', 'aZ')) {
298 print load_fiche_titre($langs->trans("MassBarcodeInit"), '', 'title_setup');
299 print '<br>';
300}
301
302print '<span class="opacitymedium">'.$langs->trans("MassBarcodeInitDesc").'</span><br>';
303print '<br>';
304print '<br>';
305
306
307// Example 1 : Adding jquery code
308print '<script type="text/javascript">
309function confirm_erase() {
310 return confirm("'.dol_escape_js($langs->trans("ConfirmEraseAllCurrentBarCode")).'");
311}
312</script>';
313
314
315// For products
316if (isModEnabled('product') || isModEnabled('service')) {
317 print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
318 print '<input type="hidden" name="mode" value="label">';
319 print '<input type="hidden" name="action" value="initbarcodeproducts">';
320 print '<input type="hidden" name="token" value="'.newToken().'">';
321 print '<input type="hidden" name="dol_openinpopup" value="'.GETPOST('dol_openinpopup', 'aZ').'">';
322
323 $nbproductno = $nbproducttotal = 0;
324
325 print '<div class="divsection">';
326
327 print load_fiche_titre($langs->trans("BarcodeInitForProductsOrServices"), '', 'product');
328
329 $sql = "SELECT count(rowid) as nb, fk_product_type, datec";
330 $sql .= " FROM ".MAIN_DB_PREFIX."product";
331 $sql .= " WHERE barcode IS NULL OR barcode = ''";
332 $sql .= " AND entity IN (".getEntity('product').")";
333 $sql .= " GROUP BY fk_product_type, datec";
334 $sql .= " ORDER BY datec";
335 $resql = $db->query($sql);
336 if ($resql) {
337 $num = $db->num_rows($resql);
338
339 $i = 0;
340 while ($i < $num) {
341 $obj = $db->fetch_object($resql);
342 $nbproductno += $obj->nb;
343
344 $i++;
345 }
346 } else {
347 dol_print_error($db);
348 }
349
350 $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."product";
351 $sql .= " WHERE entity IN (".getEntity('product').")";
352 $resql = $db->query($sql);
353 if ($resql) {
354 $obj = $db->fetch_object($resql);
355 $nbproducttotal = $obj->nb;
356 } else {
357 dol_print_error($db);
358 }
359
360 print $langs->trans("CurrentlyNWithoutBarCode", $nbproductno, $nbproducttotal, $langs->transnoentitiesnoconv("ProductsOrServices"))."\n";
361
362 $disabledproduct = $disabledproduct1 = 0;
363
364 $titleno = '';
365 if (is_object($modBarCodeProduct)) {
366 print '<br>'.$langs->trans("BarCodeNumberManager").": ";
367 $objproduct = new Product($db);
368 print '<b>'.(isset($modBarCodeProduct->name) ? $modBarCodeProduct->name : $modBarCodeProduct->nom).'</b> - '.$langs->trans("NextValue").': <b>'.$modBarCodeProduct->getNextValue($objproduct).'</b><br>';
369 $disabledproduct = 0;
370 print '<br>';
371 } else {
372 $disabledproduct = 1;
373 $titleno = $langs->trans("NoBarcodeNumberingTemplateDefined");
374 print '<br><div class="warning">'.$langs->trans("NoBarcodeNumberingTemplateDefined");
375 print '<br><a href="'.DOL_URL_ROOT.'/admin/barcode.php">'.$langs->trans("ToGenerateCodeDefineAutomaticRuleFirst").'</a>';
376 print '</div>';
377 }
378 if (empty($nbproductno)) {
379 $disabledproduct1 = 1;
380 }
381
382 //print '<input type="checkbox" id="erasealreadyset" name="erasealreadyset"> '.$langs->trans("ResetBarcodeForAllRecords").'<br>';
383 $moretagsproduct1 = (($disabledproduct || $disabledproduct1) ? ' disabled title="'.dol_escape_htmltag($titleno).'"' : '');
384 print '<input type="submit" class="button" name="submitformbarcodeproductgen" id="submitformbarcodeproductgen" value="'.$langs->trans("InitEmptyBarCode", min($maxperinit, $nbproductno)).'"'.$moretagsproduct1.'>';
385 $moretagsproduct2 = (($nbproductno == $nbproducttotal) ? ' disabled' : '');
386 print ' &nbsp; ';
387 print '<input type="submit" class="button butActionDelete" name="eraseallproductbarcode" id="eraseallproductbarcode" value="'.$langs->trans("EraseAllCurrentBarCode").'"'.$moretagsproduct2.' onClick="return confirm_erase();">';
388 print '<br><br>';
389 print '</div>';
390 print '<br>';
391 print '</form>';
392}
393
394
395
396// For thirdparty
397if (isModEnabled('societe')) {
398 print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
399 print '<input type="hidden" name="mode" value="label">';
400 print '<input type="hidden" name="action" value="initbarcodethirdparties">';
401 print '<input type="hidden" name="token" value="'.newToken().'">';
402 print '<input type="hidden" name="dol_openinpopup" value="'.GETPOST('dol_openinpopup', 'aZ').'">';
403
404 $nbthirdpartyno = $nbthirdpartytotal = 0;
405
406 print '<div class="divsection">';
407
408 print load_fiche_titre($langs->trans("BarcodeInitForThirdparties"), '', 'company');
409
410 $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."societe where barcode IS NULL or barcode = ''";
411 $resql = $db->query($sql);
412 if ($resql) {
413 $obj = $db->fetch_object($resql);
414 $nbthirdpartyno = $obj->nb;
415 } else {
416 dol_print_error($db);
417 }
418
419 $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."societe";
420 $sql .= " WHERE entity IN (".getEntity('societe').")";
421 $resql = $db->query($sql);
422 if ($resql) {
423 $obj = $db->fetch_object($resql);
424 $nbthirdpartytotal = $obj->nb;
425 } else {
426 dol_print_error($db);
427 }
428
429 print $langs->trans("CurrentlyNWithoutBarCode", $nbthirdpartyno, $nbthirdpartytotal, $langs->transnoentitiesnoconv("ThirdParties"))."\n";
430
431 $disabledthirdparty = $disabledthirdparty1 = 0;
432 $titleno = '';
433
434 if (is_object($modBarCodeThirdparty)) {
435 print '<br>'.$langs->trans("BarCodeNumberManager").": ";
436 $objthirdparty = new Societe($db);
437 print '<b>'.(isset($modBarCodeThirdparty->name) ? $modBarCodeThirdparty->name : $modBarCodeThirdparty->nom).'</b> - '.$langs->trans("NextValue").': <b>'.$modBarCodeThirdparty->getNextValue($objthirdparty).'</b><br>';
438 $disabledthirdparty = 0;
439 print '<br>';
440 } else {
441 $disabledthirdparty = 1;
442 $titleno = $langs->trans("NoBarcodeNumberingTemplateDefined");
443 print '<div class="warning">'.$langs->trans("NoBarcodeNumberingTemplateDefined");
444 print '<br><a href="'.DOL_URL_ROOT.'/admin/barcode.php">'.$langs->trans("ToGenerateCodeDefineAutomaticRuleFirst").'</a>';
445 print '</div>';
446 }
447 if (empty($nbthirdpartyno)) {
448 $disabledthirdparty1 = 1;
449 }
450
451 $moretagsthirdparty1 = (($disabledthirdparty || $disabledthirdparty1) ? ' disabled title="'.dol_escape_htmltag($titleno).'"' : '');
452 print '<input class="button button-add" type="submit" id="submitformbarcodethirdpartygen" value="'.$langs->trans("InitEmptyBarCode", $nbthirdpartyno).'"'.$moretagsthirdparty1.'>';
453 $moretagsthirdparty2 = (($nbthirdpartyno == $nbthirdpartytotal) ? ' disabled' : '');
454 print ' &nbsp; ';
455 print '<input type="submit" class="button butActionDelete" name="eraseallthirdpartybarcode" id="eraseallthirdpartybarcode" value="'.$langs->trans("EraseAllCurrentBarCode").'"'.$moretagsthirdparty2.' onClick="return confirm_erase();">';
456 print '<br><br>';
457 print '</div>';
458 print '<br>';
459 print '</form>';
460}
461
462
463print '<div class="divsection">';
464
465print load_fiche_titre($langs->trans("BarCodePrintsheet"), '', 'generic');
466print $langs->trans("ClickHereToGoTo").' : <a href="'.DOL_URL_ROOT.'/barcode/printsheet.php">'.$langs->trans("BarCodePrintsheet").'</a>';
467print '<br>'."\n";
468
469print '<br>';
470
471print '</div>';
472
473
474// End of page
475llxFooter();
476$db->close();
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
Class to manage products or services.
Class to manage third parties objects (customers, suppliers, prospects...)
dol_now($mode='gmt')
Return date for now.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
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.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
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.