changeset c97ccd66de3a in modules/stock_package:default
details: 
https://hg.tryton.org/modules/stock_package?cmd=changeset&node=c97ccd66de3a
description:
        Include package weight to weight parcel

        issue10145
        review335721002
diffstat:

 CHANGELOG                  |   3 ++
 stock.py                   |  69 ++++++++++++++++++++++++++++++++++++++++++++--
 tryton.cfg                 |   3 ++
 view/package_form.xml      |  15 ++++++++-
 view/package_type_form.xml |   5 +++
 5 files changed, 90 insertions(+), 5 deletions(-)

diffs (178 lines):

diff -r c912f787d5f5 -r c97ccd66de3a CHANGELOG
--- a/CHANGELOG Sun Mar 21 22:42:17 2021 +0100
+++ b/CHANGELOG Mon Mar 29 23:40:01 2021 +0200
@@ -1,3 +1,6 @@
+* Include package weight to weight parcel
+* Add package weight
+
 Version 5.8.0 - 2020-11-02
 * Bug fixes (see mercurial logs for details)
 * Remove support for Python 3.5
diff -r c912f787d5f5 -r c97ccd66de3a stock.py
--- a/stock.py  Sun Mar 21 22:42:17 2021 +0100
+++ b/stock.py  Mon Mar 29 23:40:01 2021 +0200
@@ -7,7 +7,7 @@
 from trytond.i18n import gettext
 from trytond.model import ModelSQL, ModelView, Workflow, fields, tree
 from trytond.pool import Pool, PoolMeta
-from trytond.pyson import Eval, Id
+from trytond.pyson import Eval, Bool, Id
 from trytond.report import Report
 from trytond.transaction import Transaction
 
@@ -77,7 +77,32 @@
             return None
 
 
-class Package(tree(), ModelSQL, ModelView):
+class WeightMixin:
+    __slots__ = ()
+
+    packaging_weight = fields.Float(
+        "Packaging Weight", digits=(16, Eval('packaging_weight_digits', 2)),
+        depends=['packaging_weight_digits'],
+        help="The weight of the package when empty.")
+    packaging_weight_uom = fields.Many2One(
+        'product.uom', "Packaging Weight Uom",
+        domain=[('category', '=', Id('product', 'uom_cat_weight'))],
+        states={
+            'required': Bool(Eval('packaging_weight')),
+            },
+        depends=['packaging_weight'])
+    packaging_weight_digits = fields.Function(
+        fields.Integer("Packaging Weight Digits"),
+        'on_change_with_packaging_weight_digits')
+
+    @fields.depends('packaging_weight_uom')
+    def on_change_with_packaging_weight_digits(self, name=None):
+        return (
+            self.packaging_weight_uom.digits if self.packaging_weight_uom
+            else None)
+
+
+class Package(tree(), WeightMixin, ModelSQL, ModelView):
     'Stock Package'
     __name__ = 'stock.package'
     _rec_name = 'code'
@@ -190,6 +215,12 @@
             return 'closed'
         return 'open'
 
+    @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
+
     @classmethod
     def create(cls, vlist):
         pool = Pool()
@@ -211,7 +242,7 @@
         return super().copy(packages, default=default)
 
 
-class Type(ModelSQL, ModelView):
+class Type(WeightMixin, ModelSQL, ModelView):
     'Stock Package Type'
     __name__ = 'stock.package.type'
     name = fields.Char('Name', required=True)
@@ -229,6 +260,16 @@
             },
         depends=['company', 'state'])
 
+    @property
+    def package_path(self):
+        path = []
+        package = self.package
+        while package:
+            path.append(package)
+            package = package.parent
+        path.reverse()
+        return path
+
     @classmethod
     def copy(cls, moves, default=None):
         if default is None:
@@ -316,6 +357,28 @@
     def packages_moves(self):
         return (m for m in self.outgoing_moves if m.state != 'cancelled')
 
+    def _group_parcel_key(self, lines, line):
+        try:
+            root_package = line.package_path[0]
+        except IndexError:
+            root_package = None
+        return super()._group_parcel_key(lines, line) + (root_package,)
+
+    @fields.depends('carrier')
+    def _parcel_weight(self, parcel):
+        pool = Pool()
+        Uom = pool.get('product.uom')
+        weight = super()._parcel_weight(parcel)
+        if self.carrier:
+            carrier_uom = self.carrier.weight_uom
+            packages = {p for l in parcel for p in l.package_path}
+            for package in packages:
+                if package.packaging_weight:
+                    weight += Uom.compute_qty(
+                        package.packaging_weight_uom, package.packaging_weight,
+                        carrier_uom, round=False)
+        return weight
+
 
 class ShipmentInReturn(PackageMixin, object, metaclass=PoolMeta):
     __name__ = 'stock.shipment.in.return'
diff -r c912f787d5f5 -r c97ccd66de3a tryton.cfg
--- a/tryton.cfg        Sun Mar 21 22:42:17 2021 +0100
+++ b/tryton.cfg        Mon Mar 29 23:40:01 2021 +0200
@@ -3,8 +3,11 @@
 depends:
     company
     ir
+    product
     res
     stock
+extras_depend:
+    carrier_weight
 xml:
     stock.xml
     message.xml
diff -r c912f787d5f5 -r c97ccd66de3a view/package_form.xml
--- a/view/package_form.xml     Sun Mar 21 22:42:17 2021 +0100
+++ b/view/package_form.xml     Mon Mar 29 23:40:01 2021 +0200
@@ -12,8 +12,19 @@
     <field name="company"/>
     <label name="shipment"/>
     <field name="shipment"/>
-    <field name="moves" colspan="4" widget="many2many"/>
-    <field name="children" colspan="4"/>
+    <notebook colspan="4">
+        <page name="moves">
+            <field name="moves" colspan="4" widget="many2many"/>
+        </page>
+        <page name="children">
+            <field name="children" colspan="4"/>
+        </page>
+        <page string="Measurements" col="3" id="measurements">
+            <label name="packaging_weight"/>
+            <field name="packaging_weight"/>
+            <field name="packaging_weight_uom"/>
+        </page>
+    </notebook>
     <label name="state"/>
     <field name="state"/>
 </form>
diff -r c912f787d5f5 -r c97ccd66de3a view/package_type_form.xml
--- a/view/package_type_form.xml        Sun Mar 21 22:42:17 2021 +0100
+++ b/view/package_type_form.xml        Mon Mar 29 23:40:01 2021 +0200
@@ -5,5 +5,10 @@
     <label name="name"/>
     <field name="name"/>
     <notebook colspan="4">
+        <page string="Measurements" col="3" id="measurements">
+            <label name="packaging_weight"/>
+            <field name="packaging_weight"/>
+            <field name="packaging_weight_uom"/>
+        </page>
     </notebook>
 </form>

Reply via email to