Merge pull request #27329 from thibdrev/thibdrev-patch-1

qual move the code used to validate a SIRET into profid.lib.php
This commit is contained in:
Laurent Destailleur 2024-01-10 15:03:42 +01:00 committed by GitHub
commit da56b40ab5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 32 deletions

View file

@ -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;
}

View file

@ -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