|
| 1 | +from odoo import models, fields, api |
| 2 | + |
| 3 | + |
| 4 | +class KitProductsWizardLine(models.TransientModel): |
| 5 | + _name = "kit.products.wizard.line" |
| 6 | + _description = "Kit Products Wizard Line" |
| 7 | + |
| 8 | + wizard_line_id = fields.Many2one("kit.products.wizard", required=True) |
| 9 | + product_id = fields.Many2one("product.product", string="Product") |
| 10 | + product_quantity = fields.Float(string="Quantity") |
| 11 | + price = fields.Float(string="Unit Price") |
| 12 | + |
| 13 | + |
| 14 | +class KitProductsWizard(models.TransientModel): |
| 15 | + _name = "kit.products.wizard" |
| 16 | + _description = "Kit Products Wizard" |
| 17 | + |
| 18 | + sale_order_id = fields.Many2one("sale.order", string="Sale Order", readonly=True) |
| 19 | + product_template_id = fields.Many2one("product.template", string="Product", readonly=True) |
| 20 | + subProduct_ids = fields.One2many("kit.products.wizard.line", "wizard_line_id", string="Sub Products") |
| 21 | + |
| 22 | + @api.model |
| 23 | + def default_get(self, fields_list): |
| 24 | + res = super().default_get(fields_list) |
| 25 | + sale_order_id = self.env.context.get("default_sale_order_id") |
| 26 | + product_template_id = self.env.context.get("default_product_template_id") |
| 27 | + |
| 28 | + res.update({ |
| 29 | + "sale_order_id": sale_order_id, |
| 30 | + "product_template_id": product_template_id, |
| 31 | + }) |
| 32 | + |
| 33 | + if sale_order_id and product_template_id: |
| 34 | + product_template = self.env["product.template"].browse(product_template_id) |
| 35 | + sub_products = product_template.subProduct_ids |
| 36 | + |
| 37 | + existing_lines = self.env["sale.order.line"].search([ |
| 38 | + ("order_id", "=", sale_order_id), |
| 39 | + ("product_id", "in", sub_products.ids) |
| 40 | + ]) |
| 41 | + |
| 42 | + line_data = [] |
| 43 | + for sub_product in sub_products: |
| 44 | + existing_line = existing_lines.filtered(lambda l: l.product_id.id == sub_product.id) |
| 45 | + line_data.append((0, 0, { |
| 46 | + "product_id": sub_product.id, |
| 47 | + "product_quantity": existing_line.product_uom_qty if existing_line else 1, |
| 48 | + "price": existing_line.last_updated_price if existing_line else sub_product.list_price, |
| 49 | + })) |
| 50 | + |
| 51 | + res["subProduct_ids"] = line_data |
| 52 | + |
| 53 | + return res |
| 54 | + |
| 55 | + def action_open_wizard_popup(self): |
| 56 | + self.ensure_one() |
| 57 | + |
| 58 | + so_line_model = self.env["sale.order.line"] |
| 59 | + total_price = 0.0 |
| 60 | + |
| 61 | + for sub in self.subProduct_ids: |
| 62 | + sub_total = sub.product_quantity * sub.price |
| 63 | + total_price += sub_total |
| 64 | + |
| 65 | + line_vals = { |
| 66 | + "product_uom_qty": sub.product_quantity, |
| 67 | + "price_unit": 0.0, |
| 68 | + "last_updated_price": sub.price, |
| 69 | + } |
| 70 | + |
| 71 | + existing_line = so_line_model.search([ |
| 72 | + ("order_id", "=", self.sale_order_id.id), |
| 73 | + ("product_id", "=", sub.product_id.id), |
| 74 | + ("is_subProduct", "=", True), |
| 75 | + ], limit=1) |
| 76 | + |
| 77 | + if existing_line: |
| 78 | + existing_line.write(line_vals) |
| 79 | + else: |
| 80 | + line_vals.update({ |
| 81 | + "name": sub.product_id.name, |
| 82 | + "order_id": self.sale_order_id.id, |
| 83 | + "product_id": sub.product_id.id, |
| 84 | + "is_subProduct": True, |
| 85 | + }) |
| 86 | + so_line_model.create(line_vals) |
| 87 | + |
| 88 | + main_product = self.product_template_id.product_variant_id |
| 89 | + main_line = so_line_model.search([ |
| 90 | + ("order_id", "=", self.sale_order_id.id), |
| 91 | + ("product_id", "=", main_product.id), |
| 92 | + ], limit=1) |
| 93 | + |
| 94 | + if main_line: |
| 95 | + main_line.price_unit = total_price * main_line.product_uom_qty |
| 96 | + else: |
| 97 | + so_line_model.create({ |
| 98 | + "order_id": self.sale_order_id.id, |
| 99 | + "product_id": main_product.id, |
| 100 | + "product_uom_qty": 1, |
| 101 | + "price_unit": total_price, |
| 102 | + "name": self.product_template_id.name, |
| 103 | + }) |
| 104 | + |
| 105 | + return {"type": "ir.actions.act_window_close"} |
0 commit comments