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.3 KiB

6 years ago
6 years ago
6 years ago
  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(string="Title",
  10. help="The title is displayed on the PDF."
  11. "The name is not.")
  12. sequence = fields.Integer(string="Sequence")
  13. agreement_id = fields.Many2one(
  14. 'agreement',
  15. string="Agreement",
  16. ondelete="cascade"
  17. )
  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. active = fields.Boolean(
  29. string="Active",
  30. default=True,
  31. help="If unchecked, it will allow you to hide the agreement without "
  32. "removing it.")
  33. # Dynamic field editor
  34. field_id = fields.Many2one(
  35. 'ir.model.fields', string="Field",
  36. help="""Select target field from the related document model. If it is a
  37. relationship field you will be able to select a target field at the
  38. destination of the relationship.""")
  39. sub_object_id = fields.Many2one(
  40. 'ir.model', 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', string="Sub-field",
  45. help="""When a relationship field is selected as first field, this
  46. field lets you select the target field within the destination document
  47. model (sub-model).""")
  48. default_value = fields.Char(
  49. string="Default Value",
  50. help="Optional value to use if the target field is empty.")
  51. copyvalue = fields.Char(
  52. string="Placeholder Expression",
  53. help="""Final placeholder expression, to be copy-pasted in the desired
  54. template field.""")
  55. @api.onchange('field_id')
  56. def onchange_sub_object_id(self):
  57. if self.field_id and self.field_id.relation:
  58. self.sub_object_id = self.env['ir.model'].search(
  59. [('model', '=', self.field_id.relation)])[0]
  60. @api.onchange('sub_model_object_field_id', 'default_value')
  61. def onchange_copyvalue(self):
  62. if self.sub_model_object_field_id or self.default_value:
  63. self.copyvalue = "${object.%s.%s or %s}" % \
  64. (self.field_id.name,
  65. self.sub_model_object_field_id.name,
  66. self.default_value or '\'\'')
  67. # compute the dynamic content for mako expression
  68. @api.multi
  69. def _compute_dynamic_content(self):
  70. MailTemplates = self.env['mail.template']
  71. for clause in self:
  72. lang = clause.agreement_id and \
  73. clause.agreement_id.partner_id.lang or 'en_US'
  74. content = MailTemplates.with_context(
  75. lang=lang).render_template(
  76. clause.content, 'agreement.clause', clause.id)
  77. clause.dynamic_content = content