[IMP] stock_picking_volume: Move _get_volume_for_qty to product.product

This commit is contained in:
Michael Tietz
2023-07-11 16:35:36 +02:00
parent ef2e3045bf
commit 8b1ef46d5d
7 changed files with 48 additions and 30 deletions

View File

@@ -1,2 +1,3 @@
from . import product_product
from . import stock_move
from . import stock_picking

View File

@@ -0,0 +1,27 @@
# Copyright 2023 Michael Tietz (MT Software) <mtietz@mt-software.de>
# Copyright 2023 ACSONE SA/NV
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import models
class ProductProduct(models.Model):
_inherit = "product.product"
def _get_volume_for_qty(self, qty, from_uom=None):
"""Return the volume for the given qty.
This method is meant to be inherited to change the volume
computation for a specific product.
qty: float quantity to compute the volume for.
from_uom: uom of given qty
An override of this method could take into account the packaging
of the product to compute the volume. (using the volume information
on the packaging provided by the module stock_quant_package_dimension
and the method product_qty_by_packaging on the product provided by the
module stock_packaging_calculator)
"""
self.ensure_one()
qty = from_uom and from_uom._compute_quantity(qty, self.uom_id) or qty
return qty * self.volume

View File

@@ -23,27 +23,10 @@ class StockMove(models.Model):
qty = move.product_uom_qty
if move.state in ("partially_available", "assigned"):
qty = move.reserved_availability
new_volume = move._get_volume_for_qty(qty=qty)
new_volume = move.product_id._get_volume_for_qty(qty, move.product_uom)
if move.volume != new_volume:
move.volume = new_volume
def _get_volume_for_qty(self, qty):
"""Return the volume for the move.
This method is meant to be inherited to change the volume
computation for a specific move.
qty: float quantity to compute the volume for.
An override of this method could take into account the packaging
of the product to compute the volume. (using the volume information
on the packaging provided by the module stock_quant_package_dimension
and the method product_qty_by_packaging on the product provided by the
module stock_packaging_calculator)
"""
self.ensure_one()
return qty * self.product_id.volume
def _compute_volume_uom_name(self):
self.volume_uom_name = self.env[
"product.template"

View File

@@ -189,3 +189,10 @@ class TestStockPickingVolume(SavepointCase):
self.picking.move_lines[1]._action_cancel()
self.picking.invalidate_cache()
self.assertEqual(self.picking.volume, 750)
def test_product_volume(self):
self.assertEqual(self.product._get_volume_for_qty(5), 750)
from_uom = self.env.ref("uom.product_uom_dozen")
self.assertEqual(
self.product._get_volume_for_qty(5 * from_uom.factor, from_uom), 750
)

View File

@@ -1 +1 @@
from . import stock_move
from . import product_product

View File

@@ -1,26 +1,26 @@
# Copyright 2020-2022 Camptocamp SA
# Copyright 2023 ACSONE SA/NV
# Copyright 2023 Michael Tietz (MT Software) <mtietz@mt-software.de>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import models
class StockMove(models.Model):
class ProductProduct(models.Model):
_inherit = "product.product"
_inherit = "stock.move"
def _get_volume_for_qty(self, qty):
def _get_volume_for_qty(self, qty, from_uom=None):
self.ensure_one()
product = self.product_id
if not product.packaging_ids.filtered("volume"):
return super()._get_volume_for_qty(qty=qty)
packagings_with_volume = product.with_context(
if not self.packaging_ids.filtered("volume"):
return super()._get_volume_for_qty(qty, from_uom)
qty = from_uom and from_uom._compute_quantity(qty, self.uom_id) or qty
packagings_with_volume = self.with_context(
_packaging_filter=lambda p: p.volume
).product_qty_by_packaging(qty)
volume = 0
for packaging_info in packagings_with_volume:
if packaging_info.get("is_unit"):
pack_volume = product.volume
pack_volume = self.volume
else:
packaging = self.env["product.packaging"].browse(packaging_info["id"])
pack_volume = packaging.volume

View File

@@ -52,7 +52,7 @@ class TestStockMoveVolume(SavepointCase):
move = self.env["stock.move"].new(
{"product_id": self.product, "product_uom_qty": 16}
)
self.assertEqual(move._get_volume_for_qty(16), 2400)
self.assertEqual(move.product_id._get_volume_for_qty(16), 2400)
def test_move_volume_package_with_dimension(self):
"""
@@ -88,4 +88,4 @@ class TestStockMoveVolume(SavepointCase):
{"product_id": self.product, "product_uom_qty": 16}
)
self.assertEqual(move._get_volume_for_qty(16), 153)
self.assertEqual(move.product_id._get_volume_for_qty(16), 153)