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 osv import fields, osv
24 from tools.translate import _
27 class translate_message(osv.osv_memory):
28 _name = "translate.message"
29 _description = "Translation Message"
32 'message' : fields.text('Message', readonly=True),
38 class wizard_multi_charts_accounts(osv.osv_memory):
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.
44 _inherit = 'wizard.multi.charts.accounts'
46 def copy_translations(self, cr, uid, langs, in_obj, in_field, in_ids, out_obj, out_ids):
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
58 @param Return: String containing information of the translations loaded.
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})
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)):
73 # if source have no translation available
74 notdone.append(src[in_id])
76 #copy Translation from Source to Destination object
77 xlat_obj.create(cr, uid, {
78 'name': out_obj._name + ',' + in_field,
83 'value': value[in_id],
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))
89 message += '\nLanguage: %s \n\tTranslation successfully done for %s(s) .' % (lang, out_obj._description)
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')
104 resource_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'l10n_multilang', 'view_translate_message_wizard')
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]
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)
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)
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)
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)
136 #open new wizard its for displaying warning message
138 res_id = self.pool.get('translate.message').create(cr, uid, {'message': warn_msg})
143 'res_model': 'translate.message',
144 'views': [(resource_id and resource_id[1] or False,'form')],
145 'type': 'ir.actions.act_window',
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})
155 def get_lang(self, cr, uid, template_id=False, context=None):
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
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)
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)
170 installed_lang.append(lang_id[0])
171 return installed_lang
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': []})
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),
185 wizard_multi_charts_accounts()
187 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: