[ADD] account: l10n_multilang:Add Translation from Source object account.tax.template...
[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
26
27 class translate_message(osv.osv_memory):
28     _name = "translate.message"
29     _description = "Translation Message"
30
31     _columns = {
32         'message' : fields.text('Message', readonly=True),
33      }
34
35 translate_message()
36
37
38 class wizard_multi_charts_accounts(osv.osv_memory):
39     """
40     Change wizard that a new account chart for a company.
41         * Add option to install languages during the setup
42         * Copy translations for COA, Tax, Tax Code and Fiscal Position from templates to target objects.
43     """
44     _inherit = 'wizard.multi.charts.accounts'
45
46     def copy_translations(self, cr, uid, langs, in_obj, in_field, in_ids, out_obj, out_ids):
47         """
48         This method copies translations values of templates into new Accounts/Taxes/Journals for languages selected
49         @param cr: A database cursor
50         @param uid: ID of the user currently logged in
51         @param langs: List of languages to load for new records
52         @param in_field: Name of the translatable field of source templates
53         @param in_obj: Name of source object of templates.
54         @param in_ids: List of ids of source object
55         @param out_obj: Destination object for which translation is to be copied
56         @param out_ids: List of ids of destination object
57         
58         @param Return: String containing information of the translations loaded.
59         """
60         src = {}
61         xlat_obj = self.pool.get('ir.translation')
62         #find the source from Account Template
63         for x in in_obj.browse(cr, uid, in_ids):
64             src.update({x.id: x.name})
65         message = ''
66         for lang in langs:
67             notdone = []
68             #find the value from Translation
69             value = xlat_obj._get_ids(cr, uid, in_obj._name + ',' + in_field, 'model', lang, in_ids)
70             for j in range(len(in_ids)):
71                 in_id = in_ids[j]
72                 if not value[in_id]:
73                     # if source have no translation available
74                     notdone.append(src[in_id])
75                 else:
76                     #copy Translation from Source to Destination object
77                     xlat_obj.create(cr, uid, {
78                       'name': out_obj._name + ',' + in_field,
79                       'type': 'model',
80                       'res_id': out_ids[j],
81                       'lang': lang,
82                       'src': src[in_id],
83                       'value': value[in_id],
84                 })
85             if notdone:
86                 message += '\nLanguage: %s \n\tThere is no translation available for following %s(s): \n\t%s '\
87                             % (lang, out_obj._description, '\n\t'.join(notdone))
88             else:
89                 message += '\nLanguage: %s \n\tTranslation successfully done for %s(s) .' % (lang, out_obj._description)
90
91         return message
92
93     def execute(self, cr, uid, ids, context=None):
94         res = super(wizard_multi_charts_accounts, self).execute(cr, uid, ids, context=context)
95         obj_multi = self.browse(cr, uid, ids[0], context=context)
96         obj_mod = self.pool.get('ir.module.module')
97         obj_acc_template = self.pool.get('account.account.template')
98         obj_acc = self.pool.get('account.account')
99         obj_tax_code_template = self.pool.get('account.tax.code.template')
100         obj_tax_code = self.pool.get('account.tax.code')
101         obj_tax_template = self.pool.get('account.tax.template')
102         obj_tax = self.pool.get('account.tax')
103         
104         resource_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'l10n_multilang', 'view_translate_message_wizard')
105         
106         company_id = obj_multi.company_id.id
107         acc_template_root_id = obj_multi.chart_template_id.account_root_id.id
108         acc_root_id = obj_acc.search(cr, uid, [('company_id', '=', company_id), ('parent_id', '=', None)])[0]
109         tax_code_template_root_id = obj_multi.chart_template_id.tax_code_root_id.id
110         tax_code_root_id = obj_tax_code.search(cr, uid, [('company_id', '=', company_id), ('parent_id', '=', None)])[0]
111
112         # load languages
113         langs = []
114         installed_mids = obj_mod.search(cr, uid, [('state', '=', 'installed')])
115         for lang in obj_multi.lang_ids:
116             langs.append(lang.code)
117             obj_mod.update_translations(cr, uid, installed_mids, lang.code)
118
119         warn_msg = ''
120
121         # copy account.account translations
122         in_ids = obj_acc_template.search(cr, uid, [('id', 'child_of', [acc_template_root_id])], order='id')[1:]
123         out_ids = obj_acc.search(cr, uid, [('id', 'child_of', [acc_root_id])], order='id')[1:]
124         warn_msg += self.copy_translations(cr, uid, langs, obj_acc_template, 'name', in_ids, obj_acc, out_ids)
125
126         # copy account.tax.code translations
127         in_ids = obj_tax_code_template.search(cr, uid, [('id', 'child_of', [tax_code_template_root_id])], order='id')[1:]
128         out_ids = obj_tax_code.search(cr, uid, [('id', 'child_of', [tax_code_root_id])], order='id')[1:]
129         warn_msg += self.copy_translations(cr, uid, langs, obj_tax_code_template, 'name', in_ids, obj_tax_code, out_ids)
130
131         # copy account.tax translations
132         in_ids = sorted([x.id for x in obj_multi.chart_template_id.tax_template_ids])
133         out_ids = obj_tax.search(cr, uid, [('company_id', '=', company_id)], order='id')
134         warn_msg += self.copy_translations(cr, uid, langs, obj_tax_template, 'name', in_ids, obj_tax, out_ids)
135
136         #open new wizard its for displaying warning message
137         if warn_msg:
138             res_id = self.pool.get('translate.message').create(cr, uid, {'message': warn_msg})
139             return {
140                 'res_id': res_id,
141                 'view_type': 'form',
142                 'view_mode': 'form',
143                 'res_model': 'translate.message',
144                 'views': [(resource_id and resource_id[1] or False,'form')],
145                 'type': 'ir.actions.act_window',
146                 'target': 'new',
147             }
148
149     def onchange_chart_template_id(self, cr, uid, ids, chart_template_id=False, context=None):
150         res = super(wizard_multi_charts_accounts, self).onchange_chart_template_id(cr, uid, ids, chart_template_id, context=context)
151         installed_lang = self.get_lang(cr, uid, chart_template_id, context=context)
152         res['value'].update({'lang_ids': installed_lang})
153         return res
154     
155     def get_lang(self, cr, uid, template_id=False, context=None):
156         installed_lang = []
157         if template_id:
158             cr.execute("SELECT module from ir_model_data where model='account.chart.template' and res_id=%s" % (template_id))
159             modulename = cr.fetchone()
160             modulename = modulename and modulename[0] or False
161             if modulename:
162                 module_obj = self.pool.get('ir.module.module')
163                 module_id = module_obj.search(cr, uid, [('name', '=', modulename)], context=context)
164                 module = module_obj.browse(cr, uid, module_id, context=context)[0]
165                 dirpath = module_obj._translations_subdir(module)
166                 if dirpath:
167                     for po in os.listdir(dirpath):
168                         lang_id = self.pool.get('res.lang').search(cr, uid, [('code', 'ilike', '%s' % (po.split('.')[0])), ('translatable', '=', True)], context=context)
169                         if lang_id:
170                             installed_lang.append(lang_id[0])
171         return installed_lang
172
173     def default_get(self, cr, uid, fields, context=None):
174         res = super(wizard_multi_charts_accounts, self).default_get(cr, uid, fields, context=context)
175         installed_lang = self.get_lang(cr, uid, res.get('chart_template_id'), context=context)
176         res.update({'lang_ids': installed_lang, 'bank_accounts_id': []})
177         return res
178
179     _columns = {
180         'lang_ids': fields.many2many('res.lang', 'res_lang_type_rel', 'wizard_id', 'lang_id', 'Language'),
181         'bank_from_template': fields.boolean('Banks/Cash from Template', 
182             help="If True then Generate Bank/Cash accounts and journals from the Templates.", readonly=True),
183     }
184   
185 wizard_multi_charts_accounts()
186
187 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: