diff --git a/stdnum/fr/tva.py b/stdnum/fr/tva.py index 2638663a..25c059e8 100644 --- a/stdnum/fr/tva.py +++ b/stdnum/fr/tva.py @@ -41,6 +41,8 @@ Traceback (most recent call last): ... InvalidFormat: ... +>>> to_siren('Fr 40 303 265 045') +'303 265 045' """ from __future__ import annotations @@ -101,3 +103,18 @@ def is_valid(number: str) -> bool: return bool(validate(number)) except ValidationError: return False + + +def to_siren(number: str) -> str: + """Convert the VAT number to a SIREN number. + + The SIREN number is the 9 last digits of the VAT number. + """ + _siren: list[str] = [] + digit_count = 0 + for char in reversed(number): + if digit_count < 9: + _siren.insert(0, char) + if isdigits(char): + digit_count += 1 + return ''.join(_siren) diff --git a/tests/test_fr_tva.doctest b/tests/test_fr_tva.doctest new file mode 100644 index 00000000..71885773 --- /dev/null +++ b/tests/test_fr_tva.doctest @@ -0,0 +1,39 @@ +test_fr_tva.doctest - more detailed doctests for the stdnum.fr.tva module + +Copyright (C) 2025 + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA + + +This file contains more detailed doctests for the stdnum.fr.tva module. + +>>> from stdnum.fr import tva +>>> from stdnum.exceptions import * + + +>>> tva.validate('Fr 40 303 265 045') +'40303265045' +>>> tva.validate('23 334 175 221') +'23334175221' +>>> tva.validate('23334175221') +'23334175221' +>>> tva.validate('84 323 140 391') +Traceback (most recent call last): + ... +InvalidChecksum: ... +>>> tva.to_siren('Fr 40 303 265 045') +'303 265 045' +