|
| 1 | +from odoo import _, fields, models |
| 2 | + |
| 3 | + |
| 4 | +class SaleOrderLine(models.Model): |
| 5 | + _inherit = 'sale.order.line' |
| 6 | + |
| 7 | + is_kit = fields.Boolean(related="product_template_id.is_kit") |
| 8 | + is_kit_component = fields.Boolean(string="Is Subproduct") |
| 9 | + kit_parent_line_id = fields.Many2one('sale.order.line', string="Kit Parent Line", ondelete="cascade") |
| 10 | + kit_unit_cost = fields.Float(string="Unit Price (Wizard)", default=0.0) |
| 11 | + |
| 12 | + def action_open_kit_wizard(self): |
| 13 | + return { |
| 14 | + 'name': 'Kit Components', |
| 15 | + 'type': 'ir.actions.act_window', |
| 16 | + 'res_model': 'kit.wizard', |
| 17 | + 'view_mode': 'form', |
| 18 | + 'target': 'new', |
| 19 | + 'context': { |
| 20 | + "active_id": self.order_id.id, |
| 21 | + 'default_product_id': self.product_id.id, |
| 22 | + 'default_sale_order_line_id': self.id, |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + def unlink(self): |
| 27 | + # Identify sub-product lines |
| 28 | + sub_products = self.filtered(lambda line: line.is_kit_component) |
| 29 | + |
| 30 | + if sub_products and not self.env.context.get('allow_sub_product_deletion'): |
| 31 | + if self == sub_products: |
| 32 | + raise models.UserError(_("You cannot delete kit sub-products directly. Delete the main kit line instead.")) |
| 33 | + return (self - sub_products).unlink() |
| 34 | + |
| 35 | + child_lines = self.env['sale.order.line'].search([ |
| 36 | + ('kit_parent_line_id', 'in', self.ids) |
| 37 | + ]) |
| 38 | + |
| 39 | + if child_lines: |
| 40 | + child_lines.with_context(allow_sub_product_deletion=True).unlink() |
| 41 | + |
| 42 | + return super().unlink() |
0 commit comments