dolibarr 24.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
60function isValidSiren($siren, $lengthonly = 0)
61{
62 $siren = trim($siren);
63 $siren = preg_replace('/(\s)/', '', $siren);
64
65 if (!is_numeric($siren) || dol_strlen($siren) != 9) {
66 return false;
67 }
68
69 return ($lengthonly || isValidLuhn($siren));
70}
71
72
81function isValidSiret($siret, $lengthonly = 0)
82{
83 $siret = trim($siret);
84 $siret = preg_replace('/(\s)/', '', $siret);
85
86 if (!is_numeric($siret) || dol_strlen($siret) != 14) {
87 return false;
88 }
89
90 if ($lengthonly || isValidLuhn($siret)) {
91 return true;
92 } elseif ((substr($siret, 0, 9) == "356000000") && (array_sum(str_split($siret)) % 5 == 0)) {
98 return true;
99 } else {
100 return false;
101 }
102}
103
104
113function isValidTinForPT($str)
114{
115 $str = trim($str);
116 $str = preg_replace('/(\s)/', '', $str);
117
118 if (preg_match('/(^[0-9]{9}$)/', $str)) {
119 return true;
120 } else {
121 return false;
122 }
123}
124
125
134function isValidTinForDZ($str)
135{
136 $str = trim($str);
137 $str = preg_replace('/(\s)/', '', $str);
138
139 if (preg_match('/(^[0-9]{15}$)/', $str)) {
140 return true;
141 } else {
142 return false;
143 }
144}
145
146
155function isValidTinForBE($str)
156{
157 // https://economie.fgov.be/fr/themes/entreprises/banque-carrefour-des/actualites/structure-du-numero
158 $str = trim($str);
159 $str = preg_replace('/(\s)/', '', $str);
160
161 if (preg_match('/(^[0-1]{1}[0-9]{3}\.[0-9]{3}\.[0-9]{3}$)/', $str)) {
162 return true;
163 } else {
164 return false;
165 }
166}
167
168
179function isValidTinForES($str)
180{
181 $str = trim($str);
182 $str = preg_replace('/(\s)/', '', $str);
183 $str = strtoupper($str);
184
185 //Check format
186 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)) {
187 return 0;
188 }
189
190 $num = array();
191 for ($i = 0; $i < 9; $i++) {
192 $num[$i] = substr($str, $i, 1);
193 }
194 '@phan-var-force array<int<0,8>,string> $num';
195
196 //Check NIF
197 if (preg_match('/(^[0-9]{8}[A-Z]{1}$)/', $str)) {
198 if ($num[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', (int) substr($str, 0, 8) % 23, 1)) {
199 return 1;
200 } else {
201 return -1;
202 }
203 }
204
205 //algorithm checking type code CIF
206 $sum = (int) $num[2] + (int) $num[4] + (int) $num[6];
207 for ($i = 1; $i < 8; $i += 2) {
208 $sum += intval(substr((string) (2 * (int) $num[$i]), 0, 1)) + intval(substr((string) (2 * (int) $num[$i]), 1, 1));
209 }
210 $n = 10 - (int) substr((string) $sum, strlen((string) $sum) - 1, 1);
211
212 //Check special NIF
213 if (preg_match('/^[KLM]{1}/', $str)) {
214 if ($num[8] == chr(64 + $n) || $num[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', (int) substr($str, 1, 8) % 23, 1)) {
215 return 1;
216 } else {
217 return -1;
218 }
219 }
220
221 //Check CIF
222 if (preg_match('/^[ABCDEFGHJNPQRSUVW]{1}/', $str)) {
223 if ($num[8] == chr(64 + $n) || $num[8] == substr((string) $n, strlen((string) $n) - 1, 1)) {
224 return 2;
225 } else {
226 return -2;
227 }
228 }
229
230 //Check NIE T
231 if (preg_match('/^[T]{1}/', $str)) {
232 if ($num[8] == preg_match('/^[T]{1}[A-Z0-9]{8}$/', $str)) {
233 return 3;
234 } else {
235 return -3;
236 }
237 }
238
239 //Check NIE XYZ
240 if (preg_match('/^[XYZ]{1}/', $str)) {
241 if ($num[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', (int) substr(str_replace(array('X', 'Y', 'Z'), array('0', '1', '2'), $str), 0, 8) % 23, 1)) {
242 return 3;
243 } else {
244 return -3;
245 }
246 }
247
248 //Can not be verified
249 return -4;
250}
251
252
261function isValidProfIds($idprof, $thirdparty, $lenghtonly = 0)
262{
263 $ok = 1;
264
265 if (getDolGlobalString('MAIN_DISABLEPROFIDRULES')) {
266 return 1;
267 }
268
269 // Check SIREN
270 if ($thirdparty->country_code == 'FR') {
271 if ($idprof == 1 && !isValidSiren($thirdparty->idprof1, $lenghtonly)) {
272 return -1;
273 }
274
275 // Check SIRET
276 if ($idprof == 2 && !isValidSiret($thirdparty->idprof2, $lenghtonly)) {
277 return -2;
278 }
279 }
280
281 // Verify CIF/NIF/NIE if pays ES
282 if ($idprof == 1 && $thirdparty->country_code == 'ES') {
283 return isValidTinForES($thirdparty->idprof1);
284 }
285
286 // Verify NIF if country is PT
287 if ($idprof == 1 && $thirdparty->country_code == 'PT' && !isValidTinForPT($thirdparty->idprof1)) {
288 return -1;
289 }
290
291 // Verify NIF if country is DZ
292 if ($idprof == 1 && $thirdparty->country_code == 'DZ' && !isValidTinForDZ($thirdparty->idprof1)) {
293 return -1;
294 }
295
296 // Verify ID Prof 1 if country is BE
297 if ($idprof == 1 && $thirdparty->country_code == 'BE' && !isValidTinForBE($thirdparty->idprof1)) {
298 return -1;
299 }
300
301 return $ok;
302}
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string
buildzip.php
isValidSiren($siren, $lengthonly=0)
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).
isValidTinForES($str)
Check the syntax validity of a Spanish (ES) Tax Identification Number (TIN), where:
isValidProfIds($idprof, $thirdparty, $lenghtonly=0)
Check the validity of a professional identifier according to the properties (country) of the company ...
isValidTinForBE($str)
Check the syntax validity of a Belgium (BE) Tax Identification Number (TIN).
isValidSiret($siret, $lengthonly=0)
Check the syntax validity of a SIRET.
isValidTinForDZ($str)
Check the syntax validity of an Algerian (DZ) Tax Identification Number (TIN).