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.

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