changeset 18e7528fb716 in modules/stock_package:default
details: 
https://hg.tryton.org/modules/stock_package?cmd=changeset&node=18e7528fb716
description:
        Add volume dimensions to package

        issue11214
        review397221003
diffstat:

 CHANGELOG                        |    1 +
 exceptions.py                    |    5 +
 message.xml                      |    3 +
 stock.py                         |  129 ++++++++++++++++++++++++++++++++++++--
 tests/scenario_stock_package.rst |   27 +++++++-
 view/package_form.xml            |   16 ++++
 view/package_type_form.xml       |   18 +++++-
 7 files changed, 189 insertions(+), 10 deletions(-)

diffs (306 lines):

diff -r b5caa8be2061 -r 18e7528fb716 CHANGELOG
--- a/CHANGELOG Sun Mar 06 18:17:01 2022 +0100
+++ b/CHANGELOG Mon Mar 07 00:30:47 2022 +0100
@@ -1,3 +1,4 @@
+* Add volume dimensions to package
 * Filter out moves without quantity from package
 * Add support for Python 3.10
 * Remove support for Python 3.6
diff -r b5caa8be2061 -r 18e7528fb716 exceptions.py
--- a/exceptions.py     Sun Mar 06 18:17:01 2022 +0100
+++ b/exceptions.py     Mon Mar 07 00:30:47 2022 +0100
@@ -2,7 +2,12 @@
 # this repository contains the full copyright notices and license terms.
 
 from trytond.exceptions import UserError
+from trytond.model.exceptions import ValidationError
 
 
 class PackageError(UserError):
     pass
+
+
+class PackageValidationError(ValidationError):
+    pass
diff -r b5caa8be2061 -r 18e7528fb716 message.xml
--- a/message.xml       Sun Mar 06 18:17:01 2022 +0100
+++ b/message.xml       Mon Mar 07 00:30:47 2022 +0100
@@ -6,5 +6,8 @@
         <record model="ir.message" id="msg_package_mismatch">
             <field name="text">To process shipment "%(shipment)s", you must 
pack all its moves.</field>
         </record>
+        <record model="ir.message" id="msg_package_volume_too_small">
+            <field name="text">The volume (%(volume)s) of package 
"%(package)s" is too small for summed volume (%(children_volume)s) of its 
children packages.</field>
+        </record>
     </data>
 </tryton>
diff -r b5caa8be2061 -r 18e7528fb716 stock.py
--- a/stock.py  Sun Mar 06 18:17:01 2022 +0100
+++ b/stock.py  Mon Mar 07 00:30:47 2022 +0100
@@ -10,7 +10,7 @@
 from trytond.pyson import Bool, Eval, Id, If
 from trytond.transaction import Transaction
 
-from .exceptions import PackageError
+from .exceptions import PackageError, PackageValidationError
 
 
 class Configuration(metaclass=PoolMeta):
@@ -77,22 +77,108 @@
             return None
 
 
-class WeightMixin:
+class MeasurementsMixin:
     __slots__ = ()
 
+    packaging_length = fields.Float(
+        "Packaging Length", digits='packaging_length_uom',
+        help="The length of the package.")
+    packaging_length_uom = fields.Many2One(
+        'product.uom', "Packaging Length UOM",
+        domain=[('category', '=', Id('product', 'uom_cat_length'))],
+        states={
+            'required': Bool(Eval('packaging_length')),
+            },
+        depends=['packaging_length'])
+    packaging_height = fields.Float(
+        "Packaging Height", digits='packaging_height_uom',
+        help="The height of the package.")
+    packaging_height_uom = fields.Many2One(
+        'product.uom', "Packaging Height UOM",
+        domain=[('category', '=', Id('product', 'uom_cat_length'))],
+        states={
+            'required': Bool(Eval('packaging_height')),
+            },
+        depends=['packaging_height'])
+    packaging_width = fields.Float(
+        "Packaging Width", digits='packaging_width_uom',
+        help="The width of the package.")
+    packaging_width_uom = fields.Many2One(
+        'product.uom', "Packaging Width UOM",
+        domain=[('category', '=', Id('product', 'uom_cat_length'))],
+        states={
+            'required': Bool(Eval('packaging_width')),
+            },
+        depends=['packaging_width'])
+
+    packaging_volume = fields.Float(
+        "Packaging Volume", digits='packaging_volume_uom',
+        states={
+            'readonly': (
+                Bool(Eval('packaging_length'))
+                & Bool(Eval('packaging_height'))
+                & Bool(Eval('packaging_width'))),
+            },
+        depends=['packaging_length', 'packaging_height', 'packaging_width'],
+        help="The volume of the package.")
+    packaging_volume_uom = fields.Many2One(
+        'product.uom', "Packaging Volume UOM",
+        domain=[('category', '=', Id('product', 'uom_cat_volume'))],
+        states={
+            'required': Bool(Eval('packaging_volume')),
+            },
+        depends=['packaging_volume'])
+
     packaging_weight = fields.Float(
         "Packaging Weight", digits='packaging_weight_uom',
         help="The weight of the package when empty.")
     packaging_weight_uom = fields.Many2One(
-        'product.uom', "Packaging Weight Uom",
+        'product.uom', "Packaging Weight UOM",
         domain=[('category', '=', Id('product', 'uom_cat_weight'))],
         states={
             'required': Bool(Eval('packaging_weight')),
             },
         depends=['packaging_weight'])
 
+    @fields.depends(
+        'packaging_volume', 'packaging_volume_uom',
+        'packaging_length', 'packaging_length_uom',
+        'packaging_height', 'packaging_height_uom',
+        'packaging_width', 'packaging_width_uom')
+    def on_change_with_packaging_volume(self):
+        pool = Pool()
+        ModelData = pool.get('ir.model.data')
+        Uom = pool.get('product.uom')
 
-class Package(tree(), WeightMixin, ModelSQL, ModelView):
+        if not all([self.packaging_volume_uom,
+                    self.packaging_length, self.packaging_length_uom,
+                    self.packaging_height, self.packaging_height_uom,
+                    self.packaging_width, self.packaging_width_uom]):
+            if all([
+                        self.packaging_length,
+                        self.packaging_height,
+                        self.packaging_width]):
+                return
+            return self.packaging_volume
+
+        meter = Uom(ModelData.get_id('product', 'uom_meter'))
+        cubic_meter = Uom(ModelData.get_id('product', 'uom_cubic_meter'))
+
+        length = Uom.compute_qty(
+            self.packaging_length_uom, self.packaging_length, meter,
+            round=False)
+        height = Uom.compute_qty(
+            self.packaging_height_uom, self.packaging_height, meter,
+            round=False)
+        width = Uom.compute_qty(
+            self.packaging_width_uom, self.packaging_width, meter,
+            round=False)
+
+        return Uom.compute_qty(
+            cubic_meter, length * height * width, self.packaging_volume_uom)
+
+
+class Package(tree(), MeasurementsMixin, ModelSQL, ModelView):
     'Stock Package'
     __name__ = 'stock.package'
     _rec_name = 'code'
@@ -216,8 +302,37 @@
     @fields.depends('type')
     def on_change_type(self):
         if self.type:
-            self.packaging_weight = self.type.packaging_weight
-            self.packaging_weight_uom = self.type.packaging_weight_uom
+            for name in dir(MeasurementsMixin):
+                if isinstance(getattr(MeasurementsMixin, name), fields.Field):
+                    setattr(self, name, getattr(self.type, name))
+
+    @classmethod
+    def validate(cls, packages):
+        super().validate(packages)
+        for package in packages:
+            package.check_volume()
+
+    def check_volume(self):
+        pool = Pool()
+        Uom = pool.get('product.uom')
+        Lang = pool.get('ir.lang')
+        lang = Lang.get()
+        if not self.packaging_volume:
+            return
+        children_volume = 0
+        for child in self.children:
+            if child.packaging_volume:
+                children_volume += Uom.compute_qty(
+                    child.packaging_volume_uom, child.packaging_volume,
+                    self.packaging_volume_uom, round=False)
+        if self.packaging_volume < children_volume:
+            raise PackageValidationError(
+                gettext('stock_package.msg_package_volume_too_small',
+                    package=self.rec_name,
+                    volume=lang.format_number_symbol(
+                        self.packaging_volume, self.packaging_volume_uom),
+                    children_volume=lang.format_number_symbol(
+                        children_volume, self.packaging_volume_uom)))
 
     @classmethod
     def create(cls, vlist):
@@ -243,7 +358,7 @@
         return super().copy(packages, default=default)
 
 
-class Type(WeightMixin, ModelSQL, ModelView):
+class Type(MeasurementsMixin, ModelSQL, ModelView):
     'Stock Package Type'
     __name__ = 'stock.package.type'
     name = fields.Char('Name', required=True)
diff -r b5caa8be2061 -r 18e7528fb716 tests/scenario_stock_package.rst
--- a/tests/scenario_stock_package.rst  Sun Mar 06 18:17:01 2022 +0100
+++ b/tests/scenario_stock_package.rst  Mon Mar 07 00:30:47 2022 +0100
@@ -86,12 +86,35 @@
 
     >>> PackageType = Model.get('stock.package.type')
     >>> box = PackageType(name='box')
+    >>> box.packaging_length = 80
+    >>> box.packaging_length_uom, = ProductUom.find([('name', '=', 
"Centimeter")])
+    >>> box.packaging_width = 1
+    >>> box.packaging_width_uom, = ProductUom.find([('name', '=', "Meter")])
+    >>> box.packaging_height_uom = box.packaging_length_uom
+    >>> box.packaging_volume
+    >>> box.packaging_volume_uom, = ProductUom.find([('name', '=', "Cubic 
meter")])
     >>> box.save()
     >>> package1 = shipment_out.packages.new(type=box)
-    >>> moves = package1.moves.find()
+    >>> package1.packaging_length
+    80.0
+    >>> package1.packaging_height = 50
+    >>> package1.packaging_volume
+    0.4
+    >>> package_child = package1.children.new(shipment=shipment_out, type=box)
+    >>> package_child.packaging_height = 100
+    >>> moves = package_child.moves.find()
     >>> len(moves)
     2
-    >>> package1.moves.append(moves[0])
+    >>> package_child.moves.append(moves[0])
+
+    >>> shipment_out.save()  # doctest: +IGNORE_EXCEPTION_DETAIL
+    Traceback (most recent call last):
+        ...
+    PackageValidationError: ...
+
+    >>> package1.packaging_height = 120
+    >>> package1.packaging_volume
+    0.96
 
     >>> shipment_out.click('pack')  # doctest: +IGNORE_EXCEPTION_DETAIL
     Traceback (most recent call last):
diff -r b5caa8be2061 -r 18e7528fb716 view/package_form.xml
--- a/view/package_form.xml     Sun Mar 06 18:17:01 2022 +0100
+++ b/view/package_form.xml     Mon Mar 07 00:30:47 2022 +0100
@@ -20,6 +20,22 @@
             <field name="children" colspan="4"/>
         </page>
         <page string="Measurements" col="3" id="measurements">
+            <label name="packaging_length"/>
+            <field name="packaging_length"/>
+            <field name="packaging_length_uom" widget="selection"/>
+
+            <label name="packaging_height"/>
+            <field name="packaging_height"/>
+            <field name="packaging_height_uom" widget="selection"/>
+
+            <label name="packaging_width"/>
+            <field name="packaging_width"/>
+            <field name="packaging_width_uom" widget="selection"/>
+
+            <label name="packaging_volume"/>
+            <field name="packaging_volume"/>
+            <field name="packaging_volume_uom" widget="selection"/>
+
             <label name="packaging_weight"/>
             <field name="packaging_weight"/>
             <field name="packaging_weight_uom"/>
diff -r b5caa8be2061 -r 18e7528fb716 view/package_type_form.xml
--- a/view/package_type_form.xml        Sun Mar 06 18:17:01 2022 +0100
+++ b/view/package_type_form.xml        Mon Mar 07 00:30:47 2022 +0100
@@ -6,9 +6,25 @@
     <field name="name"/>
     <notebook colspan="4">
         <page string="Measurements" col="3" id="measurements">
+            <label name="packaging_length"/>
+            <field name="packaging_length"/>
+            <field name="packaging_length_uom" widget="selection"/>
+
+            <label name="packaging_height"/>
+            <field name="packaging_height"/>
+            <field name="packaging_height_uom" widget="selection"/>
+
+            <label name="packaging_width"/>
+            <field name="packaging_width"/>
+            <field name="packaging_width_uom" widget="selection"/>
+
+            <label name="packaging_volume"/>
+            <field name="packaging_volume"/>
+            <field name="packaging_volume_uom" widget="selection"/>
+
             <label name="packaging_weight"/>
             <field name="packaging_weight"/>
-            <field name="packaging_weight_uom"/>
+            <field name="packaging_weight_uom" widget="selection"/>
         </page>
     </notebook>
 </form>

Reply via email to