dolibarr 20.0.2
mod_barcode_thirdparty_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) 2022 Faustin Boitel <fboitel@enseirb-matmeca.fr>
7 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
8 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 * or see https://www.gnu.org/
23 */
24
31require_once DOL_DOCUMENT_ROOT.'/core/modules/barcode/modules_barcode.class.php';
32
33
38{
39 public $name = 'Standard'; // Model Name
40
41 public $code_modifiable; // Editable code
42
43 public $code_modifiable_invalide; // Modified code if it is invalid
44
45 public $code_modifiable_null; // Modified code if it is null
46
47 public $code_null; // Optional code
48
53 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
54
55 public $searchcode; // Search string
56
57 public $numbitcounter; // Number of digits the counter
58
59 public $prefixIsRequired; // The prefix field of third party must be filled when using {pre}
60
61
65 public function __construct()
66 {
67 $this->code_null = 0;
68 $this->code_modifiable = 1;
69 $this->code_modifiable_invalide = 1;
70 $this->code_modifiable_null = 1;
71 $this->code_auto = 1;
72 $this->prefixIsRequired = 0;
73 }
74
75
82 public function info($langs)
83 {
84 global $conf, $mc;
85 global $form;
86
87 $langs->load("thirdparties");
88
89 $disabled = ((!empty($mc->sharings['referent']) && $mc->sharings['referent'] != $conf->entity) ? ' disabled' : '');
90
91 $texte = $langs->trans('GenericNumRefModelDesc')."<br>\n";
92 $texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
93 $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
94 $texte .= '<input type="hidden" name="page_y" value="">';
95 $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
96 $texte .= '<input type="hidden" name="param1" value="BARCODE_STANDARD_THIRDPARTY_MASK">';
97 $texte .= '<table class="nobordernopadding" width="100%">';
98
99 $tooltip = $langs->trans("GenericMaskCodes", $langs->transnoentities("BarCode"), $langs->transnoentities("BarCode"));
100 $tooltip .= $langs->trans("GenericMaskCodes3EAN");
101 $tooltip .= '<strong>'.$langs->trans("Example").':</strong><br>';
102 $tooltip .= '04{0000000000}? (for internal use)<br>';
103 $tooltip .= '9771234{00000}? (example of ISSN code with prefix 1234)<br>';
104 $tooltip .= '9791234{00000}? (example of ISMN code with prefix 1234)<br>';
105 //$tooltip . =$langs->trans("GenericMaskCodes5");
106 //$tooltip .= '<br>'.$langs->trans("GenericMaskCodes5b");
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="'.(getDolGlobalString('BARCODE_STANDARD_THIRDPARTY_MASK') ? $conf->global->BARCODE_STANDARD_THIRDPARTY_MASK : '').'"'.$disabled.'>', $tooltip, 1, 1).'</td>';
112 $texte .= '<td class="left" rowspan="2">&nbsp; <input type="submit" class="button button-edit reposition smallpaddingimp" 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, $objthirdparty = null)
130 {
131 $examplebarcode = $this->getNextValue($objthirdparty, '');
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 = 0)
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 = ". (int) $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($objthirdparty = null, $type = '')
180 {
181 global $db;
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 // Get barcode type configuration for products if $type not set
187 if (empty($type)) {
188 $type = getDolGlobalString('GENBARCODE_BARCODETYPE_THIRDPARTY');
189 }
190
191 // Get Mask value
192 $mask = getDolGlobalString('BARCODE_STANDARD_THIRDPARTY_MASK');
193
194 if (empty($mask)) {
195 $this->error = 'NotConfigured';
196 return '';
197 }
198
199 $field = 'barcode';
200 $where = '';
201
202 $now = dol_now();
203
204 $numFinal = get_next_value($db, $mask, 'societe', $field, $where, '', $now);
205 //Begin barcode with key: for barcode with key (EAN13...) calculate and substitute the last character (* or ?) used in the mask by the key
206 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...)
207 $literaltype = '';
208 $literaltype = $this->literalBarcodeType($db, $type);//get literal_Barcode_Type
209 switch ($literaltype) {
210 case 'EAN13': //EAN13 rowid = 2
211 if (strlen($numFinal) == 13) {// be sure that the mask length is correct for EAN13
212 $ean = substr($numFinal, 0, 12); //take first 12 digits
213 $eansum = barcode_gen_ean_sum($ean);
214 $ean .= $eansum; //substitute the las character by the key
215 $numFinal = $ean;
216 }
217 break;
218 // Other barcode cases with key could be written here
219 default:
220 break;
221 }
222 }
223 //End barcode with key
224
225 return $numFinal;
226 }
227
228
243 public function verif($db, &$code, $thirdparty, $thirdparty_type, $type)
244 {
245 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
246
247 $result = 0;
248 $code = strtoupper(trim($code));
249
250 if (empty($code) && $this->code_null && !getDolGlobalString('BARCODE_STANDARD_THIRDPARTY_MASK')) {
251 $result = 0;
252 } elseif (empty($code) && (!$this->code_null || getDolGlobalString('BARCODE_STANDARD_THIRDPARTY_MASK'))) {
253 $result = -2;
254 } else {
255 if ($this->verif_syntax($code, $type) >= 0) {
256 $is_dispo = $this->verif_dispo($db, $code, $thirdparty);
257 if ($is_dispo != 0) {
258 $result = -3;
259 } else {
260 $result = 0;
261 }
262 } else {
263 if (dol_strlen($code) == 0) {
264 $result = -2;
265 } else {
266 $result = -1;
267 }
268 }
269 }
270
271 dol_syslog(get_class($this)."::verif type=".$thirdparty_type." result=".$result);
272
273 return $result;
274 }
275
276
277 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
286 public function verif_dispo($db, $code, $thirdparty)
287 {
288 // phpcs:enable
289 $sql = "SELECT barcode FROM ".MAIN_DB_PREFIX."societe";
290 $sql .= " WHERE barcode = '".$db->escape($code)."'";
291 if ($thirdparty->id > 0) {
292 $sql .= " AND rowid <> ".$thirdparty->id;
293 }
294
295 $resql = $db->query($sql);
296 if ($resql) {
297 if ($db->num_rows($resql) == 0) {
298 return 0;
299 } else {
300 return -1;
301 }
302 } else {
303 return -2;
304 }
305 }
306
307 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
315 public function verif_syntax($codefortest, $typefortest)
316 {
317 // phpcs:enable
318 global $conf;
319
320 $result = 0;
321
322 // Get Mask value
323 $mask = !getDolGlobalString('BARCODE_STANDARD_THIRDPARTY_MASK') ? '' : $conf->global->BARCODE_STANDARD_THIRDPARTY_MASK;
324 if (!$mask) {
325 $this->error = 'NotConfigured';
326 return -1;
327 }
328
329 dol_syslog(get_class($this).'::verif_syntax codefortest='.$codefortest." typefortest=".$typefortest);
330
331 $newcodefortest = $codefortest;
332
333 // Special case, if mask is on 12 digits instead of 13, we remove last char into code to test
334 if (in_array($typefortest, array('EAN13', 'ISBN'))) { // We remove the CRC char not included into mask
335 if (preg_match('/\{(0+)([@\+][0-9]+)?([@\+][0-9]+)?\}/i', $mask, $reg)) {
336 if (strlen($reg[1]) == 12) {
337 $newcodefortest = substr($newcodefortest, 0, 12);
338 }
339 dol_syslog(get_class($this).'::verif_syntax newcodefortest='.$newcodefortest);
340 }
341 }
342
343 $result = check_value($mask, $newcodefortest);
344 if (is_string($result)) {
345 $this->error = $result;
346 return -1;
347 }
348
349 return $result;
350 }
351}
barcode_gen_ean_sum($ean)
Calculate EAN sum.
Parent class for barcode numbering models.
Class to manage barcode with standard rule.
verif($db, &$code, $thirdparty, $thirdparty_type, $type)
Check validity of code according to its rules.
getNextValue($objthirdparty=null, $type='')
Return next value.
verif_syntax($codefortest, $typefortest)
Return if a barcode value match syntax.
verif_dispo($db, $code, $thirdparty)
Return if a code is used (by other element)
info($langs)
Return description of module.
literalBarcodeType($db, $type=0)
Return literal barcode type code from numerical rowid type of barcode.
getExample($langs, $objthirdparty=null)
Return an example of result returned by getNextValue.
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.