Skip to content

Commit

Permalink
[IMP] product_warranty: add functionality that removes warranty line …
Browse files Browse the repository at this point in the history
…on change

Now when main product sale order line is deleted then related warranty/
warranties gets deleted instantly. (For better user experience)

Removed unnecessary browse and search queries which used to get performed
while adding warranty. Linked sale order line on wizard opening.

Task - 4504888
  • Loading branch information
nipl-odoo committed Jan 24, 2025
1 parent 8034e6c commit 940e332
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
22 changes: 21 additions & 1 deletion product_warranty/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from odoo import models
from odoo import api, Command, models


class InheritedSaleOrder(models.Model):
Expand All @@ -13,3 +13,23 @@ def action_open_warranty_wizard(self):
"target": "new",
"context": {"default_sale_order_id": self.id},
}

@api.onchange("order_line")
def _onchange_order_line(self):
super()._onchange_order_line()
deleted_product_template_ids = []
for line in self.order_line:
# Find each products that is not in Sale Order currently
if (
line.linked_line_id.id
and line.linked_line_id.id not in self.order_line.ids
):
deleted_product_template_ids.append(line.linked_line_id.id)

linked_line_ids_to_delete = self.order_line.search(
[("linked_line_id", "in", deleted_product_template_ids)]
)
self.order_line = [
Command.unlink(linked_line_id)
for linked_line_id in linked_line_ids_to_delete.ids
]
32 changes: 14 additions & 18 deletions product_warranty/wizard/product_warranty_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,25 @@ def default_get(self, fields_list):
sale_order = self.env["sale.order"].browse(self.env.context.get("active_id"))
warranty_lines = []

for line in sale_order.order_line.filtered(
lambda l: l.product_id.is_warranty_product
):
warranty = self.env["product.warranty.wizard.line"].create(
{
"product_template_id": line.product_template_id.id,
"warranty_config_id": False,
}
)
warranty_lines.append(Command.link(warranty.id))
for line in sale_order.order_line:
if (
line.product_template_id
and line.product_template_id.is_warranty_product
):
warranty = self.env["product.warranty.wizard.line"].create(
{
"sale_order_line_id": line.id,
"warranty_config_id": False,
}
)
warranty_lines.append(Command.link(warranty.id))

res["sale_order_id"] = sale_order.id
res["line_ids"] = warranty_lines
return res

def apply_warranty(self):
for line in self.line_ids:
sale_order_line = self.env["sale.order.line"].search(
[
("order_id", "=", self.sale_order_id.id),
("product_template_id", "=", line.product_template_id.id),
]
)
product = self.env["product.template"].browse(line.product_template_id.id)
if line.warranty_config_id:
self.env["sale.order.line"].create(
Expand All @@ -48,9 +44,9 @@ def apply_warranty(self):
"name": "Extended Warranty of %d Years - %s"
% (line.warranty_config_id.years, product.name),
"product_uom_qty": 1,
"related_line_id": sale_order_line.id,
"linked_line_id": line.sale_order_line_id.id,
"price_unit": (line.warranty_config_id.percentage / 100)
* float(product.list_price),
* line.sale_order_line_id.price_subtotal,
}
)

Expand Down
13 changes: 12 additions & 1 deletion product_warranty/wizard/product_warranty_wizard_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ class WarrantyWizardLine(models.TransientModel):
_description = "Warranty Wizard Line"

wizard_id = fields.Many2one("product.warranty.wizard", string="Wizard")
product_template_id = fields.Many2one("product.template", string="Product", required=True)
sale_order_line_id = fields.Many2one("sale.order.line", string="Sale Order Line")
product_template_id = fields.Many2one(
"product.template", string="Product", compute="_compute_product_template_id"
)
warranty_config_id = fields.Many2one(
"product.warranty.config", string="Warranty Type"
)
Expand All @@ -21,3 +24,11 @@ def _compute_date_end(self):
line.date_end = datetime.today() + relativedelta(
years=line.warranty_config_id.years
)

@api.depends("sale_order_line_id")
def _compute_product_template_id(self):
for line in self:
if line.sale_order_line_id:
line.product_template_id = line.sale_order_line_id.product_template_id
else:
line.product_template_id = False

0 comments on commit 940e332

Please sign in to comment.