PageRenderTime 35ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/addons/base_vat/base_vat.py

https://gitlab.com/thanhchatvn/cloud-odoo
Python | 330 lines | 251 code | 29 blank | 50 comment | 58 complexity | 229545d80426ad60c70a0d593f85e23d MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. import logging
  4. import string
  5. import datetime
  6. import re
  7. _logger = logging.getLogger(__name__)
  8. try:
  9. import vatnumber
  10. except ImportError:
  11. _logger.warning("VAT validation partially unavailable because the `vatnumber` Python library cannot be found. "
  12. "Install it to support more countries, for example with `easy_install vatnumber`.")
  13. vatnumber = None
  14. from openerp.osv import osv
  15. from openerp.tools.misc import ustr
  16. from openerp.tools.translate import _
  17. _ref_vat = {
  18. 'at': 'ATU12345675',
  19. 'be': 'BE0477472701',
  20. 'bg': 'BG1234567892',
  21. 'ch': 'CHE-123.456.788 TVA or CH TVA 123456', #Swiss by Yannick Vaucher @ Camptocamp
  22. 'cy': 'CY12345678F',
  23. 'cz': 'CZ12345679',
  24. 'de': 'DE123456788',
  25. 'dk': 'DK12345674',
  26. 'ee': 'EE123456780',
  27. 'el': 'EL12345670',
  28. 'es': 'ESA12345674',
  29. 'fi': 'FI12345671',
  30. 'fr': 'FR32123456789',
  31. 'gb': 'GB123456782',
  32. 'gr': 'GR12345670',
  33. 'hu': 'HU12345676',
  34. 'hr': 'HR01234567896', # Croatia, contributed by Milan Tribuson
  35. 'ie': 'IE1234567FA',
  36. 'it': 'IT12345670017',
  37. 'lt': 'LT123456715',
  38. 'lu': 'LU12345613',
  39. 'lv': 'LV41234567891',
  40. 'mt': 'MT12345634',
  41. 'mx': 'MXABC123456T1B',
  42. 'nl': 'NL123456782B90',
  43. 'no': 'NO123456785',
  44. 'pe': 'PER10254824220 or PED10254824220',
  45. 'pl': 'PL1234567883',
  46. 'pt': 'PT123456789',
  47. 'ro': 'RO1234567897',
  48. 'se': 'SE123456789701',
  49. 'si': 'SI12345679',
  50. 'sk': 'SK0012345675',
  51. 'tr': 'TR1234567890 (VERGINO) veya TR12345678901 (TCKIMLIKNO)' # Levent Karakas @ Eska Yazilim A.S.
  52. }
  53. class res_partner(osv.osv):
  54. _inherit = 'res.partner'
  55. def _split_vat(self, vat):
  56. vat_country, vat_number = vat[:2].lower(), vat[2:].replace(' ', '')
  57. return vat_country, vat_number
  58. def simple_vat_check(self, cr, uid, country_code, vat_number, context=None):
  59. '''
  60. Check the VAT number depending of the country.
  61. http://sima-pc.com/nif.php
  62. '''
  63. if not ustr(country_code).encode('utf-8').isalpha():
  64. return False
  65. check_func_name = 'check_vat_' + country_code
  66. check_func = getattr(self, check_func_name, None) or \
  67. getattr(vatnumber, check_func_name, None)
  68. if not check_func:
  69. # No VAT validation available, default to check that the country code exists
  70. if country_code.upper() == 'EU':
  71. # Foreign companies that trade with non-enterprises in the EU
  72. # may have a VATIN starting with "EU" instead of a country code.
  73. return True
  74. res_country = self.pool.get('res.country')
  75. return bool(res_country.search(cr, uid, [('code', '=ilike', country_code)], context=context))
  76. return check_func(vat_number)
  77. def vies_vat_check(self, cr, uid, country_code, vat_number, context=None):
  78. try:
  79. # Validate against VAT Information Exchange System (VIES)
  80. # see also http://ec.europa.eu/taxation_customs/vies/
  81. return vatnumber.check_vies(country_code.upper()+vat_number)
  82. except Exception:
  83. # see http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl
  84. # Fault code may contain INVALID_INPUT, SERVICE_UNAVAILABLE, MS_UNAVAILABLE,
  85. # TIMEOUT or SERVER_BUSY. There is no way we can validate the input
  86. # with VIES if any of these arise, including the first one (it means invalid
  87. # country code or empty VAT number), so we fall back to the simple check.
  88. return self.simple_vat_check(cr, uid, country_code, vat_number, context=context)
  89. def check_vat(self, cr, uid, ids, context=None):
  90. user_company = self.pool.get('res.users').browse(cr, uid, uid).company_id
  91. if user_company.vat_check_vies:
  92. # force full VIES online check
  93. check_func = self.vies_vat_check
  94. else:
  95. # quick and partial off-line checksum validation
  96. check_func = self.simple_vat_check
  97. for partner in self.browse(cr, uid, ids, context=context):
  98. if not partner.vat:
  99. continue
  100. vat_country, vat_number = self._split_vat(partner.vat)
  101. if not check_func(cr, uid, vat_country, vat_number, context=context):
  102. _logger.info("Importing VAT Number [%s] is not valid !", vat_number)
  103. return False
  104. return True
  105. def _construct_constraint_msg(self, cr, uid, ids, context=None):
  106. def default_vat_check(cn, vn):
  107. # by default, a VAT number is valid if:
  108. # it starts with 2 letters
  109. # has more than 3 characters
  110. return cn[0] in string.ascii_lowercase and cn[1] in string.ascii_lowercase
  111. vat_country, vat_number = self._split_vat(self.browse(cr, uid, ids)[0].vat)
  112. vat_no = "'CC##' (CC=Country Code, ##=VAT Number)"
  113. error_partner = self.browse(cr, uid, ids, context=context)
  114. if default_vat_check(vat_country, vat_number):
  115. vat_no = _ref_vat[vat_country] if vat_country in _ref_vat else vat_no
  116. if self.pool['res.users'].browse(cr, uid, uid).company_id.vat_check_vies:
  117. return '\n' + _('The VAT number [%s] for partner [%s] either failed the VIES VAT validation check or did not respect the expected format %s.') % (error_partner[0].vat, error_partner[0].name, vat_no)
  118. return '\n' + _('The VAT number [%s] for partner [%s] does not seem to be valid. \nNote: the expected format is %s') % (error_partner[0].vat, error_partner[0].name, vat_no)
  119. _constraints = [(check_vat, _construct_constraint_msg, ["vat"])]
  120. __check_vat_ch_re1 = re.compile(r'(MWST|TVA|IVA)[0-9]{6}$')
  121. __check_vat_ch_re2 = re.compile(r'E([0-9]{9}|-[0-9]{3}\.[0-9]{3}\.[0-9]{3})(MWST|TVA|IVA)$')
  122. def check_vat_ch(self, vat):
  123. '''
  124. Check Switzerland VAT number.
  125. '''
  126. # VAT number in Switzerland will change between 2011 and 2013
  127. # http://www.estv.admin.ch/mwst/themen/00154/00589/01107/index.html?lang=fr
  128. # Old format is "TVA 123456" we will admit the user has to enter ch before the number
  129. # Format will becomes such as "CHE-999.999.99C TVA"
  130. # Both old and new format will be accepted till end of 2013
  131. # Accepted format are: (spaces are ignored)
  132. # CH TVA ######
  133. # CH IVA ######
  134. # CH MWST #######
  135. #
  136. # CHE#########MWST
  137. # CHE#########TVA
  138. # CHE#########IVA
  139. # CHE-###.###.### MWST
  140. # CHE-###.###.### TVA
  141. # CHE-###.###.### IVA
  142. #
  143. if self.__check_vat_ch_re1.match(vat):
  144. return True
  145. match = self.__check_vat_ch_re2.match(vat)
  146. if match:
  147. # For new TVA numbers, do a mod11 check
  148. num = filter(lambda s: s.isdigit(), match.group(1)) # get the digits only
  149. factor = (5,4,3,2,7,6,5,4)
  150. csum = sum([int(num[i]) * factor[i] for i in range(8)])
  151. check = (11 - (csum % 11)) % 11
  152. return check == int(num[8])
  153. return False
  154. def _ie_check_char(self, vat):
  155. vat = vat.zfill(8)
  156. extra = 0
  157. if vat[7] not in ' W':
  158. if vat[7].isalpha():
  159. extra = 9 * (ord(vat[7]) - 64)
  160. else:
  161. # invalid
  162. return -1
  163. checksum = extra + sum((8-i) * int(x) for i, x in enumerate(vat[:7]))
  164. return 'WABCDEFGHIJKLMNOPQRSTUV'[checksum % 23]
  165. def check_vat_ie(self, vat):
  166. """ Temporary Ireland VAT validation to support the new format
  167. introduced in January 2013 in Ireland, until upstream is fixed.
  168. TODO: remove when fixed upstream"""
  169. if len(vat) not in (8, 9) or not vat[2:7].isdigit():
  170. return False
  171. if len(vat) == 8:
  172. # Normalize pre-2013 numbers: final space or 'W' not significant
  173. vat += ' '
  174. if vat[:7].isdigit():
  175. return vat[7] == self._ie_check_char(vat[:7] + vat[8])
  176. elif vat[1] in (string.ascii_uppercase + '+*'):
  177. # Deprecated format
  178. # See http://www.revenue.ie/en/online/third-party-reporting/reporting-payment-details/faqs.html#section3
  179. return vat[7] == self._ie_check_char(vat[2:7] + vat[0] + vat[8])
  180. return False
  181. # Mexican VAT verification, contributed by Vauxoo
  182. # and Panos Christeas <p_christ@hol.gr>
  183. __check_vat_mx_re = re.compile(r"(?P<primeras>[A-Za-z\xd1\xf1&]{3,4})" \
  184. r"[ \-_]?" \
  185. r"(?P<ano>[0-9]{2})(?P<mes>[01][0-9])(?P<dia>[0-3][0-9])" \
  186. r"[ \-_]?" \
  187. r"(?P<code>[A-Za-z0-9&\xd1\xf1]{3})$")
  188. def check_vat_mx(self, vat):
  189. ''' Mexican VAT verification
  190. Verificar RFC México
  191. '''
  192. # we convert to 8-bit encoding, to help the regex parse only bytes
  193. vat = ustr(vat).encode('iso8859-1')
  194. m = self.__check_vat_mx_re.match(vat)
  195. if not m:
  196. #No valid format
  197. return False
  198. try:
  199. ano = int(m.group('ano'))
  200. if ano > 30:
  201. ano = 1900 + ano
  202. else:
  203. ano = 2000 + ano
  204. datetime.date(ano, int(m.group('mes')), int(m.group('dia')))
  205. except ValueError:
  206. return False
  207. #Valid format and valid date
  208. return True
  209. # Norway VAT validation, contributed by Rolv Råen (adEgo) <rora@adego.no>
  210. def check_vat_no(self, vat):
  211. '''
  212. Check Norway VAT number.See http://www.brreg.no/english/coordination/number.html
  213. '''
  214. if len(vat) != 9:
  215. return False
  216. try:
  217. int(vat)
  218. except ValueError:
  219. return False
  220. sum = (3 * int(vat[0])) + (2 * int(vat[1])) + \
  221. (7 * int(vat[2])) + (6 * int(vat[3])) + \
  222. (5 * int(vat[4])) + (4 * int(vat[5])) + \
  223. (3 * int(vat[6])) + (2 * int(vat[7]))
  224. check = 11 -(sum % 11)
  225. if check == 11:
  226. check = 0
  227. if check == 10:
  228. # 10 is not a valid check digit for an organization number
  229. return False
  230. return check == int(vat[8])
  231. # Peruvian VAT validation, contributed by Vauxoo
  232. def check_vat_pe(self, vat):
  233. vat_type,vat = vat and len(vat)>=2 and (vat[0], vat[1:]) or (False, False)
  234. if vat_type and vat_type.upper() == 'D':
  235. #DNI
  236. return True
  237. elif vat_type and vat_type.upper() == 'R':
  238. #verify RUC
  239. factor = '5432765432'
  240. sum = 0
  241. dig_check = False
  242. if len(vat) != 11:
  243. return False
  244. try:
  245. int(vat)
  246. except ValueError:
  247. return False
  248. for f in range(0,10):
  249. sum += int(factor[f]) * int(vat[f])
  250. subtraction = 11 - (sum % 11)
  251. if subtraction == 10:
  252. dig_check = 0
  253. elif subtraction == 11:
  254. dig_check = 1
  255. else:
  256. dig_check = subtraction
  257. return int(vat[10]) == dig_check
  258. else:
  259. return False
  260. # VAT validation in Turkey, contributed by # Levent Karakas @ Eska Yazilim A.S.
  261. def check_vat_tr(self, vat):
  262. if not (10 <= len(vat) <= 11):
  263. return False
  264. try:
  265. int(vat)
  266. except ValueError:
  267. return False
  268. # check vat number (vergi no)
  269. if len(vat) == 10:
  270. sum = 0
  271. check = 0
  272. for f in range(0,9):
  273. c1 = (int(vat[f]) + (9-f)) % 10
  274. c2 = ( c1 * (2 ** (9-f)) ) % 9
  275. if (c1 != 0) and (c2 == 0): c2 = 9
  276. sum += c2
  277. if sum % 10 == 0:
  278. check = 0
  279. else:
  280. check = 10 - (sum % 10)
  281. return int(vat[9]) == check
  282. # check personal id (tc kimlik no)
  283. if len(vat) == 11:
  284. c1a = 0
  285. c1b = 0
  286. c2 = 0
  287. for f in range(0,9,2):
  288. c1a += int(vat[f])
  289. for f in range(1,9,2):
  290. c1b += int(vat[f])
  291. c1 = ( (7 * c1a) - c1b) % 10
  292. for f in range(0,10):
  293. c2 += int(vat[f])
  294. c2 = c2 % 10
  295. return int(vat[9]) == c1 and int(vat[10]) == c2
  296. return False