From 96383a10a500ff805b7c21bf9b82c089ffde088a Mon Sep 17 00:00:00 2001 From: thibdrev Date: Fri, 12 Jan 2024 20:59:42 +0100 Subject: [PATCH] qual: move the function checking the syntax of Spanish Tax Identification Numbers (CIF, NIF, NIE) to profid.lib.php (#27472) * qual: Move the function checking Spanish Tax Identification Numbers from societe.class.php to profid.lib.php Create the function isValidTinForES($str) * qual: Update societe.class.php using function isValidTinForES($str) from profid.lib.php * phpcs --------- Co-authored-by: Laurent Destailleur --- htdocs/core/lib/profid.lib.php | 83 ++++++++++++++++++++++++++ htdocs/societe/class/societe.class.php | 70 +--------------------- 2 files changed, 84 insertions(+), 69 deletions(-) diff --git a/htdocs/core/lib/profid.lib.php b/htdocs/core/lib/profid.lib.php index 11e8a49d779..b80aacbf10b 100644 --- a/htdocs/core/lib/profid.lib.php +++ b/htdocs/core/lib/profid.lib.php @@ -161,3 +161,86 @@ function isValidTinForBE($str) return false; } } + + +/** + * Check the syntax validity of a Spanish (ES) Tax Identification Number (TIN), where: + * - NIF = Número de Identificación Fiscal + * - CIF = Código de Identificación Fiscal + * - NIE = Número de Identidad de Extranjero + * + * @param string $str TIN to check + * @return int 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 + * @since Dolibarr V20 + */ +function isValidTinForES($str) +{ + $str = trim($str); + $str = preg_replace('/(\s)/', '', $str); + $str = strtoupper($str); + + //Check format + 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)) { + return 0; + } + + $num = array(); + for ($i = 0; $i < 9; $i++) { + $num[$i] = substr($str, $i, 1); + } + + //Check NIF + if (preg_match('/(^[0-9]{8}[A-Z]{1}$)/', $str)) { + if ($num[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', substr($str, 0, 8) % 23, 1)) { + return 1; + } else { + return -1; + } + } + + //algorithm checking type code CIF + $sum = $num[2] + $num[4] + $num[6]; + for ($i = 1; $i < 8; $i += 2) { + $sum += intval(substr((2 * $num[$i]), 0, 1)) + intval(substr((2 * $num[$i]), 1, 1)); + } + $n = 10 - substr($sum, strlen($sum) - 1, 1); + + //Check special NIF + if (preg_match('/^[KLM]{1}/', $str)) { + if ($num[8] == chr(64 + $n) || $num[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', substr($str, 1, 8) % 23, 1)) { + return 1; + } else { + return -1; + } + } + + //Check CIF + if (preg_match('/^[ABCDEFGHJNPQRSUVW]{1}/', $str)) { + if ($num[8] == chr(64 + $n) || $num[8] == substr($n, strlen($n) - 1, 1)) { + return 2; + } else { + return -2; + } + } + + //Check NIE T + if (preg_match('/^[T]{1}/', $str)) { + if ($num[8] == preg_match('/^[T]{1}[A-Z0-9]{8}$/', $str)) { + return 3; + } else { + return -3; + } + } + + //Check NIE XYZ + if (preg_match('/^[XYZ]{1}/', $str)) { + if ($num[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', substr(str_replace(array('X', 'Y', 'Z'), array('0', '1', '2'), $str), 0, 8) % 23, 1)) { + return 3; + } else { + return -3; + } + } + + //Can not be verified + return -4; +} diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index 0e569927ee1..bbf4d2008ad 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -3857,76 +3857,8 @@ class Societe extends CommonObject } //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 if ($idprof == 1 && $soc->country_code == 'ES') { - $string = trim($this->idprof1); - $string = preg_replace('/(\s)/', '', $string); - $string = strtoupper($string); - - //Check format - 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}$)/', $string)) { - return 0; - } - - $num = array(); - for ($i = 0; $i < 9; $i++) { - $num[$i] = substr($string, $i, 1); - } - - //Check NIF - if (preg_match('/(^[0-9]{8}[A-Z]{1}$)/', $string)) { - if ($num[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', substr($string, 0, 8) % 23, 1)) { - return 1; - } else { - return -1; - } - } - - //algorithm checking type code CIF - $sum = $num[2] + $num[4] + $num[6]; - for ($i = 1; $i < 8; $i += 2) { - $sum += intval(substr((2 * $num[$i]), 0, 1)) + intval(substr((2 * $num[$i]), 1, 1)); - } - $n = 10 - substr($sum, strlen($sum) - 1, 1); - - //Check special NIF - if (preg_match('/^[KLM]{1}/', $string)) { - if ($num[8] == chr(64 + $n) || $num[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', substr($string, 1, 8) % 23, 1)) { - return 1; - } else { - return -1; - } - } - - //Check CIF - if (preg_match('/^[ABCDEFGHJNPQRSUVW]{1}/', $string)) { - if ($num[8] == chr(64 + $n) || $num[8] == substr($n, strlen($n) - 1, 1)) { - return 2; - } else { - return -2; - } - } - - //Check NIE T - if (preg_match('/^[T]{1}/', $string)) { - if ($num[8] == preg_match('/^[T]{1}[A-Z0-9]{8}$/', $string)) { - return 3; - } else { - return -3; - } - } - - //Check NIE XYZ - if (preg_match('/^[XYZ]{1}/', $string)) { - if ($num[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', substr(str_replace(array('X', 'Y', 'Z'), array('0', '1', '2'), $string), 0, 8) % 23, 1)) { - return 3; - } else { - return -3; - } - } - - //Can not be verified - return -4; + return isValidTinForES($this->idprof1); } //Verify NIF if country is PT