From fd5d06ec568450c21d289f4e003294e546ec2201 Mon Sep 17 00:00:00 2001 From: Olivier Dony Date: Wed, 22 Dec 2010 17:09:26 +0100 Subject: [PATCH] [FIX] res.currency, account: moved account-related res.currency code from base lp bug: https://launchpad.net/bugs/557325 fixed bzr revid: odo@openerp.com-20101222160926-0eprbvw7wvqked81 --- addons/account/__init__.py | 1 + addons/account/account_move_line.py | 14 +++++++-- addons/account/res_currency.py | 44 +++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 addons/account/res_currency.py diff --git a/addons/account/__init__.py b/addons/account/__init__.py index 0608bb83c88..24129306e13 100644 --- a/addons/account/__init__.py +++ b/addons/account/__init__.py @@ -33,5 +33,6 @@ import report import product import sequence import company +import res_currency # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/account/account_move_line.py b/addons/account/account_move_line.py index ffd82d329f7..02b8be3a8c1 100644 --- a/addons/account/account_move_line.py +++ b/addons/account/account_move_line.py @@ -289,7 +289,12 @@ class account_move_line(osv.osv): acc = account if s>0: acc = acc1 - v = currency_obj.compute(cr, uid, account.company_id.currency_id.id, data['currency_id'], s, account=acc, account_invert=True) + compute_ctx = context.copy() + compute_ctx.update({ + 'res.currency.compute.account': acc, + 'res.currency.compute.account_invert': True, + }) + v = currency_obj.compute(cr, uid, account.company_id.currency_id.id, data['currency_id'], s, context=compute_ctx) data['amount_currency'] = v return data @@ -556,8 +561,11 @@ class account_move_line(osv.osv): if (amount>0) and journal: x = journal_obj.browse(cr, uid, journal).default_credit_account_id if x: acc = x - context.update({'date': date}) - v = currency_obj.compute(cr, uid, currency_id, acc.company_id.currency_id.id, amount, account=acc, context=context) + context.update({ + 'date': date, + 'res.currency.compute.account': acc, + }) + v = currency_obj.compute(cr, uid, currency_id, acc.company_id.currency_id.id, amount, context=context) result['value'] = { 'debit': v > 0 and v or 0.0, 'credit': v < 0 and -v or 0.0 diff --git a/addons/account/res_currency.py b/addons/account/res_currency.py new file mode 100644 index 00000000000..6ec404edcf4 --- /dev/null +++ b/addons/account/res_currency.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# Copyright (C) 2010 OpenERP s.a. (). +# +# 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 . +# +############################################################################## + +from osv import osv + +"""Inherit res.currency to handle accounting date values when converting currencies""" +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) + 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 + +res_currency_account() \ No newline at end of file