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 fields
26 from tools.func import partial
27 from tools.translate import _
34 """Sum each digits of the multiplication of i and j."""
35 return reduce(lambda x, y: x + int(y), str(i*j), 0)
37 class res_partner(osv.osv):
38 _inherit = 'res.partner'
40 def _split_vat(self, vat):
41 vat_country, vat_number = vat[:2].lower(), vat[2:].replace(' ', '')
42 return vat_country, vat_number
44 def check_vat(self, cr, uid, ids):
46 Check the VAT number depending of the country.
47 http://sima-pc.com/nif.php
49 for partner in self.browse(cr, uid, ids):
52 vat_country, vat_number = self._split_vat(partner.vat)
53 if not hasattr(self, 'check_vat_' + vat_country):
55 check = getattr(self, 'check_vat_' + vat_country)
56 if not check(vat_number):
61 def vat_change(self, cr, uid, ids, value, context={}):
62 return {'value': {'vat_subjected': bool(value)}}
65 '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.")
68 def _construct_constraint_msg(self, cr, uid, ids):
69 def default_vat_check(cn, vn):
70 # by default, a VAT number is valid if:
71 # it starts with 2 letters
72 # has more than 3 characters
73 return cn[0] in string.ascii_lowercase and cn[1] in string.ascii_lowercase
75 vat_country, vat_number = self._split_vat(self.browse(cr, uid, ids)[0].vat)
76 if default_vat_check(vat_country, vat_number):
77 return _('The Vat does not seems to be correct. You should have entered something like this %s'), (_ref_vat[vat_country])
78 return _('The VAT is invalid, it should begin with the country code'), ()
80 _constraints = [(check_vat, _construct_constraint_msg, ["vat"])]
82 # code from the following methods come from Tryton (B2CK)
83 # http://www.tryton.org/hgwebdir.cgi/modules/relationship/file/544d1de586d9/party.py
84 def check_vat_at(self, vat):
86 Check Austria VAT number.
97 sum = int(num[0]) + mult_add(2, int(num[1])) + \
98 int(num[2]) + mult_add(2, int(num[3])) + \
99 int(num[4]) + mult_add(2, int(num[5])) + \
101 check = 10 - ((sum + 4) % 10)
104 if int(vat[-1:]) != check:
108 def check_vat_be(self, vat):
110 Check Belgium VAT number.
118 if int(vat[-2:]) != \
119 97 - (int(vat[:8]) % 97):
123 def check_vat_bg(self, vat):
125 Check Bulgaria VAT number.
127 if len(vat) not in [9,10]:
133 if int(vat[0]) in (2, 3) and \
136 sum = 4 * int(vat[0]) + 3 * int(vat[1]) + 2 * int(vat[2]) + \
137 7 * int(vat[3]) + 6 * int(vat[4]) + 5 * int(vat[5]) + \
138 4 * int(vat[6]) + 3 * int(vat[7]) + 2 * int(vat[8])
139 check = 11 - (sum % 11)
144 # if check != int(vat[9]):
148 def check_vat_cy(self, vat):
150 Check Cyprus VAT number.
194 sum = n0 + n1 + n2 + n3 + n4 + n5 + n6 + n7
195 check = chr(sum % 26 + 65)
200 def check_vat_cz(self, vat):
202 Check Czech Republic VAT number.
204 if len(vat) not in (8, 9, 10):
212 if int(vat[0]) not in (0, 1, 2, 3, 4, 5, 6, 7, 8):
214 sum = 8 * int(vat[0]) + 7 * int(vat[1]) + 6 * int(vat[2]) + \
215 5 * int(vat[3]) + 4 * int(vat[4]) + 3 * int(vat[5]) + \
217 check = 11 - (sum % 11)
222 if check != int(vat[7]):
224 elif len(vat) == 9 and int(vat[0]) == 6:
225 sum = 8 * int(vat[1]) + 7 * int(vat[2]) + 6 * int(vat[3]) + \
226 5 * int(vat[4]) + 4 * int(vat[5]) + 3 * int(vat[6]) + \
228 check = 9 - ((11 - (sum % 11)) % 10)
229 if check != int(vat[8]):
232 if int(vat[0:2]) > 53 and int(vat[0:2]) < 80:
234 if int(vat[2:4]) < 1:
236 if int(vat[2:4]) > 12 and int(vat[2:4]) < 51:
238 if int(vat[2:4]) > 62:
240 if int(vat[2:4]) in (2, 52) and int(vat[0:2]) % 4 > 0:
241 if int(vat[4:6]) < 1:
243 if int(vat[4:6]) > 28:
245 if int(vat[2:4]) in (2, 52) and int(vat[0:2]) % 4 == 0:
246 if int(vat[4:6]) < 1:
248 if int(vat[4:6]) > 29:
250 if int(vat[2:4]) in (4, 6, 9, 11, 54, 56, 59, 61):
251 if int(vat[4:6]) < 1:
253 if int(vat[4:6]) > 30:
255 if int(vat[2:4]) in (1, 3, 5, 7, 8, 10, 12, 51,
256 53, 55, 57, 58, 60, 62):
257 if int(vat[4:6]) < 1:
259 if int(vat[4:6]) > 31:
262 if int(vat[0:2]) < 54:
264 if int(vat[2:4]) < 1:
266 if int(vat[2:4]) > 12 and int(vat[2:4]) < 51:
268 if int(vat[2:4]) > 62:
270 if int(vat[2:4]) in (2, 52) and int(vat[0:2]) % 4 > 0:
271 if int(vat[4:6]) < 1:
273 if int(vat[4:6]) > 28:
275 if int(vat[2:4]) in (2, 52) and int(vat[0:2]) % 4 == 0:
276 if int(vat[4:6]) < 1:
278 if int(vat[4:6]) > 29:
280 if int(vat[2:4]) in (4, 6, 9, 11, 54, 56, 59, 61):
281 if int(vat[4:6]) < 1:
283 if int(vat[4:6]) > 30:
285 if int(vat[2:4]) in (1, 3, 5, 7, 8, 10, 12, 51,
286 53, 55, 57, 58, 60, 62):
287 if int(vat[4:6]) < 1:
289 if int(vat[4:6]) > 31:
291 if (int(vat[0:2]) + int(vat[2:4]) + int(vat[4:6]) + int(vat[6:8]) +
292 int(vat[8:10])) % 11 != 0:
294 if int(vat[0:10]) % 11 != 0:
298 def check_vat_de(self, vat):
300 Check Germany VAT number.
308 if int(vat[0:7]) <= 0:
312 sum = (2 * ((int(vat[i]) + sum + 9) % 10 + 1)) % 11
316 if int(vat[8]) != check:
320 def check_vat_dk(self, vat):
322 Check Denmark VAT number.
332 sum = 2 * int(vat[0]) + 7 * int(vat[1]) + 6 * int(vat[2]) + \
333 5 * int(vat[3]) + 4 * int(vat[4]) + 3 * int(vat[5]) + \
334 2 * int(vat[6]) + int(vat[7])
339 def check_vat_ee(self, vat):
341 Check Estonia VAT number.
349 sum = 3 * int(vat[0]) + 7 * int(vat[1]) + 1 * int(vat[2]) + \
350 3 * int(vat[3]) + 7 * int(vat[4]) + 1 * int(vat[5]) + \
351 3 * int(vat[6]) + 7 * int(vat[7])
352 check = 10 - (sum % 10)
355 if check != int(vat[8]):
359 def check_vat_es(self, vat):
361 Check Spain VAT number.
391 #Legal persons with profit aim
392 if vat[0] in ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'U', 'V'):
397 sum = mult_add(2, int(vat[1])) + int(vat[2]) + \
398 mult_add(2, int(vat[3])) + int(vat[4]) + \
399 mult_add(2, int(vat[5])) + int(vat[6]) + \
400 mult_add(2, int(vat[7]))
401 check = 10 - (sum % 10)
404 # if check != int(vat[8]):
407 #Legal persons with non-profit aim
408 elif vat[0] in ('N', 'P', 'Q', 'R', 'S', 'W'):
413 sum = mult_add(2, int(vat[1])) + int(vat[2]) + \
414 mult_add(2, int(vat[3])) + int(vat[4]) + \
415 mult_add(2, int(vat[5])) + int(vat[6]) + \
416 mult_add(2, int(vat[7]))
417 check = 10 - (sum % 10)
418 check = chr(check + 64)
422 #Foreign natural persons, under age 14 or non-residents
423 elif vat[0] in ('K', 'L', 'M', 'X', 'Y', 'Z'):
425 check_value = '1' + vat[1:8]
427 check_value = '2' + vat[1:8]
429 check_value = vat[1:8]
435 check = 1 + (int(check_value) % 23)
441 #Spanish natural persons
447 check = 1 + (int(vat[:8]) % 23)
454 def check_vat_fi(self, vat):
456 Check Finland VAT number.
464 sum = 7 * int(vat[0]) + 9 * int(vat[1]) + 10 * int(vat[2]) + \
465 5 * int(vat[3]) + 8 * int(vat[4]) + 4 * int(vat[5]) + \
467 check = 11 - (sum % 11)
472 if check != int(vat[7]):
476 def check_vat_fr(self, vat):
478 Check France VAT number.
496 check = ((int(vat[2:11]) * 100) + 12) % 97
497 if check != int(vat[0:2]):
501 conv = ['0', '1', '2', '3', '4', '5', '6', '7',
502 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
503 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T',
504 'U', 'V', 'W', 'X', 'Y', 'Z']
505 if vat[0] not in conv \
506 or vat[1] not in conv:
508 c1 = conv.index(vat[0])
509 c2 = conv.index(vat[1])
512 sum = c1 * 24 + c2 - 10
514 sum = c1 * 34 + c2 - 100
517 sum = (int(sum) / 11) + 1
518 y = (int(vat[2:11]) + sum) % 11
523 def check_vat_gb(self, vat):
525 Check United Kingdom VAT number.
535 if int(vat[2:5]) >= 500:
539 if int(vat[2:5]) < 500:
543 elif len(vat) in (9, 10):
549 if int(vat[0:7]) < 1:
551 if int(vat[0:7]) > 19999 and int(vat[0:7]) < 1000000:
553 if int(vat[7:9]) > 97:
555 if len(vat) == 10 and int(vat[9]) != 3:
558 sum = 8 * int(vat[0]) + 7 * int(vat[1]) + 6 * int(vat[2]) + \
559 5 * int(vat[3]) + 4 * int(vat[4]) + 3 * int(vat[5]) + \
560 2 * int(vat[6]) + 10 * int(vat[7]) + int(vat[8])
564 elif len(vat) in (12, 13):
570 if int(vat[0:3]) not in (0, 1):
573 if int(vat[3:10]) < 1:
575 if int(vat[3:10]) > 19999 and int(vat[3:10]) < 1000000:
577 if int(vat[10:12]) > 97:
579 if len(vat) == 13 and int(vat[12]) != 3:
582 sum = 8 * int(vat[3]) + 7 * int(vat[4]) + 6 * int(vat[5]) + \
583 5 * int(vat[6]) + 4 * int(vat[7]) + 3 * int(vat[8]) + \
584 2 * int(vat[9]) + 10 * int(vat[10]) + int(vat[11])
590 def check_vat_gr(self, vat):
592 Check Greece VAT number.
599 sum = 128 * int(vat[0]) + 64 * int(vat[1]) + 32 * int(vat[2]) + \
600 16 * int(vat[3]) + 8 * int(vat[4]) + 4 * int(vat[5]) + \
605 if check != int(vat[7]):
609 sum = 256 * int(vat[0]) + 128 * int(vat[1]) + 64 * int(vat[2]) + \
610 32 * int(vat[3]) + 16 * int(vat[4]) + 8 * int(vat[5]) + \
611 4 * int(vat[6]) + 2 * int(vat[7])
615 if check != int(vat[8]):
620 def check_vat_el(self, vat):
621 return self.check_vat_gr(vat)
623 def check_vat_hu(self, vat):
625 Check Hungary VAT number.
635 sum = 9 * int(vat[0]) + 7 * int(vat[1]) + 3 * int(vat[2]) + \
636 1 * int(vat[3]) + 9 * int(vat[4]) + 7 * int(vat[5]) + \
638 check = 10 - (sum % 10)
641 if check != int(vat[7]):
645 def check_vat_ie(self, vat):
647 Check Ireland VAT number.
651 if (ord(vat[1]) >= 65 and ord(vat[1]) <= 90) \
652 or vat[1] in ('+', '*'):
662 sum = 7 * int(vat[2]) + 6 * int(vat[3]) + 5 * int(vat[4]) + \
663 4 * int(vat[5]) + 3 * int(vat[6]) + 2 * int(vat[0])
668 check = chr(check + 64)
678 sum = 8 * int(vat[0]) + 7 * int(vat[1]) + 6 * int(vat[2]) + \
679 5 * int(vat[3]) + 4 * int(vat[4]) + 3 * int(vat[5]) + \
685 check = chr(check + 64)
690 def check_vat_it(self, vat):
692 Check Italy VAT number.
700 if int(vat[0:7]) <= 0:
702 if int(vat[7:10]) <= 0:
704 if int(vat[7:10]) > 100 and int(vat[7:10]) < 120:
706 if int(vat[7:10]) > 121:
709 sum = int(vat[0]) + mult_add(2, int(vat[1])) + int(vat[2]) + \
710 mult_add(2, int(vat[3])) + int(vat[4]) + \
711 mult_add(2, int(vat[5])) + int(vat[6]) + \
712 mult_add(2, int(vat[7])) + int(vat[8]) + \
713 mult_add(2, int(vat[9]))
714 check = 10 - (sum % 10)
717 if check != int(vat[10]):
721 def check_vat_lt(self, vat):
723 Check Lithuania VAT number.
733 sum = 1 * int(vat[0]) + 2 * int(vat[1]) + 3 * int(vat[2]) + \
734 4 * int(vat[3]) + 5 * int(vat[4]) + 6 * int(vat[5]) + \
735 7 * int(vat[6]) + 8 * int(vat[7])
737 sum = 3 * int(vat[0]) + 4 * int(vat[1]) + 5 * int(vat[2]) + \
738 6 * int(vat[3]) + 7 * int(vat[4]) + 8 * int(vat[5]) + \
739 9 * int(vat[6]) + 1 * int(vat[7])
743 if check != int(vat[8]):
747 if int(vat[10]) != 1:
749 sum = 1 * int(vat[0]) + 2 * int(vat[1]) + 3 * int(vat[2]) + \
750 4 * int(vat[3]) + 5 * int(vat[4]) + 6 * int(vat[5]) + \
751 7 * int(vat[6]) + 8 * int(vat[7]) + 9 * int(vat[8]) + \
752 1 * int(vat[9]) + 2 * int(vat[10])
754 sum = 3 * int(vat[0]) + 4 * int(vat[1]) + 5 * int(vat[2]) + \
755 6 * int(vat[3]) + 7 * int(vat[4]) + 8 * int(vat[5]) + \
756 9 * int(vat[6]) + 1 * int(vat[7]) + 2 * int(vat[8]) + \
757 3 * int(vat[9]) + 4 * int(vat[10])
761 if check != int(vat[11]):
766 def check_vat_lu(self, vat):
768 Check Luxembourg VAT number.
776 if int(vat[0:6]) <= 0:
778 check = int(vat[0:6]) % 89
779 if check != int(vat[6:8]):
783 def check_vat_lv(self, vat):
785 Check Latvia VAT number.
794 sum = 9 * int(vat[0]) + 1 * int(vat[1]) + 4 * int(vat[2]) + \
795 8 * int(vat[3]) + 3 * int(vat[4]) + 10 * int(vat[5]) + \
796 2 * int(vat[6]) + 5 * int(vat[7]) + 7 * int(vat[8]) + \
798 if sum % 11 == 4 and int(vat[0]) == 9:
801 check = 4 - (sum % 11)
803 check = 14 - (sum % 11)
805 check = 3 - (sum % 11)
806 if check != int(vat[10]):
810 if int(vat[2:4]) == 2 and int(vat[4:6]) % 4 > 0:
811 if int(vat[0:2]) < 1 or int(vat[0:2]) > 28:
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]) > 29:
816 if int(vat[2:4]) in (4, 6, 9, 11):
817 if int(vat[0:2]) < 1 or int(vat[0:2]) > 30:
819 if int(vat[2:4]) in (1, 3, 5, 7, 8, 10, 12):
820 if int(vat[0:2]) < 1 or int(vat[0:2]) > 31:
822 if int(vat[2:4]) < 1 or int(vat[2:4]) > 12:
826 def check_vat_mt(self, vat):
828 Check Malta VAT number.
837 if int(vat[0:6]) < 100000:
840 sum = 3 * int(vat[0]) + 4 * int(vat[1]) + 6 * int(vat[2]) + \
841 7 * int(vat[3]) + 8 * int(vat[4]) + 9 * int(vat[5])
842 check = 37 - (sum % 37)
843 if check != int(vat[6:8]):
847 def check_vat_nl(self, vat):
849 Check Netherlands VAT number.
858 if int(vat[0:8]) <= 0:
863 sum = 9 * int(vat[0]) + 8 * int(vat[1]) + 7 * int(vat[2]) + \
864 6 * int(vat[3]) + 5 * int(vat[4]) + 4 * int(vat[5]) + \
865 3 * int(vat[6]) + 2 * int(vat[7])
870 if check != int(vat[8]):
874 def check_vat_pl(self, vat):
876 Check Poland VAT number.
885 sum = 6 * int(vat[0]) + 5 * int(vat[1]) + 7 * int(vat[2]) + \
886 2 * int(vat[3]) + 3 * int(vat[4]) + 4 * int(vat[5]) + \
887 5 * int(vat[6]) + 6 * int(vat[7]) + 7 * int(vat[8])
891 if check != int(vat[9]):
895 def check_vat_pt(self, vat):
897 Check Portugal VAT number.
909 sum = 9 * int(vat[0]) + 8 * int(vat[1]) + 7 * int(vat[2]) + \
910 6 * int(vat[3]) + 5 * int(vat[4]) + 4 * int(vat[5]) + \
911 3 * int(vat[6]) + 2 * int(vat[7])
912 check = 11 - (sum % 11)
913 if check == 10 or check == 11:
915 # if check != int(vat[8]):
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 # if int(vat[-2:]) != 1:
986 sum = mult_add(2, int(vat[0])) + int(vat[1]) + \
987 mult_add(2, int(vat[2])) + int(vat[3]) + \
988 mult_add(2, int(vat[4])) + int(vat[5]) + \
989 mult_add(2, int(vat[6])) + int(vat[7]) + \
990 mult_add(2, int(vat[8]))
991 check = 10 - (sum % 10)
994 if check != int(vat[9]):
998 def check_vat_si(self, vat):
1000 Check Slovenia VAT number.
1008 if int(vat[0:7]) <= 999999:
1011 sum = 8 * int(vat[0]) + 7 * int(vat[1]) + 6 * int(vat[2]) + \
1012 5 * int(vat[3]) + 4 * int(vat[4]) + 3 * int(vat[5]) + \
1014 check = 11 - (sum % 11)
1019 if check != int(vat[7]):
1023 def check_vat_sk(self, vat):
1025 Check Slovakia VAT number.
1031 if len(vat) not in(9, 10):
1034 if int(vat[0:2]) == 0 and len(vat) == 10:
1038 if int(vat[0:2]) < 54 or int(vat[0:2]) > 99:
1042 if int(vat[0:2]) > 53 :
1045 if int(vat[2:4]) < 1:
1047 if int(vat[2:4]) > 12 and int(vat[2:4]) < 51:
1049 if int(vat[2:4]) > 62:
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]) > 28:
1054 if int(vat[2:4]) in (2, 52) and int(vat[0:2]) % 4 == 0:
1055 if int(vat[4:6]) < 1 or int(vat[4:6]) > 29:
1057 if int(vat[2:4]) in (4, 6, 9, 11, 54, 56, 59, 61):
1058 if int(vat[4:6]) < 1 or int(vat[4:6]) > 30:
1060 if int(vat[2:4]) in (1, 3, 5, 7, 8, 10, 12,
1061 51, 53, 55, 57, 58, 60, 62):
1062 if int(vat[4:6]) < 1 or int(vat[4:6]) > 31:
1068 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: