[FIX] base: fix error message when creating new currency rate

When creating a new currency from the list view, the currency was given 1.0
as rate since this was the field's default value.
The "onchange" on rate would then trigger. If 1.0 is more than 20% different
than the latest rate, a warning was popped. This warning prevented the creation of the new rate.
Meaning one could not create a new currency rate if the latest rate > 1.25 or < 0.833.

This is fixed by setting the latest rate as default value.

closes odoo/odoo#66798

Task: 2450747
X-original-commit: 02f241a35cf3a758d7759c394eb8642375dee5df
Signed-off-by: Laurent Smet <smetl@users.noreply.github.com>
Signed-off-by: jbw-odoo <jbw-odoo@users.noreply.github.com>
This commit is contained in:
jbw
2021-02-25 09:35:43 +00:00
parent 951145dd1a
commit bda98d8418
+16 -6
View File
@@ -237,7 +237,8 @@ class CurrencyRate(models.Model):
name = fields.Date(string='Date', required=True, index=True,
default=lambda self: fields.Date.today())
rate = fields.Float(digits=0, default=1.0, group_operator="avg", help='The rate of the currency to the currency of rate 1')
rate = fields.Float(digits=0, compute="_compute_rate", store=True, readonly=False, group_operator="avg",
help='The rate of the currency to the currency of rate 1')
currency_id = fields.Many2one('res.currency', string='Currency', readonly=True, required=True, ondelete="cascade")
company_id = fields.Many2one('res.company', string='Company',
default=lambda self: self.env.company)
@@ -247,16 +248,25 @@ class CurrencyRate(models.Model):
('currency_rate_check', 'CHECK (rate>0)', 'The currency rate must be strictly positive.'),
]
def _get_latest_rate(self):
return self.search([
('currency_id', '=', self.currency_id.id),
('company_id', '=', self.company_id.id or self.env.company.id),
('name', '<=', self.name or fields.Date.today()),
], order="name desc", limit=1)
@api.depends('currency_id', 'company_id', 'name')
def _compute_rate(self):
for currency_rate in self:
latest_rate = currency_rate._get_latest_rate()
currency_rate.rate = latest_rate.rate if latest_rate else 1.0
@api.onchange('rate')
def _onchange_rate_warning(self):
if not self.currency_id.id:
return
latest_rate = self.search([
('currency_id', '=', self.currency_id.id),
('company_id', '=', self.company_id.id or self.env.company.id),
('name', '<=', self.name or fields.Date.today()),
], order="name desc", limit=1)
latest_rate = self._get_latest_rate()
if latest_rate and latest_rate.rate:
diff = (latest_rate.rate - self.rate) / latest_rate.rate
if abs(diff) > 0.2: