Skip to content

Commit b3a42a1

Browse files
fmichel-adistaarthurdejong
authored andcommitted
Add fr.tva.to_siren() function
The function can convert a French VAT number to a SIREN. Closes #480
1 parent 843bbec commit b3a42a1

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

stdnum/fr/tva.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
Traceback (most recent call last):
4242
...
4343
InvalidFormat: ...
44+
>>> to_siren('Fr 40 303 265 045')
45+
'303265045'
4446
"""
4547

4648
from __future__ import annotations
@@ -101,3 +103,15 @@ def is_valid(number: str) -> bool:
101103
return bool(validate(number))
102104
except ValidationError:
103105
return False
106+
107+
108+
def to_siren(number: str) -> str:
109+
"""Convert the VAT number to a SIREN number.
110+
111+
The SIREN number is the 9 last digits of the VAT number.
112+
"""
113+
number = compact(number)
114+
if number[2:5] == '000':
115+
# numbers from Monaco are valid TVA but not SIREN
116+
raise InvalidComponent()
117+
return number[2:]

tests/test_fr_tva.doctest

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
test_fr_tva.doctest - more detailed doctests for the stdnum.fr.tva module
2+
3+
Copyright (C) 2025 Fabien Michel
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18+
02110-1301 USA
19+
20+
21+
This file contains more detailed doctests for the stdnum.fr.tva module.
22+
23+
>>> from stdnum.fr import tva
24+
>>> from stdnum.exceptions import *
25+
26+
27+
>>> tva.validate('Fr 40 303 265 045')
28+
'40303265045'
29+
>>> tva.validate('23 334 175 221')
30+
'23334175221'
31+
>>> tva.validate('23334175221')
32+
'23334175221'
33+
>>> tva.validate('84 323 140 391')
34+
Traceback (most recent call last):
35+
...
36+
InvalidChecksum: ...
37+
>>> tva.to_siren('Fr 40 303 265 045')
38+
'303265045'
39+
>>> tva.to_siren('23 334 175 221')
40+
'334175221'
41+
>>> tva.to_siren('FR 53 0000 04605') # Monaco VAT code canot be converted
42+
Traceback (most recent call last):
43+
...
44+
InvalidComponent: ...

0 commit comments

Comments
 (0)