dolibarr 21.0.0-alpha
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 = null, $objproduct = null)
117 {
118 if (!$langs instanceof Translate) {
119 $langs = $GLOBALS['langs'];
120 '@phan-var-force Translate $langs';
121 }
122 $examplebarcode = $this->getNextValue($objproduct, '');
123 if (!$examplebarcode) {
124 $examplebarcode = $langs->trans('NotConfigured');
125 }
126 if ($examplebarcode == "ErrorBadMask") {
127 $langs->load("errors");
128 $examplebarcode = $langs->trans($examplebarcode);
129 }
130
131 return $examplebarcode;
132 }
140 public function literalBarcodeType($db, $type = 0)
141 {
142 global $conf;
143 $out = '';
144
145 $sql = "SELECT rowid, code, libelle as label";
146 $sql .= " FROM ".MAIN_DB_PREFIX."c_barcode_type";
147 $sql .= " WHERE rowid = ".(int) $type;
148 $sql .= " AND entity = ".((int) $conf->entity);
149 $result = $db->query($sql);
150 if ($result) {
151 $num = $db->num_rows($result);
152
153 if ($num > 0) {
154 $obj = $db->fetch_object($result);
155 $out .= $obj->label; //take the label corresponding to the type rowid in the database
156 }
157 } else {
158 dol_print_error($db);
159 }
160
161 return $out;
162 }
163
171 public function getNextValue($objproduct = null, $type = '')
172 {
173 global $db;
174
175 if (is_object($objproduct) && !$objproduct instanceof Product) {
176 dol_syslog(get_class($this)."::getNextValue used on incompatible".get_class($objproduct), LOG_ERR);
177 return -1;
178 }
179
180 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
181 require_once DOL_DOCUMENT_ROOT.'/core/lib/barcode.lib.php'; // to be able to call function barcode_gen_ean_sum($ean)
182
183 // Get barcode type configuration for products if $type not set
184 if (empty($type)) {
185 $type = getDolGlobalString('PRODUIT_DEFAULT_BARCODE_TYPE');
186 }
187
188 // Get Mask value
189 $mask = getDolGlobalString('BARCODE_STANDARD_PRODUCT_MASK');
190
191 if (empty($mask)) {
192 $this->error = 'NotConfigured';
193 return '';
194 }
195
196 $field = 'barcode';
197 $where = '';
198
199 $now = dol_now();
200
201 $numFinal = get_next_value($db, $mask, 'product', $field, $where, '', $now);
202 //Begin barcode with key: for barcode with key (EAN13...) calculate and substitute the last character (* or ?) used in the mask by the key
203 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...)
204 $literaltype = '';
205 $literaltype = $this->literalBarcodeType($db, $type);//get literal_Barcode_Type
206 switch ($literaltype) {
207 case 'EAN13': //EAN13 rowid = 2
208 if (strlen($numFinal) == 13) {// be sure that the mask length is correct for EAN13
209 $ean = substr($numFinal, 0, 12); //take first 12 digits
210 $eansum = barcode_gen_ean_sum($ean);
211 $ean .= $eansum; //substitute the las character by the key
212 $numFinal = $ean;
213 }
214 break;
215 // Other barcode cases with key could be written here
216 default:
217 break;
218 }
219 }
220 //End barcode with key
221
222 return $numFinal;
223 }
224
225
241 public function verif($db, &$code, $product, $thirdparty_type, $type)
242 {
243 if (!$product instanceof Product) {
244 dol_syslog(get_class($this)."::verif called with ".get_class($product)." Expected Product", LOG_ERR);
245 return -7;
246 }
247
248 //var_dump($code.' '.$product->ref.' '.$thirdparty_type);exit;
249
250 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
251
252 $result = 0;
253 $code = strtoupper(trim($code));
254
255 if (empty($code) && $this->code_null && !getDolGlobalString('BARCODE_STANDARD_PRODUCT_MASK')) {
256 $result = 0;
257 } elseif (empty($code) && (!$this->code_null || getDolGlobalString('BARCODE_STANDARD_PRODUCT_MASK'))) {
258 $result = -2;
259 } else {
260 if ($this->verif_syntax($code, $type) >= 0) {
261 $is_dispo = $this->verif_dispo($db, $code, $product);
262 if ($is_dispo != 0) {
263 $result = -3;
264 } else {
265 $result = 0;
266 }
267 } else {
268 if (dol_strlen($code) == 0) {
269 $result = -2;
270 } else {
271 $result = -1;
272 }
273 }
274 }
275
276 dol_syslog(get_class($this)."::verif type=".$thirdparty_type." result=".$result);
277 return $result;
278 }
279
280
281 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
290 public function verif_dispo($db, $code, $product)
291 {
292 // phpcs:enable
293 $sql = "SELECT barcode FROM ".MAIN_DB_PREFIX."product";
294 $sql .= " WHERE barcode = '".$db->escape($code)."'";
295 $sql .= " AND entity IN (".getEntity('product').")";
296
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 = !getDolGlobalString('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}
barcode_gen_ean_sum($ean)
Calculate EAN sum.
Class to manage products or services.
Class to manage translations.
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.
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.
getExample($langs=null, $objproduct=null)
Return an example of result returned by getNextValue.
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 a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.