|
| 1 | +from odoo import api, fields, models |
| 2 | + |
| 3 | + |
| 4 | +class SubProducts(models.TransientModel): |
| 5 | + _name = "sale.order.line.wizard" |
| 6 | + _description = "change.quantity.of.subproducts.in.kit" |
| 7 | + |
| 8 | + main_product = fields.Char(string="Main Product") |
| 9 | + line_ids = fields.One2many( |
| 10 | + "sale.order.line.wizard.line", "wizard_id", string="Sub Products" |
| 11 | + ) |
| 12 | + |
| 13 | + @api.model |
| 14 | + def default_get(self, fields_list): |
| 15 | + res = super().default_get(fields_list) |
| 16 | + active_id = self.env.context.get("active_id") |
| 17 | + main_order_line = self.env["sale.order.line"].browse(active_id) |
| 18 | + sale_order = main_order_line.order_id |
| 19 | + order_line = self.env["sale.order.line"].browse(active_id) |
| 20 | + res["main_product"] = order_line.product_template_id.name |
| 21 | + |
| 22 | + if order_line: |
| 23 | + lines = [] |
| 24 | + for product in order_line.product_template_id.sub_products: |
| 25 | + existing_line = self.env["sale.order.line"].search( |
| 26 | + [ |
| 27 | + ("order_id", "=", sale_order.id), |
| 28 | + ("main_order_line_id", "=", main_order_line.id), |
| 29 | + ("product_id", "=", product.id), |
| 30 | + ], |
| 31 | + limit=1, |
| 32 | + ) |
| 33 | + lines.append( |
| 34 | + ( |
| 35 | + 0, |
| 36 | + 0, |
| 37 | + { |
| 38 | + "product_id": product.id, |
| 39 | + "quantity": existing_line.product_uom_qty |
| 40 | + if existing_line |
| 41 | + else 1, |
| 42 | + "price": existing_line.wizard_price |
| 43 | + if existing_line |
| 44 | + else product.list_price, |
| 45 | + }, |
| 46 | + ) |
| 47 | + ) |
| 48 | + res["line_ids"] = lines |
| 49 | + return res |
| 50 | + |
| 51 | + def confirm_sub_products(self): |
| 52 | + active_id = self.env.context.get("active_id") |
| 53 | + main_order_line = self.env["sale.order.line"].browse(active_id) |
| 54 | + sale_order = main_order_line.order_id |
| 55 | + main_product_subtotal = ( |
| 56 | + main_order_line.price_unit * main_order_line.product_uom_qty |
| 57 | + ) |
| 58 | + for wizard in self: |
| 59 | + for line in wizard.line_ids: |
| 60 | + if line.quantity > 0: |
| 61 | + main_product_subtotal += line.quantity * line.price |
| 62 | + |
| 63 | + existing_line = self.env["sale.order.line"].search( |
| 64 | + [ |
| 65 | + ("order_id", "=", sale_order.id), |
| 66 | + ("main_order_line_id", "=", main_order_line.id), |
| 67 | + ("product_id", "=", line.product_id.id), |
| 68 | + ], |
| 69 | + limit=1, |
| 70 | + ) |
| 71 | + |
| 72 | + if existing_line: |
| 73 | + existing_line.write( |
| 74 | + { |
| 75 | + "product_uom_qty": line.quantity, |
| 76 | + "wizard_price": line.price, |
| 77 | + "price_unit": line.price, |
| 78 | + } |
| 79 | + ) |
| 80 | + else: |
| 81 | + self.env["sale.order.line"].create( |
| 82 | + { |
| 83 | + "order_id": sale_order.id, |
| 84 | + "product_id": line.product_id.id, |
| 85 | + "product_uom_qty": line.quantity, |
| 86 | + "name": line.product_id.name, |
| 87 | + "is_sub_product_ol": True, |
| 88 | + "wizard_price": line.price, |
| 89 | + "price_unit": line.price, |
| 90 | + "main_order_line_id": main_order_line.id, |
| 91 | + } |
| 92 | + ) |
| 93 | + main_order_line.write({"price_subtotal": main_product_subtotal}) |
0 commit comments