From 6baada7f2f9a9f691adc85654bedb80a714ea500 Mon Sep 17 00:00:00 2001 From: thibdrev Date: Mon, 8 Jan 2024 12:06:53 +0100 Subject: [PATCH 1/4] Update societe.class.php - Use isValidSiret() --- htdocs/societe/class/societe.class.php | 37 ++++---------------------- 1 file changed, 5 insertions(+), 32 deletions(-) diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index a5dc591b4e4..38046c52920 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -3847,43 +3847,16 @@ class Societe extends CommonObject return 1; } - // Check SIREN if country FR + // Check SIREN if ($idprof == 1 && $soc->country_code == 'FR' && !isValidSiren($this->idprof1)) { return -1; } - // Verifie SIRET si pays FR - if ($idprof == 2 && $soc->country_code == 'FR') { - $chaine = trim($this->idprof2); - $chaine = preg_replace('/(\s)/', '', $chaine); - - if (!is_numeric($chaine)) { - return -1; - } - if (dol_strlen($chaine) != 14) { - return -1; - } - - // on prend chaque chiffre un par un - // si son index (position dans la chaîne en commence à 0 au premier caractère) est pair - // on double sa valeur et si cette dernière est supérieure à 9, on lui retranche 9 - // on ajoute cette valeur à la somme totale - $sum = 0; - for ($index = 0; $index < 14; $index++) { - $number = (int) $chaine[$index]; - if (($index % 2) == 0) { - if (($number *= 2) > 9) { - $number -= 9; - } - } - $sum += $number; - } - - // le numéro est valide si la somme des chiffres est multiple de 10 - if ($sum % 10 != 0) { - return -1; - } + // Check SIRET + if ($idprof == 2 && $soc->country_code == 'FR' && !isValidSiret($this->idprof2)) { + return -1; } + //Verify CIF/NIF/NIE if pays ES //Returns: 1 if NIF ok, 2 if CIF ok, 3 if NIE ok, -1 if NIF bad, -2 if CIF bad, -3 if NIE bad, 0 if unexpected bad From a7dc286784fd84da03978b72f35619ac91b40786 Mon Sep 17 00:00:00 2001 From: thibdrev Date: Mon, 8 Jan 2024 14:01:37 +0100 Subject: [PATCH 2/4] Update profid.lib.php - Copy the algorithm to validate a SIRET --- htdocs/core/lib/profid.lib.php | 40 ++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/htdocs/core/lib/profid.lib.php b/htdocs/core/lib/profid.lib.php index 98990266a42..413694ec83a 100644 --- a/htdocs/core/lib/profid.lib.php +++ b/htdocs/core/lib/profid.lib.php @@ -38,6 +38,8 @@ function isValidSiren($siren) return false; } + // TODO : use the Luhn algorithm instead + // we take each figure one by one and: // - if its index is odd then we double its value, // - if the latter is higher than 9 then we substract 9 from it, @@ -56,3 +58,41 @@ function isValidSiren($siren) // the siren is valid if the sum is a multiple of 10 return (($sum % 10) == 0) ? true : false; } + + +/** + * Check the validity of a SIRET. + * + * @param string $siret SIRET to check + * @return boolean True if valid, False otherwise + */ +function isValidSiret($siret) +{ + $siret = trim($siret); + $siret = preg_replace('/(\s)/', '', $siret); + + if (!is_numeric($siret) || dol_strlen($siret) != 14) { + return false; + } + + // TODO: use the Luhn algorithm instead + // TODO: handle the exception of "La Poste" (356 000 000 #####) + + // we take each figure one by one and: + // - if its index is even then we double its value, + // - if the latter is higher than 9 then we substract 9 from it, + // - and finally we add the result to the overall sum. + $sum = 0; + for ($index = 0; $index < 14; $index++) { + $number = (int) $siret[$index]; + if ($index % 2 == 0) { + if (($number *= 2) > 9) { + $number -= 9; + } + } + $sum += $number; + } + + // the SIRET is valid if the sum is a multiple of 10 + return $sum % 10 == 0; +} From 37871169ba83f5d3a37fb52ce044b1f42f13f0aa Mon Sep 17 00:00:00 2001 From: thibdrev Date: Mon, 8 Jan 2024 14:08:49 +0100 Subject: [PATCH 3/4] phpcs --- htdocs/core/lib/profid.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/lib/profid.lib.php b/htdocs/core/lib/profid.lib.php index 413694ec83a..f167a8f3713 100644 --- a/htdocs/core/lib/profid.lib.php +++ b/htdocs/core/lib/profid.lib.php @@ -67,7 +67,7 @@ function isValidSiren($siren) * @return boolean True if valid, False otherwise */ function isValidSiret($siret) -{ +{ $siret = trim($siret); $siret = preg_replace('/(\s)/', '', $siret); From 636d8ed01bceef84ff01f3ac2319a0f06b9171e3 Mon Sep 17 00:00:00 2001 From: thibdrev Date: Mon, 8 Jan 2024 14:10:13 +0100 Subject: [PATCH 4/4] phpcs --- htdocs/societe/class/societe.class.php | 1 - 1 file changed, 1 deletion(-) diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index 38046c52920..de44b1fcb79 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -3856,7 +3856,6 @@ class Societe extends CommonObject if ($idprof == 2 && $soc->country_code == 'FR' && !isValidSiret($this->idprof2)) { return -1; } - //Verify CIF/NIF/NIE if pays ES //Returns: 1 if NIF ok, 2 if CIF ok, 3 if NIE ok, -1 if NIF bad, -2 if CIF bad, -3 if NIE bad, 0 if unexpected bad