trytond-patches/issue6322_6501.diff

102 lines
4.2 KiB
Diff

diff --git a/trytond/trytond/modules/commission/commission.py b/trytond/trytond/modules/commission/commission.py
index dd57371..8880c0a 100644
--- a/trytond/trytond/modules/commission/commission.py
+++ b/trytond/trytond/modules/commission/commission.py
@@ -141,9 +141,28 @@ class Plan(ModelSQL, ModelView):
def compute(self, amount, product, pattern=None):
'Compute commission amount for the amount'
+ Template = Pool().get('product.template')
+
+ def parents(categories):
+ for category in categories:
+ while category:
+ yield category
+ category = category.parent
+
if pattern is None:
pattern = {}
- pattern['product'] = product.id if product else None
+ if product:
+ if hasattr(Template, 'categories'):
+ pattern['categories'] = [c.id for c in parents(product.categories)]
+ else:
+ pattern['category'] = product.category.id if product.category else None
+ pattern['product'] = product.id
+ else:
+ if hasattr(Template, 'categories'):
+ pattern['categories'] = []
+ else:
+ pattern['category'] = None
+ pattern['product'] = None
context = self.get_context_formula(amount, product)
for line in self.lines:
if line.match(pattern):
@@ -155,6 +174,8 @@ class PlanLines(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')
sequence = fields.Integer('Sequence')
formula = fields.Char('Formula', required=True,
@@ -204,6 +225,15 @@ class PlanLines(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 -r dcef1b340df0 trytond/trytond/modules/commission/view/plan_line_form.xml
--- a/trytond/trytond/modules/commission/view/plan_line_form.xml Wed Jan 04 00:28:37 2017 +0100
+++ b/trytond/trytond/modules/commission/view/plan_line_form.xml Fri Nov 17 13:33:24 2017 +0100
@@ -5,11 +5,12 @@
<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"/>
<label name="formula"/>
- <field name="formula" colspan="3"/>
+ <field name="formula" colspan="4"/>
</form>
diff -r dcef1b340df0 trytond/trytond/modules/commission/view/plan_line_list.xml
--- a/trytond/trytond/modules/commission/view/plan_line_list.xml Wed Jan 04 00:28:37 2017 +0100
+++ b/trytond/trytond/modules/commission/view/plan_line_list.xml Fri Nov 17 13:33:24 2017 +0100
@@ -4,6 +4,7 @@
<tree string="Commission Plan Lines">
<field name="plan"/>
<field name="sequence"/>
+ <field name="category"/>
<field name="product"/>
<field name="formula"/>
</tree>
diff -r dcef1b340df0 trytond/trytond/modules/commission/view/plan_line_list_sequence.xml
--- a/trytond/trytond/modules/commission/view/plan_line_list_sequence.xml Wed Jan 04 00:28:37 2017 +0100
+++ b/trytond/trytond/modules/commission/view/plan_line_list_sequence.xml Fri Nov 17 13:33:24 2017 +0100
@@ -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 string="Commission Plan Lines" sequence="sequence">
+ <field name="category"/>
<field name="product"/>
<field name="formula"/>
</tree>