Skip to content

Commit

Permalink
[ADD] warranty: implemented Warranty Functionality to Sales and Inven…
Browse files Browse the repository at this point in the history
…tory

After this commit:

- Added warranty configuration model and settings.
- Extended sale order and sale order line models to include warranty fields.
- Implemented warranty wizard for adding warranties to sale orders.
- Created views for managing warranty settings and applying warranties to
  products.
- Added security rules to control access to warranty features.
  • Loading branch information
yasp-odoo committed Sep 26, 2024
1 parent 6e3c45f commit a7c8e66
Show file tree
Hide file tree
Showing 16 changed files with 214 additions and 0 deletions.
Binary file added warranty.zip
Binary file not shown.
2 changes: 2 additions & 0 deletions warranty/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import models
from . import wizard
25 changes: 25 additions & 0 deletions warranty/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
'name': 'Warranty',
'summary': 'Warranty Configuration Module',
'description': 'Module for managing warranty configurations and related functionalities.',
'category': 'Sales',
'version': '1.0',
'author': 'yasp',
'license': 'LGPL-3',
'depends': [
'base',
'sale_subscription',
'stock',
'website_sale',
],
'data': [
'security/ir.model.access.csv',
'wizard/warranty_wizard_views.xml',
'views/warranty_config_views.xml',
'views/product_views.xml',
'views/sale_order_views.xml',
'views/menu_views.xml',
],
'installable': True,
'application': True,
}
3 changes: 3 additions & 0 deletions warranty/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import warranty_config
from . import product_template
from . import sale_order_line
7 changes: 7 additions & 0 deletions warranty/models/product_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from odoo import models, fields


class ProductTemplate(models.Model):
_inherit = 'product.template'

is_warranty_available = fields.Boolean(string='Warranty Available', default=False)
14 changes: 14 additions & 0 deletions warranty/models/sale_order_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from odoo import models, fields


class SaleOrderLine(models.Model):
_inherit = "sale.order.line"

warranty_product_id = fields.Many2one("sale.order.line", string="Warranty Product")

def unlink(self):
for line in self:
warranty_product_id = self.search([("warranty_product_id", "=", line.id)])
if warranty_product_id:
warranty_product_id.unlink()
return super().unlink()
11 changes: 11 additions & 0 deletions warranty/models/warranty_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from odoo import fields, models


class WarrantyConfiguration(models.Model):
_name = 'warranty.config'
_description = 'Warranty Configuration'

name = fields.Char(string='Warranty Name', required=True)
product_id = fields.Many2one('product.product', string='Product', required=True)
percentage = fields.Float(string='Percentage (%)', required=True)
years = fields.Integer(string='Years', required=True)
4 changes: 4 additions & 0 deletions warranty/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_warranty_config,warranty.config,model_warranty_config,,1,1,1,1
access_add_warranty,access_add_warranty,warranty.model_add_warranty,base.group_user,1,1,1,1
access_add_warranty_line,access_add_warranty_line,warranty.model_add_warranty_line,base.group_user,1,1,1,1
6 changes: 6 additions & 0 deletions warranty/views/menu_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<odoo>

<menuitem id="menu_warranty_config" name="Warranty Configuration" parent="sale.menu_sale_config"
action="warranty_config_action" />

</odoo>
14 changes: 14 additions & 0 deletions warranty/views/product_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<odoo>
<record id="view_product_form_inherit_warranty" model="ir.ui.view">
<field name="name">product.form.inherit.warranty</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_only_form_view"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='product_template_image_ids']" position="after">
<group>
<field name="is_warranty_available"/>
</group>
</xpath>
</field>
</record>
</odoo>
12 changes: 12 additions & 0 deletions warranty/views/sale_order_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<odoo>
<record id="view_order_form" model="ir.ui.view">
<field name="name">sale.order.form</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="xml">
<xpath expr="//button[@name='action_open_discount_wizard']" position="before">
<button name="%(action_add_warranty)d" string="Add Warranty" type="action" class="oe_highlight"/>
</xpath>
</field>
</record>
</odoo>
21 changes: 21 additions & 0 deletions warranty/views/warranty_config_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<odoo>
<record id="view_warranty_config_tree" model="ir.ui.view">
<field name="name">warranty.config.tree</field>
<field name="model">warranty.config</field>
<field name="arch" type="xml">
<tree string="Warranty Configuration" editable="bottom">
<field name="name"/>
<field name="product_id"/>
<field name="percentage"/>
<field name="years"/>
</tree>
</field>
</record>

<record id="warranty_config_action" model="ir.actions.act_window">
<field name="name">Warranty Configuration</field>
<field name="res_model">warranty.config</field>
<field name="view_mode">tree</field>
</record>

</odoo>
1 change: 1 addition & 0 deletions warranty/wizard/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import warranty_wizard
64 changes: 64 additions & 0 deletions warranty/wizard/warranty_wizard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from odoo import models, fields, api, Command
from dateutil.relativedelta import relativedelta


class AddWarranty(models.TransientModel):
_name = "add.warranty"
_description = "Add Warranty Wizard"

sale_order_id = fields.Many2one("sale.order", string="Sale Order", required=True)
warranty_line_ids = fields.One2many("add.warranty.line", "warranty_wizard_id")

def default_get(self, fields):
res = super().default_get(fields)
sale_order = self.env["sale.order"].browse(self._context.get("active_id"))
res["sale_order_id"] = sale_order.id
warranty_lines = []
for line in sale_order.order_line:
if line.product_id.is_warranty_available:
warranty_lines.append(Command.create({"product_id": line.product_id.id}))
res["warranty_line_ids"] = warranty_lines
return res

def add_warranty(self):

active_id = self.env.context.get("active_id")
sale_order = self.env["sale.order"].browse(active_id)

for line in self.warranty_line_ids:
if line.warranty_id:
price = 0
for record in sale_order.order_line:
if record.product_id == line.product_id:
price = (record.price_subtotal * line.warranty_id.percentage) / 100
sale_order.order_line = [Command.create({
"product_id": line.warranty_id.product_id.id,
"name": "Extended Warranty",
"order_id": sale_order.id,
"product_uom": 1,
"product_uom_qty": 1,
"price_unit": price,
"tax_id": None,
"warranty_product_id": record.id,
})]


class AddWarrantyLine(models.TransientModel):
_name = "add.warranty.line"
_description = "Add Warranty Line"

warranty_wizard_id = fields.Many2one(
"add.warranty", string="Warranty Wizard", required=True
)
product_id = fields.Many2one("product.product", string="Product", required=True)
warranty_id = fields.Many2one("warranty.config", string="Warranty years", required=True)
end_date = fields.Date(string="End Date", compute="_compute_end_date", store=True)

@api.depends("warranty_id")
def _compute_end_date(self):
for record in self:
if record.warranty_id:
time_years = record.warranty_id.years
record.end_date = fields.Date.context_today(self) + relativedelta(
years=time_years
)
30 changes: 30 additions & 0 deletions warranty/wizard/warranty_wizard_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<odoo>
<record id="view_warranty_wizard_form" model="ir.ui.view">
<field name="name">add.warranty.form</field>
<field name="model">add.warranty</field>
<field name="arch" type="xml">
<form string="Add Warranty">
<group>
<field name="sale_order_id" invisible="1"/>
<field name="warranty_line_ids">
<tree editable="bottom" create="False" delete="False">
<field name="product_id"/>
<field name="warranty_id"/>
<field name="end_date" readonly="1"/>
</tree>
</field>
</group>
<footer>
<button string="Add" type="object" name="add_warranty" class="btn-primary"/>
<button string="Cancel" class="btn-secondary" special="cancel"/>
</footer>
</form>
</field>
</record>
<record id="action_add_warranty" model="ir.actions.act_window">
<field name="name">Add Warranty</field>
<field name="res_model">add.warranty</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>
</odoo>
Binary file added warranty_1.zip
Binary file not shown.

0 comments on commit a7c8e66

Please sign in to comment.