mirror of
https://github.com/OCA/contract.git
synced 2025-02-13 17:57:24 +02:00
[FIX] product_contract: set 'Contract template' field company depend
This commit is contained in:
committed by
Pedro M. Baeza
parent
93663d99c8
commit
500461396b
@@ -10,8 +10,10 @@ class ProductTemplate(models.Model):
|
||||
_inherit = 'product.template'
|
||||
|
||||
is_contract = fields.Boolean('Is a contract')
|
||||
contract_template_id = fields.Many2one(
|
||||
comodel_name='contract.template', string='Contract Template'
|
||||
property_contract_template_id = fields.Many2one(
|
||||
comodel_name='contract.template',
|
||||
string='Contract Template',
|
||||
company_dependent=True,
|
||||
)
|
||||
default_qty = fields.Integer(string="Default Quantity", default=1)
|
||||
recurring_rule_type = fields.Selection(
|
||||
@@ -42,13 +44,13 @@ class ProductTemplate(models.Model):
|
||||
string='Termination Notice type',
|
||||
)
|
||||
|
||||
@api.onchange('is_contract')
|
||||
def _change_is_contract(self):
|
||||
""" Clear the relation to contract_template_id when downgrading
|
||||
product from contract
|
||||
"""
|
||||
if not self.is_contract:
|
||||
self.contract_template_id = False
|
||||
@api.multi
|
||||
def write(self, vals):
|
||||
if 'is_contract' in vals and vals['is_contract'] is False:
|
||||
for company in self.env['res.company'].search([]):
|
||||
self.with_context(force_company=company.id).write(
|
||||
{'property_contract_template_id': False})
|
||||
super().write(vals)
|
||||
|
||||
@api.constrains('is_contract', 'type')
|
||||
def _check_contract_product_type(self):
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
# Copyright 2018 ACSONE SA/NV.
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||
|
||||
from odoo import fields, api, models
|
||||
from odoo import _, fields, api, models
|
||||
from odoo.exceptions import ValidationError
|
||||
|
||||
|
||||
class SaleOrder(models.Model):
|
||||
@@ -70,12 +71,23 @@ class SaleOrder(models.Model):
|
||||
'sale_order_line_id'
|
||||
)
|
||||
)
|
||||
for contract_template in line_to_create_contract.mapped(
|
||||
'product_id.contract_template_id'
|
||||
):
|
||||
for order_line in line_to_create_contract:
|
||||
contract_template = order_line.product_id.with_context(
|
||||
force_company=rec.company_id.id
|
||||
).property_contract_template_id
|
||||
if not contract_template:
|
||||
raise ValidationError(
|
||||
_("You must specify a contract "
|
||||
"template for '{}' product in '{}' company.").format(
|
||||
order_line.product_id.name,
|
||||
rec.company_id.name
|
||||
)
|
||||
)
|
||||
order_lines = line_to_create_contract.filtered(
|
||||
lambda r, template=contract_template:
|
||||
r.product_id.contract_template_id == template
|
||||
r.product_id.with_context(
|
||||
force_company=r.order_id.company_id.id
|
||||
).property_contract_template_id == template
|
||||
)
|
||||
contract = contract_model.create(
|
||||
rec._prepare_contract_value(contract_template)
|
||||
|
||||
@@ -19,8 +19,7 @@ class SaleOrderLine(models.Model):
|
||||
contract_template_id = fields.Many2one(
|
||||
comodel_name='contract.template',
|
||||
string='Contract Template',
|
||||
related='product_id.product_tmpl_id.contract_template_id',
|
||||
readonly=True,
|
||||
compute='_compute_contract_template_id',
|
||||
)
|
||||
recurring_rule_type = fields.Selection(
|
||||
[
|
||||
@@ -51,6 +50,14 @@ class SaleOrderLine(models.Model):
|
||||
copy=False,
|
||||
)
|
||||
|
||||
@api.multi
|
||||
@api.depends('product_id')
|
||||
def _compute_contract_template_id(self):
|
||||
for rec in self:
|
||||
rec.contract_template_id = rec.product_id.with_context(
|
||||
force_company=rec.order_id.company_id.id
|
||||
).property_contract_template_id
|
||||
|
||||
@api.multi
|
||||
def _get_auto_renew_rule_type(self):
|
||||
"""monthly last day don't make sense for auto_renew_rule_type"""
|
||||
|
||||
Reference in New Issue
Block a user