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.

94 lines
3.6 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 Eficent Business and IT Consulting Services S.L.
  3. # License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
  4. import logging
  5. from openerp.report import report_sxw
  6. from openerp.tools.translate import _
  7. _logger = logging.getLogger(__name__)
  8. try:
  9. from openerp.addons.report_xlsx.report.report_xlsx import ReportXlsx
  10. except ImportError:
  11. _logger.debug("report_xlsx not installed, Excel export non functional")
  12. class ReportXlsx(object):
  13. def __init__(self, *args, **kwargs):
  14. pass
  15. class ReportPartnerXlsx(ReportXlsx):
  16. def _search_longest_row(self, tables):
  17. res = 0
  18. for table in tables:
  19. for model in tables[table]:
  20. if len(tables[table][model]) > 0:
  21. if len(tables[table][model][0]) > res:
  22. res = len(tables[table][model][0])
  23. return res
  24. def generate_xlsx_report(self, workbook, data, objects):
  25. partner = data['form'].get('partner_id', False)
  26. partner = self.env['res.partner'].sudo().browse(partner[0])
  27. workbook.set_properties({
  28. 'comments': 'Created with Python and XlsxWriter from Odoo'})
  29. sheet = workbook.add_worksheet(_('Partner Data'))
  30. sheet.set_landscape()
  31. sheet.fit_to_pages(1, 0)
  32. sheet.set_zoom(75)
  33. sheet.set_column(0, self._search_longest_row(data['tables']), 25)
  34. title_style = workbook.add_format(
  35. {'bold': True, 'bg_color': '#FFFFCC', 'border': 2})
  36. sheet.set_row(0, None, None, {'collapsed': 1})
  37. sheet.write_row(1, 0, ["Partner: " + partner.display_name],
  38. title_style)
  39. i = 3
  40. first_row = i+2
  41. for table in sorted(data['tables'].keys()):
  42. for model in sorted(data['tables'][table].keys()):
  43. rows = len(data['tables'][table][model])
  44. if rows:
  45. style = workbook.add_format()
  46. style.set_bold(True)
  47. style.set_border(2)
  48. sheet.write_row(i, 0, [model], style)
  49. i += 1
  50. j = 0
  51. for column in data['tables'][table][model][0]:
  52. style = workbook.add_format()
  53. style.set_bold(True)
  54. if j == 0:
  55. style.set_left(1)
  56. if j == len(data['tables'][table][model][0]) - 1:
  57. style.set_right(1)
  58. style.set_top(1)
  59. style.set_bottom(1)
  60. sheet.write_row(i, j, [column], style)
  61. j += 1
  62. for row in data['tables'][table][model]:
  63. i += 1
  64. j = 0
  65. for column in row:
  66. style = workbook.add_format()
  67. if j == 0:
  68. style.set_left(1)
  69. if j == len(row) - 1:
  70. style.set_right(1)
  71. if i == rows + first_row - 1:
  72. style.set_bottom(1)
  73. if row[column]:
  74. sheet.write_row(i, j, [row[column]], style)
  75. else:
  76. sheet.write_row(i, j, [''], style)
  77. j += 1
  78. i += 2
  79. first_row = i+2
  80. ReportPartnerXlsx(
  81. 'report.privacy.report_partner_xlsx',
  82. 'privacy.partner.report',
  83. parser=report_sxw.rml_parse
  84. )