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.

61 lines
2.6 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
  42. def action_confirm(self):
  43. # If sale_timesheet is installed, the _action_confirm()
  44. # may be setting an Analytic Account on the SO.
  45. # But since it is not a dependency, that can happen after
  46. # we create the Agreement.
  47. # To work around that, we check if that is the case,
  48. # and make sure the SO Analytic Account is copied to the Agreement.
  49. res = super(SaleOrder, self).action_confirm()
  50. for order in self:
  51. agreement = order.agreement_id
  52. if (order.analytic_account_id and agreement and
  53. not agreement.analytic_account_id):
  54. agreement.analytic_account_id = order.analytic_account_id
  55. return res