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.

46 lines
1.9 KiB

  1. # Copyright (C) 2019 - TODAY, Open Source Integrators
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import api, fields, models
  4. class SaleOrder(models.Model):
  5. _inherit = 'sale.order'
  6. agreement_template_id = fields.Many2one(
  7. 'agreement',
  8. string="Agreement Template",
  9. domain="[('is_template', '=', True)]")
  10. agreement_id = fields.Many2one('agreement', string="Agreement", copy=False)
  11. @api.multi
  12. def _action_confirm(self):
  13. res = super(SaleOrder, self)._action_confirm()
  14. for order in self:
  15. if order.agreement_template_id:
  16. order.agreement_id = order.agreement_template_id.copy(default={
  17. 'name': order.name,
  18. 'is_template': False,
  19. 'sale_id': order.id,
  20. 'partner_id': order.partner_id.id,
  21. 'analytic_account_id':
  22. order.analytic_account_id and
  23. order.analytic_account_id.id or False,
  24. })
  25. for line in self.order_line:
  26. # Create agreement line
  27. self.env['agreement.line'].create({
  28. 'product_id': line.product_id.id,
  29. 'name': line.name,
  30. 'agreement_id': order.agreement_id.id,
  31. 'qty': line.product_uom_qty,
  32. 'sale_line_id': line.id,
  33. 'uom_id': line.product_uom.id
  34. })
  35. # If the product is a service profile, create one
  36. if line.product_id.product_tmpl_id.is_serviceprofile:
  37. self.env['agreement.serviceprofile'].create({
  38. 'name': line.name,
  39. 'agreement_id': order.agreement_id.id,
  40. })
  41. return res