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.

73 lines
2.7 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
  22. )
  23. line_to_update_contract = rec.order_line.filtered('contract_id')
  24. for contract_template in line_to_create_contract.mapped(
  25. 'product_id.contract_template_id'
  26. ):
  27. order_lines = line_to_create_contract.filtered(
  28. lambda r: r.product_id.contract_template_id
  29. == contract_template
  30. )
  31. contract = contract_env.create(
  32. {
  33. 'name': '{template_name}: {sale_name}'.format(
  34. template_name=contract_template.name,
  35. sale_name=rec.name,
  36. ),
  37. 'partner_id': rec.partner_id.id,
  38. 'recurring_invoices': True,
  39. 'contract_template_id': contract_template.id,
  40. }
  41. )
  42. contract._onchange_contract_template_id()
  43. order_lines.create_contract_line(contract)
  44. order_lines.write({'contract_id': contract.id})
  45. for line in line_to_update_contract:
  46. line.create_contract_line(line.contract_id)
  47. return super(SaleOrder, self).action_confirm()
  48. @api.multi
  49. @api.depends("order_line")
  50. def _compute_contract_count(self):
  51. for rec in self:
  52. rec.contract_count = len(rec.order_line.mapped('contract_id'))
  53. @api.multi
  54. def action_show_contracts(self):
  55. self.ensure_one()
  56. action = self.env.ref(
  57. "contract.action_account_analytic_sale_overdue_all"
  58. ).read()[0]
  59. contracts = (
  60. self.env['account.analytic.invoice.line']
  61. .search([('sale_order_line_id', 'in', self.order_line.ids)])
  62. .mapped('contract_id')
  63. )
  64. action["domain"] = [("id", "in", contracts.ids)]
  65. return action