changeset b654d54f3a52 in modules/product_price_list:default
details: 
https://hg.tryton.org/modules/product_price_list?cmd=changeset;node=b654d54f3a52
description:
        Allow using the product cost price in the price list formula

        issue8467
        review259771002
diffstat:

 CHANGELOG                        |   1 +
 price_list.py                    |  15 ++++++++++++-
 tests/test_product_price_list.py |  41 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 55 insertions(+), 2 deletions(-)

diffs (106 lines):

diff -r be86459448e9 -r b654d54f3a52 CHANGELOG
--- a/CHANGELOG Fri Aug 09 16:36:20 2019 +0200
+++ b/CHANGELOG Mon Aug 19 18:44:32 2019 +0200
@@ -1,3 +1,4 @@
+* Allow using the product cost price in the price list formula
 * Add unit per price list
 * Make price list deactivable
 
diff -r be86459448e9 -r b654d54f3a52 price_list.py
--- a/price_list.py     Fri Aug 09 16:36:20 2019 +0200
+++ b/price_list.py     Mon Aug 19 18:44:32 2019 +0200
@@ -53,9 +53,11 @@
 
     def get_context_formula(self, party, product, unit_price, quantity, uom,
             pattern=None):
+        cost_price = product.get_multivalue('cost_price') or Decimal('0')
         return {
             'names': {
                 'unit_price': unit_price,
+                'cost_price': cost_price,
                 },
             }
 
@@ -111,7 +113,8 @@
         help="Apply only when quantity is greater.")
     formula = fields.Char('Formula', required=True,
         help=('Python expression that will be evaluated with:\n'
-            '- unit_price: the original unit_price'))
+            '- unit_price: the original unit_price\n'
+            '- cost_price: the cost price of the product'))
 
     @staticmethod
     def default_formula():
@@ -123,12 +126,20 @@
         for line in lines:
             line.check_formula()
 
+    @classmethod
+    def get_check_formula_product(cls):
+        pool = Pool()
+        Product = pool.get('product.product')
+        product = Product()
+        product.get_multivalue = lambda name: Decimal('0')
+        return product
+
     def check_formula(self):
         '''
         Check formula
         '''
         context = self.price_list.get_context_formula(
-            None, None, Decimal('0'), 0, None)
+            None, self.get_check_formula_product(), Decimal('0'), 0, None)
 
         try:
             if not isinstance(self.get_unit_price(**context), Decimal):
diff -r be86459448e9 -r b654d54f3a52 tests/test_product_price_list.py
--- a/tests/test_product_price_list.py  Fri Aug 09 16:36:20 2019 +0200
+++ b/tests/test_product_price_list.py  Mon Aug 19 18:44:32 2019 +0200
@@ -136,6 +136,47 @@
                 price_list.compute(None, product, product.list_price, 1, unit),
                 Decimal(8))
 
+    @with_transaction()
+    def test_price_list_cost_price(self):
+        "Test price list with cost_price formula"
+        pool = Pool()
+        Template = pool.get('product.template')
+        Product = pool.get('product.product')
+        Uom = pool.get('product.uom')
+        PriceList = pool.get('product.price_list')
+
+        unit, = Uom.search([('name', '=', 'Unit')])
+
+        company = create_company()
+        with set_company(company):
+            template = Template(
+                name="Template",
+                list_price=Decimal(10),
+                default_uom=unit,
+                products=None,
+                )
+            template.save()
+            product = Product(template=template)
+            product.save()
+
+            price_list, = PriceList.create([{
+                        'name': "Price List",
+                        'lines': [('create', [{
+                                        'formula': 'cost_price * 1.2',
+                                        }])],
+                        }])
+
+            self.assertEqual(
+                price_list.compute(None, product, product.list_price, 1, unit),
+                Decimal(0))
+
+            product.cost_price = Decimal(5)
+            product.save()
+
+            self.assertEqual(
+                price_list.compute(None, product, product.list_price, 1, unit),
+                Decimal(6))
+
 
 def suite():
     suite = trytond.tests.test_tryton.suite()

Reply via email to