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.

115 lines
4.7 KiB

9 years ago
9 years ago
9 years ago
9 years ago
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # mis_builder module for Odoo, Management Information System Builder
  5. # Copyright (C) 2014-2015 ACSONE SA/NV (<http://acsone.eu>)
  6. #
  7. # This file is a part of mis_builder
  8. #
  9. # mis_builder is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU Affero General Public License v3 or later
  11. # as published by the Free Software Foundation, either version 3 of the
  12. # License, or (at your option) any later version.
  13. #
  14. # mis_builder is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU Affero General Public License v3 or later for more details.
  18. #
  19. # You should have received a copy of the GNU Affero General Public License
  20. # v3 or later along with this program.
  21. # If not, see <http://www.gnu.org/licenses/>.
  22. #
  23. ##############################################################################
  24. from openerp.addons.report_xlsx.report.report_xlsx import ReportXlsx
  25. from openerp.report import report_sxw
  26. import logging
  27. _logger = logging.getLogger(__name__)
  28. class MisBuilderXslx(ReportXlsx):
  29. def __init__(self, name, table, rml=False, parser=False, header=True,
  30. store=False):
  31. super(MisBuilderXslx, self).__init__(
  32. name, table, rml, parser, header, store)
  33. def generate_xlsx_report(self, workbook, data, objects):
  34. report_name = objects[0].name
  35. sheet = workbook.add_worksheet(report_name[:31])
  36. row_pos = 0
  37. col_pos = 0
  38. sheet.set_column(col_pos, col_pos, 30)
  39. bold = workbook.add_format({'bold': True})
  40. header_format = workbook.add_format({'bold': True,
  41. 'align': 'center',
  42. 'border': True,
  43. 'bg_color': '#FFFFCC'})
  44. kpi_name_format = workbook.add_format({'bold': True,
  45. 'border': True,
  46. 'bg_color': '#FFFFCC'})
  47. sheet.write(row_pos, 0, report_name, bold)
  48. row_pos += 1
  49. col_pos += 1
  50. # get the computed result of the report
  51. data = objects.compute()
  52. # Column headers
  53. for header in data['header']:
  54. has_col_date = False
  55. for col in header['cols']:
  56. colspan = col['colspan']
  57. col_date = col.get('date')
  58. col_name = col['name']
  59. has_col_date = has_col_date or col_date
  60. if colspan > 1:
  61. sheet.merge_range(
  62. row_pos, col_pos, row_pos, col_pos + colspan-1,
  63. col_name, header_format)
  64. if col_date:
  65. sheet.merge_range(
  66. row_pos+1, col_pos, row_pos+1, col_pos + colspan-1,
  67. col_date, header_format)
  68. col_pos += colspan
  69. else:
  70. sheet.write(row_pos, col_pos, col['name'], header_format)
  71. if col_date:
  72. sheet.write(
  73. row_pos+1, col_pos, col.get('date'), header_format)
  74. col_pos += 1
  75. col_pos = 1
  76. row_pos += 1
  77. if has_col_date:
  78. row_pos += 1
  79. for line in data['content']:
  80. col = 0
  81. sheet.write(row_pos, col, line['kpi_name'], kpi_name_format)
  82. for value in line['cols']:
  83. col += 1
  84. num_format_str = '#'
  85. if value.get('dp'):
  86. num_format_str += '.'
  87. num_format_str += '0' * int(value['dp'])
  88. if value.get('prefix'):
  89. num_format_str = '"%s"' % value['prefix'] + num_format_str
  90. if value.get('suffix'):
  91. num_format_str = num_format_str + ' "%s"' % value['suffix']
  92. kpi_format = workbook.add_format({'num_format': num_format_str,
  93. 'border': 1,
  94. 'align': 'right'})
  95. if value.get('val'):
  96. val = value['val']
  97. if value.get('is_percentage'):
  98. val = val / 0.01
  99. sheet.write(row_pos, col, val, kpi_format)
  100. else:
  101. sheet.write(row_pos, col, value['val_r'], kpi_format)
  102. row_pos += 1
  103. MisBuilderXslx('report.mis.report.instance.xlsx',
  104. 'mis.report.instance', parser=report_sxw.rml_parse)