[MIG] product_contract: Migration to 14.0

This commit is contained in:
Mourad
2021-03-09 16:50:25 +01:00
committed by Abraham Anes
parent 4b52807e89
commit d6e4d4b391
5 changed files with 55 additions and 70 deletions

View File

@@ -65,7 +65,7 @@ class ProductTemplate(models.Model):
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(
self.with_company(company).write(
{"property_contract_template_id": False}
)
super().write(vals)

View File

@@ -16,15 +16,11 @@ class SaleOrder(models.Model):
@api.constrains("state")
def check_contact_is_not_terminated(self):
for rec in self:
if (
rec.state
not in (
"sale",
"done",
"cancel",
)
and rec.order_line.filtered("contract_id.is_terminated")
):
if rec.state not in (
"sale",
"done",
"cancel",
) and rec.order_line.filtered("contract_id.is_terminated"):
raise ValidationError(
_("You can't upsell or downsell a terminated contract")
)
@@ -81,8 +77,8 @@ class SaleOrder(models.Model):
)
contract_templates = self.env["contract.template"]
for order_line in line_to_create_contract:
contract_template = order_line.product_id.with_context(
force_company=rec.company_id.id
contract_template = order_line.product_id.with_company(
rec.company_id
).property_contract_template_id
if not contract_template:
raise ValidationError(
@@ -94,8 +90,8 @@ class SaleOrder(models.Model):
contract_templates |= contract_template
for contract_template in contract_templates:
order_lines = line_to_create_contract.filtered(
lambda r, template=contract_template: r.product_id.with_context(
force_company=r.order_id.company_id.id
lambda r, template=contract_template: r.product_id.with_company(
r.order_id.company_id
).property_contract_template_id
== template
)
@@ -112,7 +108,7 @@ class SaleOrder(models.Model):
return contracts
def action_confirm(self):
""" If we have a contract in the order, set it up """
"""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()
@@ -127,7 +123,7 @@ class SaleOrder(models.Model):
def action_show_contracts(self):
self.ensure_one()
action = self.env.ref("contract.action_customer_contract").read()[0]
action = self.env.ref("contract.action_customer_contract").sudo().read()[0]
contracts = (
self.env["contract.line"]
.search([("sale_order_line_id", "in", self.order_line.ids)])

View File

@@ -84,8 +84,8 @@ class SaleOrderLine(models.Model):
@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
rec.contract_template_id = rec.product_id.with_company(
rec.order_id.company_id
).property_contract_template_id
def _get_auto_renew_rule_type(self):
@@ -95,9 +95,21 @@ class SaleOrderLine(models.Model):
return "monthly"
return self.recurring_rule_type
def _get_date_end(self):
self.ensure_one()
contract_line_model = self.env["contract.line"]
date_end = (
self.date_start
+ contract_line_model.get_relative_delta(
self._get_auto_renew_rule_type(),
int(self.product_uom_qty),
)
- relativedelta(days=1)
)
return date_end
@api.onchange("product_id")
def onchange_product(self):
contract_line_model = self.env["contract.line"]
for rec in self:
if rec.product_id.is_contract:
rec.product_uom_qty = rec.product_id.default_qty
@@ -105,14 +117,7 @@ class SaleOrderLine(models.Model):
rec.recurring_invoicing_type = rec.product_id.recurring_invoicing_type
rec.date_start = rec.date_start or fields.Date.today()
rec.date_end = (
rec.date_start
+ contract_line_model.get_relative_delta(
rec._get_auto_renew_rule_type(),
int(rec.product_uom_qty),
)
- relativedelta(days=1)
)
rec.date_end = rec._get_date_end()
rec.is_auto_renew = rec.product_id.is_auto_renew
if rec.is_auto_renew:
rec.auto_renew_interval = rec.product_id.auto_renew_interval
@@ -120,19 +125,25 @@ class SaleOrderLine(models.Model):
@api.onchange("date_start", "product_uom_qty", "recurring_rule_type")
def onchange_date_start(self):
contract_line_model = self.env["contract.line"]
for rec in self.filtered("product_id.is_contract"):
if not rec.date_start:
rec.date_end = False
else:
rec.date_end = (
rec.date_start
+ contract_line_model.get_relative_delta(
rec._get_auto_renew_rule_type(),
int(rec.product_uom_qty),
)
- relativedelta(days=1)
)
rec.date_end = rec._get_date_end() if rec.date_start else False
def _get_contract_line_qty(self):
"""Returns the quantity to be put on new contract lines."""
self.ensure_one()
# The quantity on the generated contract line is 1, as it
# correspond to the most common use cases:
# - quantity on the SO line = number of periods sold and unit
# price the price of one period, so the
# total amount of the SO corresponds to the planned value
# of the contract; in this case the quantity on the contract
# line must be 1
# - quantity on the SO line = number of hours sold,
# automatic invoicing of the actual hours through a variable
# quantity formula, in which case the quantity on the contract
# line is not used
# Other use cases are easy to implement by overriding this method.
return 1.0
def _prepare_contract_line_values(
self, contract, predecessor_contract_line_id=False
@@ -157,19 +168,7 @@ class SaleOrderLine(models.Model):
"sequence": self.sequence,
"product_id": self.product_id.id,
"name": self.name,
# The quantity on the generated contract line is 1, as it
# correspond to the most common use cases:
# - quantity on the SO line = number of periods sold and unit
# price the price of one period, so the
# total amount of the SO corresponds to the planned value
# of the contract; in this case the quantity on the contract
# line must be 1
# - quantity on the SO line = number of hours sold,
# automatic invoicing of the actual hours through a variable
# quantity formula, in which case the quantity on the contract
# line is not used
# Other use cases are easy to implement by overriding this method.
"quantity": 1.0,
"quantity": self._get_contract_line_qty(),
"uom_id": self.product_uom.id,
"price_unit": self.price_unit,
"discount": self.discount,