[IMP] mrp: block updating live BoM
When modifying a BoM that is used in a MO not done, raise a blocking warning to avoid inconsistenties at production. Proudly pair programmed with pim <pim@odoo.com>. Some mrp tests has been adapted to not raise the new error. They used demo data while they should created inside the test. This commit is related to task id : 1855566
This commit is contained in:
@@ -68,6 +68,17 @@ class MrpBom(models.Model):
|
||||
for line in self.bom_line_ids:
|
||||
line.attribute_value_ids = False
|
||||
|
||||
@api.multi
|
||||
def write(self, values):
|
||||
mos = self.env['mrp.production'].search_count([
|
||||
('bom_id', 'in', self.ids),
|
||||
('state', 'not in', ['done', 'cancel'])
|
||||
])
|
||||
if mos and any(k not in ['code', 'sequence', 'pick_type_id', 'ready_to_produce'] for k in values.keys()):
|
||||
raise ValidationError(_('This BoM is used in some Manufacturing Orders that are still open. You should rather archive this BoM and create a new one.'))
|
||||
return super(MrpBom, self).write(values)
|
||||
|
||||
|
||||
@api.constrains('product_id', 'product_tmpl_id', 'bom_line_ids')
|
||||
def _check_product_recursion(self):
|
||||
for bom in self:
|
||||
|
||||
@@ -228,3 +228,19 @@ class TestBoM(TestMrpCommon):
|
||||
})
|
||||
# Check consumed materials in production order.
|
||||
self.assertEqual(mrp_order.move_raw_ids.mapped('product_id').ids, consu_product_ids)
|
||||
|
||||
def test_update_bom(self):
|
||||
"""Update a BoM used in open production should raise an error"""
|
||||
mo = self.env['mrp.production'].create({
|
||||
'name': 'MO 1',
|
||||
'product_id': self.product_1.id,
|
||||
'product_uom_id': self.product_1.uom_id.id,
|
||||
'product_qty': 2,
|
||||
'bom_id': self.bom_1.id,
|
||||
})
|
||||
mo.button_plan()
|
||||
# update an authorized field
|
||||
self.bom_1.write({'code': 'Hello'})
|
||||
# update a not authorized field
|
||||
with self.assertRaises(exceptions.ValidationError):
|
||||
self.bom_1.write({'product_id': self.product_2.id})
|
||||
|
||||
@@ -124,7 +124,7 @@ class TestWorkOrderProcess(common.TransactionCase):
|
||||
self.env['stock.move'].search([('product_id', '=', product_bolt.id)])._do_unreserve()
|
||||
product_bolt.type = 'product'
|
||||
|
||||
bom = self.env['mrp.bom'].browse(self.ref("mrp.mrp_bom_desk"))
|
||||
bom = self.env['mrp.bom'].browse(self.ref("mrp.mrp_bom_desk")).copy()
|
||||
bom.routing_id = self.ref('mrp.mrp_routing_1')
|
||||
|
||||
bom.bom_line_ids.filtered(lambda p: p.product_id == product_table_sheet).operation_id = bom.routing_id.operation_ids[0]
|
||||
|
||||
Reference in New Issue
Block a user