dolibarr  18.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 <https://www.gnu.org/licenses/>.
19  * or see https://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 
55  public $code_auto;
56 
57  public $searchcode; // Search string
58 
59  public $numbitcounter; // Number of digits the counter
60 
61  public $prefixIsRequired; // The prefix field of third party must be filled when using {pre}
62 
63 
67  public function __construct()
68  {
69  $this->code_null = 0;
70  $this->code_modifiable = 1;
71  $this->code_modifiable_invalide = 1;
72  $this->code_modifiable_null = 1;
73  $this->code_auto = 1;
74  $this->prefixIsRequired = 0;
75  }
76 
77 
83  public function info($langs)
84  {
85  global $conf, $mc;
86  global $form;
87 
88  $langs->load("products");
89 
90  $disabled = ((!empty($mc->sharings['referent']) && $mc->sharings['referent'] != $conf->entity) ? ' disabled' : '');
91 
92  $texte = $langs->trans('GenericNumRefModelDesc')."<br>\n";
93  $texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
94  $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
95  $texte .= '<input type="hidden" name="page_y" value="">';
96  $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
97  $texte .= '<input type="hidden" name="param1" value="BARCODE_STANDARD_PRODUCT_MASK">';
98  $texte .= '<table class="nobordernopadding" width="100%">';
99 
100  $tooltip = $langs->trans("GenericMaskCodes", $langs->transnoentities("BarCode"), $langs->transnoentities("BarCode"));
101  $tooltip .= $langs->trans("GenericMaskCodes3EAN");
102  $tooltip .= '<strong>'.$langs->trans("Example").':</strong><br>';
103  $tooltip .= '04{0000000000}? (for internal use)<br>';
104  $tooltip .= '9771234{00000}? (example of ISSN code with prefix 1234)<br>';
105  $tooltip .= '9791234{00000}? (example of ISMN code with prefix 1234)<br>';
106  //$tooltip.=$langs->trans("GenericMaskCodes5");
107 
108  // Mask parameter
109  //$texte.= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("BarCodeModel").'):</td>';
110  $texte .= '<tr><td>'.$langs->trans("Mask").':</td>';
111  $texte .= '<td class="right">'.$form->textwithpicto('<input type="text" class="flat minwidth175" name="value1" value="'.(!empty($conf->global->BARCODE_STANDARD_PRODUCT_MASK) ? $conf->global->BARCODE_STANDARD_PRODUCT_MASK : '').'"'.$disabled.'>', $tooltip, 1, 1).'</td>';
112  $texte .= '<td class="left" rowspan="2">&nbsp; <input type="submit" class="button button-edit reposition small" name="modify" value="'.$langs->trans("Modify").'"'.$disabled.'></td>';
113  $texte .= '</tr>';
114 
115  $texte .= '</table>';
116  $texte .= '</form>';
117 
118  return $texte;
119  }
120 
121 
129  public function getExample($langs, $objproduct = 0)
130  {
131  $examplebarcode = $this->getNextValue($objproduct, '');
132  if (!$examplebarcode) {
133  $examplebarcode = $langs->trans('NotConfigured');
134  }
135  if ($examplebarcode == "ErrorBadMask") {
136  $langs->load("errors");
137  $examplebarcode = $langs->trans($examplebarcode);
138  }
139 
140  return $examplebarcode;
141  }
149  public function literalBarcodeType($db, $type = '')
150  {
151  global $conf;
152  $out = '';
153 
154  $sql = "SELECT rowid, code, libelle as label";
155  $sql .= " FROM ".MAIN_DB_PREFIX."c_barcode_type";
156  $sql .= " WHERE rowid = '".$db->escape($type)."'";
157  $sql .= " AND entity = ".((int) $conf->entity);
158  $result = $db->query($sql);
159  if ($result) {
160  $num = $db->num_rows($result);
161 
162  if ($num > 0) {
163  $obj = $db->fetch_object($result);
164  $out .= $obj->label; //take the label corresponding to the type rowid in the database
165  }
166  } else {
167  dol_print_error($db);
168  }
169 
170  return $out;
171  }
179  public function getNextValue($objproduct, $type = '')
180  {
181  global $db, $conf;
182 
183  require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
184  require_once DOL_DOCUMENT_ROOT.'/core/lib/barcode.lib.php'; // to be able to call function barcode_gen_ean_sum($ean)
185 
186  if (empty($type)) {
187  $type = $conf->global->PRODUIT_DEFAULT_BARCODE_TYPE;
188  } //get barcode type configuration for products if $type not set
189 
190  // TODO
191 
192  // Get Mask value
193  $mask = '';
194  if (!empty($conf->global->BARCODE_STANDARD_PRODUCT_MASK)) {
195  $mask = $conf->global->BARCODE_STANDARD_PRODUCT_MASK;
196  }
197 
198  if (empty($mask)) {
199  $this->error = 'NotConfigured';
200  return '';
201  }
202 
203  $field = 'barcode';
204  $where = '';
205 
206  $now = dol_now();
207 
208  $numFinal = get_next_value($db, $mask, 'product', $field, $where, '', $now);
209  //Begin barcode with key: for barcode with key (EAN13...) calculate and substitute the last character (* or ?) used in the mask by the key
210  if ((substr($numFinal, -1)=='*') or (substr($numFinal, -1)=='?')) { // if last mask character is * or ? a joker, probably we have to calculate a key as last character (EAN13...)
211  $literaltype = '';
212  $literaltype = $this->literalBarcodeType($db, $type);//get literal_Barcode_Type
213  switch ($literaltype) {
214  case 'EAN13': //EAN13 rowid = 2
215  if (strlen($numFinal)==13) {// be sure that the mask length is correct for EAN13
216  $ean = substr($numFinal, 0, 12); //take first 12 digits
217  $eansum = barcode_gen_ean_sum($ean);
218  $ean .= $eansum; //substitute the las character by the key
219  $numFinal = $ean;
220  }
221  break;
222  // Other barcode cases with key could be written here
223  default:
224  break;
225  }
226  }
227  //End barcode with key
228  return $numFinal;
229  }
230 
231 
246  public function verif($db, &$code, $product, $thirdparty_type, $type)
247  {
248  global $conf;
249 
250  //var_dump($code.' '.$product->ref.' '.$thirdparty_type);exit;
251 
252  require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
253 
254  $result = 0;
255  $code = strtoupper(trim($code));
256 
257  if (empty($code) && $this->code_null && empty($conf->global->BARCODE_STANDARD_PRODUCT_MASK)) {
258  $result = 0;
259  } elseif (empty($code) && (!$this->code_null || !empty($conf->global->BARCODE_STANDARD_PRODUCT_MASK))) {
260  $result = -2;
261  } else {
262  if ($this->verif_syntax($code, $type) >= 0) {
263  $is_dispo = $this->verif_dispo($db, $code, $product);
264  if ($is_dispo <> 0) {
265  $result = -3;
266  } else {
267  $result = 0;
268  }
269  } else {
270  if (dol_strlen($code) == 0) {
271  $result = -2;
272  } else {
273  $result = -1;
274  }
275  }
276  }
277 
278  dol_syslog(get_class($this)."::verif type=".$thirdparty_type." result=".$result);
279  return $result;
280  }
281 
282 
283  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
292  public function verif_dispo($db, $code, $product)
293  {
294  // phpcs:enable
295  $sql = "SELECT barcode FROM ".MAIN_DB_PREFIX."product";
296  $sql .= " WHERE barcode = '".$db->escape($code)."'";
297  if ($product->id > 0) {
298  $sql .= " AND rowid <> ".$product->id;
299  }
300 
301  $resql = $db->query($sql);
302  if ($resql) {
303  if ($db->num_rows($resql) == 0) {
304  return 0;
305  } else {
306  return -1;
307  }
308  } else {
309  return -2;
310  }
311  }
312 
313  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
321  public function verif_syntax($codefortest, $typefortest)
322  {
323  // phpcs:enable
324  global $conf;
325 
326  $result = 0;
327 
328  // Get Mask value
329  $mask = empty($conf->global->BARCODE_STANDARD_PRODUCT_MASK) ? '' : $conf->global->BARCODE_STANDARD_PRODUCT_MASK;
330  if (!$mask) {
331  $this->error = 'NotConfigured';
332  return -1;
333  }
334 
335  dol_syslog(get_class($this).'::verif_syntax codefortest='.$codefortest." typefortest=".$typefortest);
336 
337  $newcodefortest = $codefortest;
338 
339  // Special case, if mask is on 12 digits instead of 13, we remove last char into code to test
340  if (in_array($typefortest, array('EAN13', 'ISBN'))) { // We remove the CRC char not included into mask
341  if (preg_match('/\{(0+)([@\+][0-9]+)?([@\+][0-9]+)?\}/i', $mask, $reg)) {
342  if (strlen($reg[1]) == 12) {
343  $newcodefortest = substr($newcodefortest, 0, 12);
344  }
345  dol_syslog(get_class($this).'::verif_syntax newcodefortest='.$newcodefortest);
346  }
347  }
348 
349  $result = check_value($mask, $newcodefortest);
350  if (is_string($result)) {
351  $this->error = $result;
352  return -1;
353  }
354 
355  return $result;
356  }
357 }
mod_barcode_product_standard\getExample
getExample($langs, $objproduct=0)
Return an example of result returned by getNextValue.
Definition: mod_barcode_product_standard.php:129
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:5107
mod_barcode_product_standard\verif_dispo
verif_dispo($db, $code, $product)
Return if a code is used (by other element)
Definition: mod_barcode_product_standard.php:292
mod_barcode_product_standard
Class to manage barcode with standard rule.
Definition: mod_barcode_product_standard.php:34
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
ModeleNumRefBarCode
Parent class for barcode numbering models.
Definition: modules_barcode.class.php:53
mod_barcode_product_standard\__construct
__construct()
Constructor.
Definition: mod_barcode_product_standard.php:67
mod_barcode_product_standard\verif_syntax
verif_syntax($codefortest, $typefortest)
Return if a barcode value match syntax.
Definition: mod_barcode_product_standard.php:321
mod_barcode_product_standard\getNextValue
getNextValue($objproduct, $type='')
Return next value.
Definition: mod_barcode_product_standard.php:179
mod_barcode_product_standard\info
info($langs)
Return description of module.
Definition: mod_barcode_product_standard.php:83
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1741
mod_barcode_product_standard\literalBarcodeType
literalBarcodeType($db, $type='')
Return literal barcode type code from numerical rowid type of barcode.
Definition: mod_barcode_product_standard.php:149
$sql
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3997
get_next_value
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)
Definition: functions2.lib.php:970
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:3056
check_value
check_value($mask, $value)
Check value.
Definition: functions2.lib.php:1520
barcode_gen_ean_sum
barcode_gen_ean_sum($ean)
Calculate EAN sum.
Definition: barcode.lib.php:175
mod_barcode_product_standard\verif
verif($db, &$code, $product, $thirdparty_type, $type)
Check validity of code according to its rules.
Definition: mod_barcode_product_standard.php:246