dolibarr  18.0.6
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 .= " 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; $nbok = $nbtry = 0;
134  while ($i < min($num, $maxperinit)) {
135  $obj = $db->fetch_object($resql);
136  if ($obj) {
137  $thirdpartystatic->id = $obj->rowid;
138  $nextvalue = $modBarCodeThirdparty->getNextValue($thirdpartystatic, '');
139 
140  $result = $thirdpartystatic->setValueFrom('barcode', $nextvalue, '', '', 'text', '', $user, 'THIRDPARTY_MODIFY');
141 
142  $nbtry++;
143  if ($result > 0) {
144  $nbok++;
145  }
146  }
147 
148  $i++;
149  }
150  } else {
151  $error++;
152  dol_print_error($db);
153  }
154 
155  if (!$error) {
156  setEventMessages($langs->trans("RecordsModified", $nbok), null, 'mesgs');
157  }
158  }
159 
160  if (!$error) {
161  //$db->rollback();
162  $db->commit();
163  } else {
164  $db->rollback();
165  }
166  }
167 
168  $action = '';
169 }
170 
171 // Define barcode template for products
172 if (!empty($conf->global->BARCODE_PRODUCT_ADDON_NUM)) {
173  $dirbarcodenum = array_merge(array('/core/modules/barcode/'), $conf->modules_parts['barcode']);
174 
175  foreach ($dirbarcodenum as $dirroot) {
176  $dir = dol_buildpath($dirroot, 0);
177 
178  $handle = @opendir($dir);
179  if (is_resource($handle)) {
180  while (($file = readdir($handle)) !== false) {
181  if (preg_match('/^mod_barcode_product_.*php$/', $file)) {
182  $file = substr($file, 0, dol_strlen($file) - 4);
183 
184  if ($file == $conf->global->BARCODE_PRODUCT_ADDON_NUM) {
185  try {
186  dol_include_once($dirroot.$file.'.php');
187  } catch (Exception $e) {
188  dol_syslog($e->getMessage(), LOG_ERR);
189  }
190 
191  $modBarCodeProduct = new $file();
192  break;
193  }
194  }
195  }
196  closedir($handle);
197  }
198  }
199 }
200 
201 if ($action == 'initbarcodeproducts') {
202  if (!is_object($modBarCodeProduct)) {
203  $error++;
204  setEventMessages($langs->trans("NoBarcodeNumberingTemplateDefined"), null, 'errors');
205  }
206 
207  if (!$error) {
208  $productstatic = new Product($db);
209 
210  $db->begin();
211 
212  $nbok = 0;
213  if (!empty($eraseallproductbarcode)) {
214  $sql = "UPDATE ".MAIN_DB_PREFIX."product";
215  $sql .= " SET barcode = NULL";
216  $sql .= " WHERE entity IN (".getEntity('product').")";
217  $resql = $db->query($sql);
218  if ($resql) {
219  setEventMessages($langs->trans("AllBarcodeReset"), null, 'mesgs');
220  } else {
221  $error++;
222  dol_print_error($db);
223  }
224  } else {
225  $sql = "SELECT rowid, ref, fk_product_type";
226  $sql .= " FROM ".MAIN_DB_PREFIX."product";
227  $sql .= " WHERE barcode IS NULL or barcode = ''";
228  $sql .= " AND entity IN (".getEntity('product').")";
229  $sql .= $db->order("datec", "ASC");
230  $sql .= $db->plimit($maxperinit);
231 
232  dol_syslog("codeinit", LOG_DEBUG);
233  $resql = $db->query($sql);
234  if ($resql) {
235  $num = $db->num_rows($resql);
236 
237  $i = 0; $nbok = $nbtry = 0;
238  while ($i < min($num, $maxperinit)) {
239  $obj = $db->fetch_object($resql);
240  if ($obj) {
241  $productstatic->id = $obj->rowid;
242  $productstatic->ref = $obj->ref;
243  $productstatic->type = $obj->fk_product_type;
244  $nextvalue = $modBarCodeProduct->getNextValue($productstatic, '');
245 
246  //print 'Set value '.$nextvalue.' to product '.$productstatic->id." ".$productstatic->ref." ".$productstatic->type."<br>\n";
247  $result = $productstatic->setValueFrom('barcode', $nextvalue, '', '', 'text', '', $user, 'PRODUCT_MODIFY');
248 
249  $nbtry++;
250  if ($result > 0) {
251  $nbok++;
252  }
253  }
254 
255  $i++;
256  }
257  } else {
258  $error++;
259  dol_print_error($db);
260  }
261 
262  if (!$error) {
263  setEventMessages($langs->trans("RecordsModified", $nbok), null, 'mesgs');
264  }
265  }
266 
267  if (!$error) {
268  //$db->rollback();
269  $db->commit();
270  } else {
271  $db->rollback();
272  }
273  }
274 
275  $action = '';
276 }
277 
278 
279 /*
280  * View
281  */
282 
283 $form = new Form($db);
284 
285 llxHeader('', $langs->trans("MassBarcodeInit"));
286 
287 print load_fiche_titre($langs->trans("MassBarcodeInit"), '', 'title_setup.png');
288 print '<br>';
289 
290 print '<span class="opacitymedium">'.$langs->trans("MassBarcodeInitDesc").'</span><br>';
291 print '<br>';
292 
293 //print img_picto('','puce').' '.$langs->trans("PrintsheetForOneBarCode").'<br>';
294 //print '<br>';
295 
296 print '<br>';
297 
298 
299 
300 // Example 1 : Adding jquery code
301 print '<script type="text/javascript">
302 function confirm_erase() {
303  return confirm("'.dol_escape_js($langs->trans("ConfirmEraseAllCurrentBarCode")).'");
304 }
305 </script>';
306 
307 
308 // For thirdparty
309 if (isModEnabled('societe')) {
310  print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
311  print '<input type="hidden" name="mode" value="label">';
312  print '<input type="hidden" name="action" value="initbarcodethirdparties">';
313  print '<input type="hidden" name="token" value="'.newToken().'">';
314  $nbthirdpartyno = $nbthirdpartytotal = 0;
315 
316  print load_fiche_titre($langs->trans("BarcodeInitForThirdparties"), '', 'company');
317 
318  print '<br>'."\n";
319  $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."societe where barcode IS NULL or barcode = ''";
320  $resql = $db->query($sql);
321  if ($resql) {
322  $obj = $db->fetch_object($resql);
323  $nbthirdpartyno = $obj->nb;
324  } else {
325  dol_print_error($db);
326  }
327 
328  $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."societe";
329  $sql .= " WHERE entity IN (".getEntity('societe').")";
330  $resql = $db->query($sql);
331  if ($resql) {
332  $obj = $db->fetch_object($resql);
333  $nbthirdpartytotal = $obj->nb;
334  } else {
335  dol_print_error($db);
336  }
337 
338  print $langs->trans("CurrentlyNWithoutBarCode", $nbthirdpartyno, $nbthirdpartytotal, $langs->transnoentitiesnoconv("ThirdParties"))."\n";
339 
340  $disabledthirdparty = $disabledthirdparty1 = 0;
341 
342  if (is_object($modBarCodeThirdparty)) {
343  print '<br>'.$langs->trans("BarCodeNumberManager").": ";
344  $objthirdparty = new Societe($db);
345  print '<b>'.(isset($modBarCodeThirdparty->name) ? $modBarCodeThirdparty->name : $modBarCodeThirdparty->nom).'</b> - '.$langs->trans("NextValue").': <b>'.$modBarCodeThirdparty->getNextValue($objthirdparty).'</b><br>';
346  $disabledthirdparty = 0;
347  print '<br>';
348  } else {
349  $disabledthirdparty = 1;
350  $titleno = $langs->trans("NoBarcodeNumberingTemplateDefined");
351  print '<div class="warning">'.$langs->trans("NoBarcodeNumberingTemplateDefined");
352  print '<br><a href="'.DOL_URL_ROOT.'/admin/barcode.php">'.$langs->trans("ToGenerateCodeDefineAutomaticRuleFirst").'</a>';
353  print '</div>';
354  }
355  if (empty($nbthirdpartyno)) {
356  $disabledthirdparty1 = 1;
357  }
358 
359  $moretagsthirdparty1 = (($disabledthirdparty || $disabledthirdparty1) ? ' disabled title="'.dol_escape_htmltag($titleno).'"' : '');
360  print '<br><input class="button button-add" type="submit" id="submitformbarcodethirdpartygen" value="'.$langs->trans("InitEmptyBarCode", $nbthirdpartyno).'"'.$moretagsthirdparty1.'>';
361  $moretagsthirdparty2 = (($nbthirdpartyno == $nbthirdpartytotal) ? ' disabled' : '');
362  print ' &nbsp; ';
363  print '<input type="submit" class="button butActionDelete" name="eraseallthirdpartybarcode" id="eraseallthirdpartybarcode" value="'.$langs->trans("EraseAllCurrentBarCode").'"'.$moretagsthirdparty2.' onClick="return confirm_erase();">';
364  print '<br><br><br><br>';
365  print '</form>';
366 }
367 
368 
369 // For products
370 if (isModEnabled('product') || isModEnabled('service')) {
371  print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
372  print '<input type="hidden" name="mode" value="label">';
373  print '<input type="hidden" name="action" value="initbarcodeproducts">';
374  print '<input type="hidden" name="token" value="'.newToken().'">';
375 
376  $nbproductno = $nbproducttotal = 0;
377 
378  print load_fiche_titre($langs->trans("BarcodeInitForProductsOrServices"), '', 'product');
379  print '<br>'."\n";
380 
381  $sql = "SELECT count(rowid) as nb, fk_product_type, datec";
382  $sql .= " FROM ".MAIN_DB_PREFIX."product";
383  $sql .= " WHERE barcode IS NULL OR barcode = ''";
384  $sql .= " AND entity IN (".getEntity('product').")";
385  $sql .= " GROUP BY fk_product_type, datec";
386  $sql .= " ORDER BY datec";
387  $resql = $db->query($sql);
388  if ($resql) {
389  $num = $db->num_rows($resql);
390 
391  $i = 0;
392  while ($i < $num) {
393  $obj = $db->fetch_object($resql);
394  $nbproductno += $obj->nb;
395 
396  $i++;
397  }
398  } else {
399  dol_print_error($db);
400  }
401 
402  $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."product";
403  $sql .= " WHERE entity IN (".getEntity('product').")";
404  $resql = $db->query($sql);
405  if ($resql) {
406  $obj = $db->fetch_object($resql);
407  $nbproducttotal = $obj->nb;
408  } else {
409  dol_print_error($db);
410  }
411 
412  print $langs->trans("CurrentlyNWithoutBarCode", $nbproductno, $nbproducttotal, $langs->transnoentitiesnoconv("ProductsOrServices"))."\n";
413 
414  $disabledproduct = $disabledproduct1 = 0;
415 
416  if (is_object($modBarCodeProduct)) {
417  print '<br>'.$langs->trans("BarCodeNumberManager").": ";
418  $objproduct = new Product($db);
419  print '<b>'.(isset($modBarCodeProduct->name) ? $modBarCodeProduct->name : $modBarCodeProduct->nom).'</b> - '.$langs->trans("NextValue").': <b>'.$modBarCodeProduct->getNextValue($objproduct).'</b><br>';
420  $disabledproduct = 0;
421  print '<br>';
422  } else {
423  $disabledproduct = 1;
424  $titleno = $langs->trans("NoBarcodeNumberingTemplateDefined");
425  print '<br><div class="warning">'.$langs->trans("NoBarcodeNumberingTemplateDefined");
426  print '<br><a href="'.DOL_URL_ROOT.'/admin/barcode.php">'.$langs->trans("ToGenerateCodeDefineAutomaticRuleFirst").'</a>';
427  print '</div>';
428  }
429  if (empty($nbproductno)) {
430  $disabledproduct1 = 1;
431  }
432 
433  //print '<input type="checkbox" id="erasealreadyset" name="erasealreadyset"> '.$langs->trans("ResetBarcodeForAllRecords").'<br>';
434  $moretagsproduct1 = (($disabledproduct || $disabledproduct1) ? ' disabled title="'.dol_escape_htmltag($titleno).'"' : '');
435  print '<input type="submit" class="button" name="submitformbarcodeproductgen" id="submitformbarcodeproductgen" value="'.$langs->trans("InitEmptyBarCode", min($maxperinit, $nbproductno)).'"'.$moretagsproduct1.'>';
436  $moretagsproduct2 = (($nbproductno == $nbproducttotal) ? ' disabled' : '');
437  print ' &nbsp; ';
438  print '<input type="submit" class="button butActionDelete" name="eraseallproductbarcode" id="eraseallproductbarcode" value="'.$langs->trans("EraseAllCurrentBarCode").'"'.$moretagsproduct2.' onClick="return confirm_erase();">';
439  print '<br><br><br><br>';
440  print '</form>';
441 }
442 
443 
444 print load_fiche_titre($langs->trans("BarCodePrintsheet"), '', 'generic');
445 print '<br>'."\n";
446 print $langs->trans("ClickHereToGoTo").' : <a href="'.DOL_URL_ROOT.'/barcode/printsheet.php">'.$langs->trans("BarCodePrintsheet").'</a>';
447 
448 
449 
450 print '<br>';
451 
452 // End of page
453 llxFooter();
454 $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:56
llxFooter()
Empty footer.
Definition: wrapper.php:70
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...)
if(isModEnabled('facture') && $user->hasRight('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') && $user->hasRight('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)) $sql
Social contributions to pay.
Definition: index.php:746
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
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).
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
dol_now($mode='auto')
Return date for now.
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.
isModEnabled($module)
Is Dolibarr module enabled.
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.