dolibarr 20.0.2
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 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 * or see https://www.gnu.org/
22 */
23
30require_once DOL_DOCUMENT_ROOT.'/core/modules/barcode/modules_barcode.class.php';
31
32
37{
38 public $name = 'Standard'; // Model Name
39
44 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
45
46 public $searchcode; // Search string
47
48 public $numbitcounter; // Number of digits the counter
49
53 public function __construct()
54 {
55 $this->code_null = 0;
56 $this->code_modifiable = 1;
57 $this->code_modifiable_invalide = 1;
58 $this->code_modifiable_null = 1;
59 $this->code_auto = 1;
60 $this->prefixIsRequired = 0;
61 }
62
63
69 public function info($langs)
70 {
71 global $conf, $mc;
72 global $form;
73
74 $langs->load("products");
75
76 $disabled = ((!empty($mc->sharings['referent']) && $mc->sharings['referent'] != $conf->entity) ? ' disabled' : '');
77
78 $texte = $langs->trans('GenericNumRefModelDesc')."<br>\n";
79 $texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
80 $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
81 $texte .= '<input type="hidden" name="page_y" value="">';
82 $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
83 $texte .= '<input type="hidden" name="param1" value="BARCODE_STANDARD_PRODUCT_MASK">';
84 $texte .= '<table class="nobordernopadding" width="100%">';
85
86 $tooltip = $langs->trans("GenericMaskCodes", $langs->transnoentities("BarCode"), $langs->transnoentities("BarCode"));
87 $tooltip .= $langs->trans("GenericMaskCodes3EAN");
88 $tooltip .= '<strong>'.$langs->trans("Example").':</strong><br>';
89 $tooltip .= '04{0000000000}? (for internal use)<br>';
90 $tooltip .= '9771234{00000}? (example of ISSN code with prefix 1234)<br>';
91 $tooltip .= '9791234{00000}? (example of ISMN code with prefix 1234)<br>';
92 //$tooltip .= $langs->trans("GenericMaskCodes5");
93 //$tooltip .= '<br>'.$langs->trans("GenericMaskCodes5b");
94
95 // Mask parameter
96 //$texte.= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("BarCodeModel").'):</td>';
97 $texte .= '<tr><td>'.$langs->trans("Mask").':</td>';
98 $texte .= '<td class="right">'.$form->textwithpicto('<input type="text" class="flat minwidth175" name="value1" value="'.(getDolGlobalString('BARCODE_STANDARD_PRODUCT_MASK') ? $conf->global->BARCODE_STANDARD_PRODUCT_MASK : '').'"'.$disabled.'>', $tooltip, 1, 1).'</td>';
99 $texte .= '<td class="left" rowspan="2">&nbsp; <input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.$langs->trans("Modify").'"'.$disabled.'></td>';
100 $texte .= '</tr>';
101
102 $texte .= '</table>';
103 $texte .= '</form>';
104
105 return $texte;
106 }
107
108
116 public function getExample($langs, $objproduct = null)
117 {
118 $examplebarcode = $this->getNextValue($objproduct, '');
119 if (!$examplebarcode) {
120 $examplebarcode = $langs->trans('NotConfigured');
121 }
122 if ($examplebarcode == "ErrorBadMask") {
123 $langs->load("errors");
124 $examplebarcode = $langs->trans($examplebarcode);
125 }
126
127 return $examplebarcode;
128 }
136 public function literalBarcodeType($db, $type = 0)
137 {
138 global $conf;
139 $out = '';
140
141 $sql = "SELECT rowid, code, libelle as label";
142 $sql .= " FROM ".MAIN_DB_PREFIX."c_barcode_type";
143 $sql .= " WHERE rowid = ".(int) $type;
144 $sql .= " AND entity = ".((int) $conf->entity);
145 $result = $db->query($sql);
146 if ($result) {
147 $num = $db->num_rows($result);
148
149 if ($num > 0) {
150 $obj = $db->fetch_object($result);
151 $out .= $obj->label; //take the label corresponding to the type rowid in the database
152 }
153 } else {
154 dol_print_error($db);
155 }
156
157 return $out;
158 }
159
167 public function getNextValue($objproduct = null, $type = '')
168 {
169 global $db;
170
171 if (is_object($objproduct) && !$objproduct instanceof Product) {
172 dol_syslog(get_class($this)."::getNextValue used on incompatible".get_class($objproduct), LOG_ERR);
173 return -1;
174 }
175
176 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
177 require_once DOL_DOCUMENT_ROOT.'/core/lib/barcode.lib.php'; // to be able to call function barcode_gen_ean_sum($ean)
178
179 // Get barcode type configuration for products if $type not set
180 if (empty($type)) {
181 $type = getDolGlobalString('PRODUIT_DEFAULT_BARCODE_TYPE');
182 }
183
184 // Get Mask value
185 $mask = getDolGlobalString('BARCODE_STANDARD_PRODUCT_MASK');
186
187 if (empty($mask)) {
188 $this->error = 'NotConfigured';
189 return '';
190 }
191
192 $field = 'barcode';
193 $where = '';
194
195 $now = dol_now();
196
197 $numFinal = get_next_value($db, $mask, 'product', $field, $where, '', $now);
198 //Begin barcode with key: for barcode with key (EAN13...) calculate and substitute the last character (* or ?) used in the mask by the key
199 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...)
200 $literaltype = '';
201 $literaltype = $this->literalBarcodeType($db, $type);//get literal_Barcode_Type
202 switch ($literaltype) {
203 case 'EAN13': //EAN13 rowid = 2
204 if (strlen($numFinal) == 13) {// be sure that the mask length is correct for EAN13
205 $ean = substr($numFinal, 0, 12); //take first 12 digits
206 $eansum = barcode_gen_ean_sum($ean);
207 $ean .= $eansum; //substitute the las character by the key
208 $numFinal = $ean;
209 }
210 break;
211 // Other barcode cases with key could be written here
212 default:
213 break;
214 }
215 }
216 //End barcode with key
217
218 return $numFinal;
219 }
220
221
236 public function verif($db, &$code, $product, $thirdparty_type, $type)
237 {
238 global $conf;
239
240 //var_dump($code.' '.$product->ref.' '.$thirdparty_type);exit;
241
242 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
243
244 $result = 0;
245 $code = strtoupper(trim($code));
246
247 if (empty($code) && $this->code_null && !getDolGlobalString('BARCODE_STANDARD_PRODUCT_MASK')) {
248 $result = 0;
249 } elseif (empty($code) && (!$this->code_null || getDolGlobalString('BARCODE_STANDARD_PRODUCT_MASK'))) {
250 $result = -2;
251 } else {
252 if ($this->verif_syntax($code, $type) >= 0) {
253 $is_dispo = $this->verif_dispo($db, $code, $product);
254 if ($is_dispo != 0) {
255 $result = -3;
256 } else {
257 $result = 0;
258 }
259 } else {
260 if (dol_strlen($code) == 0) {
261 $result = -2;
262 } else {
263 $result = -1;
264 }
265 }
266 }
267
268 dol_syslog(get_class($this)."::verif type=".$thirdparty_type." result=".$result);
269 return $result;
270 }
271
272
273 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
282 public function verif_dispo($db, $code, $product)
283 {
284 // phpcs:enable
285 $sql = "SELECT barcode FROM ".MAIN_DB_PREFIX."product";
286 $sql .= " WHERE barcode = '".$db->escape($code)."'";
287 $sql .= " AND entity IN (".getEntity('product').")";
288
289 if ($product->id > 0) {
290 $sql .= " AND rowid <> ".$product->id;
291 }
292
293 $resql = $db->query($sql);
294 if ($resql) {
295 if ($db->num_rows($resql) == 0) {
296 return 0;
297 } else {
298 return -1;
299 }
300 } else {
301 return -2;
302 }
303 }
304
305 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
313 public function verif_syntax($codefortest, $typefortest)
314 {
315 // phpcs:enable
316 global $conf;
317
318 $result = 0;
319
320 // Get Mask value
321 $mask = !getDolGlobalString('BARCODE_STANDARD_PRODUCT_MASK') ? '' : $conf->global->BARCODE_STANDARD_PRODUCT_MASK;
322 if (!$mask) {
323 $this->error = 'NotConfigured';
324 return -1;
325 }
326
327 dol_syslog(get_class($this).'::verif_syntax codefortest='.$codefortest." typefortest=".$typefortest);
328
329 $newcodefortest = $codefortest;
330
331 // Special case, if mask is on 12 digits instead of 13, we remove last char into code to test
332 if (in_array($typefortest, array('EAN13', 'ISBN'))) { // We remove the CRC char not included into mask
333 if (preg_match('/\{(0+)([@\+][0-9]+)?([@\+][0-9]+)?\}/i', $mask, $reg)) {
334 if (strlen($reg[1]) == 12) {
335 $newcodefortest = substr($newcodefortest, 0, 12);
336 }
337 dol_syslog(get_class($this).'::verif_syntax newcodefortest='.$newcodefortest);
338 }
339 }
340
341 $result = check_value($mask, $newcodefortest);
342 if (is_string($result)) {
343 $this->error = $result;
344 return -1;
345 }
346
347 return $result;
348 }
349}
barcode_gen_ean_sum($ean)
Calculate EAN sum.
Parent class for barcode numbering models.
Class to manage products or services.
Class to manage barcode with standard rule.
getNextValue($objproduct=null, $type='')
Return next value.
verif_dispo($db, $code, $product)
Return if a code is used (by other element)
verif($db, &$code, $product, $thirdparty_type, $type)
Check validity of code according to its rules.
getExample($langs, $objproduct=null)
Return an example of result returned by getNextValue.
literalBarcodeType($db, $type=0)
Return literal barcode type code from numerical rowid type of barcode.
verif_syntax($codefortest, $typefortest)
Return if a barcode value match syntax.
info($langs)
Return description of module.
check_value($mask, $value)
Check 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)
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_now($mode='auto')
Return date for now.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.