Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

17.0 training koye #152

Draft
wants to merge 11 commits into
base: 18.0
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[ADD] warranty: add warranty management functionality
- Created AddWarranty button to open a wizard for warranty management.
- Implemented a wizard with an Add button to apply warranty to order lines if
available.
- Computed warranty price based on a percentage defined in the warranty
configuration.
- Integrated warranty product into the warranty configuration for better
 management.
koye-odoo committed Oct 4, 2024
commit aad8df4a4d883b6595887d23879951b88622abc9
3 changes: 0 additions & 3 deletions estate/views/property_controller_template.xml
Original file line number Diff line number Diff line change
@@ -81,9 +81,6 @@
</b>
<t t-out="prop_details.Postcode" />
</p>
<!-- <a href="/contactus" class="btn btn-lg btn-primary mt-4">Contact Us for
More
Information</a> -->
</div>
</div>
</div>
9 changes: 4 additions & 5 deletions warranty/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "warranty",
"depends": ["base", "stock", "sale_management"],
"depends": ["base", "sale_subscription"],
"Description": "warranty_Module",
"license": "LGPL-3",
"summary": "warranty module for different purpose",
@@ -10,10 +10,9 @@
"installable": True,
"data": [
"security/ir.model.access.csv",
"wizard/add_warranty_view.xml",
"views/warranty_config_view.xml",
"views/sale_order_view.xml",
"views/product_template_view.xml",
"views/warranty_configuration_views.xml",
"views/warranty_configuration_menus.xml",
"wizard/wizard_view.xml",
"views/sale_order_line_view.xml",
],
}
5 changes: 2 additions & 3 deletions warranty/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from . import product_template
from . import warranty_configuration
from . import sales_order_model
from . import sale_order_line
from . import sale_order
from . import product_template
3 changes: 2 additions & 1 deletion warranty/models/product_template.py
Original file line number Diff line number Diff line change
@@ -3,4 +3,5 @@

class ProductTemplate(models.Model):
_inherit = "product.template"
is_warranty_available = fields.Boolean(string="Is Warranty Available")

is_warranty_available = fields.Boolean(default=False)
28 changes: 28 additions & 0 deletions warranty/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from odoo import api, fields, models


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

warranty_year_id = fields.Many2one("warranty.config", string="Warranty")
warranty_end_date = fields.Date(string="Warranty End Date")


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

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

# ------------------------------------------ CRUD Method -------------------------------------
def unlink(self):
for line in self:
warranty_product_ids = self.env["sale.order.line"].search(
[("warranty_product_id", "=", line.id)]
)
if warranty_product_ids:
warranty_product_ids.unlink()

return super().unlink()
5 changes: 0 additions & 5 deletions warranty/models/sale_order_line.py

This file was deleted.

5 changes: 0 additions & 5 deletions warranty/models/sales_order_model.py

This file was deleted.

12 changes: 6 additions & 6 deletions warranty/models/warranty_configuration.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from odoo import fields, models


class WarrantyConfiguration(models.Model):
_name = "storewarranty.configuration"
_description = "model to store warranty configuration"
class WarrantyConfig(models.Model):
_name = "warranty.config"
_description = "Warranty Configuration for declaring warranty"

name = fields.Char(string="Name")
product_id = fields.Many2one("product.product", required=True)
name = fields.Char(string="Name", required=True)
period = fields.Integer(string="Period", required=True)
product_id = fields.Many2one("product.product", string="Product", required=True)
percentage = fields.Float(string="Percentage", required=True)
period = fields.Integer(string="year", required=True)
8 changes: 5 additions & 3 deletions warranty/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
warranty.access_storewarranty_configuration,access_storewarranty_configuration,warranty.model_storewarranty_configuration,base.group_user,1,1,1,1
warranty.access_warranty_wizard,access_warranty_wizard,warranty.model_warranty_wizard,base.group_user,1,1,1,1
warranty.access_add_warranty_lines,access_add_warranty_lines,warranty.model_add_warranty_lines,base.group_user,1,1,1,1
warranty.access_warranty_config,access_warranty_config,warranty.model_warranty_config,base.group_user,1,1,1,1
warranty.access_add_warranty,access_add_warranty,warranty.model_add_warranty,base.group_user,1,1,1,1
warranty.access_add_warranty_line,access_add_warranty_line,warranty.model_add_warranty_line,base.group_user,1,1,1,1


9 changes: 5 additions & 4 deletions warranty/views/product_template_view.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<odoo>
<record id="is_warranty_available_form_view" model="ir.ui.view">
<field name="name">is.warranty.available.form.view</field>
<record model="ir.ui.view" id="product_template_form_view">
<field name="name">product.template.product.website.form</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_form_view" />
<field name="arch" type="xml">
<xpath expr="//page[@name='sales']//group[@name='sale']" position="inside">
<!-- inside sales page adding is_warranty_available field -->
<xpath expr="//page[@name='sales']/group[@name='sale']" position="inside">
<group>
<field name="is_warranty_available" />
<field name="is_warranty_available" string="Is warranty available" />
</group>
</xpath>
</field>
14 changes: 0 additions & 14 deletions warranty/views/sale_order_line_view.xml

This file was deleted.

17 changes: 17 additions & 0 deletions warranty/views/sale_order_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="sale_order_view" model="ir.ui.view">
<field name="name">sale.order.form.inherit.view</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form" />
<field name="arch" type="xml">
<xpath expr="//div[@name='so_button_below_order_lines']/button" position="before">
<button name='%(warranty.add_warranty_action)d'
string="Add Warranty"
type="action"
class="btn-primary"
/>
</xpath>
</field>
</record>
</odoo>
26 changes: 26 additions & 0 deletions warranty/views/warranty_config_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<odoo>
<!-- Tree View for Warranty Configuration -->
<record id="warranty_config_tree_view" 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="period" />
<field name="percentage" />
</tree>
</field>
</record>
<!-- Action for Warranty Configuration-->
<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>
<field name="view_id" ref="warranty_config_tree_view" />
</record>
<!-- Adding Menu Item( in sales configuration) -->
<menuitem id="menu_warranty_config" name="Warranty Configuration"
parent="sale.menu_sale_config"
action="warranty_config_action" />
</odoo>
11 changes: 0 additions & 11 deletions warranty/views/warranty_configuration_menus.xml

This file was deleted.

14 changes: 0 additions & 14 deletions warranty/views/warranty_configuration_views.xml

This file was deleted.

83 changes: 61 additions & 22 deletions warranty/wizard/add_warranty.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,91 @@
from datetime import date
from datetime import datetime
from dateutil.relativedelta import relativedelta
from odoo import api, fields, models
from odoo import api, Command, fields, models


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

# ---------------------------------------- Private Attributes ---------------------------------
_name = "add.warranty"
_description = "Add Warranty Wizard"
# Relational Fields
sale_order_id = fields.Many2one("sale.order", string="Sale Order", required=True)
warranty_line_ids = fields.One2many("add.warranty.lines", "warranty_wizard_id")
warranty_line_ids = fields.One2many("add.warranty.line", "warranty_wizard_id")

@api.model
def default_get(self, fields):
result = super().default_get(fields)
sale_order = self.env["sale.order"].browse(self._context.get("active_id"))
result["sale_order_id"] = sale_order.id

# warranty lines from the sale order
warranty_lines = []
for line in sale_order.order_line:
print(line)
if line.product_id.is_warranty_available:
for ele in sale_order.order_line:
if ele.product_id.is_warranty_available:
warranty_lines.append(
(
0,
0,
{
"product_id": line.product_id.id,
"product_id": ele.product_id.id,
},
)
)
result["warranty_line_ids"] = warranty_lines
return result

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

# Iterating over warranty lines from the wizard
for line in self.warranty_line_ids:
if line.warranty_years:
for order_line in sale_order.order_line:
# Finding the product inside the sale order line
if order_line.product_id == line.product_id:
# Calculating the warranty price based on the original product line subtotal
price = (
order_line.price_subtotal * line.warranty_years.percentage
) / 100

# Appending the warranty as a separate sale order line
sale_order.order_line = [
Command.create(
{
"name": f"Extended Warranty\nEnd date: {line.end_date}",
"order_id": sale_order.id,
"product_id": line.warranty_years.product_id.id,
"product_uom": 1,
"product_uom_qty": 1,
"price_unit": price,
"warranty_product_id": order_line.id,
"tax_id": None,
}
)
]


class AddWarrantyLine(models.TransientModel):
_name = "add.warranty.lines"
# ---------------------------------------- Private Attributes ---------------------------------
_name = "add.warranty.line"
_description = "Add Warranty Line"

# Relational Fields
warranty_wizard_id = fields.Many2one(
"warranty.wizard", string="Warranty Wizard", required=True
"add.warranty", string="Warranty Wizard", required=True
)
product_id = fields.Many2one("product.product", string="Product", required=True)
year = fields.Many2one("storewarranty.configuration", string="Period")
end_date = fields.Date(string="End Date", compute="_onchange_year", store=True)

@api.onchange("year.period")
def _onchange_year(self):
if self.year:
self.end_date = date.today() + relativedelta(years=self.year.period)
else:
self.end_date = False
warranty_years = fields.Many2one(
"warranty.config", string="Warranty Period", required=True
)
# Computed Field
end_date = fields.Date(string="End Date", compute="_compute_end_date", store=True)

# ---------------------------------------- Compute method ------------------------------------
@api.depends("warranty_years")
def _compute_end_date(self):
for record in self:
if record.warranty_years:
time_period = record.warranty_years.period
record.end_date = datetime.now() + relativedelta(
years=time_period
)
36 changes: 36 additions & 0 deletions warranty/wizard/add_warranty_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<odoo>
<record id="add_warranty_action" 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>
<record id="add_warranty_tree_view" model="ir.ui.view">
<field name="name">add.warranty.tree.view</field>
<field name="model">add.warranty.line</field>
<field name="arch" type="xml">
<tree sample="1" editable="bottom" create="0">
<field name="product_id" />
<field name="warranty_years" />
<field name="end_date" />
</tree>
</field>
</record>
<record id="add_warranty_form_view" model="ir.ui.view">
<field name="name">add.warranty.tree.view</field>
<field name="model">add.warranty</field>
<field name="arch" type="xml">
<form>
<sheet>
<field name="warranty_line_ids"></field>
<footer>
<button name="add_warranty" type="object" string="Add" class="btn-primary"
data-hotkey="q" />
<button string="Cancel" class="btn-secondary" special="cancel"
data-hotkey="x" />
</footer>
</sheet>
</form>
</field>
</record>
</odoo>
19 changes: 0 additions & 19 deletions warranty/wizard/wizard_view.xml

This file was deleted.