dolibarr  9.0.0
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@inodbox.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  public $name='Standard'; // Model Name
37 
38  public $code_modifiable; // Editable code
39 
40  public $code_modifiable_invalide; // Modified code if it is invalid
41 
42  public $code_modifiable_null; // Modified code if it is null
43 
44  public $code_null; // Optional code
45 
50  public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
51 
52  public $code_auto; // Automatic Numbering
53 
54  public $searchcode; // Search string
55 
56  public $numbitcounter; // Number of digits the counter
57 
58  public $prefixIsRequired; // The prefix field of third party must be filled when using {pre}
59 
60 
64  function __construct()
65  {
66  $this->code_null = 0;
67  $this->code_modifiable = 1;
68  $this->code_modifiable_invalide = 1;
69  $this->code_modifiable_null = 1;
70  $this->code_auto = 1;
71  $this->prefixIsRequired = 0;
72  }
73 
74 
80  function info($langs)
81  {
82  global $conf, $mc;
83  global $form;
84 
85  $langs->load("products");
86 
87  $disabled = ((! empty($mc->sharings['referent']) && $mc->sharings['referent'] != $conf->entity) ? ' disabled' : '');
88 
89  $texte = $langs->trans('GenericNumRefModelDesc')."<br>\n";
90  $texte.= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
91  $texte.= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
92  $texte.= '<input type="hidden" name="action" value="setModuleOptions">';
93  $texte.= '<input type="hidden" name="param1" value="BARCODE_STANDARD_PRODUCT_MASK">';
94  $texte.= '<table class="nobordernopadding" width="100%">';
95 
96  $tooltip=$langs->trans("GenericMaskCodes",$langs->transnoentities("BarCode"),$langs->transnoentities("BarCode"));
97  $tooltip.=$langs->trans("GenericMaskCodes3");
98  $tooltip.=$langs->trans("GenericMaskCodes4c");
99  $tooltip.=$langs->trans("GenericMaskCodes5");
100 
101  // Mask parameter
102  //$texte.= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("BarCodeModel").'):</td>';
103  $texte.= '<tr><td>'.$langs->trans("Mask").':</td>';
104  $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>';
105  $texte.= '<td align="left" rowspan="2">&nbsp; <input type="submit" class="button" value="'.$langs->trans("Modify").'" name="Button"'.$disabled.'></td>';
106  $texte.= '</tr>';
107 
108  $texte.= '</table>';
109  $texte.= '</form>';
110 
111  return $texte;
112  }
113 
114 
122  function getExample($langs,$objproduct=0)
123  {
124  $examplebarcode = $this->getNextValue($objproduct,'');
125  if (! $examplebarcode)
126  {
127  $examplebarcode = $langs->trans('NotConfigured');
128  }
129  if($examplebarcode=="ErrorBadMask")
130  {
131  $langs->load("errors");
132  $examplebarcode=$langs->trans($examplebarcode);
133  }
134 
135  return $examplebarcode;
136  }
137 
145  function getNextValue($objproduct=null,$type='')
146  {
147  global $db,$conf;
148 
149  require_once DOL_DOCUMENT_ROOT .'/core/lib/functions2.lib.php';
150 
151  // TODO
152 
153  // Get Mask value
154  $mask = '';
155  if (! empty($conf->global->BARCODE_STANDARD_PRODUCT_MASK)) $mask = $conf->global->BARCODE_STANDARD_PRODUCT_MASK;
156 
157  if (empty($mask))
158  {
159  $this->error='NotConfigured';
160  return '';
161  }
162 
163  $field='barcode';$where='';
164 
165  $now=dol_now();
166 
167  $numFinal=get_next_value($db,$mask,'product',$field,$where,'',$now);
168 
169  return $numFinal;
170  }
171 
172 
187  function verif($db, &$code, $product, $thirdparty_type, $type)
188  {
189  global $conf;
190 
191  //var_dump($code.' '.$product->ref.' '.$thirdparty_type);exit;
192 
193  require_once DOL_DOCUMENT_ROOT .'/core/lib/functions2.lib.php';
194 
195  $result=0;
196  $code = strtoupper(trim($code));
197 
198  if (empty($code) && $this->code_null && empty($conf->global->BARCODE_STANDARD_PRODUCT_MASK))
199  {
200  $result=0;
201  }
202  else if (empty($code) && (! $this->code_null || ! empty($conf->global->BARCODE_STANDARD_PRODUCT_MASK)) )
203  {
204  $result=-2;
205  }
206  else
207  {
208  if ($this->verif_syntax($code, $type) >= 0)
209  {
210  $is_dispo = $this->verif_dispo($db, $code, $product);
211  if ($is_dispo <> 0)
212  {
213  $result=-3;
214  }
215  else
216  {
217  $result=0;
218  }
219  }
220  else
221  {
222  if (dol_strlen($code) == 0)
223  {
224  $result=-2;
225  }
226  else
227  {
228  $result=-1;
229  }
230  }
231  }
232 
233  dol_syslog(get_class($this)."::verif type=".$thirdparty_type." result=".$result);
234  return $result;
235  }
236 
237 
238  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
247  function verif_dispo($db, $code, $product)
248  {
249  // phpcs:enable
250  $sql = "SELECT barcode FROM ".MAIN_DB_PREFIX."product";
251  $sql.= " WHERE barcode = '".$code."'";
252  if ($product->id > 0) $sql.= " AND rowid <> ".$product->id;
253 
254  $resql=$db->query($sql);
255  if ($resql)
256  {
257  if ($db->num_rows($resql) == 0)
258  {
259  return 0;
260  }
261  else
262  {
263  return -1;
264  }
265  }
266  else
267  {
268  return -2;
269  }
270  }
271 
272  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
280  function verif_syntax($codefortest, $typefortest)
281  {
282  // phpcs:enable
283  global $conf;
284 
285  $result = 0;
286 
287  // Get Mask value
288  $mask = empty($conf->global->BARCODE_STANDARD_PRODUCT_MASK)?'':$conf->global->BARCODE_STANDARD_PRODUCT_MASK;
289  if (! $mask)
290  {
291  $this->error='NotConfigured';
292  return -1;
293  }
294 
295  dol_syslog(get_class($this).'::verif_syntax codefortest='.$codefortest." typefortest=".$typefortest);
296 
297  $newcodefortest=$codefortest;
298 
299  // Special case, if mask is on 12 digits instead of 13, we remove last char into code to test
300  if (in_array($typefortest,array('EAN13','ISBN'))) // We remove the CRC char not included into mask
301  {
302  if (preg_match('/\{(0+)([@\+][0-9]+)?([@\+][0-9]+)?\}/i',$mask,$reg))
303  {
304  if (strlen($reg[1]) == 12) $newcodefortest=substr($newcodefortest,0,12);
305  dol_syslog(get_class($this).'::verif_syntax newcodefortest='.$newcodefortest);
306  }
307  }
308 
309  $result=check_value($mask,$newcodefortest);
310  if (is_string($result))
311  {
312  $this->error = $result;
313  return -1;
314  }
315 
316  return $result;
317  }
318 }
Parent class for barcode numbering models.
$version
Dolibarr version of the loaded document string.
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
getExample($langs, $objproduct=0)
Return an example of result returned by getNextValue.
check_value($mask, $value)
Check value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
get_next_value($db, $mask, $table, $field, $where='', $objsoc='', $date='', $mode='next', $bentityon=true, $objuser=null, $forceentity=null)
Return last or next value for a mask (according to area we should not reset)
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.
getNextValue($objproduct=null, $type='')
Return next value.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.