diff --git a/currency_rate_update/model/company.py b/currency_rate_update/model/company.py index 8c8b66feb..a5f7033af 100644 --- a/currency_rate_update/model/company.py +++ b/currency_rate_update/model/company.py @@ -18,142 +18,44 @@ # along with this program. If not, see . # ############################################################################## -from openerp.osv import fields, orm + +from openerp import models, fields, api, _ -class res_company(orm.Model): +class res_company(models.Model): """override company to add currency update""" - def _multi_curr_enable(self, cr, uid, ids, field_name, arg, context=None): + @api.multi + def _compute_multi_curr_enable(self): "check if multi company currency is enabled" - result = {} - field_ids = self.pool.get('ir.model.fields').search( - cr, uid, - [('name', '=', 'company_id'), - ('model', '=', 'res.currency')] - ) - if not field_ids: - enable = 0 - else: - enable = 1 - for id in ids: - result[id] = enable - return result + company_currency = self.env['res.currency'].search([('company_id', + '!=', False)]) + for company in self: + company.multi_company_currency_enable = \ + 1 if company_currency else 0 - def button_refresh_currency(self, cr, uid, ids, context=None): - """Refrech the currency for all the company now""" - currency_updater_obj = self.pool.get('currency.rate.update') - try: - currency_updater_obj.run_currency_update(cr, uid) - except Exception: - return False - return True - - def on_change_auto_currency_up(self, cr, uid, id, value): - """handle the activation of the currecny update on compagnies. - There are two ways of implementing multi_company currency, - the currency is shared or not. The module take care of the two - ways. If the currency are shared, you will only be able to set - auto update on one company, this will avoid to have unusefull cron - object running. - If yours currency are not share you will be able to activate the - auto update on each separated company - - """ - - if len(id): - id = id[0] - else: - return {} - enable = self.browse(cr, uid, id).multi_company_currency_enable - compagnies = self.search(cr, uid, []) - activate_cron = 'f' - if not value: - # this statement is here beacaus we do no want to save # - # in case of error - self.write(cr, uid, id, {'auto_currency_up': value}) - for comp in compagnies: - if self.browse(cr, uid, comp).auto_currency_up: - activate_cron = 't' - break - self.pool.get('currency.rate.update').save_cron( - cr, - uid, - {'active': activate_cron} - ) - return {} - else: - for comp in compagnies: - if comp != id and not enable: - current = self.browse(cr, uid, comp) - if current.multi_company_currency_enable: - # We ensure taht we did not have write a true value - self.write(cr, uid, id, {'auto_currency_up': False}) - msg = ('You can not activate auto currency' - 'update on more thant one company with this ' - 'multi company configuration') - return { - 'value': {'auto_currency_up': False}, - - 'warning': { - 'title': "Warning", - 'message': msg, - } - } - self.write(cr, uid, id, {'auto_currency_up': value}) - for comp in compagnies: - if self.browse(cr, uid, comp).auto_currency_up: - activate_cron = 't' - self.pool.get('currency.rate.update').save_cron( - cr, - uid, - {'active': activate_cron} - ) - break - return {} - - def on_change_intervall(self, cr, uid, id, interval): - # Function that will update the cron freqeuence - self.pool.get('currency.rate.update').save_cron( - cr, - uid, - {'interval_type': interval} - ) - compagnies = self.search(cr, uid, []) - for comp in compagnies: - self.write(cr, uid, comp, {'interval_type': interval}) - return {} + @api.one + def button_refresh_currency(self): + """Refresh the currencies rates !!for all companies now""" + self.services_to_use.refresh_currency() _inherit = "res.company" - _columns = { - # Activate the currency update - 'auto_currency_up': fields.boolean( - 'Automatical update of the currency this company' - ), - 'services_to_use': fields.one2many( - 'currency.rate.update.service', - 'company_id', - 'Currency update services' - ), - # Predifine cron frequence - 'interval_type': fields.selection( - [ - ('days', 'Day(s)'), - ('weeks', 'Week(s)'), - ('months', 'Month(s)') - ], - 'Currency update frequence', - help="Changing this value will " - "also affect other compagnies" - ), - # Function field that allows to know the - # mutli company currency implementation - 'multi_company_currency_enable': fields.function( - _multi_curr_enable, - method=True, - type='boolean', - string="Multi company currency", - help="If this case is not check you can" - " not set currency is active on two company" - ), - } + _name = "res.company" + + # Activate the currency update + auto_currency_up = fields.Boolean( + string="Automatic Update", + help="Automatical update of the currencies for this company") + # Function field that allows to know the + # multi company currency implementation + multi_company_currency_enable = fields.Boolean( + string="Multi company currency", + compute="_compute_multi_curr_enable", + help="When this option is unchecked it will allow users " + "to set a distinct currency updates on each company." + ) + # List of services to fetch rates + services_to_use = fields.One2many( + 'currency.rate.update.service', + 'company_id', + 'Currency update services')