2010-12-22 16:09:26 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# Copyright (C) 2010 OpenERP s.a. (<http://www.openerp.com>).
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
2012-12-06 14:56:32 +00:00
|
|
|
from openerp.osv import osv
|
2010-12-22 16:09:26 +00:00
|
|
|
|
|
|
|
"""Inherit res.currency to handle accounting date values when converting currencies"""
|
2011-01-04 12:57:06 +00:00
|
|
|
|
2010-12-22 16:09:26 +00:00
|
|
|
class res_currency_account(osv.osv):
|
|
|
|
_inherit = "res.currency"
|
|
|
|
|
|
|
|
def _get_conversion_rate(self, cr, uid, from_currency, to_currency, context=None):
|
|
|
|
if context is None:
|
|
|
|
context = {}
|
|
|
|
rate = super(res_currency_account, self)._get_conversion_rate(cr, uid, from_currency, to_currency, context=context)
|
2012-11-23 16:27:58 +00:00
|
|
|
#process the case where the account doesn't work with an outgoing currency rate method 'at date' but 'average'
|
2010-12-22 16:09:26 +00:00
|
|
|
account = context.get('res.currency.compute.account')
|
|
|
|
account_invert = context.get('res.currency.compute.account_invert')
|
|
|
|
if account and account.currency_mode == 'average' and account.currency_id:
|
|
|
|
query = self.pool.get('account.move.line')._query_get(cr, uid, context=context)
|
|
|
|
cr.execute('select sum(debit-credit),sum(amount_currency) from account_move_line l ' \
|
|
|
|
'where l.currency_id=%s and l.account_id=%s and '+query, (account.currency_id.id,account.id,))
|
|
|
|
tot1,tot2 = cr.fetchone()
|
|
|
|
if tot2 and not account_invert:
|
|
|
|
rate = float(tot1)/float(tot2)
|
|
|
|
elif tot1 and account_invert:
|
|
|
|
rate = float(tot2)/float(tot1)
|
|
|
|
return rate
|
|
|
|
|