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.

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