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