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.

118 lines
4.5 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. # Copyright 2015-2020 Onestein (<https://www.onestein.eu>)
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. from odoo import _, api, fields, models
  4. from odoo.exceptions import ValidationError
  5. class BveViewLine(models.Model):
  6. _name = "bve.view.line"
  7. _description = "BI View Editor Lines"
  8. name = fields.Char(compute="_compute_name")
  9. sequence = fields.Integer(default=1)
  10. bve_view_id = fields.Many2one("bve.view", ondelete="cascade")
  11. model_id = fields.Many2one("ir.model")
  12. model_name = fields.Char(related="model_id.model", store=True, string="Model Name")
  13. table_alias = fields.Char(required=True)
  14. join_model_id = fields.Many2one("ir.model")
  15. field_id = fields.Many2one("ir.model.fields")
  16. field_name = fields.Char(compute="_compute_model_field_name", store=True)
  17. ttype = fields.Char(string="Type")
  18. description = fields.Char(translate=True)
  19. relation = fields.Char()
  20. join_node = fields.Char()
  21. left_join = fields.Boolean()
  22. row = fields.Boolean()
  23. column = fields.Boolean()
  24. measure = fields.Boolean()
  25. in_list = fields.Boolean()
  26. list_attr = fields.Selection(
  27. [("sum", "Sum"), ("avg", "Average")], string="List Attribute", default="sum"
  28. )
  29. view_field_type = fields.Char(compute="_compute_view_field_type")
  30. @api.depends("row", "column", "measure")
  31. def _compute_view_field_type(self):
  32. for line in self:
  33. row = line.row and "row"
  34. column = line.column and "col"
  35. measure = line.measure and "measure"
  36. line.view_field_type = row or column or measure
  37. @api.constrains("row", "column", "measure")
  38. def _constrains_options_check(self):
  39. measure_types = ["float", "integer", "monetary"]
  40. for line in self.filtered(lambda l: l.row or l.column):
  41. if line.join_model_id or line.ttype in measure_types:
  42. err_msg = _("This field cannot be a row or a column.")
  43. raise ValidationError(err_msg)
  44. for line in self.filtered(lambda l: l.measure):
  45. if line.join_model_id or line.ttype not in measure_types:
  46. err_msg = _("This field cannot be a measure.")
  47. raise ValidationError(err_msg)
  48. @api.constrains("table_alias", "field_id")
  49. def _constrains_unique_fields_check(self):
  50. seen = set()
  51. for line in self.mapped("bve_view_id.field_ids"):
  52. if (
  53. line.table_alias,
  54. line.field_id.id,
  55. ) not in seen:
  56. seen.add(
  57. (
  58. line.table_alias,
  59. line.field_id.id,
  60. )
  61. )
  62. else:
  63. raise ValidationError(
  64. _("Field %s/%s is duplicated.\n" "Please remove the duplications.")
  65. % (line.field_id.model, line.field_id.name)
  66. )
  67. @api.depends("field_id", "sequence")
  68. def _compute_name(self):
  69. for line in self:
  70. line.name = False
  71. if line.field_id:
  72. line.name = "x_bve_{}_{}".format(line.table_alias, line.field_id.name)
  73. @api.depends("field_id")
  74. def _compute_model_field_name(self):
  75. for line in self:
  76. line.field_name = False
  77. if line.field_id:
  78. line.field_name = "{} ({})".format(line.description, line.model_name)
  79. def _prepare_field_vals(self):
  80. vals_list = []
  81. for line in self:
  82. field = line.field_id
  83. vals = {
  84. "name": line.name,
  85. "complete_name": field.complete_name,
  86. "model": line.bve_view_id.model_name,
  87. "relation": field.relation,
  88. "field_description": line.description,
  89. "ttype": field.ttype,
  90. "selection": field.selection,
  91. "size": field.size,
  92. "state": "manual",
  93. "readonly": True,
  94. "groups": [(6, 0, field.groups.ids)],
  95. }
  96. if vals["ttype"] == "monetary":
  97. vals.update({"ttype": "float"})
  98. if field.ttype == "selection" and not field.selection:
  99. model_obj = self.env[field.model_id.model]
  100. selection = model_obj._fields[field.name].selection
  101. if callable(selection):
  102. selection_domain = selection(model_obj)
  103. else:
  104. selection_domain = selection
  105. vals.update({"selection": str(selection_domain)})
  106. vals_list.append(vals)
  107. return vals_list