From 88e2b33052a9df95b2d7a1f85076456087406af6 Mon Sep 17 00:00:00 2001 From: Atte Isopuro Date: Thu, 5 Sep 2024 15:32:15 +0300 Subject: [PATCH] [FEAT] mrp_stock_analytic: add analytic to new raw lines Also add analytic distribution when manually adding raw lines to an MO with an analytic distribution set. --- mrp_stock_analytic/models/__init__.py | 1 + mrp_stock_analytic/models/stock_move.py | 24 +++++++++++++ .../tests/test_mrp_stock_analytic.py | 35 +++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 mrp_stock_analytic/models/stock_move.py diff --git a/mrp_stock_analytic/models/__init__.py b/mrp_stock_analytic/models/__init__.py index a9e5f13e42..0dbd3464c3 100644 --- a/mrp_stock_analytic/models/__init__.py +++ b/mrp_stock_analytic/models/__init__.py @@ -1 +1,2 @@ from . import mrp_production +from . import stock_move diff --git a/mrp_stock_analytic/models/stock_move.py b/mrp_stock_analytic/models/stock_move.py new file mode 100644 index 0000000000..b979688efd --- /dev/null +++ b/mrp_stock_analytic/models/stock_move.py @@ -0,0 +1,24 @@ +from odoo import api, models + + +class StockMove(models.Model): + _inherit = "stock.move" + + @api.model_create_multi + def create(self, vals_list): + """ + Extend to copy the analytic distribution of the manufacturing order + if a move is added as a raw material move to it. + """ + for vals in vals_list: + if "analytic_distribution" in vals: + continue + raw_production = ( + self.env["mrp.production"] + .browse(vals.get("raw_material_production_id")) + .exists() + ) + if not raw_production.analytic_distribution: + continue + vals["analytic_distribution"] = raw_production.analytic_distribution + return super().create(vals_list) diff --git a/mrp_stock_analytic/tests/test_mrp_stock_analytic.py b/mrp_stock_analytic/tests/test_mrp_stock_analytic.py index 362441371a..6fb24b6ab5 100644 --- a/mrp_stock_analytic/tests/test_mrp_stock_analytic.py +++ b/mrp_stock_analytic/tests/test_mrp_stock_analytic.py @@ -146,3 +146,38 @@ def test_analytic_propagation_backorder(self): backorder.move_raw_ids.analytic_distribution, backorder.analytic_distribution, ) + + def _check_analytic_when_adding_new_line(self): + self.production.analytic_distribution = self.analytic_distribution + self.product_C = self.env["product.product"].create( + { + "name": "Product C", + "type": "product", + "categ_id": self.product_categ.id, + "standard_price": 50.0, + } + ) + self.assertGreater(len(self.production.move_raw_ids), 0) + edit_production = Form(self.production) + with edit_production.move_raw_ids.new() as new_raw: + new_raw.product_id = self.product_C + production = edit_production.save() + for raw_line in production.move_raw_ids: + with self.subTest( + raw_move=raw_line.display_name, + raw_product=raw_line.product_id.display_name, + ): + self.assertEqual( + raw_line.analytic_distribution, + self.analytic_distribution, + msg="When a new raw line is added to a draft production, " + "it should get the analytic distribution of the " + "production", + ) + + def test_analytic_added_to_new_lines_on_draft(self): + self._check_analytic_when_adding_new_line() + + def test_analytic_added_to_new_lines_on_confirmed(self): + self.production.action_confirm() + self._check_analytic_when_adding_new_line()