mirror of
https://github.com/OCA/contract.git
synced 2025-02-13 17:57:24 +02:00
[IMP] product_contract: code improvements
This commit is contained in:
@@ -35,15 +35,19 @@ class ContractLine(models.Model):
|
||||
for rec in self:
|
||||
rec.date_start = fields.Date.today()
|
||||
if rec.product_id.is_contract:
|
||||
rec.recurring_rule_type = rec.product_id.recurring_rule_type
|
||||
rec.recurring_invoicing_type = rec.product_id.recurring_invoicing_type
|
||||
rec.recurring_interval = 1
|
||||
rec.is_auto_renew = rec.product_id.is_auto_renew
|
||||
rec.auto_renew_interval = rec.product_id.auto_renew_interval
|
||||
rec.auto_renew_rule_type = rec.product_id.auto_renew_rule_type
|
||||
rec.termination_notice_interval = (
|
||||
rec.product_id.termination_notice_interval
|
||||
)
|
||||
rec.termination_notice_rule_type = (
|
||||
rec.product_id.termination_notice_rule_type
|
||||
rec.update(
|
||||
{
|
||||
"recurring_rule_type": rec.product_id.recurring_rule_type,
|
||||
"recurring_invoicing_type": rec.product_id.recurring_invoicing_type,
|
||||
"recurring_interval": 1,
|
||||
"is_auto_renew": rec.product_id.is_auto_renew,
|
||||
"auto_renew_interval": rec.product_id.auto_renew_interval,
|
||||
"auto_renew_rule_type": rec.product_id.auto_renew_rule_type,
|
||||
"termination_notice_interval": (
|
||||
rec.product_id.termination_notice_interval
|
||||
),
|
||||
"termination_notice_rule_type": (
|
||||
rec.product_id.termination_notice_rule_type
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
@@ -14,7 +14,7 @@ class SaleOrder(models.Model):
|
||||
need_contract_creation = fields.Boolean(compute="_compute_need_contract_creation")
|
||||
|
||||
@api.constrains("state")
|
||||
def check_contact_is_not_terminated(self):
|
||||
def _check_contact_is_not_terminated(self):
|
||||
for rec in self:
|
||||
if rec.state not in (
|
||||
"sale",
|
||||
@@ -27,8 +27,8 @@ class SaleOrder(models.Model):
|
||||
|
||||
@api.depends("order_line.contract_id", "state")
|
||||
def _compute_need_contract_creation(self):
|
||||
self.update({"need_contract_creation": False})
|
||||
for rec in self:
|
||||
rec.need_contract_creation = False
|
||||
if rec.state in ("sale", "done"):
|
||||
line_to_create_contract = rec.order_line.filtered(
|
||||
lambda r: not r.contract_id and r.product_id.is_contract
|
||||
@@ -64,7 +64,7 @@ class SaleOrder(models.Model):
|
||||
|
||||
def action_create_contract(self):
|
||||
contract_model = self.env["contract.contract"]
|
||||
contracts = self.env["contract.contract"]
|
||||
contracts = []
|
||||
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
|
||||
@@ -98,14 +98,14 @@ class SaleOrder(models.Model):
|
||||
contract = contract_model.create(
|
||||
rec._prepare_contract_value(contract_template)
|
||||
)
|
||||
contracts |= contract
|
||||
contracts.append(contract)
|
||||
contract._onchange_contract_template_id()
|
||||
contract._onchange_contract_type()
|
||||
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
|
||||
return contract_model.browse(contracts)
|
||||
|
||||
def action_confirm(self):
|
||||
"""If we have a contract in the order, set it up"""
|
||||
@@ -117,19 +117,22 @@ class SaleOrder(models.Model):
|
||||
@api.depends("order_line")
|
||||
def _compute_contract_count(self):
|
||||
for rec in self:
|
||||
rec.contract_count = len(
|
||||
rec.order_line.mapped("contract_id").filtered(lambda r: r.active)
|
||||
)
|
||||
rec.contract_count = len(rec.order_line.mapped("contract_id"))
|
||||
|
||||
def action_show_contracts(self):
|
||||
self.ensure_one()
|
||||
action = self.env.ref("contract.action_customer_contract").sudo().read()[0]
|
||||
action = self.env["ir.actions.act_window"]._for_xml_id(
|
||||
"contract.action_customer_contract"
|
||||
)
|
||||
|
||||
contracts = (
|
||||
self.env["contract.line"]
|
||||
.search([("sale_order_line_id", "in", self.order_line.ids)])
|
||||
.mapped("contract_id")
|
||||
)
|
||||
action["domain"] = [("id", "in", contracts.ids)]
|
||||
action["domain"] = [
|
||||
("contract_line_ids.sale_order_line_id", "in", self.order_line.ids)
|
||||
]
|
||||
if len(contracts) == 1:
|
||||
# If there is only one contract, open it directly
|
||||
action.update(
|
||||
|
||||
@@ -52,10 +52,19 @@ class SaleOrderLine(models.Model):
|
||||
required=False,
|
||||
copy=False,
|
||||
)
|
||||
is_auto_renew = fields.Boolean(string="Auto Renew", default=False)
|
||||
is_auto_renew = fields.Boolean(
|
||||
string="Auto Renew",
|
||||
compute="_compute_auto_renew",
|
||||
default=False,
|
||||
store=True,
|
||||
readonly=False,
|
||||
)
|
||||
auto_renew_interval = fields.Integer(
|
||||
default=1,
|
||||
string="Renew Every",
|
||||
compute="_compute_auto_renew",
|
||||
store=True,
|
||||
readonly=False,
|
||||
help="Renew every (Days/Week/Month/Year)",
|
||||
)
|
||||
auto_renew_rule_type = fields.Selection(
|
||||
@@ -66,12 +75,15 @@ class SaleOrderLine(models.Model):
|
||||
("yearly", "Year(s)"),
|
||||
],
|
||||
default="yearly",
|
||||
compute="_compute_auto_renew",
|
||||
store=True,
|
||||
readonly=False,
|
||||
string="Renewal type",
|
||||
help="Specify Interval for automatic renewal.",
|
||||
)
|
||||
|
||||
@api.constrains("contract_id")
|
||||
def check_contact_is_not_terminated(self):
|
||||
def _check_contact_is_not_terminated(self):
|
||||
for rec in self:
|
||||
if (
|
||||
rec.order_id.state not in ("sale", "done", "cancel")
|
||||
@@ -108,8 +120,8 @@ class SaleOrderLine(models.Model):
|
||||
)
|
||||
return date_end
|
||||
|
||||
@api.onchange("product_id")
|
||||
def onchange_product(self):
|
||||
@api.depends("product_id")
|
||||
def _compute_auto_renew(self):
|
||||
for rec in self:
|
||||
if rec.product_id.is_contract:
|
||||
rec.product_uom_qty = rec.product_id.default_qty
|
||||
@@ -248,9 +260,8 @@ class SaleOrderLine(models.Model):
|
||||
)
|
||||
|
||||
def _compute_invoice_status(self):
|
||||
res = super(SaleOrderLine, self)._compute_invoice_status()
|
||||
for line in self.filtered("contract_id"):
|
||||
line.invoice_status = "no"
|
||||
res = super()._compute_invoice_status()
|
||||
self.filtered("contract_id").update({"invoice_status": "no"})
|
||||
return res
|
||||
|
||||
def invoice_line_create(self, invoice_id, qty):
|
||||
|
||||
Reference in New Issue
Block a user