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.

123 lines
3.9 KiB

  1. # Copyright 2019 Camptocamp SA
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. import logging
  4. from odoo import api, fields, models
  5. _logger = logging.getLogger(__name__)
  6. class SaleOrder(models.Model):
  7. _inherit = "sale.order"
  8. agreement_type_id = fields.Many2one(
  9. 'agreement.type',
  10. string='Agreement Type',
  11. required=True,
  12. copy=True,
  13. default=lambda s: s.env['agreement.type'].search([], limit=1),
  14. )
  15. agreement_id = fields.Many2one(
  16. domain=[
  17. ('is_template', '=', False),
  18. ('is_customer_requirement', '=', False),
  19. ],
  20. readonly=True,
  21. )
  22. @api.onchange('agreement_type_id')
  23. def onchange_agreement_type(self):
  24. self._update_agreement()
  25. @api.onchange('partner_id')
  26. def onchange_partner_for_agreement(self):
  27. if self.partner_id.default_agreement_type_id:
  28. partner_agreement_type = self.partner_id.default_agreement_type_id
  29. if self.agreement_type_id != partner_agreement_type:
  30. self.agreement_type_id = partner_agreement_type
  31. self._update_agreement()
  32. def _prepare_sale_agreement_values(self):
  33. return {
  34. 'name': self.agreement_type_id.name,
  35. 'partner_id': self.partner_id.id,
  36. 'agreement_type_id': self.agreement_type_id.id,
  37. 'is_template': False,
  38. 'is_customer_requirement': False,
  39. }
  40. def _update_agreement(self):
  41. if not self.partner_id:
  42. return
  43. if not self.agreement_id and self.partner_id:
  44. self.agreement_id = self.env['agreement'].create(
  45. self._prepare_sale_agreement_values()
  46. )
  47. agreement_template = self.agreement_type_id.default_agreement_id
  48. agreement_values = {}
  49. if agreement_template:
  50. if not agreement_template.is_template:
  51. _logger.warn(
  52. 'The default agreement of the agreement type %s '
  53. 'is not a template agreement',
  54. self.agreement_type_id)
  55. else:
  56. agreement_values = \
  57. agreement_template.template_prepare_agreement_values(
  58. self
  59. )
  60. self.agreement_id.write(agreement_values)
  61. @api.model
  62. def create(self, vals):
  63. rec = super().create(vals)
  64. if rec.agreement_id and not rec.agreement_id.code:
  65. rec.agreement_id.code = rec.name
  66. return rec
  67. @api.multi
  68. def unlink(self):
  69. agreements = self.mapped('agreement_id')
  70. res = super().unlink()
  71. agreements.sudo().unlink()
  72. return res
  73. @api.multi
  74. def _action_confirm(self):
  75. self.filtered(lambda r: not r.agreement_id)._update_agreement()
  76. result = super()._action_confirm()
  77. self._apply_agreement()
  78. return result
  79. @api.multi
  80. def copy_data(self, default=None):
  81. if default is None:
  82. default = {}
  83. if 'agreement_id' not in default and self.agreement_id:
  84. new_agreement = self.agreement_id.copy()
  85. default['agreement_id'] = new_agreement.id
  86. return super().copy_data(default=default)
  87. def _apply_agreement(self):
  88. # Nothing to do for now on the SO itself
  89. return
  90. @api.multi
  91. def _prepare_invoice(self):
  92. values = super()._prepare_invoice()
  93. values['agreement_id'] = self.agreement_id.id
  94. return values
  95. class SaleOrderLine(models.Model):
  96. _inherit = 'sale.order.line'
  97. def _prepare_procurement_values(self, group_id=False):
  98. agreement = self.order_id.agreement_id
  99. values = super()._prepare_procurement_values(group_id=group_id)
  100. values['agreement_id'] = agreement.id
  101. # if agreement.delivery_date:
  102. # values['date_planned'] = agreement.delivery_date - timedelta(
  103. # days=self.order_id.company_id.security_lead
  104. # )
  105. return values