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