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

[16.0][IMP] mis_builder: allow to export multiple report in one xls #669

Open
wants to merge 1 commit into
base: 16.0
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion mis_builder/models/mis_report_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,6 @@ def print_pdf(self):
)

def export_xls(self):
self.ensure_one()
return self.env.ref("mis_builder.xls_export").report_action(
self, data=dict(dummy=True)
) # required to propagate context
Expand Down Expand Up @@ -940,3 +939,10 @@ def _get_drilldown_action_name(self, arg):
return f"{kpi.description} - {account.display_name} - {period.display_name}"
else:
return f"{kpi.description} - {period.display_name}"

def _get_xlsx_report_name(self):
self.ensure_one()
return "{} - {}".format(
self.name,
", ".join([a.name for a in self.query_company_ids]),
)
34 changes: 24 additions & 10 deletions mis_builder/report/mis_report_instance_xlsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,15 @@ class MisBuilderXlsx(models.AbstractModel):
_description = "MIS Builder XLSX report"
_inherit = "report.report_xlsx.abstract"

def generate_xlsx_report(self, workbook, data, objects):
# get the computed result of the report
matrix = objects._compute_matrix()
def _get_worksheet_name(self, mis_instance):
return mis_instance._get_xlsx_report_name()[:31]

def _generate_xlsx_one_report(self, workbook, mis_instance, matrix):
style_obj = self.env["mis.report.style"]

# create worksheet
report_name = "{} - {}".format(
objects[0].name, ", ".join([a.name for a in objects[0].query_company_ids])
)
sheet = workbook.add_worksheet(report_name[:31])
worksheet_name = self._get_worksheet_name(mis_instance)
sheet = workbook.add_worksheet(worksheet_name)
row_pos = 0
col_pos = 0
# width of the labels column
Expand All @@ -48,13 +47,14 @@ def generate_xlsx_report(self, workbook, data, objects):
header_format = workbook.add_format(
{"bold": True, "align": "center", "bg_color": "#F0EEEE"}
)
report_name = mis_instance._get_xlsx_report_name()
sheet.write(row_pos, 0, report_name, bold)
row_pos += 2

# filters
filter_descriptions = objects.get_filter_descriptions()
filter_descriptions = mis_instance.get_filter_descriptions()
if filter_descriptions:
for filter_description in objects.get_filter_descriptions():
for filter_description in mis_instance.get_filter_descriptions():
sheet.write(row_pos, 0, filter_description)
row_pos += 1
row_pos += 1
Expand All @@ -79,7 +79,9 @@ def generate_xlsx_report(self, workbook, data, objects):
else:
sheet.write(row_pos, col_pos, label, header_format)
col_width[col_pos] = max(
col_width[col_pos], len(col.label or ""), len(col.description or "")
col_width[col_pos],
len(col.label or ""),
len(col.description or ""),
)
col_pos += col.colspan
row_pos += 1
Expand Down Expand Up @@ -174,3 +176,15 @@ def generate_xlsx_report(self, workbook, data, objects):
min_col_pos = min(col_width.keys())
max_col_pos = max(col_width.keys())
sheet.set_column(min_col_pos, max_col_pos, data_col_width * COL_WIDTH)

return sheet

def _precompute_matrixes(self, objects):
"""
Compute result of the report before actually generating worksheet
"""
return [(obj, obj._compute_matrix()) for obj in objects]
Copy link
Member

Choose a reason for hiding this comment

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

This _recompute_matrixes method does not seem to add much value?


def generate_xlsx_report(self, workbook, data, objects):
for instance, matrix in self._precompute_matrixes(objects):
self._generate_xlsx_one_report(workbook, instance, matrix)
11 changes: 11 additions & 0 deletions mis_builder/report/mis_report_instance_xlsx.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,15 @@
<field name="report_type">xlsx</field>
<field name="report_file">mis_report_instance</field>
</record>

<record model="ir.actions.server" id="mis_builder_print_xls_report">
<field name="name">Export XLS</field>
<field name="model_id" ref="mis_builder.model_mis_report_instance" />
<field name="binding_model_id" ref="mis_builder.model_mis_report_instance" />
<field name="binding_view_types">list</field>
<field name="state">code</field>
<field name="code">
action = records.export_xls()
</field>
</record>
</odoo>
31 changes: 31 additions & 0 deletions mis_builder/tests/test_mis_report_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,27 @@ def setUp(self):
)
)

# create a duplicate of first instance with different period
self.report_instance_4 = self.env["mis.report.instance"].create(
dict(
name="test instance",
report_id=self.report.id,
company_id=self.env.ref("base.main_company").id,
period_ids=[
(
0,
0,
dict(
name="p2",
mode="fix",
manual_date_from="2015-01-01",
manual_date_to="2015-12-31",
),
),
],
)
)

def test_compute(self):
matrix = self.report_instance._compute_matrix()
for row in matrix.iter_rows():
Expand Down Expand Up @@ -512,6 +533,16 @@ def test_xlsx(self):
report_type="xlsx",
)

def test_xlsx_multiple_instances(self):
self.report_instance.export_xls() # get action
test_reports.try_report(
self.env.cr,
self.env.uid,
"mis_builder.mis_report_instance_xlsx",
[self.report_instance.id, self.report_instance_4.id],
report_type="xlsx",
)

def test_get_kpis_by_account_id(self):
account_ids = (
self.env["account.account"]
Expand Down
Loading