1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
24 from osv import fields, osv
27 from tools.misc import currency
28 from tools.translate import _
31 from mx.DateTime import RelativeDateTime, now, DateTime, localtime
33 class res_currency(osv.osv):
34 def _current_rate(self, cr, uid, ids, name, arg, context={}):
39 date=time.strftime('%Y-%m-%d')
40 date= date or time.strftime('%Y-%m-%d')
42 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))
44 id, rate=cr.fetchall()[0]
49 _name = "res.currency"
50 _description = "Currency"
52 'name': fields.char('Currency', size=32, required=True),
53 'code': fields.char('Code', size=3),
54 'rate': fields.function(_current_rate, method=True, string='Current rate', digits=(12,6),
55 help='The rate of the currency to the currency of rate 1'),
56 'rate_ids': fields.one2many('res.currency.rate', 'currency_id', 'Rates'),
57 'accuracy': fields.integer('Computational Accuracy'),
58 'rounding': fields.float('Rounding factor', digits=(12,6)),
59 'active': fields.boolean('Active'),
62 'active': lambda *a: 1,
66 def round(self, cr, uid, currency, amount):
67 if currency.rounding == 0:
70 return round(amount / currency.rounding) * currency.rounding
72 def is_zero(self, cr, uid, currency, amount):
73 return abs(self.round(cr, uid, currency, amount)) < currency.rounding
75 def compute(self, cr, uid, from_currency_id, to_currency_id, from_amount, round=True, context={}, account=None, account_invert=False):
76 if not from_currency_id:
77 from_currency_id = to_currency_id
78 xc=self.browse(cr, uid, [from_currency_id,to_currency_id], context=context)
79 from_currency = (xc[0].id == from_currency_id and xc[0]) or xc[1]
80 to_currency = (xc[0].id == to_currency_id and xc[0]) or xc[1]
81 if from_currency['rate'] == 0 or to_currency['rate'] == 0:
82 date = context.get('date', time.strftime('%Y-%m-%d'))
83 if from_currency['rate'] == 0:
84 code = from_currency.code
86 code = to_currency.code
87 raise osv.except_osv(_('Error'), _('No rate found \n' \
88 'for the currency: %s \n' \
89 'at the date: %s') % (code, date))
90 rate = to_currency.rate/from_currency.rate
91 if account and (account.currency_mode=='average') and account.currency_id:
92 q = self.pool.get('account.move.line')._query_get(cr, uid, context=context)
93 cr.execute('select sum(debit-credit),sum(amount_currency) from account_move_line l ' \
94 'where l.currency_id=%s and l.account_id=%s and '+q, (account.currency_id.id,account.id,))
95 tot1,tot2 = cr.fetchone()
96 if tot2 and not account_invert:
97 rate = float(tot1)/float(tot2)
98 elif tot1 and account_invert:
99 rate = float(tot2)/float(tot1)
100 if to_currency_id==from_currency_id:
102 return self.round(cr, uid, to_currency, from_amount)
107 return self.round(cr, uid, to_currency, from_amount * rate)
109 return (from_amount * rate)
111 def name_search(self, cr, uid, name, args=[], operator='ilike', context={}, limit=80):
114 args += [('name', operator, name)]
115 args2 += [('code', operator, name)]
116 ids = self.search(cr, uid, args, limit=limit)
117 ids += self.search(cr, uid, args2, limit=limit)
118 res = self.name_get(cr, uid, ids, context)
122 class res_currency_rate(osv.osv):
123 _name = "res.currency.rate"
124 _description = "Currency Rate"
126 'name': fields.date('Date', required=True, select=True),
127 'rate': fields.float('Rate', digits=(12,6), required=True,
128 help='The rate of the currency to the currency of rate 1'),
129 'currency_id': fields.many2one('res.currency', 'Currency', readonly=True),
132 'name': lambda *a: time.strftime('%Y-%m-%d'),
137 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: