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.

75 lines
2.8 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 LasLabs Inc.
  3. # Copyright 2018 ACSONE SA/NV.
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from odoo import fields, api, models
  6. class SaleOrder(models.Model):
  7. _inherit = 'sale.order'
  8. is_contract = fields.Boolean(
  9. string='Is a contract', compute='_compute_is_contract'
  10. )
  11. contract_count = fields.Integer(compute='_compute_contract_count')
  12. @api.depends('order_line')
  13. def _compute_is_contract(self):
  14. self.is_contract = any(self.order_line.mapped('is_contract'))
  15. @api.multi
  16. def action_confirm(self):
  17. """ If we have a contract in the order, set it up """
  18. contract_env = self.env['account.analytic.account']
  19. for rec in self.filtered('is_contract'):
  20. line_to_create_contract = rec.order_line.filtered(
  21. lambda r: not r.contract_id and r.product_id.is_contract
  22. )
  23. line_to_update_contract = rec.order_line.filtered(
  24. lambda r: r.contract_id and r.product_id.is_contract
  25. )
  26. for contract_template in line_to_create_contract.mapped(
  27. 'product_id.contract_template_id'
  28. ):
  29. order_lines = line_to_create_contract.filtered(
  30. lambda r: r.product_id.contract_template_id
  31. == contract_template
  32. )
  33. contract = contract_env.create(
  34. {
  35. 'name': '{template_name}: {sale_name}'.format(
  36. template_name=contract_template.name,
  37. sale_name=rec.name,
  38. ),
  39. 'partner_id': rec.partner_id.id,
  40. 'recurring_invoices': True,
  41. 'contract_template_id': contract_template.id,
  42. }
  43. )
  44. contract._onchange_contract_template_id()
  45. order_lines.create_contract_line(contract)
  46. order_lines.write({'contract_id': contract.id})
  47. for line in line_to_update_contract:
  48. line.create_contract_line(line.contract_id)
  49. return super(SaleOrder, self).action_confirm()
  50. @api.multi
  51. @api.depends("order_line")
  52. def _compute_contract_count(self):
  53. for rec in self:
  54. rec.contract_count = len(rec.order_line.mapped('contract_id'))
  55. @api.multi
  56. def action_show_contracts(self):
  57. self.ensure_one()
  58. action = self.env.ref(
  59. "contract.action_account_analytic_sale_overdue_all"
  60. ).read()[0]
  61. contracts = (
  62. self.env['account.analytic.invoice.line']
  63. .search([('sale_order_line_id', 'in', self.order_line.ids)])
  64. .mapped('contract_id')
  65. )
  66. action["domain"] = [("id", "in", contracts.ids)]
  67. return action