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.

40 lines
1.4 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 LasLabs Inc.
  3. # Copyright 2019 Therp BV <https://therp.nl>.
  4. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  5. # pylint: disable=missing-docstring,protected-access
  6. from odoo import api, fields, models
  7. class SaleOrderLine(models.Model):
  8. _inherit = 'sale.order.line'
  9. contract_id = fields.Many2one(
  10. comodel_name='account.analytic.account',
  11. string='Contract')
  12. @api.multi
  13. def create_contract(self):
  14. """Create contract for sale order line.
  15. Should be called on confirmation of sale order.
  16. """
  17. for line in self.filtered('product_id.is_contract'):
  18. contract_vals = line._prepare_contract_vals()
  19. contract = self.env['account.analytic.account'].create(
  20. contract_vals)
  21. line.contract_id = contract.id
  22. @api.multi
  23. def _prepare_contract_vals(self):
  24. """Prepare values to create contract from template."""
  25. self.ensure_one()
  26. contract_tmpl = self.product_id.contract_template_id
  27. order = self.order_id
  28. contract = self.env['account.analytic.account'].new({
  29. 'name': '%s Contract' % order.name,
  30. 'partner_id': order.partner_id.id,
  31. 'contract_template_id': contract_tmpl.id,
  32. 'recurring_invoices': True})
  33. contract._onchange_contract_template_id()
  34. return contract._convert_to_write(contract._cache)