diff --git a/stock_inventory/__manifest__.py b/stock_inventory/__manifest__.py index 16fa4a782e90..42e10fb24356 100644 --- a/stock_inventory/__manifest__.py +++ b/stock_inventory/__manifest__.py @@ -14,6 +14,7 @@ "views/stock_inventory.xml", "views/stock_quant.xml", "views/stock_move_line.xml", + "views/res_config_settings_view.xml", ], "installable": True, "application": False, diff --git a/stock_inventory/models/__init__.py b/stock_inventory/models/__init__.py index 09732e334e94..b7114e34a3db 100644 --- a/stock_inventory/models/__init__.py +++ b/stock_inventory/models/__init__.py @@ -1,3 +1,5 @@ from . import stock_inventory from . import stock_quant from . import stock_move_line +from . import res_company +from . import res_config_settings diff --git a/stock_inventory/models/res_company.py b/stock_inventory/models/res_company.py new file mode 100644 index 000000000000..1181c7cc9990 --- /dev/null +++ b/stock_inventory/models/res_company.py @@ -0,0 +1,15 @@ +# Copyright 2024 ForgeFlow S.L. (http://www.forgeflow.com) +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). + + +from odoo import fields, models + + +class ResCompany(models.Model): + _inherit = "res.company" + + stock_inventory_auto_complete = fields.Boolean( + help="If enabled, when all the quants prepared for the adjustment " + "are done, the adjustment is automatically set to done.", + default=False, + ) diff --git a/stock_inventory/models/res_config_settings.py b/stock_inventory/models/res_config_settings.py new file mode 100644 index 000000000000..3817c268b445 --- /dev/null +++ b/stock_inventory/models/res_config_settings.py @@ -0,0 +1,12 @@ +# Copyright 2024 ForgeFlow S.L. (http://www.forgeflow.com) +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). + +from odoo import fields, models + + +class ResConfigSettings(models.TransientModel): + _inherit = "res.config.settings" + + stock_inventory_auto_complete = fields.Boolean( + related="company_id.stock_inventory_auto_complete", readonly=False + ) diff --git a/stock_inventory/models/stock_inventory.py b/stock_inventory/models/stock_inventory.py index fec6e898bbde..f237057dbadf 100644 --- a/stock_inventory/models/stock_inventory.py +++ b/stock_inventory/models/stock_inventory.py @@ -74,11 +74,7 @@ class InventoryAdjustmentsGroup(models.Model): @api.depends("stock_quant_ids") def _compute_count_stock_quants(self): self.count_stock_quants = len(self.stock_quant_ids) - count_todo = len( - self.stock_quant_ids.search( - [("id", "in", self.stock_quant_ids.ids), ("to_do", "=", "True")] - ) - ) + count_todo = len(self.stock_quant_ids.filtered(lambda sq: sq.to_do)) self.count_stock_quants_string = "{} / {}".format( count_todo, self.count_stock_quants ) @@ -188,6 +184,12 @@ def action_state_to_done(self): self.stock_quant_ids.update({"to_do": True}) return + def action_auto_state_to_done(self): + self.ensure_one() + if not any(self.stock_quant_ids.filtered(lambda sq: sq.to_do)): + self.action_state_to_done() + return + def action_state_to_draft(self): self.state = "draft" self.stock_quant_ids.update({"to_do": True}) diff --git a/stock_inventory/models/stock_quant.py b/stock_inventory/models/stock_quant.py index bb4359b86c12..bbd544d10e9f 100644 --- a/stock_inventory/models/stock_quant.py +++ b/stock_inventory/models/stock_quant.py @@ -38,6 +38,8 @@ def _apply_inventory(self): adjustment.stock_move_ids |= move move.inventory_adjustment_id = adjustment rec.to_do = False + if self.env.company.stock_inventory_auto_complete: + adjustment.action_auto_state_to_done() return res def _get_inventory_fields_write(self): diff --git a/stock_inventory/static/description/index.html b/stock_inventory/static/description/index.html index 3f92e3c818ce..173226cfae56 100644 --- a/stock_inventory/static/description/index.html +++ b/stock_inventory/static/description/index.html @@ -1,4 +1,3 @@ - diff --git a/stock_inventory/tests/test_stock_inventory.py b/stock_inventory/tests/test_stock_inventory.py index ba8ed55cadae..fcf7680fdb52 100644 --- a/stock_inventory/tests/test_stock_inventory.py +++ b/stock_inventory/tests/test_stock_inventory.py @@ -8,6 +8,7 @@ class TestStockInventory(TransactionCase): def setUp(self): super(TestStockInventory, self).setUp() + self.env.company.stock_inventory_auto_complete = False self.quant_model = self.env["stock.quant"] self.move_model = self.env["stock.move.line"] self.inventory_model = self.env["stock.inventory"] @@ -359,3 +360,52 @@ def test_06_exclude_sub_locations(self): self.assertEqual(inventory1.stock_move_ids.lot_id.id, self.lot_1.id) self.assertEqual(inventory1.stock_move_ids.location_id.id, self.location1.id) inventory1.action_state_to_done() + + def test_07_stock_inventory_auto_complete(self): + self.env.company.stock_inventory_auto_complete = True + with self.assertRaises(ValidationError), self.cr.savepoint(): + inventory1 = self.inventory_model.create( + { + "name": "Inventory_Test_5", + "product_selection": "one", + "location_ids": [self.location1.id], + "product_ids": [self.product.id, self.product2.id], + } + ) + inventory1 = self.inventory_model.create( + { + "name": "Inventory_Test_5", + "product_selection": "one", + "location_ids": [self.location1.id], + "product_ids": [self.product.id], + } + ) + inventory1.action_state_to_in_progress() + inventory1.product_ids = [self.product.id] + self.assertEqual( + inventory1.stock_quant_ids.ids, [self.quant1.id, self.quant3.id] + ) + inventory1.action_state_to_draft() + self.assertEqual(inventory1.stock_quant_ids.ids, []) + inventory1.action_state_to_in_progress() + self.assertEqual(inventory1.state, "in_progress") + self.assertEqual(inventory1.count_stock_moves, 0) + self.assertEqual(inventory1.count_stock_quants, 2) + self.assertEqual(inventory1.count_stock_quants_string, "2 / 2") + inventory1.action_view_inventory_adjustment() + self.quant3.inventory_quantity = 74 + self.quant3.action_apply_inventory() + inventory1._compute_count_stock_quants() + inventory1.action_view_stock_moves() + self.assertEqual(inventory1.count_stock_moves, 1) + self.assertEqual(inventory1.count_stock_quants, 2) + self.assertEqual(inventory1.count_stock_quants_string, "1 / 2") + self.assertEqual(inventory1.stock_move_ids.qty_done, 26) + self.assertEqual(inventory1.stock_move_ids.product_id.id, self.product.id) + self.assertEqual(inventory1.stock_move_ids.lot_id.id, self.lot_3.id) + self.assertEqual(inventory1.stock_move_ids.location_id.id, self.location3.id) + self.quant1.inventory_quantity = 65 + self.quant1.action_apply_inventory() + self.assertEqual(inventory1.count_stock_moves, 2) + self.assertEqual(inventory1.count_stock_quants, 2) + self.assertEqual(inventory1.state, "done") diff --git a/stock_inventory/views/res_config_settings_view.xml b/stock_inventory/views/res_config_settings_view.xml new file mode 100644 index 000000000000..3ff8ec98778c --- /dev/null +++ b/stock_inventory/views/res_config_settings_view.xml @@ -0,0 +1,28 @@ + + + + + res_config_settings_view_form - stock_inventory + res.config.settings + + + +

Stock Inventory

+
+
+
+ +
+
+
+
+
+
+
+
+
diff --git a/stock_inventory/views/stock_inventory.xml b/stock_inventory/views/stock_inventory.xml index df592f25fae1..ae11b660aa65 100644 --- a/stock_inventory/views/stock_inventory.xml +++ b/stock_inventory/views/stock_inventory.xml @@ -155,5 +155,7 @@ sequence="30" action="action_view_inventory_group_form" /> - + + +