From 20edf21fdada5be97df7214cfbe1b53900915f5b Mon Sep 17 00:00:00 2001
From: Victor Feyens <vfe@odoo.com>
Date: Tue, 12 Nov 2019 13:26:25 +0000
Subject: [PATCH] [FIX] orm: do not try to convert zero amount for monetary
 fields.

When assigning a default value to a computed monetary field, if self
contains records with different currencies, the assignation will fail
when trying to round the value (because self[currency_field] is a
recordset of multiple currencies).

Globally, assigning a monetary value to records in different currencies
should never happen.  The only case we want to support is defaulting a
computed monetary to 0.0.

Add clear error when trying to set monetaries in multi-currency.

closes odoo/odoo#40155

X-original-commit: 3a58dfa1e51702a659e9113271f19d087e3ec349
Signed-off-by: Victor Feyens (vfe) <vfe@odoo.com>
Co-authored-by: Raphael Collet <rco@odoo.com>
---
 odoo/fields.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/odoo/fields.py b/odoo/fields.py
index b4f2b126acc8..f94676b43031 100644
--- a/odoo/fields.py
+++ b/odoo/fields.py
@@ -1301,10 +1301,14 @@ class Monetary(Field):
     def convert_to_cache(self, value, record, validate=True):
         # cache format: float
         value = float(value or 0.0)
-        if validate and record.sudo()[self.currency_field]:
+        if value and validate:
             # FIXME @rco-odoo: currency may not be already initialized if it is
             # a function or related field!
-            value = record[self.currency_field].round(value)
+            currency = record.sudo()[self.currency_field]
+            if len(currency) > 1:
+                raise ValueError("Got multiple currencies while assigning values of monetary field %s" % str(self))
+            elif currency:
+                value = currency.round(value)
         return value
 
     def convert_to_record(self, value, record):
-- 
GitLab