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. @api.multi
  11. def _action_confirm(self):
  12. res = super(SaleOrder, self)._action_confirm()
  13. for order in self:
  14. if order.agreement_template_id:
  15. order.agreement_id = order.agreement_template_id.copy(default={
  16. 'name': order.name,
  17. 'code': order.name,
  18. 'is_template': False,
  19. 'sale_id': order.id,
  20. 'partner_id': order.partner_id.id,
  21. 'analytic_account_id': order.analytic_account_id and
  22. order.analytic_account_id.id or False,
  23. })
  24. for line in order.order_line:
  25. # Create agreement line
  26. self.env['agreement.line'].create({
  27. 'product_id': line.product_id.id,
  28. 'name': line.name,
  29. 'agreement_id': order.agreement_id.id,
  30. 'qty': line.product_uom_qty,
  31. 'sale_line_id': line.id,
  32. 'uom_id': line.product_uom.id
  33. })
  34. # If the product creates service profiles, create one
  35. if line.product_id.product_tmpl_id.is_serviceprofile:
  36. self.env['agreement.serviceprofile'].create({
  37. 'name': line.name,
  38. 'product_id': line.product_id.product_tmpl_id.id,
  39. 'agreement_id': order.agreement_id.id,
  40. })
  41. return res