Skip to content

Commit bec276c

Browse files
committed
[ADD] add_product_warranty: allow adding warranty lines to sale orders
-Introduced a new field is_warranty_available on product template to indicate whether a product is eligible for a warranty. -Added a Warranty Configuration model to define warranty products, duration, and pricing rules. -Created a new menu item for Warranty Configuration under the Sales module. -Added an "Add Warranty" button on the sale order form to allow users to add warranties for selected products. -Implemented a wizard that opens upon clicking "Add Warranty", enabling users to choose warranty options. -Ensured warranty prices are computed based on product price and warranty percentage. -Automatically removes the warranty when the associated product is deleted from the order. Task-5079801
1 parent fbf9ee9 commit bec276c

16 files changed

+329
-0
lines changed

add_product_warranty/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from . import models
2+
from . import wizard
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "Product Warranty",
3+
"description": "Adds functionality to manage product warranties in sales orders and linking warranties to products",
4+
"depends": ["sale_management"],
5+
"data": [
6+
"security/ir.model.access.csv",
7+
"wizard/sale_order_add_warranty_views.xml",
8+
"views/product_template_views.xml",
9+
"views/sale_order_views.xml",
10+
"views/warranty_configuration_views.xml",
11+
"views/warranty_configuration_menus.xml",
12+
],
13+
"installable": True,
14+
"license": "LGPL-3",
15+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from . import warranty_configuration
2+
from . import product_template
3+
from . import sale_order_line
4+
from . import sale_order
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from odoo import fields, models
2+
3+
4+
class ProductTemplate(models.Model):
5+
_inherit = "product.template"
6+
7+
is_warranty_available = fields.Boolean(
8+
help="Indicates if the product has an associated warranty option.",
9+
)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from odoo import api, fields, models
2+
3+
4+
class SaleOrder(models.Model):
5+
_inherit = "sale.order"
6+
7+
show_warranty_button = fields.Boolean(
8+
string="Show Warranty Button",
9+
compute="_compute_show_warranty_button",
10+
store=True
11+
)
12+
13+
@api.depends("order_line")
14+
def _compute_show_warranty_button(self):
15+
for order in self:
16+
show_button = False
17+
for line in order.order_line:
18+
# Check if this product has warranty available
19+
if line.product_template_id.is_warranty_available:
20+
# And there is no other line that links to this line as warranty
21+
linked_as_warranty = any(
22+
l.warranty_line_linked_with_so_line == line
23+
for l in order.order_line
24+
)
25+
if not linked_as_warranty:
26+
show_button = True
27+
break # We can stop early if condition is satisfied
28+
29+
order.show_warranty_button = show_button
30+
31+
@api.onchange("order_line")
32+
def _onchange_order_line(self):
33+
original_line_ids = self._origin.order_line.ids
34+
35+
current_line_ids = self.order_line.ids
36+
37+
deleted_line_ids = list(set(original_line_ids) - set(current_line_ids))
38+
39+
if deleted_line_ids:
40+
# Remove warranty lines linked to deleted lines
41+
self.order_line = [
42+
(
43+
6,
44+
0,
45+
self.order_line.filtered(
46+
lambda line: line.warranty_line_linked_with_so_line.id
47+
not in deleted_line_ids
48+
).ids,
49+
)
50+
]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from odoo import fields, models
2+
3+
4+
class SaleOrderLine(models.Model):
5+
_inherit = "sale.order.line"
6+
7+
warranty_line_linked_with_so_line = fields.Many2one(
8+
"sale.order.line", ondelete="cascade"
9+
)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from odoo import fields, models
2+
3+
4+
class WarrantyConfiguration(models.Model):
5+
_name = "warranty.configuration"
6+
_description = "Warranty configuration for all products"
7+
8+
name = fields.Char(required=True)
9+
period = fields.Integer(
10+
string="Warranty Period (years)",
11+
default=1,
12+
help="Duration of the warranty period in year.",
13+
)
14+
percentage = fields.Float(
15+
required=True,
16+
help="Percentage of the product price to be charged for the warranty.",
17+
)
18+
product_id = fields.Many2one(
19+
comodel_name="product.product",
20+
string="Product",
21+
required=True,
22+
help="Product used to represent the warranty service.",
23+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
2+
access_warranty_configuration,warranty.configuration,model_warranty_configuration,base.group_user,1,1,1,1
3+
access_sale_order_add_warranty,sale.order.add.warranty,model_sale_order_add_warranty,base.group_user,1,1,1,1
4+
access_sale_order_add_warranty_line,sale.order.add.warranty.line,model_sale_order_add_warranty_line,base.group_user,1,1,1,1
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<odoo>
3+
<record id="product_template_form_view" model="ir.ui.view">
4+
<field name="name">product.template.form.view.inherit.add.prodcut.warranty</field>
5+
<field name="model">product.template</field>
6+
<field name="inherit_id" ref="product.product_template_form_view"/>
7+
<field name="arch" type="xml">
8+
<xpath expr="//page[@name='sales']" position="inside">
9+
<group>
10+
<field name="is_warranty_available"/>
11+
</group>
12+
</xpath>
13+
</field>
14+
</record>
15+
</odoo>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<odoo>
3+
<record id="view_order_form" model="ir.ui.view">
4+
<field name="name">view.order.form.inherit.add.product.warranty</field>
5+
<field name="model">sale.order</field>
6+
<field name="inherit_id" ref="sale.view_order_form"/>
7+
<field name="arch" type="xml">
8+
<xpath expr="//page[@name='order_lines']//button[@name='action_open_discount_wizard']" position="before">
9+
<button name="add_product_warranty.action_open_warranty_wizard" string="Add Warranty"
10+
class="btn btn-secondary" type="action" invisible="not show_warranty_button"/>
11+
</xpath>
12+
</field>
13+
</record>
14+
</odoo>

0 commit comments

Comments
 (0)