changeset ca49a47fefe0 in modules/product_price_list:default
details: 
https://hg.tryton.org/modules/product_price_list?cmd=changeset&node=ca49a47fefe0
description:
        Support empty list price on product

        issue10244
        review350341002
diffstat:

 __init__.py                      |   3 +++
 price_list.py                    |  35 +++++++++++++++++++++++++++++++----
 tests/test_product_price_list.py |  21 +++++++++++++++++++++
 3 files changed, 55 insertions(+), 4 deletions(-)

diffs (110 lines):

diff -r 8691c72e49e1 -r ca49a47fefe0 __init__.py
--- a/__init__.py       Sun Jul 04 17:54:47 2021 +0200
+++ b/__init__.py       Mon Sep 13 23:29:56 2021 +0200
@@ -2,6 +2,9 @@
 # this repository contains the full copyright notices and license terms.
 from trytond.pool import Pool
 from . import price_list
+from .price_list import Null
+
+__all__ = ['register', 'Null']
 
 
 def register():
diff -r 8691c72e49e1 -r ca49a47fefe0 price_list.py
--- a/price_list.py     Sun Jul 04 17:54:47 2021 +0200
+++ b/price_list.py     Mon Sep 13 23:29:56 2021 +0200
@@ -1,6 +1,7 @@
 # This file is part of Tryton.  The COPYRIGHT file at the top level of
 # this repository contains the full copyright notices and license terms.
 from decimal import Decimal
+from functools import total_ordering
 
 from simpleeval import simple_eval
 
@@ -14,6 +15,29 @@
 from .exceptions import FormulaError
 
 
+@total_ordering
+class Null:
+    def __eq__(self, other):
+        if isinstance(other, Null) or other is None:
+            return True
+        return False
+
+    def __lt__(self, other):
+        return 0 < other
+
+
+def _return_self(self, *args, **kwargs):
+    return self
+
+
+_OPERATORS = (
+    'add sub mul matmul truediv floordiv mod divmod pow lshift rshift and xor '
+    'or'.split())
+for op in _OPERATORS:
+    setattr(Null, '__%s__' % op, _return_self)
+    setattr(Null, '__r%s__' % op, _return_self)
+
+
 class PriceList(DeactivableMixin, ModelSQL, ModelView):
     'Price List'
     __name__ = 'product.price_list'
@@ -57,9 +81,9 @@
             list_price = unit_price
         return {
             'names': {
-                'unit_price': unit_price,
-                'cost_price': cost_price,
-                'list_price': list_price,
+                'unit_price': unit_price if unit_price is not None else Null(),
+                'cost_price': cost_price if unit_price is not None else Null(),
+                'list_price': list_price if unit_price is not None else Null(),
                 },
             }
 
@@ -93,7 +117,10 @@
             party, product, unit_price, quantity, uom, pattern=pattern)
         for line in self.lines:
             if line.match(pattern):
-                return line.get_unit_price(**context)
+                unit_price = line.get_unit_price(**context)
+                if isinstance(unit_price, Null):
+                    unit_price = None
+                break
         return unit_price
 
 
diff -r 8691c72e49e1 -r ca49a47fefe0 tests/test_product_price_list.py
--- a/tests/test_product_price_list.py  Sun Jul 04 17:54:47 2021 +0200
+++ b/tests/test_product_price_list.py  Mon Sep 13 23:29:56 2021 +0200
@@ -212,6 +212,27 @@
                 price_list.compute(None, product, Decimal(0), 1, unit),
                 Decimal(8))
 
+    @with_transaction()
+    def test_price_list_with_none(self):
+        "Test price list without price"
+        pool = Pool()
+        PriceList = pool.get('product.price_list')
+        Uom = pool.get('product.uom')
+
+        unit, = Uom.search([('name', '=', 'Unit')])
+
+        company = create_company()
+        with set_company(company):
+            price_list, = PriceList.create([{
+                        'name': "Price List",
+                        'lines': [('create', [{
+                                        'formula': 'list_price * 0.8',
+                                        }])],
+                        }])
+            self.assertEqual(
+                price_list.compute(None, None, None, 1, unit),
+                None)
+
 
 def suite():
     suite = trytond.tests.test_tryton.suite()

Reply via email to