Skip to content

Commit 404a2a2

Browse files
mt-software-desebalix
authored andcommitted
[IMP] stock_picking_volume: Move _get_volume_for_qty to product.product
1 parent a450f7c commit 404a2a2

File tree

7 files changed

+48
-30
lines changed

7 files changed

+48
-30
lines changed
+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
from . import product_product
12
from . import stock_move
23
from . import stock_picking
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2023 Michael Tietz (MT Software) <mtietz@mt-software.de>
2+
# Copyright 2023 ACSONE SA/NV
3+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
4+
from odoo import models
5+
6+
7+
class ProductProduct(models.Model):
8+
_inherit = "product.product"
9+
10+
def _get_volume_for_qty(self, qty, from_uom=None):
11+
"""Return the volume for the given qty.
12+
13+
This method is meant to be inherited to change the volume
14+
computation for a specific product.
15+
16+
qty: float quantity to compute the volume for.
17+
from_uom: uom of given qty
18+
19+
An override of this method could take into account the packaging
20+
of the product to compute the volume. (using the volume information
21+
on the packaging provided by the module stock_quant_package_dimension
22+
and the method product_qty_by_packaging on the product provided by the
23+
module stock_packaging_calculator)
24+
"""
25+
self.ensure_one()
26+
qty = from_uom and from_uom._compute_quantity(qty, self.uom_id) or qty
27+
return qty * self.volume

stock_picking_volume/models/stock_move.py

+1-18
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,10 @@ def _compute_volume(self):
2626
qty = move.product_uom_qty
2727
if move.state in ("partially_available", "assigned"):
2828
qty = move.reserved_availability
29-
new_volume = move._get_volume_for_qty(qty=qty)
29+
new_volume = move.product_id._get_volume_for_qty(qty, move.product_uom)
3030
if move.volume != new_volume:
3131
move.volume = new_volume
3232

33-
def _get_volume_for_qty(self, qty):
34-
"""Return the volume for the move.
35-
36-
This method is meant to be inherited to change the volume
37-
computation for a specific move.
38-
39-
qty: float quantity to compute the volume for.
40-
41-
An override of this method could take into account the packaging
42-
of the product to compute the volume. (using the volume information
43-
on the packaging provided by the module stock_quant_package_dimension
44-
and the method product_qty_by_packaging on the product provided by the
45-
module stock_packaging_calculator)
46-
"""
47-
self.ensure_one()
48-
return qty * self.product_id.volume
49-
5033
def _compute_volume_uom_name(self):
5134
self.volume_uom_name = self.env[
5235
"product.template"

stock_picking_volume/tests/test_stock_picking_volume.py

+7
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,10 @@ def test_picking_with_canceled_move(self):
184184
self.picking.move_ids[1]._action_cancel()
185185
self.picking.invalidate_cache()
186186
self.assertEqual(self.picking.volume, 750)
187+
188+
def test_product_volume(self):
189+
self.assertEqual(self.product._get_volume_for_qty(5), 750)
190+
from_uom = self.env.ref("uom.product_uom_dozen")
191+
self.assertEqual(
192+
self.product._get_volume_for_qty(5 * from_uom.factor, from_uom), 750
193+
)
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from . import stock_move
1+
from . import product_product

stock_picking_volume_packaging/models/stock_move.py stock_picking_volume_packaging/models/product_product.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
# Copyright 2020-2022 Camptocamp SA
22
# Copyright 2023 ACSONE SA/NV
3+
# Copyright 2023 Michael Tietz (MT Software) <mtietz@mt-software.de>
34
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
45

56
from odoo import models
67

78

8-
class StockMove(models.Model):
9+
class ProductProduct(models.Model):
10+
_inherit = "product.product"
911

10-
_inherit = "stock.move"
11-
12-
def _get_volume_for_qty(self, qty):
12+
def _get_volume_for_qty(self, qty, from_uom=None):
1313
self.ensure_one()
14-
product = self.product_id
15-
if not product.packaging_ids.filtered("volume"):
16-
return super()._get_volume_for_qty(qty=qty)
17-
packagings_with_volume = product.with_context(
14+
if not self.packaging_ids.filtered("volume"):
15+
return super()._get_volume_for_qty(qty, from_uom)
16+
qty = from_uom and from_uom._compute_quantity(qty, self.uom_id) or qty
17+
packagings_with_volume = self.with_context(
1818
_packaging_filter=lambda p: p.volume
1919
).product_qty_by_packaging(qty)
2020
volume = 0
2121
for packaging_info in packagings_with_volume:
2222
if packaging_info.get("is_unit"):
23-
pack_volume = product.volume
23+
pack_volume = self.volume
2424
else:
2525
packaging = self.env["product.packaging"].browse(packaging_info["id"])
2626
pack_volume = packaging.volume

stock_picking_volume_packaging/tests/test_stock_move_volume.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def test_move_volume_package_no_dimension(self):
5151
move = self.env["stock.move"].new(
5252
{"product_id": self.product, "product_uom_qty": 16}
5353
)
54-
self.assertEqual(move._get_volume_for_qty(16), 2400)
54+
self.assertEqual(move.product_id._get_volume_for_qty(16), 2400)
5555

5656
def test_move_volume_package_with_dimension(self):
5757
"""
@@ -87,4 +87,4 @@ def test_move_volume_package_with_dimension(self):
8787
{"product_id": self.product, "product_uom_qty": 16}
8888
)
8989

90-
self.assertEqual(move._get_volume_for_qty(16), 153)
90+
self.assertEqual(move.product_id._get_volume_for_qty(16), 153)

0 commit comments

Comments
 (0)