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.

120 lines
4.6 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
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. # -*- coding: utf-8 -*-
  2. # © 2014-2015 ACSONE SA/NV (<http://acsone.eu>)
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  4. import xlwt
  5. from openerp.report import report_sxw
  6. from openerp.addons.report_xls.report_xls import report_xls
  7. import logging
  8. _logger = logging.getLogger(__name__)
  9. class MisBuilderXlsParser(report_sxw.rml_parse):
  10. def __init__(self, cr, uid, name, context):
  11. super(MisBuilderXlsParser, self).__init__(
  12. cr, uid, name, context=context)
  13. self.context = context
  14. class MisBuilderXls(report_xls):
  15. def __init__(self, name, table, rml=False, parser=False, header=True,
  16. store=False):
  17. super(MisBuilderXls, self).__init__(
  18. name, table, rml, parser, header, store)
  19. # Cell Styles
  20. _xs = self.xls_styles
  21. # header
  22. rh_cell_format = _xs['bold'] + _xs['fill'] + \
  23. _xs['borders_all'] + _xs['right']
  24. self.rh_cell_style = xlwt.easyxf(rh_cell_format)
  25. self.rh_cell_style_date = xlwt.easyxf(
  26. rh_cell_format, num_format_str=report_xls.date_format)
  27. # lines
  28. self.mis_rh_cell_style = xlwt.easyxf(
  29. _xs['borders_all'] + _xs['bold'] + _xs['fill'])
  30. def generate_xls_report(self, _p, _xs, data, objects, wb):
  31. report_name = objects[0].name
  32. ws = wb.add_sheet(report_name[:31])
  33. ws.panes_frozen = True
  34. ws.remove_splits = True
  35. ws.portrait = 0 # Landscape
  36. ws.fit_width_to_pages = 1
  37. row_pos = 0
  38. # set print header/footer
  39. ws.header_str = self.xls_headers['standard']
  40. ws.footer_str = self.xls_footers['standard']
  41. # Title
  42. c_specs = [
  43. ('report_name', 1, 0, 'text', report_name),
  44. ]
  45. row_data = self.xls_row_template(c_specs, ['report_name'])
  46. row_pos = self.xls_write_row(
  47. ws, row_pos, row_data, row_style=xlwt.easyxf(_xs['xls_title']))
  48. row_pos += 1
  49. # get the computed result of the report
  50. data = self.pool.get('mis.report.instance').compute(
  51. self.cr, self.uid, objects[0].id, self.context)
  52. # Column headers
  53. header_name_list = ['']
  54. col_specs_template = {'': {'header': [1, 30, 'text', ''],
  55. 'header_date': [1, 1, 'text', '']}}
  56. for col in data['header'][0]['cols']:
  57. col_specs_template[col['name']] = {'header': [1, 30, 'text',
  58. col['name']],
  59. 'header_date': [1, 1, 'text',
  60. col['date']]}
  61. header_name_list.append(col['name'])
  62. c_specs = map(
  63. lambda x: self.render(x, col_specs_template, 'header'),
  64. header_name_list)
  65. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  66. row_pos = self.xls_write_row(
  67. ws, row_pos, row_data, row_style=self.rh_cell_style,
  68. set_column_size=True)
  69. c_specs = map(lambda x: self.render(
  70. x, col_specs_template, 'header_date'), header_name_list)
  71. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  72. row_pos = self.xls_write_row(
  73. ws, row_pos, row_data, row_style=self.rh_cell_style_date)
  74. ws.set_horz_split_pos(row_pos)
  75. ws.set_vert_split_pos(1)
  76. for line in data['content']:
  77. col = 0
  78. ws.write(row_pos, col, line['kpi_name'], self.mis_rh_cell_style)
  79. for value in line['cols']:
  80. col += 1
  81. num_format_str = '#'
  82. if value.get('dp'):
  83. num_format_str += '.'
  84. num_format_str += '0' * int(value['dp'])
  85. if value.get('prefix'):
  86. num_format_str = '"%s"' % value['prefix'] + num_format_str
  87. if value.get('suffix'):
  88. num_format_str += ' "%s"' % value['suffix']
  89. kpi_cell_style = xlwt.easyxf(
  90. _xs['borders_all'] + _xs['right'],
  91. 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. MisBuilderXls('report.mis.report.instance.xls',
  101. 'mis.report.instance',
  102. parser=MisBuilderXlsParser)