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
85 vat_country, vat_number = self._split_vat(self.browse(cr, uid, ids)[0].vat)
86 if default_vat_check(vat_country, vat_number):
87 vat_no = vat_country in _ref_vat and _ref_vat[vat_country] or 'Country Code + Vat Number'
88 return _('The Vat does not seems to be correct. You should have entered something like this %s'), (vat_no)
89 return _('The VAT is invalid, It should begin with the country code'), ()
91 _constraints = [(check_vat, _construct_constraint_msg, ["vat"])]
93 # code from the following methods come from Tryton (B2CK)
94 # http://www.tryton.org/hgwebdir.cgi/modules/relationship/file/544d1de586d9/party.py
95 def check_vat_at(self, vat):
97 Check Austria VAT number.
108 sum = int(num[0]) + mult_add(2, int(num[1])) + \
109 int(num[2]) + mult_add(2, int(num[3])) + \
110 int(num[4]) + mult_add(2, int(num[5])) + \
112 check = 10 - ((sum + 4) % 10)
115 if int(vat[-1:]) != check:
119 def check_vat_be(self, vat):
121 Check Belgium VAT number.
129 if int(vat[-2:]) != \
130 97 - (int(vat[:8]) % 97):
134 def check_vat_bg(self, vat):
136 Check Bulgaria VAT number.
138 if len(vat) not in [9,10]:
144 if int(vat[0]) in (2, 3) and \
147 sum = 4 * int(vat[0]) + 3 * int(vat[1]) + 2 * int(vat[2]) + \
148 7 * int(vat[3]) + 6 * int(vat[4]) + 5 * int(vat[5]) + \
149 4 * int(vat[6]) + 3 * int(vat[7]) + 2 * int(vat[8])
150 check = 11 - (sum % 11)
155 def check_vat_cy(self, vat):
157 Check Cyprus VAT number.
201 sum = n0 + n1 + n2 + n3 + n4 + n5 + n6 + n7
202 check = chr(sum % 26 + 65)
207 def check_vat_cz(self, vat):
209 Check Czech Republic VAT number.
211 if len(vat) not in (8, 9, 10):
219 if int(vat[0]) not in (0, 1, 2, 3, 4, 5, 6, 7, 8):
221 sum = 8 * int(vat[0]) + 7 * int(vat[1]) + 6 * int(vat[2]) + \
222 5 * int(vat[3]) + 4 * int(vat[4]) + 3 * int(vat[5]) + \
224 check = 11 - (sum % 11)
229 if check != int(vat[7]):
231 elif len(vat) == 9 and int(vat[0]) == 6:
232 sum = 8 * int(vat[1]) + 7 * int(vat[2]) + 6 * int(vat[3]) + \
233 5 * int(vat[4]) + 4 * int(vat[5]) + 3 * int(vat[6]) + \
235 check = 9 - ((11 - (sum % 11)) % 10)
236 if check != int(vat[8]):
239 if int(vat[0:2]) > 53 and int(vat[0:2]) < 80:
241 if int(vat[2:4]) < 1:
243 if int(vat[2:4]) > 12 and int(vat[2:4]) < 51:
245 if int(vat[2:4]) > 62:
247 if int(vat[2:4]) in (2, 52) and int(vat[0:2]) % 4 > 0:
248 if int(vat[4:6]) < 1:
250 if int(vat[4:6]) > 28:
252 if int(vat[2:4]) in (2, 52) and int(vat[0:2]) % 4 == 0:
253 if int(vat[4:6]) < 1:
255 if int(vat[4:6]) > 29:
257 if int(vat[2:4]) in (4, 6, 9, 11, 54, 56, 59, 61):
258 if int(vat[4:6]) < 1:
260 if int(vat[4:6]) > 30:
262 if int(vat[2:4]) in (1, 3, 5, 7, 8, 10, 12, 51,
263 53, 55, 57, 58, 60, 62):
264 if int(vat[4:6]) < 1:
266 if int(vat[4:6]) > 31:
269 if int(vat[0:2]) < 54:
271 if int(vat[2:4]) < 1:
273 if int(vat[2:4]) > 12 and int(vat[2:4]) < 51:
275 if int(vat[2:4]) > 62:
277 if int(vat[2:4]) in (2, 52) and int(vat[0:2]) % 4 > 0:
278 if int(vat[4:6]) < 1:
280 if int(vat[4:6]) > 28:
282 if int(vat[2:4]) in (2, 52) and int(vat[0:2]) % 4 == 0:
283 if int(vat[4:6]) < 1:
285 if int(vat[4:6]) > 29:
287 if int(vat[2:4]) in (4, 6, 9, 11, 54, 56, 59, 61):
288 if int(vat[4:6]) < 1:
290 if int(vat[4:6]) > 30:
292 if int(vat[2:4]) in (1, 3, 5, 7, 8, 10, 12, 51,
293 53, 55, 57, 58, 60, 62):
294 if int(vat[4:6]) < 1:
296 if int(vat[4:6]) > 31:
298 if (int(vat[0:2]) + int(vat[2:4]) + int(vat[4:6]) + int(vat[6:8]) +
299 int(vat[8:10])) % 11 != 0:
301 if int(vat[0:10]) % 11 != 0:
305 def check_vat_de(self, vat):
307 Check Germany VAT number.
315 if int(vat[0:7]) <= 0:
319 sum = (2 * ((int(vat[i]) + sum + 9) % 10 + 1)) % 11
323 if int(vat[8]) != check:
327 def check_vat_dk(self, vat):
329 Check Denmark VAT number.
339 sum = 2 * int(vat[0]) + 7 * int(vat[1]) + 6 * int(vat[2]) + \
340 5 * int(vat[3]) + 4 * int(vat[4]) + 3 * int(vat[5]) + \
341 2 * int(vat[6]) + int(vat[7])
346 def check_vat_ee(self, vat):
348 Check Estonia VAT number.
356 sum = 3 * int(vat[0]) + 7 * int(vat[1]) + 1 * int(vat[2]) + \
357 3 * int(vat[3]) + 7 * int(vat[4]) + 1 * int(vat[5]) + \
358 3 * int(vat[6]) + 7 * int(vat[7])
359 check = 10 - (sum % 10)
362 if check != int(vat[8]):
366 def check_vat_es(self, vat):
368 Check Spain VAT number.
398 #Legal persons with profit aim
399 if vat[0] in ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'U', 'V'):
404 sum = mult_add(2, int(vat[1])) + int(vat[2]) + \
405 mult_add(2, int(vat[3])) + int(vat[4]) + \
406 mult_add(2, int(vat[5])) + int(vat[6]) + \
407 mult_add(2, int(vat[7]))
408 check = 10 - (sum % 10)
412 #Legal persons with non-profit aim
413 elif vat[0] in ('N', 'P', 'Q', 'R', 'S', 'W'):
418 sum = mult_add(2, int(vat[1])) + int(vat[2]) + \
419 mult_add(2, int(vat[3])) + int(vat[4]) + \
420 mult_add(2, int(vat[5])) + int(vat[6]) + \
421 mult_add(2, int(vat[7]))
422 check = 10 - (sum % 10)
423 check = chr(check + 64)
427 #Foreign natural persons, under age 14 or non-residents
428 elif vat[0] in ('K', 'L', 'M', 'X', 'Y', 'Z'):
430 check_value = '1' + vat[1:8]
432 check_value = '2' + vat[1:8]
434 check_value = vat[1:8]
440 check = 1 + (int(check_value) % 23)
446 #Spanish natural persons
452 check = 1 + (int(vat[:8]) % 23)
459 def check_vat_fi(self, vat):
461 Check Finland VAT number.
469 sum = 7 * int(vat[0]) + 9 * int(vat[1]) + 10 * int(vat[2]) + \
470 5 * int(vat[3]) + 8 * int(vat[4]) + 4 * int(vat[5]) + \
472 check = 11 - (sum % 11)
477 if check != int(vat[7]):
481 def check_vat_fr(self, vat):
483 Check France VAT number.
501 check = ((int(vat[2:11]) * 100) + 12) % 97
502 if check != int(vat[0:2]):
506 conv = ['0', '1', '2', '3', '4', '5', '6', '7',
507 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
508 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T',
509 'U', 'V', 'W', 'X', 'Y', 'Z']
510 if vat[0] not in conv \
511 or vat[1] not in conv:
513 c1 = conv.index(vat[0])
514 c2 = conv.index(vat[1])
517 sum = c1 * 24 + c2 - 10
519 sum = c1 * 34 + c2 - 100
522 sum = (int(sum) / 11) + 1
523 y = (int(vat[2:11]) + sum) % 11
528 def check_vat_gb(self, vat):
530 Check United Kingdom VAT number.
540 if int(vat[2:5]) >= 500:
544 if int(vat[2:5]) < 500:
548 elif len(vat) in (9, 10):
554 if int(vat[0:7]) < 1:
556 if int(vat[0:7]) > 19999 and int(vat[0:7]) < 1000000:
558 if int(vat[7:9]) > 97:
560 if len(vat) == 10 and int(vat[9]) != 3:
563 sum = 8 * int(vat[0]) + 7 * int(vat[1]) + 6 * int(vat[2]) + \
564 5 * int(vat[3]) + 4 * int(vat[4]) + 3 * int(vat[5]) + \
565 2 * int(vat[6]) + 10 * int(vat[7]) + int(vat[8])
569 elif len(vat) in (12, 13):
575 if int(vat[0:3]) not in (0, 1):
578 if int(vat[3:10]) < 1:
580 if int(vat[3:10]) > 19999 and int(vat[3:10]) < 1000000:
582 if int(vat[10:12]) > 97:
584 if len(vat) == 13 and int(vat[12]) != 3:
587 sum = 8 * int(vat[3]) + 7 * int(vat[4]) + 6 * int(vat[5]) + \
588 5 * int(vat[6]) + 4 * int(vat[7]) + 3 * int(vat[8]) + \
589 2 * int(vat[9]) + 10 * int(vat[10]) + int(vat[11])
595 def check_vat_gr(self, vat):
597 Check Greece VAT number.
604 sum = 128 * int(vat[0]) + 64 * int(vat[1]) + 32 * int(vat[2]) + \
605 16 * int(vat[3]) + 8 * int(vat[4]) + 4 * int(vat[5]) + \
610 if check != int(vat[7]):
614 sum = 256 * int(vat[0]) + 128 * int(vat[1]) + 64 * int(vat[2]) + \
615 32 * int(vat[3]) + 16 * int(vat[4]) + 8 * int(vat[5]) + \
616 4 * int(vat[6]) + 2 * int(vat[7])
620 if check != int(vat[8]):
625 def check_vat_el(self, vat):
626 return self.check_vat_gr(vat)
628 def check_vat_hu(self, vat):
630 Check Hungary VAT number.
640 sum = 9 * int(vat[0]) + 7 * int(vat[1]) + 3 * int(vat[2]) + \
641 1 * int(vat[3]) + 9 * int(vat[4]) + 7 * int(vat[5]) + \
643 check = 10 - (sum % 10)
646 if check != int(vat[7]):
650 def check_vat_ie(self, vat):
652 Check Ireland VAT number.
656 if (ord(vat[1]) >= 65 and ord(vat[1]) <= 90) \
657 or vat[1] in ('+', '*'):
667 sum = 7 * int(vat[2]) + 6 * int(vat[3]) + 5 * int(vat[4]) + \
668 4 * int(vat[5]) + 3 * int(vat[6]) + 2 * int(vat[0])
673 check = chr(check + 64)
683 sum = 8 * int(vat[0]) + 7 * int(vat[1]) + 6 * int(vat[2]) + \
684 5 * int(vat[3]) + 4 * int(vat[4]) + 3 * int(vat[5]) + \
690 check = chr(check + 64)
695 def check_vat_it(self, vat):
697 Check Italy VAT number.
705 if int(vat[0:7]) <= 0:
707 if int(vat[7:10]) <= 0:
709 if int(vat[7:10]) > 100 and int(vat[7:10]) < 120:
712 sum = int(vat[0]) + mult_add(2, int(vat[1])) + int(vat[2]) + \
713 mult_add(2, int(vat[3])) + int(vat[4]) + \
714 mult_add(2, int(vat[5])) + int(vat[6]) + \
715 mult_add(2, int(vat[7])) + int(vat[8]) + \
716 mult_add(2, int(vat[9]))
717 check = 10 - (sum % 10)
720 if check != int(vat[10]):
724 def check_vat_lt(self, vat):
726 Check Lithuania VAT number.
736 sum = 1 * int(vat[0]) + 2 * int(vat[1]) + 3 * int(vat[2]) + \
737 4 * int(vat[3]) + 5 * int(vat[4]) + 6 * int(vat[5]) + \
738 7 * int(vat[6]) + 8 * int(vat[7])
740 sum = 3 * int(vat[0]) + 4 * int(vat[1]) + 5 * int(vat[2]) + \
741 6 * int(vat[3]) + 7 * int(vat[4]) + 8 * int(vat[5]) + \
742 9 * int(vat[6]) + 1 * int(vat[7])
746 if check != int(vat[8]):
750 if int(vat[10]) != 1:
752 sum = 1 * int(vat[0]) + 2 * int(vat[1]) + 3 * int(vat[2]) + \
753 4 * int(vat[3]) + 5 * int(vat[4]) + 6 * int(vat[5]) + \
754 7 * int(vat[6]) + 8 * int(vat[7]) + 9 * int(vat[8]) + \
755 1 * int(vat[9]) + 2 * int(vat[10])
757 sum = 3 * int(vat[0]) + 4 * int(vat[1]) + 5 * int(vat[2]) + \
758 6 * int(vat[3]) + 7 * int(vat[4]) + 8 * int(vat[5]) + \
759 9 * int(vat[6]) + 1 * int(vat[7]) + 2 * int(vat[8]) + \
760 3 * int(vat[9]) + 4 * int(vat[10])
764 if check != int(vat[11]):
769 def check_vat_lu(self, vat):
771 Check Luxembourg VAT number.
779 if int(vat[0:6]) <= 0:
781 check = int(vat[0:6]) % 89
782 if check != int(vat[6:8]):
786 def check_vat_lv(self, vat):
788 Check Latvia VAT number.
797 sum = 9 * int(vat[0]) + 1 * int(vat[1]) + 4 * int(vat[2]) + \
798 8 * int(vat[3]) + 3 * int(vat[4]) + 10 * int(vat[5]) + \
799 2 * int(vat[6]) + 5 * int(vat[7]) + 7 * int(vat[8]) + \
801 if sum % 11 == 4 and int(vat[0]) == 9:
804 check = 4 - (sum % 11)
806 check = 14 - (sum % 11)
808 check = 3 - (sum % 11)
809 if check != int(vat[10]):
813 if int(vat[2:4]) == 2 and int(vat[4:6]) % 4 > 0:
814 if int(vat[0:2]) < 1 or int(vat[0:2]) > 28:
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]) > 29:
819 if int(vat[2:4]) in (4, 6, 9, 11):
820 if int(vat[0:2]) < 1 or int(vat[0:2]) > 30:
822 if int(vat[2:4]) in (1, 3, 5, 7, 8, 10, 12):
823 if int(vat[0:2]) < 1 or int(vat[0:2]) > 31:
825 if int(vat[2:4]) < 1 or int(vat[2:4]) > 12:
829 def check_vat_mt(self, vat):
831 Check Malta VAT number.
840 if int(vat[0:6]) < 100000:
843 sum = 3 * int(vat[0]) + 4 * int(vat[1]) + 6 * int(vat[2]) + \
844 7 * int(vat[3]) + 8 * int(vat[4]) + 9 * int(vat[5])
845 check = 37 - (sum % 37)
846 if check != int(vat[6:8]):
850 def check_vat_nl(self, vat):
852 Check Netherlands VAT number.
861 if int(vat[0:8]) <= 0:
866 sum = 9 * int(vat[0]) + 8 * int(vat[1]) + 7 * int(vat[2]) + \
867 6 * int(vat[3]) + 5 * int(vat[4]) + 4 * int(vat[5]) + \
868 3 * int(vat[6]) + 2 * int(vat[7])
873 if check != int(vat[8]):
877 def check_vat_pl(self, vat):
879 Check Poland VAT number.
888 sum = 6 * int(vat[0]) + 5 * int(vat[1]) + 7 * int(vat[2]) + \
889 2 * int(vat[3]) + 3 * int(vat[4]) + 4 * int(vat[5]) + \
890 5 * int(vat[6]) + 6 * int(vat[7]) + 7 * int(vat[8])
894 if check != int(vat[9]):
898 def check_vat_pt(self, vat):
900 Check Portugal VAT number.
912 sum = 9 * int(vat[0]) + 8 * int(vat[1]) + 7 * int(vat[2]) + \
913 6 * int(vat[3]) + 5 * int(vat[4]) + 4 * int(vat[5]) + \
914 3 * int(vat[6]) + 2 * int(vat[7])
915 check = 11 - (sum % 11)
916 if check == 10 or check == 11:
920 def check_vat_ro(self, vat):
922 Check Romania VAT number.
929 if len(vat) >= 2 and len(vat) <= 10:
930 vat = (10 - len(vat)) * '0' + vat
931 sum = 7 * int(vat[0]) + 5 * int(vat[1]) + 3 * int(vat[2]) + \
932 2 * int(vat[3]) + 1 * int(vat[4]) + 7 * int(vat[5]) + \
933 5 * int(vat[6]) + 3 * int(vat[7]) + 2 * int(vat[8])
934 check = (sum * 10) % 11
937 if check != int(vat[9]):
941 if int(vat[0]) not in (1, 2, 3, 4, 6):
943 if int(vat[3:5]) < 1 or int(vat[3:5]) > 12:
945 if int(vat[3:5]) == 2 and int(vat[1:3]) % 4 > 0:
946 if int(vat[5:7]) < 1 or int(vat[5:7]) > 28:
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]) > 29:
951 if int(vat[3:5]) in (4, 6, 9, 11):
952 if int(vat[5:7]) < 1 or int(vat[5:7]) > 30:
954 if int(vat[3:5]) in (1, 3, 5, 7, 8, 10, 12):
955 if int(vat[5:7]) < 1 or int(vat[5:7]) > 31:
958 sum = 2 * int(vat[0]) + 7 * int(vat[1]) + 9 * int(vat[2]) + \
959 1 * int(vat[3]) + 4 * int(vat[4]) + 6 * int(vat[5]) + \
960 3 * int(vat[6]) + 5 * int(vat[7]) + 8 * int(vat[8]) + \
961 2 * int(vat[9]) + 7 * int(vat[10]) + 9 * int(vat[11])
965 if check != int(vat[12]):
970 def check_vat_se(self, vat):
972 Check Sweden VAT number.
981 if int(vat[9:11]) < 0:
984 sum = mult_add(2, int(vat[0])) + int(vat[1]) + \
985 mult_add(2, int(vat[2])) + int(vat[3]) + \
986 mult_add(2, int(vat[4])) + int(vat[5]) + \
987 mult_add(2, int(vat[6])) + int(vat[7]) + \
988 mult_add(2, int(vat[8]))
989 check = 10 - (sum % 10)
992 if check != int(vat[9]):
996 def check_vat_si(self, vat):
998 Check Slovenia VAT number.
1006 if int(vat[0:7]) <= 999999:
1009 sum = 8 * int(vat[0]) + 7 * int(vat[1]) + 6 * int(vat[2]) + \
1010 5 * int(vat[3]) + 4 * int(vat[4]) + 3 * int(vat[5]) + \
1012 check = 11 - (sum % 11)
1017 if check != int(vat[7]):
1021 def check_vat_sk(self, vat):
1023 Check Slovakia VAT number.
1029 if len(vat) not in(9, 10):
1032 if int(vat[0:2]) == 0 and len(vat) == 10:
1036 if int(vat[0:2]) < 54 or int(vat[0:2]) > 99:
1040 if int(vat[0:2]) > 53 :
1043 if int(vat[2:4]) < 1:
1045 if int(vat[2:4]) > 12 and int(vat[2:4]) < 51:
1047 if int(vat[2:4]) > 62:
1049 if int(vat[2:4]) in (2, 52) and int(vat[0:2]) % 4 > 0:
1050 if int(vat[4:6]) < 1 or int(vat[4:6]) > 28:
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]) > 29:
1055 if int(vat[2:4]) in (4, 6, 9, 11, 54, 56, 59, 61):
1056 if int(vat[4:6]) < 1 or int(vat[4:6]) > 30:
1058 if int(vat[2:4]) in (1, 3, 5, 7, 8, 10, 12,
1059 51, 53, 55, 57, 58, 60, 62):
1060 if int(vat[4:6]) < 1 or int(vat[4:6]) > 31:
1066 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: