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. # Copyright (C) 2018 - TODAY, Pavlov Media
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import api, fields, models
  4. class AgreementClause(models.Model):
  5. _name = "agreement.clause"
  6. _description = "Agreement Clauses"
  7. _order = "sequence"
  8. name = fields.Char(string="Name", required=True)
  9. title = fields.Char(
  10. string="Title",
  11. help="The title is displayed on the PDF." "The name is not.")
  12. sequence = fields.Integer(string="Sequence")
  13. agreement_id = fields.Many2one(
  14. "agreement",
  15. string="Agreement",
  16. ondelete="cascade")
  17. section_id = fields.Many2one(
  18. "agreement.section",
  19. string="Section",
  20. ondelete="cascade")
  21. content = fields.Html(string="Clause Content")
  22. dynamic_content = fields.Html(
  23. compute="_compute_dynamic_content",
  24. string="Dynamic Content",
  25. help="compute dynamic Content")
  26. active = fields.Boolean(
  27. string="Active",
  28. default=True,
  29. help="If unchecked, it will allow you to hide the agreement without "
  30. "removing it.")
  31. # Dynamic field editor
  32. field_id = fields.Many2one(
  33. "ir.model.fields",
  34. string="Field",
  35. help="""Select target field from the related document model. If it is a
  36. relationship field you will be able to select a target field at the
  37. destination of the relationship.""")
  38. sub_object_id = fields.Many2one(
  39. "ir.model",
  40. string="Sub-model",
  41. help="""When a relationship field is selected as first field, this
  42. field shows the document model the relationship goes to.""")
  43. sub_model_object_field_id = fields.Many2one(
  44. "ir.model.fields",
  45. string="Sub-field",
  46. help="""When a relationship field is selected as first field, this
  47. field lets you select the target field within the destination document
  48. model (sub-model).""")
  49. default_value = fields.Char(
  50. string="Default Value",
  51. help="Optional value to use if the target field is empty.")
  52. copyvalue = fields.Char(
  53. string="Placeholder Expression",
  54. help="""Final placeholder expression, to be copy-pasted in the desired
  55. template field.""")
  56. @api.onchange('field_id', 'sub_model_object_field_id', 'default_value')
  57. def onchange_copyvalue(self):
  58. self.sub_object_id = False
  59. self.copyvalue = False
  60. self.sub_object_id = False
  61. if self.field_id and not self.field_id.relation:
  62. self.copyvalue = "${object.%s or %s}" % \
  63. (self.field_id.name,
  64. self.default_value or '\'\'')
  65. self.sub_model_object_field_id = False
  66. if self.field_id and self.field_id.relation:
  67. self.sub_object_id = self.env['ir.model'].search(
  68. [('model', '=', self.field_id.relation)])[0]
  69. if self.sub_model_object_field_id:
  70. self.copyvalue = "${object.%s.%s or %s}" %\
  71. (self.field_id.name,
  72. self.sub_model_object_field_id.name,
  73. self.default_value or '\'\'')
  74. # compute the dynamic content for mako expression
  75. @api.multi
  76. def _compute_dynamic_content(self):
  77. MailTemplates = self.env["mail.template"]
  78. for clause in self:
  79. lang = (
  80. clause.agreement_id
  81. and clause.agreement_id.partner_id.lang
  82. or "en_US"
  83. )
  84. content = MailTemplates.with_context(lang=lang)._render_template(
  85. clause.content, "agreement.clause", clause.id
  86. )
  87. clause.dynamic_content = content