From 3f4e15bb4c2f542ff79fe018e89acad3f740a9a0 Mon Sep 17 00:00:00 2001 From: Walid Date: Wed, 6 Mar 2024 12:32:57 +0100 Subject: [PATCH] [FIX] sale_mrp: inherit MO analytic account from SO Steps to reproduce: - Enable MTO - Create a manufactured product (no analytic distribution on the bom) - Create an SO for that product and set an analytic account on it - Confirm the SO Bug: no analytic account on the created MO Fix: inherit MO analytic account from SO if none is set on the BOM opw-3708134 closes odoo/odoo#156535 X-original-commit: cd72327ff3fb3633e353b7f9443ca973c358fc01 Signed-off-by: William Henrotin (whe) Signed-off-by: Walid Hanniche (waha) --- addons/mrp_account/models/stock_rule.py | 7 ++++-- addons/sale_mrp/models/stock_move.py | 2 ++ addons/sale_mrp/tests/__init__.py | 1 + .../sale_mrp/tests/test_sale_mrp_account.py | 23 +++++++++++++++++++ 4 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 addons/sale_mrp/tests/test_sale_mrp_account.py diff --git a/addons/mrp_account/models/stock_rule.py b/addons/mrp_account/models/stock_rule.py index 5c1624eccdf..01a2806a67e 100644 --- a/addons/mrp_account/models/stock_rule.py +++ b/addons/mrp_account/models/stock_rule.py @@ -9,6 +9,9 @@ class StockRule(models.Model): def _prepare_mo_vals(self, product_id, product_qty, product_uom, location_id, name, origin, company_id, values, bom): res = super()._prepare_mo_vals(product_id, product_qty, product_uom, location_id, name, origin, company_id, values, bom) - if not bom.analytic_distribution and values.get('analytic_distribution'): - res['analytic_distribution'] = values.get('analytic_distribution') + if not bom.analytic_distribution: + if values.get('analytic_distribution'): + res['analytic_distribution'] = values.get('analytic_distribution') + elif values.get('analytic_account_id'): + res['analytic_distribution'] = {values.get('analytic_account_id').id: 100} return res diff --git a/addons/sale_mrp/models/stock_move.py b/addons/sale_mrp/models/stock_move.py index 4d31d27e3fd..51b89754859 100644 --- a/addons/sale_mrp/models/stock_move.py +++ b/addons/sale_mrp/models/stock_move.py @@ -10,6 +10,8 @@ class StockMove(models.Model): def _prepare_procurement_values(self): res = super()._prepare_procurement_values() res['analytic_account_id'] = self.sale_line_id.order_id.analytic_account_id + if self.sale_line_id.order_id.analytic_account_id: + res['analytic_distribution'] = {self.sale_line_id.order_id.analytic_account_id.id: 100} return res diff --git a/addons/sale_mrp/tests/__init__.py b/addons/sale_mrp/tests/__init__.py index 09a9e105b3d..026ac4cbb32 100644 --- a/addons/sale_mrp/tests/__init__.py +++ b/addons/sale_mrp/tests/__init__.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. +from . import test_sale_mrp_account from . import test_sale_mrp_flow from . import test_sale_mrp_kit_bom from . import test_sale_mrp_lead_time diff --git a/addons/sale_mrp/tests/test_sale_mrp_account.py b/addons/sale_mrp/tests/test_sale_mrp_account.py new file mode 100644 index 00000000000..d201da942eb --- /dev/null +++ b/addons/sale_mrp/tests/test_sale_mrp_account.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from . import test_multistep_manufacturing +from odoo.tests import common + + +@common.tagged('post_install', '-at_install') +class TestSaleMrpAccount(test_multistep_manufacturing.TestMultistepManufacturing): + def test_mo_analytic_distribution(self): + """ ensure analytic account/distribution is inherited from the SO + when none is set on the bom + """ + analytic_plan = self.env['account.analytic.plan'].create({ + 'name': 'Plan', + }) + self.sale_order.analytic_account_id = self.env['account.analytic.account'].create({ + 'name': 'test_analytic_account', + 'plan_id': analytic_plan.id, + }) + self.sale_order.action_confirm() + self.assertTrue(self.sale_order.mrp_production_ids.analytic_distribution) + self.assertTrue(self.sale_order.mrp_production_ids.analytic_account_ids)