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.func import partial
26 from tools.translate import _
29 'be': 'BE0477472701', 'at': 'ATU12345675',
30 'bg': 'BG1234567892', 'cy': 'CY12345678F',
31 'cz': 'CZ12345679', 'de': 'DE123456788',
32 'dk': 'DK12345674', 'ee': 'EE123456780',
33 'es': 'ESA12345674', 'fi': 'FI12345671',
34 'fr': 'FR32123456789', 'gb': 'GB123456782',
35 'gr': 'GR12345670', 'hu': 'HU12345676',
36 'ie': 'IE1234567T', 'it': 'IT12345670017',
37 'lt': 'LT123456715', 'lu': 'LU12345613',
38 'lv': 'LV41234567891', 'mt': 'MT12345634',
39 'nl': 'NL123456782B90', 'pl': 'PL1234567883',
40 'pt': 'PT123456789', 'ro': 'RO1234567897',
41 'se': 'SE123456789701', 'si': 'SI12345679',
42 'sk': 'SK0012345675', 'el': 'EL12345670'
46 """Sum each digits of the multiplication of i and j."""
47 return reduce(lambda x, y: x + int(y), str(i*j), 0)
49 class res_partner(osv.osv):
50 _inherit = 'res.partner'
52 def _split_vat(self, vat):
53 vat_country, vat_number = vat[:2].lower(), vat[2:].replace(' ', '')
54 return vat_country, vat_number
56 def check_vat(self, cr, uid, ids):
58 Check the VAT number depending of the country.
59 http://sima-pc.com/nif.php
61 for partner in self.browse(cr, uid, ids):
64 vat_country, vat_number = self._split_vat(partner.vat)
65 if not hasattr(self, 'check_vat_' + vat_country):
67 check = getattr(self, 'check_vat_' + vat_country)
68 if not check(vat_number):
72 def vat_change(self, cr, uid, ids, value, context=None):
73 return {'value': {'vat_subjected': bool(value)}}
76 '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.")
79 def _construct_constraint_msg(self, cr, uid, ids):
80 def default_vat_check(cn, vn):
81 # by default, a VAT number is valid if:
82 # it starts with 2 letters
83 # has more than 3 characters
84 return cn[0] in string.ascii_lowercase and cn[1] in string.ascii_lowercase
86 vat_country, vat_number = self._split_vat(self.browse(cr, uid, ids)[0].vat)
87 if default_vat_check(vat_country, vat_number):
88 vat_no = vat_country in _ref_vat and _ref_vat[vat_country] or 'Country Code + Vat Number'
89 return _('The Vat does not seems to be correct. You should have entered something like this %s'), (vat_no)
90 return _('The VAT is invalid, It should begin with the country code'), ()
92 _constraints = [(check_vat, _construct_constraint_msg, ["vat"])]
94 # code from the following methods come from Tryton (B2CK)
95 # http://www.tryton.org/hgwebdir.cgi/modules/relationship/file/544d1de586d9/party.py
96 def check_vat_at(self, vat):
98 Check Austria VAT number.
109 sum = int(num[0]) + mult_add(2, int(num[1])) + \
110 int(num[2]) + mult_add(2, int(num[3])) + \
111 int(num[4]) + mult_add(2, int(num[5])) + \
113 check = 10 - ((sum + 4) % 10)
116 if int(vat[-1:]) != check:
120 def check_vat_be(self, vat):
122 Check Belgium VAT number.
130 if int(vat[-2:]) != \
131 97 - (int(vat[:8]) % 97):
135 def check_vat_bg(self, vat):
137 Check Bulgaria VAT number.
139 if len(vat) not in [9,10]:
145 if int(vat[0]) in (2, 3) and \
148 sum = 4 * int(vat[0]) + 3 * int(vat[1]) + 2 * int(vat[2]) + \
149 7 * int(vat[3]) + 6 * int(vat[4]) + 5 * int(vat[5]) + \
150 4 * int(vat[6]) + 3 * int(vat[7]) + 2 * int(vat[8])
151 check = 11 - (sum % 11)
156 def check_vat_cy(self, vat):
158 Check Cyprus VAT number.
202 sum = n0 + n1 + n2 + n3 + n4 + n5 + n6 + n7
203 check = chr(sum % 26 + 65)
208 def check_vat_cz(self, vat):
210 Check Czech Republic VAT number.
212 if len(vat) not in (8, 9, 10):
220 if int(vat[0]) not in (0, 1, 2, 3, 4, 5, 6, 7, 8):
222 sum = 8 * int(vat[0]) + 7 * int(vat[1]) + 6 * int(vat[2]) + \
223 5 * int(vat[3]) + 4 * int(vat[4]) + 3 * int(vat[5]) + \
225 check = 11 - (sum % 11)
230 if check != int(vat[7]):
232 elif len(vat) == 9 and int(vat[0]) == 6:
233 sum = 8 * int(vat[1]) + 7 * int(vat[2]) + 6 * int(vat[3]) + \
234 5 * int(vat[4]) + 4 * int(vat[5]) + 3 * int(vat[6]) + \
236 check = 9 - ((11 - (sum % 11)) % 10)
237 if check != int(vat[8]):
240 if int(vat[0:2]) > 53 and int(vat[0:2]) < 80:
242 if int(vat[2:4]) < 1:
244 if int(vat[2:4]) > 12 and int(vat[2:4]) < 51:
246 if int(vat[2:4]) > 62:
248 if int(vat[2:4]) in (2, 52) and int(vat[0:2]) % 4 > 0:
249 if int(vat[4:6]) < 1:
251 if int(vat[4:6]) > 28:
253 if int(vat[2:4]) in (2, 52) and int(vat[0:2]) % 4 == 0:
254 if int(vat[4:6]) < 1:
256 if int(vat[4:6]) > 29:
258 if int(vat[2:4]) in (4, 6, 9, 11, 54, 56, 59, 61):
259 if int(vat[4:6]) < 1:
261 if int(vat[4:6]) > 30:
263 if int(vat[2:4]) in (1, 3, 5, 7, 8, 10, 12, 51,
264 53, 55, 57, 58, 60, 62):
265 if int(vat[4:6]) < 1:
267 if int(vat[4:6]) > 31:
270 if int(vat[0:2]) < 54:
272 if int(vat[2:4]) < 1:
274 if int(vat[2:4]) > 12 and int(vat[2:4]) < 51:
276 if int(vat[2:4]) > 62:
278 if int(vat[2:4]) in (2, 52) and int(vat[0:2]) % 4 > 0:
279 if int(vat[4:6]) < 1:
281 if int(vat[4:6]) > 28:
283 if int(vat[2:4]) in (2, 52) and int(vat[0:2]) % 4 == 0:
284 if int(vat[4:6]) < 1:
286 if int(vat[4:6]) > 29:
288 if int(vat[2:4]) in (4, 6, 9, 11, 54, 56, 59, 61):
289 if int(vat[4:6]) < 1:
291 if int(vat[4:6]) > 30:
293 if int(vat[2:4]) in (1, 3, 5, 7, 8, 10, 12, 51,
294 53, 55, 57, 58, 60, 62):
295 if int(vat[4:6]) < 1:
297 if int(vat[4:6]) > 31:
299 if (int(vat[0:2]) + int(vat[2:4]) + int(vat[4:6]) + int(vat[6:8]) +
300 int(vat[8:10])) % 11 != 0:
302 if int(vat[0:10]) % 11 != 0:
306 def check_vat_de(self, vat):
308 Check Germany VAT number.
316 if int(vat[0:7]) <= 0:
320 sum = (2 * ((int(vat[i]) + sum + 9) % 10 + 1)) % 11
324 if int(vat[8]) != check:
328 def check_vat_dk(self, vat):
330 Check Denmark VAT number.
340 sum = 2 * int(vat[0]) + 7 * int(vat[1]) + 6 * int(vat[2]) + \
341 5 * int(vat[3]) + 4 * int(vat[4]) + 3 * int(vat[5]) + \
342 2 * int(vat[6]) + int(vat[7])
347 def check_vat_ee(self, vat):
349 Check Estonia VAT number.
357 sum = 3 * int(vat[0]) + 7 * int(vat[1]) + 1 * int(vat[2]) + \
358 3 * int(vat[3]) + 7 * int(vat[4]) + 1 * int(vat[5]) + \
359 3 * int(vat[6]) + 7 * int(vat[7])
360 check = 10 - (sum % 10)
363 if check != int(vat[8]):
367 def check_vat_es(self, vat):
369 Check Spain VAT number.
399 #Legal persons with profit aim
400 if vat[0] in ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'U', 'V'):
405 sum = mult_add(2, int(vat[1])) + int(vat[2]) + \
406 mult_add(2, int(vat[3])) + int(vat[4]) + \
407 mult_add(2, int(vat[5])) + int(vat[6]) + \
408 mult_add(2, int(vat[7]))
409 check = 10 - (sum % 10)
413 #Legal persons with non-profit aim
414 elif vat[0] in ('N', 'P', 'Q', 'R', 'S', 'W'):
419 sum = mult_add(2, int(vat[1])) + int(vat[2]) + \
420 mult_add(2, int(vat[3])) + int(vat[4]) + \
421 mult_add(2, int(vat[5])) + int(vat[6]) + \
422 mult_add(2, int(vat[7]))
423 check = 10 - (sum % 10)
424 check = chr(check + 64)
428 #Foreign natural persons, under age 14 or non-residents
429 elif vat[0] in ('K', 'L', 'M', 'X', 'Y', 'Z'):
431 check_value = '1' + vat[1:8]
433 check_value = '2' + vat[1:8]
435 check_value = vat[1:8]
441 check = 1 + (int(check_value) % 23)
447 #Spanish natural persons
453 check = 1 + (int(vat[:8]) % 23)
460 def check_vat_fi(self, vat):
462 Check Finland VAT number.
470 sum = 7 * int(vat[0]) + 9 * int(vat[1]) + 10 * int(vat[2]) + \
471 5 * int(vat[3]) + 8 * int(vat[4]) + 4 * int(vat[5]) + \
473 check = 11 - (sum % 11)
478 if check != int(vat[7]):
482 def check_vat_fr(self, vat):
484 Check France VAT number.
502 check = ((int(vat[2:11]) * 100) + 12) % 97
503 if check != int(vat[0:2]):
507 conv = ['0', '1', '2', '3', '4', '5', '6', '7',
508 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
509 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T',
510 'U', 'V', 'W', 'X', 'Y', 'Z']
511 if vat[0] not in conv \
512 or vat[1] not in conv:
514 c1 = conv.index(vat[0])
515 c2 = conv.index(vat[1])
518 sum = c1 * 24 + c2 - 10
520 sum = c1 * 34 + c2 - 100
523 sum = (int(sum) / 11) + 1
524 y = (int(vat[2:11]) + sum) % 11
529 def check_vat_gb(self, vat):
531 Check United Kingdom VAT number.
541 if int(vat[2:5]) >= 500:
545 if int(vat[2:5]) < 500:
549 elif len(vat) in (9, 10):
555 if int(vat[0:7]) < 1:
557 if int(vat[0:7]) > 19999 and int(vat[0:7]) < 1000000:
559 if int(vat[7:9]) > 97:
561 if len(vat) == 10 and int(vat[9]) != 3:
564 sum = 8 * int(vat[0]) + 7 * int(vat[1]) + 6 * int(vat[2]) + \
565 5 * int(vat[3]) + 4 * int(vat[4]) + 3 * int(vat[5]) + \
566 2 * int(vat[6]) + 10 * int(vat[7]) + int(vat[8])
570 elif len(vat) in (12, 13):
576 if int(vat[0:3]) not in (0, 1):
579 if int(vat[3:10]) < 1:
581 if int(vat[3:10]) > 19999 and int(vat[3:10]) < 1000000:
583 if int(vat[10:12]) > 97:
585 if len(vat) == 13 and int(vat[12]) != 3:
588 sum = 8 * int(vat[3]) + 7 * int(vat[4]) + 6 * int(vat[5]) + \
589 5 * int(vat[6]) + 4 * int(vat[7]) + 3 * int(vat[8]) + \
590 2 * int(vat[9]) + 10 * int(vat[10]) + int(vat[11])
596 def check_vat_gr(self, vat):
598 Check Greece VAT number.
605 sum = 128 * int(vat[0]) + 64 * int(vat[1]) + 32 * int(vat[2]) + \
606 16 * int(vat[3]) + 8 * int(vat[4]) + 4 * int(vat[5]) + \
611 if check != int(vat[7]):
615 sum = 256 * int(vat[0]) + 128 * int(vat[1]) + 64 * int(vat[2]) + \
616 32 * int(vat[3]) + 16 * int(vat[4]) + 8 * int(vat[5]) + \
617 4 * int(vat[6]) + 2 * int(vat[7])
621 if check != int(vat[8]):
626 def check_vat_el(self, vat):
627 return self.check_vat_gr(vat)
629 def check_vat_hu(self, vat):
631 Check Hungary VAT number.
641 sum = 9 * int(vat[0]) + 7 * int(vat[1]) + 3 * int(vat[2]) + \
642 1 * int(vat[3]) + 9 * int(vat[4]) + 7 * int(vat[5]) + \
644 check = 10 - (sum % 10)
647 if check != int(vat[7]):
651 def check_vat_ie(self, vat):
653 Check Ireland VAT number.
657 if (ord(vat[1]) >= 65 and ord(vat[1]) <= 90) \
658 or vat[1] in ('+', '*'):
668 sum = 7 * int(vat[2]) + 6 * int(vat[3]) + 5 * int(vat[4]) + \
669 4 * int(vat[5]) + 3 * int(vat[6]) + 2 * int(vat[0])
674 check = chr(check + 64)
684 sum = 8 * int(vat[0]) + 7 * int(vat[1]) + 6 * int(vat[2]) + \
685 5 * int(vat[3]) + 4 * int(vat[4]) + 3 * int(vat[5]) + \
691 check = chr(check + 64)
696 def check_vat_it(self, vat):
698 Check Italy VAT number.
706 if int(vat[0:7]) <= 0:
708 if int(vat[7:10]) <= 0:
710 if int(vat[7:10]) > 100 and int(vat[7:10]) < 120:
712 if int(vat[7:10]) > 121:
715 sum = int(vat[0]) + mult_add(2, int(vat[1])) + int(vat[2]) + \
716 mult_add(2, int(vat[3])) + int(vat[4]) + \
717 mult_add(2, int(vat[5])) + int(vat[6]) + \
718 mult_add(2, int(vat[7])) + int(vat[8]) + \
719 mult_add(2, int(vat[9]))
720 check = 10 - (sum % 10)
723 if check != int(vat[10]):
727 def check_vat_lt(self, vat):
729 Check Lithuania VAT number.
739 sum = 1 * int(vat[0]) + 2 * int(vat[1]) + 3 * int(vat[2]) + \
740 4 * int(vat[3]) + 5 * int(vat[4]) + 6 * int(vat[5]) + \
741 7 * int(vat[6]) + 8 * int(vat[7])
743 sum = 3 * int(vat[0]) + 4 * int(vat[1]) + 5 * int(vat[2]) + \
744 6 * int(vat[3]) + 7 * int(vat[4]) + 8 * int(vat[5]) + \
745 9 * int(vat[6]) + 1 * int(vat[7])
749 if check != int(vat[8]):
753 if int(vat[10]) != 1:
755 sum = 1 * int(vat[0]) + 2 * int(vat[1]) + 3 * int(vat[2]) + \
756 4 * int(vat[3]) + 5 * int(vat[4]) + 6 * int(vat[5]) + \
757 7 * int(vat[6]) + 8 * int(vat[7]) + 9 * int(vat[8]) + \
758 1 * int(vat[9]) + 2 * int(vat[10])
760 sum = 3 * int(vat[0]) + 4 * int(vat[1]) + 5 * int(vat[2]) + \
761 6 * int(vat[3]) + 7 * int(vat[4]) + 8 * int(vat[5]) + \
762 9 * int(vat[6]) + 1 * int(vat[7]) + 2 * int(vat[8]) + \
763 3 * int(vat[9]) + 4 * int(vat[10])
767 if check != int(vat[11]):
772 def check_vat_lu(self, vat):
774 Check Luxembourg VAT number.
782 if int(vat[0:6]) <= 0:
784 check = int(vat[0:6]) % 89
785 if check != int(vat[6:8]):
789 def check_vat_lv(self, vat):
791 Check Latvia VAT number.
800 sum = 9 * int(vat[0]) + 1 * int(vat[1]) + 4 * int(vat[2]) + \
801 8 * int(vat[3]) + 3 * int(vat[4]) + 10 * int(vat[5]) + \
802 2 * int(vat[6]) + 5 * int(vat[7]) + 7 * int(vat[8]) + \
804 if sum % 11 == 4 and int(vat[0]) == 9:
807 check = 4 - (sum % 11)
809 check = 14 - (sum % 11)
811 check = 3 - (sum % 11)
812 if check != int(vat[10]):
816 if int(vat[2:4]) == 2 and int(vat[4:6]) % 4 > 0:
817 if int(vat[0:2]) < 1 or int(vat[0:2]) > 28:
819 if int(vat[2:4]) == 2 and int(vat[4:6]) % 4 == 0:
820 if int(vat[0:2]) < 1 or int(vat[0:2]) > 29:
822 if int(vat[2:4]) in (4, 6, 9, 11):
823 if int(vat[0:2]) < 1 or int(vat[0:2]) > 30:
825 if int(vat[2:4]) in (1, 3, 5, 7, 8, 10, 12):
826 if int(vat[0:2]) < 1 or int(vat[0:2]) > 31:
828 if int(vat[2:4]) < 1 or int(vat[2:4]) > 12:
832 def check_vat_mt(self, vat):
834 Check Malta VAT number.
843 if int(vat[0:6]) < 100000:
846 sum = 3 * int(vat[0]) + 4 * int(vat[1]) + 6 * int(vat[2]) + \
847 7 * int(vat[3]) + 8 * int(vat[4]) + 9 * int(vat[5])
848 check = 37 - (sum % 37)
849 if check != int(vat[6:8]):
853 def check_vat_nl(self, vat):
855 Check Netherlands VAT number.
864 if int(vat[0:8]) <= 0:
869 sum = 9 * int(vat[0]) + 8 * int(vat[1]) + 7 * int(vat[2]) + \
870 6 * int(vat[3]) + 5 * int(vat[4]) + 4 * int(vat[5]) + \
871 3 * int(vat[6]) + 2 * int(vat[7])
876 if check != int(vat[8]):
880 def check_vat_pl(self, vat):
882 Check Poland VAT number.
891 sum = 6 * int(vat[0]) + 5 * int(vat[1]) + 7 * int(vat[2]) + \
892 2 * int(vat[3]) + 3 * int(vat[4]) + 4 * int(vat[5]) + \
893 5 * int(vat[6]) + 6 * int(vat[7]) + 7 * int(vat[8])
897 if check != int(vat[9]):
901 def check_vat_pt(self, vat):
903 Check Portugal VAT number.
915 sum = 9 * int(vat[0]) + 8 * int(vat[1]) + 7 * int(vat[2]) + \
916 6 * int(vat[3]) + 5 * int(vat[4]) + 4 * int(vat[5]) + \
917 3 * int(vat[6]) + 2 * int(vat[7])
918 check = 11 - (sum % 11)
919 if check == 10 or check == 11:
923 def check_vat_ro(self, vat):
925 Check Romania VAT number.
932 if len(vat) >= 2 and len(vat) <= 10:
933 vat = (10 - len(vat)) * '0' + vat
934 sum = 7 * int(vat[0]) + 5 * int(vat[1]) + 3 * int(vat[2]) + \
935 2 * int(vat[3]) + 1 * int(vat[4]) + 7 * int(vat[5]) + \
936 5 * int(vat[6]) + 3 * int(vat[7]) + 2 * int(vat[8])
937 check = (sum * 10) % 11
940 if check != int(vat[9]):
944 if int(vat[0]) not in (1, 2, 3, 4, 6):
946 if int(vat[3:5]) < 1 or int(vat[3:5]) > 12:
948 if int(vat[3:5]) == 2 and int(vat[1:3]) % 4 > 0:
949 if int(vat[5:7]) < 1 or int(vat[5:7]) > 28:
951 if int(vat[3:5]) == 2 and int(vat[1:3]) % 4 == 0:
952 if int(vat[5:7]) < 1 or int(vat[5:7]) > 29:
954 if int(vat[3:5]) in (4, 6, 9, 11):
955 if int(vat[5:7]) < 1 or int(vat[5:7]) > 30:
957 if int(vat[3:5]) in (1, 3, 5, 7, 8, 10, 12):
958 if int(vat[5:7]) < 1 or int(vat[5:7]) > 31:
961 sum = 2 * int(vat[0]) + 7 * int(vat[1]) + 9 * int(vat[2]) + \
962 1 * int(vat[3]) + 4 * int(vat[4]) + 6 * int(vat[5]) + \
963 3 * int(vat[6]) + 5 * int(vat[7]) + 8 * int(vat[8]) + \
964 2 * int(vat[9]) + 7 * int(vat[10]) + 9 * int(vat[11])
968 if check != int(vat[12]):
973 def check_vat_se(self, vat):
975 Check Sweden VAT number.
984 if int(vat[9:11]) < 0:
987 sum = mult_add(2, int(vat[0])) + int(vat[1]) + \
988 mult_add(2, int(vat[2])) + int(vat[3]) + \
989 mult_add(2, int(vat[4])) + int(vat[5]) + \
990 mult_add(2, int(vat[6])) + int(vat[7]) + \
991 mult_add(2, int(vat[8]))
992 check = 10 - (sum % 10)
995 if check != int(vat[9]):
999 def check_vat_si(self, vat):
1001 Check Slovenia VAT number.
1009 if int(vat[0:7]) <= 999999:
1012 sum = 8 * int(vat[0]) + 7 * int(vat[1]) + 6 * int(vat[2]) + \
1013 5 * int(vat[3]) + 4 * int(vat[4]) + 3 * int(vat[5]) + \
1015 check = 11 - (sum % 11)
1020 if check != int(vat[7]):
1024 def check_vat_sk(self, vat):
1026 Check Slovakia VAT number.
1032 if len(vat) not in(9, 10):
1035 if int(vat[0:2]) == 0 and len(vat) == 10:
1039 if int(vat[0:2]) < 54 or int(vat[0:2]) > 99:
1043 if int(vat[0:2]) > 53 :
1046 if int(vat[2:4]) < 1:
1048 if int(vat[2:4]) > 12 and int(vat[2:4]) < 51:
1050 if int(vat[2:4]) > 62:
1052 if int(vat[2:4]) in (2, 52) and int(vat[0:2]) % 4 > 0:
1053 if int(vat[4:6]) < 1 or int(vat[4:6]) > 28:
1055 if int(vat[2:4]) in (2, 52) and int(vat[0:2]) % 4 == 0:
1056 if int(vat[4:6]) < 1 or int(vat[4:6]) > 29:
1058 if int(vat[2:4]) in (4, 6, 9, 11, 54, 56, 59, 61):
1059 if int(vat[4:6]) < 1 or int(vat[4:6]) > 30:
1061 if int(vat[2:4]) in (1, 3, 5, 7, 8, 10, 12,
1062 51, 53, 55, 57, 58, 60, 62):
1063 if int(vat[4:6]) < 1 or int(vat[4:6]) > 31:
1069 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: