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.

117 lines
5.4 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 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. _ir_translation_name = 'mis.report.instance.xls'
  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__(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, store=False):
  35. super(mis_builder_xls, self).__init__(name, table, rml, parser, header, store)
  36. # Cell Styles
  37. _xs = self.xls_styles
  38. # header
  39. rh_cell_format = _xs['bold'] + _xs['fill'] + _xs['borders_all'] + _xs['right']
  40. self.rh_cell_style = xlwt.easyxf(rh_cell_format)
  41. self.rh_cell_style_date = xlwt.easyxf(rh_cell_format, num_format_str=report_xls.date_format)
  42. # lines
  43. self.mis_rh_cell_style = xlwt.easyxf(_xs['borders_all'] + _xs['bold'] + _xs['fill'])
  44. self.mis_cell_style = xlwt.easyxf(_xs['borders_all'] + _xs['right'], num_format_str=report_xls.decimal_format)
  45. def generate_xls_report(self, _p, _xs, data, objects, wb):
  46. report_name = objects[0].name
  47. ws = wb.add_sheet(report_name[:31])
  48. ws.panes_frozen = True
  49. ws.remove_splits = True
  50. ws.portrait = 0 # Landscape
  51. ws.fit_width_to_pages = 1
  52. row_pos = 0
  53. # set print header/footer
  54. ws.header_str = self.xls_headers['standard']
  55. ws.footer_str = self.xls_footers['standard']
  56. # Title
  57. c_specs = [
  58. ('report_name', 1, 0, 'text', report_name),
  59. ]
  60. row_data = self.xls_row_template(c_specs, ['report_name'])
  61. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=xlwt.easyxf(_xs['xls_title']))
  62. row_pos += 1
  63. # get the computed result of the report
  64. data = self.pool.get('mis.report.instance').compute(self.cr, self.uid, objects[0].id)
  65. # Column headers
  66. header_name_list = ['']
  67. col_specs_template = {'': {'header': [1, 30, 'text', ''],
  68. 'header_date': [1, 1, 'text', '']}}
  69. for col in data['header']['']['cols']:
  70. col_specs_template[col['name']] = {'header': [1, 30, 'text', col['name']],
  71. 'header_date': [1, 1, 'text', col['date']]}
  72. header_name_list.append(col['name'])
  73. c_specs = map(lambda x: self.render(x, col_specs_template, 'header'), header_name_list)
  74. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  75. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=self.rh_cell_style, set_column_size=True)
  76. c_specs = map(lambda x: self.render(x, col_specs_template, 'header_date'), header_name_list)
  77. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  78. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=self.rh_cell_style_date)
  79. ws.set_horz_split_pos(row_pos)
  80. ws.set_vert_split_pos(1)
  81. for key in data['content']:
  82. line = data['content'][key]
  83. col = 0
  84. ws.write(row_pos, col, line['kpi_name'], self.mis_rh_cell_style)
  85. for value in line['cols']:
  86. col += 1
  87. kpi_cell_style = self.mis_cell_style
  88. if value.get('suffix'):
  89. kpi_cell_style = xlwt.easyxf(_xs['borders_all'] + _xs['right'], num_format_str='#,##0.00" %s"' % value['suffix'])
  90. if value.get('val'):
  91. ws.write(row_pos, col, value['val'], kpi_cell_style)
  92. else:
  93. ws.write(row_pos, col, value['val_r'], kpi_cell_style)
  94. row_pos += 1
  95. mis_builder_xls('report.mis.report.instance.xls',
  96. 'mis.report.instance',
  97. parser=mis_builder_xls_parser)
  98. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: