55from plone .z3cform import layout
66from zope import schema
77from zope .interface import Interface
8+ from zope .interface import Invalid
9+
10+
11+ def validate_vat_number (va_nb ):
12+ """Validate the VAT number format. It should start with BE followed by 10 digits,
13+ with the last 2 digits being a control checksum of the first 8 digits."""
14+ if not va_nb :
15+ return True
16+
17+ # Check format: BE followed by 10 digits
18+ if not va_nb .startswith ('BE' ):
19+ raise Invalid (_ ("VAT number must start with 'BE'" ))
20+
21+ if len (va_nb ) != 12 :
22+ raise Invalid (_ ("VAT number must be 12 characters (BE + 10 digits)" ))
23+
24+ digits = va_nb [2 :]
25+ if not digits .isdigit ():
26+ raise Invalid (_ ("VAT number must contain 10 digits after 'BE'" ))
27+
28+ # Validate checksum: last 2 digits should be 97 - (first 8 digits modulo 97)
29+ first_eight = int (digits [:8 ])
30+ control = int (digits [8 :10 ])
31+ expected_control = 97 - (first_eight % 97 )
32+
33+ if control != expected_control :
34+ raise Invalid (_ ("Invalid VAT number: checksum verification failed" ))
35+
36+ return True
837
938
1039class IImioEsignSettings (Interface ):
@@ -21,6 +50,13 @@ class IImioEsignSettings(Interface):
2150 required = False ,
2251 )
2352
53+ vat_number = schema .TextLine (
54+ title = _ ("VAT number" ),
55+ description = _ ("VAT number used for esign billing (BE0123456789)." ),
56+ constraint = validate_vat_number ,
57+ required = True ,
58+ )
59+
2460
2561class ImioEsignSettings (RegistryEditForm ):
2662 schema = IImioEsignSettings
0 commit comments