diff --git a/repair_picking_after_done/__init__.py b/repair_picking_after_done/__init__.py index aee8895e..08eb1400 100644 --- a/repair_picking_after_done/__init__.py +++ b/repair_picking_after_done/__init__.py @@ -1,2 +1,3 @@ from . import models from . import wizards +from .hooks import post_load_hook diff --git a/repair_picking_after_done/__manifest__.py b/repair_picking_after_done/__manifest__.py index f65dc3d5..a0edf33d 100644 --- a/repair_picking_after_done/__manifest__.py +++ b/repair_picking_after_done/__manifest__.py @@ -18,4 +18,5 @@ "development_status": "Alpha", "license": "AGPL-3", "application": False, + "post_load": "post_load_hook", } diff --git a/repair_picking_after_done/hooks.py b/repair_picking_after_done/hooks.py new file mode 100644 index 00000000..220aa3c2 --- /dev/null +++ b/repair_picking_after_done/hooks.py @@ -0,0 +1,25 @@ +# Copyright 2024 Patryk Pyczko (APSL-Nagarro) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo.addons.repair.models.stock_move import StockMove + + +def post_load_hook(): + """ + This hook modifies the stock move splitting logic to: + - Allow splitting stock moves related to repair + orders that are marked as "done", which is prevented + by the core Odoo logic. + - This change enables the creation of backorders for + these split stock moves when the associated repair is completed. + """ + + def _split_for_repair_custom(self, qty, restrict_partner_id=False): + if self.repair_id and self.repair_id.state != "done": + return [] + + return super(StockMove, self)._split(qty, restrict_partner_id) + + if not hasattr(StockMove, "_split_original"): + StockMove._split_original = StockMove._split + StockMove._split = _split_for_repair_custom diff --git a/repair_picking_after_done/tests/__init__.py b/repair_picking_after_done/tests/__init__.py index 7d52509a..d7527875 100644 --- a/repair_picking_after_done/tests/__init__.py +++ b/repair_picking_after_done/tests/__init__.py @@ -2,3 +2,4 @@ # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) from . import test_repair_transfers +from . import test_stock_move_split diff --git a/repair_picking_after_done/tests/test_stock_move_split.py b/repair_picking_after_done/tests/test_stock_move_split.py new file mode 100644 index 00000000..e3b60ffb --- /dev/null +++ b/repair_picking_after_done/tests/test_stock_move_split.py @@ -0,0 +1,58 @@ +from odoo.tests.common import TransactionCase + + +class TestStockMoveSplit(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + # Create a product + cls.product = cls.env["product.product"].create( + { + "name": "Test Product", + "type": "product", + } + ) + + # Create a stock location + cls.location = cls.env["stock.location"].create( + { + "name": "Test Location", + "usage": "internal", + } + ) + + # Create a repair order in 'draft' state + cls.repair = cls.env["repair.order"].create( + { + "name": "Repair Order 1", + "product_id": cls.product.id, + "state": "draft", + } + ) + + # Create a stock move linked to the repair order + cls.stock_move = cls.env["stock.move"].create( + { + "name": "Stock Move for Repair Order 1", + "product_id": cls.product.id, + "product_uom_qty": 10.0, + "product_uom": cls.product.uom_id.id, + "repair_id": cls.repair.id, + "state": "confirmed", + "location_id": cls.location.id, + "location_dest_id": cls.location.id, + } + ) + + def test_split_move_with_incomplete_repair(self): + """Ensure a stock move linked to an incomplete repair cannot be split""" + result = self.stock_move._split(5.0) + self.assertEqual( + result, [], "Move should not split as the repair is not 'done'." + ) + + def test_split_move_with_completed_repair(self): + """Ensure a stock move linked to a completed repair can be split""" + self.repair.write({"state": "done"}) + result = self.stock_move._split(5.0) + self.assertTrue(result, "Move should split as the repair is 'done'.")