1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 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
29 """Sum each digits of the multiplication of i and j."""
30 return reduce(lambda x, y: x + int(y), str(i*j), 0)
32 class res_partner(osv.osv):
33 _inherit = 'res.partner'
35 def check_vat(self, cr, uid, ids):
37 Check the VAT number depending of the country.
38 http://sima-pc.com/nif.php
40 for partner in self.browse(cr, uid, ids):
42 continue #FIXME return False? empty vat numbre is invalid?
44 vat_country, vat_number = partner.vat[:2].lower(), partner.vat[2:].replace(' ', '')
45 check = getattr(self, 'check_vat_' + vat_country)
46 if not check(vat_number):
51 def __getattr__(self, attr):
52 if not attr.startswith('check_vat_'):
53 super(res_partner, self).__getattr__(attr)
55 def default_vat_check(self, cn, vn):
56 # by default, a VAT number is valid if:
57 # it starts with 2 letters
58 # has more than 3 characters
59 return len(vn) > 0 and len(cn) == 2 and cn[0] in string.ascii_lowercase and cn[1] in string.ascii_lowercase
61 return partial(default_vat_check, self, attr[10:])
64 def vat_change(self, cr, uid, ids, value, context={}):
65 return {'value': {'vat_subjected': bool(value)}}
68 '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.")
70 _constraints = [(check_vat, "The VAT doesn't seem to be correct.", ["vat"])]
72 # code from the following methods come from Tryton (B2CK)
73 # http://www.tryton.org/hgwebdir.cgi/modules/relationship/file/544d1de586d9/party.py
74 def check_vat_at(self, vat):
76 Check Austria VAT number.
87 sum = int(num[0]) + mult_add(2, int(num[1])) + \
88 int(num[2]) + mult_add(2, int(num[3])) + \
89 int(num[4]) + mult_add(2, int(num[5])) + \
91 check = 10 - ((sum + 4) % 10)
94 if int(vat[-1:]) != check:
98 def check_vat_be(self, vat):
100 Check Belgium VAT number.
108 if int(vat[-2:]) != \
109 97 - (int(vat[:8]) % 97):
113 def check_vat_bg(self, vat):
115 Check Bulgaria VAT number.
117 if len(vat) not in [9,10]:
123 if int(vat[0]) in (2, 3) and \
126 sum = 4 * int(vat[0]) + 3 * int(vat[1]) + 2 * int(vat[2]) + \
127 7 * int(vat[3]) + 6 * int(vat[4]) + 5 * int(vat[5]) + \
128 4 * int(vat[6]) + 3 * int(vat[7]) + 2 * int(vat[8])
129 check = 11 - (sum % 11)
134 # if check != int(vat[9]):
138 def check_vat_cy(self, vat):
140 Check Cyprus VAT number.
184 sum = n0 + n1 + n2 + n3 + n4 + n5 + n6 + n7
185 check = chr(sum % 26 + 65)
190 def check_vat_cz(self, vat):
192 Check Czech Republic VAT number.
194 if len(vat) not in (8, 9, 10):
202 if int(vat[0]) not in (0, 1, 2, 3, 4, 5, 6, 7, 8):
204 sum = 8 * int(vat[0]) + 7 * int(vat[1]) + 6 * int(vat[2]) + \
205 5 * int(vat[3]) + 4 * int(vat[4]) + 3 * int(vat[5]) + \
207 check = 11 - (sum % 11)
212 if check != int(vat[7]):
214 elif len(vat) == 9 and int(vat[0]) == 6:
215 sum = 8 * int(vat[1]) + 7 * int(vat[2]) + 6 * int(vat[3]) + \
216 5 * int(vat[4]) + 4 * int(vat[5]) + 3 * int(vat[6]) + \
218 check = 9 - ((11 - (sum % 11)) % 10)
219 if check != int(vat[8]):
222 if int(vat[0:2]) > 53 and int(vat[0:2]) < 80:
224 if int(vat[2:4]) < 1:
226 if int(vat[2:4]) > 12 and int(vat[2:4]) < 51:
228 if int(vat[2:4]) > 62:
230 if int(vat[2:4]) in (2, 52) and int(vat[0:2]) % 4 > 0:
231 if int(vat[4:6]) < 1:
233 if int(vat[4:6]) > 28:
235 if int(vat[2:4]) in (2, 52) and int(vat[0:2]) % 4 == 0:
236 if int(vat[4:6]) < 1:
238 if int(vat[4:6]) > 29:
240 if int(vat[2:4]) in (4, 6, 9, 11, 54, 56, 59, 61):
241 if int(vat[4:6]) < 1:
243 if int(vat[4:6]) > 30:
245 if int(vat[2:4]) in (1, 3, 5, 7, 8, 10, 12, 51,
246 53, 55, 57, 58, 60, 62):
247 if int(vat[4:6]) < 1:
249 if int(vat[4:6]) > 31:
252 if int(vat[0:2]) < 54:
254 if int(vat[2:4]) < 1:
256 if int(vat[2:4]) > 12 and int(vat[2:4]) < 51:
258 if int(vat[2:4]) > 62:
260 if int(vat[2:4]) in (2, 52) and int(vat[0:2]) % 4 > 0:
261 if int(vat[4:6]) < 1:
263 if int(vat[4:6]) > 28:
265 if int(vat[2:4]) in (2, 52) and int(vat[0:2]) % 4 == 0:
266 if int(vat[4:6]) < 1:
268 if int(vat[4:6]) > 29:
270 if int(vat[2:4]) in (4, 6, 9, 11, 54, 56, 59, 61):
271 if int(vat[4:6]) < 1:
273 if int(vat[4:6]) > 30:
275 if int(vat[2:4]) in (1, 3, 5, 7, 8, 10, 12, 51,
276 53, 55, 57, 58, 60, 62):
277 if int(vat[4:6]) < 1:
279 if int(vat[4:6]) > 31:
281 if (int(vat[0:2]) + int(vat[2:4]) + int(vat[4:6]) + int(vat[6:8]) +
282 int(vat[8:10])) % 11 != 0:
284 if int(vat[0:10]) % 11 != 0:
288 def check_vat_de(self, vat):
290 Check Germany VAT number.
298 if int(vat[0:7]) <= 0:
302 sum = (2 * ((int(vat[i]) + sum + 9) % 10 + 1)) % 11
306 if int(vat[8]) != check:
310 def check_vat_dk(self, vat):
312 Check Denmark VAT number.
322 sum = 2 * int(vat[0]) + 7 * int(vat[1]) + 6 * int(vat[2]) + \
323 5 * int(vat[3]) + 4 * int(vat[4]) + 3 * int(vat[5]) + \
324 2 * int(vat[6]) + int(vat[7])
329 def check_vat_ee(self, vat):
331 Check Estonia VAT number.
339 sum = 3 * int(vat[0]) + 7 * int(vat[1]) + 1 * int(vat[2]) + \
340 3 * int(vat[3]) + 7 * int(vat[4]) + 1 * int(vat[5]) + \
341 3 * int(vat[6]) + 7 * int(vat[7])
342 check = 10 - (sum % 10)
345 if check != int(vat[8]):
349 def check_vat_es(self, vat):
351 Check Spain VAT number.
381 #Legal persons with profit aim
382 if vat[0] in ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'U', 'V'):
387 sum = mult_add(2, int(vat[1])) + int(vat[2]) + \
388 mult_add(2, int(vat[3])) + int(vat[4]) + \
389 mult_add(2, int(vat[5])) + int(vat[6]) + \
390 mult_add(2, int(vat[7]))
391 check = 10 - (sum % 10)
394 # if check != int(vat[8]):
397 #Legal persons with non-profit aim
398 elif vat[0] in ('N', 'P', 'Q', 'R', 'S', 'W'):
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)
408 check = chr(check + 64)
412 #Foreign natural persons, under age 14 or non-residents
413 elif vat[0] in ('K', 'L', 'M', 'X', 'Y', 'Z'):
415 check_value = '1' + vat[1:8]
417 check_value = '2' + vat[1:8]
419 check_value = vat[1:8]
425 check = 1 + (int(check_value) % 23)
431 #Spanish natural persons
437 check = 1 + (int(vat[:8]) % 23)
444 def check_vat_fi(self, vat):
446 Check Finland VAT number.
454 sum = 7 * int(vat[0]) + 9 * int(vat[1]) + 10 * int(vat[2]) + \
455 5 * int(vat[3]) + 8 * int(vat[4]) + 4 * int(vat[5]) + \
457 check = 11 - (sum % 11)
462 if check != int(vat[7]):
466 def check_vat_fr(self, vat):
468 Check France VAT number.
486 check = ((int(vat[2:11]) * 100) + 12) % 97
487 if check != int(vat[0:2]):
491 conv = ['0', '1', '2', '3', '4', '5', '6', '7',
492 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
493 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T',
494 'U', 'V', 'W', 'X', 'Y', 'Z']
495 if vat[0] not in conv \
496 or vat[1] not in conv:
498 c1 = conv.index(vat[0])
499 c2 = conv.index(vat[1])
502 sum = c1 * 24 + c2 - 10
504 sum = c1 * 34 + c2 - 100
507 sum = (int(sum) / 11) + 1
508 y = (int(vat[2:11]) + sum) % 11
513 def check_vat_gb(self, vat):
515 Check United Kingdom VAT number.
525 if int(vat[2:5]) >= 500:
529 if int(vat[2:5]) < 500:
533 elif len(vat) in (9, 10):
539 if int(vat[0:7]) < 1:
541 if int(vat[0:7]) > 19999 and int(vat[0:7]) < 1000000:
543 if int(vat[7:9]) > 97:
545 if len(vat) == 10 and int(vat[9]) != 3:
548 sum = 8 * int(vat[0]) + 7 * int(vat[1]) + 6 * int(vat[2]) + \
549 5 * int(vat[3]) + 4 * int(vat[4]) + 3 * int(vat[5]) + \
550 2 * int(vat[6]) + 10 * int(vat[7]) + int(vat[8])
554 elif len(vat) in (12, 13):
560 if int(vat[0:3]) not in (0, 1):
563 if int(vat[3:10]) < 1:
565 if int(vat[3:10]) > 19999 and int(vat[3:10]) < 1000000:
567 if int(vat[10:12]) > 97:
569 if len(vat) == 13 and int(vat[12]) != 3:
572 sum = 8 * int(vat[3]) + 7 * int(vat[4]) + 6 * int(vat[5]) + \
573 5 * int(vat[6]) + 4 * int(vat[7]) + 3 * int(vat[8]) + \
574 2 * int(vat[9]) + 10 * int(vat[10]) + int(vat[11])
580 def check_vat_gr(self, vat):
582 Check Greece VAT number.
589 sum = 128 * int(vat[0]) + 64 * int(vat[1]) + 32 * int(vat[2]) + \
590 16 * int(vat[3]) + 8 * int(vat[4]) + 4 * int(vat[5]) + \
595 if check != int(vat[7]):
599 sum = 256 * int(vat[0]) + 128 * int(vat[1]) + 64 * int(vat[2]) + \
600 32 * int(vat[3]) + 16 * int(vat[4]) + 8 * int(vat[5]) + \
601 4 * int(vat[6]) + 2 * int(vat[7])
605 if check != int(vat[8]):
610 def check_vat_el(self, vat):
611 return self.check_vat_gr(vat)
613 def check_vat_hu(self, vat):
615 Check Hungary VAT number.
625 sum = 9 * int(vat[0]) + 7 * int(vat[1]) + 3 * int(vat[2]) + \
626 1 * int(vat[3]) + 9 * int(vat[4]) + 7 * int(vat[5]) + \
628 check = 10 - (sum % 10)
631 if check != int(vat[7]):
635 def check_vat_ie(self, vat):
637 Check Ireland VAT number.
641 if (ord(vat[1]) >= 65 and ord(vat[1]) <= 90) \
642 or vat[1] in ('+', '*'):
652 sum = 7 * int(vat[2]) + 6 * int(vat[3]) + 5 * int(vat[4]) + \
653 4 * int(vat[5]) + 3 * int(vat[6]) + 2 * int(vat[0])
658 check = chr(check + 64)
668 sum = 8 * int(vat[0]) + 7 * int(vat[1]) + 6 * int(vat[2]) + \
669 5 * int(vat[3]) + 4 * int(vat[4]) + 3 * int(vat[5]) + \
675 check = chr(check + 64)
680 def check_vat_it(self, vat):
682 Check Italy VAT number.
690 if int(vat[0:7]) <= 0:
692 if int(vat[7:10]) <= 0:
694 if int(vat[7:10]) > 100 and int(vat[7:10]) < 120:
696 if int(vat[7:10]) > 121:
699 sum = int(vat[0]) + mult_add(2, int(vat[1])) + int(vat[2]) + \
700 mult_add(2, int(vat[3])) + int(vat[4]) + \
701 mult_add(2, int(vat[5])) + int(vat[6]) + \
702 mult_add(2, int(vat[7])) + int(vat[8]) + \
703 mult_add(2, int(vat[9]))
704 check = 10 - (sum % 10)
707 if check != int(vat[10]):
711 def check_vat_lt(self, vat):
713 Check Lithuania VAT number.
723 sum = 1 * int(vat[0]) + 2 * int(vat[1]) + 3 * int(vat[2]) + \
724 4 * int(vat[3]) + 5 * int(vat[4]) + 6 * int(vat[5]) + \
725 7 * int(vat[6]) + 8 * int(vat[7])
727 sum = 3 * int(vat[0]) + 4 * int(vat[1]) + 5 * int(vat[2]) + \
728 6 * int(vat[3]) + 7 * int(vat[4]) + 8 * int(vat[5]) + \
729 9 * int(vat[6]) + 1 * int(vat[7])
733 if check != int(vat[8]):
737 if int(vat[10]) != 1:
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]) + 9 * int(vat[8]) + \
742 1 * int(vat[9]) + 2 * int(vat[10])
744 sum = 3 * int(vat[0]) + 4 * int(vat[1]) + 5 * int(vat[2]) + \
745 6 * int(vat[3]) + 7 * int(vat[4]) + 8 * int(vat[5]) + \
746 9 * int(vat[6]) + 1 * int(vat[7]) + 2 * int(vat[8]) + \
747 3 * int(vat[9]) + 4 * int(vat[10])
751 if check != int(vat[11]):
756 def check_vat_lu(self, vat):
758 Check Luxembourg VAT number.
766 if int(vat[0:6]) <= 0:
768 check = int(vat[0:6]) % 89
769 if check != int(vat[6:8]):
773 def check_vat_lv(self, vat):
775 Check Latvia VAT number.
784 sum = 9 * int(vat[0]) + 1 * int(vat[1]) + 4 * int(vat[2]) + \
785 8 * int(vat[3]) + 3 * int(vat[4]) + 10 * int(vat[5]) + \
786 2 * int(vat[6]) + 5 * int(vat[7]) + 7 * int(vat[8]) + \
788 if sum % 11 == 4 and int(vat[0]) == 9:
791 check = 4 - (sum % 11)
793 check = 14 - (sum % 11)
795 check = 3 - (sum % 11)
796 if check != int(vat[10]):
800 if int(vat[2:4]) == 2 and int(vat[4:6]) % 4 > 0:
801 if int(vat[0:2]) < 1 or int(vat[0:2]) > 28:
803 if int(vat[2:4]) == 2 and int(vat[4:6]) % 4 == 0:
804 if int(vat[0:2]) < 1 or int(vat[0:2]) > 29:
806 if int(vat[2:4]) in (4, 6, 9, 11):
807 if int(vat[0:2]) < 1 or int(vat[0:2]) > 30:
809 if int(vat[2:4]) in (1, 3, 5, 7, 8, 10, 12):
810 if int(vat[0:2]) < 1 or int(vat[0:2]) > 31:
812 if int(vat[2:4]) < 1 or int(vat[2:4]) > 12:
816 def check_vat_mt(self, vat):
818 Check Malta VAT number.
827 if int(vat[0:6]) < 100000:
830 sum = 3 * int(vat[0]) + 4 * int(vat[1]) + 6 * int(vat[2]) + \
831 7 * int(vat[3]) + 8 * int(vat[4]) + 9 * int(vat[5])
832 check = 37 - (sum % 37)
833 if check != int(vat[6:8]):
837 def check_vat_nl(self, vat):
839 Check Netherlands VAT number.
848 if int(vat[0:8]) <= 0:
853 sum = 9 * int(vat[0]) + 8 * int(vat[1]) + 7 * int(vat[2]) + \
854 6 * int(vat[3]) + 5 * int(vat[4]) + 4 * int(vat[5]) + \
855 3 * int(vat[6]) + 2 * int(vat[7])
860 if check != int(vat[8]):
864 def check_vat_pl(self, vat):
866 Check Poland VAT number.
875 sum = 6 * int(vat[0]) + 5 * int(vat[1]) + 7 * int(vat[2]) + \
876 2 * int(vat[3]) + 3 * int(vat[4]) + 4 * int(vat[5]) + \
877 5 * int(vat[6]) + 6 * int(vat[7]) + 7 * int(vat[8])
881 if check != int(vat[9]):
885 def check_vat_pt(self, vat):
887 Check Portugal VAT number.
899 sum = 9 * int(vat[0]) + 8 * int(vat[1]) + 7 * int(vat[2]) + \
900 6 * int(vat[3]) + 5 * int(vat[4]) + 4 * int(vat[5]) + \
901 3 * int(vat[6]) + 2 * int(vat[7])
902 check = 11 - (sum % 11)
903 if check == 10 or check == 11:
905 # if check != int(vat[8]):
909 def check_vat_ro(self, vat):
911 Check Romania VAT number.
918 if len(vat) >= 2 and len(vat) <= 10:
919 vat = (10 - len(vat)) * '0' + vat
920 sum = 7 * int(vat[0]) + 5 * int(vat[1]) + 3 * int(vat[2]) + \
921 2 * int(vat[3]) + 1 * int(vat[4]) + 7 * int(vat[5]) + \
922 5 * int(vat[6]) + 3 * int(vat[7]) + 2 * int(vat[8])
923 check = (sum * 10) % 11
926 if check != int(vat[9]):
930 if int(vat[0]) not in (1, 2, 3, 4, 6):
932 if int(vat[3:5]) < 1 or int(vat[3:5]) > 12:
934 if int(vat[3:5]) == 2 and int(vat[1:3]) % 4 > 0:
935 if int(vat[5:7]) < 1 or int(vat[5:7]) > 28:
937 if int(vat[3:5]) == 2 and int(vat[1:3]) % 4 == 0:
938 if int(vat[5:7]) < 1 or int(vat[5:7]) > 29:
940 if int(vat[3:5]) in (4, 6, 9, 11):
941 if int(vat[5:7]) < 1 or int(vat[5:7]) > 30:
943 if int(vat[3:5]) in (1, 3, 5, 7, 8, 10, 12):
944 if int(vat[5:7]) < 1 or int(vat[5:7]) > 31:
947 sum = 2 * int(vat[0]) + 7 * int(vat[1]) + 9 * int(vat[2]) + \
948 1 * int(vat[3]) + 4 * int(vat[4]) + 6 * int(vat[5]) + \
949 3 * int(vat[6]) + 5 * int(vat[7]) + 8 * int(vat[8]) + \
950 2 * int(vat[9]) + 7 * int(vat[10]) + 9 * int(vat[11])
954 if check != int(vat[12]):
959 def check_vat_se(self, vat):
961 Check Sweden VAT number.
970 if int(vat[9:11]) < 0:
973 # if int(vat[-2:]) != 1:
976 sum = mult_add(2, int(vat[0])) + int(vat[1]) + \
977 mult_add(2, int(vat[2])) + int(vat[3]) + \
978 mult_add(2, int(vat[4])) + int(vat[5]) + \
979 mult_add(2, int(vat[6])) + int(vat[7]) + \
980 mult_add(2, int(vat[8]))
981 check = 10 - (sum % 10)
984 if check != int(vat[9]):
988 def check_vat_si(self, vat):
990 Check Slovenia VAT number.
998 if int(vat[0:7]) <= 999999:
1001 sum = 8 * int(vat[0]) + 7 * int(vat[1]) + 6 * int(vat[2]) + \
1002 5 * int(vat[3]) + 4 * int(vat[4]) + 3 * int(vat[5]) + \
1004 check = 11 - (sum % 11)
1009 if check != int(vat[7]):
1013 def check_vat_sk(self, vat):
1015 Check Slovakia VAT number.
1021 if len(vat) not in(9, 10):
1024 if int(vat[0:2]) == 0 and len(vat) == 10:
1028 if int(vat[0:2]) < 54 or int(vat[0:2]) > 99:
1032 if int(vat[0:2]) > 53 :
1035 if int(vat[2:4]) < 1:
1037 if int(vat[2:4]) > 12 and int(vat[2:4]) < 51:
1039 if int(vat[2:4]) > 62:
1041 if int(vat[2:4]) in (2, 52) and int(vat[0:2]) % 4 > 0:
1042 if int(vat[4:6]) < 1 or int(vat[4:6]) > 28:
1044 if int(vat[2:4]) in (2, 52) and int(vat[0:2]) % 4 == 0:
1045 if int(vat[4:6]) < 1 or int(vat[4:6]) > 29:
1047 if int(vat[2:4]) in (4, 6, 9, 11, 54, 56, 59, 61):
1048 if int(vat[4:6]) < 1 or int(vat[4:6]) > 30:
1050 if int(vat[2:4]) in (1, 3, 5, 7, 8, 10, 12,
1051 51, 53, 55, 57, 58, 60, 62):
1052 if int(vat[4:6]) < 1 or int(vat[4:6]) > 31:
1058 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: