dolibarr 21.0.0-beta
profid.lib.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2006-2011 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2022 Frédéric France <frederic.france@netlogic.fr>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 * or see https://www.gnu.org/
19 */
20
33function isValidLuhn($str)
34{
35 $str = (string) $str;
36 $len = dol_strlen($str);
37 $parity = $len % 2;
38 $sum = 0;
39 for ($i = $len - 1; $i >= 0; $i--) {
40 $d = (int) $str[$i];
41 if ($i % 2 == $parity) {
42 if (($d *= 2) > 9) {
43 $d -= 9;
44 }
45 }
46 $sum += $d;
47 }
48 return $sum % 10 == 0;
49}
50
51
59function isValidSiren($siren)
60{
61 $siren = trim($siren);
62 $siren = preg_replace('/(\s)/', '', $siren);
63
64 if (!is_numeric($siren) || dol_strlen($siren) != 9) {
65 return false;
66 }
67
68 return isValidLuhn($siren);
69}
70
71
79function isValidSiret($siret)
80{
81 $siret = trim($siret);
82 $siret = preg_replace('/(\s)/', '', $siret);
83
84 if (!is_numeric($siret) || dol_strlen($siret) != 14) {
85 return false;
86 }
87
88 if (isValidLuhn($siret)) {
89 return true;
90 } elseif ((substr($siret, 0, 9) == "356000000") && (array_sum(str_split($siret)) % 5 == 0)) {
96 return true;
97 } else {
98 return false;
99 }
100}
101
102
111function isValidTinForPT($str)
112{
113 $str = trim($str);
114 $str = preg_replace('/(\s)/', '', $str);
115
116 if (preg_match('/(^[0-9]{9}$)/', $str)) {
117 return true;
118 } else {
119 return false;
120 }
121}
122
123
132function isValidTinForDZ($str)
133{
134 $str = trim($str);
135 $str = preg_replace('/(\s)/', '', $str);
136
137 if (preg_match('/(^[0-9]{15}$)/', $str)) {
138 return true;
139 } else {
140 return false;
141 }
142}
143
144
153function isValidTinForBE($str)
154{
155 // https://economie.fgov.be/fr/themes/entreprises/banque-carrefour-des/actualites/structure-du-numero
156 $str = trim($str);
157 $str = preg_replace('/(\s)/', '', $str);
158
159 if (preg_match('/(^[0-1]{1}[0-9]{3}\.[0-9]{3}\.[0-9]{3}$)/', $str)) {
160 return true;
161 } else {
162 return false;
163 }
164}
165
166
177function isValidTinForES($str)
178{
179 $str = trim($str);
180 $str = preg_replace('/(\s)/', '', $str);
181 $str = strtoupper($str);
182
183 //Check format
184 if (!preg_match('/((^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$|^[T]{1}[A-Z0-9]{8}$)|^[0-9]{8}[A-Z]{1}$)/', $str)) {
185 return 0;
186 }
187
188 $num = array();
189 for ($i = 0; $i < 9; $i++) {
190 $num[$i] = substr($str, $i, 1);
191 }
192 '@phan-var-force array<int<0,8>,string> $num';
193
194 //Check NIF
195 if (preg_match('/(^[0-9]{8}[A-Z]{1}$)/', $str)) {
196 if ($num[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', (int) substr($str, 0, 8) % 23, 1)) {
197 return 1;
198 } else {
199 return -1;
200 }
201 }
202
203 //algorithm checking type code CIF
204 $sum = (int) $num[2] + (int) $num[4] + (int) $num[6];
205 for ($i = 1; $i < 8; $i += 2) {
206 $sum += intval(substr((string) (2 * (int) $num[$i]), 0, 1)) + intval(substr((string) (2 * (int) $num[$i]), 1, 1));
207 }
208 $n = 10 - (int) substr((string) $sum, strlen((string) $sum) - 1, 1);
209
210 //Check special NIF
211 if (preg_match('/^[KLM]{1}/', $str)) {
212 if ($num[8] == chr(64 + $n) || $num[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', (int) substr($str, 1, 8) % 23, 1)) {
213 return 1;
214 } else {
215 return -1;
216 }
217 }
218
219 //Check CIF
220 if (preg_match('/^[ABCDEFGHJNPQRSUVW]{1}/', $str)) {
221 if ($num[8] == chr(64 + $n) || $num[8] == substr((string) $n, strlen((string) $n) - 1, 1)) {
222 return 2;
223 } else {
224 return -2;
225 }
226 }
227
228 //Check NIE T
229 if (preg_match('/^[T]{1}/', $str)) {
230 if ($num[8] == preg_match('/^[T]{1}[A-Z0-9]{8}$/', $str)) {
231 return 3;
232 } else {
233 return -3;
234 }
235 }
236
237 //Check NIE XYZ
238 if (preg_match('/^[XYZ]{1}/', $str)) {
239 if ($num[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', (int) substr(str_replace(array('X', 'Y', 'Z'), array('0', '1', '2'), $str), 0, 8) % 23, 1)) {
240 return 3;
241 } else {
242 return -3;
243 }
244 }
245
246 //Can not be verified
247 return -4;
248}
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
isValidSiren($siren)
Check the syntax validity of a SIREN.
isValidLuhn($str)
Check if a string passes the Luhn algorithm test.
isValidTinForPT($str)
Check the syntax validity of a Portuguese (PT) Tax Identification Number (TIN).
isValidSiret($siret)
Check the syntax validity of a SIRET.
isValidTinForES($str)
Check the syntax validity of a Spanish (ES) Tax Identification Number (TIN), where:
isValidTinForBE($str)
Check the syntax validity of a Belgium (BE) Tax Identification Number (TIN).
isValidTinForDZ($str)
Check the syntax validity of an Algerian (DZ) Tax Identification Number (TIN).