[12.0][IMP] - option to decide if we automaticallt create contract a sale confirm

This commit is contained in:
sbejaoui
2019-09-05 13:01:02 +02:00
committed by Abraham Anes
parent a1cdd4169d
commit bc0f33760e
8 changed files with 130 additions and 4 deletions

View File

@@ -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

View 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,
)

View 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
)

View File

@@ -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