mirror of
https://github.com/OCA/contract.git
synced 2025-02-13 17:57:24 +02:00
[IMP] - Create contract on sale order confirmation
- On Sale Order confirmation, a contract is created for each contract template used on sale order lines - A not finished contract can be mentioned on sale order line - A sale order line linked to a contract will update it and don't create a new one if it had the same template
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2017 LasLabs Inc.
|
||||
# Copyright 2018 ACSONE SA/NV.
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||
|
||||
from odoo import fields, api, models
|
||||
@@ -9,29 +10,61 @@ class SaleOrder(models.Model):
|
||||
_inherit = 'sale.order'
|
||||
|
||||
is_contract = fields.Boolean(
|
||||
string='Is a contract', compute="_compute_is_contract"
|
||||
string='Is a contract', compute='_compute_is_contract'
|
||||
)
|
||||
contract_count = fields.Integer(compute='_compute_contract_count')
|
||||
|
||||
@api.depends('order_line')
|
||||
def _compute_is_contract(self):
|
||||
self.is_contract = any(
|
||||
self.order_line.mapped('is_contract')
|
||||
)
|
||||
self.is_contract = any(self.order_line.mapped('is_contract'))
|
||||
|
||||
@api.multi
|
||||
def action_confirm(self):
|
||||
""" If we have a contract in the order, set it up """
|
||||
for rec in self:
|
||||
order_lines = self.mapped('order_line').filtered(
|
||||
lambda r: r.product_id.is_contract
|
||||
contract_env = self.env['account.analytic.account']
|
||||
for rec in self.filtered('is_contract'):
|
||||
line_to_create_contract = rec.order_line.filtered(
|
||||
lambda r: not r.contract_id
|
||||
)
|
||||
for line in order_lines:
|
||||
contract_tmpl = line.product_id.contract_template_id
|
||||
contract = self.env['account.analytic.account'].create({
|
||||
'name': '%s Contract' % rec.name,
|
||||
'partner_id': rec.partner_id.id,
|
||||
'contract_template_id': contract_tmpl.id,
|
||||
})
|
||||
line.contract_id = contract.id
|
||||
contract.recurring_create_invoice()
|
||||
for contract_template in line_to_create_contract.mapped(
|
||||
'product_id.contract_template_id'
|
||||
):
|
||||
order_lines = line_to_create_contract.filtered(
|
||||
lambda r: r.product_id.contract_template_id
|
||||
== contract_template
|
||||
)
|
||||
contract = contract_env.create(
|
||||
{
|
||||
'name': '{template_name}: {sale_name}'.format(
|
||||
template_name=contract_template.name,
|
||||
sale_name=rec.name,
|
||||
),
|
||||
'partner_id': rec.partner_id.id,
|
||||
'recurring_invoices': True,
|
||||
'contract_template_id': contract_template.id,
|
||||
}
|
||||
)
|
||||
contract._onchange_contract_template_id()
|
||||
order_lines.create_contract_line(contract)
|
||||
order_lines.write({'contract_id': contract.id})
|
||||
line_to_update_contract = rec.order_line.filtered('contract_id')
|
||||
for line in line_to_update_contract:
|
||||
line.create_contract_line(line.contract_id)
|
||||
return super(SaleOrder, self).action_confirm()
|
||||
|
||||
@api.multi
|
||||
@api.depends("order_line")
|
||||
def _compute_contract_count(self):
|
||||
for rec in self:
|
||||
rec.contract_count = len(rec.order_line.mapped('contract_id'))
|
||||
|
||||
@api.multi
|
||||
def action_show_contracts(self):
|
||||
self.ensure_one()
|
||||
action = self.env.ref(
|
||||
"contract.action_account_analytic_sale_overdue_all"
|
||||
).read()[0]
|
||||
action["domain"] = [
|
||||
("id", "in", self.order_line.mapped('contract_id').ids)
|
||||
]
|
||||
return action
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
# Copyright 2017 ACSONE SA/NV.
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||
|
||||
from odoo import api, fields, models
|
||||
from odoo import api, fields, models, _
|
||||
from odoo.exceptions import ValidationError
|
||||
|
||||
|
||||
class SaleOrderLine(models.Model):
|
||||
@@ -13,7 +14,13 @@ class SaleOrderLine(models.Model):
|
||||
string='Is a contract', related="product_id.is_contract"
|
||||
)
|
||||
contract_id = fields.Many2one(
|
||||
comodel_name='account.analytic.account', string='Contract'
|
||||
comodel_name='account.analytic.account', string='Contract', copy=False
|
||||
)
|
||||
contract_template_id = fields.Many2one(
|
||||
comodel_name='account.analytic.contract',
|
||||
string='Contract Template',
|
||||
related='product_id.product_tmpl_id.contract_template_id',
|
||||
readonly=True
|
||||
)
|
||||
recurring_rule_type = fields.Selection(
|
||||
[
|
||||
@@ -55,3 +62,54 @@ class SaleOrderLine(models.Model):
|
||||
self.product_id.recurring_invoicing_type
|
||||
)
|
||||
self.recurring_interval = self.product_id.recurring_interval
|
||||
|
||||
@api.multi
|
||||
def _prepare_contract_line_values(self, contract):
|
||||
self.ensure_one()
|
||||
return {
|
||||
'sequence': self.sequence,
|
||||
'product_id': self.product_id.id,
|
||||
'name': self.name,
|
||||
'quantity': self.product_uom_qty,
|
||||
'uom_id': self.product_uom.id,
|
||||
'price_unit': self.price_unit,
|
||||
'discount': self.discount,
|
||||
'recurring_next_date': self.recurring_next_date
|
||||
or fields.Date.today(),
|
||||
'date_end': self.date_end,
|
||||
'date_start': self.date_start or fields.Date.today(),
|
||||
'recurring_interval': self.recurring_interval,
|
||||
'recurring_invoicing_type': self.recurring_invoicing_type,
|
||||
'recurring_rule_type': self.recurring_rule_type,
|
||||
'contract_id': contract.id,
|
||||
}
|
||||
|
||||
@api.multi
|
||||
def create_contract_line(self, contract):
|
||||
contract_line = self.env['account.analytic.invoice.line']
|
||||
for rec in self:
|
||||
contract_line.create(rec._prepare_contract_line_values(contract))
|
||||
|
||||
@api.constrains('contract_id')
|
||||
def _check_contract_sale_partner(self):
|
||||
for rec in self:
|
||||
if rec.contract_id:
|
||||
if rec.order_id.partner_id != rec.contract_id.partner_id:
|
||||
raise ValidationError(
|
||||
_(
|
||||
"Sale Order and contract should be "
|
||||
"linked to the same partner"
|
||||
)
|
||||
)
|
||||
|
||||
@api.constrains('product_id', 'contract_id')
|
||||
def _check_contract_sale_contract_template(self):
|
||||
for rec in self:
|
||||
if rec.contract_id:
|
||||
if (
|
||||
rec.contract_template_id
|
||||
!= rec.contract_id.contract_template_id
|
||||
):
|
||||
raise ValidationError(
|
||||
_("Contract product has different contract template")
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user