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.

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