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.

116 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. 'bg_color': '#F0EEEE'})
  43. sheet.write(row_pos, 0, report_name, bold)
  44. row_pos += 1
  45. col_pos += 1
  46. # get the computed result of the report
  47. data = objects.compute()
  48. # Column headers
  49. for header in data['header']:
  50. has_col_date = False
  51. for col in header['cols']:
  52. colspan = col['colspan']
  53. col_date = col.get('date')
  54. col_name = col['name']
  55. has_col_date = has_col_date or col_date
  56. if colspan > 1:
  57. sheet.merge_range(
  58. row_pos, col_pos, row_pos, col_pos + colspan-1,
  59. col_name, header_format)
  60. if col_date:
  61. sheet.merge_range(
  62. row_pos+1, col_pos, row_pos+1, col_pos + colspan-1,
  63. col_date, header_format)
  64. col_pos += colspan
  65. else:
  66. sheet.write(row_pos, col_pos, col['name'], header_format)
  67. if col_date:
  68. sheet.write(
  69. row_pos+1, col_pos, col.get('date'), header_format)
  70. col_pos += 1
  71. col_pos = 1
  72. row_pos += 1
  73. if has_col_date:
  74. row_pos += 1
  75. for line in data['content']:
  76. row_xlsx_syle = line.get('default_xlsx_style', {})
  77. row_format = workbook.add_format(row_xlsx_syle)
  78. col = 0
  79. sheet.write(row_pos, col, line['kpi_name'], row_format)
  80. for value in line['cols']:
  81. col += 1
  82. num_format_str = '#'
  83. if value.get('dp'):
  84. num_format_str += '.'
  85. num_format_str += '0' * int(value['dp'])
  86. if value.get('prefix'):
  87. num_format_str = '"%s"' % value['prefix'] + num_format_str
  88. if value.get('suffix'):
  89. num_format_str = num_format_str + ' "%s"' % value['suffix']
  90. kpi_xlsx_syle = value.get('xlsx_style', {}) or row_xlsx_syle
  91. kpi_xlsx_syle.update({
  92. 'num_format': num_format_str,
  93. 'align': 'right'
  94. })
  95. kpi_format = workbook.add_format(kpi_xlsx_syle)
  96. if value.get('val'):
  97. val = value['val']
  98. if value.get('is_percentage'):
  99. val = val / 0.01
  100. sheet.write(row_pos, col, val, kpi_format)
  101. else:
  102. sheet.write(row_pos, col, value['val_r'], kpi_format)
  103. row_pos += 1
  104. MisBuilderXslx('report.mis.report.instance.xlsx',
  105. 'mis.report.instance', parser=report_sxw.rml_parse)