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