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.

135 lines
4.8 KiB

  1. # Copyright 2019 Camptocamp SA
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import _, api, exceptions, fields, models
  4. class Agreement(models.Model):
  5. _inherit = 'agreement'
  6. code = fields.Char(required=False)
  7. sale_ids = fields.One2many(
  8. 'sale.order', 'agreement_id', string="Sale Orders"
  9. )
  10. is_sale_agreement = fields.Boolean(
  11. 'Sale Agreement?', compute='_compute_is_sale_agreement', store=True
  12. )
  13. is_customer_requirement = fields.Boolean('Customer requirement?')
  14. @api.depends('sale_ids')
  15. def _compute_is_sale_agreement(self):
  16. for rec in self:
  17. rec.is_sale_agreement = bool(rec.sale_ids)
  18. @api.model
  19. def _get_customer_agreement_fields(self):
  20. """return a list of field names which are related to customer preferences"""
  21. # extend the list in modules implementing specific conditions
  22. return []
  23. @api.model
  24. def _get_sale_agreement_fields(self):
  25. """return a list of field names which are related to sale conditions"""
  26. # extend the list in modules implementing specific conditions
  27. return []
  28. def _get_agreement_fields(self):
  29. return self._get_sale_agreement_fields() + \
  30. self._get_customer_agreement_fields()
  31. def template_prepare_agreement_values(self, sale_order):
  32. self.ensure_one()
  33. assert self.is_template, \
  34. (
  35. 'Programming error: '
  36. 'this method must only be called on a template agreement'
  37. )
  38. fields = ['agreement_type_id'] + self._get_sale_agreement_fields()
  39. tmpl_values = self.read(fields, load='_classic_write')[0]
  40. del tmpl_values['id']
  41. tmpl_values.update(
  42. {
  43. 'name': self.agreement_type_id.name,
  44. 'is_template': False,
  45. 'is_customer_requirement': False
  46. }
  47. )
  48. partner = sale_order.partner_id
  49. customer_settings = partner.customer_agreement_settings_id
  50. assert not customer_settings or \
  51. customer_settings.is_customer_requirement, \
  52. 'customer_settings must be a customer requirement agreement'
  53. if customer_settings:
  54. fields = self._get_customer_agreement_fields()
  55. cust_vals = customer_settings.read(
  56. fields, load='_classic_write'
  57. )[0]
  58. del cust_vals['id']
  59. tmpl_values.update(cust_vals)
  60. tmpl_values['name'] = self.agreement_type.name
  61. return tmpl_values
  62. @api.multi
  63. def write(self, values):
  64. res = super().write(values)
  65. if values.get('agreement_type_id'):
  66. self._check_agreement_type_default_agreement()
  67. return res
  68. @api.model_create_multi
  69. @api.returns('self', lambda value: value.id)
  70. def create(self, vals_list):
  71. res = super().create(vals_list)
  72. res._check_agreement_type_default_agreement()
  73. return res
  74. def _check_agreement_type_default_agreement(self):
  75. # if we are setting an agreement type on a template agreement then the
  76. # template agreement becomes the default agreement of the agreement
  77. # type
  78. template_agreements = self.filtered('is_template')
  79. for template in template_agreements:
  80. agreement_type = template.agreement_type_id
  81. if not agreement_type.default_agreement_id:
  82. agreement_type.default_agreement_id = template
  83. elif agreement_type.default_agreement_id == template:
  84. continue
  85. else:
  86. raise exceptions.UserError(
  87. _('It is forbidden to have 2 templates in the '
  88. 'same agreement type.')
  89. )
  90. def _values_for_clear_defaults(self):
  91. values = {}
  92. for field in self._get_customer_agreement_fields():
  93. values[field] = False
  94. return values
  95. @api.onchange('is_template')
  96. def onchange_is_template(self):
  97. if self.is_template:
  98. self.unset_customer_defaults()
  99. self.partner_id = False
  100. def unset_customer_defaults(self):
  101. values = self._values_for_clear_defaults()
  102. for rec in self:
  103. rec.update(values)
  104. @api.onchange('is_customer_requirement')
  105. def onchange_is_customer(self):
  106. if self.is_customer_requirement:
  107. self.is_template = False
  108. self.set_customer_defaults()
  109. def _get_customer_defaults_values(self):
  110. ParameterValue = self.env['agreement.parameter.value']
  111. values = {}
  112. for field in self._get_customer_agreement_fields():
  113. values[field] = ParameterValue.get_default(field)
  114. return values
  115. def set_customer_defaults(self):
  116. values = self._get_customer_defaults_values()
  117. for rec in self:
  118. rec.update(values)