dolibarr  16.0.1
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 = 1000;
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  try {
69  dol_include_once($dirroot.$file.'.php');
70  } catch (Exception $e) {
71  dol_syslog($e->getMessage(), LOG_ERR);
72  }
73 
74  $modBarCodeProduct = new $file();
75  break;
76  }
77  }
78  closedir($handle);
79  }
80  }
81 }
82 
83 if ($action == 'initbarcodeproducts') {
84  if (!is_object($modBarCodeProduct)) {
85  $error++;
86  setEventMessages($langs->trans("NoBarcodeNumberingTemplateDefined"), null, 'errors');
87  }
88 
89  if (!$error) {
90  $productstatic = new Product($db);
91 
92  $db->begin();
93 
94  $nbok = 0;
95  if (!empty($eraseallbarcode)) {
96  $sql = "UPDATE ".MAIN_DB_PREFIX."product";
97  $sql .= " SET barcode = NULL";
98  $resql = $db->query($sql);
99  if ($resql) {
100  setEventMessages($langs->trans("AllBarcodeReset"), null, 'mesgs');
101  } else {
102  $error++;
103  dol_print_error($db);
104  }
105  } else {
106  $sql = "SELECT rowid, ref, fk_product_type";
107  $sql .= " FROM ".MAIN_DB_PREFIX."product";
108  $sql .= " WHERE barcode IS NULL or barcode = ''";
109  $sql .= $db->order("datec", "ASC");
110  $sql .= $db->plimit($maxperinit);
111 
112  dol_syslog("codeinit", LOG_DEBUG);
113  $resql = $db->query($sql);
114  if ($resql) {
115  $num = $db->num_rows($resql);
116 
117  $i = 0; $nbok = $nbtry = 0;
118  while ($i < min($num, $maxperinit)) {
119  $obj = $db->fetch_object($resql);
120  if ($obj) {
121  $productstatic->id = $obj->rowid;
122  $productstatic->ref = $obj->ref;
123  $productstatic->type = $obj->fk_product_type;
124  $nextvalue = $modBarCodeProduct->getNextValue($productstatic, '');
125 
126  //print 'Set value '.$nextvalue.' to product '.$productstatic->id." ".$productstatic->ref." ".$productstatic->type."<br>\n";
127  $result = $productstatic->setValueFrom('barcode', $nextvalue, '', '', 'text', '', $user, 'PRODUCT_MODIFY');
128 
129  $nbtry++;
130  if ($result > 0) {
131  $nbok++;
132  }
133  }
134 
135  $i++;
136  }
137  } else {
138  $error++;
139  dol_print_error($db);
140  }
141 
142  if (!$error) {
143  setEventMessages($langs->trans("RecordsModified", $nbok), null, 'mesgs');
144  }
145  }
146 
147  if (!$error) {
148  //$db->rollback();
149  $db->commit();
150  } else {
151  $db->rollback();
152  }
153  }
154 
155  $action = '';
156 }
157 
158 
159 
160 /*
161  * View
162  */
163 
164 if (!$user->admin) {
165  accessforbidden();
166 }
167 if (empty($conf->barcode->enabled)) {
168  accessforbidden();
169 }
170 
171 $form = new Form($db);
172 
173 llxHeader('', $langs->trans("MassBarcodeInit"));
174 
175 print load_fiche_titre($langs->trans("MassBarcodeInit"), '', 'title_setup.png');
176 print '<br>';
177 
178 print '<span class="opacitymedium">'.$langs->trans("MassBarcodeInitDesc").'</span><br>';
179 print '<br>';
180 
181 //print img_picto('','puce').' '.$langs->trans("PrintsheetForOneBarCode").'<br>';
182 //print '<br>';
183 
184 print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
185 print '<input type="hidden" name="mode" value="label">';
186 print '<input type="hidden" name="action" value="initbarcodeproducts">';
187 print '<input type="hidden" name="token" value="'.newToken().'">';
188 
189 print '<br>';
190 
191 // For thirdparty
192 if (isModEnabled('societe')) {
193  $nbno = $nbtotal = 0;
194 
195  print load_fiche_titre($langs->trans("BarcodeInitForThirdparties"), '', 'company');
196 
197  print '<br>'."\n";
198  $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."societe where barcode IS NULL or barcode = ''";
199  $resql = $db->query($sql);
200  if ($resql) {
201  $obj = $db->fetch_object($resql);
202  $nbno = $obj->nb;
203  } else {
204  dol_print_error($db);
205  }
206 
207  $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."societe";
208  $resql = $db->query($sql);
209  if ($resql) {
210  $obj = $db->fetch_object($resql);
211  $nbtotal = $obj->nb;
212  } else {
213  dol_print_error($db);
214  }
215 
216  print $langs->trans("CurrentlyNWithoutBarCode", $nbno, $nbtotal, $langs->transnoentitiesnoconv("ThirdParties")).'<br>'."\n";
217 
218  print '<br><input class="button button-add" type="submit" id="submitformbarcodethirdpartygen" '.((GETPOST("selectorforbarcode") && GETPOST("selectorforbarcode")) ? '' : 'disabled ').'value="'.$langs->trans("InitEmptyBarCode", $nbno).'"';
219  print ' title="'.dol_escape_htmltag($langs->trans("FeatureNotYetAvailable")).'" disabled';
220  print '>';
221  print '<br><br><br><br>';
222 }
223 
224 
225 // For products
226 if ($conf->product->enabled || $conf->product->service) {
227  // Example 1 : Adding jquery code
228  print '<script type="text/javascript">
229  function confirm_erase() {
230  return confirm("'.dol_escape_js($langs->trans("ConfirmEraseAllCurrentBarCode")).'");
231  }
232  </script>';
233 
234  $nbno = $nbtotal = 0;
235 
236  print load_fiche_titre($langs->trans("BarcodeInitForProductsOrServices"), '', 'product');
237  print '<br>'."\n";
238 
239  $sql = "SELECT count(rowid) as nb, fk_product_type, datec";
240  $sql .= " FROM ".MAIN_DB_PREFIX."product";
241  $sql .= " WHERE barcode IS NULL OR barcode = ''";
242  $sql .= " GROUP BY fk_product_type, datec";
243  $sql .= " ORDER BY datec";
244  $resql = $db->query($sql);
245  if ($resql) {
246  $num = $db->num_rows($resql);
247 
248  $i = 0;
249  while ($i < $num) {
250  $obj = $db->fetch_object($resql);
251  $nbno += $obj->nb;
252 
253  $i++;
254  }
255  } else {
256  dol_print_error($db);
257  }
258 
259  $sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."product";
260  $resql = $db->query($sql);
261  if ($resql) {
262  $obj = $db->fetch_object($resql);
263  $nbtotal = $obj->nb;
264  } else {
265  dol_print_error($db);
266  }
267 
268  print $langs->trans("CurrentlyNWithoutBarCode", $nbno, $nbtotal, $langs->transnoentitiesnoconv("ProductsOrServices"))."\n";
269 
270  if (is_object($modBarCodeProduct)) {
271  print $langs->trans("BarCodeNumberManager").": ";
272  $objproduct = new Product($db);
273  print '<b>'.(isset($modBarCodeProduct->name) ? $modBarCodeProduct->name : $modBarCodeProduct->nom).'</b> - '.$langs->trans("NextValue").': <b>'.$modBarCodeProduct->getNextValue($objproduct).'</b><br>';
274  $disabled = 0;
275  print '<br>';
276  } else {
277  $disabled = 1;
278  $titleno = $langs->trans("NoBarcodeNumberingTemplateDefined");
279  print '<br><div class="warning">'.$langs->trans("NoBarcodeNumberingTemplateDefined");
280  print '<br><a href="'.DOL_URL_ROOT.'/admin/barcode.php">'.$langs->trans("ToGenerateCodeDefineAutomaticRuleFirst").'</a>';
281  print '</div>';
282  }
283  if (empty($nbno)) {
284  $disabled1 = 1;
285  }
286 
287  //print '<input type="checkbox" id="erasealreadyset" name="erasealreadyset"> '.$langs->trans("ResetBarcodeForAllRecords").'<br>';
288  $moretags1 = (($disabled || $disabled1) ? ' disabled title="'.dol_escape_htmltag($titleno).'"' : '');
289  print '<input type="submit" class="button" name="submitformbarcodeproductgen" id="submitformbarcodeproductgen" value="'.$langs->trans("InitEmptyBarCode", min($maxperinit, $nbno)).'"'.$moretags1.'>';
290  $moretags2 = (($nbno == $nbtotal) ? ' disabled' : '');
291  print ' &nbsp; ';
292  print '<input type="submit" class="button butActionDelete" name="eraseallbarcode" id="eraseallbarcode" value="'.$langs->trans("EraseAllCurrentBarCode").'"'.$moretags2.' onClick="return confirm_erase();">';
293  print '<br><br><br><br>';
294 }
295 
296 
297 print load_fiche_titre($langs->trans("BarCodePrintsheet"), '', 'generic');
298 print '<br>'."\n";
299 print $langs->trans("ClickHereToGoTo").' : <a href="'.DOL_URL_ROOT.'/barcode/printsheet.php">'.$langs->trans("BarCodePrintsheet").'</a>';
300 
301 
302 
303 print '</form>';
304 print '<br>';
305 
306 // End of page
307 llxFooter();
308 $db->close();
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.
if($cancel &&!$id) if($action== 'add'&&!$cancel) if($action== 'delete') if($id) $form
Actions.
Definition: card.php:142
Class to manage products or services.
dol_now($mode= 'auto')
Return date for now.
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
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...
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
Class to manage generation of HTML components Only common components must be here.
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_strlen($string, $stringencoding= 'UTF-8')
Make a strlen call.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
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 ...
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
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_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
isModEnabled($module)
Is Dolibarr module enabled.
llxFooter()
Empty footer.
Definition: wrapper.php:73