diff --git a/CHANGELOG b/CHANGELOG index aef8a57d1b..0c9575f026 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Add category as matching criteria + Version 4.2.0 - 2016-11-28 * Bug fixes (see mercurial logs for details) diff --git a/price_list.py b/price_list.py index 9457776d68..dd00cb38be 100644 --- a/price_list.py +++ b/price_list.py @@ -53,7 +53,9 @@ class PriceList(ModelSQL, ModelView): pattern = {} pattern = pattern.copy() - pattern['product'] = product and product.id or None + if product: + pattern['categories'] = [c.id for c in product.categories] + pattern['product'] = product.id pattern['quantity'] = Uom.compute_qty(uom, quantity, product.default_uom, round=False) if product else quantity @@ -71,9 +73,12 @@ class PriceListLine(sequence_ordered(), ModelSQL, ModelView, MatchMixin): price_list = fields.Many2One('product.price_list', 'Price List', required=True, ondelete='CASCADE') + category = fields.Many2One( + 'product.category', "Category", ondelete='CASCADE') product = fields.Many2One('product.product', 'Product', ondelete='CASCADE') quantity = fields.Float('Quantity', digits=(16, Eval('unit_digits', 2)), - depends=['unit_digits']) + depends=['unit_digits'], + help="Minimal quantity in the product default UoM.") unit_digits = fields.Function(fields.Integer('Unit Digits'), 'on_change_with_unit_digits') formula = fields.Char('Formula', required=True, @@ -107,7 +112,6 @@ class PriceListLine(sequence_ordered(), ModelSQL, ModelView, MatchMixin): def on_change_with_unit_digits(self, name=None): if self.product: return self.product.default_uom.digits - return 2 @classmethod def validate(cls, lines): @@ -138,6 +142,12 @@ class PriceListLine(sequence_ordered(), ModelSQL, ModelView, MatchMixin): quantity = pattern.pop('quantity') if self.quantity is not None and self.quantity > quantity: return False + if 'categories' in pattern: + pattern = pattern.copy() + categories = pattern.pop('categories') + if (self.category is not None + and self.category.id not in categories): + return False return super(PriceListLine, self).match(pattern) def get_unit_price(self, **context): diff --git a/tests/test_product_price_list.py b/tests/test_product_price_list.py index 7c8d68f3bf..30ba11ecf0 100644 --- a/tests/test_product_price_list.py +++ b/tests/test_product_price_list.py @@ -78,6 +78,56 @@ class ProductPriceListTestCase(ModuleTestCase): quantity, unit), result) + @with_transaction() + def test_price_list_category(self): + "Test price list with category" + pool = Pool() + Category = pool.get('product.category') + Template = pool.get('product.template') + Product = pool.get('product.product') + Uom = pool.get('product.uom') + PriceList = pool.get('product.price_list') + + category = Category(name="Category") + category.save() + + unit, = Uom.search([('name', '=', 'Unit')]) + + company = create_company() + with set_company(company): + template = Template( + name="Template", + list_price=Decimal(10), + cost_price=Decimal(3), + default_uom=unit, + products=None, + categories=[category], + ) + template.save() + product = Product(template=template) + product.save() + + price_list, = PriceList.create([{ + 'name': "Price List", + 'lines': [('create', [{ + 'category': category.id, + 'formula': 'unit_price * 0.8', + }, { + 'formula': 'unit_price', + }])], + }]) + + self.assertEqual( + price_list.compute(None, product, product.list_price, 1, unit), + Decimal(8)) + + template.categories = [] + template.save() + + self.assertEqual( + price_list.compute(None, product, product.list_price, 1, unit), + Decimal(10)) + def suite(): suite = trytond.tests.test_tryton.suite() diff --git a/view/price_list_line_form.xml b/view/price_list_line_form.xml index 5f163a63ae..ce524ce491 100644 --- a/view/price_list_line_form.xml +++ b/view/price_list_line_form.xml @@ -4,14 +4,15 @@ this repository contains the full copyright notices and license terms. -->
diff --git a/view/price_list_line_tree.xml b/view/price_list_line_tree.xml index 4bef5026d2..1a9111f8c9 100644 --- a/view/price_list_line_tree.xml +++ b/view/price_list_line_tree.xml @@ -4,6 +4,7 @@ this repository contains the full copyright notices and license terms. -->