a7d3e5bc34592660d58e15f7f95fa0dcea74e8e0
[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 openerp.osv import osv
22
23 """Inherit res.currency to handle accounting date values when converting currencies"""
24
25 class res_currency_account(osv.osv):
26     _inherit = "res.currency"
27
28     def _get_conversion_rate(self, cr, uid, from_currency, to_currency, context=None):
29         if context is None:
30             context = {}
31         rate = super(res_currency_account, self)._get_conversion_rate(cr, uid, from_currency, to_currency, context=context)
32         #process the case where the account doesn't work with an outgoing currency rate method 'at date' but 'average'
33         account = context.get('res.currency.compute.account')
34         account_invert = context.get('res.currency.compute.account_invert')
35         if account and account.currency_mode == 'average' and account.currency_id:
36             query = self.pool.get('account.move.line')._query_get(cr, uid, context=context)
37             cr.execute('select sum(debit-credit),sum(amount_currency) from account_move_line l ' \
38               'where l.currency_id=%s and l.account_id=%s and '+query, (account.currency_id.id,account.id,))
39             tot1,tot2 = cr.fetchone()
40             if tot2 and not account_invert:
41                 rate = float(tot1)/float(tot2)
42             elif tot1 and account_invert:
43                 rate = float(tot2)/float(tot1)
44         return rate
45
46
47 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: