From eeaff033b84080f95f157708356bb980acb4242f Mon Sep 17 00:00:00 2001 From: "Touati Djamel (otd)" Date: Mon, 28 Mar 2022 12:23:41 +0000 Subject: [PATCH] [FIX] mrp_landed_costs: validate a landed cost without the need for MRP access MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Steps to reproduce the bug: - Install mrp_landed_costs - Connect as Admin - Remove MRP access to Admin and Give him Stock manager access - Go to inventory settings > Enable “Landed costs” - Go to inventory > operation > Select any landed cost - Try to validate Problem: An access error is triggered because we check if there are finished moves in the associated pickings and manufacturing orders before validating the landed cost, but in the "_get_targeted_move_ids" function we access the "mrp_production_ids" field, which is reserved for mrp users. Solution: Creating and validating a landed cost is a feature for stock manager users. We can therefore have users who do not need access to MRP, but who need to validate and submit landed costs. So we can give access to all of them even if they are not MRP users. opw-2800367 closes odoo/odoo#87624 X-original-commit: 3b92178b27dcaa25024b9d79ae35c8768cd72fa6 Signed-off-by: William Henrotin (whe) Signed-off-by: Djamel Touati (otd) --- .../models/stock_landed_cost.py | 2 +- .../tests/test_stock_landed_costs_mrp.py | 41 +++++++++++++++++++ .../views/stock_landed_cost_views.xml | 2 +- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/addons/mrp_landed_costs/models/stock_landed_cost.py b/addons/mrp_landed_costs/models/stock_landed_cost.py index e6410269d78..cb6d9c10304 100644 --- a/addons/mrp_landed_costs/models/stock_landed_cost.py +++ b/addons/mrp_landed_costs/models/stock_landed_cost.py @@ -12,7 +12,7 @@ class StockLandedCost(models.Model): ], ondelete={'manufacturing': 'set default'}) mrp_production_ids = fields.Many2many( 'mrp.production', string='Manufacturing order', - copy=False, states={'done': [('readonly', True)]}, groups='mrp.group_mrp_user') + copy=False, states={'done': [('readonly', True)]}, groups='stock.group_stock_manager') @api.onchange('target_model') def _onchange_target_model(self): diff --git a/addons/mrp_landed_costs/tests/test_stock_landed_costs_mrp.py b/addons/mrp_landed_costs/tests/test_stock_landed_costs_mrp.py index a81a89cbf0c..62c3fcf0ef1 100644 --- a/addons/mrp_landed_costs/tests/test_stock_landed_costs_mrp.py +++ b/addons/mrp_landed_costs/tests/test_stock_landed_costs_mrp.py @@ -135,3 +135,44 @@ class TestStockLandedCostsMrp(ValuationReconciliationTestCommon): self.assertEqual(len(landed_cost.stock_valuation_layer_ids), 1) self.assertEqual(landed_cost.stock_valuation_layer_ids.product_id, self.product_refrigerator) self.assertEqual(landed_cost.stock_valuation_layer_ids.value, 5.0) + + def test_landed_cost_on_mrp_02(self): + """ + Test that a user who has manager access to stock can create and validate a landed cost linked + to a Manufacturing order without the need for MRP access + """ + # Create a user with only manager access to stock + stock_manager = self.env['res.users'].with_context({'no_reset_password': True}).create({ + 'name': "Stock Manager", + 'login': "test", + 'email': "test@test.com", + 'groups_id': [(6, 0, [self.env.ref('stock.group_stock_manager').id])] + }) + # Make some stock and reserve + self.env['stock.quant']._update_available_quantity(self.product_component1, self.warehouse_1.lot_stock_id, 10) + self.env['stock.quant']._update_available_quantity(self.product_component2, self.warehouse_1.lot_stock_id, 10) + + # Create and confirm a MO with a user who has access to MRP + man_order_form = Form(self.env['mrp.production'].with_user(self.allow_user)) + man_order_form.product_id = self.product_refrigerator + man_order_form.bom_id = self.bom_refri + man_order_form.product_qty = 1.0 + man_order = man_order_form.save() + man_order.action_confirm() + # produce product + man_order_form.qty_producing = 1 + man_order_form.save() + man_order.button_mark_done() + + # Create the landed cost with the stock_manager user + landed_cost = Form(self.env['stock.landed.cost'].with_user(stock_manager)).save() + landed_cost.target_model = 'manufacturing' + + # Check that the MO can be selected by the stock_manger user + self.assertTrue(man_order in self.env['mrp.production'].search([ + ('move_finished_ids.stock_valuation_layer_ids', '!=', False), ('company_id', '=', landed_cost.company_id.id)])) + landed_cost.mrp_production_ids = [(6, 0, [man_order.id])] + + # Check that he can validate the landed cost without an access error + landed_cost.with_user(stock_manager).button_validate() + self.assertEqual(landed_cost.state, 'done') diff --git a/addons/mrp_landed_costs/views/stock_landed_cost_views.xml b/addons/mrp_landed_costs/views/stock_landed_cost_views.xml index c70749af799..cd153337b01 100644 --- a/addons/mrp_landed_costs/views/stock_landed_cost_views.xml +++ b/addons/mrp_landed_costs/views/stock_landed_cost_views.xml @@ -7,7 +7,7 @@ 0 - mrp.group_mrp_user + stock.group_stock_manager