| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from odoo import _, models |
| 5 |
from odoo.exceptions import UserError |
| 6 |
|
| 7 |
|
| 8 |
class MrpBom(models.Model): |
| 9 |
_inherit = 'mrp.bom' |
| 10 |
|
| 11 |
def write(self, vals): |
| 12 |
if not vals.get('active', True) or ('phantom' in self.mapped('type') and vals.get('type', 'phantom') != 'phantom'): |
| 13 |
self._ensure_bom_is_free() |
| 14 |
return super().write(vals) |
| 15 |
|
| 16 |
def unlink(self): |
| 17 |
self._ensure_bom_is_free() |
| 18 |
return super().unlink() |
| 19 |
|
| 20 |
def _ensure_bom_is_free(self): |
| 21 |
product_ids = [] |
| 22 |
for bom in self: |
| 23 |
if not bom.active or bom.type != 'phantom': |
| 24 |
continue |
| 25 |
product_ids += bom.product_id.ids or bom.product_tmpl_id.product_variant_ids.ids |
| 26 |
if not product_ids: |
| 27 |
return |
| 28 |
lines = self.env['sale.order.line'].sudo().search([ |
| 29 |
('state', '=', 'sale'), |
| 30 |
('invoice_status', 'in', ('no', 'to invoice')), |
| 31 |
('product_id', 'in', product_ids), |
| 32 |
('move_ids.state', '!=', 'cancel'), |
| 33 |
]) |
| 34 |
if lines: |
| 35 |
product_names = ', '.join(lines.product_id.mapped('display_name')) |
| 36 |
raise UserError(_('As long as there are some sale order lines that must be delivered/invoiced and are ' |
| 37 |
'related to these bills of materials, you can not remove them.\n' |
| 38 |
'The error concerns these products: %s', product_names)) |