|
| 1 | +from odoo import models, fields, api |
| 2 | +from odoo.exceptions import UserError |
| 3 | + |
| 4 | + |
| 5 | +class SaleOrderLine(models.Model): |
| 6 | + _inherit = "sale.order.line" |
| 7 | + |
| 8 | + is_kit = fields.Boolean(related="product_template_id.is_kit") |
| 9 | + parent_line_id = fields.Many2one("sale.order.line", ondelete="cascade") |
| 10 | + |
| 11 | + @api.ondelete(at_uninstall=False) |
| 12 | + def _ondelete_sale_order_line(self): |
| 13 | + if not self.parent_line_id: |
| 14 | + for line in self: |
| 15 | + sub_product_lines = self.env["sale.order.line"].search( |
| 16 | + [("parent_line_id", "=", line.id)] |
| 17 | + ) |
| 18 | + sub_product_lines.with_context(allow_child_unlink=True).unlink() |
| 19 | + else: |
| 20 | + if not self.env.context.get("allow_child_unlink"): |
| 21 | + raise UserError( |
| 22 | + "You cannot delete a child line directly. Please delete the parent line instead." |
| 23 | + ) |
| 24 | + |
| 25 | + def write(self, vals): |
| 26 | + if "product_uom_qty" in vals and not self.parent_line_id: |
| 27 | + for line in self: |
| 28 | + sub_product_lines = self.env["sale.order.line"].search( |
| 29 | + [("parent_line_id", "=", line.id)] |
| 30 | + ) |
| 31 | + old_qty = line.product_uom_qty |
| 32 | + for sub_line in sub_product_lines: |
| 33 | + if sub_line: |
| 34 | + if old_qty != 0: |
| 35 | + qty = sub_line.product_uom_qty / old_qty |
| 36 | + new_qty = vals["product_uom_qty"] * qty |
| 37 | + sub_line.update({"product_uom_qty": new_qty}) |
| 38 | + else: |
| 39 | + new_qty = vals["product_uom_qty"] * sub_line.product_uom_qty |
| 40 | + sub_line.update({"product_uom_qty": new_qty}) |
| 41 | + return super().write(vals) |
| 42 | + |
| 43 | + def action_subproduct(self): |
| 44 | + return { |
| 45 | + "type": "ir.actions.act_window", |
| 46 | + "name": f"Product: {self.product_id.name}", |
| 47 | + "res_model": "sub.product.wizard", |
| 48 | + "view_mode": "form", |
| 49 | + "target": "new", |
| 50 | + "context": {"default_sale_order_line_id": self.id}, |
| 51 | + } |
0 commit comments