# Rules for imported statement

**URL:** https://discuss.tryton.org/t/rules-for-imported-statement/939
**Category:** Feature
**Tags:** accounting
**Created:** [December 6, 2018, 9:12am UTC](https://discuss.tryton.org/t/rules-for-imported-statement/939 "2018-12-06T09:12:31Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [December 6, 2018, 9:12am UTC](https://discuss.tryton.org/t/rules-for-imported-statement/939/1 "2018-12-06T09:12:31Z")

</div>

## Rational

With [Import Statement Files](https://discuss.tryton.org/t/import-statement-files/179), it is easy to import statement but the user still have to create statement lines from origins. In many cases, such work could be automated with some rules using the imported data like the description or informations.

## Proposal

A new model `account.statement.rule` is created which is composed of two parts: matching criteria for origin and lines template.  
A button on the `account.statement` loop over all the origin lines without any statement line (to allow to run it multiple times without losing previous customization). It tries to find the first matching rule and then apply the template to create lines.

### Criteria

- Company: `ManyOne` (optional)

- Journal: `Many2One` (optional)

- Amount: range (optional) (in journal currency)

- Description: [Regular expression](https://docs.python.org/3.6/library/re.html#regular-expression-syntax)

- Information: `One2Many`

All filled criteria must be valid to match a line.  
The regexp are evaluated using [`re.search`](https://docs.python.org/3.6/library/re.html#re.search) so a simple string will match even if the value does not start with it. Group name can be used to define `party`, `bank_account`, `invoice`, `payment` and `payment_group` which could be use as rec\_name search for line template.

### Lines template

A list of line template similar to the `account.move.template`.

- Amount: formula based on `amount` and `pending_amount`
- Party: `Many2One` (if not filled, use group name `party` to search for one)
- Account: `Many2One` (if not filled, use the party receivable or payable depending on the sign)

Invoice, Payment and Payment Group are set using the respecting group name.  
The group name are taken first from the description and then on each information regexp.

## Implementation

> **[Account Statement Rule (#7925) · Issues · Tryton / Tryton · GitLab](https://foss.heptapod.net/tryton/tryton/-/issues/7925)**
>
> From https://discuss.tryton.org/t/rules-for-imported-statement/939

---

<div class="post-metadata">

### Author: ![albert](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/albert/32/21_2.png) [@albert](https://discuss.tryton.org/u/albert)
#### Post date: [December 7, 2018, 10:18am UTC](https://discuss.tryton.org/t/rules-for-imported-statement/939/2 "2018-12-07T10:18:20Z")

</div>

> [@ced](#):
>
> Amount: `Numeric` percentage of the origin amount (sum of line must be 100%)

I think amount should be a formula just like in product\_price\_list [1]. It is quite usual to have fixed amount which could not be managed by %.

Convenient variables for the formula would be “total\_amount” and “pending\_amount” (that’s what we are using in [2]). It is also very flexible because it could allow other modules to implement new variables such as “bank\_default\_commission” that could be stored in the journal, for example.

This way if you know that all origin lines with the text “COMMISSIONED PAYMENT” have an expense of 2€ you can create two lines:

- One with the 2€ to “Bank Expense Account”
- The other one with the “pending\_amount” to “Some other account”

Also, for me, it is not necessary to ensure that the 100% is spread because user may want to automate only half of the origin line. Again, if the line contains the text “COMMISSIONED” the user may want to create one line of 2€ to “Bank Expense Account” automatically but leave the pending amount to be introduced manually.

[1] [http://hg.tryton.org/modules/product\_price\_list/file/tip/price\_list.py#l81](http://hg.tryton.org/modules/product_price_list/file/tip/price_list.py#l81)  
[2] [https://bitbucket.org/nantic/trytond-account\_bank\_statement\_rule/src/a8110cd5f6bd4bc2a6046b0d701c9738e81512a8/statement.py#lines-83](https://bitbucket.org/nantic/trytond-account_bank_statement_rule/src/a8110cd5f6bd4bc2a6046b0d701c9738e81512a8/statement.py#lines-83)

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [December 7, 2018, 10:28am UTC](https://discuss.tryton.org/t/rules-for-imported-statement/939/3 "2018-12-07T10:28:11Z")

</div>

> [@albert](#):
>
> Convenient variables for the formula would be “total\_amount” and “pending\_amount”

For me, it can not have pending\_amount as the rule applies only on empty origin.

---

<div class="post-metadata">

### Author: ![albert](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/albert/32/21_2.png) [@albert](https://discuss.tryton.org/u/albert)
#### Post date: [December 7, 2018, 11:12am UTC](https://discuss.tryton.org/t/rules-for-imported-statement/939/4 "2018-12-07T11:12:51Z")

</div>

> [@ced](#):
>
> For me, it can not have pending\_amount as the rule applies only on empty origin.

You can if you add a sequence to Lines Template list. This way Tryton will process those lines in the given order and so you can do what I mentioned in the example:

- First line sets a fixed amount of 2€
- Second line uses “pending\_amount” which is the total amount of the origin minus the amount already used in previous lines already processed (2€ in the example)

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [December 7, 2018, 11:33am UTC](https://discuss.tryton.org/t/rules-for-imported-statement/939/5 "2018-12-07T11:33:45Z")

</div>

OK but it should be computed in the fly as the lines will not be stored directly.

---

<div class="post-metadata">

### Author: ![pokoli](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/pokoli/32/22_2.png) [@pokoli](https://discuss.tryton.org/u/pokoli)
#### Post date: [December 10, 2018, 11:40am UTC](https://discuss.tryton.org/t/rules-for-imported-statement/939/6 "2018-12-10T11:40:19Z")

</div>

> [@ced](#):
>
> A wizard on the `account.statement` loop over all the origin lines without any statement line (to allow to run it multiple times without losing previous customization). It tries to find the first matching rule and then apply the template to create lines.

I think this wizard should be automatically executed after importing a statement file so all the rules are applied.

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [December 10, 2018, 11:51am UTC](https://discuss.tryton.org/t/rules-for-imported-statement/939/7 "2018-12-10T11:51:40Z")

</div>

I do not think because we need to allow user to run it when he wants. For example if he changed some rules, he may want to re-run it. So for me, it is a better workflow to always have the same workflow.  
Now on automatized operation like [Continuous importation of bank statement](https://discuss.tryton.org/t/continuous-importation-of-bank-statement/943/1) this wizard should be run automatically.

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [December 11, 2018, 5:33pm UTC](https://discuss.tryton.org/t/rules-for-imported-statement/939/8 "2018-12-11T17:33:37Z")

</div>

> [@ced](#):
>
> A wizard on the `account.statement` loop over all the origin lines

Indeed I think it could be a button instead of a wizard because there is no interaction with the user.

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [December 13, 2018, 7:16pm UTC](https://discuss.tryton.org/t/rules-for-imported-statement/939/9 "2018-12-13T19:16:33Z")

</div>

The patch of [Account Statement Rule (#7925) · Issues · Tryton / Tryton · GitLab](https://bugs.tryton.org/issue7925) is ready for testing.

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [February 1, 2019, 5:00pm UTC](https://discuss.tryton.org/t/rules-for-imported-statement/939/10 "2019-02-01T17:00:06Z")

</div>

This topic was automatically closed after 14 days. New replies are no longer allowed.
