|
| 1 | +from odoo import api, models, fields |
| 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_sub_product_ol = fields.Boolean(default=False) |
| 9 | + wizard_price = fields.Float() |
| 10 | + main_order_line_id = fields.Many2one( |
| 11 | + "sale.order.line", |
| 12 | + string="Parent Line", |
| 13 | + ondelete="cascade", |
| 14 | + ) |
| 15 | + |
| 16 | + child_line_ids = fields.One2many( |
| 17 | + "sale.order.line", |
| 18 | + "main_order_line_id", |
| 19 | + string="Child Lines", |
| 20 | + ) |
| 21 | + display_price = fields.Float( |
| 22 | + compute="_compute_display_price", inverse="_compute_unit_price" |
| 23 | + ) |
| 24 | + display_sub_total = fields.Float(compute="_compute_amount_price") |
| 25 | + |
| 26 | + @api.depends("price_unit", "is_sub_product_ol") |
| 27 | + def _compute_display_price(self): |
| 28 | + for line in self: |
| 29 | + line.display_price = 0.0 if line.is_sub_product_ol else line.price_unit |
| 30 | + |
| 31 | + @api.depends("display_price", "price_subtotal", "is_sub_product_ol") |
| 32 | + def _compute_amount_price(self): |
| 33 | + for line in self: |
| 34 | + line.display_sub_total = ( |
| 35 | + 0.0 if line.is_sub_product_ol else line.price_subtotal |
| 36 | + ) |
| 37 | + |
| 38 | + def _compute_unit_price(self): |
| 39 | + for line in self: |
| 40 | + line.price_unit = line.display_price |
| 41 | + |
| 42 | + def unlink(self): |
| 43 | + for line in self: |
| 44 | + line.main_order_line_id.price_subtotal -= ( |
| 45 | + line.product_uom_qty * line.wizard_price |
| 46 | + ) |
| 47 | + |
| 48 | + return super().unlink() |
| 49 | + |
| 50 | + def open_sub_product_wizard(self): |
| 51 | + return { |
| 52 | + "name": "Sub Products Wizard", |
| 53 | + "type": "ir.actions.act_window", |
| 54 | + "res_model": "sale.order.line.wizard", |
| 55 | + "view_mode": "form", |
| 56 | + "target": "new", |
| 57 | + "context": {"active_id": self.id}, |
| 58 | + } |
0 commit comments