Skip to content

Commit

Permalink
[IMP] warranty_configuration: created transient model for wizard
Browse files Browse the repository at this point in the history
Updated module created transient models required to add order line as per
selected product warranty. facing issues in it.
updated wizard views to display only product which has warranty and select
particular warranty product to add in the orderlines of sales order.
  • Loading branch information
djip-odoo committed Dec 5, 2024
1 parent 424bc85 commit 4f7cd6c
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 48 deletions.
1 change: 1 addition & 0 deletions warranty_configuration/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import models
from . import wizards
3 changes: 1 addition & 2 deletions warranty_configuration/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
""",
"data": [
"security/ir.model.access.csv",
"wizards/so_add_warranty_wizard.xml",

"wizards/add_warranty_wizard_view.xml",
"views/sales_management_menu.xml",
"views/warranty_configuration_views.xml",
"views/product_views.xml",
Expand Down
18 changes: 3 additions & 15 deletions warranty_configuration/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
from odoo import models, api, fields
from datetime import date
from dateutil.relativedelta import relativedelta


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

def action_open_warranty_wizard(self):
print("-" * 100)
# Properly define the return dictionary for the wizard action
return {
'type': 'ir.actions.act_window',
'name': 'Add Warranty',
'res_model': 'warranty.configuration', # Your wizard model
'view_mode': 'form',
'view_id': self.env.ref('warranty_configuration.view_add_warranty_wizard').id, # Adjust the module/view XML ID
'target': 'new',
'context': {
'default_product_ids': self.order_line.ids, # Adjust based on the wizard's logic
},
}
15 changes: 3 additions & 12 deletions warranty_configuration/models/sale_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@

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

@api.model_create_multi
def create(self, vals_list):
print(vals_list)
return []

def action_add_warranty(self):
print("-*- " * 100)
print("hello from warranty !")
print("-*- " * 100)

return []

# warranty_id =

2 changes: 1 addition & 1 deletion warranty_configuration/models/warranty_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ class WarrantyConfiguration(models.Model):
required=True,
string="Name",
)
product_id = fields.Many2one("product.product", string="Product")
product_id = fields.Many2one("product.product", string="Product", index=True)
year = fields.Integer(default=0, required=True)
percentage = fields.Float(default=0.0, digits=(5, 2), required=True)
2 changes: 2 additions & 0 deletions warranty_configuration/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
warranty_configuration.access_warranty_configuration,access_warranty_configuration,warranty_configuration.model_warranty_configuration,base.group_user,1,1,1,1
warranty_configuration.access_add_warranty_wizard,access_add_warranty_wizard,warranty_configuration.model_add_warranty_wizard,base.group_user,1,1,1,1
warranty_configuration.access_add_warranty_line_wizard,access_add_warranty_line_wizard,warranty_configuration.model_add_warranty_line_wizard,base.group_user,1,1,1,1
4 changes: 2 additions & 2 deletions warranty_configuration/views/so_add_warranty_button_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<field name="arch" type="xml">
<xpath expr="//div[@name='so_button_below_order_lines']" position="inside">
<button string="Add Warranty"
name="action_open_warranty_wizard"
type="object"
name="%(action_open_warranty_wizard)d"
type="action"
class="btn btn-primary"
/>
</xpath>
Expand Down
2 changes: 2 additions & 0 deletions warranty_configuration/wizards/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import add_warranty_wizard
from . import add_warranty_line_wizard
37 changes: 37 additions & 0 deletions warranty_configuration/wizards/add_warranty_line_wizard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from odoo import models, fields, api
from datetime import date
from dateutil.relativedelta import relativedelta


class AddWarrantyLineWizard(models.TransientModel):
_name = "add.warranty.line.wizard"
_description = "Temporary line model for warranty in wizard"

wizard_id = fields.Many2one(
"add.warranty.wizard",
string="Wizard Reference",
readonly=True,
required=True,
ondelete='cascade',
)

product_id = fields.Many2one(
"product.product",
string="Product",
readonly=True,
ondelete='cascade',
)
warranty_config_id = fields.Many2one("warranty.configuration", string="Year")
end_date = fields.Date(string="End Date", compute="_compute_end_date", store=True)

@api.depends("warranty_config_id")
def _compute_end_date(self):
today = date.today()
for record in self:
if record.warranty_config_id and record.warranty_config_id.year:
record.end_date = today + relativedelta(
years=int(record.warranty_config_id.year)
)
else:
record.product_id = record.product_id
record.end_date = False
59 changes: 59 additions & 0 deletions warranty_configuration/wizards/add_warranty_wizard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from odoo import models, fields, api, Command


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

order_id = fields.Many2one("sale.order", string="Sale Order")
product_ids = fields.Many2many("product.product")
warranty_lines_ids = fields.One2many(
comodel_name="add.warranty.line.wizard",
inverse_name="wizard_id",
store=True,
string="Warranty Lines",
)

def action_add_warranty(self):
print("-*- " * 100)

new_order_line_list = []
# Iterate through the warranty lines in the wizard
for record in self:
print(record.order_id)
warranty_product_ids = record.warranty_lines_ids.mapped("product_id.id")
print("Warranty product IDs:", warranty_product_ids)

# # Iterate over order lines to find matching products
# for ol in record.order_id.order_line:
# new_order_line_list.append(ol)
# print(
# f"Checking Order Line Product: {ol.product_id.id} in Warranty Lines"
# )

# if ol.product_id.id in warranty_product_ids:
# print(f"Match found: {ol}")

print(new_order_line_list)
print("hello from warranty !")
print("-*- " * 100)

return new_order_line_list

@api.model
def default_get(self, fields):
res = super(AddWarrantyWizard, self).default_get(fields)
order_id = self.env.context.get("active_id")
sale_order = self.env["sale.order"].browse(order_id)

warranty_products = sale_order.order_line.mapped("product_id").filtered(
lambda p: p.warranty
)

warranty_line_vals = []
for product in warranty_products:
warranty_line_vals.append(Command.create({"product_id": product.id}))
print(warranty_products)
res["product_ids"] = [Command.set(warranty_products)]

return res
32 changes: 32 additions & 0 deletions warranty_configuration/wizards/add_warranty_wizard_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<odoo>
<record id="view_add_warranty_wizard" model="ir.ui.view">
<field name="name">add.warranty.wizard.form</field>
<field name="model">add.warranty.wizard</field>
<field name="arch" type="xml">
<form string="Add Warranty" editable="false">
<sheet>
<field name="warranty_lines_ids">
<list create="false" no_open="1" editable="top" >
<field name="product_id" readonly="1" />
<field name="warranty_config_id" />
<field name="end_date" readonly="1" />
</list>
</field>
</sheet>
<footer>
<button string="Add" name="action_add_warranty" type="object"
class="btn-primary" />
<button string="Cancel" class="btn-secondary" special="cancel" />
</footer>
</form>
</field>
</record>

<record id="action_open_warranty_wizard" model="ir.actions.act_window">
<field name="name">Add Warranty</field>
<field name="res_model">add.warranty.wizard</field>
<field name="view_mode">form</field>
<field name="view_id" ref="view_add_warranty_wizard" />
<field name="target">new</field>
</record>
</odoo>
16 changes: 0 additions & 16 deletions warranty_configuration/wizards/so_add_warranty_wizard.xml

This file was deleted.

0 comments on commit 4f7cd6c

Please sign in to comment.