Skip to content

Commit ad69395

Browse files
committed
[ADD] product_warranty: bridge module to add warranty for products in SO
Specification: 1) Different warranty configurations can be created. Example- 1 year, 2 year. 2) Warranty can be added for products in the sales order.
1 parent 3be1f82 commit ad69395

14 files changed

+234
-0
lines changed

product_warranty/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import models

product_warranty/__manifest__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
'name': 'Product Warranty',
3+
'version': '1.0',
4+
'category': 'Sales',
5+
'depends': ['base', 'sale'],
6+
'data': [
7+
"security/ir.model.access.csv",
8+
"views/warranty_config_views.xml",
9+
"views/warranty_wizard_views.xml",
10+
"views/sale_order_form_views.xml",
11+
"views/product_template_views.xml",
12+
"views/warranty_config_menu.xml",
13+
],
14+
"installable": True,
15+
"application": False,
16+
"auto_install":True,
17+
"license": "AGPL-3",
18+
"author": "Odoo",
19+
}

product_warranty/models/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from . import product_template
2+
from . import sale_order_line
3+
from . import warranty_config
4+
from . import warranty_line_wizard
5+
from . import warranty_wizard
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from odoo import fields, models
2+
3+
class ProductTemplate(models.Model):
4+
_inherit = "product.template"
5+
6+
is_warranty = fields.Boolean(string="Is Warranty Available", default=False)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from odoo import fields, models
2+
3+
class SaleOrderLine(models.Model):
4+
_inherit = "sale.order.line"
5+
6+
warranty_id = fields.Many2one('sale.order.line', ondelete="cascade")
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from odoo import fields, models
2+
3+
class WarrantyConfig(models.Model):
4+
_name = "warranty.config"
5+
_description = "Warranty Config"
6+
7+
name = fields.Char(required=True, string="Name")
8+
product_id = fields.Many2one("product.template", string="Product")
9+
percentage = fields.Float(string="Percentage", required=True)
10+
duration = fields.Integer(string="Duration (Years)", required=True, default=0)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from odoo import fields, models, api
2+
from datetime import timedelta
3+
4+
5+
class WarrantyLineWizard(models.TransientModel):
6+
_name = "warranty.line.wizard"
7+
_description = "Warranty Line Wizard"
8+
9+
wizard_id = fields.Many2one("warranty.wizard", string="Wizard")
10+
11+
sale_order_line_id = fields.Many2one("sale.order.line", string="Sale Order Line")
12+
13+
product_id = fields.Many2one(
14+
"product.template", compute="_compute_product", string="Product"
15+
)
16+
17+
warranty_config_id = fields.Many2one("warranty.config", string="Year")
18+
end_date = fields.Date(string="End Date", compute="_compute_end_date")
19+
20+
@api.depends("warranty_config_id")
21+
def _compute_end_date(self):
22+
for record in self:
23+
if record.warranty_config_id and record.warranty_config_id.duration:
24+
record.end_date = fields.Date.today() + timedelta(
25+
days=record.warranty_config_id.duration * 365
26+
)
27+
else:
28+
record.warranty_config_id = None
29+
record.end_date = None
30+
31+
@api.depends("sale_order_line_id")
32+
def _compute_product(self):
33+
for record in self:
34+
if record.sale_order_line_id:
35+
record.product_id = record.sale_order_line_id.product_template_id
36+
else:
37+
record.product_id = False
38+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from odoo import models, fields, api
2+
3+
class WarrantyWizard(models.TransientModel):
4+
_name = 'warranty.wizard'
5+
_description = 'Warranty Wizard'
6+
7+
sale_order_id = fields.Many2one('sale.order', string="Sale Order")
8+
warranty_line_ids = fields.One2many('warranty.line.wizard', 'wizard_id', string="Warranty Lines")
9+
10+
@api.model
11+
def default_get(self, fields):
12+
res = super(WarrantyWizard, self).default_get(fields)
13+
active_id = self.env.context.get('active_id')
14+
if active_id:
15+
sale_order = self.env['sale.order'].browse(active_id)
16+
if not sale_order:
17+
return res
18+
19+
warranty_lines = []
20+
for line in sale_order.order_line:
21+
if line.product_template_id and line.product_template_id.is_warranty:
22+
warranty_lines.append((0, 0, {
23+
'sale_order_line_id': line.id,
24+
'product_id': line.product_template_id.id,
25+
}))
26+
27+
res.update({
28+
'sale_order_id': sale_order.id,
29+
'warranty_line_ids': warranty_lines,
30+
})
31+
return res
32+
33+
def action_add_warranty(self):
34+
new_order_line_list = []
35+
for record in self:
36+
sale_order = record.sale_order_id
37+
for line in record.warranty_line_ids:
38+
if line.warranty_config_id:
39+
new_order_line_list.append({
40+
"order_id": sale_order.id,
41+
"name": f"{line.warranty_config_id.name} / {line.end_date}",
42+
"product_id": line.warranty_config_id.product_id.product_variant_id.id,
43+
"price_unit": line.sale_order_line_id.price_subtotal
44+
* (line.warranty_config_id.percentage / 100),
45+
"warranty_id": line.sale_order_line_id.id,
46+
"sequence": line.sale_order_line_id.sequence,
47+
})
48+
49+
res = self.env['sale.order.line'].create(new_order_line_list)
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_config_manager,res.warranty.manager,model_warranty_config,base.group_user,1,1,1,1
3+
access_warranty_wizard_manager,res.warranty.wizard.managar,model_warranty_wizard,base.group_user,1,1,1,1
4+
access_warranty_wizard_line_manager,res.warranty.wizard.line.managar,model_warranty_line_wizard,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+
4+
<record id="is_warranty_button" model="ir.ui.view">
5+
<field name="name">product.template.is.warranty.available</field>
6+
<field name="model">product.template</field>
7+
<field name="inherit_id" ref="product.product_template_form_view" />
8+
<field name="arch" type="xml">
9+
<field name="product_tag_ids" position="after">
10+
<field name="is_warranty"/>
11+
</field>
12+
</field>
13+
</record>
14+
15+
</odoo>

0 commit comments

Comments
 (0)