~txerpa-openerp/openerp-spain/7.0-l10n_es_account_asset_extensions

« back to all changes in this revision

Viewing changes to l10n_es_aeat_mod340/account.py

  • Committer: Ignacio Ibeas - Acysos S.L.
  • Date: 2014-02-03 11:13:07 UTC
  • mto: (391.1.31 openerp-spain)
  • mto: This revision was merged to the branch mainline in revision 393.
  • Revision ID: ignacio@acysos.com-20140203111307-01wochg4hx3yd0l1
[ADD] l10n_es_aeat_mod340: migración a OpenERP 7.0 y función añadida de Libros de IVA

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    Copyright (c) 2013 Acysos S.L. (http://acysos.com) All Rights Reserved
 
5
#                       Ignacio Ibeas Izquierdo <ignacio@acysos.com>
 
6
#
 
7
#    This program is free software: you can redistribute it and/or modify
 
8
#    it under the terms of the GNU Affero General Public License as published by
 
9
#    the Free Software Foundation, either version 3 of the License, or
 
10
#    (at your option) any later version.
 
11
#
 
12
#    This program is distributed in the hope that it will be useful,
 
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#    GNU Affero General Public License for more details.
 
16
#
 
17
#    You should have received a copy of the GNU Affero General Public License
 
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
#
 
20
##############################################################################
 
21
 
 
22
from osv import osv, fields
 
23
from tools.translate import _
 
24
 
 
25
class account_tax_code_template(osv.osv):
 
26
 
 
27
    _inherit = 'account.tax.code.template'
 
28
    
 
29
    _columns = {
 
30
        'mod340':fields.boolean("Include in mod340"),
 
31
    }
 
32
    
 
33
account_tax_code_template()
 
34
 
 
35
class account_tax_code(osv.osv):
 
36
 
 
37
    _inherit = 'account.tax.code'
 
38
    
 
39
    _columns = {
 
40
        'mod340':fields.boolean("Include in mod340"),
 
41
    }
 
42
    
 
43
account_tax_code()
 
44
 
 
45
class wizard_update_charts_accounts(osv.osv_memory):
 
46
    _inherit = 'wizard.update.charts.accounts'
 
47
    
 
48
    def _find_tax_codes(self, cr, uid, wizard, context=None):
 
49
        """
 
50
        Search for, and load, tax code templates to create/update.
 
51
        """
 
52
        new_tax_codes = 0
 
53
        updated_tax_codes = 0
 
54
        tax_code_template_mapping = {}
 
55
        
 
56
        tax_code_templ_obj = self.pool.get('account.tax.code.template')
 
57
        tax_code_obj = self.pool.get('account.tax.code')
 
58
        wiz_tax_code_obj = self.pool.get('wizard.update.charts.accounts.tax.code')
 
59
        
 
60
        # Remove previous tax codes
 
61
        wiz_tax_code_obj.unlink(cr, uid, wiz_tax_code_obj.search(cr, uid, []))
 
62
        
 
63
        #
 
64
        # Search for new / updated tax codes
 
65
        #
 
66
        root_tax_code_id = wizard.chart_template_id.tax_code_root_id.id
 
67
        children_tax_code_template = tax_code_templ_obj.search(cr, uid, [('parent_id', 'child_of', [root_tax_code_id])], order='id')
 
68
        for tax_code_template in tax_code_templ_obj.browse(cr, uid, children_tax_code_template):
 
69
            # Ensure the tax code template is on the map (search for the mapped tax code id).
 
70
            self._map_tax_code_template(cr, uid, wizard, tax_code_template_mapping, tax_code_template, context)
 
71
 
 
72
            tax_code_id = tax_code_template_mapping.get(tax_code_template.id)
 
73
            if not tax_code_id:
 
74
                new_tax_codes += 1
 
75
                wiz_tax_code_obj.create(cr, uid, {
 
76
                        'tax_code_id': tax_code_template.id,
 
77
                        'update_chart_wizard_id': wizard.id,
 
78
                        'type': 'new',
 
79
                    }, context)
 
80
            elif wizard.update_tax_code:
 
81
                #
 
82
                # Check the tax code for changes.
 
83
                #
 
84
                modified = False
 
85
                notes = ""
 
86
                tax_code = tax_code_obj.browse(cr, uid, tax_code_id, context=context)
 
87
 
 
88
                if tax_code.code != tax_code_template.code:
 
89
                    notes += _("The code field is different.\n")
 
90
                    modified = True
 
91
                if tax_code.info != tax_code_template.info:
 
92
                    notes += _("The info field is different.\n")
 
93
                    modified = True
 
94
                if tax_code.sign != tax_code_template.sign:
 
95
                    notes += _("The sign field is different.\n")
 
96
                    modified = True
 
97
                if tax_code.mod340 != tax_code_template.mod340:
 
98
                    notes += _("The Mod 340 field is different.\n")
 
99
                    modified = True
 
100
 
 
101
                # TODO: We could check other account fields for changes...
 
102
 
 
103
                if modified:
 
104
                    #
 
105
                    # Tax code to update.
 
106
                    #
 
107
                    updated_tax_codes += 1
 
108
                    wiz_tax_code_obj.create(cr, uid, {
 
109
                            'tax_code_id': tax_code_template.id,
 
110
                            'update_chart_wizard_id': wizard.id,
 
111
                            'type': 'updated',
 
112
                            'update_tax_code_id': tax_code_id,
 
113
                            'notes': notes,
 
114
                        }, context)
 
115
 
 
116
        return { 'new': new_tax_codes, 'updated': updated_tax_codes, 'mapping': tax_code_template_mapping }
 
117
                
 
118
    def _update_tax_codes(self, cr, uid, wizard, log, context=None):
 
119
        """
 
120
        Search for, and load, tax code templates to create/update.
 
121
        """
 
122
        tax_code_obj = self.pool.get('account.tax.code')
 
123
 
 
124
        root_tax_code_id = wizard.chart_template_id.tax_code_root_id.id
 
125
 
 
126
        new_tax_codes = 0
 
127
        updated_tax_codes = 0
 
128
        tax_code_template_mapping = {}
 
129
 
 
130
        for wiz_tax_code in wizard.tax_code_ids:
 
131
            tax_code_template = wiz_tax_code.tax_code_id
 
132
            tax_code_name = (root_tax_code_id == tax_code_template.id) and wizard.company_id.name or tax_code_template.name
 
133
 
 
134
            # Ensure the parent tax code template is on the map.
 
135
            self._map_tax_code_template(cr, uid, wizard, tax_code_template_mapping, tax_code_template.parent_id, context)
 
136
 
 
137
            #
 
138
            # Values
 
139
            #
 
140
            vals = {
 
141
                'name': tax_code_name,
 
142
                'code': tax_code_template.code,
 
143
                'info': tax_code_template.info,
 
144
                'parent_id': tax_code_template.parent_id and tax_code_template_mapping.get(tax_code_template.parent_id.id),
 
145
                'company_id': wizard.company_id.id,
 
146
                'sign': tax_code_template.sign,
 
147
                'mod340': tax_code_template.mod340
 
148
            }
 
149
 
 
150
            tax_code_id = None
 
151
            modified = False
 
152
 
 
153
            if wiz_tax_code.type == 'new':
 
154
                #
 
155
                # Create the tax code
 
156
                #
 
157
                tax_code_id = tax_code_obj.create(cr, uid, vals)
 
158
                log.add(_("Created tax code %s.\n") % tax_code_name)
 
159
                new_tax_codes += 1
 
160
                modified = True
 
161
            elif wizard.update_tax_code and wiz_tax_code.update_tax_code_id:
 
162
                #
 
163
                # Update the tax code
 
164
                #
 
165
                tax_code_id = wiz_tax_code.update_tax_code_id.id
 
166
                tax_code_obj.write(cr, uid, [tax_code_id], vals)
 
167
                log.add(_("Updated tax code %s.\n") % tax_code_name)
 
168
                updated_tax_codes += 1
 
169
                modified = True
 
170
            else:
 
171
                tax_code_id = wiz_tax_code.update_tax_code_id and wiz_tax_code.update_tax_code_id.id
 
172
                modified = False
 
173
 
 
174
            # Store the tax codes on the map
 
175
            tax_code_template_mapping[tax_code_template.id] = tax_code_id
 
176
 
 
177
            if modified:
 
178
                #
 
179
                # Detect errors
 
180
                #
 
181
                if tax_code_template.parent_id and not tax_code_template_mapping.get(tax_code_template.parent_id.id):
 
182
                    log.add(_("Tax code %s: The parent tax code %s can not be set.\n") % (tax_code_name, tax_code_template.parent_id.name), True)
 
183
 
 
184
        return {
 
185
            'new': new_tax_codes,
 
186
            'updated': updated_tax_codes,
 
187
            'mapping': tax_code_template_mapping
 
188
        }
 
189
    
 
190
wizard_update_charts_accounts()
 
 
b'\\ No newline at end of file'