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.

85 lines
3.4 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 ACSONE SA/NV (<http://acsone.eu>)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import io
  5. import ast
  6. from odoo import fields, models, api, _
  7. from odoo.exceptions import ValidationError
  8. class ReportHeaderFooter(models.Model):
  9. _name = 'report.xlsx.hf'
  10. name = fields.Char(string="Name", required=True)
  11. hf_type = fields.Selection(
  12. [('header', 'Header'), ('footer', 'Footer')],
  13. string="Type",
  14. required=True)
  15. value = fields.Char(string="Value")
  16. manual_options = fields.Char(string="Options")
  17. image_left = fields.Binary(string='Image left')
  18. image_left_name = fields.Char('File Name')
  19. image_center = fields.Binary(string='Image center')
  20. image_center_name = fields.Char('File Name')
  21. image_right = fields.Binary(string='Image right')
  22. image_right_name = fields.Char('File Name')
  23. header_report_ids = fields.One2many(
  24. 'ir.actions.report.xml',
  25. 'header_id',
  26. string="Associated report(s)")
  27. footer_report_ids = fields.One2many(
  28. 'ir.actions.report.xml',
  29. 'footer_id',
  30. string="Associated report(s)")
  31. @api.multi
  32. @api.constrains('manual_options')
  33. def _check_manual_options(self):
  34. for rec in self:
  35. if rec.manual_options:
  36. options = ast.literal_eval(rec.manual_options)
  37. if not isinstance(options, dict):
  38. raise ValidationError(
  39. _('The Header/Footer is not configured properly.\
  40. Options must be a dictionary.'))
  41. @api.multi
  42. @api.constrains('image_left', 'image_center', 'image_right')
  43. def _check_images(self):
  44. for rec in self:
  45. error = ""
  46. if rec.image_left and ("&L&G" not in rec.value
  47. and "&L&[Picture]" not in rec.value):
  48. error += _('You must specify the control character &L&G or \
  49. &L&[Picture] in the "Value" when you add an "Image left".\n')
  50. if rec.image_center and ("&C&G" not in rec.value
  51. and "&C&[Picture]" not in rec.value):
  52. error += _('You must specify the control character &C&G or \
  53. &C&[Picture] in the "Value" when you add an "Image center".\n')
  54. if rec.image_right and ("&R&G" not in rec.value
  55. and "&R&[Picture]" not in rec.value):
  56. error += _('You must specify the control character &R&G or \
  57. &R&[Picture] in the "Value" when you add an "Image right".\n')
  58. if error:
  59. raise ValidationError(error)
  60. @api.multi
  61. def get_options(self):
  62. self.ensure_one()
  63. options = {}
  64. if self.manual_options:
  65. options = ast.literal_eval(self.manual_options)
  66. if self.image_left:
  67. options['image_left'] = self.image_left_name
  68. options['image_data_left'] = io.BytesIO(
  69. self.image_left.decode('base64'))
  70. if self.image_center:
  71. options['image_center'] = self.image_center_name
  72. options['image_data_center'] = io.BytesIO(
  73. self.image_center.decode('base64'))
  74. if self.image_right:
  75. options['image_right'] = self.image_right_name
  76. options['image_data_right'] = io.BytesIO(
  77. self.image_right.decode('base64'))
  78. return options