|
| 1 | +from odoo import api, Command, fields, models |
| 2 | + |
| 3 | + |
| 4 | +class KitWizard(models.TransientModel): |
| 5 | + _name = 'kit.wizard' |
| 6 | + _description = 'Kit Wizard' |
| 7 | + |
| 8 | + product_id = fields.Many2one('product.product', string='Product', required=True) |
| 9 | + kit_line_ids = fields.One2many('kit.wizard.line', 'wizard_id', string="Sub Products") |
| 10 | + sale_order_line_id = fields.Many2one('sale.order.line', string="Parent Line") |
| 11 | + |
| 12 | + @api.model |
| 13 | + def default_get(self, fields): |
| 14 | + res = super().default_get(fields) |
| 15 | + product_id = self.env.context.get("default_product_id") |
| 16 | + order_id = self.env.context.get("active_id") |
| 17 | + sale_order_line_id = self.env.context.get("default_sale_order_line_id") |
| 18 | + |
| 19 | + if not product_id or not order_id: |
| 20 | + return res |
| 21 | + |
| 22 | + product = self.env["product.product"].browse(product_id) |
| 23 | + order = self.env["sale.order"].browse(order_id) |
| 24 | + sub_products = product.sub_product_ids |
| 25 | + lines = [] |
| 26 | + |
| 27 | + for sub in sub_products: |
| 28 | + existing_line = order.order_line.filtered( |
| 29 | + lambda l: l.product_id.id == sub.id |
| 30 | + and l.kit_parent_line_id |
| 31 | + and l.kit_parent_line_id.id == sale_order_line_id |
| 32 | + ) |
| 33 | + lines.append(Command.create({ |
| 34 | + 'product_id': sub.id, |
| 35 | + 'quantity': existing_line.product_uom_qty if existing_line else 1.0, |
| 36 | + 'price': existing_line.kit_unit_cost if existing_line else sub.lst_price, |
| 37 | + })) |
| 38 | + |
| 39 | + res.update({ |
| 40 | + 'product_id': product_id, |
| 41 | + 'kit_line_ids': lines, |
| 42 | + 'sale_order_line_id': sale_order_line_id, |
| 43 | + }) |
| 44 | + return res |
| 45 | + |
| 46 | + def action_confirm(self): |
| 47 | + order_id = self.env.context.get("active_id") |
| 48 | + sale_order_line_id = self.env.context.get("default_sale_order_line_id") |
| 49 | + order = self.env["sale.order"].browse(order_id) |
| 50 | + parent_line = order.order_line.filtered(lambda l: l.id == sale_order_line_id) |
| 51 | + total_price = self.product_id.lst_price |
| 52 | + |
| 53 | + for line in self.kit_line_ids: |
| 54 | + existing_line = order.order_line.filtered( |
| 55 | + lambda l: l.product_id.id == line.product_id.id |
| 56 | + and l.kit_parent_line_id |
| 57 | + and l.kit_parent_line_id.id == sale_order_line_id |
| 58 | + ) |
| 59 | + if existing_line: |
| 60 | + existing_line.write({ |
| 61 | + 'product_uom_qty': line.quantity, |
| 62 | + 'kit_unit_cost': line.price, |
| 63 | + 'price_unit': 0, |
| 64 | + }) |
| 65 | + else: |
| 66 | + self.env['sale.order.line'].create({ |
| 67 | + 'order_id': order.id, |
| 68 | + 'product_id': line.product_id.id, |
| 69 | + 'product_uom_qty': line.quantity, |
| 70 | + 'kit_unit_cost': line.price, |
| 71 | + 'price_unit': 0.0, |
| 72 | + 'is_kit_component': True, |
| 73 | + "sequence": parent_line.sequence, |
| 74 | + 'kit_parent_line_id': sale_order_line_id, |
| 75 | + # 'name': product.name, |
| 76 | + }) |
| 77 | + |
| 78 | + total_price += line.quantity * line.price |
| 79 | + parent_line.write({"price_unit": total_price}) |
0 commit comments