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.

119 lines
4.4 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. need_contract_creation = fields.Boolean(
  12. compute='_compute_need_contract_creation'
  13. )
  14. @api.depends('order_line.contract_id', 'state')
  15. def _compute_need_contract_creation(self):
  16. for rec in self:
  17. if rec.state in ('sale', 'done'):
  18. line_to_create_contract = rec.order_line.filtered(
  19. lambda r: not r.contract_id and r.product_id.is_contract
  20. )
  21. line_to_update_contract = rec.order_line.filtered(
  22. lambda r: r.contract_id
  23. and r.product_id.is_contract
  24. and r
  25. not in r.contract_id.contract_line_ids.mapped(
  26. 'sale_order_line_id'
  27. )
  28. )
  29. if line_to_create_contract or line_to_update_contract:
  30. rec.need_contract_creation = True
  31. @api.depends('order_line')
  32. def _compute_is_contract(self):
  33. self.is_contract = any(self.order_line.mapped('is_contract'))
  34. @api.multi
  35. def _prepare_contract_value(self, contract_template):
  36. self.ensure_one()
  37. return {
  38. 'name': '{template_name}: {sale_name}'.format(
  39. template_name=contract_template.name, sale_name=self.name
  40. ),
  41. 'partner_id': self.partner_id.id,
  42. 'company_id': self.company_id.id,
  43. 'contract_template_id': contract_template.id,
  44. 'user_id': self.user_id.id,
  45. 'payment_term_id': self.payment_term_id.id,
  46. 'fiscal_position_id': self.fiscal_position_id.id,
  47. 'invoice_partner_id': self.partner_invoice_id.id,
  48. }
  49. @api.multi
  50. def action_create_contract(self):
  51. contract_model = self.env['contract.contract']
  52. contracts = self.env['contract.contract']
  53. for rec in self.filtered('is_contract'):
  54. line_to_create_contract = rec.order_line.filtered(
  55. lambda r: not r.contract_id and r.product_id.is_contract
  56. )
  57. line_to_update_contract = rec.order_line.filtered(
  58. lambda r: r.contract_id
  59. and r.product_id.is_contract
  60. and r
  61. not in r.contract_id.contract_line_ids.mapped(
  62. 'sale_order_line_id'
  63. )
  64. )
  65. for contract_template in line_to_create_contract.mapped(
  66. 'product_id.contract_template_id'
  67. ):
  68. order_lines = line_to_create_contract.filtered(
  69. lambda r, template=contract_template:
  70. r.product_id.contract_template_id == template
  71. )
  72. contract = contract_model.create(
  73. rec._prepare_contract_value(contract_template)
  74. )
  75. contracts |= contract
  76. contract._onchange_contract_template_id()
  77. order_lines.create_contract_line(contract)
  78. order_lines.write({'contract_id': contract.id})
  79. for line in line_to_update_contract:
  80. line.create_contract_line(line.contract_id)
  81. return contracts
  82. @api.multi
  83. def action_confirm(self):
  84. """ If we have a contract in the order, set it up """
  85. self.filtered(
  86. lambda order: (
  87. order.company_id.create_contract_at_sale_order_confirmation
  88. )
  89. ).action_create_contract()
  90. return super(SaleOrder, self).action_confirm()
  91. @api.multi
  92. @api.depends("order_line")
  93. def _compute_contract_count(self):
  94. for rec in self:
  95. rec.contract_count = len(rec.order_line.mapped('contract_id'))
  96. @api.multi
  97. def action_show_contracts(self):
  98. self.ensure_one()
  99. action = self.env.ref(
  100. "contract.action_customer_contract"
  101. ).read()[0]
  102. contracts = (
  103. self.env['contract.line']
  104. .search([('sale_order_line_id', 'in', self.order_line.ids)])
  105. .mapped('contract_id')
  106. )
  107. action["domain"] = [("id", "in", contracts.ids)]
  108. return action