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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# -*- coding: utf-8 -*-
##############################################################################
#
#    Copyright (c) 2013 Acysos S.L. (http://acysos.com) All Rights Reserved
#                       Ignacio Ibeas Izquierdo <ignacio@acysos.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from osv import osv, fields
from tools.translate import _

class account_tax_code_template(osv.osv):

    _inherit = 'account.tax.code.template'
    
    _columns = {
        'mod340':fields.boolean("Include in mod340"),
    }
    
account_tax_code_template()

class account_tax_code(osv.osv):

    _inherit = 'account.tax.code'
    
    _columns = {
        'mod340':fields.boolean("Include in mod340"),
    }
    
account_tax_code()

class wizard_update_charts_accounts(osv.osv_memory):
    _inherit = 'wizard.update.charts.accounts'
    
    def _find_tax_codes(self, cr, uid, wizard, context=None):
        """
        Search for, and load, tax code templates to create/update.
        """
        new_tax_codes = 0
        updated_tax_codes = 0
        tax_code_template_mapping = {}
        
        tax_code_templ_obj = self.pool.get('account.tax.code.template')
        tax_code_obj = self.pool.get('account.tax.code')
        wiz_tax_code_obj = self.pool.get('wizard.update.charts.accounts.tax.code')
        
        # Remove previous tax codes
        wiz_tax_code_obj.unlink(cr, uid, wiz_tax_code_obj.search(cr, uid, []))
        
        #
        # Search for new / updated tax codes
        #
        root_tax_code_id = wizard.chart_template_id.tax_code_root_id.id
        children_tax_code_template = tax_code_templ_obj.search(cr, uid, [('parent_id', 'child_of', [root_tax_code_id])], order='id')
        for tax_code_template in tax_code_templ_obj.browse(cr, uid, children_tax_code_template):
            # Ensure the tax code template is on the map (search for the mapped tax code id).
            self._map_tax_code_template(cr, uid, wizard, tax_code_template_mapping, tax_code_template, context)

            tax_code_id = tax_code_template_mapping.get(tax_code_template.id)
            if not tax_code_id:
                new_tax_codes += 1
                wiz_tax_code_obj.create(cr, uid, {
                        'tax_code_id': tax_code_template.id,
                        'update_chart_wizard_id': wizard.id,
                        'type': 'new',
                    }, context)
            elif wizard.update_tax_code:
                #
                # Check the tax code for changes.
                #
                modified = False
                notes = ""
                tax_code = tax_code_obj.browse(cr, uid, tax_code_id, context=context)

                if tax_code.code != tax_code_template.code:
                    notes += _("The code field is different.\n")
                    modified = True
                if tax_code.info != tax_code_template.info:
                    notes += _("The info field is different.\n")
                    modified = True
                if tax_code.sign != tax_code_template.sign:
                    notes += _("The sign field is different.\n")
                    modified = True
                if tax_code.mod340 != tax_code_template.mod340:
                    notes += _("The Mod 340 field is different.\n")
                    modified = True

                # TODO: We could check other account fields for changes...

                if modified:
                    #
                    # Tax code to update.
                    #
                    updated_tax_codes += 1
                    wiz_tax_code_obj.create(cr, uid, {
                            'tax_code_id': tax_code_template.id,
                            'update_chart_wizard_id': wizard.id,
                            'type': 'updated',
                            'update_tax_code_id': tax_code_id,
                            'notes': notes,
                        }, context)

        return { 'new': new_tax_codes, 'updated': updated_tax_codes, 'mapping': tax_code_template_mapping }
                
    def _update_tax_codes(self, cr, uid, wizard, log, context=None):
        """
        Search for, and load, tax code templates to create/update.
        """
        tax_code_obj = self.pool.get('account.tax.code')

        root_tax_code_id = wizard.chart_template_id.tax_code_root_id.id

        new_tax_codes = 0
        updated_tax_codes = 0
        tax_code_template_mapping = {}

        for wiz_tax_code in wizard.tax_code_ids:
            tax_code_template = wiz_tax_code.tax_code_id
            tax_code_name = (root_tax_code_id == tax_code_template.id) and wizard.company_id.name or tax_code_template.name

            # Ensure the parent tax code template is on the map.
            self._map_tax_code_template(cr, uid, wizard, tax_code_template_mapping, tax_code_template.parent_id, context)

            #
            # Values
            #
            vals = {
                'name': tax_code_name,
                'code': tax_code_template.code,
                'info': tax_code_template.info,
                'parent_id': tax_code_template.parent_id and tax_code_template_mapping.get(tax_code_template.parent_id.id),
                'company_id': wizard.company_id.id,
                'sign': tax_code_template.sign,
                'mod340': tax_code_template.mod340
            }

            tax_code_id = None
            modified = False

            if wiz_tax_code.type == 'new':
                #
                # Create the tax code
                #
                tax_code_id = tax_code_obj.create(cr, uid, vals)
                log.add(_("Created tax code %s.\n") % tax_code_name)
                new_tax_codes += 1
                modified = True
            elif wizard.update_tax_code and wiz_tax_code.update_tax_code_id:
                #
                # Update the tax code
                #
                tax_code_id = wiz_tax_code.update_tax_code_id.id
                tax_code_obj.write(cr, uid, [tax_code_id], vals)
                log.add(_("Updated tax code %s.\n") % tax_code_name)
                updated_tax_codes += 1
                modified = True
            else:
                tax_code_id = wiz_tax_code.update_tax_code_id and wiz_tax_code.update_tax_code_id.id
                modified = False

            # Store the tax codes on the map
            tax_code_template_mapping[tax_code_template.id] = tax_code_id

            if modified:
                #
                # Detect errors
                #
                if tax_code_template.parent_id and not tax_code_template_mapping.get(tax_code_template.parent_id.id):
                    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)

        return {
            'new': new_tax_codes,
            'updated': updated_tax_codes,
            'mapping': tax_code_template_mapping
        }
    
wizard_update_charts_accounts()