From 2f4330bbb32ec294843158fb16f50ff08e55314c Mon Sep 17 00:00:00 2001 From: Mustufa Rangwala Date: Thu, 14 Jul 2011 14:57:37 +0530 Subject: [PATCH 01/20] [ADD] Base: add res.currency.rate.type object and its m2o on res.currency.rate bzr revid: mra@mra-laptop-20110714092737-9jlk0xjc3hkb4rog --- openerp/addons/base/res/res_currency.py | 11 +++++++++++ openerp/addons/base/res/res_currency_view.xml | 1 + 2 files changed, 12 insertions(+) diff --git a/openerp/addons/base/res/res_currency.py b/openerp/addons/base/res/res_currency.py index d81248a735d..fa718ddffbc 100644 --- a/openerp/addons/base/res/res_currency.py +++ b/openerp/addons/base/res/res_currency.py @@ -133,6 +133,15 @@ class res_currency(osv.osv): res_currency() +class res_currency_rate_type(osv.osv): + _name = "res.currency.rate.type" + _description = "Currency Rate Type" + _columns = { + 'name': fields.char('Name', size=32, required=True), + } + +res_currency_rate_type() + class res_currency_rate(osv.osv): _name = "res.currency.rate" _description = "Currency Rate" @@ -141,11 +150,13 @@ class res_currency_rate(osv.osv): 'rate': fields.float('Rate', digits=(12,6), required=True, help='The rate of the currency to the currency of rate 1'), 'currency_id': fields.many2one('res.currency', 'Currency', readonly=True), + 'currency_rate_type_id': fields.many2one('res.currency.rate.type', 'Currency Rate Type'), } _defaults = { 'name': lambda *a: time.strftime('%Y-%m-%d'), } _order = "name desc" + res_currency_rate() # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/openerp/addons/base/res/res_currency_view.xml b/openerp/addons/base/res/res_currency_view.xml index 37a8693e5b3..346da2488c9 100644 --- a/openerp/addons/base/res/res_currency_view.xml +++ b/openerp/addons/base/res/res_currency_view.xml @@ -48,6 +48,7 @@
+ From 72db8f87cbacb53b7f098531aa0b9607516ed353 Mon Sep 17 00:00:00 2001 From: Mustufa Rangwala Date: Thu, 14 Jul 2011 16:15:17 +0530 Subject: [PATCH 02/20] [IMP] Base: add argument on compute method bzr revid: mra@mra-laptop-20110714104517-11w0ptxmob5s481n --- openerp/addons/base/res/res_currency.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/openerp/addons/base/res/res_currency.py b/openerp/addons/base/res/res_currency.py index fa718ddffbc..957c0fb625f 100644 --- a/openerp/addons/base/res/res_currency.py +++ b/openerp/addons/base/res/res_currency.py @@ -43,6 +43,7 @@ class res_currency(osv.osv): res[id] = rate else: res[id] = 0 + return res _name = "res.currency" _description = "Currency" @@ -68,7 +69,7 @@ class res_currency(osv.osv): _order = "name" def read(self, cr, user, ids, fields=None, context=None, load='_classic_read'): - res=super(osv.osv, self).read(cr, user, ids, fields, context, load) + res = super(osv.osv, self).read(cr, user, ids, fields, context, load) for r in res: if r.__contains__('rate_ids'): rates=r['rate_ids'] @@ -76,6 +77,7 @@ class res_currency(osv.osv): currency_rate_obj= self.pool.get('res.currency.rate') currency_date = currency_rate_obj.read(cr,user,rates[0],['name'])['name'] r['date'] = currency_date + return res def name_get(self, cr, uid, ids, context=None): @@ -109,13 +111,17 @@ class res_currency(osv.osv): raise osv.except_osv(_('Error'), _('No rate found \n' \ 'for the currency: %s \n' \ 'at the date: %s') % (currency_symbol, date)) + return to_currency.rate/from_currency.rate - def compute(self, cr, uid, from_currency_id, to_currency_id, from_amount, round=True, context=None): + def compute(self, cr, uid, from_currency_id, to_currency_id, from_amount, round=True, rate_type=False, context=None): if not from_currency_id: from_currency_id = to_currency_id if not to_currency_id: to_currency_id = from_currency_id + + if rate_type: + context.update({'currency_rate_type_id': rate_type}) xc = self.browse(cr, uid, [from_currency_id,to_currency_id], context=context) from_currency = (xc[0].id == from_currency_id and xc[0]) or xc[1] to_currency = (xc[0].id == to_currency_id and xc[0]) or xc[1] From 5c7075cac6ae44b6daf2bb55793f10aa855c0a44 Mon Sep 17 00:00:00 2001 From: "Ujjvala Collins (OpenERP)" Date: Tue, 12 Jul 2011 17:06:01 +0530 Subject: [PATCH 03/20] [IMP] base: Added a condition to check currency rate type. bzr revid: uco@tinyerp.com-20110712113601-slvo0c3590a7dkxf --- openerp/addons/base/res/res_currency.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/openerp/addons/base/res/res_currency.py b/openerp/addons/base/res/res_currency.py index 957c0fb625f..46b4dd45207 100644 --- a/openerp/addons/base/res/res_currency.py +++ b/openerp/addons/base/res/res_currency.py @@ -36,8 +36,12 @@ class res_currency(osv.osv): else: date = time.strftime('%Y-%m-%d') date = date or time.strftime('%Y-%m-%d') + currency_rate_type_id = context.get('currency_rate_type_id', 0) for id in ids: - cr.execute("SELECT currency_id, rate FROM res_currency_rate WHERE currency_id = %s AND name <= %s ORDER BY name desc LIMIT 1" ,(id, date)) + cr.execute("SELECT currency_id, rate FROM res_currency_rate "\ + "WHERE currency_id = %s AND name <= %s AND currency_rate_type_id = %s "\ + "ORDER BY name desc LIMIT 1" ,(id, date, currency_rate_type_id)) + if cr.rowcount: id, rate = cr.fetchall()[0] res[id] = rate From b9cc3d22b6c9cd4f79c05aa4a91c054494392467 Mon Sep 17 00:00:00 2001 From: "Ujjvala Collins (OpenERP)" Date: Tue, 12 Jul 2011 18:12:53 +0530 Subject: [PATCH 04/20] [IMP] base: Improved currency rate function to avoid sql query. bzr revid: uco@tinyerp.com-20110712124253-vy1g8lq40jsdmyi8 --- openerp/addons/base/res/res_currency.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/openerp/addons/base/res/res_currency.py b/openerp/addons/base/res/res_currency.py index 46b4dd45207..9d6b183bba8 100644 --- a/openerp/addons/base/res/res_currency.py +++ b/openerp/addons/base/res/res_currency.py @@ -28,6 +28,7 @@ from tools.translate import _ class res_currency(osv.osv): def _current_rate(self, cr, uid, ids, name, arg, context=None): + currency_rate_obj = self.pool.get('res.currency.rate') if context is None: context = {} res = {} @@ -36,15 +37,16 @@ class res_currency(osv.osv): else: date = time.strftime('%Y-%m-%d') date = date or time.strftime('%Y-%m-%d') - currency_rate_type_id = context.get('currency_rate_type_id', 0) + domain = [('currency_id','in',ids), ('name','<=',date)] + currency_rate_type_id = context.get('currency_rate_type_id', False) + if currency_rate_type_id: + domain.append(('currency_rate_type_id','=',currency_rate_type_id)) + curr_rate_ids = currency_rate_obj.search(cr, uid, domain, context=context) for id in ids: - cr.execute("SELECT currency_id, rate FROM res_currency_rate "\ - "WHERE currency_id = %s AND name <= %s AND currency_rate_type_id = %s "\ - "ORDER BY name desc LIMIT 1" ,(id, date, currency_rate_type_id)) - - if cr.rowcount: - id, rate = cr.fetchall()[0] - res[id] = rate + if curr_rate_ids: + for cur in currency_rate_obj.browse(cr, uid, curr_rate_ids, context=context): + if cur.currency_id.id == id: + res[id] = cur.rate else: res[id] = 0 From a6b7507ae5b4e4385ca2d34305be3293e29b1659 Mon Sep 17 00:00:00 2001 From: "Ujjvala Collins (OpenERP)" Date: Wed, 13 Jul 2011 11:05:03 +0530 Subject: [PATCH 05/20] [IMP] base: Improved _currency_rate function. bzr revid: uco@tinyerp.com-20110713053503-rejienqc30ocr78g --- openerp/addons/base/res/res_currency.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/openerp/addons/base/res/res_currency.py b/openerp/addons/base/res/res_currency.py index 9d6b183bba8..e4b5c73bd02 100644 --- a/openerp/addons/base/res/res_currency.py +++ b/openerp/addons/base/res/res_currency.py @@ -41,14 +41,14 @@ class res_currency(osv.osv): currency_rate_type_id = context.get('currency_rate_type_id', False) if currency_rate_type_id: domain.append(('currency_rate_type_id','=',currency_rate_type_id)) - curr_rate_ids = currency_rate_obj.search(cr, uid, domain, context=context) + curr_rate_ids = currency_rate_obj.search(cr, uid, domain, order='name desc', context=context) + curr_rates = currency_rate_obj.browse(cr, uid, curr_rate_ids, context=context) for id in ids: - if curr_rate_ids: - for cur in currency_rate_obj.browse(cr, uid, curr_rate_ids, context=context): - if cur.currency_id.id == id: - res[id] = cur.rate - else: - res[id] = 0 + res[id] = 0 + for cur in curr_rates: + if cur.currency_id.id == id: + res[id] = cur.rate + break return res _name = "res.currency" From 33947346e4393d7861c792e08452ec74e3da20e4 Mon Sep 17 00:00:00 2001 From: Mustufa Rangwala Date: Fri, 15 Jul 2011 11:25:41 +0530 Subject: [PATCH 06/20] [IMP] Base: Currency improve code bzr revid: mra@mra-laptop-20110715055541-il3hhp42b9jiojo4 --- openerp/addons/base/res/res_currency.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/openerp/addons/base/res/res_currency.py b/openerp/addons/base/res/res_currency.py index e4b5c73bd02..30d93921bd3 100644 --- a/openerp/addons/base/res/res_currency.py +++ b/openerp/addons/base/res/res_currency.py @@ -29,18 +29,17 @@ from tools.translate import _ class res_currency(osv.osv): def _current_rate(self, cr, uid, ids, name, arg, context=None): currency_rate_obj = self.pool.get('res.currency.rate') + res = {} if context is None: context = {} - res = {} if 'date' in context: date = context['date'] else: date = time.strftime('%Y-%m-%d') - date = date or time.strftime('%Y-%m-%d') - domain = [('currency_id','in',ids), ('name','<=',date)] + domain = [('currency_id', 'in', ids), ('name', '<=', date)] currency_rate_type_id = context.get('currency_rate_type_id', False) if currency_rate_type_id: - domain.append(('currency_rate_type_id','=',currency_rate_type_id)) + domain.append(('currency_rate_type_id', '=', currency_rate_type_id)) curr_rate_ids = currency_rate_obj.search(cr, uid, domain, order='name desc', context=context) curr_rates = currency_rate_obj.browse(cr, uid, curr_rate_ids, context=context) for id in ids: @@ -49,8 +48,8 @@ class res_currency(osv.osv): if cur.currency_id.id == id: res[id] = cur.rate break - return res + _name = "res.currency" _description = "Currency" _columns = { @@ -75,15 +74,14 @@ class res_currency(osv.osv): _order = "name" def read(self, cr, user, ids, fields=None, context=None, load='_classic_read'): + currency_rate_obj= self.pool.get('res.currency.rate') res = super(osv.osv, self).read(cr, user, ids, fields, context, load) for r in res: if r.__contains__('rate_ids'): rates=r['rate_ids'] if rates: - currency_rate_obj= self.pool.get('res.currency.rate') - currency_date = currency_rate_obj.read(cr,user,rates[0],['name'])['name'] + currency_date = currency_rate_obj.read(cr, user, rates[0], ['name'])['name'] r['date'] = currency_date - return res def name_get(self, cr, uid, ids, context=None): From efe44324d194c7e58b324ae8d9f18c533897d0bf Mon Sep 17 00:00:00 2001 From: "Ujjvala Collins (OpenERP)" Date: Fri, 15 Jul 2011 11:58:37 +0530 Subject: [PATCH 07/20] [ADD] base: Added view and menu item for currency rate type object. bzr revid: uco@tinyerp.com-20110715062837-bw70on2op2b0pv1z --- openerp/addons/base/res/res_currency_view.xml | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/openerp/addons/base/res/res_currency_view.xml b/openerp/addons/base/res/res_currency_view.xml index 346da2488c9..63a55c0fddf 100644 --- a/openerp/addons/base/res/res_currency_view.xml +++ b/openerp/addons/base/res/res_currency_view.xml @@ -67,5 +67,28 @@ + + + res.currency.rate.type.form + res.currency.rate.type + form + +
+ + +
+
+ + + Currency Rate Type + res.currency.rate.type + form + tree,form + + + + From 416b2693ac0185d0d706c805319a3a3bcc3104d3 Mon Sep 17 00:00:00 2001 From: "Ujjvala Collins (OpenERP)" Date: Fri, 19 Aug 2011 16:37:19 +0530 Subject: [PATCH 08/20] [IMP] base: Improved arguments in compute method to avoid error of No rate found. bzr revid: uco@tinyerp.com-20110819110719-r82nsaui8a2kk58i --- openerp/addons/base/res/res_currency.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/openerp/addons/base/res/res_currency.py b/openerp/addons/base/res/res_currency.py index 30d93921bd3..5e69daddb13 100644 --- a/openerp/addons/base/res/res_currency.py +++ b/openerp/addons/base/res/res_currency.py @@ -118,17 +118,17 @@ class res_currency(osv.osv): return to_currency.rate/from_currency.rate - def compute(self, cr, uid, from_currency_id, to_currency_id, from_amount, round=True, rate_type=False, context=None): + def compute(self, cr, uid, from_currency_id, to_currency_id, from_amount, + round=True, currency_rate_type_from=False, currency_rate_type_to=False, context=None): if not from_currency_id: from_currency_id = to_currency_id if not to_currency_id: to_currency_id = from_currency_id - if rate_type: - context.update({'currency_rate_type_id': rate_type}) - xc = self.browse(cr, uid, [from_currency_id,to_currency_id], context=context) + xc = self.browse(cr, uid, [from_currency_id], context={'currency_rate_type_id': currency_rate_type_from}) + xc1 = self.browse(cr, uid, [to_currency_id], context={'currency_rate_type_id': currency_rate_type_to}) from_currency = (xc[0].id == from_currency_id and xc[0]) or xc[1] - to_currency = (xc[0].id == to_currency_id and xc[0]) or xc[1] + to_currency = (xc1[0].id == to_currency_id and xc1[0]) or xc1[1] if to_currency_id == from_currency_id: if round: return self.round(cr, uid, to_currency, from_amount) From bd5cb5e54af9a311753037b96f032503a35e65bc Mon Sep 17 00:00:00 2001 From: "Ujjvala Collins (OpenERP)" Date: Tue, 23 Aug 2011 17:31:16 +0530 Subject: [PATCH 09/20] [IMP] base: Improved code to update the context. bzr revid: uco@tinyerp.com-20110823120116-6mrgalyg77jthxnt --- openerp/addons/base/res/res_currency.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/openerp/addons/base/res/res_currency.py b/openerp/addons/base/res/res_currency.py index 5e69daddb13..8a8067c6ff4 100644 --- a/openerp/addons/base/res/res_currency.py +++ b/openerp/addons/base/res/res_currency.py @@ -125,8 +125,14 @@ class res_currency(osv.osv): if not to_currency_id: to_currency_id = from_currency_id - xc = self.browse(cr, uid, [from_currency_id], context={'currency_rate_type_id': currency_rate_type_from}) - xc1 = self.browse(cr, uid, [to_currency_id], context={'currency_rate_type_id': currency_rate_type_to}) + context.update({'currency_rate_type_id': False}) + if currency_rate_type_from: + context.update({'currency_rate_type_id': currency_rate_type_from}) + if currency_rate_type_to: + context.update({'currency_rate_type_id': currency_rate_type_to}) + + xc = self.browse(cr, uid, [from_currency_id], context=context) + xc1 = self.browse(cr, uid, [to_currency_id], context=context) from_currency = (xc[0].id == from_currency_id and xc[0]) or xc[1] to_currency = (xc1[0].id == to_currency_id and xc1[0]) or xc1[1] if to_currency_id == from_currency_id: From 108db379d07eef72ba427428c83c8b799c2cc458 Mon Sep 17 00:00:00 2001 From: "Ujjvala Collins (OpenERP)" Date: Wed, 24 Aug 2011 11:14:16 +0530 Subject: [PATCH 10/20] [REF] base: Changed code to avoid overriding of context values. bzr revid: uco@tinyerp.com-20110824054416-ngrtkk5oh1v9jgb3 --- openerp/addons/base/res/res_currency.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/openerp/addons/base/res/res_currency.py b/openerp/addons/base/res/res_currency.py index 8a8067c6ff4..13bb14d0578 100644 --- a/openerp/addons/base/res/res_currency.py +++ b/openerp/addons/base/res/res_currency.py @@ -125,13 +125,9 @@ class res_currency(osv.osv): if not to_currency_id: to_currency_id = from_currency_id - context.update({'currency_rate_type_id': False}) - if currency_rate_type_from: - context.update({'currency_rate_type_id': currency_rate_type_from}) - if currency_rate_type_to: - context.update({'currency_rate_type_id': currency_rate_type_to}) - + context.update({'currency_rate_type_id': currency_rate_type_from}) xc = self.browse(cr, uid, [from_currency_id], context=context) + context.update({'currency_rate_type_id': currency_rate_type_to}) xc1 = self.browse(cr, uid, [to_currency_id], context=context) from_currency = (xc[0].id == from_currency_id and xc[0]) or xc[1] to_currency = (xc1[0].id == to_currency_id and xc1[0]) or xc1[1] From 2ef83855446422320dea2bc3f9739647db523bfe Mon Sep 17 00:00:00 2001 From: "Ujjvala Collins (OpenERP)" Date: Wed, 24 Aug 2011 14:40:22 +0530 Subject: [PATCH 11/20] [FIX] base: Fixed currency rate argument problem in context. bzr revid: uco@tinyerp.com-20110824091022-0n1iwkefv2h4nh74 --- openerp/addons/base/res/res_currency.py | 32 ++++++++++++++++--------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/openerp/addons/base/res/res_currency.py b/openerp/addons/base/res/res_currency.py index 13bb14d0578..495a931f91d 100644 --- a/openerp/addons/base/res/res_currency.py +++ b/openerp/addons/base/res/res_currency.py @@ -103,41 +103,51 @@ class res_currency(osv.osv): def is_zero(self, cr, uid, currency, amount): return abs(self.round(cr, uid, currency, amount)) < currency.rounding - def _get_conversion_rate(self, cr, uid, from_currency, to_currency, context=None): + def _get_conversion_rate(self, cr, uid, from_currency_rate, to_currency_rate, context=None): if context is None: context = {} - if from_currency['rate'] == 0 or to_currency['rate'] == 0: + if from_currency_rate == 0 or to_currency_rate == 0: date = context.get('date', time.strftime('%Y-%m-%d')) - if from_currency['rate'] == 0: - currency_symbol = from_currency.symbol + if from_currency_rate == 0: + currency_symbol = context.get('from_curr_symbol') else: - currency_symbol = to_currency.symbol + currency_symbol = context.get('to_curr_symbol') raise osv.except_osv(_('Error'), _('No rate found \n' \ 'for the currency: %s \n' \ 'at the date: %s') % (currency_symbol, date)) - return to_currency.rate/from_currency.rate + return to_currency_rate/from_currency_rate def compute(self, cr, uid, from_currency_id, to_currency_id, from_amount, round=True, currency_rate_type_from=False, currency_rate_type_to=False, context=None): + if not context: + context = {} if not from_currency_id: from_currency_id = to_currency_id if not to_currency_id: to_currency_id = from_currency_id - context.update({'currency_rate_type_id': currency_rate_type_from}) - xc = self.browse(cr, uid, [from_currency_id], context=context) - context.update({'currency_rate_type_id': currency_rate_type_to}) - xc1 = self.browse(cr, uid, [to_currency_id], context=context) + ctx1 = context.copy() + ctx1.update({'currency_rate_type_id': currency_rate_type_from}) + xc = self.browse(cr, uid, [from_currency_id], context=ctx1) from_currency = (xc[0].id == from_currency_id and xc[0]) or xc[1] + from_currency_rate = from_currency['rate'] + context.update({'from_curr_symbol': from_currency.symbol}) + + ctx2 = context.copy() + ctx2.update({'currency_rate_type_id': currency_rate_type_to}) + xc1 = self.browse(cr, uid, [to_currency_id], context=ctx2) to_currency = (xc1[0].id == to_currency_id and xc1[0]) or xc1[1] + to_currency_rate = to_currency['rate'] + context.update({'to_curr_symbol': to_currency.symbol}) + if to_currency_id == from_currency_id: if round: return self.round(cr, uid, to_currency, from_amount) else: return from_amount else: - rate = self._get_conversion_rate(cr, uid, from_currency, to_currency, context=context) + rate = self._get_conversion_rate(cr, uid, from_currency_rate, to_currency_rate, context=context) if round: return self.round(cr, uid, to_currency, from_amount * rate) else: From 5b17d2ccb7638543f02ac4d018a984441c7f2ce2 Mon Sep 17 00:00:00 2001 From: "Ujjvala Collins (OpenERP)" Date: Thu, 25 Aug 2011 17:26:45 +0530 Subject: [PATCH 12/20] [FIX] base: Fixed problem of browse method in _get_conversion_rate method. bzr revid: uco@tinyerp.com-20110825115645-85ya32yjnpirbuga --- openerp/addons/base/res/res_currency.py | 34 ++++++++++++------------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/openerp/addons/base/res/res_currency.py b/openerp/addons/base/res/res_currency.py index 495a931f91d..f8bd4f21893 100644 --- a/openerp/addons/base/res/res_currency.py +++ b/openerp/addons/base/res/res_currency.py @@ -103,15 +103,24 @@ class res_currency(osv.osv): def is_zero(self, cr, uid, currency, amount): return abs(self.round(cr, uid, currency, amount)) < currency.rounding - def _get_conversion_rate(self, cr, uid, from_currency_rate, to_currency_rate, context=None): + def _get_conversion_rate(self, cr, uid, from_currency, to_currency, context=None): if context is None: context = {} + ctx = context.copy() + ctx.update({'currency_rate_type_id': ctx.get('currency_rate_type_from')}) + from_currency = self.browse(cr, uid, [from_currency.id], context=ctx)[0] + from_currency_rate = from_currency['rate'] + + ctx.update({'currency_rate_type_id': ctx.get('currency_rate_type_to')}) + to_currency = self.browse(cr, uid, [to_currency.id], context=ctx)[0] + to_currency_rate = to_currency['rate'] + if from_currency_rate == 0 or to_currency_rate == 0: date = context.get('date', time.strftime('%Y-%m-%d')) if from_currency_rate == 0: - currency_symbol = context.get('from_curr_symbol') + currency_symbol = from_currency.symbol else: - currency_symbol = context.get('to_curr_symbol') + currency_symbol = to_currency.symbol raise osv.except_osv(_('Error'), _('No rate found \n' \ 'for the currency: %s \n' \ 'at the date: %s') % (currency_symbol, date)) @@ -126,28 +135,17 @@ class res_currency(osv.osv): from_currency_id = to_currency_id if not to_currency_id: to_currency_id = from_currency_id - - ctx1 = context.copy() - ctx1.update({'currency_rate_type_id': currency_rate_type_from}) - xc = self.browse(cr, uid, [from_currency_id], context=ctx1) + xc = self.browse(cr, uid, [from_currency_id,to_currency_id], context=context) from_currency = (xc[0].id == from_currency_id and xc[0]) or xc[1] - from_currency_rate = from_currency['rate'] - context.update({'from_curr_symbol': from_currency.symbol}) - - ctx2 = context.copy() - ctx2.update({'currency_rate_type_id': currency_rate_type_to}) - xc1 = self.browse(cr, uid, [to_currency_id], context=ctx2) - to_currency = (xc1[0].id == to_currency_id and xc1[0]) or xc1[1] - to_currency_rate = to_currency['rate'] - context.update({'to_curr_symbol': to_currency.symbol}) - + to_currency = (xc[0].id == to_currency_id and xc[0]) or xc[1] if to_currency_id == from_currency_id: if round: return self.round(cr, uid, to_currency, from_amount) else: return from_amount else: - rate = self._get_conversion_rate(cr, uid, from_currency_rate, to_currency_rate, context=context) + context.update({'currency_rate_type_from': currency_rate_type_from, 'currency_rate_type_to': currency_rate_type_to}) + rate = self._get_conversion_rate(cr, uid, from_currency, to_currency, context=context) if round: return self.round(cr, uid, to_currency, from_amount * rate) else: From e117018b7b0d7896ad886c871589a7defb45dec5 Mon Sep 17 00:00:00 2001 From: "Mustufa Rangwala (OpenERP)" Date: Thu, 25 Aug 2011 22:36:57 +0530 Subject: [PATCH 13/20] [REF] Base: small improvement bzr revid: mra@tinyerp.com-20110825170657-tgajqc4yknz9f83b --- openerp/addons/base/res/res_currency.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/openerp/addons/base/res/res_currency.py b/openerp/addons/base/res/res_currency.py index f8bd4f21893..8182a207c5c 100644 --- a/openerp/addons/base/res/res_currency.py +++ b/openerp/addons/base/res/res_currency.py @@ -107,11 +107,11 @@ class res_currency(osv.osv): if context is None: context = {} ctx = context.copy() - ctx.update({'currency_rate_type_id': ctx.get('currency_rate_type_from')}) + ctx.update({'currency_rate_type_id': ctx.get('currency_rate_type_from', False)}) from_currency = self.browse(cr, uid, [from_currency.id], context=ctx)[0] from_currency_rate = from_currency['rate'] - ctx.update({'currency_rate_type_id': ctx.get('currency_rate_type_to')}) + ctx.update({'currency_rate_type_id': ctx.get('currency_rate_type_to', False)}) to_currency = self.browse(cr, uid, [to_currency.id], context=ctx)[0] to_currency_rate = to_currency['rate'] @@ -124,7 +124,6 @@ class res_currency(osv.osv): raise osv.except_osv(_('Error'), _('No rate found \n' \ 'for the currency: %s \n' \ 'at the date: %s') % (currency_symbol, date)) - return to_currency_rate/from_currency_rate def compute(self, cr, uid, from_currency_id, to_currency_id, from_amount, From 46f690d64c0200cf72e22e5ccaad489ed5f11c1d Mon Sep 17 00:00:00 2001 From: "Mustufa Rangwala (OpenERP)" Date: Thu, 25 Aug 2011 23:06:51 +0530 Subject: [PATCH 14/20] [IMP] Base: added currency rate type on tree view bzr revid: mra@tinyerp.com-20110825173651-5rn1vsukicpdo331 --- openerp/addons/base/res/res_currency_view.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/openerp/addons/base/res/res_currency_view.xml b/openerp/addons/base/res/res_currency_view.xml index 63a55c0fddf..ddfee8fa090 100644 --- a/openerp/addons/base/res/res_currency_view.xml +++ b/openerp/addons/base/res/res_currency_view.xml @@ -53,6 +53,7 @@ +
From 9421188847a74992ffab17eea99674ba2a49feb0 Mon Sep 17 00:00:00 2001 From: "Quentin (OpenERP)" Date: Sun, 28 Aug 2011 00:53:30 +0200 Subject: [PATCH 15/20] [IMP] base/res_currency: improved res.currency.rate.type. Added search view and help tooltip. Made some code cleaning bzr revid: qdp-launchpad@openerp.com-20110827225330-3ng37d8yg31psgsd --- openerp/addons/base/res/res_currency.py | 45 +++++++++---------- openerp/addons/base/res/res_currency_view.xml | 12 ++++- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/openerp/addons/base/res/res_currency.py b/openerp/addons/base/res/res_currency.py index 8182a207c5c..5333a8eab20 100644 --- a/openerp/addons/base/res/res_currency.py +++ b/openerp/addons/base/res/res_currency.py @@ -28,28 +28,24 @@ from tools.translate import _ class res_currency(osv.osv): def _current_rate(self, cr, uid, ids, name, arg, context=None): - currency_rate_obj = self.pool.get('res.currency.rate') - res = {} if context is None: context = {} + res = {} if 'date' in context: date = context['date'] else: date = time.strftime('%Y-%m-%d') - domain = [('currency_id', 'in', ids), ('name', '<=', date)] - currency_rate_type_id = context.get('currency_rate_type_id', False) - if currency_rate_type_id: - domain.append(('currency_rate_type_id', '=', currency_rate_type_id)) - curr_rate_ids = currency_rate_obj.search(cr, uid, domain, order='name desc', context=context) - curr_rates = currency_rate_obj.browse(cr, uid, curr_rate_ids, context=context) + date = date or time.strftime('%Y-%m-%d') + currency_rate_type = context.get('currency_rate_type_id', None) + operator = currency_rate_type and '=' or 'is' for id in ids: - res[id] = 0 - for cur in curr_rates: - if cur.currency_id.id == id: - res[id] = cur.rate - break + cr.execute("SELECT currency_id, rate FROM res_currency_rate WHERE currency_id = %s AND name <= %s AND currency_rate_type_id " + operator +" %s ORDER BY name desc LIMIT 1" ,(id, date, currency_rate_type)) + if cr.rowcount: + id, rate = cr.fetchall()[0] + res[id] = rate + else: + res[id] = 0 return res - _name = "res.currency" _description = "Currency" _columns = { @@ -107,24 +103,22 @@ class res_currency(osv.osv): if context is None: context = {} ctx = context.copy() - ctx.update({'currency_rate_type_id': ctx.get('currency_rate_type_from', False)}) - from_currency = self.browse(cr, uid, [from_currency.id], context=ctx)[0] - from_currency_rate = from_currency['rate'] + ctx.update({'currency_rate_type_id': ctx.get('currency_rate_type_from', None)}) + from_currency = self.browse(cr, uid, from_currency.id, context=ctx) - ctx.update({'currency_rate_type_id': ctx.get('currency_rate_type_to', False)}) - to_currency = self.browse(cr, uid, [to_currency.id], context=ctx)[0] - to_currency_rate = to_currency['rate'] + ctx.update({'currency_rate_type_id': ctx.get('currency_rate_type_to', None)}) + to_currency = self.browse(cr, uid, to_currency.id, context=ctx) - if from_currency_rate == 0 or to_currency_rate == 0: + if from_currency.rate == 0 or to_currency.rate == 0: date = context.get('date', time.strftime('%Y-%m-%d')) - if from_currency_rate == 0: + if from_currency.rate == 0: currency_symbol = from_currency.symbol else: currency_symbol = to_currency.symbol raise osv.except_osv(_('Error'), _('No rate found \n' \ 'for the currency: %s \n' \ 'at the date: %s') % (currency_symbol, date)) - return to_currency_rate/from_currency_rate + return to_currency.rate/from_currency.rate def compute(self, cr, uid, from_currency_id, to_currency_id, from_amount, round=True, currency_rate_type_from=False, currency_rate_type_to=False, context=None): @@ -156,7 +150,7 @@ class res_currency_rate_type(osv.osv): _name = "res.currency.rate.type" _description = "Currency Rate Type" _columns = { - 'name': fields.char('Name', size=32, required=True), + 'name': fields.char('Name', size=64, required=True, translate=True), } res_currency_rate_type() @@ -164,12 +158,13 @@ res_currency_rate_type() class res_currency_rate(osv.osv): _name = "res.currency.rate" _description = "Currency Rate" + _columns = { 'name': fields.date('Date', required=True, select=True), 'rate': fields.float('Rate', digits=(12,6), required=True, help='The rate of the currency to the currency of rate 1'), 'currency_id': fields.many2one('res.currency', 'Currency', readonly=True), - 'currency_rate_type_id': fields.many2one('res.currency.rate.type', 'Currency Rate Type'), + 'currency_rate_type_id': fields.many2one('res.currency.rate.type', 'Currency Rate Type', help="Allow you to define your own currency rate types, like average or Day to Year. Leave empty if you simply want to use the normal 'spot' rate type"), } _defaults = { 'name': lambda *a: time.strftime('%Y-%m-%d'), diff --git a/openerp/addons/base/res/res_currency_view.xml b/openerp/addons/base/res/res_currency_view.xml index ddfee8fa090..4220832606b 100644 --- a/openerp/addons/base/res/res_currency_view.xml +++ b/openerp/addons/base/res/res_currency_view.xml @@ -88,7 +88,17 @@ form tree,form - + + res.currency.rate.type.search + res.currency.rate.type + search + + + + + + + From b8b78c43ddd108bc6baff809c3e5589bd1056b24 Mon Sep 17 00:00:00 2001 From: "Quentin (OpenERP)" Date: Tue, 30 Aug 2011 14:06:44 +0200 Subject: [PATCH 16/20] [IMP] base, res.currency: by default, don't show the currency_rate_type_id field, and don't display its menuitem. Both should be added by specific module using it bzr revid: qdp-launchpad@openerp.com-20110830120644-lwuv3h1vrqjhu32y --- openerp/addons/base/res/res_currency_view.xml | 3 --- 1 file changed, 3 deletions(-) diff --git a/openerp/addons/base/res/res_currency_view.xml b/openerp/addons/base/res/res_currency_view.xml index 4220832606b..24939bd8642 100644 --- a/openerp/addons/base/res/res_currency_view.xml +++ b/openerp/addons/base/res/res_currency_view.xml @@ -48,12 +48,10 @@
- - @@ -99,7 +97,6 @@ - From 17156bd16a2c10f6201ff01040fc8c27763fea6f Mon Sep 17 00:00:00 2001 From: "Quentin (OpenERP)" Date: Tue, 30 Aug 2011 14:07:27 +0200 Subject: [PATCH 17/20] [IMP] base, res.currency: fixes bzr revid: qdp-launchpad@openerp.com-20110830120727-82z1fn0msp7m5096 --- openerp/addons/base/res/res_currency.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openerp/addons/base/res/res_currency.py b/openerp/addons/base/res/res_currency.py index 5333a8eab20..422d40db727 100644 --- a/openerp/addons/base/res/res_currency.py +++ b/openerp/addons/base/res/res_currency.py @@ -131,7 +131,7 @@ class res_currency(osv.osv): xc = self.browse(cr, uid, [from_currency_id,to_currency_id], context=context) from_currency = (xc[0].id == from_currency_id and xc[0]) or xc[1] to_currency = (xc[0].id == to_currency_id and xc[0]) or xc[1] - if to_currency_id == from_currency_id: + if (to_currency_id == from_currency_id) and (currency_rate_type_from == currency_rate_type_to): if round: return self.round(cr, uid, to_currency, from_amount) else: @@ -164,7 +164,7 @@ class res_currency_rate(osv.osv): 'rate': fields.float('Rate', digits=(12,6), required=True, help='The rate of the currency to the currency of rate 1'), 'currency_id': fields.many2one('res.currency', 'Currency', readonly=True), - 'currency_rate_type_id': fields.many2one('res.currency.rate.type', 'Currency Rate Type', help="Allow you to define your own currency rate types, like average or Day to Year. Leave empty if you simply want to use the normal 'spot' rate type"), + 'currency_rate_type_id': fields.many2one('res.currency.rate.type', 'Currency Rate Type', help="Allow you to define your own currency rate types, like 'Average' or 'Year to Date'. Leave empty if you simply want to use the normal 'spot' rate type"), } _defaults = { 'name': lambda *a: time.strftime('%Y-%m-%d'), From 13b2db36315bfb4407b9b53728cb648ae77a085a Mon Sep 17 00:00:00 2001 From: "Quentin (OpenERP)" Date: Tue, 30 Aug 2011 14:27:01 +0200 Subject: [PATCH 18/20] [FIX] base, res.currency: ProgrammingError. When giving a context value like {'currency_rate_type_id': False}, it crashed because the value False was kept instead of None. Thanks Guewen (c2c) for the clue. bzr revid: qdp-launchpad@openerp.com-20110830122701-yrcpo19lokkgrpcc --- openerp/addons/base/res/res_currency.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openerp/addons/base/res/res_currency.py b/openerp/addons/base/res/res_currency.py index 422d40db727..c9cd36276ba 100644 --- a/openerp/addons/base/res/res_currency.py +++ b/openerp/addons/base/res/res_currency.py @@ -36,7 +36,7 @@ class res_currency(osv.osv): else: date = time.strftime('%Y-%m-%d') date = date or time.strftime('%Y-%m-%d') - currency_rate_type = context.get('currency_rate_type_id', None) + currency_rate_type = context.get('currency_rate_type_id') or None operator = currency_rate_type and '=' or 'is' for id in ids: cr.execute("SELECT currency_id, rate FROM res_currency_rate WHERE currency_id = %s AND name <= %s AND currency_rate_type_id " + operator +" %s ORDER BY name desc LIMIT 1" ,(id, date, currency_rate_type)) From 9c6fcf06d2520c48d2382f5e5f80cbb8d44f38b8 Mon Sep 17 00:00:00 2001 From: "Quentin (OpenERP)" Date: Tue, 30 Aug 2011 14:55:21 +0200 Subject: [PATCH 19/20] [IMP] base, res.currency: code cleaning bzr revid: qdp-launchpad@openerp.com-20110830125521-1kt54s56865388ch --- openerp/addons/base/res/res_currency.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openerp/addons/base/res/res_currency.py b/openerp/addons/base/res/res_currency.py index c9cd36276ba..88ee993a5fc 100644 --- a/openerp/addons/base/res/res_currency.py +++ b/openerp/addons/base/res/res_currency.py @@ -70,8 +70,8 @@ class res_currency(osv.osv): _order = "name" def read(self, cr, user, ids, fields=None, context=None, load='_classic_read'): - currency_rate_obj= self.pool.get('res.currency.rate') res = super(osv.osv, self).read(cr, user, ids, fields, context, load) + currency_rate_obj = self.pool.get('res.currency.rate') for r in res: if r.__contains__('rate_ids'): rates=r['rate_ids'] @@ -148,7 +148,7 @@ res_currency() class res_currency_rate_type(osv.osv): _name = "res.currency.rate.type" - _description = "Currency Rate Type" + _description = "Used to define the type of Currency Rates" _columns = { 'name': fields.char('Name', size=64, required=True, translate=True), } From 399de9724c7da178eb8ff4638af469ed118dfa74 Mon Sep 17 00:00:00 2001 From: "Quentin (OpenERP)" Date: Tue, 30 Aug 2011 15:20:35 +0200 Subject: [PATCH 20/20] [IMP] base, res.currency: code cleaning bzr revid: qdp-launchpad@openerp.com-20110830132035-x2gs62lft5o19u6i --- openerp/addons/base/res/res_currency.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/openerp/addons/base/res/res_currency.py b/openerp/addons/base/res/res_currency.py index 88ee993a5fc..ccac26eead9 100644 --- a/openerp/addons/base/res/res_currency.py +++ b/openerp/addons/base/res/res_currency.py @@ -36,8 +36,8 @@ class res_currency(osv.osv): else: date = time.strftime('%Y-%m-%d') date = date or time.strftime('%Y-%m-%d') - currency_rate_type = context.get('currency_rate_type_id') or None - operator = currency_rate_type and '=' or 'is' + currency_rate_type = context.get('currency_rate_type_id') or None #to deal with the case were context['currency_rate_type_id'] == False + operator = '=' if currency_rate_type else 'is' for id in ids: cr.execute("SELECT currency_id, rate FROM res_currency_rate WHERE currency_id = %s AND name <= %s AND currency_rate_type_id " + operator +" %s ORDER BY name desc LIMIT 1" ,(id, date, currency_rate_type)) if cr.rowcount: @@ -103,10 +103,10 @@ class res_currency(osv.osv): if context is None: context = {} ctx = context.copy() - ctx.update({'currency_rate_type_id': ctx.get('currency_rate_type_from', None)}) + ctx.update({'currency_rate_type_id': ctx.get('currency_rate_type_from')}) from_currency = self.browse(cr, uid, from_currency.id, context=ctx) - ctx.update({'currency_rate_type_id': ctx.get('currency_rate_type_to', None)}) + ctx.update({'currency_rate_type_id': ctx.get('currency_rate_type_to')}) to_currency = self.browse(cr, uid, to_currency.id, context=ctx) if from_currency.rate == 0 or to_currency.rate == 0: