Skip to content

Commit eb4dfbf

Browse files
committed
[FIX] mrp: update BoM on scrap when product is changed
Problem: When a user changes the product on a scrap order, the bom_id field does not get updated. If they update the product from a product that has BoM to a product that doesn’t have one, then the bom_id field is hidden and remains set. This will cause the scrap quantity to be set to 0 when they validate the scrap. However, the product move actually happens for the correct quantity causing an inconsistency. Purpose: This will either set the bom_id field to False if the new product doesn’t have a valid BoM, or it will update it to the first available BoM. Steps to Reproduce on Runbot: 1. Create a scrap order for a product that has a kit type BoM and set the kit field. 2. Change the product to a product without a kit type BoM. 3. Validate the scrap order. 4. Observe the quantity field is set to 0, but there are product moves for the correct quantity. opw-5122880 closes odoo#234050 X-original-commit: 07a6b28 Signed-off-by: Steve Van Essche <[email protected]>
1 parent 3519695 commit eb4dfbf

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

addons/mrp/models/stock_scrap.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ def _onchange_serial_number(self):
6464
else:
6565
return super()._onchange_serial_number()
6666

67+
@api.onchange('product_id')
68+
def _onchange_product_id(self):
69+
if self.product_is_kit:
70+
self.bom_id = self.env['mrp.bom']._bom_find(self.product_id, company_id=self.company_id.id, bom_type='phantom')[self.product_id]
71+
else:
72+
self.bom_id = False
73+
6774
@api.depends('move_ids', 'move_ids.move_line_ids.quantity', 'product_id')
6875
def _compute_scrap_qty(self):
6976
self.scrap_qty = 1

addons/mrp/tests/test_stock.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,3 +736,37 @@ def test_search_kit_on_quantity(self):
736736
self.assertNotIn(self.kit_1, products) # 12
737737
self.assertIn(self.kit_2, products) # 6
738738
self.assertNotIn(self.kit_3, products) # 3
739+
740+
def test_scrap_change_product(self):
741+
""" Ensure a scrap order automatically updates the BoM when its product is changed,
742+
selecting the product's first BoM if it's a kit or set the field empty otherwise."""
743+
bom_a = self.bom_1
744+
bom_a.type = 'phantom'
745+
product_a = bom_a.product_id
746+
747+
bom_b = self.bom_3
748+
bom_b.type = 'phantom'
749+
product_b = bom_b.product_id
750+
751+
product_c = self.env['product.product'].create({'name': 'product_c', 'is_storable': True})
752+
753+
form = Form(self.env['stock.scrap'])
754+
form.product_id = product_a
755+
form.bom_id = bom_a
756+
form.scrap_qty = 1
757+
scrap = form.save()
758+
759+
# assert the scrap's bom_id is set to bom_a
760+
self.assertEqual(scrap.bom_id, bom_a)
761+
762+
form.product_id = product_b
763+
scrap = form.save()
764+
765+
# assert the scrap's bom_id is set to bom_b after updating the product
766+
self.assertEqual(scrap.bom_id, bom_b)
767+
768+
form.product_id = product_c
769+
scrap = form.save()
770+
771+
# assert the scrap's bom_id is updated to False after updating the product
772+
self.assertFalse(scrap.bom_id)

0 commit comments

Comments
 (0)