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.

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