dolibarr 21.0.0-alpha
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
44 public $code_modifiable;
45
49 public $code_modifiable_invalide;
50
54 public $code_modifiable_null;
55
59 public $code_null;
60
65 public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
66
70 public $searchcode;
71
77 public $numbitcounter;
78
82 public $prefixIsRequired;
83
84
88 public function __construct()
89 {
90 $this->code_null = 0;
91 $this->code_modifiable = 1;
92 $this->code_modifiable_invalide = 1;
93 $this->code_modifiable_null = 1;
94 $this->code_auto = 1;
95 $this->prefixIsRequired = 0;
96 }
97
98
105 public function info($langs)
106 {
107 global $conf, $mc;
108 global $form;
109
110 $langs->load("thirdparties");
111
112 $disabled = ((!empty($mc->sharings['referent']) && $mc->sharings['referent'] != $conf->entity) ? ' disabled' : '');
113
114 $texte = $langs->trans('GenericNumRefModelDesc')."<br>\n";
115 $texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
116 $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
117 $texte .= '<input type="hidden" name="page_y" value="">';
118 $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
119 $texte .= '<input type="hidden" name="param1" value="BARCODE_STANDARD_THIRDPARTY_MASK">';
120 $texte .= '<table class="nobordernopadding" width="100%">';
121
122 $tooltip = $langs->trans("GenericMaskCodes", $langs->transnoentities("BarCode"), $langs->transnoentities("BarCode"));
123 $tooltip .= $langs->trans("GenericMaskCodes3EAN");
124 $tooltip .= '<strong>'.$langs->trans("Example").':</strong><br>';
125 $tooltip .= '04{0000000000}? (for internal use)<br>';
126 $tooltip .= '9771234{00000}? (example of ISSN code with prefix 1234)<br>';
127 $tooltip .= '9791234{00000}? (example of ISMN code with prefix 1234)<br>';
128 //$tooltip . =$langs->trans("GenericMaskCodes5");
129 //$tooltip .= '<br>'.$langs->trans("GenericMaskCodes5b");
130
131 // Mask parameter
132 //$texte.= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("BarCodeModel").'):</td>';
133 $texte .= '<tr><td>'.$langs->trans("Mask").':</td>';
134 $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>';
135 $texte .= '<td class="left" rowspan="2">&nbsp; <input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.$langs->trans("Modify").'"'.$disabled.'></td>';
136 $texte .= '</tr>';
137
138 $texte .= '</table>';
139 $texte .= '</form>';
140
141 return $texte;
142 }
143
144
152 public function getExample($langs = null, $objthirdparty = null)
153 {
154 if (!$langs instanceof Translate) {
155 $langs = $GLOBALS['langs'];
156 '@phan-var-force Translate $langs';
157 }
158 $examplebarcode = $this->getNextValue($objthirdparty, '');
159 if (!$examplebarcode) {
160 $examplebarcode = $langs->trans('NotConfigured');
161 }
162 if ($examplebarcode == "ErrorBadMask") {
163 $langs->load("errors");
164 $examplebarcode = $langs->trans($examplebarcode);
165 }
166
167 return $examplebarcode;
168 }
176 public function literalBarcodeType($db, $type = 0)
177 {
178 global $conf;
179 $out = '';
180
181 $sql = "SELECT rowid, code, libelle as label";
182 $sql .= " FROM ".MAIN_DB_PREFIX."c_barcode_type";
183 $sql .= " WHERE rowid = ". (int) $type;
184 $sql .= " AND entity = ".((int) $conf->entity);
185 $result = $db->query($sql);
186 if ($result) {
187 $num = $db->num_rows($result);
188
189 if ($num > 0) {
190 $obj = $db->fetch_object($result);
191 $out .= $obj->label; //take the label corresponding to the type rowid in the database
192 }
193 } else {
194 dol_print_error($db);
195 }
196
197 return $out;
198 }
206 public function getNextValue($objthirdparty = null, $type = '')
207 {
208 global $db;
209
210 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
211 require_once DOL_DOCUMENT_ROOT.'/core/lib/barcode.lib.php'; // to be able to call function barcode_gen_ean_sum($ean)
212
213 // Get barcode type configuration for products if $type not set
214 if (empty($type)) {
215 $type = getDolGlobalString('GENBARCODE_BARCODETYPE_THIRDPARTY');
216 }
217
218 // Get Mask value
219 $mask = getDolGlobalString('BARCODE_STANDARD_THIRDPARTY_MASK');
220
221 if (empty($mask)) {
222 $this->error = 'NotConfigured';
223 return '';
224 }
225
226 $field = 'barcode';
227 $where = '';
228
229 $now = dol_now();
230
231 $numFinal = get_next_value($db, $mask, 'societe', $field, $where, '', $now);
232 //Begin barcode with key: for barcode with key (EAN13...) calculate and substitute the last character (* or ?) used in the mask by the key
233 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...)
234 $literaltype = '';
235 $literaltype = $this->literalBarcodeType($db, $type);//get literal_Barcode_Type
236 switch ($literaltype) {
237 case 'EAN13': //EAN13 rowid = 2
238 if (strlen($numFinal) == 13) {// be sure that the mask length is correct for EAN13
239 $ean = substr($numFinal, 0, 12); //take first 12 digits
240 $eansum = barcode_gen_ean_sum($ean);
241 $ean .= $eansum; //substitute the las character by the key
242 $numFinal = $ean;
243 }
244 break;
245 // Other barcode cases with key could be written here
246 default:
247 break;
248 }
249 }
250 //End barcode with key
251
252 return $numFinal;
253 }
254
255
271 public function verif($db, &$code, $thirdparty, $thirdparty_type, $type)
272 {
273 if (!$thirdparty instanceof Societe) {
274 dol_syslog(get_class($this)."::verif called with ".get_class($thirdparty)." Expected Societe", LOG_ERR);
275 return -7;
276 }
277 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
278
279 $result = 0;
280 $code = strtoupper(trim($code));
281
282 if (empty($code) && $this->code_null && !getDolGlobalString('BARCODE_STANDARD_THIRDPARTY_MASK')) {
283 $result = 0;
284 } elseif (empty($code) && (!$this->code_null || getDolGlobalString('BARCODE_STANDARD_THIRDPARTY_MASK'))) {
285 $result = -2;
286 } else {
287 if ($this->verif_syntax($code, $type) >= 0) {
288 $is_dispo = $this->verif_dispo($db, $code, $thirdparty);
289 if ($is_dispo != 0) {
290 $result = -3;
291 } else {
292 $result = 0;
293 }
294 } else {
295 if (dol_strlen($code) == 0) {
296 $result = -2;
297 } else {
298 $result = -1;
299 }
300 }
301 }
302
303 dol_syslog(get_class($this)."::verif type=".$thirdparty_type." result=".$result);
304
305 return $result;
306 }
307
308
309 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
318 public function verif_dispo($db, $code, $thirdparty)
319 {
320 // phpcs:enable
321 $sql = "SELECT barcode FROM ".MAIN_DB_PREFIX."societe";
322 $sql .= " WHERE barcode = '".$db->escape($code)."'";
323 if ($thirdparty->id > 0) {
324 $sql .= " AND rowid <> ".$thirdparty->id;
325 }
326
327 $resql = $db->query($sql);
328 if ($resql) {
329 if ($db->num_rows($resql) == 0) {
330 return 0;
331 } else {
332 return -1;
333 }
334 } else {
335 return -2;
336 }
337 }
338
339 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
347 public function verif_syntax($codefortest, $typefortest)
348 {
349 // phpcs:enable
350 global $conf;
351
352 $result = 0;
353
354 // Get Mask value
355 $mask = !getDolGlobalString('BARCODE_STANDARD_THIRDPARTY_MASK') ? '' : $conf->global->BARCODE_STANDARD_THIRDPARTY_MASK;
356 if (!$mask) {
357 $this->error = 'NotConfigured';
358 return -1;
359 }
360
361 dol_syslog(get_class($this).'::verif_syntax codefortest='.$codefortest." typefortest=".$typefortest);
362
363 $newcodefortest = $codefortest;
364
365 // Special case, if mask is on 12 digits instead of 13, we remove last char into code to test
366 if (in_array($typefortest, array('EAN13', 'ISBN'))) { // We remove the CRC char not included into mask
367 if (preg_match('/\{(0+)([@\+][0-9]+)?([@\+][0-9]+)?\}/i', $mask, $reg)) {
368 if (strlen($reg[1]) == 12) {
369 $newcodefortest = substr($newcodefortest, 0, 12);
370 }
371 dol_syslog(get_class($this).'::verif_syntax newcodefortest='.$newcodefortest);
372 }
373 }
374
375 $result = check_value($mask, $newcodefortest);
376 if (is_string($result)) {
377 $this->error = $result;
378 return -1;
379 }
380
381 return $result;
382 }
383}
barcode_gen_ean_sum($ean)
Calculate EAN sum.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage translations.
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.
getExample($langs=null, $objthirdparty=null)
Return an example of result returned by getNextValue.
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.
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.