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.

103 lines
3.7 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. )
  13. sequence = fields.Integer(string="Sequence")
  14. agreement_id = fields.Many2one(
  15. "agreement",
  16. string="Agreement",
  17. ondelete="cascade")
  18. section_id = fields.Many2one(
  19. "agreement.section",
  20. string="Section",
  21. ondelete="cascade"
  22. )
  23. content = fields.Html(string="Clause Content")
  24. dynamic_content = fields.Html(
  25. compute="_compute_dynamic_content",
  26. string="Dynamic Content",
  27. help="compute dynamic Content",
  28. )
  29. active = fields.Boolean(
  30. string="Active",
  31. default=True,
  32. help="If unchecked, it will allow you to hide the agreement without "
  33. "removing it.",
  34. )
  35. # Dynamic field editor
  36. field_id = fields.Many2one(
  37. "ir.model.fields",
  38. string="Field",
  39. help="""Select target field from the related document model. If it is a
  40. relationship field you will be able to select a target field at the
  41. destination of the relationship.""",
  42. )
  43. sub_object_id = fields.Many2one(
  44. "ir.model",
  45. string="Sub-model",
  46. help="""When a relationship field is selected as first field, this
  47. field shows the document model the relationship goes to.""",
  48. )
  49. sub_model_object_field_id = fields.Many2one(
  50. "ir.model.fields",
  51. string="Sub-field",
  52. help="""When a relationship field is selected as first field, this
  53. field lets you select the target field within the destination document
  54. model (sub-model).""",
  55. )
  56. default_value = fields.Char(
  57. string="Default Value",
  58. help="Optional value to use if the target field is empty.",
  59. )
  60. copyvalue = fields.Char(
  61. string="Placeholder Expression",
  62. help="""Final placeholder expression, to be copy-pasted in the desired
  63. template field.""",
  64. )
  65. @api.onchange('field_id', 'sub_model_object_field_id', 'default_value')
  66. def onchange_copyvalue(self):
  67. self.sub_object_id = False
  68. self.copyvalue = False
  69. self.sub_object_id = False
  70. if self.field_id and not self.field_id.relation:
  71. self.copyvalue = "${object.%s or %s}" % \
  72. (self.field_id.name,
  73. self.default_value or '\'\'')
  74. self.sub_model_object_field_id = False
  75. if self.field_id and self.field_id.relation:
  76. self.sub_object_id = self.env['ir.model'].search(
  77. [('model', '=', self.field_id.relation)])[0]
  78. if self.sub_model_object_field_id:
  79. self.copyvalue = "${object.%s.%s or %s}" %\
  80. (self.field_id.name,
  81. self.sub_model_object_field_id.name,
  82. self.default_value or '\'\'')
  83. # compute the dynamic content for mako expression
  84. @api.multi
  85. def _compute_dynamic_content(self):
  86. MailTemplates = self.env["mail.template"]
  87. for clause in self:
  88. lang = (
  89. clause.agreement_id
  90. and clause.agreement_id.partner_id.lang
  91. or "en_US"
  92. )
  93. content = MailTemplates.with_context(lang=lang)._render_template(
  94. clause.content, "agreement.clause", clause.id
  95. )
  96. clause.dynamic_content = content