Skip to content

Commit

Permalink
[IMP] warranty: implemented the on delete warranty with product
Browse files Browse the repository at this point in the history
- Implement the on delete on sale order line to delete the product then delete
  the warranty product also.
  • Loading branch information
amya-odoo committed Sep 25, 2024
1 parent 0b1f226 commit f863581
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 112 deletions.
1 change: 0 additions & 1 deletion installment/models/installment_installment.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from datetime import timedelta
import logging
from odoo import api, Command, models, fields


Expand Down
2 changes: 1 addition & 1 deletion installment/models/res_config_setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ResConfigSettings(models.TransientModel):
)
delay_penalty_percentage = fields.Integer(string="Delay Penalty Percentage", config_parameter="installment.delay_penalty_percentage")
delay_penalty_process = fields.Integer(string="Delay Penalty Process", config_parameter="installment.delay_penalty_process")
nid = fields.Boolean(string="National ID (NID)",config_parameter="installment.nid")
nid = fields.Boolean(string="National ID (NID)", config_parameter="installment.nid")
salary_components = fields.Boolean(string="Salary Components", config_parameter="installment.salary_components")
bank_statement = fields.Boolean(string="Bank Statement", config_parameter="installment.bank_statement")
bank_rate_letter = fields.Boolean(string="Bank Rate Letter", config_parameter="installment.bank_rate_letter")
Expand Down
2 changes: 1 addition & 1 deletion installment/wizard/add_emi.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,5 @@ def add_installment(self):
"product_uom": self.env.ref("uom.product_uom_unit").id,
"tax_id": None,
})]

return
2 changes: 1 addition & 1 deletion warranty/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from . import models
from . import wizard
from . import wizard
2 changes: 1 addition & 1 deletion warranty/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from . import warranty_config
from . import product_template
from . import sale_order
from . import sale_order_line
from . import sale_order_line
56 changes: 20 additions & 36 deletions warranty/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,24 @@ class SalesOrder(models.Model):
_inherit = "sale.order"

@api.onchange('order_line')
def _delete_warranty_onchange(self):
print("on change")
# self.order_line.unlink
product_list = []
def _onchange_delete_warranty(self):
for line in self.order_line:
product_list.append(line.product_template_id.id)

product =set(product_list)
print(product)

# warranty_list = []
# for line in self.order_line:
# print(f"{line.name}, {line.product_template_id.warranty_available}, id- {line.id}, parent_id- {line.warranty_product_id}")
# print(f"-->name {line.product_template_id.name}, id- {line.product_template_id.id}")

# if line.warranty_product_id:
# warranty_list.append(line.warranty_product_id)
# else:
# product_list.append(line.product_template_id.id)
# print(warranty_list)
# print(product_list)

# # Assuming product_list is a list, we convert it to a set for faster lookups
# product_set = set(product_list)

# for line in self.order_line:
# if line.warranty_product_id and line.warranty_product_id not in product_set:
# line.unlink()
# parent = self.env['sale.order.line'].browse(line.warranty_product_id)
# parent.unlink()



@api.ondelete(at_uninstall=False)
def _delete_order_line_product(self):

print("deletion called")

if line.warranty_product_id:
flag = True

for record in self.order_line:

line_id = str(record.id)
line_id = line_id.split('_')

if len(line_id) > 2:
continue

line_id = int(line_id[-1])

if int(line_id) == line.warranty_product_id:
flag = False

if flag:
self.order_line -= line
26 changes: 2 additions & 24 deletions warranty/models/sale_order_line.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,7 @@
from odoo import api, fields, models
from odoo import fields, models


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

warranty_product_id = fields.Integer("Attached Product Id")

# @api.ondelete(at_uninstall=False)
# def _delete_recode_with_warranty(self):
# print("Executed the delition of warranty along with product.")
# product_id = None

# for record in self:
# product_id = record.id
# warranty = self.env['sale.order.line'].search([("warranty_product_id", "=", product_id)])
# warranty.unlink()
# print(warranty.id)
# print("Executed the delition of warranty along with product.", product_id)

def unlink(self):
for line in self:
# Check if any other order lines have this line as their warranty product
warranty_lines = self.search([('warranty_product_id', '=', line.id)])
if warranty_lines:
# If warranty lines exist, delete them
warranty_lines.unlink()

# Call super to delete the original order lines (including the product being removed)
return super(SaleOrderLine, self).unlink()
7 changes: 4 additions & 3 deletions warranty/views/product_template_views.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<odoo>

<record id="warranty_product_template_form_view" model="ir.ui.view">
<field name="name">warranty.product.template.product.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']" position="inside">
<group string="Warranty" name="warranty">
<field name="warranty_available"/>
</group>
</xpath>

</field>
</record>



</odoo>
</odoo>
4 changes: 3 additions & 1 deletion warranty/views/sale_order.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
<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.action_add_warranty" class="btn btn-primary" invisible="state not in ['draft']" string="Add Warranty" type="action"/>
<button name="warranty.action_add_warranty" invisible="state not in ['draft']" string="Add Warranty" type="action"/>
</xpath>

</field>
</record>
</odoo>
1 change: 1 addition & 0 deletions warranty/wizard/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import add_waranty_wizard
from . import warranty_add_line
46 changes: 14 additions & 32 deletions warranty/wizard/add_waranty_wizard.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from datetime import date
from dateutil.relativedelta import relativedelta
from odoo import api, fields, models


class AddWarranty(models.TransientModel):
_name = "warranty.add"

product_ids = fields.One2many('warranty.add.line', 'warranty_id')

@api.model
Expand All @@ -14,11 +13,13 @@ def default_get(self, fields):
sale_order = self.env["sale.order"].browse(active_id)

products_to_add = []
for product in sale_order.order_line:
for line in sale_order.order_line:

if line.product_template_id.warranty_available:

if product.product_template_id.warranty_available:
product_values = {
'product': product.product_template_id.id,
'product_id': line.id,
'name': line.name,
}

products_to_add.append((0, 0, product_values))
Expand All @@ -27,43 +28,24 @@ def default_get(self, fields):
return res

def add_warranty_wizard_action(self):
print("clicked ADD")

active_id = self.env.context.get('active_id')
sale_order = self.env['sale.order'].browse(active_id)
# Product tempolate id iterate

for record in self.product_ids:
# print(record)
# print(record.product.name)

if record.year:
sale_order_line = sale_order.order_line.filtered(
lambda line: line.product_template_id == record.product)
# print("t-> ", sale_order_line.name)

price = sale_order_line.price_subtotal * (record.year.percentage/100)
# print("p->", price)

sale_order_line = sale_order.order_line.filtered(lambda line: line == record.product_id)

price = sale_order_line.price_subtotal * (record.year.percentage / 100)

sale_order.order_line.create({
"name": f"{record.year.product_id.name}/ {record.end_date}",
"order_id": sale_order.id,
"price_unit": price,
"product_id": record.year.product_id.id,
"product_uom": self.env.ref("uom.product_uom_unit").id,
"tax_id": None,
"warranty_product_id": sale_order_line.product_template_id.id
"warranty_product_id": sale_order_line.id
})


class ProductList(models.TransientModel):
_name = "warranty.add.line"

warranty_id = fields.Many2one("warranty.add")
product = fields.Many2one("product.template", string="Product")
year = fields.Many2one("warranty.config")
end_date = fields.Date(compute="_compute_end_date", readonly=True)

@api.depends('year')
def _compute_end_date(self):
print("end date computed")
for record in self:
if record.year != None:
record.end_date = fields.Date.today() + relativedelta(years=record.year.period)
14 changes: 3 additions & 11 deletions warranty/wizard/add_warranty_wizard.xml
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
<odoo>

<!-- <record id="action_add_warranty_products" model="ir.actions.act_window">
<field name="name">Add Warranty</field>
<field name="res_model">warranty.add.line</field>
<field name="view_mode">tree</field>
</record> -->

<record id='add_warranty_wizard_product_tree' model='ir.ui.view'>
<field name="name">Warranty wizard products</field>
<field name="model">warranty.add.line</field>
<field name="arch" type="xml">
<tree string="Products" editable="bottom" create='0'>
<field name="product"/>
<field name="product_id" optional="hide"/>
<field name="name" readonly='1'/>
<field name="year"/>
<field name="end_date"/>
</tree>
</field>
</record>


<record id='add_warranty_form' model='ir.ui.view'>
<field name="name">Add Warranty</field>
<field name="model">warranty.add</field>
Expand All @@ -40,6 +34,4 @@
<field name="target">new</field>
</record>



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


class WarrantyAddLine(models.TransientModel):
_name = "warranty.add.line"

name = fields.Char(string="Product")
product_id = fields.Many2one("sale.order.line")
warranty_id = fields.Many2one("warranty.add")
year = fields.Many2one("warranty.config")
end_date = fields.Date(compute="_compute_end_date", readonly=True)

@api.depends('year')
def _compute_end_date(self):
for record in self:
if record.year is not None:
record.end_date = fields.Date.today() + relativedelta(years=record.year.period)

0 comments on commit f863581

Please sign in to comment.