dolibarr 21.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 *
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 $modBarCodeThirdparty';
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, '', null, '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 $modBarCodeProduct';
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, '', null, '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
288llxHeader('', $langs->trans("MassBarcodeInit"), '', '', 0, 0, '', '', '', 'mod-barcode page-codeinit');
289
290print load_fiche_titre($langs->trans("MassBarcodeInit"), '', 'title_setup.png');
291print '<br>';
292
293print '<span class="opacitymedium">'.$langs->trans("MassBarcodeInitDesc").'</span><br>';
294print '<br>';
295
296//print img_picto('','puce').' '.$langs->trans("PrintsheetForOneBarCode").'<br>';
297//print '<br>';
298
299print '<br>';
300
301
302
303// Example 1 : Adding jquery code
304print '<script type="text/javascript">
305function confirm_erase() {
306 return confirm("'.dol_escape_js($langs->trans("ConfirmEraseAllCurrentBarCode")).'");
307}
308</script>';
309
310
311// For thirdparty
312if (isModEnabled('societe')) {
313 print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
314 print '<input type="hidden" name="mode" value="label">';
315 print '<input type="hidden" name="action" value="initbarcodethirdparties">';
316 print '<input type="hidden" name="token" value="'.newToken().'">';
317 $nbthirdpartyno = $nbthirdpartytotal = 0;
318
319 print '<div class="divsection">';
320
321 print load_fiche_titre($langs->trans("BarcodeInitForThirdparties"), '', 'company');
322
323 $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."societe where barcode IS NULL or barcode = ''";
324 $resql = $db->query($sql);
325 if ($resql) {
326 $obj = $db->fetch_object($resql);
327 $nbthirdpartyno = $obj->nb;
328 } else {
329 dol_print_error($db);
330 }
331
332 $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."societe";
333 $sql .= " WHERE entity IN (".getEntity('societe').")";
334 $resql = $db->query($sql);
335 if ($resql) {
336 $obj = $db->fetch_object($resql);
337 $nbthirdpartytotal = $obj->nb;
338 } else {
339 dol_print_error($db);
340 }
341
342 print $langs->trans("CurrentlyNWithoutBarCode", $nbthirdpartyno, $nbthirdpartytotal, $langs->transnoentitiesnoconv("ThirdParties"))."\n";
343
344 $disabledthirdparty = $disabledthirdparty1 = 0;
345 $titleno = '';
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 '<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>';
370 print '</div>';
371 print '<br>';
372 print '</form>';
373}
374
375
376// For products
377if (isModEnabled('product') || isModEnabled('service')) {
378 print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
379 print '<input type="hidden" name="mode" value="label">';
380 print '<input type="hidden" name="action" value="initbarcodeproducts">';
381 print '<input type="hidden" name="token" value="'.newToken().'">';
382
383 $nbproductno = $nbproducttotal = 0;
384
385 print '<div class="divsection">';
386
387 print load_fiche_titre($langs->trans("BarcodeInitForProductsOrServices"), '', 'product');
388
389 $sql = "SELECT count(rowid) as nb, fk_product_type, datec";
390 $sql .= " FROM ".MAIN_DB_PREFIX."product";
391 $sql .= " WHERE barcode IS NULL OR barcode = ''";
392 $sql .= " AND entity IN (".getEntity('product').")";
393 $sql .= " GROUP BY fk_product_type, datec";
394 $sql .= " ORDER BY datec";
395 $resql = $db->query($sql);
396 if ($resql) {
397 $num = $db->num_rows($resql);
398
399 $i = 0;
400 while ($i < $num) {
401 $obj = $db->fetch_object($resql);
402 $nbproductno += $obj->nb;
403
404 $i++;
405 }
406 } else {
407 dol_print_error($db);
408 }
409
410 $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."product";
411 $sql .= " WHERE entity IN (".getEntity('product').")";
412 $resql = $db->query($sql);
413 if ($resql) {
414 $obj = $db->fetch_object($resql);
415 $nbproducttotal = $obj->nb;
416 } else {
417 dol_print_error($db);
418 }
419
420 print $langs->trans("CurrentlyNWithoutBarCode", $nbproductno, $nbproducttotal, $langs->transnoentitiesnoconv("ProductsOrServices"))."\n";
421
422 $disabledproduct = $disabledproduct1 = 0;
423
424 $titleno = '';
425 if (is_object($modBarCodeProduct)) {
426 print '<br>'.$langs->trans("BarCodeNumberManager").": ";
427 $objproduct = new Product($db);
428 print '<b>'.(isset($modBarCodeProduct->name) ? $modBarCodeProduct->name : $modBarCodeProduct->nom).'</b> - '.$langs->trans("NextValue").': <b>'.$modBarCodeProduct->getNextValue($objproduct).'</b><br>';
429 $disabledproduct = 0;
430 print '<br>';
431 } else {
432 $disabledproduct = 1;
433 $titleno = $langs->trans("NoBarcodeNumberingTemplateDefined");
434 print '<br><div class="warning">'.$langs->trans("NoBarcodeNumberingTemplateDefined");
435 print '<br><a href="'.DOL_URL_ROOT.'/admin/barcode.php">'.$langs->trans("ToGenerateCodeDefineAutomaticRuleFirst").'</a>';
436 print '</div>';
437 }
438 if (empty($nbproductno)) {
439 $disabledproduct1 = 1;
440 }
441
442 //print '<input type="checkbox" id="erasealreadyset" name="erasealreadyset"> '.$langs->trans("ResetBarcodeForAllRecords").'<br>';
443 $moretagsproduct1 = (($disabledproduct || $disabledproduct1) ? ' disabled title="'.dol_escape_htmltag($titleno).'"' : '');
444 print '<input type="submit" class="button" name="submitformbarcodeproductgen" id="submitformbarcodeproductgen" value="'.$langs->trans("InitEmptyBarCode", min($maxperinit, $nbproductno)).'"'.$moretagsproduct1.'>';
445 $moretagsproduct2 = (($nbproductno == $nbproducttotal) ? ' disabled' : '');
446 print ' &nbsp; ';
447 print '<input type="submit" class="button butActionDelete" name="eraseallproductbarcode" id="eraseallproductbarcode" value="'.$langs->trans("EraseAllCurrentBarCode").'"'.$moretagsproduct2.' onClick="return confirm_erase();">';
448 print '<br><br>';
449 print '</div>';
450 print '<br>';
451 print '</form>';
452}
453
454
455print '<div class="divsection">';
456
457print load_fiche_titre($langs->trans("BarCodePrintsheet"), '', 'generic');
458print $langs->trans("ClickHereToGoTo").' : <a href="'.DOL_URL_ROOT.'/barcode/printsheet.php">'.$langs->trans("BarCodePrintsheet").'</a>';
459print '<br>'."\n";
460
461print '<br>';
462
463print '</div>';
464
465
466// End of page
467llxFooter();
468$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:70
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...
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.