~vauxoo/openerp-mexico-localization/type_invoice_report_dev_luis

« back to all changes in this revision

Viewing changes to base_vat_mx/base_vat.py

  • Committer: moylop260
  • Date: 2011-05-17 03:46:11 UTC
  • mto: This revision was merged to the branch mainline in revision 52.
  • Revision ID: moylop260@hotmail.com-20110517034611-oprpx39eq9d21x0h
[FIX]: Reverted delete addons

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
###########################################################################
 
3
#    Module Writen to OpenERP, Open Source Management Solution
 
4
#
 
5
#    Copyright (c) 2010 moylop260 - http://moylop.blogspot.com/
 
6
#    All Rights Reserved.
 
7
#    info moylop260 (moylop260@hotmail.com)
 
8
############################################################################
 
9
#    Coded by: moylop260 (moylop260@hotmail.com)
 
10
############################################################################
 
11
#
 
12
#    This program is free software: you can redistribute it and/or modify
 
13
#    it under the terms of the GNU Affero General Public License as
 
14
#    published by the Free Software Foundation, either version 3 of the
 
15
#    License, or (at your option) any later version.
 
16
#
 
17
#    This program is distributed in the hope that it will be useful,
 
18
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
#    GNU Affero General Public License for more details.
 
21
#
 
22
#    You should have received a copy of the GNU Affero General Public License
 
23
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
24
#
 
25
##############################################################################
 
26
from osv import osv
 
27
from osv import fields
 
28
from tools.translate import _
 
29
 
 
30
def conv_ascii(text):
 
31
    """Convierte vocales accentuadas, ñ y ç a sus caracteres equivalentes ASCII"""
 
32
    old_chars = ['á', 'é', 'í', 'ó', 'ú', 'à', 'è', 'ì', 'ò', 'ù', 'ä', 'ë', 'ï', 'ö', 'ü', 'â', 'ê', 'î', \
 
33
        'ô', 'û', 'Á', 'É', 'Í', 'Ú', 'Ó', 'À', 'È', 'Ì', 'Ò', 'Ù', 'Ä', 'Ë', 'Ï', 'Ö', 'Ü', 'Â', 'Ê', 'Î', \
 
34
        'Ô', 'Û', 'ñ', 'Ñ', 'ç', 'Ç', 'ª', 'º', '°', ' '
 
35
    ]
 
36
    new_chars = ['a', 'e', 'i', 'o', 'u', 'a', 'e', 'i', 'o', 'u', 'a', 'e', 'i', 'o', 'u', 'a', 'e', 'i', \
 
37
        'o', 'u', 'A', 'E', 'I', 'O', 'U', 'A', 'E', 'I', 'O', 'U', 'A', 'E', 'I', 'O', 'U', 'A', 'E', 'I', \
 
38
        'O', 'U', 'n', 'N', 'c', 'C', 'a', 'o', 'o', ' '
 
39
    ]
 
40
    for old, new in zip(old_chars, new_chars):
 
41
        try:
 
42
            text = text.replace(unicode(old,'UTF-8'), new)
 
43
        except:
 
44
            try:
 
45
                text = text.replace(old, new)
 
46
            except:
 
47
                raise osv.except_osv(_('Warning !'), 'No se pudo re-codificar la cadena [%s] en la letra [%s]'%(text, old) )
 
48
    return text
 
49
 
 
50
class res_partner(osv.osv):
 
51
    _inherit = 'res.partner'
 
52
    
 
53
    def check_vat(self, cr, uid, ids, context=None):
 
54
        '''
 
55
        Verificar RFC México
 
56
        '''
 
57
        #[IMP] base_vat: check_vat_mx by moylop260@hotmail.com, tested with 242,665 real RFC's
 
58
        import time
 
59
        import re
 
60
        map_initials = "[A-Z|&]"*4
 
61
        map_date = "[0-9][0-9][0-1][1-9][0-3][0-9]"
 
62
        map_code = "[A-Z|&|0-9]"*3
 
63
        mapping = map_initials + map_date + map_code
 
64
        for partner in self.browse(cr, uid, ids, context=context):
 
65
            vat = partner.vat
 
66
            if not vat:
 
67
                continue
 
68
            vat = conv_ascii(vat).upper().replace(' ', '').replace('-', '')
 
69
            #vat = vat.upper().replace('ñ', 'Ñ').replace('\xd1', 'Ñ').replace('\xf1', 'Ñ')#upper ascii
 
70
            #vat = vat.replace('Ñ', 'X')#Replace ascii valid char, these is problems with match in regexp
 
71
            #vat = vat.replace(' ', '').replace('-', '')#Replace some char valid, but no required
 
72
            if len(vat)==12:
 
73
                vat = "X" + vat#Add a valid char, for pass validation with case with cad of len = 12
 
74
            if len(vat) <> 13:
 
75
                return False
 
76
            regex = re.compile(mapping)
 
77
            if not regex.match(vat):
 
78
                #No valid format
 
79
                return False
 
80
            date_match = re.search(map_date, vat)
 
81
            date_format = '%y%m%d'
 
82
            try:
 
83
                time.strptime(date_match.group(), date_format)
 
84
            except:
 
85
                #Valid format, but date wrong
 
86
                return False
 
87
            #Valid format and valid date
 
88
        return True
 
89
    
 
90
    def _______ANTERIOR____check_vat(self, cr, uid, ids, context=None):
 
91
        for partner in self.browse(cr, uid, ids, context=context):
 
92
            vat = partner.vat
 
93
            if not vat:
 
94
                continue
 
95
            vat = vat.upper()
 
96
            vat = ''.join( [x for x in vat if x.isupper() or x.isdigit() ] ) #Remove all characteres what no is digit or letter
 
97
            if len(vat)==12:
 
98
                vat = "X" + vat#Add a valid char, for pass validation with case with cad of len = 12
 
99
            if len(vat) <> 13 or not( vat[:4].isupper() 
 
100
            and vat[4:10].isdigit() and vat[10:13].isalnum() ):
 
101
                return False
 
102
        return True
 
103
    
 
104
    _constraints = [(check_vat, _('Error RFC es incorrecto, debería ser algo como XYZA010203A01 or XYZ0102039A8'), ["vat"])]
 
105
res_partner()
 
 
b'\\ No newline at end of file'