Skip to content

Commit 6058b9a

Browse files
committed
[IMP] warranty_configuration: created transient model for wizard
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.
1 parent 275224e commit 6058b9a

12 files changed

+143
-48
lines changed

warranty_configuration/__init__.py

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

warranty_configuration/__manifest__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
""",
1010
"data": [
1111
"security/ir.model.access.csv",
12-
"wizards/so_add_warranty_wizard.xml",
13-
12+
"wizards/add_warranty_wizard_view.xml",
1413
"views/sales_management_menu.xml",
1514
"views/warranty_configuration_views.xml",
1615
"views/product_views.xml",
Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
11
from odoo import models, api, fields
2+
from datetime import date
3+
from dateutil.relativedelta import relativedelta
4+
25

36
class SaleOrder(models.Model):
47
_inherit = "sale.order"
5-
6-
def action_open_warranty_wizard(self):
7-
print("-" * 100)
8-
# Properly define the return dictionary for the wizard action
9-
return {
10-
'type': 'ir.actions.act_window',
11-
'name': 'Add Warranty',
12-
'res_model': 'warranty.configuration', # Your wizard model
13-
'view_mode': 'form',
14-
'view_id': self.env.ref('warranty_configuration.view_add_warranty_wizard').id, # Adjust the module/view XML ID
15-
'target': 'new',
16-
'context': {
17-
'default_product_ids': self.order_line.ids, # Adjust based on the wizard's logic
18-
},
19-
}

warranty_configuration/models/sale_order_line.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,6 @@
22

33
class SaleOrderLine(models.Model):
44
_inherit = "sale.order.line"
5-
6-
@api.model_create_multi
7-
def create(self, vals_list):
8-
print(vals_list)
9-
return []
10-
11-
def action_add_warranty(self):
12-
print("-*- " * 100)
13-
print("hello from warranty !")
14-
print("-*- " * 100)
15-
16-
return []
5+
6+
# warranty_id =
7+

warranty_configuration/models/warranty_configuration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ class WarrantyConfiguration(models.Model):
99
required=True,
1010
string="Name",
1111
)
12-
product_id = fields.Many2one("product.product", string="Product")
12+
product_id = fields.Many2one("product.product", string="Product", index=True)
1313
year = fields.Integer(default=0, required=True)
1414
percentage = fields.Float(default=0.0, digits=(5, 2), required=True)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
22
warranty_configuration.access_warranty_configuration,access_warranty_configuration,warranty_configuration.model_warranty_configuration,base.group_user,1,1,1,1
3+
warranty_configuration.access_add_warranty_wizard,access_add_warranty_wizard,warranty_configuration.model_add_warranty_wizard,base.group_user,1,1,1,1
4+
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

warranty_configuration/views/so_add_warranty_button_view.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<field name="arch" type="xml">
77
<xpath expr="//div[@name='so_button_below_order_lines']" position="inside">
88
<button string="Add Warranty"
9-
name="action_open_warranty_wizard"
10-
type="object"
9+
name="%(action_open_warranty_wizard)d"
10+
type="action"
1111
class="btn btn-primary"
1212
/>
1313
</xpath>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from . import add_warranty_wizard
2+
from . import add_warranty_line_wizard
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from odoo import models, fields, api
2+
from datetime import date
3+
from dateutil.relativedelta import relativedelta
4+
5+
6+
class AddWarrantyLineWizard(models.TransientModel):
7+
_name = "add.warranty.line.wizard"
8+
_description = "Temporary line model for warranty in wizard"
9+
10+
wizard_id = fields.Many2one(
11+
"add.warranty.wizard",
12+
string="Wizard Reference",
13+
readonly=True,
14+
required=True,
15+
ondelete='cascade',
16+
)
17+
18+
product_id = fields.Many2one(
19+
"product.product",
20+
string="Product",
21+
readonly=True,
22+
ondelete='cascade',
23+
)
24+
warranty_config_id = fields.Many2one("warranty.configuration", string="Year")
25+
end_date = fields.Date(string="End Date", compute="_compute_end_date", store=True)
26+
27+
@api.depends("warranty_config_id")
28+
def _compute_end_date(self):
29+
today = date.today()
30+
for record in self:
31+
if record.warranty_config_id and record.warranty_config_id.year:
32+
record.end_date = today + relativedelta(
33+
years=int(record.warranty_config_id.year)
34+
)
35+
else:
36+
record.product_id = record.product_id
37+
record.end_date = False
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from odoo import models, fields, api, Command
2+
3+
4+
class AddWarrantyWizard(models.TransientModel):
5+
_name = "add.warranty.wizard"
6+
_description = "Wizard to Add Warranty"
7+
8+
order_id = fields.Many2one("sale.order", string="Sale Order")
9+
product_ids = fields.Many2many("product.product")
10+
warranty_lines_ids = fields.One2many(
11+
comodel_name="add.warranty.line.wizard",
12+
inverse_name="wizard_id",
13+
store=True,
14+
string="Warranty Lines",
15+
)
16+
17+
def action_add_warranty(self):
18+
print("-*- " * 100)
19+
20+
new_order_line_list = []
21+
# Iterate through the warranty lines in the wizard
22+
for record in self:
23+
print(record.order_id)
24+
warranty_product_ids = record.warranty_lines_ids.mapped("product_id.id")
25+
print("Warranty product IDs:", warranty_product_ids)
26+
27+
# # Iterate over order lines to find matching products
28+
# for ol in record.order_id.order_line:
29+
# new_order_line_list.append(ol)
30+
# print(
31+
# f"Checking Order Line Product: {ol.product_id.id} in Warranty Lines"
32+
# )
33+
34+
# if ol.product_id.id in warranty_product_ids:
35+
# print(f"Match found: {ol}")
36+
37+
print(new_order_line_list)
38+
print("hello from warranty !")
39+
print("-*- " * 100)
40+
41+
return new_order_line_list
42+
43+
@api.model
44+
def default_get(self, fields):
45+
res = super(AddWarrantyWizard, self).default_get(fields)
46+
order_id = self.env.context.get("active_id")
47+
sale_order = self.env["sale.order"].browse(order_id)
48+
49+
warranty_products = sale_order.order_line.mapped("product_id").filtered(
50+
lambda p: p.warranty
51+
)
52+
53+
warranty_line_vals = []
54+
for product in warranty_products:
55+
warranty_line_vals.append(Command.create({"product_id": product.id}))
56+
print(warranty_products)
57+
res["product_ids"] = [Command.set(warranty_products)]
58+
59+
return res

0 commit comments

Comments
 (0)