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.

140 lines
5.3 KiB

9 years ago
9 years ago
9 years ago
9 years ago
  1. # -*- coding: utf-8 -*-
  2. # © 2014-2016 ACSONE SA/NV (<http://acsone.eu>)
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  4. from collections import defaultdict
  5. import logging
  6. from openerp.report import report_sxw
  7. from ..models.accounting_none import AccountingNone
  8. from ..models.data_error import DataError
  9. _logger = logging.getLogger(__name__)
  10. try:
  11. from openerp.addons.report_xlsx.report.report_xlsx import ReportXlsx
  12. except ImportError:
  13. _logger.debug("report_xlsx not installed, Excel export non functional")
  14. class ReportXslx:
  15. pass
  16. ROW_HEIGHT = 15 # xlsxwriter units
  17. COL_WIDTH = 0.9 # xlsxwriter units
  18. MIN_COL_WIDTH = 10 # characters
  19. MAX_COL_WIDTH = 50 # characters
  20. class MisBuilderXslx(ReportXlsx):
  21. def __init__(self, name, table, rml=False, parser=False, header=True,
  22. store=False):
  23. super(MisBuilderXslx, self).__init__(
  24. name, table, rml, parser, header, store)
  25. def generate_xlsx_report(self, workbook, data, objects):
  26. # get the computed result of the report
  27. matrix = objects._compute_matrix()
  28. style_obj = self.env['mis.report.style']
  29. # create worksheet
  30. report_name = '{} - {}'.format(
  31. objects[0].name, objects[0].company_id.name)
  32. sheet = workbook.add_worksheet(report_name[:31])
  33. row_pos = 0
  34. col_pos = 0
  35. # width of the labels column
  36. label_col_width = MIN_COL_WIDTH
  37. # {col_pos: max width in characters}
  38. col_width = defaultdict(lambda: MIN_COL_WIDTH)
  39. # document title
  40. bold = workbook.add_format({'bold': True})
  41. header_format = workbook.add_format({
  42. 'bold': True, 'align': 'center', 'bg_color': '#F0EEEE'})
  43. sheet.write(row_pos, 0, report_name, bold)
  44. row_pos += 2
  45. # column headers
  46. sheet.write(row_pos, 0, '', header_format)
  47. col_pos = 1
  48. for col in matrix.iter_cols():
  49. label = col.label
  50. if col.description:
  51. label += '\n' + col.description
  52. sheet.set_row(row_pos, ROW_HEIGHT * 2)
  53. if col.colspan > 1:
  54. sheet.merge_range(
  55. row_pos, col_pos, row_pos,
  56. col_pos + col.colspan-1,
  57. label, header_format)
  58. else:
  59. sheet.write(row_pos, col_pos, label, header_format)
  60. col_width[col_pos] = max(col_width[col_pos],
  61. len(col.label or ''),
  62. len(col.description or ''))
  63. col_pos += col.colspan
  64. row_pos += 1
  65. # sub column headers
  66. sheet.write(row_pos, 0, '', header_format)
  67. col_pos = 1
  68. for subcol in matrix.iter_subcols():
  69. label = subcol.label
  70. if subcol.description:
  71. label += '\n' + subcol.description
  72. sheet.set_row(row_pos, ROW_HEIGHT * 2)
  73. sheet.write(row_pos, col_pos, label, header_format)
  74. col_width[col_pos] = max(col_width[col_pos],
  75. len(subcol.label or ''),
  76. len(subcol.description or ''))
  77. col_pos += 1
  78. row_pos += 1
  79. # rows
  80. for row in matrix.iter_rows():
  81. row_xlsx_style = style_obj.to_xlsx_style(row.style_props)
  82. row_format = workbook.add_format(row_xlsx_style)
  83. col_pos = 0
  84. label = row.label
  85. if row.description:
  86. label += '\n' + row.description
  87. sheet.set_row(row_pos, ROW_HEIGHT * 2)
  88. sheet.write(row_pos, col_pos, label, row_format)
  89. label_col_width = max(label_col_width,
  90. len(row.label or ''),
  91. len(row.description or ''))
  92. for cell in row.iter_cells():
  93. col_pos += 1
  94. if not cell or cell.val is AccountingNone:
  95. # TODO col/subcol format
  96. sheet.write(row_pos, col_pos, '', row_format)
  97. continue
  98. cell_xlsx_style = style_obj.to_xlsx_style(cell.style_props)
  99. cell_xlsx_style['align'] = 'right'
  100. cell_format = workbook.add_format(cell_xlsx_style)
  101. if isinstance(cell.val, DataError):
  102. val = cell.val.name
  103. # TODO display cell.val.msg as Excel comment?
  104. elif cell.val is None or cell.val is AccountingNone:
  105. val = ''
  106. else:
  107. val = cell.val / float(cell.style_props.get('divider', 1))
  108. sheet.write(row_pos, col_pos, val, cell_format)
  109. col_width[col_pos] = max(col_width[col_pos],
  110. len(cell.val_rendered or ''))
  111. row_pos += 1
  112. # adjust col widths
  113. sheet.set_column(0, 0, min(label_col_width, MAX_COL_WIDTH) * COL_WIDTH)
  114. data_col_width = min(MAX_COL_WIDTH, max(*col_width.values()))
  115. min_col_pos = min(*col_width.keys())
  116. max_col_pos = max(*col_width.keys())
  117. sheet.set_column(min_col_pos, max_col_pos, data_col_width * COL_WIDTH)
  118. MisBuilderXslx('report.mis.report.instance.xlsx',
  119. 'mis.report.instance', parser=report_sxw.rml_parse)