From c128064b714dd6e18f498c780910e5d615374d66 Mon Sep 17 00:00:00 2001 From: Raimon Esteve Date: Thu, 24 Aug 2023 10:32:23 +0200 Subject: [PATCH] issue12216.diff [stock] Handle evaluation error of cost price in cost price revision --- issue12216.diff | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ series | 2 ++ 2 files changed, 69 insertions(+) create mode 100644 issue12216.diff diff --git a/issue12216.diff b/issue12216.diff new file mode 100644 index 0000000..a3fc462 --- /dev/null +++ b/issue12216.diff @@ -0,0 +1,67 @@ +diff --git a/tryton/modules/stock/message.xml b/tryton/modules/stock/message.xml +index ac05419240..dcae4e4632 100644 +--- a/tryton/modules/stock/message.xml ++++ b/tryton/modules/stock/message.xml +@@ -16,6 +16,9 @@ You must use the "Modify Cost Price" wizard. + + Invalid cost price "%(cost_price)s" for product "%(product)s" with exception "%(exception)s". + ++ ++ The value "%(value)s" of "%(cost_price)s" for product "%(product)s" is not a number. ++ + + You cannot change the type of location "%(location)s" to "%(type)s" + because the type does not support moves and location has existing moves. +diff --git a/tryton/modules/stock/product.py b/tryton/modules/stock/product.py +index e4da113ef3..96a9fac580 100644 +--- a/tryton/modules/stock/product.py ++++ b/tryton/modules/stock/product.py +@@ -6,7 +6,7 @@ from collections import defaultdict + from copy import deepcopy + from decimal import Decimal + +-from simpleeval import simple_eval ++from simpleeval import InvalidExpression, simple_eval + from sql import Literal, Select, Window, With + from sql.aggregate import Max, Sum + from sql.conditionals import Case, Coalesce +@@ -1233,22 +1233,27 @@ class CostPriceRevision(ModelSQL, ModifyCostPriceStart): + if field_names and 'cost_price' not in field_names: + return + for revision in revisions: +- try: +- if not isinstance( +- revision.get_cost_price(Decimal(0)), Decimal): +- raise ValueError +- except Exception as exception: +- product = revision.product or revision.template +- raise ProductCostPriceError( +- gettext('stock.msg_invalid_cost_price', +- cost_price=revision.cost_price, +- product=product.rec_name, +- exception=exception)) from exception ++ revision.get_cost_price(Decimal(0)) + + def get_cost_price(self, cost_price, **context): + context.setdefault('names', {})['cost_price'] = cost_price + context.setdefault('functions', {})['Decimal'] = Decimal +- return simple_eval(decistmt(self.cost_price), **context) ++ try: ++ amount = simple_eval(decistmt(self.cost_price), **context) ++ except (InvalidExpression, SyntaxError) as exception: ++ product = self.product or self.template ++ raise ProductCostPriceError( ++ gettext('stock.msg_invalid_cost_price', ++ cost_price=self.cost_price, ++ product=product.rec_name, ++ exception=exception)) from exception ++ if not isinstance(amount, Decimal): ++ raise ProductCostPriceError( ++ gettext('stock.msg_invalid_cost_price_not_number', ++ value=amount, ++ cost_price=self.cost_price, ++ product=product.rec_name)) ++ return amount + + @classmethod + def _get_for_product_domain(cls): diff --git a/series b/series index d729a05..ccdda05 100644 --- a/series +++ b/series @@ -45,3 +45,5 @@ issue12398.diff # [account_dunning] Missing searc_rec_name in model 'account.dun issue12414.diff # [stock_lot_sled] Check expiration lot in case Shelf Life Time State is not "none" merge_request581.diff # [account] Post cancelled, grouped, rescheduled and delegated moves + +issue12216.diff # [stock] Handle evaluation error of cost price in cost price revision