~altegra/pepess/pepess_dev_carlos_scripts

« back to all changes in this revision

Viewing changes to base_vat_pepess/base_vat.py

  • Committer: Carlos-Blanco
  • Date: 2014-01-16 19:33:42 UTC
  • mto: This revision was merged to the branch mainline in revision 27.
  • Revision ID: carlos.blanco@grupoaltegra.com-20140116193342-urduh1awhgxa54by
[ADD] Added a new module that added country code to partner vat

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#-*- coding:utf-8 -*-
 
3
 
 
4
#############################################################################
 
5
#    Module Writen to OpenERP, Open Source Management Solution
 
6
#    CopyLeft 2012 - http://www.grupoaltegra.com
 
7
#    You are free to share, copy, distribute, transmit, adapt and use for commercial purpose
 
8
#    More information about license: http://www.gnu.org/licenses/agpl.html
 
9
#    info Grupo Altegra (openerp@grupoaltegra.com)
 
10
#
 
11
#############################################################################
 
12
#
 
13
#    Coded by: Carlos Blanco (carlos.blanco@grupoaltegra.com)
 
14
#
 
15
#############################################################################
 
16
#
 
17
#    This program is free software: you can redistribute it and/or modify
 
18
#    it under the terms of the GNU Affero General Public License as
 
19
#    published by the Free Software Foundation, either version 3 of the
 
20
#    License, or (at your option) any later version.
 
21
#
 
22
#    This program is distributed in the hope that it will be useful,
 
23
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
24
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
25
#    GNU Affero General Public License for more details.
 
26
#
 
27
#    You should have received a copy of the GNU Affero General Public License
 
28
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
29
#
 
30
#############################################################################
 
31
 
 
32
import re
 
33
from openerp.osv import fields, osv
 
34
from openerp.tools.translate import _
 
35
import string
 
36
 
 
37
_ref_vat = {
 
38
    'at': 'ATU12345675',
 
39
    'be': 'BE0477472701',
 
40
    'bg': 'BG1234567892',
 
41
    'ch': 'CHE-123.456.788 TVA or CH TVA 123456', #Swiss by Yannick Vaucher @ Camptocamp
 
42
    'cy': 'CY12345678F',
 
43
    'cz': 'CZ12345679',
 
44
    'de': 'DE123456788',
 
45
    'dk': 'DK12345674',
 
46
    'ee': 'EE123456780',
 
47
    'el': 'EL12345670',
 
48
    'es': 'ESA12345674',
 
49
    'fi': 'FI12345671',
 
50
    'fr': 'FR32123456789',
 
51
    'gb': 'GB123456782',
 
52
    'gr': 'GR12345670',
 
53
    'hu': 'HU12345676',
 
54
    'hr': 'HR01234567896', # Croatia, contributed by Milan Tribuson 
 
55
    'ie': 'IE1234567T',
 
56
    'it': 'IT12345670017',
 
57
    'lt': 'LT123456715',
 
58
    'lu': 'LU12345613',
 
59
    'lv': 'LV41234567891',
 
60
    'mt': 'MT12345634',
 
61
    'mx': 'MXABC123456T1B',
 
62
    'nl': 'NL123456782B90',
 
63
    'no': 'NO123456785',
 
64
    'pl': 'PL1234567883',
 
65
    'pt': 'PT123456789',
 
66
    'ro': 'RO1234567897',
 
67
    'se': 'SE123456789701',
 
68
    'si': 'SI12345679',
 
69
    'sk': 'SK0012345675',
 
70
}
 
71
 
 
72
class res_partner(osv.Model):
 
73
    _inherit = 'res.partner'
 
74
    
 
75
    def check_vat(self, cr, uid, ids, context=None):
 
76
        vat_list = []
 
77
        #~ return all( [ self.check_vat_mx( partner.vat ) for partner in self.browse(cr, uid, ids, context=context) ] )
 
78
        # Se Verifica si tiene el codigo de pais agregado en el RFC
 
79
        for partner in self.browse(cr, uid, ids, context=context):
 
80
            vat = partner.vat
 
81
            if partner.vat:
 
82
                country_code = partner.vat[:2]
 
83
                # Si tiene el codigo de pais se quita y solo se toma n cuenta el RFC
 
84
                if country_code.count('MX'):
 
85
                    vat = partner.vat[2:]
 
86
                # Si no si manda todo el RFC sin quitarne nada para validarlo
 
87
                else:
 
88
                    vat = partner.vat
 
89
                vat_list.append(self.check_vat_mx( vat ))
 
90
            else:
 
91
                vat_list.append(True)
 
92
        return all(vat_list)
 
93
    
 
94
    def _construct_constraint_msg(self, cr, uid, ids, context=None):
 
95
        # Se construye el mensaje que se mandara si el RFC es incorrecto
 
96
        def default_vat_check(cn, vn):
 
97
            # by default, a VAT number is valid if:
 
98
            #  it starts with 2 letters
 
99
            #  has more than 3 characters
 
100
            return cn[0] in string.ascii_lowercase and cn[1] in string.ascii_lowercase
 
101
        vat_country, vat_number = self._split_vat(self.browse(cr, uid, ids)[0].vat)
 
102
        vat_no = "'CC##' (CC=Country Code, ##=VAT Number)"
 
103
        if default_vat_check(vat_country, vat_number):
 
104
            vat_no = _ref_vat[vat_country] if vat_country in _ref_vat else vat_no
 
105
        return '\n' + _('This VAT number does not seem to be valid.\nNote: the expected format is %s') % vat_no
 
106
    
 
107
    _constraints = [(check_vat, _construct_constraint_msg, ["vat"])]
 
108
    
 
109
    def write(self, cr, uid, ids, vals, context=None):
 
110
        """
 
111
        Se redefine el metodo write para ponerle el codigo de pais al principio del RFC
 
112
        """
 
113
        if context is None:
 
114
            context = {}
 
115
        if 'vat' in vals:
 
116
            if vals['vat']:
 
117
                vals['vat'] = vals['vat'] and vals['vat'].replace(u'-', u'').replace(u' ', u'').replace(u'.', u'').replace(u'/', u'').replace(u'ñ', u'Ñ').upper() or vals['vat']
 
118
                country_code = vals['vat'][:2]
 
119
                if not country_code.count('MX'):
 
120
                    vals['vat'] = 'MX'+vals['vat']
 
121
        return super(res_partner, self).write(cr, uid, ids, vals, context=context)