diff --git a/CHANGELOG b/CHANGELOG index b5230c7ceb..153cd17026 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Match also with parent categories + Version 4.6.0 - 2017-10-30 * Bug fixes (see mercurial logs for details) * Don't match product and category lines when product is empty diff --git a/commission.py b/commission.py index 81c3818ed6..120c66f3b1 100644 --- a/commission.py +++ b/commission.py @@ -141,10 +141,17 @@ class Plan(ModelSQL, ModelView): def compute(self, amount, product, pattern=None): 'Compute commission amount for the amount' + def parents(categories): + for category in categories: + while category: + yield category + category = category.parent + if pattern is None: pattern = {} if product: - pattern['categories'] = [c.id for c in product.categories_all] + pattern['categories'] = [ + c.id for c in parents(product.categories_all)] pattern['product'] = product.id else: pattern['categories'] = [] diff --git a/tests/test_commission.py b/tests/test_commission.py index fd30747fb7..ef218f6995 100644 --- a/tests/test_commission.py +++ b/tests/test_commission.py @@ -63,6 +63,8 @@ class CommissionTestCase(ModuleTestCase): category = Category(name="Category") category.save() + child_category = Category(name="Child Category", parent=category) + child_category.save() company = create_company() with set_company(company): @@ -83,6 +85,11 @@ class CommissionTestCase(ModuleTestCase): self.assertEqual(plan.compute(Decimal(1), product), Decimal(1)) + template.categories = [child_category] + template.save() + + self.assertEqual(plan.compute(Decimal(1), product), Decimal('0.8')) + @with_transaction() def test_plan_no_product(self): "Test plan with no product"