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.

117 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 not in ('draft', 'sent'):
  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.recurring_invoice_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. 'recurring_invoices': True,
  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_env = self.env['account.analytic.account']
  52. contracts = self.env['account.analytic.account']
  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.recurring_invoice_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: r.product_id.contract_template_id
  70. == contract_template
  71. )
  72. contract = contract_env.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: order.company_id.create_contract_at_sale_order_confirmation
  87. ).action_create_contract()
  88. return super(SaleOrder, self).action_confirm()
  89. @api.multi
  90. @api.depends("order_line")
  91. def _compute_contract_count(self):
  92. for rec in self:
  93. rec.contract_count = len(rec.order_line.mapped('contract_id'))
  94. @api.multi
  95. def action_show_contracts(self):
  96. self.ensure_one()
  97. action = self.env.ref(
  98. "contract.action_account_analytic_sale_overdue_all"
  99. ).read()[0]
  100. contracts = (
  101. self.env['account.analytic.invoice.line']
  102. .search([('sale_order_line_id', 'in', self.order_line.ids)])
  103. .mapped('contract_id')
  104. )
  105. action["domain"] = [("id", "in", contracts.ids)]
  106. return action