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