diff --git a/CHANGELOG b/CHANGELOG index 3ca0e1d95f..27d7c5e8b1 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Add product and category on analytic rule + Version 5.0.0 - 2018-10-01 * Bug fixes (see mercurial logs for details) * Remove support for Python 2.7 diff --git a/__init__.py b/__init__.py index cc5e9d75c0..4c097e4e75 100644 --- a/__init__.py +++ b/__init__.py @@ -5,6 +5,7 @@ from trytond.pool import Pool from . import product from . import configuration from . import account +from . import analytic_account def register(): @@ -21,6 +22,10 @@ def register(): configuration.Configuration, configuration.ConfigurationDefaultAccount, module='account_product', type_='model') + Pool.register( + account.MoveLine, + analytic_account.Rule, + module='account_product', type_='model', depends=['analytic_account']) Pool.register( account.CreateChart, module='account_product', type_='wizard') diff --git a/account.py b/account.py index b94e630043..1a8118c3bd 100644 --- a/account.py +++ b/account.py @@ -5,8 +5,6 @@ from trytond.pyson import Eval from trytond.model import fields from trytond.transaction import Transaction -__all__ = ['CreateChart', 'CreateChartProperties'] - class CreateChartProperties(metaclass=PoolMeta): __name__ = 'account.create_chart.properties' @@ -44,3 +42,29 @@ class CreateChart(metaclass=PoolMeta): getattr(self.properties, name, None)) config.save() return state + + +class MoveLine(metaclass=PoolMeta): + __name__ = 'account.move.line' + + @property + def product(self): + pass + + @property + def rule_pattern(self): + def parents(categories): + for category in categories: + while category: + yield category + category = category.parent + + pattern = super().rule_pattern + if self.product: + pattern['product'] = self.product.id + pattern['product_categories'] = [ + c.id for c in parents(self.product.categories_all)] + else: + pattern['product'] = None + pattern['product_categories'] = [] + return pattern diff --git a/analytic_account.py b/analytic_account.py new file mode 100644 index 0000000000..30ac70488a --- /dev/null +++ b/analytic_account.py @@ -0,0 +1,21 @@ +# This file is part of Tryton. The COPYRIGHT file at the top level of +# this repository contains the full copyright notices and license terms. +from trytond.model import fields +from trytond.pool import PoolMeta + + +class Rule(metaclass=PoolMeta): + __name__ = 'analytic_account.rule' + + product = fields.Many2One('product.product', "Product", ondelete='CASCADE') + product_category = fields.Many2One( + 'product.category', "Product Category", ondelete='CASCADE') + + def match(self, pattern): + if 'product_categories' in pattern: + pattern = pattern.copy() + categories = pattern.pop('product_categories') + if (self.product_category is not None + and self.product_category.id not in categories): + return False + return super().match(pattern) diff --git a/analytic_account.xml b/analytic_account.xml new file mode 100644 index 0000000000..86ee7bde86 --- /dev/null +++ b/analytic_account.xml @@ -0,0 +1,18 @@ + + + + + + analytic_account.rule + + analytic_account_rule_list + + + + analytic_account.rule + + analytic_account_rule_form + + + diff --git a/setup.py b/setup.py index 6456a73ef6..abc224043e 100755 --- a/setup.py +++ b/setup.py @@ -51,6 +51,7 @@ for dep in info.get('depends', []): requires.append(get_require_version('trytond_%s' % dep)) requires.append(get_require_version('trytond')) +tests_require = [get_require_version('trytond_analytic_account')] dependency_links = [] if minor_version % 2: dependency_links.append('https://trydevpi.tryton.org/') @@ -118,4 +119,5 @@ setup(name=name, """, test_suite='tests', test_loader='trytond.test_loader:Loader', + tests_require=tests_require, ) diff --git a/tests/test_account_product.py b/tests/test_account_product.py index 94205551b8..411ea0ddcc 100644 --- a/tests/test_account_product.py +++ b/tests/test_account_product.py @@ -15,6 +15,7 @@ from trytond.modules.account.tests import create_chart class AccountProductTestCase(ModuleTestCase): 'Test AccountProduct module' module = 'account_product' + extras = ['analytic_account'] @with_transaction() def test_account_used(self): diff --git a/tryton.cfg b/tryton.cfg index cac99da4e1..bedf7bf897 100644 --- a/tryton.cfg +++ b/tryton.cfg @@ -5,8 +5,11 @@ depends: company ir product +extras_depend: + analytic_account xml: product.xml configuration.xml account.xml message.xml + analytic_account.xml diff --git a/view/analytic_account_rule_form.xml b/view/analytic_account_rule_form.xml new file mode 100644 index 0000000000..2314f0c8ad --- /dev/null +++ b/view/analytic_account_rule_form.xml @@ -0,0 +1,12 @@ + + + + + + + diff --git a/view/analytic_account_rule_list.xml b/view/analytic_account_rule_list.xml new file mode 100644 index 0000000000..4be150a7a3 --- /dev/null +++ b/view/analytic_account_rule_list.xml @@ -0,0 +1,9 @@ + + + + + + + +