You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
5.4 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # mis_builder module for Odoo, Management Information System Builder
  5. # Copyright (C) 2014-2015 ACSONE SA/NV (<http://acsone.eu>)
  6. #
  7. # This file is a part of mis_builder
  8. #
  9. # mis_builder is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU Affero General Public License v3 or later
  11. # as published by the Free Software Foundation, either version 3 of the
  12. # License, or (at your option) any later version.
  13. #
  14. # mis_builder is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU Affero General Public License v3 or later for more details.
  18. #
  19. # You should have received a copy of the GNU Affero General Public License
  20. # v3 or later along with this program.
  21. # If not, see <http://www.gnu.org/licenses/>.
  22. #
  23. ##############################################################################
  24. import xlwt
  25. from openerp.report import report_sxw
  26. from openerp.addons.report_xls.report_xls import report_xls
  27. import logging
  28. _logger = logging.getLogger(__name__)
  29. class mis_builder_xls_parser(report_sxw.rml_parse):
  30. def __init__(self, cr, uid, name, context):
  31. super(mis_builder_xls_parser, self).__init__(
  32. cr, uid, name, context=context)
  33. self.context = context
  34. class mis_builder_xls(report_xls):
  35. def __init__(self, name, table, rml=False, parser=False, header=True,
  36. store=False):
  37. super(mis_builder_xls, self).__init__(
  38. name, table, rml, parser, header, store)
  39. # Cell Styles
  40. _xs = self.xls_styles
  41. # header
  42. rh_cell_format = _xs['bold'] + _xs['fill'] + \
  43. _xs['borders_all'] + _xs['right']
  44. self.rh_cell_style = xlwt.easyxf(rh_cell_format)
  45. self.rh_cell_style_date = xlwt.easyxf(
  46. rh_cell_format, num_format_str=report_xls.date_format)
  47. # lines
  48. self.mis_rh_cell_style = xlwt.easyxf(
  49. _xs['borders_all'] + _xs['bold'] + _xs['fill'])
  50. def generate_xls_report(self, _p, _xs, data, objects, wb):
  51. report_name = objects[0].name
  52. ws = wb.add_sheet(report_name[:31])
  53. ws.panes_frozen = True
  54. ws.remove_splits = True
  55. ws.portrait = 0 # Landscape
  56. ws.fit_width_to_pages = 1
  57. row_pos = 0
  58. # set print header/footer
  59. ws.header_str = self.xls_headers['standard']
  60. ws.footer_str = self.xls_footers['standard']
  61. # Title
  62. c_specs = [
  63. ('report_name', 1, 0, 'text', report_name),
  64. ]
  65. row_data = self.xls_row_template(c_specs, ['report_name'])
  66. row_pos = self.xls_write_row(
  67. ws, row_pos, row_data, row_style=xlwt.easyxf(_xs['xls_title']))
  68. row_pos += 1
  69. # get the computed result of the report
  70. data = self.pool.get('mis.report.instance').compute(
  71. self.cr, self.uid, objects[0].id)
  72. # Column headers
  73. header_name_list = ['']
  74. col_specs_template = {'': {'header': [1, 30, 'text', ''],
  75. 'header_date': [1, 1, 'text', '']}}
  76. for col in data['header'][0]['cols']:
  77. col_specs_template[col['name']] = {'header': [1, 30, 'text',
  78. col['name']],
  79. 'header_date': [1, 1, 'text',
  80. col['date']]}
  81. header_name_list.append(col['name'])
  82. c_specs = map(
  83. lambda x: self.render(x, col_specs_template, 'header'),
  84. header_name_list)
  85. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  86. row_pos = self.xls_write_row(
  87. ws, row_pos, row_data, row_style=self.rh_cell_style,
  88. set_column_size=True)
  89. c_specs = map(lambda x: self.render(
  90. x, col_specs_template, 'header_date'), header_name_list)
  91. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  92. row_pos = self.xls_write_row(
  93. ws, row_pos, row_data, row_style=self.rh_cell_style_date)
  94. ws.set_horz_split_pos(row_pos)
  95. ws.set_vert_split_pos(1)
  96. for line in data['content']:
  97. col = 0
  98. ws.write(row_pos, col, line['kpi_name'], self.mis_rh_cell_style)
  99. for value in line['cols']:
  100. col += 1
  101. num_format_str = '#'
  102. if value.get('dp'):
  103. num_format_str += '.'
  104. num_format_str += '0' * int(value['dp'])
  105. if value.get('suffix'):
  106. num_format_str = num_format_str + ' "%s"' % value['suffix']
  107. kpi_cell_style = xlwt.easyxf(
  108. _xs['borders_all'] + _xs['right'],
  109. num_format_str=num_format_str)
  110. if value.get('val'):
  111. val = value['val']
  112. if value.get('is_percentage'):
  113. val = val / 0.01
  114. ws.write(row_pos, col, val, kpi_cell_style)
  115. else:
  116. ws.write(row_pos, col, value['val_r'], kpi_cell_style)
  117. row_pos += 1
  118. mis_builder_xls('report.mis.report.instance.xls',
  119. 'mis.report.instance',
  120. parser=mis_builder_xls_parser)