[REF+IMP] l10n_multilang: code refactoring in order to split the big method into...
[odoo/odoo.git] / addons / l10n_multilang / l10n_multilang.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (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 fields, osv
23 import os
24 from tools.translate import _
25 import netsvc
26 logger=netsvc.Logger()
27
28 class wizard_multi_charts_accounts(osv.osv_memory):
29     """
30     Change wizard that a new account chart for a company.
31         * Add option to install languages during the setup
32         * Copy translations for COA, Tax, Tax Code and Fiscal Position from templates to target objects.
33     """
34     _inherit = 'wizard.multi.charts.accounts'
35
36     def process_translations(self, cr, uid, langs, in_obj, in_field, in_ids, out_obj, out_ids, force_write=False, context=None):
37         """
38         This method copies translations values of templates into new Accounts/Taxes/Journals for languages selected
39
40         :param cr: A database cursor
41         :param uid: ID of the user currently logged in
42         :param langs: List of languages to load for new records
43         :param in_field: Name of the translatable field of source templates
44         :param in_obj: Name of source object of templates.
45         :param in_ids: List of ids of source object
46         :param out_obj: Destination object for which translation is to be copied
47         :param out_ids: List of ids of destination object
48         :param force_write: boolean that depicts if we need to create a translation OR simply replace the actual value
49             with the translation in the uid's language by doing a write (in case it's TRUE)
50         :param context: usual context information. May contain the key 'lang', which is the language of the user running
51             the wizard, that will be used if force_write is True
52
53         :return: True
54         """
55         if context is None:
56             context = {}
57         src = {}
58         xlat_obj = self.pool.get('ir.translation')
59         #find the source from Account Template
60         for x in in_obj.browse(cr, uid, in_ids):
61             src.update({x.id: x.name})
62         for lang in langs:
63             #find the value from Translation
64             value = xlat_obj._get_ids(cr, uid, in_obj._name + ',' + in_field, 'model', lang, in_ids)
65             for j in range(len(in_ids)):
66                 in_id = in_ids[j]
67                 if value[in_id]:
68                     if not force_write:
69                         #copy Translation from Source to Destination object
70                         xlat_obj.create(cr, uid, {
71                           'name': out_obj._name + ',' + in_field,
72                           'type': 'model',
73                           'res_id': out_ids[j],
74                           'lang': lang,
75                           'src': src[in_id],
76                           'value': value[in_id],
77                     })
78                     else:
79                         #replace the value in the destination object only if it's the user lang
80                         if context.get('lang') == lang:
81                             self.pool.get(out_obj._name).write(cr, uid, out_ids[j], {in_field: value[in_id]})
82                 else:
83                     logger.notifyChannel('addons.'+self._name, netsvc.LOG_WARNING,
84                              'Language: %s. Translation from template: there is no translation available for %s!' %(lang,  src[in_id]))#out_obj._name))
85         return True
86
87     def execute(self, cr, uid, ids, context=None):
88         res = super(wizard_multi_charts_accounts, self).execute(cr, uid, ids, context=context)
89
90         obj_multi = self.browse(cr, uid, ids[0], context=context)
91         company_id = obj_multi.company_id.id
92
93         # load languages
94         langs = []
95         res_lang_obj = self.pool.get('res.lang')
96         installed_lang_ids = res_lang_obj.search(cr, uid, [])
97         installed_langs = [x.code for x in res_lang_obj.browse(cr, uid, installed_lang_ids, context=context)]
98         for lang in obj_multi.chart_template_id.spoken_languages.split(';'):
99             if lang not in installed_langs:
100                 # the language is not installed, so we don't need to load its translations
101                 continue
102             else: 
103                 # the language was already installed, so the po files have been loaded at the installation time
104                 # and now we need to copy the translations of templates to the right objects
105                 langs.append(lang)
106         if langs:
107             # write account.account translations in the real COA
108             self._process_accounts_translations(cr, uid, obj_multi, company_id, langs, 'name', context=context)
109
110             # copy account.tax.code translations
111             self._process_tax_codes_translations(cr, uid, obj_multi, company_id, langs, 'name', context=context)
112
113             # copy account.tax translations
114             self._process_taxes_translations(cr, uid, obj_multi, company_id, langs, 'name', context=context)
115
116             # copy account.fiscal.position translations
117             self.process_fiscal_pos_translations(cr, uid, obj_multi, company_id, langs, 'name', context=context)
118
119         return res
120
121     def _process_accounts_translations(self, cr, uid, obj_multi, company_id, langs, field, context=None):
122         obj_acc_template = self.pool.get('account.account.template')
123         obj_acc = self.pool.get('account.account')
124         acc_template_root_id = obj_multi.chart_template_id.account_root_id.id
125         acc_root_id = obj_acc.search(cr, uid, [('company_id', '=', company_id), ('parent_id', '=', None)])[0]
126         in_ids = obj_acc_template.search(cr, uid, [('id', 'child_of', [acc_template_root_id])], order='id')[1:]
127         out_ids = obj_acc.search(cr, uid, [('id', 'child_of', [acc_root_id])], order='id')[1:]
128         return self.process_translations(cr, uid, langs, obj_acc_template, field, in_ids, obj_acc, out_ids, force_write=True, context=context)
129
130     def _process_tax_codes_translations(self, cr, uid, obj_multi, company_id, langs, field, context=None):
131         obj_tax_code_template = self.pool.get('account.tax.code.template')
132         obj_tax_code = self.pool.get('account.tax.code')
133         tax_code_template_root_id = obj_multi.chart_template_id.tax_code_root_id.id
134         tax_code_root_id = obj_tax_code.search(cr, uid, [('company_id', '=', company_id), ('parent_id', '=', None)])[0]
135         in_ids = obj_tax_code_template.search(cr, uid, [('id', 'child_of', [tax_code_template_root_id])], order='id')[1:]
136         out_ids = obj_tax_code.search(cr, uid, [('id', 'child_of', [tax_code_root_id])], order='id')[1:]
137         return self.process_translations(cr, uid, langs, obj_tax_code_template, field, in_ids, obj_tax_code, out_ids, force_write=False, context=context)
138
139     def _process_taxes_translations(self, cr, uid, obj_multi, company_id, langs, field, context=None):
140         obj_tax_template = self.pool.get('account.tax.template')
141         obj_tax = self.pool.get('account.tax')
142         in_ids = sorted([x.id for x in obj_multi.chart_template_id.tax_template_ids])
143         out_ids = obj_tax.search(cr, uid, [('company_id', '=', company_id)], order='id')
144         return self.process_translations(cr, uid, langs, obj_tax_template, field, in_ids, obj_tax, out_ids, force_write=False, context=context)
145
146     def _process_fiscal_pos_translations(self, cr, uid, obj_multi, company_id, langs, field, context=None):
147         obj_fiscal_position_template = self.pool.get('account.fiscal.position.template')
148         obj_fiscal_position = self.pool.get('account.fiscal.position')
149         in_ids = obj_fiscal_position_template.search(cr, uid, [('chart_template_id', '=', obj_multi.chart_template_id.id)], order='id')
150         out_ids = obj_fiscal_position.search(cr, uid, [('company_id', '=', company_id)], order='id')
151         return self.process_translations(cr, uid, langs, obj_fiscal_position_template, field, in_ids, obj_fiscal_position, out_ids, force_write=False, context=context)
152
153 wizard_multi_charts_accounts()
154
155 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: