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.6 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. )
  26. sheet = workbook.add_worksheet(_("Partner Data"))
  27. sheet.set_landscape()
  28. sheet.fit_to_pages(1, 0)
  29. sheet.set_zoom(75)
  30. sheet.set_column(0, self._search_longest_row(report_data["tables"]), 25)
  31. title_style = workbook.add_format(
  32. {"bold": True, "bg_color": "#FFFFCC", "border": 2}
  33. )
  34. sheet.set_row(0, None, None, {"collapsed": 1})
  35. sheet.write_row(1, 0, ["Partner: " + partner.display_name], 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"][table][model][0]) - 1:
  54. style.set_right(1)
  55. style.set_top(1)
  56. style.set_bottom(1)
  57. sheet.write_row(i, j, [column], style)
  58. j += 1
  59. for row in report_data["tables"][table][model]:
  60. i += 1
  61. j = 0
  62. for column in row:
  63. style = workbook.add_format()
  64. if j == 0:
  65. style.set_left(1)
  66. if j == len(row) - 1:
  67. style.set_right(1)
  68. if i == rows + first_row - 1:
  69. style.set_bottom(1)
  70. if row[column]:
  71. sheet.write_row(i, j, [row[column]], style)
  72. else:
  73. sheet.write_row(i, j, [""], style)
  74. j += 1
  75. i += 2
  76. first_row = i + 2