From 5202b61d73c5f73d2c091ad87943e9010cb8a4fc Mon Sep 17 00:00:00 2001 From: Denis Roussel Date: Tue, 15 Oct 2024 13:20:11 +0200 Subject: [PATCH 1/2] [FIX] stock_picking_group_by_max_weight: Don't recompute all pickings if changing option As we don't want to recompute all pickings max weight, don't use the picking type option in the depends. Moreover, don't do those writes for cancel and done pickings. --- .../models/stock_picking.py | 6 ++- .../tests/test_group_maxweight.py | 47 +++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/stock_picking_group_by_max_weight/models/stock_picking.py b/stock_picking_group_by_max_weight/models/stock_picking.py index 75116bc9a1e5..add41282f755 100644 --- a/stock_picking_group_by_max_weight/models/stock_picking.py +++ b/stock_picking_group_by_max_weight/models/stock_picking.py @@ -23,10 +23,12 @@ class StockPicking(models.Model): def _assignation_max_weight_precision(self): return self._fields["assignation_max_weight"].get_digits(self.env)[1] - @api.depends("picking_type_id.group_pickings_maxweight", "weight", "state") + @api.depends("picking_type_id", "weight", "state") def _compute_assignation_max_weight(self): precision_digits = self._assignation_max_weight_precision - for picking in self: + for picking in self.filtered( + lambda picking: picking.state not in ("draft", "done", "cancel") + ): max_weight = ( picking.picking_type_id.group_pickings_maxweight - picking.weight ) diff --git a/stock_picking_group_by_max_weight/tests/test_group_maxweight.py b/stock_picking_group_by_max_weight/tests/test_group_maxweight.py index 90a57f02f9c3..0e98bd2dcd83 100644 --- a/stock_picking_group_by_max_weight/tests/test_group_maxweight.py +++ b/stock_picking_group_by_max_weight/tests/test_group_maxweight.py @@ -58,6 +58,53 @@ def test_group_max_weight(self): self.assertEqual(2, len(sale.picking_ids)) + def test_group_max_weight_change_parameter(self): + """ + Create a Sale order with first product + Confirm the Sale Order + Add a new product + New move should be assigned to a new picking + + Create inventory quantity + Assign the picking + """ + self.product.weight = 6.0 + self.product_2.weight = 3.0 + self.product_3.weight = 9.0 + sale = self._get_new_sale_order(amount=1.0) + sale.action_confirm() + self.assertEqual(1, len(sale.picking_ids)) + picking_1 = sale.picking_ids + self.assertEqual(2.0, sale.picking_ids.assignation_max_weight) + with Form(sale) as sale_form: + self._set_line(sale_form, self.product_2, 1.0) + self.assertEqual(2, len(sale.picking_ids)) + + # Change the strategy, check if the number of pickings still == 2 + self.picking_type_out.group_pickings_maxweight = 0 + with Form(sale) as sale_form: + self._set_line(sale_form, self.product_3, 1.0) + + self.assertEqual(2, len(sale.picking_ids)) + + self.env["stock.quant"].with_context(inventory_mode=True).create( + { + "product_id": self.product.id, + "inventory_quantity": 50.0, + "location_id": self.env.ref("stock.stock_location_stock").id, + } + )._apply_inventory() + + picking_1.action_assign() + self.assertEqual("assigned", picking_1.state) + for line in picking_1.move_line_ids: + line.qty_done = line.reserved_qty + picking_1._action_done() + + self.picking_type_out.group_pickings_maxweight = 2.0 + + self.assertEqual(2.0, picking_1.assignation_max_weight) + def test_group_max_weight_several_quantities(self): """ Create a Sale order with first product From 2d4afe229ca3ed40fdcdfa88f2795455f9d07ff8 Mon Sep 17 00:00:00 2001 From: Denis Roussel Date: Tue, 15 Oct 2024 13:20:56 +0200 Subject: [PATCH 2/2] [IMP] stock_picking_group_by_max_weight: Improve tests and coverage --- .../tests/test_group_maxweight.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/stock_picking_group_by_max_weight/tests/test_group_maxweight.py b/stock_picking_group_by_max_weight/tests/test_group_maxweight.py index 0e98bd2dcd83..5aa22ccbbfea 100644 --- a/stock_picking_group_by_max_weight/tests/test_group_maxweight.py +++ b/stock_picking_group_by_max_weight/tests/test_group_maxweight.py @@ -1,14 +1,14 @@ # Copyright 2023 ACSONE SA/NV # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -from odoo.tests.common import Form, TransactionCase +from odoo.tests.common import Form +from odoo.addons.base.tests.common import BaseCommon -class TestGroupMaxWeight(TransactionCase): + +class TestGroupMaxWeight(BaseCommon): @classmethod def setUpClass(cls): super().setUpClass() - - cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True)) cls.partner = cls.env["res.partner"].create({"name": "Test Partner"}) cls.product = cls.env.ref("product.product_delivery_01") cls.product_2 = cls.env.ref("product.product_delivery_02") @@ -33,6 +33,9 @@ def _get_new_sale_order(self, amount=10.0, partner=None): sale = sale_form.save() return sale + def test_init(self): + self.env["stock.picking"].init() + def test_group_max_weight(self): """ Create a Sale order with first product