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.

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