From ab7122deb2c82c11b3744fcd1a8a4202b19a6ac2 Mon Sep 17 00:00:00 2001 From: Michael Tietz <78783475+mt-software-de@users.noreply.github.com> Date: Tue, 30 May 2023 09:26:32 +0200 Subject: [PATCH] [FIX] stock_picking_volume: stock.picking _compute_volume Calculate the volume only from not canceled moves --- stock_picking_volume/models/stock_picking.py | 14 ++-- .../tests/test_stock_picking_volume.py | 69 +++++++++++++++---- 2 files changed, 67 insertions(+), 16 deletions(-) diff --git a/stock_picking_volume/models/stock_picking.py b/stock_picking_volume/models/stock_picking.py index 731957fd5..b426d7977 100644 --- a/stock_picking_volume/models/stock_picking.py +++ b/stock_picking_volume/models/stock_picking.py @@ -16,12 +16,18 @@ class StockPicking(models.Model): string="Volume unit of measure label", compute="_compute_volume_uom_name" ) - @api.depends("move_lines", "move_lines.volume") + @api.depends("move_lines", "move_lines.volume", "move_lines.state") def _compute_volume(self): for picking in self: - new_volume = sum(picking.move_lines.mapped("volume")) - if picking.volume != new_volume: - picking.volume = new_volume + moves = picking.move_lines + exclude_cancel = any(m.state != "cancel" for m in moves) + volume = 0 + for move in moves: + if move.state == "cancel" and exclude_cancel: + continue + volume += move.volume + if picking.volume != volume: + picking.volume = volume def _compute_volume_uom_name(self): self.volume_uom_name = self.env[ diff --git a/stock_picking_volume/tests/test_stock_picking_volume.py b/stock_picking_volume/tests/test_stock_picking_volume.py index b18e094b8..4ffe1fe54 100644 --- a/stock_picking_volume/tests/test_stock_picking_volume.py +++ b/stock_picking_volume/tests/test_stock_picking_volume.py @@ -20,18 +20,7 @@ class TestStockPickingVolume(SavepointCase): ) cls.loc_stock = cls.wh.lot_stock_id cls.loc_customer = cls.env.ref("stock.stock_location_customers") - cls.product = cls.env["product.product"].create( - { - "name": "Unittest P1", - "product_length": 10.0, - "product_width": 5.0, - "product_height": 3.0, - "uom_id": cls.env.ref("uom.product_uom_unit").id, - "dimensional_uom_id": cls.env.ref("uom.product_uom_meter").id, - "type": "product", - } - ) - cls.product.onchange_calculate_volume() + cls.product = cls._create_product("Unittest P1", 10.0, 5.0, 3.0) cls.picking_type_out = cls.env.ref("stock.picking_type_out") cls.picking = cls.env["stock.picking"].create( { @@ -56,6 +45,22 @@ class TestStockPickingVolume(SavepointCase): } ) + @classmethod + def _create_product(cls, name, length, width, height): + product = cls.env["product.product"].create( + { + "name": name, + "product_length": length, + "product_width": width, + "product_height": height, + "uom_id": cls.env.ref("uom.product_uom_unit").id, + "dimensional_uom_id": cls.env.ref("uom.product_uom_meter").id, + "type": "product", + } + ) + product.onchange_calculate_volume() + return product + def _set_product_qty(self, product, qty): self.env["stock.quant"]._update_available_quantity(product, self.loc_stock, qty) @@ -144,3 +149,43 @@ class TestStockPickingVolume(SavepointCase): self.picking.button_validate() self.picking.action_cancel() self.assertEqual(self.picking.volume, 750) + + def test_picking_with_canceled_move(self): + """ + Data: + one picking with two move lines with 5 units of product + Test Case: + set 5 unit of product as available + get the volume of the picking + Expected result: + volume is 5 * 10 * 5 * 3 = 750 + The volume is computed from the expected quantity + """ + product2 = self._create_product("Product2", 10.0, 5.0, 3.0) + self._set_product_qty(self.product, 5) + self._set_product_qty(product2, 5) + self.picking.write( + { + "move_lines": [ + ( + 0, + 0, + { + "name": product2.name, + "product_id": product2.id, + "product_uom": product2.uom_id.id, + "product_uom_qty": 5.0, + "location_id": self.loc_stock.id, + "location_dest_id": self.loc_customer.id, + }, + ) + ] + } + ) + self.picking.action_confirm() + self.picking.action_assign() + self.picking.invalidate_cache() + self.assertEqual(self.picking.volume, 750 * 2) + self.picking.move_lines[1]._action_cancel() + self.picking.invalidate_cache() + self.assertEqual(self.picking.volume, 750)