mirror of
https://github.com/OCA/account-financial-tools.git
synced 2025-02-02 12:47:26 +02:00
- Extension of translation file should be .pot
- better code for the constraint on the field currency_rate_max_delta - coding style enhancements suggested by the reviewers
This commit is contained in:
@@ -20,6 +20,6 @@
|
||||
#
|
||||
##############################################################################
|
||||
|
||||
import company
|
||||
import currency_rate_date_check
|
||||
from . import company
|
||||
from . import currency_rate_date_check
|
||||
|
||||
|
||||
@@ -20,26 +20,23 @@
|
||||
#
|
||||
##############################################################################
|
||||
|
||||
from openerp.osv import osv, fields
|
||||
from openerp.osv import orm, fields
|
||||
|
||||
class res_company(osv.Model):
|
||||
class res_company(orm.Model):
|
||||
_inherit = 'res.company'
|
||||
|
||||
_columns = {
|
||||
'currency_rate_max_delta': fields.integer('Max Time Delta in Days for Currency Rates', help="This is the maximum interval in days between the date associated with the amount to convert and the date of the nearest currency rate available in OpenERP."),
|
||||
'currency_rate_max_delta': fields.integer(
|
||||
'Max Time Delta in Days for Currency Rates',
|
||||
help="This is the maximum interval in days between the date associated with the amount to convert and the date of the nearest currency rate available in OpenERP."),
|
||||
}
|
||||
|
||||
_defaults = {
|
||||
'currency_rate_max_delta': 7,
|
||||
}
|
||||
|
||||
def _check_currency_rate_max_delta(self, cr, uid, ids):
|
||||
for company in self.read(cr, uid, ids, ['currency_rate_max_delta']):
|
||||
if company['currency_rate_max_delta'] < 0:
|
||||
return False
|
||||
return True
|
||||
|
||||
_constraints = [
|
||||
(_check_currency_rate_max_delta, "The value must be positive or 0", ['currency_rate_max_delta']),
|
||||
]
|
||||
|
||||
_sql_constraints = [
|
||||
('currency_rate_max_delta_positive',
|
||||
'CHECK (currency_rate_max_delta >= 0)',
|
||||
"The value of the field 'Max Time Delta in Days for Currency Rates' must be positive or 0."),
|
||||
]
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
#
|
||||
##############################################################################
|
||||
|
||||
from openerp.osv import osv, fields
|
||||
from openerp.osv import fields, orm
|
||||
from datetime import datetime, timedelta
|
||||
from openerp.tools.translate import _
|
||||
|
||||
@@ -41,13 +41,14 @@ from openerp.tools.translate import _
|
||||
# => that's the solution I implement in the code below
|
||||
|
||||
|
||||
class res_currency(osv.Model):
|
||||
class res_currency(orm.Model):
|
||||
_inherit = 'res.currency'
|
||||
|
||||
def _current_rate_computation(self, cr, uid, ids, name, arg, raise_on_no_rate, context=None):
|
||||
if context is None: context = {}
|
||||
# We only do the check if there is an explicit date in the context and
|
||||
# there is no specific currency_rate_type_id
|
||||
if context and context.get('date') and not context.get('currency_rate_type_id') and not context.get('disable_rate_date_check'):
|
||||
if context.get('date') and not context.get('currency_rate_type_id') and not context.get('disable_rate_date_check'):
|
||||
for currency_id in ids:
|
||||
# We could get the company from the currency, but it's not a
|
||||
# 'required' field, so we should probably continue to get it from
|
||||
@@ -61,7 +62,6 @@ class res_currency(osv.Model):
|
||||
# now we do the real work !
|
||||
date = context.get('date', datetime.today().strftime('%Y-%m-%d'))
|
||||
date_datetime = datetime.strptime(date, '%Y-%m-%d')
|
||||
#print "date =", date
|
||||
rate_obj = self.pool['res.currency.rate']
|
||||
selected_rate = rate_obj.search(cr, uid, [
|
||||
('currency_id', '=', currency_id),
|
||||
@@ -74,11 +74,9 @@ class res_currency(osv.Model):
|
||||
rate_date = rate_obj.read(cr, uid, selected_rate[0], ['name'], context=context)['name']
|
||||
rate_date_datetime = datetime.strptime(rate_date, '%Y-%m-%d')
|
||||
max_delta = user.company_id.currency_rate_max_delta
|
||||
#print "max_delta=", max_delta
|
||||
#print "rate_date=", rate_date
|
||||
if (date_datetime - rate_date_datetime).days > max_delta:
|
||||
currency_name = self.read(cr, uid, currency_id, ['name'], context=context)['name']
|
||||
raise osv.except_osv(_('Error'), _('You are requesting a rate conversion on %s for currency %s but the nearest rate before that date is dated %s and the maximum currency rate time delta for your company is %s days') % (date, currency_name, rate_date, max_delta))
|
||||
raise orm.except_orm(_('Error'), _('You are requesting a rate conversion on %s for currency %s but the nearest rate before that date is dated %s and the maximum currency rate time delta for your company is %s days') % (date, currency_name, rate_date, max_delta))
|
||||
# Now we call the regular function from the "base" module
|
||||
return super(res_currency, self)._current_rate_computation(cr, uid, ids, name, arg, raise_on_no_rate, context=context)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user