mirror of
https://github.com/OCA/contract.git
synced 2025-02-13 17:57:24 +02:00
[12.0][IMP] - option to decide if we automaticallt create contract a sale confirm
This commit is contained in:
@@ -7,3 +7,5 @@ from . import contract_line
|
||||
from . import product_template
|
||||
from . import sale_order
|
||||
from . import sale_order_line
|
||||
from . import res_company
|
||||
from . import res_config_settings
|
||||
|
||||
14
product_contract/models/res_company.py
Normal file
14
product_contract/models/res_company.py
Normal file
@@ -0,0 +1,14 @@
|
||||
# Copyright 2019 ACSONE SA/NV
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import fields, models
|
||||
|
||||
|
||||
class ResCompany(models.Model):
|
||||
|
||||
_inherit = 'res.company'
|
||||
|
||||
create_contract_at_sale_order_confirmation = fields.Boolean(
|
||||
string="Automatically Create Contracts At Sale Order Confirmation",
|
||||
default=True,
|
||||
)
|
||||
14
product_contract/models/res_config_settings.py
Normal file
14
product_contract/models/res_config_settings.py
Normal file
@@ -0,0 +1,14 @@
|
||||
# Copyright 2019 ACSONE SA/NV
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import fields, models
|
||||
|
||||
|
||||
class ResConfigSettings(models.TransientModel):
|
||||
|
||||
_inherit = 'res.config.settings'
|
||||
|
||||
create_contract_at_sale_order_confirmation = fields.Boolean(
|
||||
related="company_id.create_contract_at_sale_order_confirmation",
|
||||
readonly=False
|
||||
)
|
||||
@@ -12,6 +12,27 @@ class SaleOrder(models.Model):
|
||||
string='Is a contract', compute='_compute_is_contract'
|
||||
)
|
||||
contract_count = fields.Integer(compute='_compute_contract_count')
|
||||
need_contract_creation = fields.Boolean(
|
||||
compute='_compute_need_contract_creation'
|
||||
)
|
||||
|
||||
@api.depends('order_line.contract_id', 'state')
|
||||
def _compute_need_contract_creation(self):
|
||||
for rec in self:
|
||||
if rec.state not in ('draft', 'sent'):
|
||||
line_to_create_contract = rec.order_line.filtered(
|
||||
lambda r: not r.contract_id and r.product_id.is_contract
|
||||
)
|
||||
line_to_update_contract = rec.order_line.filtered(
|
||||
lambda r: r.contract_id
|
||||
and r.product_id.is_contract
|
||||
and r
|
||||
not in r.contract_id.recurring_invoice_line_ids.mapped(
|
||||
'sale_order_line_id'
|
||||
)
|
||||
)
|
||||
if line_to_create_contract or line_to_update_contract:
|
||||
rec.need_contract_creation = True
|
||||
|
||||
@api.depends('order_line')
|
||||
def _compute_is_contract(self):
|
||||
@@ -34,15 +55,20 @@ class SaleOrder(models.Model):
|
||||
}
|
||||
|
||||
@api.multi
|
||||
def action_confirm(self):
|
||||
""" If we have a contract in the order, set it up """
|
||||
def action_create_contract(self):
|
||||
contract_env = self.env['account.analytic.account']
|
||||
contracts = 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 and r.product_id.is_contract
|
||||
)
|
||||
line_to_update_contract = rec.order_line.filtered(
|
||||
lambda r: r.contract_id and r.product_id.is_contract
|
||||
lambda r: r.contract_id
|
||||
and r.product_id.is_contract
|
||||
and r
|
||||
not in r.contract_id.recurring_invoice_line_ids.mapped(
|
||||
'sale_order_line_id'
|
||||
)
|
||||
)
|
||||
for contract_template in line_to_create_contract.mapped(
|
||||
'product_id.contract_template_id'
|
||||
@@ -54,11 +80,20 @@ class SaleOrder(models.Model):
|
||||
contract = contract_env.create(
|
||||
rec._prepare_contract_value(contract_template)
|
||||
)
|
||||
contracts |= contract
|
||||
contract._onchange_contract_template_id()
|
||||
order_lines.create_contract_line(contract)
|
||||
order_lines.write({'contract_id': contract.id})
|
||||
for line in line_to_update_contract:
|
||||
line.create_contract_line(line.contract_id)
|
||||
return contracts
|
||||
|
||||
@api.multi
|
||||
def action_confirm(self):
|
||||
""" If we have a contract in the order, set it up """
|
||||
self.filtered(
|
||||
lambda order: order.company_id.create_contract_at_sale_order_confirmation
|
||||
).action_create_contract()
|
||||
return super(SaleOrder, self).action_confirm()
|
||||
|
||||
@api.multi
|
||||
|
||||
Reference in New Issue
Block a user