diff --git a/stock.py b/stock.py index 0d8f5e01d6..83bc07a044 100644 --- a/stock.py +++ b/stock.py @@ -402,13 +402,20 @@ class Package(MeasurementsMixin, object, metaclass=PoolMeta): def _measurements_location_condition(cls, package, move, location): return move.to_location == location.id - def get_total_measurements(self, name): + def get_total_measurements(self, name, round=True): pool = Pool() Uom = pool.get('product.uom') - field = name[len('total_'):] - measurement = ((getattr(self, field) or 0) - + sum(p.get_total_measurements(name) for p in self.children)) + + if name == 'total_volume' and self.packaging_volume is not None: + return Uom.compute_qty( + self.packaging_volume_uom, self.packaging_volume, + self.volume_uom, round=round) + + measurement = ( + (getattr(self, field) or 0) + + sum(p.get_total_measurements(name, round=False) + for p in self.children)) if name == 'total_weight': if self.additional_weight: measurement += Uom.compute_qty( @@ -418,19 +425,29 @@ class Package(MeasurementsMixin, object, metaclass=PoolMeta): measurement += Uom.compute_qty( self.packaging_weight_uom, self.packaging_weight, self.weight_uom, round=False) - return measurement + if round: + return getattr(self, field + '_uom').round(measurement) + else: + return measurement class MeasurementsPackageMixin: __slots__ = () packages_weight = fields.Function( - fields.Float("Packages Weight", digits=None, - help="The total weight of the packages in kg."), - 'get_packages_weight') + fields.Float("Packages Weight", digits='weight_uom', + help="The total weight of the packages."), + 'get_packages_measurements') + packages_volume = fields.Function( + fields.Float("Packages Volume", digits='volume_uom', + help="The total volume of the packages."), + 'get_packages_measurements') - def get_packages_weight(self, name): - return sum(p.total_weight for p in self.root_packages) + def get_packages_measurements(self, name): + name = name[len('packages_'):] + uom = getattr(self, name + '_uom') + return uom.round( + sum(getattr(p, 'total_' + name)for p in self.root_packages)) class ShipmentOutPackage(MeasurementsPackageMixin, metaclass=PoolMeta): diff --git a/tests/test_stock_shipment_measurements.py b/tests/test_stock_shipment_measurements.py index c4e1256ec4..ccdfe00b3c 100644 --- a/tests/test_stock_shipment_measurements.py +++ b/tests/test_stock_shipment_measurements.py @@ -198,6 +198,8 @@ class StockShipmentMeasurementsTestCase(CompanyTestMixin, ModuleTestCase): package_root.type = package_type package_root.shipment = shipment package_root.additional_weight = 1 + package_root.packaging_volume = 3 + package_root.packaging_volume_uom = liter package_root.save() package = Package() @@ -214,7 +216,10 @@ class StockShipmentMeasurementsTestCase(CompanyTestMixin, ModuleTestCase): self.assertEqual(package.volume, 2) self.assertEqual(package_root.total_weight, 11) - self.assertEqual(package_root.total_volume, 2) + self.assertEqual(package_root.total_volume, 3) + + self.assertEqual(package.total_weight, 10) + self.assertEqual(package.total_volume, 2) def suite(): diff --git a/view/shipment_form_package.xml b/view/shipment_form_package.xml index edcf4f0040..72d036d52d 100644 --- a/view/shipment_form_package.xml +++ b/view/shipment_form_package.xml @@ -6,4 +6,8 @@ this repository contains the full copyright notices and license terms. -->