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.

136 lines
6.4 KiB

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 datetime import datetime
  25. from openerp.report import report_sxw
  26. from openerp.addons.report_xls.report_xls import report_xls
  27. from openerp.tools.translate import translate
  28. import logging
  29. _logger = logging.getLogger(__name__)
  30. _ir_translation_name = 'mis.report.instance.xls'
  31. class mis_builder_xls_parser(report_sxw.rml_parse):
  32. def __init__(self, cr, uid, name, context):
  33. super(mis_builder_xls_parser, self).__init__(cr, uid, name, context=context)
  34. self.context = context
  35. self.localcontext.update({
  36. 'datetime': datetime,
  37. '_': self._,
  38. })
  39. def _(self, src):
  40. lang = self.context.get('lang', 'en_US')
  41. return translate(self.cr, _ir_translation_name, 'report', lang, src) or src
  42. class mis_builder_xls(report_xls):
  43. def __init__(self, name, table, rml=False, parser=False, header=True, store=False):
  44. super(mis_builder_xls, self).__init__(name, table, rml, parser, header, store)
  45. # Cell Styles
  46. _xs = self.xls_styles
  47. # header
  48. rh_cell_format = _xs['bold'] + _xs['fill'] + _xs['borders_all']
  49. self.rh_cell_style = xlwt.easyxf(rh_cell_format)
  50. self.rh_cell_style_center = xlwt.easyxf(rh_cell_format + _xs['center'])
  51. self.rh_cell_style_right = xlwt.easyxf(rh_cell_format + _xs['right'])
  52. # lines
  53. mis_cell_format = _xs['borders_all']
  54. self.mis_cell_style = xlwt.easyxf(mis_cell_format)
  55. self.mis_cell_style_center = xlwt.easyxf(mis_cell_format + _xs['center'])
  56. self.mis_cell_style_date = xlwt.easyxf(mis_cell_format + _xs['left'], num_format_str=report_xls.date_format)
  57. self.mis_cell_style_decimal = xlwt.easyxf(mis_cell_format + _xs['right'], num_format_str=report_xls.decimal_format)
  58. def generate_xls_report(self, _p, _xs, data, objects, wb):
  59. _ = _p._
  60. report_name = objects[0].name
  61. ws = wb.add_sheet(report_name[:31])
  62. ws.panes_frozen = True
  63. ws.remove_splits = True
  64. ws.portrait = 0 # Landscape
  65. ws.fit_width_to_pages = 1
  66. row_pos = 0
  67. # set print header/footer
  68. ws.header_str = self.xls_headers['standard']
  69. ws.footer_str = self.xls_footers['standard']
  70. # Title
  71. cell_style = xlwt.easyxf(_xs['xls_title'])
  72. c_specs = [
  73. ('report_name', 1, 0, 'text', report_name),
  74. ]
  75. row_data = self.xls_row_template(c_specs, ['report_name'])
  76. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=cell_style)
  77. row_pos += 1
  78. # get the computed result
  79. data = self.pool.get('mis.report.instance').compute(self.cr, self.uid, objects[0].id)
  80. # Column headers
  81. header_name_list = ['']
  82. col_specs_template = {'': {'header': [1, 42, 'text', ''],
  83. 'header_date': [1, 42, 'text', '']}}
  84. for col in data['header']['']['cols']:
  85. col_specs_template[col['name']] = {'header': [1, 42, 'text', col['name']],
  86. 'header_date': [1, 42, 'text', col['date']]}
  87. header_name_list.append(col['name'])
  88. c_specs = map(lambda x: self.render(x, col_specs_template, 'header', render_space={'_': _p._}), header_name_list)
  89. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  90. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=self.rh_cell_style, set_column_size=True)
  91. ws.set_horz_split_pos(row_pos)
  92. c_specs = map(lambda x: self.render(x, col_specs_template, 'header_date', render_space={'_': _p._}), header_name_list)
  93. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  94. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=self.rh_cell_style, set_column_size=True)
  95. ws.set_horz_split_pos(row_pos)
  96. for key in data['content']:
  97. line = data['content'][key]
  98. kpi_list = [line['kpi_name']]
  99. col_specs_template = {line['kpi_name']: {'lines': [1, 42, 'text', line['kpi_name']]}}
  100. col_count = 0
  101. for value in line['cols']:
  102. col_count = col_count + 1
  103. kpi_value_name = '_'.join([line['kpi_name'], str(col_count)])
  104. kpi_list.append(kpi_value_name)
  105. if value.get('val'):
  106. col_specs_template[kpi_value_name] = {'lines': [1, 42, 'number', value['val']]}
  107. else:
  108. col_specs_template[kpi_value_name] = {'lines': [1, 42, 'text', value['val_r']]}
  109. c_specs = map(lambda x: self.render(x, col_specs_template, 'lines'), kpi_list)
  110. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  111. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=self.mis_cell_style)
  112. mis_builder_xls('report.mis.report.instance.xls',
  113. 'mis.report.instance',
  114. parser=mis_builder_xls_parser)
  115. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: