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.

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