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.

74 lines
2.8 KiB

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