dolibarr  16.0.5
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 require '../main.inc.php';
26 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
27 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
28 
29 // Load translation files required by the page
30 $langs->loadLangs(array('admin', 'members', 'errors', 'other'));
31 
32 // Choice of print year or current year.
33 $now = dol_now();
34 $year = dol_print_date($now, '%Y');
35 $month = dol_print_date($now, '%m');
36 $day = dol_print_date($now, '%d');
37 $forbarcode = GETPOST('forbarcode');
38 $fk_barcode_type = GETPOST('fk_barcode_type');
39 $eraseallbarcode = GETPOST('eraseallbarcode');
40 
41 $action = GETPOST('action', 'aZ09');
42 
43 $producttmp = new Product($db);
44 $thirdpartytmp = new Societe($db);
45 
46 $modBarCodeProduct = '';
47 
48 $maxperinit = empty($conf->global->BARCODE_INIT_MAX) ? 1000 : $conf->global->BARCODE_INIT_MAX;
49 
50 
51 /*
52  * Actions
53  */
54 
55 // Define barcode template for products
56 if (!empty($conf->global->BARCODE_PRODUCT_ADDON_NUM)) {
57  $dirbarcodenum = array_merge(array('/core/modules/barcode/'), $conf->modules_parts['barcode']);
58 
59  foreach ($dirbarcodenum as $dirroot) {
60  $dir = dol_buildpath($dirroot, 0);
61 
62  $handle = @opendir($dir);
63  if (is_resource($handle)) {
64  while (($file = readdir($handle)) !== false) {
65  if (preg_match('/^mod_barcode_product_.*php$/', $file)) {
66  $file = substr($file, 0, dol_strlen($file) - 4);
67 
68  if ($file == $conf->global->BARCODE_PRODUCT_ADDON_NUM) {
69  try {
70  dol_include_once($dirroot.$file.'.php');
71  } catch (Exception $e) {
72  dol_syslog($e->getMessage(), LOG_ERR);
73  }
74 
75  $modBarCodeProduct = new $file();
76  break;
77  }
78  }
79  }
80  closedir($handle);
81  }
82  }
83 }
84 
85 if ($action == 'initbarcodeproducts') {
86  if (!is_object($modBarCodeProduct)) {
87  $error++;
88  setEventMessages($langs->trans("NoBarcodeNumberingTemplateDefined"), null, 'errors');
89  }
90 
91  if (!$error) {
92  $productstatic = new Product($db);
93 
94  $db->begin();
95 
96  $nbok = 0;
97  if (!empty($eraseallbarcode)) {
98  $sql = "UPDATE ".MAIN_DB_PREFIX."product";
99  $sql .= " SET barcode = NULL";
100  $resql = $db->query($sql);
101  if ($resql) {
102  setEventMessages($langs->trans("AllBarcodeReset"), null, 'mesgs');
103  } else {
104  $error++;
105  dol_print_error($db);
106  }
107  } else {
108  $sql = "SELECT rowid, ref, fk_product_type";
109  $sql .= " FROM ".MAIN_DB_PREFIX."product";
110  $sql .= " WHERE barcode IS NULL or barcode = ''";
111  $sql .= $db->order("datec", "ASC");
112  $sql .= $db->plimit($maxperinit);
113 
114  dol_syslog("codeinit", LOG_DEBUG);
115  $resql = $db->query($sql);
116  if ($resql) {
117  $num = $db->num_rows($resql);
118 
119  $i = 0; $nbok = $nbtry = 0;
120  while ($i < min($num, $maxperinit)) {
121  $obj = $db->fetch_object($resql);
122  if ($obj) {
123  $productstatic->id = $obj->rowid;
124  $productstatic->ref = $obj->ref;
125  $productstatic->type = $obj->fk_product_type;
126  $nextvalue = $modBarCodeProduct->getNextValue($productstatic, '');
127 
128  //print 'Set value '.$nextvalue.' to product '.$productstatic->id." ".$productstatic->ref." ".$productstatic->type."<br>\n";
129  $result = $productstatic->setValueFrom('barcode', $nextvalue, '', '', 'text', '', $user, 'PRODUCT_MODIFY');
130 
131  $nbtry++;
132  if ($result > 0) {
133  $nbok++;
134  }
135  }
136 
137  $i++;
138  }
139  } else {
140  $error++;
141  dol_print_error($db);
142  }
143 
144  if (!$error) {
145  setEventMessages($langs->trans("RecordsModified", $nbok), null, 'mesgs');
146  }
147  }
148 
149  if (!$error) {
150  //$db->rollback();
151  $db->commit();
152  } else {
153  $db->rollback();
154  }
155  }
156 
157  $action = '';
158 }
159 
160 
161 
162 /*
163  * View
164  */
165 
166 if (!$user->admin) {
167  accessforbidden();
168 }
169 if (empty($conf->barcode->enabled)) {
170  accessforbidden();
171 }
172 
173 $form = new Form($db);
174 
175 llxHeader('', $langs->trans("MassBarcodeInit"));
176 
177 print load_fiche_titre($langs->trans("MassBarcodeInit"), '', 'title_setup.png');
178 print '<br>';
179 
180 print '<span class="opacitymedium">'.$langs->trans("MassBarcodeInitDesc").'</span><br>';
181 print '<br>';
182 
183 //print img_picto('','puce').' '.$langs->trans("PrintsheetForOneBarCode").'<br>';
184 //print '<br>';
185 
186 print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
187 print '<input type="hidden" name="mode" value="label">';
188 print '<input type="hidden" name="action" value="initbarcodeproducts">';
189 print '<input type="hidden" name="token" value="'.newToken().'">';
190 
191 print '<br>';
192 
193 // For thirdparty
194 if (isModEnabled('societe')) {
195  $nbno = $nbtotal = 0;
196 
197  print load_fiche_titre($langs->trans("BarcodeInitForThirdparties"), '', 'company');
198 
199  print '<br>'."\n";
200  $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."societe where barcode IS NULL or barcode = ''";
201  $resql = $db->query($sql);
202  if ($resql) {
203  $obj = $db->fetch_object($resql);
204  $nbno = $obj->nb;
205  } else {
206  dol_print_error($db);
207  }
208 
209  $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."societe";
210  $resql = $db->query($sql);
211  if ($resql) {
212  $obj = $db->fetch_object($resql);
213  $nbtotal = $obj->nb;
214  } else {
215  dol_print_error($db);
216  }
217 
218  print $langs->trans("CurrentlyNWithoutBarCode", $nbno, $nbtotal, $langs->transnoentitiesnoconv("ThirdParties")).'<br>'."\n";
219 
220  print '<br><input class="button button-add" type="submit" id="submitformbarcodethirdpartygen" '.((GETPOST("selectorforbarcode") && GETPOST("selectorforbarcode")) ? '' : 'disabled ').'value="'.$langs->trans("InitEmptyBarCode", $nbno).'"';
221  print ' title="'.dol_escape_htmltag($langs->trans("FeatureNotYetAvailable")).'" disabled';
222  print '>';
223  print '<br><br><br><br>';
224 }
225 
226 
227 // For products
228 if ($conf->product->enabled || $conf->product->service) {
229  // Example 1 : Adding jquery code
230  print '<script type="text/javascript">
231  function confirm_erase() {
232  return confirm("'.dol_escape_js($langs->trans("ConfirmEraseAllCurrentBarCode")).'");
233  }
234  </script>';
235 
236  $nbno = $nbtotal = 0;
237 
238  print load_fiche_titre($langs->trans("BarcodeInitForProductsOrServices"), '', 'product');
239  print '<br>'."\n";
240 
241  $sql = "SELECT count(rowid) as nb, fk_product_type, datec";
242  $sql .= " FROM ".MAIN_DB_PREFIX."product";
243  $sql .= " WHERE barcode IS NULL OR barcode = ''";
244  $sql .= " GROUP BY fk_product_type, datec";
245  $sql .= " ORDER BY datec";
246  $resql = $db->query($sql);
247  if ($resql) {
248  $num = $db->num_rows($resql);
249 
250  $i = 0;
251  while ($i < $num) {
252  $obj = $db->fetch_object($resql);
253  $nbno += $obj->nb;
254 
255  $i++;
256  }
257  } else {
258  dol_print_error($db);
259  }
260 
261  $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."product";
262  $resql = $db->query($sql);
263  if ($resql) {
264  $obj = $db->fetch_object($resql);
265  $nbtotal = $obj->nb;
266  } else {
267  dol_print_error($db);
268  }
269 
270  print $langs->trans("CurrentlyNWithoutBarCode", $nbno, $nbtotal, $langs->transnoentitiesnoconv("ProductsOrServices"))."\n";
271 
272  if (is_object($modBarCodeProduct)) {
273  print $langs->trans("BarCodeNumberManager").": ";
274  $objproduct = new Product($db);
275  print '<b>'.(isset($modBarCodeProduct->name) ? $modBarCodeProduct->name : $modBarCodeProduct->nom).'</b> - '.$langs->trans("NextValue").': <b>'.$modBarCodeProduct->getNextValue($objproduct).'</b><br>';
276  $disabled = 0;
277  print '<br>';
278  } else {
279  $disabled = 1;
280  $titleno = $langs->trans("NoBarcodeNumberingTemplateDefined");
281  print '<br><div class="warning">'.$langs->trans("NoBarcodeNumberingTemplateDefined");
282  print '<br><a href="'.DOL_URL_ROOT.'/admin/barcode.php">'.$langs->trans("ToGenerateCodeDefineAutomaticRuleFirst").'</a>';
283  print '</div>';
284  }
285  if (empty($nbno)) {
286  $disabled1 = 1;
287  }
288 
289  //print '<input type="checkbox" id="erasealreadyset" name="erasealreadyset"> '.$langs->trans("ResetBarcodeForAllRecords").'<br>';
290  $moretags1 = (($disabled || $disabled1) ? ' disabled title="'.dol_escape_htmltag($titleno).'"' : '');
291  print '<input type="submit" class="button" name="submitformbarcodeproductgen" id="submitformbarcodeproductgen" value="'.$langs->trans("InitEmptyBarCode", min($maxperinit, $nbno)).'"'.$moretags1.'>';
292  $moretags2 = (($nbno == $nbtotal) ? ' disabled' : '');
293  print ' &nbsp; ';
294  print '<input type="submit" class="button butActionDelete" name="eraseallbarcode" id="eraseallbarcode" value="'.$langs->trans("EraseAllCurrentBarCode").'"'.$moretags2.' onClick="return confirm_erase();">';
295  print '<br><br><br><br>';
296 }
297 
298 
299 print load_fiche_titre($langs->trans("BarCodePrintsheet"), '', 'generic');
300 print '<br>'."\n";
301 print $langs->trans("ClickHereToGoTo").' : <a href="'.DOL_URL_ROOT.'/barcode/printsheet.php">'.$langs->trans("BarCodePrintsheet").'</a>';
302 
303 
304 
305 print '</form>';
306 print '<br>';
307 
308 // End of page
309 llxFooter();
310 $db->close();
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:48
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:1468
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
load_fiche_titre
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
Definition: functions.lib.php:5204
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:484
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:4844
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:1033
dol_buildpath
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
Definition: functions.lib.php:1062
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
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:2514
Exception
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3747
isModEnabled
isModEnabled($module)
Is Dolibarr module enabled.
Definition: functions.lib.php:105
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:52
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->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->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
accessforbidden
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Definition: security.lib.php:933
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOCSRFCHECK')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:59