Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable account.account model (can use any model that has a code field) #149

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mis_builder/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,10 @@ Contributors
* Eric Caudal <eric.caudal@elico-corp.com>
* Andrea Stirpe <a.stirpe@onestein.nl>
* Maxence Groine <mgroine@fiefmanage.ch>
* `Tecnativa <https://www.tecnativa.com>`_:

* Ernesto Tejeda
* Pedro M. Baeza

Maintainers
~~~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion mis_builder/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{
'name': 'MIS Builder',
'version': '11.0.3.4.0',
'version': '11.0.4.0.0',
'category': 'Reporting',
'summary': """
Build 'Management Information System' Reports and Dashboards
Expand Down
10 changes: 6 additions & 4 deletions mis_builder/models/aep.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class AccountingExpressionProcessor(object):
r"(?P<ml_domain>\[.*?\])?"
)

def __init__(self, companies, currency=None):
def __init__(self, companies, currency=None, account_model=None):
self.env = companies.env
self.companies = companies
if not currency:
Expand All @@ -112,6 +112,9 @@ def __init__(self, companies, currency=None):
# a first query to get the initial balance and another
# to get the variation, so it's a bit slower
self.smart_end = True
# Account model
self._account_model = self.env[
account_model or 'account.account'].with_context(active_test=False)

def _account_codes_to_domain(self, account_codes):
"""Convert a comma separated list of account codes
Expand Down Expand Up @@ -188,16 +191,15 @@ def parse_expr(self, expr):

def done_parsing(self):
""" Replace account domains by account ids in map """
account_model = self.env['account.account'].\
with_context(active_test=False)
for key, acc_domains in self._map_account_ids.items():
all_account_ids = set()
for acc_domain in acc_domains:
acc_domain_with_company = expression.AND([
acc_domain,
[('company_id', 'in', self.companies.ids)],
])
account_ids = account_model.\
account_ids = self._account_model.\
with_context(active_test=False).\
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
with_context(active_test=False).\

search(acc_domain_with_company).ids
self._account_ids_by_acc_domain[acc_domain].\
update(account_ids)
Expand Down
4 changes: 2 additions & 2 deletions mis_builder/models/kpimatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,12 @@ def __init__(self, row, subcol,

class KpiMatrix(object):

def __init__(self, env):
def __init__(self, env, account_model=None):
# cache language id for faster rendering
lang_model = env['res.lang']
self.lang = lang_model._lang_get(env.user.lang)
self._style_model = env['mis.report.style']
self._account_model = env['account.account']
self._account_model = env[account_model or 'account.account']
# data structures
# { kpi: KpiMatrixRow }
self._kpi_rows = OrderedDict()
Expand Down
32 changes: 30 additions & 2 deletions mis_builder/models/mis_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,10 @@ class MisReport(models.Model):
_name = 'mis.report'
_description = 'MIS Report Template'

def _default_move_lines_source(self):
return self.env['ir.model'].search(
[('model', '=', 'account.move.line')]).id

name = fields.Char(required=True,
string='Name', translate=True)
description = fields.Char(required=False,
Expand All @@ -470,6 +474,30 @@ class MisReport(models.Model):
subkpi_ids = fields.One2many('mis.report.subkpi', 'report_id',
string="Sub KPI",
copy=True)
move_lines_source = fields.Many2one(
comodel_name='ir.model',
string='Move lines source',
domain=[('field_id.name', '=', 'debit'),
('field_id.name', '=', 'credit'),
('field_id.name', '=', 'account_id'),
('field_id.name', '=', 'date'),
('field_id.name', '=', 'company_id')],
default=_default_move_lines_source,
required=True,
help="A 'move line like' model, ie having at least debit, credit, "
"date, account_id and company_id fields.",
)
account_model = fields.Char(
compute='_compute_account_model',
string='Account model',
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make this field a Char


@api.multi
@api.depends('move_lines_source')
def _compute_account_model(self):
for record in self:
record.account_model = record.move_lines_source.field_id.filtered(
lambda r: r.name == 'account_id').relation

@api.onchange('subkpi_ids')
def _on_change_subkpi_ids(self):
Expand Down Expand Up @@ -536,15 +564,15 @@ def copy(self, default=None):
@api.multi
def prepare_kpi_matrix(self):
self.ensure_one()
kpi_matrix = KpiMatrix(self.env)
kpi_matrix = KpiMatrix(self.env, self.account_model)
for kpi in self.kpi_ids:
kpi_matrix.declare_kpi(kpi)
return kpi_matrix

@api.multi
def _prepare_aep(self, companies, currency=None):
self.ensure_one()
aep = AEP(companies, currency)
aep = AEP(companies, currency, self.account_model)
for kpi in self.kpi_ids:
for expression in kpi.expression_ids:
if expression.name:
Expand Down
20 changes: 18 additions & 2 deletions mis_builder/models/mis_report_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,20 @@ def _compute_dates(self):
'Period name should be unique by report'),
]

@api.constrains('source_aml_model_id')
def _check_source_aml_model_id(self):
for record in self:
if record.source_aml_model_id:
record_model = record.source_aml_model_id.field_id.filtered(
lambda r: r.name == 'account_id').relation
report_account_model = record.report_id.account_model
if record_model != report_account_model:
raise ValidationError(_(
'Actual (alternative) models used in columns must '
'have the same account model in account_id and must '
'be the same defined in the '
'Template: %s') % report_account_model)

@api.onchange('date_range_id')
def _onchange_date_range(self):
if self.date_range_id:
Expand Down Expand Up @@ -681,6 +695,7 @@ def _add_column_actuals(
period.subkpi_ids,
period._get_additional_move_line_filter,
period._get_additional_query_filter,
aml_model=self.report_id.move_lines_source.model,
no_auto_expand_accounts=self.no_auto_expand_accounts,
)

Expand Down Expand Up @@ -777,7 +792,8 @@ def drilldown(self, arg):
account_id = arg.get('account_id')
if period_id and expr and AEP.has_account_var(expr):
period = self.env['mis.report.instance.period'].browse(period_id)
aep = AEP(self.query_company_ids, self.currency_id)
aep = AEP(self.query_company_ids, self.currency_id,
self.report_id.account_model)
aep.parse_expr(expr)
aep.done_parsing()
domain = aep.get_aml_domain_for_expr(
Expand All @@ -789,7 +805,7 @@ def drilldown(self, arg):
if period.source == SRC_ACTUALS_ALT:
aml_model_name = period.source_aml_model_id.model
else:
aml_model_name = 'account.move.line'
aml_model_name = self.report_id.move_lines_source.model
return {
'name': u'{} - {}'.format(expr, period.name),
'domain': domain,
Expand Down
4 changes: 4 additions & 0 deletions mis_builder/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@
* Eric Caudal <eric.caudal@elico-corp.com>
* Andrea Stirpe <a.stirpe@onestein.nl>
* Maxence Groine <mgroine@fiefmanage.ch>
* `Tecnativa <https://www.tecnativa.com>`_:

* Ernesto Tejeda
* Pedro M. Baeza
4 changes: 4 additions & 0 deletions mis_builder/readme/newsfragments/149.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Now account_id field of the model selected in 'Move lines source'
in the Period form (mis.report.instance.period) can be a Many2one
relationship with any model that has a field 'code' (not only with
'account.account' model)
7 changes: 6 additions & 1 deletion mis_builder/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.15.1: http://docutils.sourceforge.net/" />
<meta name="generator" content="Docutils 0.15.2: http://docutils.sourceforge.net/" />
<title>MIS Builder</title>
<style type="text/css">

Expand Down Expand Up @@ -804,6 +804,11 @@ <h2><a class="toc-backref" href="#id73">Contributors</a></h2>
<li>Eric Caudal &lt;<a class="reference external" href="mailto:eric.caudal&#64;elico-corp.com">eric.caudal&#64;elico-corp.com</a>&gt;</li>
<li>Andrea Stirpe &lt;<a class="reference external" href="mailto:a.stirpe&#64;onestein.nl">a.stirpe&#64;onestein.nl</a>&gt;</li>
<li>Maxence Groine &lt;<a class="reference external" href="mailto:mgroine&#64;fiefmanage.ch">mgroine&#64;fiefmanage.ch</a>&gt;</li>
<li><a class="reference external" href="https://www.tecnativa.com">Tecnativa</a>:<ul>
<li>Ernesto Tejeda</li>
<li>Pedro M. Baeza</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="maintainers">
Expand Down
2 changes: 2 additions & 0 deletions mis_builder/tests/test_data_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def setUp(self):
# create report
self.report = self.env['mis.report'].create(dict(
name='test report',
move_lines_source=self.env['ir.model'].search(
[('model', '=', 'account.move.line')]).id,
))
self.kpi1 = self.env['mis.report.kpi'].create(dict(
report_id=self.report.id,
Expand Down
3 changes: 3 additions & 0 deletions mis_builder/tests/test_kpi_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ def setUpClass(cls):

report = cls.env['mis.report'].create(dict(
name='test report',
move_lines_source=cls.env['ir.model'].search(
[('model', '=', 'account.move.line')]).id,
))
report._compute_account_model()
cls.kpi1 = cls.env['mis.report.kpi'].create(dict(
report_id=report.id,
name='k1',
Expand Down
3 changes: 3 additions & 0 deletions mis_builder/tests/test_mis_report_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def setUp(self):
# create a report with 2 subkpis and one query
self.report = self.env['mis.report'].create(dict(
name='test report',
move_lines_source=self.env['ir.model'].search(
[('model', '=', 'account.move.line')]).id,
subkpi_ids=[(0, 0, dict(
name='sk1',
description='subkpi 1',
Expand Down Expand Up @@ -87,6 +89,7 @@ def setUp(self):
sequence=2,
))]
))
self.report._compute_account_model()
# kpi with accounting formulas
self.kpi1 = self.env['mis.report.kpi'].create(dict(
report_id=self.report.id,
Expand Down
3 changes: 3 additions & 0 deletions mis_builder/tests/test_period_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ def setUp(self):
self.period_obj = self.env['mis.report.instance.period']
self.report = self.report_obj.create(dict(
name='test-report',
move_lines_source=self.env['ir.model'].search(
[('model', '=', 'account.move.line')]).id,
))
self.report._compute_account_model()
self.instance = self.instance_obj.create(dict(
name='test-instance',
report_id=self.report.id,
Expand Down
1 change: 1 addition & 0 deletions mis_builder/views/mis_report.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<field name="name"/>
<field name="description"/>
<field name="style_id"/>
<field name="move_lines_source"/>
</group>
<notebook>
<page string="KPI's">
Expand Down