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)