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.

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