OCA reporting engine fork for dev and update.
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.

59 lines
2.2 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015 ACSONE SA/NV (<http://acsone.eu>)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from cStringIO import StringIO
  5. from odoo.report.report_sxw import report_sxw
  6. from odoo.api import Environment
  7. import logging
  8. _logger = logging.getLogger(__name__)
  9. try:
  10. import xlsxwriter
  11. except ImportError:
  12. _logger.debug('Can not import xlsxwriter`.')
  13. class ReportXlsx(report_sxw):
  14. def create(self, cr, uid, ids, data, context=None):
  15. self.env = Environment(cr, uid, context)
  16. report_obj = self.env['ir.actions.report.xml']
  17. report = report_obj.search([('report_name', '=', self.name[7:])])
  18. if report.ids:
  19. self.title = report.name
  20. if report.report_type == 'xlsx':
  21. return self.create_xlsx_report(ids, data, report)
  22. return super(ReportXlsx, self).create(cr, uid, ids, data, context)
  23. def create_workbook(self, file_data, data, objs, report):
  24. workbook = xlsxwriter.Workbook(file_data, self.get_workbook_options())
  25. self.generate_xlsx_report(workbook, data, objs)
  26. for sheet in workbook.worksheets():
  27. if report and report.header_id and report.header_id.value:
  28. sheet.set_header(report.header_id.value,
  29. report.header_id.get_options())
  30. if report and report.footer_id and report.footer_id.value:
  31. sheet.set_footer(report.footer_id.value,
  32. report.footer_id.get_options())
  33. return workbook
  34. def create_xlsx_report(self, ids, data, report):
  35. self.parser_instance = self.parser(
  36. self.env.cr, self.env.uid, self.name2, self.env.context)
  37. objs = self.getObjects(
  38. self.env.cr, self.env.uid, ids, self.env.context)
  39. self.parser_instance.set_context(objs, data, ids, 'xlsx')
  40. file_data = StringIO()
  41. workbook = self.create_workbook(file_data, data, objs, report)
  42. workbook.close()
  43. file_data.seek(0)
  44. return (file_data.read(), 'xlsx')
  45. def get_workbook_options(self):
  46. return {}
  47. def generate_xlsx_report(self, workbook, data, objs):
  48. raise NotImplementedError()