|
1
|
|
|
<?php |
|
2
|
|
|
/* Copyright (C) 2004 Rodolphe Quiedeville <[email protected]> |
|
3
|
|
|
* Copyright (C) 2006-2014 Laurent Destailleur <[email protected]> |
|
4
|
|
|
* Copyright (C) 2007-2012 Regis Houssin <[email protected]> |
|
5
|
|
|
* Copyright (C) 2011 Juanjo Menent <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software; you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU General Public License as published by |
|
9
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
10
|
|
|
* (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU General Public License |
|
18
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
19
|
|
|
* or see https://www.gnu.org/ |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* \file htdocs/core/modules/barcode/mod_barcode_product_standard.php |
|
24
|
|
|
* \ingroup barcode |
|
25
|
|
|
* \brief File of class to manage barcode numbering with standard rule |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
require_once DOL_DOCUMENT_ROOT.'/core/modules/barcode/modules_barcode.class.php'; |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Class to manage barcode with standard rule |
|
33
|
|
|
*/ |
|
34
|
|
|
class mod_barcode_product_standard extends ModeleNumRefBarCode |
|
35
|
|
|
{ |
|
36
|
|
|
public $name = 'Standard'; // Model Name |
|
37
|
|
|
|
|
38
|
|
|
public $code_modifiable; // Editable code |
|
39
|
|
|
|
|
40
|
|
|
public $code_modifiable_invalide; // Modified code if it is invalid |
|
41
|
|
|
|
|
42
|
|
|
public $code_modifiable_null; // Modified code if it is null |
|
43
|
|
|
|
|
44
|
|
|
public $code_null; // Optional code |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Dolibarr version of the loaded document |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr' |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var int Automatic numbering |
|
54
|
|
|
*/ |
|
55
|
|
|
public $code_auto; |
|
56
|
|
|
|
|
57
|
|
|
public $searchcode; // Search string |
|
58
|
|
|
|
|
59
|
|
|
public $numbitcounter; // Number of digits the counter |
|
60
|
|
|
|
|
61
|
|
|
public $prefixIsRequired; // The prefix field of third party must be filled when using {pre} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Constructor |
|
66
|
|
|
*/ |
|
67
|
|
|
public function __construct() |
|
68
|
|
|
{ |
|
69
|
|
|
$this->code_null = 0; |
|
70
|
|
|
$this->code_modifiable = 1; |
|
71
|
|
|
$this->code_modifiable_invalide = 1; |
|
72
|
|
|
$this->code_modifiable_null = 1; |
|
73
|
|
|
$this->code_auto = 1; |
|
74
|
|
|
$this->prefixIsRequired = 0; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
/** Return description of module |
|
79
|
|
|
* |
|
80
|
|
|
* @param Translate $langs Object langs |
|
81
|
|
|
* @return string Description of module |
|
82
|
|
|
*/ |
|
83
|
|
|
public function info($langs) |
|
84
|
|
|
{ |
|
85
|
|
|
global $conf, $mc; |
|
86
|
|
|
global $form; |
|
87
|
|
|
|
|
88
|
|
|
$langs->load("products"); |
|
89
|
|
|
|
|
90
|
|
|
$disabled = ((!empty($mc->sharings['referent']) && $mc->sharings['referent'] != $conf->entity) ? ' disabled' : ''); |
|
91
|
|
|
|
|
92
|
|
|
$texte = $langs->trans('GenericNumRefModelDesc')."<br>\n"; |
|
93
|
|
|
$texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">'; |
|
94
|
|
|
$texte .= '<input type="hidden" name="token" value="'.newToken().'">'; |
|
95
|
|
|
$texte .= '<input type="hidden" name="action" value="setModuleOptions">'; |
|
96
|
|
|
$texte .= '<input type="hidden" name="param1" value="BARCODE_STANDARD_PRODUCT_MASK">'; |
|
97
|
|
|
$texte .= '<table class="nobordernopadding" width="100%">'; |
|
98
|
|
|
|
|
99
|
|
|
$tooltip = $langs->trans("GenericMaskCodes", $langs->transnoentities("BarCode"), $langs->transnoentities("BarCode")); |
|
100
|
|
|
$tooltip .= $langs->trans("GenericMaskCodes3"); |
|
101
|
|
|
$tooltip .= '<strong>'.$langs->trans("Example").':</strong><br>'; |
|
102
|
|
|
$tooltip .= '020{000000000}? (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
|
|
|
|
|
107
|
|
|
// Mask parameter |
|
108
|
|
|
//$texte.= '<tr><td>'.$langs->trans("Mask").' ('.$langs->trans("BarCodeModel").'):</td>'; |
|
109
|
|
|
$texte .= '<tr><td>'.$langs->trans("Mask").':</td>'; |
|
110
|
|
|
$texte .= '<td class="right">'.$form->textwithpicto('<input type="text" class="flat" size="24" name="value1" value="'.(!empty($conf->global->BARCODE_STANDARD_PRODUCT_MASK) ? $conf->global->BARCODE_STANDARD_PRODUCT_MASK : '').'"'.$disabled.'>', $tooltip, 1, 1).'</td>'; |
|
111
|
|
|
$texte .= '<td class="left" rowspan="2"> <input type="submit" class="button" value="'.$langs->trans("Modify").'" name="Button"'.$disabled.'></td>'; |
|
112
|
|
|
$texte .= '</tr>'; |
|
113
|
|
|
|
|
114
|
|
|
$texte .= '</table>'; |
|
115
|
|
|
$texte .= '</form>'; |
|
116
|
|
|
|
|
117
|
|
|
return $texte; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Return an example of result returned by getNextValue |
|
123
|
|
|
* |
|
124
|
|
|
* @param Translate $langs Object langs |
|
125
|
|
|
* @param Product $objproduct Object product |
|
126
|
|
|
* @return string Return string example |
|
127
|
|
|
*/ |
|
128
|
|
|
public function getExample($langs, $objproduct = 0) |
|
129
|
|
|
{ |
|
130
|
|
|
$examplebarcode = $this->getNextValue($objproduct, ''); |
|
131
|
|
|
if (!$examplebarcode) |
|
132
|
|
|
{ |
|
133
|
|
|
$examplebarcode = $langs->trans('NotConfigured'); |
|
134
|
|
|
} |
|
135
|
|
|
if ($examplebarcode == "ErrorBadMask") |
|
136
|
|
|
{ |
|
137
|
|
|
$langs->load("errors"); |
|
138
|
|
|
$examplebarcode = $langs->trans($examplebarcode); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return $examplebarcode; |
|
142
|
|
|
} |
|
143
|
|
|
/** |
|
144
|
|
|
* Return literal barcode type code from numerical rowid type of barcode |
|
145
|
|
|
* |
|
146
|
|
|
* @param Database $db Database |
|
147
|
|
|
* @param int $type Type of barcode (EAN, ISBN, ...) as rowid |
|
148
|
|
|
* @return string |
|
149
|
|
|
*/ |
|
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
|
|
|
} |
|
168
|
|
|
else { |
|
169
|
|
|
dol_print_error($db); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return $out; |
|
173
|
|
|
} |
|
174
|
|
|
/** |
|
175
|
|
|
* Return next value |
|
176
|
|
|
* |
|
177
|
|
|
* @param Product $objproduct Object product |
|
178
|
|
|
* @param string $type Type of barcode (EAN, ISBN, ...) |
|
179
|
|
|
* @return string Value if OK, '' if module not configured, <0 if KO |
|
180
|
|
|
*/ |
|
181
|
|
|
public function getNextValue($objproduct = null, $type = '') |
|
182
|
|
|
{ |
|
183
|
|
|
global $db, $conf; |
|
184
|
|
|
|
|
185
|
|
|
require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php'; |
|
186
|
|
|
require_once DOL_DOCUMENT_ROOT.'/core/lib/barcode.lib.php'; // to be able to call function barcode_gen_ean_sum($ean) |
|
187
|
|
|
|
|
188
|
|
|
if (empty($type)) { |
|
189
|
|
|
$type = $conf->global->PRODUIT_DEFAULT_BARCODE_TYPE; |
|
190
|
|
|
} //get barcode type configuration for products if $type not set |
|
191
|
|
|
|
|
192
|
|
|
// TODO |
|
193
|
|
|
|
|
194
|
|
|
// Get Mask value |
|
195
|
|
|
$mask = ''; |
|
196
|
|
|
if (!empty($conf->global->BARCODE_STANDARD_PRODUCT_MASK)) $mask = $conf->global->BARCODE_STANDARD_PRODUCT_MASK; |
|
197
|
|
|
|
|
198
|
|
|
if (empty($mask)) |
|
199
|
|
|
{ |
|
200
|
|
|
$this->error = 'NotConfigured'; |
|
201
|
|
|
return ''; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
$field = 'barcode'; $where = ''; |
|
205
|
|
|
|
|
206
|
|
|
$now = dol_now(); |
|
207
|
|
|
|
|
208
|
|
|
$numFinal = get_next_value($db, $mask, 'product', $field, $where, '', $now); |
|
209
|
|
|
//Begin barcode with key: for barcode with key (EAN13...) calculate and substitute the last character (* or ?) used in the mask by the key |
|
210
|
|
|
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...) |
|
211
|
|
|
{ |
|
212
|
|
|
$literaltype = ''; |
|
213
|
|
|
$literaltype = $this->literalBarcodeType($db, $type);//get literal_Barcode_Type |
|
214
|
|
|
switch ($literaltype) |
|
215
|
|
|
{ |
|
216
|
|
|
case 'EAN13': //EAN13 rowid = 2 |
|
217
|
|
|
if (strlen($numFinal)==13)// be sure that the mask length is correct for EAN13 |
|
218
|
|
|
{ |
|
219
|
|
|
$ean = substr($numFinal, 0, 12); //take first 12 digits |
|
220
|
|
|
$eansum = barcode_gen_ean_sum($ean); |
|
221
|
|
|
$ean .= $eansum; //substitute the las character by the key |
|
222
|
|
|
$numFinal = $ean; |
|
223
|
|
|
} |
|
224
|
|
|
break; |
|
225
|
|
|
// Other barcode cases with key could be written here |
|
226
|
|
|
default: |
|
227
|
|
|
break; |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
//End barcode with key |
|
231
|
|
|
return $numFinal; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Check validity of code according to its rules |
|
237
|
|
|
* |
|
238
|
|
|
* @param DoliDB $db Database handler |
|
239
|
|
|
* @param string $code Code to check/correct |
|
240
|
|
|
* @param Product $product Object product |
|
241
|
|
|
* @param int $thirdparty_type 0 = customer/prospect , 1 = supplier |
|
242
|
|
|
* @param string $type type of barcode (EAN, ISBN, ...) |
|
243
|
|
|
* @return int 0 if OK |
|
244
|
|
|
* -1 ErrorBadCustomerCodeSyntax |
|
245
|
|
|
* -2 ErrorCustomerCodeRequired |
|
246
|
|
|
* -3 ErrorCustomerCodeAlreadyUsed |
|
247
|
|
|
* -4 ErrorPrefixRequired |
|
248
|
|
|
*/ |
|
249
|
|
|
public function verif($db, &$code, $product, $thirdparty_type, $type) |
|
250
|
|
|
{ |
|
251
|
|
|
global $conf; |
|
252
|
|
|
|
|
253
|
|
|
//var_dump($code.' '.$product->ref.' '.$thirdparty_type);exit; |
|
254
|
|
|
|
|
255
|
|
|
require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php'; |
|
256
|
|
|
|
|
257
|
|
|
$result = 0; |
|
258
|
|
|
$code = strtoupper(trim($code)); |
|
259
|
|
|
|
|
260
|
|
|
if (empty($code) && $this->code_null && empty($conf->global->BARCODE_STANDARD_PRODUCT_MASK)) |
|
261
|
|
|
{ |
|
262
|
|
|
$result = 0; |
|
263
|
|
|
} elseif (empty($code) && (!$this->code_null || !empty($conf->global->BARCODE_STANDARD_PRODUCT_MASK))) |
|
264
|
|
|
{ |
|
265
|
|
|
$result = -2; |
|
266
|
|
|
} else { |
|
267
|
|
|
if ($this->verif_syntax($code, $type) >= 0) |
|
268
|
|
|
{ |
|
269
|
|
|
$is_dispo = $this->verif_dispo($db, $code, $product); |
|
270
|
|
|
if ($is_dispo <> 0) |
|
271
|
|
|
{ |
|
272
|
|
|
$result = -3; |
|
273
|
|
|
} else { |
|
274
|
|
|
$result = 0; |
|
275
|
|
|
} |
|
276
|
|
|
} else { |
|
277
|
|
|
if (dol_strlen($code) == 0) |
|
278
|
|
|
{ |
|
279
|
|
|
$result = -2; |
|
280
|
|
|
} else { |
|
281
|
|
|
$result = -1; |
|
282
|
|
|
} |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
dol_syslog(get_class($this)."::verif type=".$thirdparty_type." result=".$result); |
|
287
|
|
|
return $result; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
|
|
291
|
|
|
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
292
|
|
|
/** |
|
293
|
|
|
* Return if a code is used (by other element) |
|
294
|
|
|
* |
|
295
|
|
|
* @param DoliDB $db Handler acces base |
|
296
|
|
|
* @param string $code Code to check |
|
297
|
|
|
* @param Product $product Objet product |
|
298
|
|
|
* @return int 0 if available, <0 if KO |
|
299
|
|
|
*/ |
|
300
|
|
|
public function verif_dispo($db, $code, $product) |
|
301
|
|
|
{ |
|
302
|
|
|
// phpcs:enable |
|
303
|
|
|
$sql = "SELECT barcode FROM ".MAIN_DB_PREFIX."product"; |
|
304
|
|
|
$sql .= " WHERE barcode = '".$db->escape($code)."'"; |
|
305
|
|
|
if ($product->id > 0) $sql .= " AND rowid <> ".$product->id; |
|
306
|
|
|
|
|
307
|
|
|
$resql = $db->query($sql); |
|
308
|
|
|
if ($resql) |
|
309
|
|
|
{ |
|
310
|
|
|
if ($db->num_rows($resql) == 0) |
|
311
|
|
|
{ |
|
312
|
|
|
return 0; |
|
313
|
|
|
} else { |
|
314
|
|
|
return -1; |
|
315
|
|
|
} |
|
316
|
|
|
} else { |
|
317
|
|
|
return -2; |
|
318
|
|
|
} |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
322
|
|
|
/** |
|
323
|
|
|
* Return if a barcode value match syntax |
|
324
|
|
|
* |
|
325
|
|
|
* @param string $codefortest Code to check syntax |
|
326
|
|
|
* @param string $typefortest Type of barcode (ISBN, EAN, ...) |
|
327
|
|
|
* @return int 0 if OK, <0 if KO |
|
328
|
|
|
*/ |
|
329
|
|
|
public function verif_syntax($codefortest, $typefortest) |
|
330
|
|
|
{ |
|
331
|
|
|
// phpcs:enable |
|
332
|
|
|
global $conf; |
|
333
|
|
|
|
|
334
|
|
|
$result = 0; |
|
335
|
|
|
|
|
336
|
|
|
// Get Mask value |
|
337
|
|
|
$mask = empty($conf->global->BARCODE_STANDARD_PRODUCT_MASK) ? '' : $conf->global->BARCODE_STANDARD_PRODUCT_MASK; |
|
338
|
|
|
if (!$mask) |
|
339
|
|
|
{ |
|
340
|
|
|
$this->error = 'NotConfigured'; |
|
341
|
|
|
return -1; |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
dol_syslog(get_class($this).'::verif_syntax codefortest='.$codefortest." typefortest=".$typefortest); |
|
345
|
|
|
|
|
346
|
|
|
$newcodefortest = $codefortest; |
|
347
|
|
|
|
|
348
|
|
|
// Special case, if mask is on 12 digits instead of 13, we remove last char into code to test |
|
349
|
|
|
if (in_array($typefortest, array('EAN13', 'ISBN'))) // We remove the CRC char not included into mask |
|
350
|
|
|
{ |
|
351
|
|
|
if (preg_match('/\{(0+)([@\+][0-9]+)?([@\+][0-9]+)?\}/i', $mask, $reg)) |
|
352
|
|
|
{ |
|
353
|
|
|
if (strlen($reg[1]) == 12) $newcodefortest = substr($newcodefortest, 0, 12); |
|
354
|
|
|
dol_syslog(get_class($this).'::verif_syntax newcodefortest='.$newcodefortest); |
|
355
|
|
|
} |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
$result = check_value($mask, $newcodefortest); |
|
359
|
|
|
if (is_string($result)) |
|
360
|
|
|
{ |
|
361
|
|
|
$this->error = $result; |
|
362
|
|
|
return -1; |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
return $result; |
|
366
|
|
|
} |
|
367
|
|
|
} |
|
368
|
|
|
|