dolibarr  17.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
26 require '../main.inc.php';
27 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
28 require_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 = empty($conf->global->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;
56 if (!isModEnabled('barcode')) {
57  accessforbidden('Module not enabled');
58 }
59 //restrictedArea($user, 'barcode');
60 if (empty($user->admin)) {
61  accessforbidden('Must be admin');
62 }
63 
64 
65 /*
66  * Actions
67  */
68 
69 // Define barcode template for third-party
70 if (!empty($conf->global->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 
97 if ($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 .= " SET barcode = NULL";
112  $resql = $db->query($sql);
113  if ($resql) {
114  setEventMessages($langs->trans("AllBarcodeReset"), null, 'mesgs');
115  } else {
116  $error++;
117  dol_print_error($db);
118  }
119  } else {
120  $sql = "SELECT rowid";
121  $sql .= " FROM ".MAIN_DB_PREFIX."societe";
122  $sql .= " WHERE barcode IS NULL or barcode = ''";
123  $sql .= $db->order("datec", "ASC");
124  $sql .= $db->plimit($maxperinit);
125 
126  dol_syslog("codeinit", LOG_DEBUG);
127  $resql = $db->query($sql);
128  if ($resql) {
129  $num = $db->num_rows($resql);
130 
131  $i = 0; $nbok = $nbtry = 0;
132  while ($i < min($num, $maxperinit)) {
133  $obj = $db->fetch_object($resql);
134  if ($obj) {
135  $thirdpartystatic->id = $obj->rowid;
136  $nextvalue = $modBarCodeThirdparty->getNextValue($thirdpartystatic, '');
137 
138  $result = $thirdpartystatic->setValueFrom('barcode', $nextvalue, '', '', 'text', '', $user, 'THIRDPARTY_MODIFY');
139 
140  $nbtry++;
141  if ($result > 0) {
142  $nbok++;
143  }
144  }
145 
146  $i++;
147  }
148  } else {
149  $error++;
150  dol_print_error($db);
151  }
152 
153  if (!$error) {
154  setEventMessages($langs->trans("RecordsModified", $nbok), null, 'mesgs');
155  }
156  }
157 
158  if (!$error) {
159  //$db->rollback();
160  $db->commit();
161  } else {
162  $db->rollback();
163  }
164  }
165 
166  $action = '';
167 }
168 
169 // Define barcode template for products
170 if (!empty($conf->global->BARCODE_PRODUCT_ADDON_NUM)) {
171  $dirbarcodenum = array_merge(array('/core/modules/barcode/'), $conf->modules_parts['barcode']);
172 
173  foreach ($dirbarcodenum as $dirroot) {
174  $dir = dol_buildpath($dirroot, 0);
175 
176  $handle = @opendir($dir);
177  if (is_resource($handle)) {
178  while (($file = readdir($handle)) !== false) {
179  if (preg_match('/^mod_barcode_product_.*php$/', $file)) {
180  $file = substr($file, 0, dol_strlen($file) - 4);
181 
182  if ($file == $conf->global->BARCODE_PRODUCT_ADDON_NUM) {
183  try {
184  dol_include_once($dirroot.$file.'.php');
185  } catch (Exception $e) {
186  dol_syslog($e->getMessage(), LOG_ERR);
187  }
188 
189  $modBarCodeProduct = new $file();
190  break;
191  }
192  }
193  }
194  closedir($handle);
195  }
196  }
197 }
198 
199 if ($action == 'initbarcodeproducts') {
200  if (!is_object($modBarCodeProduct)) {
201  $error++;
202  setEventMessages($langs->trans("NoBarcodeNumberingTemplateDefined"), null, 'errors');
203  }
204 
205  if (!$error) {
206  $productstatic = new Product($db);
207 
208  $db->begin();
209 
210  $nbok = 0;
211  if (!empty($eraseallproductbarcode)) {
212  $sql = "UPDATE ".MAIN_DB_PREFIX."product";
213  $sql .= " SET barcode = NULL";
214  $resql = $db->query($sql);
215  if ($resql) {
216  setEventMessages($langs->trans("AllBarcodeReset"), null, 'mesgs');
217  } else {
218  $error++;
219  dol_print_error($db);
220  }
221  } else {
222  $sql = "SELECT rowid, ref, fk_product_type";
223  $sql .= " FROM ".MAIN_DB_PREFIX."product";
224  $sql .= " WHERE barcode IS NULL or barcode = ''";
225  $sql .= $db->order("datec", "ASC");
226  $sql .= $db->plimit($maxperinit);
227 
228  dol_syslog("codeinit", LOG_DEBUG);
229  $resql = $db->query($sql);
230  if ($resql) {
231  $num = $db->num_rows($resql);
232 
233  $i = 0; $nbok = $nbtry = 0;
234  while ($i < min($num, $maxperinit)) {
235  $obj = $db->fetch_object($resql);
236  if ($obj) {
237  $productstatic->id = $obj->rowid;
238  $productstatic->ref = $obj->ref;
239  $productstatic->type = $obj->fk_product_type;
240  $nextvalue = $modBarCodeProduct->getNextValue($productstatic, '');
241 
242  //print 'Set value '.$nextvalue.' to product '.$productstatic->id." ".$productstatic->ref." ".$productstatic->type."<br>\n";
243  $result = $productstatic->setValueFrom('barcode', $nextvalue, '', '', 'text', '', $user, 'PRODUCT_MODIFY');
244 
245  $nbtry++;
246  if ($result > 0) {
247  $nbok++;
248  }
249  }
250 
251  $i++;
252  }
253  } else {
254  $error++;
255  dol_print_error($db);
256  }
257 
258  if (!$error) {
259  setEventMessages($langs->trans("RecordsModified", $nbok), null, 'mesgs');
260  }
261  }
262 
263  if (!$error) {
264  //$db->rollback();
265  $db->commit();
266  } else {
267  $db->rollback();
268  }
269  }
270 
271  $action = '';
272 }
273 
274 
275 /*
276  * View
277  */
278 
279 $form = new Form($db);
280 
281 llxHeader('', $langs->trans("MassBarcodeInit"));
282 
283 print load_fiche_titre($langs->trans("MassBarcodeInit"), '', 'title_setup.png');
284 print '<br>';
285 
286 print '<span class="opacitymedium">'.$langs->trans("MassBarcodeInitDesc").'</span><br>';
287 print '<br>';
288 
289 //print img_picto('','puce').' '.$langs->trans("PrintsheetForOneBarCode").'<br>';
290 //print '<br>';
291 
292 print '<br>';
293 
294 
295 
296 // Example 1 : Adding jquery code
297 print '<script type="text/javascript">
298 function confirm_erase() {
299  return confirm("'.dol_escape_js($langs->trans("ConfirmEraseAllCurrentBarCode")).'");
300 }
301 </script>';
302 
303 
304 // For thirdparty
305 if (isModEnabled('societe')) {
306  print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
307  print '<input type="hidden" name="mode" value="label">';
308  print '<input type="hidden" name="action" value="initbarcodethirdparties">';
309  print '<input type="hidden" name="token" value="'.newToken().'">';
310  $nbthirdpartyno = $nbthirdpartytotal = 0;
311 
312  print load_fiche_titre($langs->trans("BarcodeInitForThirdparties"), '', 'company');
313 
314  print '<br>'."\n";
315  $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."societe where barcode IS NULL or barcode = ''";
316  $resql = $db->query($sql);
317  if ($resql) {
318  $obj = $db->fetch_object($resql);
319  $nbthirdpartyno = $obj->nb;
320  } else {
321  dol_print_error($db);
322  }
323 
324  $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."societe";
325  $resql = $db->query($sql);
326  if ($resql) {
327  $obj = $db->fetch_object($resql);
328  $nbthirdpartytotal = $obj->nb;
329  } else {
330  dol_print_error($db);
331  }
332 
333  print $langs->trans("CurrentlyNWithoutBarCode", $nbthirdpartyno, $nbthirdpartytotal, $langs->transnoentitiesnoconv("ThirdParties"))."\n";
334 
335  $disabledthirdparty = $disabledthirdparty1 = 0;
336 
337  if (is_object($modBarCodeThirdparty)) {
338  print '<br>'.$langs->trans("BarCodeNumberManager").": ";
339  $objthirdparty = new Societe($db);
340  print '<b>'.(isset($modBarCodeThirdparty->name) ? $modBarCodeThirdparty->name : $modBarCodeThirdparty->nom).'</b> - '.$langs->trans("NextValue").': <b>'.$modBarCodeThirdparty->getNextValue($objthirdparty).'</b><br>';
341  $disabledthirdparty = 0;
342  print '<br>';
343  } else {
344  $disabledthirdparty = 1;
345  $titleno = $langs->trans("NoBarcodeNumberingTemplateDefined");
346  print '<div class="warning">'.$langs->trans("NoBarcodeNumberingTemplateDefined");
347  print '<br><a href="'.DOL_URL_ROOT.'/admin/barcode.php">'.$langs->trans("ToGenerateCodeDefineAutomaticRuleFirst").'</a>';
348  print '</div>';
349  }
350  if (empty($nbthirdpartyno)) {
351  $disabledthirdparty1 = 1;
352  }
353 
354  $moretagsthirdparty1 = (($disabledthirdparty || $disabledthirdparty1) ? ' disabled title="'.dol_escape_htmltag($titleno).'"' : '');
355  print '<br><input class="button button-add" type="submit" id="submitformbarcodethirdpartygen" value="'.$langs->trans("InitEmptyBarCode", $nbthirdpartyno).'"'.$moretagsthirdparty1.'>';
356  $moretagsthirdparty2 = (($nbthirdpartyno == $nbthirdpartytotal) ? ' disabled' : '');
357  print ' &nbsp; ';
358  print '<input type="submit" class="button butActionDelete" name="eraseallthirdpartybarcode" id="eraseallthirdpartybarcode" value="'.$langs->trans("EraseAllCurrentBarCode").'"'.$moretagsthirdparty2.' onClick="return confirm_erase();">';
359  print '<br><br><br><br>';
360  print '</form>';
361 }
362 
363 
364 // For products
365 if (isModEnabled('product') || isModEnabled('service')) {
366  print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
367  print '<input type="hidden" name="mode" value="label">';
368  print '<input type="hidden" name="action" value="initbarcodeproducts">';
369  print '<input type="hidden" name="token" value="'.newToken().'">';
370 
371  $nbproductno = $nbproducttotal = 0;
372 
373  print load_fiche_titre($langs->trans("BarcodeInitForProductsOrServices"), '', 'product');
374  print '<br>'."\n";
375 
376  $sql = "SELECT count(rowid) as nb, fk_product_type, datec";
377  $sql .= " FROM ".MAIN_DB_PREFIX."product";
378  $sql .= " WHERE barcode IS NULL OR barcode = ''";
379  $sql .= " GROUP BY fk_product_type, datec";
380  $sql .= " ORDER BY datec";
381  $resql = $db->query($sql);
382  if ($resql) {
383  $num = $db->num_rows($resql);
384 
385  $i = 0;
386  while ($i < $num) {
387  $obj = $db->fetch_object($resql);
388  $nbproductno += $obj->nb;
389 
390  $i++;
391  }
392  } else {
393  dol_print_error($db);
394  }
395 
396  $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."product";
397  $resql = $db->query($sql);
398  if ($resql) {
399  $obj = $db->fetch_object($resql);
400  $nbproducttotal = $obj->nb;
401  } else {
402  dol_print_error($db);
403  }
404 
405  print $langs->trans("CurrentlyNWithoutBarCode", $nbproductno, $nbproducttotal, $langs->transnoentitiesnoconv("ProductsOrServices"))."\n";
406 
407  $disabledproduct = $disabledproduct1 = 0;
408 
409  if (is_object($modBarCodeProduct)) {
410  print '<br>'.$langs->trans("BarCodeNumberManager").": ";
411  $objproduct = new Product($db);
412  print '<b>'.(isset($modBarCodeProduct->name) ? $modBarCodeProduct->name : $modBarCodeProduct->nom).'</b> - '.$langs->trans("NextValue").': <b>'.$modBarCodeProduct->getNextValue($objproduct).'</b><br>';
413  $disabledproduct = 0;
414  print '<br>';
415  } else {
416  $disabledproduct = 1;
417  $titleno = $langs->trans("NoBarcodeNumberingTemplateDefined");
418  print '<br><div class="warning">'.$langs->trans("NoBarcodeNumberingTemplateDefined");
419  print '<br><a href="'.DOL_URL_ROOT.'/admin/barcode.php">'.$langs->trans("ToGenerateCodeDefineAutomaticRuleFirst").'</a>';
420  print '</div>';
421  }
422  if (empty($nbproductno)) {
423  $disabledproduct1 = 1;
424  }
425 
426  //print '<input type="checkbox" id="erasealreadyset" name="erasealreadyset"> '.$langs->trans("ResetBarcodeForAllRecords").'<br>';
427  $moretagsproduct1 = (($disabledproduct || $disabledproduct1) ? ' disabled title="'.dol_escape_htmltag($titleno).'"' : '');
428  print '<input type="submit" class="button" name="submitformbarcodeproductgen" id="submitformbarcodeproductgen" value="'.$langs->trans("InitEmptyBarCode", min($maxperinit, $nbproductno)).'"'.$moretagsproduct1.'>';
429  $moretagsproduct2 = (($nbproductno == $nbproducttotal) ? ' disabled' : '');
430  print ' &nbsp; ';
431  print '<input type="submit" class="button butActionDelete" name="eraseallproductbarcode" id="eraseallproductbarcode" value="'.$langs->trans("EraseAllCurrentBarCode").'"'.$moretagsproduct2.' onClick="return confirm_erase();">';
432  print '<br><br><br><br>';
433  print '</form>';
434 }
435 
436 
437 print load_fiche_titre($langs->trans("BarCodePrintsheet"), '', 'generic');
438 print '<br>'."\n";
439 print $langs->trans("ClickHereToGoTo").' : <a href="'.DOL_URL_ROOT.'/barcode/printsheet.php">'.$langs->trans("BarCodePrintsheet").'</a>';
440 
441 
442 
443 print '<br>';
444 
445 // End of page
446 llxFooter();
447 $db->close();
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:49
dol_escape_htmltag
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
Definition: functions.lib.php:1493
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:70
load_fiche_titre
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
Definition: functions.lib.php:5360
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:520
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4993
dol_include_once
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
Definition: functions.lib.php:1032
dol_buildpath
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
Definition: functions.lib.php:1061
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2550
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:56
Exception
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1628
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3887
isModEnabled
isModEnabled($module)
Is Dolibarr module enabled.
Definition: functions.lib.php:137
Product
Class to manage products or services.
Definition: product.class.php:46
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:53
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2951
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8466
accessforbidden
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.
Definition: security.lib.php:1125