|
1 |
| -from odoo import models, fields, api, Command |
| 1 | +from odoo import models, fields, api |
2 | 2 |
|
3 | 3 |
|
4 | 4 | class AddWarrantyWizard(models.TransientModel):
|
5 | 5 | _name = "add.warranty.wizard"
|
6 | 6 | _description = "Wizard to Add Warranty"
|
7 | 7 |
|
8 | 8 | order_id = fields.Many2one("sale.order", string="Sale Order")
|
9 |
| - product_ids = fields.Many2many("product.product") |
| 9 | + |
10 | 10 | warranty_lines_ids = fields.One2many(
|
11 | 11 | comodel_name="add.warranty.line.wizard",
|
12 |
| - inverse_name="wizard_id", |
13 |
| - store=True, |
| 12 | + inverse_name="warranty_id", |
14 | 13 | string="Warranty Lines",
|
15 | 14 | )
|
16 | 15 |
|
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 | 16 | @api.model
|
44 | 17 | def default_get(self, fields):
|
45 | 18 | res = super(AddWarrantyWizard, self).default_get(fields)
|
46 | 19 | order_id = self.env.context.get("active_id")
|
47 |
| - sale_order = self.env["sale.order"].browse(order_id) |
48 | 20 |
|
49 |
| - warranty_products = sale_order.order_line.mapped("product_id").filtered( |
50 |
| - lambda p: p.warranty |
51 |
| - ) |
| 21 | + sale_order = self.env["sale.order"].browse(order_id) |
52 | 22 |
|
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)] |
| 23 | + res["warranty_lines_ids"] = [ |
| 24 | + [ |
| 25 | + 0, |
| 26 | + 0, |
| 27 | + { |
| 28 | + "sale_order_line_id": line.id, |
| 29 | + }, |
| 30 | + ] |
| 31 | + for line in sale_order.order_line.filtered( |
| 32 | + lambda x: x.product_template_id.warranty |
| 33 | + ) |
| 34 | + ] |
| 35 | + res["order_id"] = order_id |
58 | 36 |
|
59 | 37 | return res
|
| 38 | + |
| 39 | + def action_add_warranty(self): |
| 40 | + new_order_line_list = [ |
| 41 | + { |
| 42 | + "order_id": line.sale_order_line_id.order_id.id, |
| 43 | + "name": str(line.warranty_config_id.name) + "/" + str(line.end_date), |
| 44 | + "price_unit": line.sale_order_line_id.price_subtotal |
| 45 | + * (line.warranty_config_id.percentage / 100), |
| 46 | + "product_id": line.warranty_config_id.product_id.id, |
| 47 | + "parent_sale_order_line_id": line.sale_order_line_id.id, |
| 48 | + "sequence": line.sale_order_line_id.sequence, |
| 49 | + } |
| 50 | + for line in self.warranty_lines_ids.filtered( |
| 51 | + lambda x: x.warranty_config_id.name |
| 52 | + ) |
| 53 | + ] |
| 54 | + self.env["sale.order.line"].create(new_order_line_list) |
0 commit comments