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