1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from openerp.osv import fields, osv
24 from openerp.tools.translate import _
26 _logger = logging.getLogger(__name__)
28 class wizard_multi_charts_accounts(osv.osv_memory):
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.
34 _inherit = 'wizard.multi.charts.accounts'
36 # FIXME: in trunk, drop the force_write param entirely
37 def process_translations(self, cr, uid, langs, in_obj, in_field, in_ids, out_obj, out_ids, force_write=False, context=None):
39 This method copies translations values of templates into new Accounts/Taxes/Journals for languages selected
41 :param cr: A database cursor
42 :param uid: ID of the user currently logged in
43 :param langs: List of languages to load for new records
44 :param in_field: Name of the translatable field of source templates
45 :param in_obj: Name of source object of templates.
46 :param in_ids: List of ids of source object
47 :param out_obj: Destination object for which translation is to be copied
48 :param out_ids: List of ids of destination object
49 :param force_write: Deprecated as of 7.0, do not use
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
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})
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)):
68 #copy Translation from Source to Destination object
69 xlat_obj.create(cr, uid, {
70 'name': out_obj._name + ',' + in_field,
75 'value': value[in_id],
78 _logger.info('Language: %s. Translation from template: there is no translation available for %s!' %(lang, src[in_id]))#out_obj._name))
81 def execute(self, cr, uid, ids, context=None):
84 # remove the lang to get the untranslated value
85 ctx = dict(context, lang=None)
86 res = super(wizard_multi_charts_accounts, self).execute(cr, uid, ids, context=ctx)
88 obj_multi = self.browse(cr, uid, ids[0], context=context)
89 company_id = obj_multi.company_id.id
93 res_lang_obj = self.pool.get('res.lang')
94 installed_lang_ids = res_lang_obj.search(cr, uid, [])
95 installed_langs = [x.code for x in res_lang_obj.browse(cr, uid, installed_lang_ids, context=context)]
96 if obj_multi.chart_template_id.spoken_languages:
97 for lang in obj_multi.chart_template_id.spoken_languages.split(';'):
98 if lang not in installed_langs:
99 # the language is not installed, so we don't need to load its translations
102 # the language was already installed, so the po files have been loaded at the installation time
103 # and now we need to copy the translations of templates to the right objects
106 # write account.account translations in the real COA
107 self._process_accounts_translations(cr, uid, obj_multi, company_id, langs, 'name', context=context)
109 # copy account.tax.code translations
110 self._process_tax_codes_translations(cr, uid, obj_multi, company_id, langs, 'name', context=context)
112 # copy account.tax translations
113 self._process_taxes_translations(cr, uid, obj_multi, company_id, langs, 'name', context=context)
115 # copy account.fiscal.position translations
116 self._process_fiscal_pos_translations(cr, uid, obj_multi, company_id, langs, 'name', context=context)
120 def _process_accounts_translations(self, cr, uid, obj_multi, company_id, langs, field, context=None):
121 obj_acc_template = self.pool.get('account.account.template')
122 obj_acc = self.pool.get('account.account')
123 acc_template_root_id = obj_multi.chart_template_id.account_root_id.id
124 acc_root_id = obj_acc.search(cr, uid, [('company_id', '=', company_id), ('parent_id', '=', None)])[0]
125 in_ids = obj_acc_template.search(cr, uid, [('id', 'child_of', [acc_template_root_id])], order='id')[1:]
126 out_ids = obj_acc.search(cr, uid, [('id', 'child_of', [acc_root_id])], order='id')[1:]
127 return self.process_translations(cr, uid, langs, obj_acc_template, field, in_ids, obj_acc, out_ids, context=context)
129 def _process_tax_codes_translations(self, cr, uid, obj_multi, company_id, langs, field, context=None):
130 obj_tax_code_template = self.pool.get('account.tax.code.template')
131 obj_tax_code = self.pool.get('account.tax.code')
132 tax_code_template_root_id = obj_multi.chart_template_id.tax_code_root_id.id
133 tax_code_root_id = obj_tax_code.search(cr, uid, [('company_id', '=', company_id), ('parent_id', '=', None)])[0]
134 in_ids = obj_tax_code_template.search(cr, uid, [('id', 'child_of', [tax_code_template_root_id])], order='id')[1:]
135 out_ids = obj_tax_code.search(cr, uid, [('id', 'child_of', [tax_code_root_id])], order='id')[1:]
136 return self.process_translations(cr, uid, langs, obj_tax_code_template, field, in_ids, obj_tax_code, out_ids, context=context)
138 def _process_taxes_translations(self, cr, uid, obj_multi, company_id, langs, field, context=None):
139 obj_tax_template = self.pool.get('account.tax.template')
140 obj_tax = self.pool.get('account.tax')
141 in_ids = sorted([x.id for x in obj_multi.chart_template_id.tax_template_ids])
142 out_ids = obj_tax.search(cr, uid, [('company_id', '=', company_id)], order='id')
143 return self.process_translations(cr, uid, langs, obj_tax_template, field, in_ids, obj_tax, out_ids, context=context)
145 def _process_fiscal_pos_translations(self, cr, uid, obj_multi, company_id, langs, field, context=None):
146 obj_fiscal_position_template = self.pool.get('account.fiscal.position.template')
147 obj_fiscal_position = self.pool.get('account.fiscal.position')
148 in_ids = obj_fiscal_position_template.search(cr, uid, [('chart_template_id', '=', obj_multi.chart_template_id.id)], order='id')
149 out_ids = obj_fiscal_position.search(cr, uid, [('company_id', '=', company_id)], order='id')
150 return self.process_translations(cr, uid, langs, obj_fiscal_position_template, field, in_ids, obj_fiscal_position, out_ids, context=context)
153 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: