diff --git a/CHANGELOG b/CHANGELOG index 7ca6ca6291..18e26558ac 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Add category as plan matching criteria + Version 4.2.0 - 2016-11-28 * Bug fixes (see mercurial logs for details) diff --git a/commission.py b/commission.py index a3067fc353..1624aed4dc 100644 --- a/commission.py +++ b/commission.py @@ -143,7 +143,9 @@ class Plan(ModelSQL, ModelView): 'Compute commission amount for the amount' if pattern is None: pattern = {} - pattern['product'] = product.id if product else None + if product: + pattern['categories'] = [c.id for c in product.categories] + pattern['product'] = product.id context = self.get_context_formula(amount, product) for line in self.lines: if line.match(pattern): @@ -155,6 +157,8 @@ class PlanLines(sequence_ordered(), ModelSQL, ModelView, MatchMixin): __name__ = 'commission.plan.line' plan = fields.Many2One('commission.plan', 'Plan', required=True, ondelete='CASCADE') + category = fields.Many2One( + 'product.category', "Category", ondelete='CASCADE') product = fields.Many2One('product.product', 'Product', ondelete='CASCADE') formula = fields.Char('Formula', required=True, help=('Python expression that will be evaluated with:\n' @@ -197,6 +201,15 @@ class PlanLines(sequence_ordered(), ModelSQL, ModelView, MatchMixin): context.setdefault('functions', {})['Decimal'] = Decimal return simple_eval(decistmt(self.formula), **context) + def match(self, pattern): + 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(PlanLines, self).match(pattern) + class Commission(ModelSQL, ModelView): 'Commission' diff --git a/tests/test_commission.py b/tests/test_commission.py index 2b6f4048f9..30dda03ccc 100644 --- a/tests/test_commission.py +++ b/tests/test_commission.py @@ -4,17 +4,79 @@ from __future__ import unicode_literals import unittest import doctest +from decimal import Decimal -from trytond.tests.test_tryton import ModuleTestCase +from trytond.tests.test_tryton import ModuleTestCase, with_transaction from trytond.tests.test_tryton import suite as test_suite from trytond.tests.test_tryton import doctest_teardown from trytond.tests.test_tryton import doctest_checker +from trytond.pool import Pool + +from trytond.modules.company.tests import create_company, set_company class CommissionTestCase(ModuleTestCase): 'Test Commission module' module = 'commission' + @with_transaction() + def test_plan_category(self): + "Test plan with category" + pool = Pool() + Category = pool.get('product.category') + Template = pool.get('product.template') + Product = pool.get('product.product') + Uom = pool.get('product.uom') + Plan = pool.get('commission.plan') + + category = Category(name="Category") + category.save() + + unit, = Uom.search([('name', '=', 'Unit')]) + + company = create_company() + with set_company(company): + commission_template = Template( + name="Commission", + type='service', + list_price=Decimal(10), + cost_price=Decimal(3), + default_uom=unit, + products=None, + ) + commission_template.save() + commission_product = Product(template=commission_template) + commission_product.save() + 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() + + plan, = Plan.create([{ + 'name': "Commission Plan", + 'commission_product': commission_product.id, + 'lines': [('create', [{ + 'category': category.id, + 'formula': 'amount * 0.8', + }, { + 'formula': 'amount', + }])], + }]) + + self.assertEqual(plan.compute(Decimal(1), product), Decimal('0.8')) + + template.categories = [] + template.save() + + self.assertEqual(plan.compute(Decimal(1), product), Decimal(1)) + def suite(): suite = test_suite() diff --git a/view/plan_line_form.xml b/view/plan_line_form.xml index fdf56f7787..2399860c98 100644 --- a/view/plan_line_form.xml +++ b/view/plan_line_form.xml @@ -4,12 +4,13 @@ this repository contains the full copyright notices and license terms. -->
diff --git a/view/plan_line_list.xml b/view/plan_line_list.xml index e63c2865a0..5a7dd1d05c 100644 --- a/view/plan_line_list.xml +++ b/view/plan_line_list.xml @@ -4,6 +4,7 @@ this repository contains the full copyright notices and license terms. -->