dolibarr 18.0.6
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 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 * or see https://www.gnu.org/
21 */
22
29require_once DOL_DOCUMENT_ROOT.'/core/modules/barcode/modules_barcode.class.php';
30
31
36{
37 public $name = 'Standard'; // Model Name
38
39 public $code_modifiable; // Editable code
40
41 public $code_modifiable_invalide; // Modified code if it is invalid
42
43 public $code_modifiable_null; // Modified code if it is null
44
45 public $code_null; // Optional code
46
51 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
52
56 public $code_auto;
57
58 public $searchcode; // Search string
59
60 public $numbitcounter; // Number of digits the counter
61
62 public $prefixIsRequired; // The prefix field of third party must be filled when using {pre}
63
64
68 public function __construct()
69 {
70 $this->code_null = 0;
71 $this->code_modifiable = 1;
72 $this->code_modifiable_invalide = 1;
73 $this->code_modifiable_null = 1;
74 $this->code_auto = 1;
75 $this->prefixIsRequired = 0;
76 }
77
78
84 public function info($langs)
85 {
86 global $conf, $mc;
87 global $form;
88
89 $langs->load("thirdparties");
90
91 $disabled = ((!empty($mc->sharings['referent']) && $mc->sharings['referent'] != $conf->entity) ? ' disabled' : '');
92
93 $texte = $langs->trans('GenericNumRefModelDesc')."<br>\n";
94 $texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
95 $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
96 $texte .= '<input type="hidden" name="page_y" value="">';
97 $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
98 $texte .= '<input type="hidden" name="param1" value="BARCODE_STANDARD_THIRDPARTY_MASK">';
99 $texte .= '<table class="nobordernopadding" width="100%">';
100
101 $tooltip = $langs->trans("GenericMaskCodes", $langs->transnoentities("BarCode"), $langs->transnoentities("BarCode"));
102 $tooltip .= $langs->trans("GenericMaskCodes3EAN");
103 $tooltip .= '<strong>'.$langs->trans("Example").':</strong><br>';
104 $tooltip .= '04{0000000000}? (for internal use)<br>';
105 $tooltip .= '9771234{00000}? (example of ISSN code with prefix 1234)<br>';
106 $tooltip .= '9791234{00000}? (example of ISMN code with prefix 1234)<br>';
107 //$tooltip.=$langs->trans("GenericMaskCodes5");
108
109 // Mask parameter
110 //$texte.= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("BarCodeModel").'):</td>';
111 $texte .= '<tr><td>'.$langs->trans("Mask").':</td>';
112 $texte .= '<td class="right">'.$form->textwithpicto('<input type="text" class="flat minwidth175" name="value1" value="'.(!empty($conf->global->BARCODE_STANDARD_THIRDPARTY_MASK) ? $conf->global->BARCODE_STANDARD_THIRDPARTY_MASK : '').'"'.$disabled.'>', $tooltip, 1, 1).'</td>';
113 $texte .= '<td class="left" rowspan="2">&nbsp; <input type="submit" class="button button-edit reposition small" name="modify" value="'.$langs->trans("Modify").'"'.$disabled.'></td>';
114 $texte .= '</tr>';
115
116 $texte .= '</table>';
117 $texte .= '</form>';
118
119 return $texte;
120 }
121
122
130 public function getExample($langs, $objthirdparty = 0)
131 {
132 $examplebarcode = $this->getNextValue($objthirdparty, '');
133 if (!$examplebarcode) {
134 $examplebarcode = $langs->trans('NotConfigured');
135 }
136 if ($examplebarcode == "ErrorBadMask") {
137 $langs->load("errors");
138 $examplebarcode = $langs->trans($examplebarcode);
139 }
140
141 return $examplebarcode;
142 }
150 public function literalBarcodeType($db, $type = '')
151 {
152 global $conf;
153 $out = '';
154
155 $sql = "SELECT rowid, code, libelle as label";
156 $sql .= " FROM ".MAIN_DB_PREFIX."c_barcode_type";
157 $sql .= " WHERE rowid = '".$db->escape($type)."'";
158 $sql .= " AND entity = ".((int) $conf->entity);
159 $result = $db->query($sql);
160 if ($result) {
161 $num = $db->num_rows($result);
162
163 if ($num > 0) {
164 $obj = $db->fetch_object($result);
165 $out .= $obj->label; //take the label corresponding to the type rowid in the database
166 }
167 } else {
168 dol_print_error($db);
169 }
170
171 return $out;
172 }
180 public function getNextValue($objthirdparty, $type = '')
181 {
182 global $db, $conf;
183
184 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
185 require_once DOL_DOCUMENT_ROOT.'/core/lib/barcode.lib.php'; // to be able to call function barcode_gen_ean_sum($ean)
186
187 if (empty($type)) {
188 $type = $conf->global->GENBARCODE_BARCODETYPE_THIRDPARTY;
189 } //get barcode type configuration for companies if $type not set
190
191 // TODO
192
193 // Get Mask value
194 $mask = '';
195 if (!empty($conf->global->BARCODE_STANDARD_THIRDPARTY_MASK)) {
196 $mask = $conf->global->BARCODE_STANDARD_THIRDPARTY_MASK;
197 }
198
199 if (empty($mask)) {
200 $this->error = 'NotConfigured';
201 return '';
202 }
203
204 $field = 'barcode';
205 $where = '';
206
207 $now = dol_now();
208
209 $numFinal = get_next_value($db, $mask, 'societe', $field, $where, '', $now);
210 //Begin barcode with key: for barcode with key (EAN13...) calculate and substitute the last character (* or ?) used in the mask by the key
211 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...)
212 $literaltype = '';
213 $literaltype = $this->literalBarcodeType($db, $type);//get literal_Barcode_Type
214 switch ($literaltype) {
215 case 'EAN13': //EAN13 rowid = 2
216 if (strlen($numFinal)==13) {// be sure that the mask length is correct for EAN13
217 $ean = substr($numFinal, 0, 12); //take first 12 digits
218 $eansum = barcode_gen_ean_sum($ean);
219 $ean .= $eansum; //substitute the las character by the key
220 $numFinal = $ean;
221 }
222 break;
223 // Other barcode cases with key could be written here
224 default:
225 break;
226 }
227 }
228 //End barcode with key
229 return $numFinal;
230 }
231
232
247 public function verif($db, &$code, $thirdparty, $thirdparty_type, $type)
248 {
249 global $conf;
250
251 //var_dump($code.' '.$thirdparty->ref.' '.$thirdparty_type);exit;
252
253 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
254
255 $result = 0;
256 $code = strtoupper(trim($code));
257
258 if (empty($code) && $this->code_null && empty($conf->global->BARCODE_STANDARD_THIRDPARTY_MASK)) {
259 $result = 0;
260 } elseif (empty($code) && (!$this->code_null || !empty($conf->global->BARCODE_STANDARD_THIRDPARTY_MASK))) {
261 $result = -2;
262 } else {
263 if ($this->verif_syntax($code, $type) >= 0) {
264 $is_dispo = $this->verif_dispo($db, $code, $thirdparty);
265 if ($is_dispo <> 0) {
266 $result = -3;
267 } else {
268 $result = 0;
269 }
270 } else {
271 if (dol_strlen($code) == 0) {
272 $result = -2;
273 } else {
274 $result = -1;
275 }
276 }
277 }
278
279 dol_syslog(get_class($this)."::verif type=".$thirdparty_type." result=".$result);
280 return $result;
281 }
282
283
284 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
293 public function verif_dispo($db, $code, $thirdparty)
294 {
295 // phpcs:enable
296 $sql = "SELECT barcode FROM ".MAIN_DB_PREFIX."societe";
297 $sql .= " WHERE barcode = '".$db->escape($code)."'";
298 if ($thirdparty->id > 0) {
299 $sql .= " AND rowid <> ".$thirdparty->id;
300 }
301
302 $resql = $db->query($sql);
303 if ($resql) {
304 if ($db->num_rows($resql) == 0) {
305 return 0;
306 } else {
307 return -1;
308 }
309 } else {
310 return -2;
311 }
312 }
313
314 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
322 public function verif_syntax($codefortest, $typefortest)
323 {
324 // phpcs:enable
325 global $conf;
326
327 $result = 0;
328
329 // Get Mask value
330 $mask = empty($conf->global->BARCODE_STANDARD_THIRDPARTY_MASK) ? '' : $conf->global->BARCODE_STANDARD_THIRDPARTY_MASK;
331 if (!$mask) {
332 $this->error = 'NotConfigured';
333 return -1;
334 }
335
336 dol_syslog(get_class($this).'::verif_syntax codefortest='.$codefortest." typefortest=".$typefortest);
337
338 $newcodefortest = $codefortest;
339
340 // Special case, if mask is on 12 digits instead of 13, we remove last char into code to test
341 if (in_array($typefortest, array('EAN13', 'ISBN'))) { // We remove the CRC char not included into mask
342 if (preg_match('/\{(0+)([@\+][0-9]+)?([@\+][0-9]+)?\}/i', $mask, $reg)) {
343 if (strlen($reg[1]) == 12) {
344 $newcodefortest = substr($newcodefortest, 0, 12);
345 }
346 dol_syslog(get_class($this).'::verif_syntax newcodefortest='.$newcodefortest);
347 }
348 }
349
350 $result = check_value($mask, $newcodefortest);
351 if (is_string($result)) {
352 $this->error = $result;
353 return -1;
354 }
355
356 return $result;
357 }
358}
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.
verif_syntax($codefortest, $typefortest)
Return if a barcode value match syntax.
literalBarcodeType($db, $type='')
Return literal barcode type code from numerical rowid type of barcode.
verif_dispo($db, $code, $thirdparty)
Return if a code is used (by other element)
info($langs)
Return description of module.
getExample($langs, $objthirdparty=0)
Return an example of result returned by getNextValue.
getNextValue($objthirdparty, $type='')
Return next value.
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_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_now($mode='auto')
Return date for now.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.