diff --git a/htdocs/core/lib/profid.lib.php b/htdocs/core/lib/profid.lib.php index 98990266a42..f167a8f3713 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; +} diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index 5a8f96fbde4..e999747f1e1 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -3846,42 +3846,14 @@ 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