1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 # Copyright (C) 2008-2009 B2CK, Cedric Krier, Bertrand Chenal (the methods "check_vat_[a-z]{2}"
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
24 from osv import osv, fields
25 from tools.translate import _
28 'be': 'BE0477472701', 'at': 'ATU12345675',
29 'bg': 'BG1234567892', 'cy': 'CY12345678F',
30 'cz': 'CZ12345679', 'de': 'DE123456788',
31 'dk': 'DK12345674', 'ee': 'EE123456780',
32 'es': 'ESA12345674', 'fi': 'FI12345671',
33 'fr': 'FR32123456789', 'gb': 'GB123456782',
34 'gr': 'GR12345670', 'hu': 'HU12345676',
35 'ie': 'IE1234567T', 'it': 'IT12345670017',
36 'lt': 'LT123456715', 'lu': 'LU12345613',
37 'lv': 'LV41234567891', 'mt': 'MT12345634',
38 'nl': 'NL123456782B90', 'pl': 'PL1234567883',
39 'pt': 'PT123456789', 'ro': 'RO1234567897',
40 'se': 'SE123456789701', 'si': 'SI12345679',
41 'sk': 'SK0012345675', 'el': 'EL12345670'
45 """Sum each digits of the multiplication of i and j."""
46 return reduce(lambda x, y: x + int(y), str(i*j), 0)
48 class res_partner(osv.osv):
49 _inherit = 'res.partner'
51 def _split_vat(self, vat):
52 vat_country, vat_number = vat[:2].lower(), vat[2:].replace(' ', '')
53 return vat_country, vat_number
55 def check_vat(self, cr, uid, ids):
57 Check the VAT number depending of the country.
58 http://sima-pc.com/nif.php
60 for partner in self.browse(cr, uid, ids):
63 vat_country, vat_number = self._split_vat(partner.vat)
64 if not hasattr(self, 'check_vat_' + vat_country):
66 check = getattr(self, 'check_vat_' + vat_country)
67 if not check(vat_number):
71 def vat_change(self, cr, uid, ids, value, context=None):
72 return {'value': {'vat_subjected': bool(value)}}
75 'vat_subjected': fields.boolean('VAT Legal Statement', help="Check this box if the partner is subjected to the VAT. It will be used for the VAT legal statement.")
78 def _construct_constraint_msg(self, cr, uid, ids):
79 def default_vat_check(cn, vn):
80 # by default, a VAT number is valid if:
81 # it starts with 2 letters
82 # has more than 3 characters
83 return cn[0] in string.ascii_lowercase and cn[1] in string.ascii_lowercase
84 vat_country, vat_number = self._split_vat(self.browse(cr, uid, ids)[0].vat)
85 if default_vat_check(vat_country, vat_number):
86 vat_no = vat_country in _ref_vat and _ref_vat[vat_country] or 'Country Code + Vat Number'
87 return _('The Vat does not seems to be correct. You should have entered something like this %s'), (vat_no)
88 return _('The VAT is invalid, It should begin with the country code'), ()
90 _constraints = [(check_vat, _construct_constraint_msg, ["vat"])]
92 # code from the following methods come from Tryton (B2CK)
93 # http://www.tryton.org/hgwebdir.cgi/modules/relationship/file/544d1de586d9/party.py
94 def check_vat_at(self, vat):
96 Check Austria VAT number.
107 sum = int(num[0]) + mult_add(2, int(num[1])) + \
108 int(num[2]) + mult_add(2, int(num[3])) + \
109 int(num[4]) + mult_add(2, int(num[5])) + \
111 check = 10 - ((sum + 4) % 10)
114 if int(vat[-1:]) != check:
118 def check_vat_be(self, vat):
120 Check Belgium VAT number.
128 if int(vat[-2:]) != \
129 97 - (int(vat[:8]) % 97):
133 def check_vat_bg(self, vat):
135 Check Bulgaria VAT number.
137 if len(vat) not in [9,10]:
143 if int(vat[0]) in (2, 3) and \
146 sum = 4 * int(vat[0]) + 3 * int(vat[1]) + 2 * int(vat[2]) + \
147 7 * int(vat[3]) + 6 * int(vat[4]) + 5 * int(vat[5]) + \
148 4 * int(vat[6]) + 3 * int(vat[7]) + 2 * int(vat[8])
149 check = 11 - (sum % 11)
154 def check_vat_cy(self, vat):
156 Check Cyprus VAT number.
200 sum = n0 + n1 + n2 + n3 + n4 + n5 + n6 + n7
201 check = chr(sum % 26 + 65)
206 def check_vat_cz(self, vat):
208 Check Czech Republic VAT number.
210 if len(vat) not in (8, 9, 10):
218 if int(vat[0]) not in (0, 1, 2, 3, 4, 5, 6, 7, 8):
220 sum = 8 * int(vat[0]) + 7 * int(vat[1]) + 6 * int(vat[2]) + \
221 5 * int(vat[3]) + 4 * int(vat[4]) + 3 * int(vat[5]) + \
223 check = 11 - (sum % 11)
228 if check != int(vat[7]):
230 elif len(vat) == 9 and int(vat[0]) == 6:
231 sum = 8 * int(vat[1]) + 7 * int(vat[2]) + 6 * int(vat[3]) + \
232 5 * int(vat[4]) + 4 * int(vat[5]) + 3 * int(vat[6]) + \
234 check = 9 - ((11 - (sum % 11)) % 10)
235 if check != int(vat[8]):
238 if int(vat[0:2]) > 53 and int(vat[0:2]) < 80:
240 if int(vat[2:4]) < 1:
242 if int(vat[2:4]) > 12 and int(vat[2:4]) < 51:
244 if int(vat[2:4]) > 62:
246 if int(vat[2:4]) in (2, 52) and int(vat[0:2]) % 4 > 0:
247 if int(vat[4:6]) < 1:
249 if int(vat[4:6]) > 28:
251 if int(vat[2:4]) in (2, 52) and int(vat[0:2]) % 4 == 0:
252 if int(vat[4:6]) < 1:
254 if int(vat[4:6]) > 29:
256 if int(vat[2:4]) in (4, 6, 9, 11, 54, 56, 59, 61):
257 if int(vat[4:6]) < 1:
259 if int(vat[4:6]) > 30:
261 if int(vat[2:4]) in (1, 3, 5, 7, 8, 10, 12, 51,
262 53, 55, 57, 58, 60, 62):
263 if int(vat[4:6]) < 1:
265 if int(vat[4:6]) > 31:
268 if int(vat[0:2]) < 54:
270 if int(vat[2:4]) < 1:
272 if int(vat[2:4]) > 12 and int(vat[2:4]) < 51:
274 if int(vat[2:4]) > 62:
276 if int(vat[2:4]) in (2, 52) and int(vat[0:2]) % 4 > 0:
277 if int(vat[4:6]) < 1:
279 if int(vat[4:6]) > 28:
281 if int(vat[2:4]) in (2, 52) and int(vat[0:2]) % 4 == 0:
282 if int(vat[4:6]) < 1:
284 if int(vat[4:6]) > 29:
286 if int(vat[2:4]) in (4, 6, 9, 11, 54, 56, 59, 61):
287 if int(vat[4:6]) < 1:
289 if int(vat[4:6]) > 30:
291 if int(vat[2:4]) in (1, 3, 5, 7, 8, 10, 12, 51,
292 53, 55, 57, 58, 60, 62):
293 if int(vat[4:6]) < 1:
295 if int(vat[4:6]) > 31:
297 if (int(vat[0:2]) + int(vat[2:4]) + int(vat[4:6]) + int(vat[6:8]) +
298 int(vat[8:10])) % 11 != 0:
300 if int(vat[0:10]) % 11 != 0:
304 def check_vat_de(self, vat):
306 Check Germany VAT number.
314 if int(vat[0:7]) <= 0:
318 sum = (2 * ((int(vat[i]) + sum + 9) % 10 + 1)) % 11
322 if int(vat[8]) != check:
326 def check_vat_dk(self, vat):
328 Check Denmark VAT number.
338 sum = 2 * int(vat[0]) + 7 * int(vat[1]) + 6 * int(vat[2]) + \
339 5 * int(vat[3]) + 4 * int(vat[4]) + 3 * int(vat[5]) + \
340 2 * int(vat[6]) + int(vat[7])
345 def check_vat_ee(self, vat):
347 Check Estonia VAT number.
355 sum = 3 * int(vat[0]) + 7 * int(vat[1]) + 1 * int(vat[2]) + \
356 3 * int(vat[3]) + 7 * int(vat[4]) + 1 * int(vat[5]) + \
357 3 * int(vat[6]) + 7 * int(vat[7])
358 check = 10 - (sum % 10)
361 if check != int(vat[8]):
365 def check_vat_es(self, vat):
367 Check Spain VAT number.
397 #Legal persons with profit aim
398 if vat[0] in ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'U', 'V'):
403 sum = mult_add(2, int(vat[1])) + int(vat[2]) + \
404 mult_add(2, int(vat[3])) + int(vat[4]) + \
405 mult_add(2, int(vat[5])) + int(vat[6]) + \
406 mult_add(2, int(vat[7]))
407 check = 10 - (sum % 10)
411 #Legal persons with non-profit aim
412 elif vat[0] in ('N', 'P', 'Q', 'R', 'S', 'W'):
417 sum = mult_add(2, int(vat[1])) + int(vat[2]) + \
418 mult_add(2, int(vat[3])) + int(vat[4]) + \
419 mult_add(2, int(vat[5])) + int(vat[6]) + \
420 mult_add(2, int(vat[7]))
421 check = 10 - (sum % 10)
422 check = chr(check + 64)
426 #Foreign natural persons, under age 14 or non-residents
427 elif vat[0] in ('K', 'L', 'M', 'X', 'Y', 'Z'):
429 check_value = '1' + vat[1:8]
431 check_value = '2' + vat[1:8]
433 check_value = vat[1:8]
439 check = 1 + (int(check_value) % 23)
445 #Spanish natural persons
451 check = 1 + (int(vat[:8]) % 23)
458 def check_vat_fi(self, vat):
460 Check Finland VAT number.
468 sum = 7 * int(vat[0]) + 9 * int(vat[1]) + 10 * int(vat[2]) + \
469 5 * int(vat[3]) + 8 * int(vat[4]) + 4 * int(vat[5]) + \
471 check = 11 - (sum % 11)
476 if check != int(vat[7]):
480 def check_vat_fr(self, vat):
482 Check France VAT number.
500 check = ((int(vat[2:11]) * 100) + 12) % 97
501 if check != int(vat[0:2]):
505 conv = ['0', '1', '2', '3', '4', '5', '6', '7',
506 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
507 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T',
508 'U', 'V', 'W', 'X', 'Y', 'Z']
509 if vat[0] not in conv \
510 or vat[1] not in conv:
512 c1 = conv.index(vat[0])
513 c2 = conv.index(vat[1])
516 sum = c1 * 24 + c2 - 10
518 sum = c1 * 34 + c2 - 100
521 sum = (int(sum) / 11) + 1
522 y = (int(vat[2:11]) + sum) % 11
527 def check_vat_gb(self, vat):
529 Check United Kingdom VAT number.
539 if int(vat[2:5]) >= 500:
543 if int(vat[2:5]) < 500:
547 elif len(vat) in (9, 10):
553 if int(vat[0:7]) < 1:
555 if int(vat[0:7]) > 19999 and int(vat[0:7]) < 1000000:
557 if int(vat[7:9]) > 97:
559 if len(vat) == 10 and int(vat[9]) != 3:
562 sum = 8 * int(vat[0]) + 7 * int(vat[1]) + 6 * int(vat[2]) + \
563 5 * int(vat[3]) + 4 * int(vat[4]) + 3 * int(vat[5]) + \
564 2 * int(vat[6]) + 10 * int(vat[7]) + int(vat[8])
568 elif len(vat) in (12, 13):
574 if int(vat[0:3]) not in (0, 1):
577 if int(vat[3:10]) < 1:
579 if int(vat[3:10]) > 19999 and int(vat[3:10]) < 1000000:
581 if int(vat[10:12]) > 97:
583 if len(vat) == 13 and int(vat[12]) != 3:
586 sum = 8 * int(vat[3]) + 7 * int(vat[4]) + 6 * int(vat[5]) + \
587 5 * int(vat[6]) + 4 * int(vat[7]) + 3 * int(vat[8]) + \
588 2 * int(vat[9]) + 10 * int(vat[10]) + int(vat[11])
594 def check_vat_gr(self, vat):
596 Check Greece VAT number.
603 sum = 128 * int(vat[0]) + 64 * int(vat[1]) + 32 * int(vat[2]) + \
604 16 * int(vat[3]) + 8 * int(vat[4]) + 4 * int(vat[5]) + \
609 if check != int(vat[7]):
613 sum = 256 * int(vat[0]) + 128 * int(vat[1]) + 64 * int(vat[2]) + \
614 32 * int(vat[3]) + 16 * int(vat[4]) + 8 * int(vat[5]) + \
615 4 * int(vat[6]) + 2 * int(vat[7])
619 if check != int(vat[8]):
624 def check_vat_el(self, vat):
625 return self.check_vat_gr(vat)
627 def check_vat_hu(self, vat):
629 Check Hungary VAT number.
639 sum = 9 * int(vat[0]) + 7 * int(vat[1]) + 3 * int(vat[2]) + \
640 1 * int(vat[3]) + 9 * int(vat[4]) + 7 * int(vat[5]) + \
642 check = 10 - (sum % 10)
645 if check != int(vat[7]):
649 def check_vat_ie(self, vat):
651 Check Ireland VAT number.
655 if (ord(vat[1]) >= 65 and ord(vat[1]) <= 90) \
656 or vat[1] in ('+', '*'):
666 sum = 7 * int(vat[2]) + 6 * int(vat[3]) + 5 * int(vat[4]) + \
667 4 * int(vat[5]) + 3 * int(vat[6]) + 2 * int(vat[0])
672 check = chr(check + 64)
682 sum = 8 * int(vat[0]) + 7 * int(vat[1]) + 6 * int(vat[2]) + \
683 5 * int(vat[3]) + 4 * int(vat[4]) + 3 * int(vat[5]) + \
689 check = chr(check + 64)
694 def check_vat_it(self, vat):
696 Check Italy VAT number.
704 if int(vat[0:7]) <= 0:
706 if int(vat[7:10]) <= 0:
708 if int(vat[7:10]) > 100 and int(vat[7:10]) < 120:
711 sum = int(vat[0]) + mult_add(2, int(vat[1])) + int(vat[2]) + \
712 mult_add(2, int(vat[3])) + int(vat[4]) + \
713 mult_add(2, int(vat[5])) + int(vat[6]) + \
714 mult_add(2, int(vat[7])) + int(vat[8]) + \
715 mult_add(2, int(vat[9]))
716 check = 10 - (sum % 10)
719 if check != int(vat[10]):
723 def check_vat_lt(self, vat):
725 Check Lithuania VAT number.
735 sum = 1 * int(vat[0]) + 2 * int(vat[1]) + 3 * int(vat[2]) + \
736 4 * int(vat[3]) + 5 * int(vat[4]) + 6 * int(vat[5]) + \
737 7 * int(vat[6]) + 8 * int(vat[7])
739 sum = 3 * int(vat[0]) + 4 * int(vat[1]) + 5 * int(vat[2]) + \
740 6 * int(vat[3]) + 7 * int(vat[4]) + 8 * int(vat[5]) + \
741 9 * int(vat[6]) + 1 * int(vat[7])
745 if check != int(vat[8]):
749 if int(vat[10]) != 1:
751 sum = 1 * int(vat[0]) + 2 * int(vat[1]) + 3 * int(vat[2]) + \
752 4 * int(vat[3]) + 5 * int(vat[4]) + 6 * int(vat[5]) + \
753 7 * int(vat[6]) + 8 * int(vat[7]) + 9 * int(vat[8]) + \
754 1 * int(vat[9]) + 2 * int(vat[10])
756 sum = 3 * int(vat[0]) + 4 * int(vat[1]) + 5 * int(vat[2]) + \
757 6 * int(vat[3]) + 7 * int(vat[4]) + 8 * int(vat[5]) + \
758 9 * int(vat[6]) + 1 * int(vat[7]) + 2 * int(vat[8]) + \
759 3 * int(vat[9]) + 4 * int(vat[10])
763 if check != int(vat[11]):
768 def check_vat_lu(self, vat):
770 Check Luxembourg VAT number.
778 if int(vat[0:6]) <= 0:
780 check = int(vat[0:6]) % 89
781 if check != int(vat[6:8]):
785 def check_vat_lv(self, vat):
787 Check Latvia VAT number.
796 sum = 9 * int(vat[0]) + 1 * int(vat[1]) + 4 * int(vat[2]) + \
797 8 * int(vat[3]) + 3 * int(vat[4]) + 10 * int(vat[5]) + \
798 2 * int(vat[6]) + 5 * int(vat[7]) + 7 * int(vat[8]) + \
800 if sum % 11 == 4 and int(vat[0]) == 9:
803 check = 4 - (sum % 11)
805 check = 14 - (sum % 11)
807 check = 3 - (sum % 11)
808 if check != int(vat[10]):
812 if int(vat[2:4]) == 2 and int(vat[4:6]) % 4 > 0:
813 if int(vat[0:2]) < 1 or int(vat[0:2]) > 28:
815 if int(vat[2:4]) == 2 and int(vat[4:6]) % 4 == 0:
816 if int(vat[0:2]) < 1 or int(vat[0:2]) > 29:
818 if int(vat[2:4]) in (4, 6, 9, 11):
819 if int(vat[0:2]) < 1 or int(vat[0:2]) > 30:
821 if int(vat[2:4]) in (1, 3, 5, 7, 8, 10, 12):
822 if int(vat[0:2]) < 1 or int(vat[0:2]) > 31:
824 if int(vat[2:4]) < 1 or int(vat[2:4]) > 12:
828 def check_vat_mt(self, vat):
830 Check Malta VAT number.
839 if int(vat[0:6]) < 100000:
842 sum = 3 * int(vat[0]) + 4 * int(vat[1]) + 6 * int(vat[2]) + \
843 7 * int(vat[3]) + 8 * int(vat[4]) + 9 * int(vat[5])
844 check = 37 - (sum % 37)
845 if check != int(vat[6:8]):
849 def check_vat_nl(self, vat):
851 Check Netherlands VAT number.
860 if int(vat[0:8]) <= 0:
865 sum = 9 * int(vat[0]) + 8 * int(vat[1]) + 7 * int(vat[2]) + \
866 6 * int(vat[3]) + 5 * int(vat[4]) + 4 * int(vat[5]) + \
867 3 * int(vat[6]) + 2 * int(vat[7])
872 if check != int(vat[8]):
876 def check_vat_pl(self, vat):
878 Check Poland VAT number.
887 sum = 6 * int(vat[0]) + 5 * int(vat[1]) + 7 * int(vat[2]) + \
888 2 * int(vat[3]) + 3 * int(vat[4]) + 4 * int(vat[5]) + \
889 5 * int(vat[6]) + 6 * int(vat[7]) + 7 * int(vat[8])
893 if check != int(vat[9]):
897 def check_vat_pt(self, vat):
899 Check Portugal VAT number.
911 sum = 9 * int(vat[0]) + 8 * int(vat[1]) + 7 * int(vat[2]) + \
912 6 * int(vat[3]) + 5 * int(vat[4]) + 4 * int(vat[5]) + \
913 3 * int(vat[6]) + 2 * int(vat[7])
914 check = 11 - (sum % 11)
915 if check == 10 or check == 11:
919 def check_vat_ro(self, vat):
921 Check Romania VAT number.
928 if len(vat) >= 2 and len(vat) <= 10:
929 vat = (10 - len(vat)) * '0' + vat
930 sum = 7 * int(vat[0]) + 5 * int(vat[1]) + 3 * int(vat[2]) + \
931 2 * int(vat[3]) + 1 * int(vat[4]) + 7 * int(vat[5]) + \
932 5 * int(vat[6]) + 3 * int(vat[7]) + 2 * int(vat[8])
933 check = (sum * 10) % 11
936 if check != int(vat[9]):
940 if int(vat[0]) not in (1, 2, 3, 4, 6):
942 if int(vat[3:5]) < 1 or int(vat[3:5]) > 12:
944 if int(vat[3:5]) == 2 and int(vat[1:3]) % 4 > 0:
945 if int(vat[5:7]) < 1 or int(vat[5:7]) > 28:
947 if int(vat[3:5]) == 2 and int(vat[1:3]) % 4 == 0:
948 if int(vat[5:7]) < 1 or int(vat[5:7]) > 29:
950 if int(vat[3:5]) in (4, 6, 9, 11):
951 if int(vat[5:7]) < 1 or int(vat[5:7]) > 30:
953 if int(vat[3:5]) in (1, 3, 5, 7, 8, 10, 12):
954 if int(vat[5:7]) < 1 or int(vat[5:7]) > 31:
957 sum = 2 * int(vat[0]) + 7 * int(vat[1]) + 9 * int(vat[2]) + \
958 1 * int(vat[3]) + 4 * int(vat[4]) + 6 * int(vat[5]) + \
959 3 * int(vat[6]) + 5 * int(vat[7]) + 8 * int(vat[8]) + \
960 2 * int(vat[9]) + 7 * int(vat[10]) + 9 * int(vat[11])
964 if check != int(vat[12]):
969 def check_vat_se(self, vat):
971 Check Sweden VAT number.
980 if int(vat[9:11]) < 0:
983 sum = mult_add(2, int(vat[0])) + int(vat[1]) + \
984 mult_add(2, int(vat[2])) + int(vat[3]) + \
985 mult_add(2, int(vat[4])) + int(vat[5]) + \
986 mult_add(2, int(vat[6])) + int(vat[7]) + \
987 mult_add(2, int(vat[8]))
988 check = 10 - (sum % 10)
991 if check != int(vat[9]):
995 def check_vat_si(self, vat):
997 Check Slovenia VAT number.
1005 if int(vat[0:7]) <= 999999:
1008 sum = 8 * int(vat[0]) + 7 * int(vat[1]) + 6 * int(vat[2]) + \
1009 5 * int(vat[3]) + 4 * int(vat[4]) + 3 * int(vat[5]) + \
1011 check = 11 - (sum % 11)
1016 if check != int(vat[7]):
1020 def check_vat_sk(self, vat):
1022 Check Slovakia VAT number.
1028 if len(vat) not in(9, 10):
1031 if int(vat[0:2]) == 0 and len(vat) == 10:
1035 if int(vat[0:2]) < 54 or int(vat[0:2]) > 99:
1039 if int(vat[0:2]) > 53 :
1042 if int(vat[2:4]) < 1:
1044 if int(vat[2:4]) > 12 and int(vat[2:4]) < 51:
1046 if int(vat[2:4]) > 62:
1048 if int(vat[2:4]) in (2, 52) and int(vat[0:2]) % 4 > 0:
1049 if int(vat[4:6]) < 1 or int(vat[4:6]) > 28:
1051 if int(vat[2:4]) in (2, 52) and int(vat[0:2]) % 4 == 0:
1052 if int(vat[4:6]) < 1 or int(vat[4:6]) > 29:
1054 if int(vat[2:4]) in (4, 6, 9, 11, 54, 56, 59, 61):
1055 if int(vat[4:6]) < 1 or int(vat[4:6]) > 30:
1057 if int(vat[2:4]) in (1, 3, 5, 7, 8, 10, 12,
1058 51, 53, 55, 57, 58, 60, 62):
1059 if int(vat[4:6]) < 1 or int(vat[4:6]) > 31:
1065 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: