dolibarr  7.0.0-beta
mod_barcode_product_standard.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2006-2014 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2007-2012 Regis Houssin <regis.houssin@capnetworks.com>
5  * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  * or see http://www.gnu.org/
20  */
21 
28 require_once DOL_DOCUMENT_ROOT.'/core/modules/barcode/modules_barcode.class.php';
29 
30 
35 {
36  var $name='Standard'; // Model Name
37  var $code_modifiable; // Editable code
38  var $code_modifiable_invalide; // Modified code if it is invalid
39  var $code_modifiable_null; // Modified code if it is null
40  var $code_null; // Optional code
41  var $version='dolibarr'; // 'development', 'experimental', 'dolibarr'
42  var $code_auto; // Automatic Numbering
43 
44  var $searchcode; // Search string
45  var $numbitcounter; // Number of digits the counter
46  var $prefixIsRequired; // The prefix field of third party must be filled when using {pre}
47 
48 
52  function __construct()
53  {
54  $this->code_null = 0;
55  $this->code_modifiable = 1;
56  $this->code_modifiable_invalide = 1;
57  $this->code_modifiable_null = 1;
58  $this->code_auto = 1;
59  $this->prefixIsRequired = 0;
60  }
61 
62 
68  function info($langs)
69  {
70  global $conf, $mc;
71  global $form;
72 
73  $langs->load("products");
74 
75  $disabled = ((! empty($mc->sharings['referent']) && $mc->sharings['referent'] != $conf->entity) ? ' disabled' : '');
76 
77  $texte = $langs->trans('GenericNumRefModelDesc')."<br>\n";
78  $texte.= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
79  $texte.= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
80  $texte.= '<input type="hidden" name="action" value="setModuleOptions">';
81  $texte.= '<input type="hidden" name="param1" value="BARCODE_STANDARD_PRODUCT_MASK">';
82  $texte.= '<table class="nobordernopadding" width="100%">';
83 
84  $tooltip=$langs->trans("GenericMaskCodes",$langs->transnoentities("BarCode"),$langs->transnoentities("BarCode"));
85  $tooltip.=$langs->trans("GenericMaskCodes3");
86  $tooltip.=$langs->trans("GenericMaskCodes4c");
87  $tooltip.=$langs->trans("GenericMaskCodes5");
88 
89  // Mask parameter
90  //$texte.= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("BarCodeModel").'):</td>';
91  $texte.= '<tr><td>'.$langs->trans("Mask").':</td>';
92  $texte.= '<td align="right">'.$form->textwithpicto('<input type="text" class="flat" size="24" name="value1" value="'.(! empty($conf->global->BARCODE_STANDARD_PRODUCT_MASK)?$conf->global->BARCODE_STANDARD_PRODUCT_MASK:'').'"'.$disabled.'>',$tooltip,1,1).'</td>';
93  $texte.= '<td align="left" rowspan="2">&nbsp; <input type="submit" class="button" value="'.$langs->trans("Modify").'" name="Button"'.$disabled.'></td>';
94  $texte.= '</tr>';
95 
96  $texte.= '</table>';
97  $texte.= '</form>';
98 
99  return $texte;
100  }
101 
102 
110  function getExample($langs,$objproduct=0)
111  {
112  $examplebarcode = $this->getNextValue($objproduct,'');
113  if (! $examplebarcode)
114  {
115  $examplebarcode = $langs->trans('NotConfigured');
116  }
117  if($examplebarcode=="ErrorBadMask")
118  {
119  $langs->load("errors");
120  $examplebarcode=$langs->trans($examplebarcode);
121  }
122 
123  return $examplebarcode;
124  }
125 
133  function getNextValue($objproduct=null,$type='')
134  {
135  global $db,$conf;
136 
137  require_once DOL_DOCUMENT_ROOT .'/core/lib/functions2.lib.php';
138 
139  // TODO
140 
141  // Get Mask value
142  $mask = '';
143  if (! empty($conf->global->BARCODE_STANDARD_PRODUCT_MASK)) $mask = $conf->global->BARCODE_STANDARD_PRODUCT_MASK;
144 
145  if (empty($mask))
146  {
147  $this->error='NotConfigured';
148  return '';
149  }
150 
151  $field='barcode';$where='';
152 
153  $now=dol_now();
154 
155  $numFinal=get_next_value($db,$mask,'product',$field,$where,'',$now);
156 
157  return $numFinal;
158  }
159 
160 
175  function verif($db, &$code, $product, $thirdparty_type, $type)
176  {
177  global $conf;
178 
179  //var_dump($code.' '.$product->ref.' '.$thirdparty_type);exit;
180 
181  require_once DOL_DOCUMENT_ROOT .'/core/lib/functions2.lib.php';
182 
183  $result=0;
184  $code = strtoupper(trim($code));
185 
186  if (empty($code) && $this->code_null && empty($conf->global->BARCODE_STANDARD_PRODUCT_MASK))
187  {
188  $result=0;
189  }
190  else if (empty($code) && (! $this->code_null || ! empty($conf->global->BARCODE_STANDARD_PRODUCT_MASK)) )
191  {
192  $result=-2;
193  }
194  else
195  {
196  if ($this->verif_syntax($code, $type) >= 0)
197  {
198  $is_dispo = $this->verif_dispo($db, $code, $product);
199  if ($is_dispo <> 0)
200  {
201  $result=-3;
202  }
203  else
204  {
205  $result=0;
206  }
207  }
208  else
209  {
210  if (dol_strlen($code) == 0)
211  {
212  $result=-2;
213  }
214  else
215  {
216  $result=-1;
217  }
218  }
219  }
220 
221  dol_syslog(get_class($this)."::verif type=".$thirdparty_type." result=".$result);
222  return $result;
223  }
224 
225 
234  function verif_dispo($db, $code, $product)
235  {
236  $sql = "SELECT barcode FROM ".MAIN_DB_PREFIX."product";
237  $sql.= " WHERE barcode = '".$code."'";
238  if ($product->id > 0) $sql.= " AND rowid <> ".$product->id;
239 
240  $resql=$db->query($sql);
241  if ($resql)
242  {
243  if ($db->num_rows($resql) == 0)
244  {
245  return 0;
246  }
247  else
248  {
249  return -1;
250  }
251  }
252  else
253  {
254  return -2;
255  }
256 
257  }
258 
266  function verif_syntax($codefortest, $typefortest)
267  {
268  global $conf;
269 
270  $result = 0;
271 
272  // Get Mask value
273  $mask = empty($conf->global->BARCODE_STANDARD_PRODUCT_MASK)?'':$conf->global->BARCODE_STANDARD_PRODUCT_MASK;
274  if (! $mask)
275  {
276  $this->error='NotConfigured';
277  return -1;
278  }
279 
280  dol_syslog(get_class($this).'::verif_syntax codefortest='.$codefortest." typefortest=".$typefortest);
281 
282  $newcodefortest=$codefortest;
283 
284  // Special case, if mask is on 12 digits instead of 13, we remove last char into code to test
285  if (in_array($typefortest,array('EAN13','ISBN'))) // We remove the CRC char not included into mask
286  {
287  if (preg_match('/\{(0+)([@\+][0-9]+)?([@\+][0-9]+)?\}/i',$mask,$reg))
288  {
289  if (strlen($reg[1]) == 12) $newcodefortest=substr($newcodefortest,0,12);
290  dol_syslog(get_class($this).'::verif_syntax newcodefortest='.$newcodefortest);
291  }
292  }
293 
294  $result=check_value($mask,$newcodefortest);
295  if (is_string($result))
296  {
297  $this->error = $result;
298  return -1;
299  }
300 
301  return $result;
302  }
303 
304 }
305 
Parent class for barcode numbering models.
getExample($langs, $objproduct=0)
Return an example of result returned by getNextValue.
if(empty($reshook)) $form
View.
Definition: perms.php:103
check_value($mask, $value)
Check value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
verif_syntax($codefortest, $typefortest)
Return if a barcode value match syntax.
Class to manage barcode with standard rule.
verif_dispo($db, $code, $product)
Return if a code is used (by other element)
info($langs)
Return description of module.
verif($db, &$code, $product, $thirdparty_type, $type)
Check validity of code according to its rules.
dol_now($mode='gmt')
Return date for now.
get_next_value($db, $mask, $table, $field, $where='', $objsoc='', $date='', $mode='next', $bentityon=true, $objuser=null)
Return last or next value for a mask (according to area we should not reset)
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
getNextValue($objproduct=null, $type='')
Return next value.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.