c68d0ceef127b83c0d3c650f4369305c63fa1c74
[odoo/odoo.git] / bin / addons / base / res / res_currency.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
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.
12 #
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.
17 #
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/>.
20 #
21 ##############################################################################
22 import time
23 import netsvc
24 from osv import fields, osv
25 import ir
26
27 from tools.misc import currency
28 from tools.translate import _
29
30 import mx.DateTime
31 from mx.DateTime import RelativeDateTime, now, DateTime, localtime
32
33 class res_currency(osv.osv):
34     def _current_rate(self, cr, uid, ids, name, arg, context={}):
35         res={}
36         if 'date' in context:
37             date=context['date']
38         else:
39             date=time.strftime('%Y-%m-%d')
40         date= date or time.strftime('%Y-%m-%d')
41         for id in ids:
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))
43             if cr.rowcount:
44                 id, rate=cr.fetchall()[0]
45                 res[id]=rate
46             else:
47                 res[id]=0
48         return res
49     _name = "res.currency"
50     _description = "Currency"
51     _columns = {
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'),
60     }
61     _defaults = {
62         'active': lambda *a: 1,
63     }
64     _order = "code"
65
66     def round(self, cr, uid, currency, amount):
67         if currency.rounding == 0:
68             return 0.0
69         else:
70             return round(amount / currency.rounding) * currency.rounding
71
72     def is_zero(self, cr, uid, currency, amount):
73         return abs(self.round(cr, uid, currency, amount)) < currency.rounding
74
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
85             else:
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:
101             if round:
102                 return self.round(cr, uid, to_currency, from_amount)
103             else:
104                 return from_amount
105         else:
106             if round:
107                 return self.round(cr, uid, to_currency, from_amount * rate)
108             else:
109                 return (from_amount * rate)
110
111     def name_search(self, cr, uid, name, args=[], operator='ilike', context={}, limit=80):
112         args2 = args[:]
113         if name:
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)
119         return res
120 res_currency()
121
122 class res_currency_rate(osv.osv):
123     _name = "res.currency.rate"
124     _description = "Currency Rate"
125     _columns = {
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),
130     }
131     _defaults = {
132         'name': lambda *a: time.strftime('%Y-%m-%d'),
133     }
134     _order = "name desc"
135 res_currency_rate()
136
137 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
138