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.

83 lines
3.7 KiB

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