Skip to content
Snippets Groups Projects
Commit 475e6b3c authored by Arnold Moyaux's avatar Arnold Moyaux Committed by William Henrotin
Browse files

[FIX] sale_mrp: Link to MO on SO


commit 96b56ea3 fix the link between SO <-> MO in manufacture 3 steps
but break it on other configuration

It happens because without `store after manufacturing` the procurement
group is not copied and used on the MO. And the stat button search on
procurement group with a MO and a SO on it. Before it was searching
on `stock_move.created_production_id` and thus was able to find the MO.

This commit do a mix of the 2 solutions. It search for procurement group
linked to SO and MO + created_production_id to be sure to find all the
MO related to a SO

opw-2645042

closes odoo/odoo#79015

Signed-off-by: default avatarWilliam Henrotin (whe) <whe@odoo.com>
parent 755c074a
No related branches found
No related tags found
No related merge requests found
...@@ -14,16 +14,21 @@ class SaleOrder(models.Model): ...@@ -14,16 +14,21 @@ class SaleOrder(models.Model):
@api.depends('procurement_group_id.stock_move_ids.created_production_id.procurement_group_id.mrp_production_ids') @api.depends('procurement_group_id.stock_move_ids.created_production_id.procurement_group_id.mrp_production_ids')
def _compute_mrp_production_count(self): def _compute_mrp_production_count(self):
data = self.env['procurement.group'].read_group([('sale_id', 'in', self.ids), ('mrp_production_ids', '!=', False)], ['id'], ['sale_id']) data = self.env['procurement.group'].read_group([('sale_id', 'in', self.ids)], ['ids:array_agg(id)'], ['sale_id'])
mrp_count = dict() mrp_count = dict()
for item in data: for item in data:
mrp_count[item['sale_id'][0]] = item['sale_id_count'] procurement_groups = self.env['procurement.group'].browse(item['ids'])
mrp_count[item['sale_id'][0]] = len(
set(procurement_groups.stock_move_ids.created_production_id.ids) |
set(procurement_groups.mrp_production_ids.ids))
for sale in self: for sale in self:
sale.mrp_production_count = mrp_count.get(sale.id) sale.mrp_production_count = mrp_count.get(sale.id)
def action_view_mrp_production(self): def action_view_mrp_production(self):
self.ensure_one() self.ensure_one()
mrp_production_ids = self.env['mrp.production'].search([('procurement_group_id.sale_id', '=', self.id)]).ids procurement_groups = self.env['procurement.group'].search([('sale_id', 'in', self.ids)])
mrp_production_ids = set(procurement_groups.stock_move_ids.created_production_id.ids) |\
set(procurement_groups.mrp_production_ids.ids)
action = { action = {
'res_model': 'mrp.production', 'res_model': 'mrp.production',
'type': 'ir.actions.act_window', 'type': 'ir.actions.act_window',
...@@ -31,12 +36,12 @@ class SaleOrder(models.Model): ...@@ -31,12 +36,12 @@ class SaleOrder(models.Model):
if len(mrp_production_ids) == 1: if len(mrp_production_ids) == 1:
action.update({ action.update({
'view_mode': 'form', 'view_mode': 'form',
'res_id': mrp_production_ids[0], 'res_id': mrp_production_ids.pop(),
}) })
else: else:
action.update({ action.update({
'name': _("Manufacturing Orders Generated by %s", self.name), 'name': _("Manufacturing Orders Generated by %s", self.name),
'domain': [('id', 'in', mrp_production_ids)], 'domain': [('id', 'in', list(mrp_production_ids))],
'view_mode': 'tree,form', 'view_mode': 'tree,form',
}) })
return action return action
......
...@@ -82,6 +82,11 @@ class TestMultistepManufacturing(TestMrpCommon): ...@@ -82,6 +82,11 @@ class TestMultistepManufacturing(TestMrpCommon):
self.sale_order.action_confirm() self.sale_order.action_confirm()
# Get manufactured procurement # Get manufactured procurement
mo_procurement = self.MrpProduction.search([('origin', '=', self.sale_order.name)]) mo_procurement = self.MrpProduction.search([('origin', '=', self.sale_order.name)])
mo = self.env['mrp.production'].search([
('origin', '=', self.sale_order.name),
('product_id', '=', self.product_manu.id),
])
self.assertEqual(self.sale_order.action_view_mrp_production()['res_id'], mo.id)
self.assertEqual(mo_procurement.location_src_id.id, self.warehouse.pbm_loc_id.id, "Source loction does not match.") self.assertEqual(mo_procurement.location_src_id.id, self.warehouse.pbm_loc_id.id, "Source loction does not match.")
self.assertEqual(mo_procurement.location_dest_id.id, self.warehouse.lot_stock_id.id, "Destination location does not match.") self.assertEqual(mo_procurement.location_dest_id.id, self.warehouse.lot_stock_id.id, "Destination location does not match.")
......
...@@ -295,6 +295,9 @@ class TestSaleMrpFlow(ValuationReconciliationTestCommon): ...@@ -295,6 +295,9 @@ class TestSaleMrpFlow(ValuationReconciliationTestCommon):
order = order_form.save() order = order_form.save()
order.action_confirm() order.action_confirm()
# Verify buttons are working as expected
self.assertEqual(order.mrp_production_count, 1, "User should see the closest manufacture order in the smart button")
# =============================================================================== # ===============================================================================
# Sales order of 10 Dozen product A should create production order # Sales order of 10 Dozen product A should create production order
# like .. # like ..
......
...@@ -69,6 +69,9 @@ class TestSaleMrpProcurement(TransactionCase): ...@@ -69,6 +69,9 @@ class TestSaleMrpProcurement(TransactionCase):
mo = self.env['mrp.production'].search([('origin', 'like', sale_order_so0.name)], limit=1) mo = self.env['mrp.production'].search([('origin', 'like', sale_order_so0.name)], limit=1)
self.assertTrue(mo, 'Manufacturing order has not been generated') self.assertTrue(mo, 'Manufacturing order has not been generated')
# Check the mo is displayed on the so
self.assertEqual(mo.id, sale_order_so0.action_view_mrp_production()['res_id'])
def test_sale_mrp_pickings(self): def test_sale_mrp_pickings(self):
""" Test sale of multiple mrp products in MTO """ Test sale of multiple mrp products in MTO
to avoid generating multiple deliveries to avoid generating multiple deliveries
...@@ -162,6 +165,9 @@ class TestSaleMrpProcurement(TransactionCase): ...@@ -162,6 +165,9 @@ class TestSaleMrpProcurement(TransactionCase):
sale_order_so0.action_confirm() sale_order_so0.action_confirm()
# Verify buttons are working as expected
self.assertEqual(sale_order_so0.mrp_production_count, 2, "User should see the correct number of manufacture orders in smart button")
pickings = sale_order_so0.picking_ids pickings = sale_order_so0.picking_ids
# One delivery... # One delivery...
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment