dolibarr  7.0.0-beta
codeinit.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2014-2015 Laurent Destailleur <eldy@users.sourceforge.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
23 require '../main.inc.php';
24 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
25 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
26 
27 $langs->load("admin");
28 $langs->load("members");
29 $langs->load("errors");
30 $langs->load("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 {
58  $dirbarcodenum=array_merge(array('/core/modules/barcode/'),$conf->modules_parts['barcode']);
59 
60  foreach ($dirbarcodenum as $dirroot)
61  {
62  $dir = dol_buildpath($dirroot,0);
63 
64  $handle = @opendir($dir);
65  if (is_resource($handle))
66  {
67  while (($file = readdir($handle))!==false)
68  {
69  if (preg_match('/^mod_barcode_product_.*php$/', $file))
70  {
71  $file = substr($file, 0, dol_strlen($file)-4);
72 
73  try {
74  dol_include_once($dirroot.$file.'.php');
75  }
76  catch(Exception $e)
77  {
78  dol_syslog($e->getMessage(), LOG_ERR);
79  }
80 
81  $modBarCodeProduct = new $file();
82  break;
83  }
84  }
85  closedir($handle);
86  }
87  }
88 }
89 
90 if ($action == 'initbarcodeproducts')
91 {
92  if (! is_object($modBarCodeProduct))
93  {
94  $error++;
95  setEventMessages($langs->trans("NoBarcodeNumberingTemplateDefined"), null, 'errors');
96  }
97 
98  if (! $error)
99  {
100  $productstatic=new Product($db);
101 
102  $db->begin();
103 
104  $nbok=0;
105  if (! empty($eraseallbarcode))
106  {
107  $sql ="UPDATE ".MAIN_DB_PREFIX."product";
108  $sql.=" SET barcode = NULL";
109  $resql=$db->query($sql);
110  if ($resql)
111  {
112  setEventMessages($langs->trans("AllBarcodeReset"), null, 'mesgs');
113  }
114  else
115  {
116  $error++;
117  dol_print_error($db);
118  }
119  }
120  else
121  {
122  $sql ="SELECT rowid, ref, fk_product_type";
123  $sql.=" FROM ".MAIN_DB_PREFIX."product";
124  $sql.=" WHERE barcode IS NULL or barcode = ''";
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  {
132  $num=$db->num_rows($resql);
133 
134  $i=0; $nbok=$nbtry=0;
135  while ($i < min($num,$maxperinit))
136  {
137  $obj=$db->fetch_object($resql);
138  if ($obj)
139  {
140  $productstatic->id=$obj->rowid;
141  $productstatic->ref=$obj->ref;
142  $productstatic->type=$obj->fk_product_type;
143  $nextvalue=$modBarCodeProduct->getNextValue($productstatic,'');
144 
145  //print 'Set value '.$nextvalue.' to product '.$productstatic->id." ".$productstatic->ref." ".$productstatic->type."<br>\n";
146  $result=$productstatic->setValueFrom('barcode', $nextvalue, '', '', 'date', '', $user, 'PRODUCT_MODIFY');
147 
148  $nbtry++;
149  if ($result > 0) $nbok++;
150  }
151 
152  $i++;
153  }
154  }
155  else
156  {
157  $error++;
158  dol_print_error($db);
159  }
160 
161  if (! $error)
162  {
163  setEventMessages($langs->trans("RecordsModified",$nbok), null, 'mesgs');
164  }
165  }
166 
167  if (! $error)
168  {
169  //$db->rollback();
170  $db->commit();
171  }
172  else
173  {
174  $db->rollback();
175  }
176  }
177 
178  $action='';
179 }
180 
181 
182 
183 /*
184  * View
185  */
186 
187 if (!$user->admin) accessforbidden();
188 if (empty($conf->barcode->enabled)) accessforbidden();
189 
190 $form=new Form($db);
191 
192 llxHeader('',$langs->trans("MassBarcodeInit"));
193 
194 print load_fiche_titre($langs->trans("MassBarcodeInit"), '', 'title_setup.png');
195 print '<br>';
196 
197 print $langs->trans("MassBarcodeInitDesc").'<br>';
198 print '<br>';
199 
200 //print img_picto('','puce').' '.$langs->trans("PrintsheetForOneBarCode").'<br>';
201 //print '<br>';
202 
203 print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
204 print '<input type="hidden" name="mode" value="label">';
205 print '<input type="hidden" name="action" value="initbarcodeproducts">';
206 
207 print '<br>';
208 
209 // For thirdparty
210 if ($conf->societe->enabled)
211 {
212  $nbno=$nbtotal=0;
213 
214  print load_fiche_titre($langs->trans("BarcodeInitForThirdparties"),'','object_company');
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"),'','object_product');
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 llxFooter();
323 
324 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:58
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
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...
if(empty($reshook)) $form
View.
Definition: perms.php:103
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...
GETPOST($paramname, $check='none', $method=0, $filter=NULL, $options=NULL, $noreplace=0)
Return value of a param into GET or POST supervariable.
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...)
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id=0, $morecssontable='', $morehtmlcenter='')
Load a title with picto.
llxHeader()
Empty header.
Definition: wrapper.php:46
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).
print
Draft customers invoices.
Definition: index.php:91
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:1013
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Return a prefix to use for this Dolibarr instance, for session/cookie names or email id...
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.