diff --git a/CHANGELOG b/CHANGELOG index 0b0376296f..f607754b65 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 --git a/price_list.py b/price_list.py index d992347e72..a7e8ca432a 100644 --- a/price_list.py +++ b/price_list.py @@ -53,9 +53,11 @@ class PriceList(DeactivableMixin, ModelSQL, ModelView): 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 @@ class PriceListLine(sequence_ordered(), ModelSQL, ModelView, MatchMixin): 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 @@ class PriceListLine(sequence_ordered(), ModelSQL, ModelView, MatchMixin): 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 --git a/tests/test_product_price_list.py b/tests/test_product_price_list.py index f83a2bff11..798dde9a34 100644 --- a/tests/test_product_price_list.py +++ b/tests/test_product_price_list.py @@ -136,6 +136,47 @@ class ProductPriceListTestCase(ModuleTestCase): 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()