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.

102 lines
4.1 KiB

  1. # -*- encoding: 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.report import report_sxw
  25. from openerp.addons.base_report_xlsx.report.report_xlsx import ReportXlsx
  26. import logging
  27. _logger = logging.getLogger(__name__)
  28. class mis_builder_xlsx_parser(report_sxw.rml_parse):
  29. def __init__(self, cr, uid, name, context):
  30. super(mis_builder_xlsx_parser, self).__init__(
  31. cr, uid, name, context=context)
  32. self.context = context
  33. class mis_builder_xlsx(ReportXlsx):
  34. def __init__(self, name, table, rml=False, parser=False, header=True,
  35. store=False):
  36. super(mis_builder_xlsx, self).__init__(
  37. name, table, rml, parser, header, store)
  38. def generate_xlsx_report(self, workbook, data, objects):
  39. report_name = objects[0].name
  40. sheet = workbook.add_worksheet(report_name[:31])
  41. row_pos = 0
  42. col_pos = 0
  43. sheet.set_column(col_pos, col_pos, 30)
  44. bold = workbook.add_format({'bold': True})
  45. header_format = workbook.add_format({'bold': True,
  46. 'align': 'center',
  47. 'border': True,
  48. 'bg_color': '#FFFFCC'})
  49. kpi_name_format = workbook.add_format({'bold': True,
  50. 'border': True,
  51. 'bg_color': '#FFFFCC'})
  52. sheet.write(row_pos, 0, report_name, bold)
  53. row_pos += 1
  54. col_pos += 1
  55. # get the computed result of the report
  56. data = objects.compute()
  57. # Column headers
  58. for col in data['header'][0]['cols']:
  59. sheet.set_column(col_pos, col_pos, 30)
  60. sheet.write(row_pos, col_pos, col['name'], header_format)
  61. sheet.write(row_pos+1, col_pos, col['date'], header_format)
  62. col_pos += 1
  63. row_pos += 2
  64. for line in data['content']:
  65. col = 0
  66. sheet.write(row_pos, col, line['kpi_name'], kpi_name_format)
  67. for value in line['cols']:
  68. col += 1
  69. num_format_str = '#'
  70. if value.get('dp'):
  71. num_format_str += '.'
  72. num_format_str += '0' * int(value['dp'])
  73. if value.get('suffix'):
  74. num_format_str = num_format_str + ' "%s"' % value['suffix']
  75. kpi_format = workbook.add_format({'num_format': num_format_str,
  76. 'border': 1,
  77. 'align': 'right'})
  78. if value.get('val'):
  79. val = value['val']
  80. if value.get('is_percentage'):
  81. val = val / 0.01
  82. sheet.write(row_pos, col, val, kpi_format)
  83. else:
  84. sheet.write(row_pos, col, value['val_r'], kpi_format)
  85. row_pos += 1
  86. mis_builder_xlsx('report.mis.report.instance.xlsx',
  87. 'mis.report.instance',
  88. parser=mis_builder_xlsx_parser)