Enforce strictly positive currency rate

This commit is contained in:
Cédric Krier committed 2024-10-14 15:18:19 +02:00
1 parent d0efae47b5
commit 745099113f
3 files changed
+14 -9

No files matched your search

+14 -5
View File
@@ -11,8 +11,8 @@ from sql.functions import NthValue
from trytond.i18n import gettext
from trytond.model import (
Check, DeactivableMixin, DigitsMixin, Index, ModelSQL, ModelView,
SymbolMixin, Unique, fields)
DeactivableMixin, DigitsMixin, Index, ModelSQL, ModelView, SymbolMixin,
Unique, fields)
from trytond.pool import Pool
from trytond.pyson import Eval, If
from trytond.rpc import RPC
@@ -274,7 +274,10 @@ class CurrencyRate(ModelSQL, ModelView):
"Date", required=True,
help="From when the rate applies.")
rate = fields.Numeric(
"Rate", digits=(rate_decimal * 2, rate_decimal), required=1,
"Rate", digits=(rate_decimal * 2, rate_decimal), required=True,
domain=[
('rate', '>', 0),
],
help="The floating exchange rate used to convert the currency.")
currency = fields.Many2One('currency.currency', 'Currency',
ondelete='CASCADE',
@@ -288,8 +291,6 @@ class CurrencyRate(ModelSQL, ModelView):
cls._sql_constraints = [
('date_currency_uniq', Unique(t, t.date, t.currency),
'currency.msg_currency_unique_rate_date'),
('check_currency_rate', Check(t, t.rate >= 0),
'currency.msg_currency_rate_positive'),
]
cls._sql_indexes.add(
Index(
@@ -299,6 +300,14 @@ class CurrencyRate(ModelSQL, ModelView):
order='DESC'))
cls._order.insert(0, ('date', 'DESC'))
@classmethod
def __register__(cls, module):
table_h = cls.__table_handler__(module)
super().__register__(module)
# Migration from 7.2: remove check_currency_rate
table_h.drop_constraint('check_currency_rate')
@staticmethod
def default_date():
Date = Pool().get('ir.date')
-3
View File
@@ -9,8 +9,5 @@ this repository contains the full copyright notices and license terms. -->
<record model="ir.message" id="msg_currency_unique_rate_date">
<field name="text">A currency can only have one rate by date.</field>
</record>
<record model="ir.message" id="msg_currency_rate_positive">
<field name="text">A currency rate must be positive.</field>
</record>
</data>
</tryton>
-1
View File
@@ -244,7 +244,6 @@ class CurrencyTestCase(ModuleTestCase):
cu1 = create_currency('cu1')
cu2 = create_currency('cu2')
add_currency_rate(cu1, Decimal('0'))
add_currency_rate(cu2, Decimal('1'))
amount = Decimal("10")