changeset 5deaa442f09a in modules/account_statement_rule:default
details: 
https://hg.tryton.org/modules/account_statement_rule?cmd=changeset&node=5deaa442f09a
description:
        Store rule keyword and use them for future matching

        issue10518
        review353631002
diffstat:

 CHANGELOG                                          |    2 +
 __init__.py                                        |    1 +
 account.py                                         |   31 ++++
 tests/scenario_account_statement_rule_keywords.rst |  137 +++++++++++++++++++++
 tests/test_account_statement_rule.py               |    5 +
 5 files changed, 176 insertions(+), 0 deletions(-)

diffs (236 lines):

diff -r fb4c6ae69d0f -r 5deaa442f09a CHANGELOG
--- a/CHANGELOG Sun Jul 04 17:54:46 2021 +0200
+++ b/CHANGELOG Tue Aug 17 10:10:20 2021 +0200
@@ -1,3 +1,5 @@
+* Store rule keywords and use them for party matching
+
 Version 6.0.0 - 2021-05-03
 * Bug fixes (see mercurial logs for details)
 
diff -r fb4c6ae69d0f -r 5deaa442f09a __init__.py
--- a/__init__.py       Sun Jul 04 17:54:46 2021 +0200
+++ b/__init__.py       Tue Aug 17 10:10:20 2021 +0200
@@ -10,6 +10,7 @@
 def register():
     Pool.register(
         account.Statement,
+        account.StatementOrigin,
         account.StatementRule,
         account.StatementRuleInformation,
         account.StatementRuleLine,
diff -r fb4c6ae69d0f -r 5deaa442f09a account.py
--- a/account.py        Sun Jul 04 17:54:46 2021 +0200
+++ b/account.py        Tue Aug 17 10:10:20 2021 +0200
@@ -47,8 +47,17 @@
             for rule in rules:
                 keywords = rule.match(origin)
                 if keywords:
+                    origin.keywords = keywords
                     yield from rule.apply(origin, keywords)
                     break
+        self.origins = self.origins
+        self.save()
+
+
+class StatementOrigin(metaclass=PoolMeta):
+    __name__ = 'account.statement.origin'
+
+    keywords = fields.Dict(None, "KeyWords")
 
 
 class StatementRule(sequence_ordered(), ModelSQL, ModelView):
@@ -369,6 +378,7 @@
     def _get_party(self, origin, keywords):
         pool = Pool()
         Party = pool.get('party.party')
+        Line = pool.get('account.statement.line')
         try:
             AccountNumber = pool.get('bank.account.number')
         except KeyError:
@@ -386,11 +396,32 @@
                     number, = numbers
                     if number.account.owners:
                         party = number.account.owners[0]
+                if not number:
+                    lines = Line.search([
+                            ('statement.state', 'in', ['validated', 'posted']),
+                            ('origin.keywords.bank_account', '=',
+                                keywords['bank_account']),
+                            ('party', '!=', None),
+                            ],
+                        order=[('date', 'DESC')], limit=1)
+                    if lines:
+                        line, = lines
+                        party = line.party
             elif keywords.get('party'):
                 parties = Party.search(
                     [('rec_name', 'ilike', keywords['party'])])
                 if len(parties) == 1:
                     party, = parties
+                if not party:
+                    lines = Line.search([
+                            ('statement.state', 'in', ['validated', 'posted']),
+                            ('origin.keywords.party', '=', keywords['party']),
+                            ('party', '!=', None),
+                            ],
+                        order=[('date', 'DESC')], limit=1)
+                    if lines:
+                        line, = lines
+                        party = line.party
         return party
 
     def _get_invoice(self, origin, keywords):
diff -r fb4c6ae69d0f -r 5deaa442f09a 
tests/scenario_account_statement_rule_keywords.rst
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/scenario_account_statement_rule_keywords.rst        Tue Aug 17 
10:10:20 2021 +0200
@@ -0,0 +1,137 @@
+=======================================
+Account Statement Rule Keyword Scenario
+=======================================
+
+Imports::
+
+    >>> import datetime
+    >>> from decimal import Decimal
+    >>> from proteus import Model, Wizard
+    >>> from trytond.tests.tools import activate_modules
+    >>> from trytond.modules.company.tests.tools import (
+    ...     create_company, get_company)
+    >>> from trytond.modules.account.tests.tools import (
+    ...     create_fiscalyear, create_chart, get_accounts)
+    >>> from trytond.modules.account_invoice.tests.tools import (
+    ...     set_fiscalyear_invoice_sequences)
+    >>> today = datetime.date.today()
+
+Activate modules::
+
+    >>> config = activate_modules('account_statement_rule')
+
+Create company::
+
+    >>> _ = create_company()
+    >>> company = get_company()
+
+Create fiscal year::
+
+    >>> fiscalyear = set_fiscalyear_invoice_sequences(
+    ...     create_fiscalyear(company))
+    >>> fiscalyear.click('create_period')
+
+Create chart of accounts::
+
+    >>> _ = create_chart(company)
+    >>> accounts = get_accounts(company)
+    >>> receivable = accounts['receivable']
+    >>> cash = accounts['cash']
+
+Create parties::
+
+    >>> Party = Model.get('party.party')
+    >>> customer = Party(name="Customer")
+    >>> customer.save()
+
+Create Account Journal::
+
+    >>> Sequence = Model.get('ir.sequence')
+    >>> SequenceType = Model.get('ir.sequence.type')
+    >>> AccountJournal = Model.get('account.journal')
+
+    >>> sequence_type, = SequenceType.find([('name', '=', "Account Journal")])
+    >>> sequence = Sequence(name="Satement",
+    ...     sequence_type=sequence_type,
+    ...     company=company,
+    ...     )
+    >>> sequence.save()
+    >>> account_journal = AccountJournal(name="Statement",
+    ...     type='statement',
+    ...     sequence=sequence,
+    ...     )
+    >>> account_journal.save()
+
+Create statement rules::
+
+    >>> Rule = Model.get('account.statement.rule')
+
+    >>> rule = Rule(name="Party Rule")
+    >>> rule.company = company
+    >>> rule.description = r"Party: *(?P<party>.*)"
+    >>> line = rule.lines.new()
+    >>> line.amount = "pending"
+    >>> line.account = receivable
+    >>> rule.save()
+
+Create a statement with non matching rule::
+
+    >>> StatementJournal = Model.get('account.statement.journal')
+    >>> Statement = Model.get('account.statement')
+    >>> journal_number = StatementJournal(name="Number",
+    ...     journal=account_journal,
+    ...     account=cash,
+    ...     validation='number_of_lines',
+    ...     )
+    >>> journal_number.save()
+
+    >>> statement = Statement(name="number origins")
+    >>> statement.journal = journal_number
+    >>> statement.number_of_lines = 1
+    >>> origin = statement.origins.new()
+    >>> origin.date = today
+    >>> origin.amount = Decimal('50.00')
+    >>> origin.description = "Party: %s-%s" % (customer.code, customer.name)
+    >>> statement.save()
+    >>> len(statement.lines)
+    0
+
+Apply rules on statement::
+
+    >>> statement.click('apply_rules')
+    >>> len(statement.lines)
+    0
+
+Manually create a line for the origin::
+
+    >>> origin, = statement.origins
+    >>> line = origin.lines.new()
+    >>> line.party = customer
+    >>> origin.save()
+    >>> statement.click('validate_statement')
+    >>> statement.click('post')
+
+
+Create a new statement with same keyword::
+
+    >>> statement = Statement(name="number origins")
+    >>> statement.journal = journal_number
+    >>> statement.number_of_lines = 1
+    >>> origin = statement.origins.new()
+    >>> origin.date = today
+    >>> origin.amount = Decimal('50.00')
+    >>> origin.description = "Party: %s-%s" % (customer.code, customer.name)
+    >>> statement.save()
+    >>> len(statement.lines)
+    0
+
+Now a party is found::
+
+    >>> statement.click('apply_rules')
+    >>> line, = statement.lines
+    >>> line.amount
+    Decimal('50.00')
+    >>> line.party == customer
+    True
+    >>> line.account == receivable
+    True
diff -r fb4c6ae69d0f -r 5deaa442f09a tests/test_account_statement_rule.py
--- a/tests/test_account_statement_rule.py      Sun Jul 04 17:54:46 2021 +0200
+++ b/tests/test_account_statement_rule.py      Tue Aug 17 10:10:20 2021 +0200
@@ -26,4 +26,9 @@
             tearDown=doctest_teardown, encoding='utf-8',
             checker=doctest_checker,
             optionflags=doctest.REPORT_ONLY_FIRST_FAILURE))
+    suite.addTests(doctest.DocFileSuite(
+            'scenario_account_statement_rule_keywords.rst',
+            tearDown=doctest_teardown, encoding='utf-8',
+            checker=doctest_checker,
+            optionflags=doctest.REPORT_ONLY_FIRST_FAILURE))
     return suite

Reply via email to