diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index bc88a0c0ae6..a043494c693 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -5388,6 +5388,31 @@ class Societe extends CommonObject } + /** + * Calculate VAT intracommunity number for a thirdparty if missing, from the professional ID + * + * @param mixed $thirdparty A thirdparty object + * @return string A VAT number + */ + public function calculateVATNumberFromProperties($thirdparty) + { + if ($thirdparty->country_code == 'FR' && empty($thirdparty->tva_intra) && !empty($thirdparty->tva_assuj)) { + $siren = trim($thirdparty->idprof1); + if (empty($siren)) { + $siren = (int) substr(str_replace(' ', '', $thirdparty->idprof2), 0, 9); + } + if (!empty($siren)) { + // [FR + code clé + numéro SIREN ] + // Key VAT = [12 + 3 × (SIREN modulo 97)] modulo 97 + $cle = (12 + 3 * $siren % 97) % 97; + $tva_intra = 'FR' . $cle . $siren; + } + } + + return $tva_intra ?? ''; + } + + /** * Create a document onto disk according to template module. *