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.

49 lines
2.0 KiB

  1. # Copyright (C) 2018 - TODAY, Open Source Integrators
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
  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")
  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 sold has a BOM, create a service profile
  36. bom = self.env['mrp.bom'].search(
  37. [('product_tmpl_id', '=',
  38. line.product_id.product_tmpl_id.id)])
  39. if bom:
  40. self.env['agreement.serviceprofile'].create({
  41. 'name': line.name,
  42. 'agreement_id': order.agreement_id.id,
  43. })
  44. return res