[MERGE] lp:~xrg/openobject-addons/trunk-patch18
[odoo/odoo.git] / addons / account / res_currency.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    Copyright (C) 2010 OpenERP s.a. (<http://www.openerp.com>).
5 #
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU Affero General Public License as
8 #    published by the Free Software Foundation, either version 3 of the
9 #    License, or (at your option) any later version.
10 #
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU Affero General Public License for more details.
15 #
16 #    You should have received a copy of the GNU Affero General Public License
17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 ##############################################################################
20
21 from osv import osv
22
23 """Inherit res.currency to handle accounting date values when converting currencies"""
24 class res_currency_account(osv.osv):
25     _inherit = "res.currency"
26
27     def _get_conversion_rate(self, cr, uid, from_currency, to_currency, context=None):
28         if context is None:
29             context = {}
30         rate = super(res_currency_account, self)._get_conversion_rate(cr, uid, from_currency, to_currency, context=context)
31         account = context.get('res.currency.compute.account')
32         account_invert = context.get('res.currency.compute.account_invert')
33         if account and account.currency_mode == 'average' and account.currency_id:
34             query = self.pool.get('account.move.line')._query_get(cr, uid, context=context)
35             cr.execute('select sum(debit-credit),sum(amount_currency) from account_move_line l ' \
36               'where l.currency_id=%s and l.account_id=%s and '+query, (account.currency_id.id,account.id,))
37             tot1,tot2 = cr.fetchone()
38             if tot2 and not account_invert:
39                 rate = float(tot1)/float(tot2)
40             elif tot1 and account_invert:
41                 rate = float(tot2)/float(tot1)
42         return rate
43
44 res_currency_account()