Add category as plan matching criteria

issue6322
review33161002
This commit is contained in:
Sergi Almacellas Abellana committed 2017-03-17 10:08:45 +01:00
1 parent ca5e18c0ac
commit bb3b2c7d86
6 files changed
+86 -6

No files matched your search

+2
View File
@@ -1,3 +1,5 @@
* Add category as plan matching criteria
Version 4.2.0 - 2016-11-28
* Bug fixes (see mercurial logs for details)
+14 -1
View File
@@ -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'
+63 -1
View File
@@ -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()
+5 -4
View File
@@ -4,12 +4,13 @@ this repository contains the full copyright notices and license terms. -->
<form>
<label name="plan"/>
<field name="plan"/>
<newline/>
<label name="product"/>
<field name="product"/>
<label name="sequence"/>
<field name="sequence"/>
<newline/>
<label name="category"/>
<field name="category"/>
<label name="product"/>
<field name="product"/>
<separator id="formula" colspan="4"/>
<label name="formula"/>
<field name="formula" colspan="3"/>
</form>
+1
View File
@@ -4,6 +4,7 @@ this repository contains the full copyright notices and license terms. -->
<tree>
<field name="plan"/>
<field name="sequence"/>
<field name="category"/>
<field name="product"/>
<field name="formula"/>
</tree>
+1
View File
@@ -2,6 +2,7 @@
<!-- This file is part of Tryton. The COPYRIGHT file at the top level of
this repository contains the full copyright notices and license terms. -->
<tree sequence="sequence">
<field name="category"/>
<field name="product"/>
<field name="formula"/>
</tree>