changeset a3e3a447615f in modules/stock_shipment_measurements:default
details:
https://hg.tryton.org/modules/stock_shipment_measurements?cmd=changeset&node=a3e3a447615f
description:
Use configured unit for measurements
issue11118
review364571002
diffstat:
CHANGELOG | 1 +
__init__.py | 2 +
stock.py | 167 ++++++++++++++++++++++++++++++++++++++-----
stock.xml | 6 +
tryton.cfg | 1 +
view/configuration_form.xml | 12 +++
view/package_form.xml | 1 +
view/package_list.xml | 4 +-
view/package_tree.xml | 4 +-
view/shipment_list.xml | 4 +-
10 files changed, 175 insertions(+), 27 deletions(-)
diffs (374 lines):
diff -r 28194f054878 -r a3e3a447615f CHANGELOG
--- a/CHANGELOG Sun Jan 30 01:32:51 2022 +0100
+++ b/CHANGELOG Thu Feb 03 16:17:53 2022 +0100
@@ -1,3 +1,4 @@
+* Use configured unit for measurements
* Add packages weight
* Add support for Python 3.10
* Remove support for Python 3.6
diff -r 28194f054878 -r a3e3a447615f __init__.py
--- a/__init__.py Sun Jan 30 01:32:51 2022 +0100
+++ b/__init__.py Thu Feb 03 16:17:53 2022 +0100
@@ -10,6 +10,8 @@
def register():
Pool.register(
+ stock.Configuration,
+ stock.ConfigurationMeasurement,
stock.Move,
stock.ShipmentIn,
stock.ShipmentInReturn,
diff -r 28194f054878 -r a3e3a447615f stock.py
--- a/stock.py Sun Jan 30 01:32:51 2022 +0100
+++ b/stock.py Thu Feb 03 16:17:53 2022 +0100
@@ -1,16 +1,75 @@
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from collections import defaultdict
+from itertools import groupby
from sql.aggregate import Sum
from sql.operators import Concat
-from trytond.model import fields
+from trytond.model import ModelSQL, fields
+from trytond.modules.company.model import CompanyValueMixin
from trytond.pool import Pool, PoolMeta
+from trytond.pyson import Bool, Eval, Id
from trytond.tools import grouped_slice, reduce_ids
from trytond.transaction import Transaction
+class Configuration(metaclass=PoolMeta):
+ __name__ = 'stock.configuration'
+
+ measurement_weight_uom = fields.MultiValue(
+ fields.Many2One(
+ 'product.uom', "Measurement Weight Uom", required=True,
+ domain=[('category', '=', Id('product', 'uom_cat_weight'))]))
+ measurement_volume_uom = fields.MultiValue(
+ fields.Many2One(
+ 'product.uom', "Measurement Volume Uom", required=True,
+ domain=[('category', '=', Id('product', 'uom_cat_volume'))]))
+
+ @classmethod
+ def multivalue_model(cls, field):
+ pool = Pool()
+ if field in {'measurement_weight_uom', 'measurement_volume_uom'}:
+ return pool.get('stock.configuration.measurement')
+ return super().multivalue_model(field)
+
+ @classmethod
+ def default_measurement_weight_uom(cls, **pattern):
+ model = cls.multivalue_model('measurement_weight_uom')
+ return model.default_measurement_weight_uom()
+
+ @classmethod
+ def default_measurement_volume_uom(cls, **pattern):
+ model = cls.multivalue_model('measurement_volume_uom')
+ return model.default_measurement_volume_uom()
+
+
+class ConfigurationMeasurement(ModelSQL, CompanyValueMixin):
+ "Stock Configuration Measurement"
+ __name__ = 'stock.configuration.measurement'
+
+ measurement_weight_uom = fields.Many2One(
+ 'product.uom', "Measurement Weight Uom", required=True,
+ domain=[('category', '=', Id('product', 'uom_cat_weight'))])
+ measurement_volume_uom = fields.Many2One(
+ 'product.uom', "Measurement Volume Uom", required=True,
+ domain=[('category', '=', Id('product', 'uom_cat_volume'))])
+
+ @classmethod
+ def default_measurement_weight_uom(cls):
+ pool = Pool()
+ Uom = pool.get('product.uom')
+ ModelData = pool.get('ir.model.data')
+ return Uom(ModelData.get_id('product', 'uom_kilogram')).id
+
+ @classmethod
+ def default_measurement_volume_uom(cls):
+ pool = Pool()
+ Uom = pool.get('product.uom')
+ ModelData = pool.get('ir.model.data')
+ return Uom(ModelData.get_id('product', 'uom_liter')).id
+
+
class Move(metaclass=PoolMeta):
__name__ = 'stock.move'
@@ -112,19 +171,49 @@
class MeasurementsMixin(object):
__slots__ = ()
weight = fields.Function(
- fields.Float("Weight", digits=None,
- help="The total weight of the record's moves in kg."),
+ fields.Float(
+ "Weight", digits='weight_uom',
+ states={
+ 'invisible': ~Eval('weight'),
+ },
+ help="The total weight of the record's moves."),
'get_measurements', searcher='search_measurements')
+ weight_uom = fields.Function(
+ fields.Many2One('product.uom', "Weight Uom"), 'get_measurements_uom')
volume = fields.Function(
- fields.Float("Volume", digits=None,
- help="The total volume of the record's moves in liter."),
+ fields.Float(
+ "Volume", digits='volume_uom',
+ states={
+ 'invisible': ~Eval('volume'),
+ },
+ help="The total volume of the record's moves."),
'get_measurements', searcher='search_measurements')
+ volume_uom = fields.Function(
+ fields.Many2One('product.uom', "Volume Uom"), 'get_measurements_uom')
+
+ @classmethod
+ def get_measurements_uom(cls, shipments, name):
+ pool = Pool()
+ Configuration = pool.get('stock.configuration')
+ configuration = Configuration(1)
+ uoms = {}
+ for company, shipments in groupby(shipments, key=lambda s: s.company):
+ uom = configuration.get_multivalue(
+ 'measurement_%s' % name, company=company.id)
+ for shipment in shipments:
+ uoms[shipment.id] = uom.id
+ return uoms
@classmethod
def get_measurements(cls, shipments, names):
pool = Pool()
+ Location = pool.get('stock.location')
+ ModelData = pool.get('ir.model.data')
Move = pool.get('stock.move')
- Location = pool.get('stock.location')
+ Uom = pool.get('product.uom')
+
+ kg = Uom(ModelData.get_id('product', 'uom_kilogram'))
+ liter = Uom(ModelData.get_id('product', 'uom_liter'))
cursor = Transaction().connection.cursor()
table = cls.__table__()
@@ -145,34 +234,49 @@
Sum(move.internal_volume),
group_by=[table.id])
+ id2shipment = {s.id: s for s in shipments}
for sub_shipments in grouped_slice(shipments):
query.where = reduce_ids(
table.id, [s.id for s in sub_shipments])
cursor.execute(*query)
for id_, weight, volume in cursor:
+ shipment = id2shipment[id_]
if 'weight' in names:
- measurements['weight'][id_] = weight
+ measurements['weight'][id_] = Uom.compute_qty(
+ kg, weight, shipment.weight_uom)
if 'volume' in names:
- measurements['volume'][id_] = volume
+ measurements['volume'][id_] = Uom.compute_qty(
+ liter, volume, shipment.volume_uom)
return measurements
@classmethod
def search_measurements(cls, name, clause):
pool = Pool()
+ Configuration = pool.get('stock.configuration')
+ Location = pool.get('stock.location')
+ ModelData = pool.get('ir.model.data')
Move = pool.get('stock.move')
- Location = pool.get('stock.location')
+ Uom = pool.get('product.uom')
table = cls.__table__()
move = Move.__table__()
location = Location.__table__()
+ configuration = Configuration(1)
+ uom = configuration.get_multivalue('measurement_%s_uom' % name)
_, operator, value = clause
Operator = fields.SQL_OPERATORS[operator]
if name == 'weight':
measurement = Sum(move.internal_weight)
+ if value is not None:
+ kg = Uom(ModelData.get_id('product', 'uom_kilogram'))
+ value = Uom.compute_qty(uom, value, kg, round=False)
else:
measurement = Sum(move.internal_volume)
+ if value is not None:
+ liter = Uom(ModelData.get_id('product', 'uom_liter'))
+ value = Uom.compute_qty(uom, value, liter, round=False)
query = table.join(
move, type_='LEFT',
@@ -257,18 +361,40 @@
__name__ = 'stock.package'
additional_weight = fields.Float(
- "Additional Weight",
- help="The weight to add to the packages in kg.")
+ "Additional Weight", digits='additional_weight_uom',
+ help="The weight to add to the packages.")
+ additional_weight_uom = fields.Many2One(
+ 'product.uom', "Additional Weight Uom",
+ domain=[('category', '=', Id('product', 'uom_cat_weight'))],
+ states={
+ 'required': Bool(Eval('additional_weight')),
+ },
+ depends=['additional_weight'])
total_weight = fields.Function(
- fields.Float("Total Weight", digits=None,
- help="The total weight of the packages in kg."),
+ fields.Float(
+ "Total Weight", digits='weight_uom',
+ states={
+ 'invisible': ~Eval('total_weight'),
+ },
+ help="The total weight of the packages."),
'get_total_measurements')
total_volume = fields.Function(
- fields.Float("Total Volume", digits=None,
- help="The total volume of the packages in liter."),
+ fields.Float(
+ "Total Volume", digits='volume_uom',
+ states={
+ 'invisible': ~Eval('total_volume'),
+ },
+ help="The total volume of the packages."),
'get_total_measurements')
@classmethod
+ def default_additional_weight_uom(cls):
+ pool = Pool()
+ Configuration = pool.get('stock.configuration')
+ configuration = Configuration(1)
+ return configuration.get_multivalue('measurement_weight_uom').id
+
+ @classmethod
def _measurements_move_condition(cls, package, move):
return package.id == move.package
@@ -279,20 +405,19 @@
def get_total_measurements(self, name):
pool = Pool()
Uom = pool.get('product.uom')
- ModelData = pool.get('ir.model.data')
-
- kg = Uom(ModelData.get_id('product', 'uom_kilogram'))
field = name[len('total_'):]
measurement = ((getattr(self, field) or 0)
+ sum(p.get_total_measurements(name) for p in self.children))
if name == 'total_weight':
if self.additional_weight:
- measurement += self.additional_weight
+ measurement += Uom.compute_qty(
+ self.additional_weight_uom, self.additional_weight,
+ self.weight_uom, round=False)
if self.packaging_weight:
measurement += Uom.compute_qty(
- self.packaging_weight_uom, self.packaging_weight, kg,
- round=False)
+ self.packaging_weight_uom, self.packaging_weight,
+ self.weight_uom, round=False)
return measurement
diff -r 28194f054878 -r a3e3a447615f stock.xml
--- a/stock.xml Sun Jan 30 01:32:51 2022 +0100
+++ b/stock.xml Thu Feb 03 16:17:53 2022 +0100
@@ -3,6 +3,12 @@
this repository contains the full copyright notices and license terms. -->
<tryton>
<data>
+ <record model="ir.ui.view" id="configuration_view_form">
+ <field name="model">stock.configuration</field>
+ <field name="inherit" ref="stock.stock_configuration_view_form"/>
+ <field name="name">configuration_form</field>
+ </record>
+
<record model="ir.ui.view" id="shipment_in_view_list">
<field name="model">stock.shipment.in</field>
<field name="inherit" ref="stock.shipment_in_view_tree"/>
diff -r 28194f054878 -r a3e3a447615f tryton.cfg
--- a/tryton.cfg Sun Jan 30 01:32:51 2022 +0100
+++ b/tryton.cfg Thu Feb 03 16:17:53 2022 +0100
@@ -1,6 +1,7 @@
[tryton]
version=6.3.0
depends:
+ company
ir
product
product_measurements
diff -r 28194f054878 -r a3e3a447615f view/configuration_form.xml
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/view/configuration_form.xml Thu Feb 03 16:17:53 2022 +0100
@@ -0,0 +1,12 @@
+<?xml version="1.0"?>
+<!-- This file is part of Tryton. The COPYRIGHT file at the top level of
+this repository contains the full copyright notices and license terms. -->
+<data>
+ <xpath expr="//form" position="inside">
+ <separator id="measurements" colspan="4" string="Measurements"/>
+ <label name="measurement_weight_uom" string="Weight"/>
+ <field name="measurement_weight_uom"/>
+ <label name="measurement_volume_uom" string="Volume"/>
+ <field name="measurement_volume_uom"/>
+ </xpath>
+</data>
diff -r 28194f054878 -r a3e3a447615f view/package_form.xml
--- a/view/package_form.xml Sun Jan 30 01:32:51 2022 +0100
+++ b/view/package_form.xml Thu Feb 03 16:17:53 2022 +0100
@@ -5,6 +5,7 @@
<xpath expr="//page[@id='measurements']" position="inside">
<label name="additional_weight"/>
<field name="additional_weight"/>
+ <field name="additional_weight_uom"/>
<newline/>
</xpath>
</data>
diff -r 28194f054878 -r a3e3a447615f view/package_list.xml
--- a/view/package_list.xml Sun Jan 30 01:32:51 2022 +0100
+++ b/view/package_list.xml Thu Feb 03 16:17:53 2022 +0100
@@ -3,7 +3,7 @@
this repository contains the full copyright notices and license terms. -->
<data>
<xpath expr="/tree/field[@name='type']" position="after">
- <field name="weight"/>
- <field name="volume"/>
+ <field name="weight" symbol="weight_uom"/>
+ <field name="volume" symbol="volume_uom"/>
</xpath>
</data>
diff -r 28194f054878 -r a3e3a447615f view/package_tree.xml
--- a/view/package_tree.xml Sun Jan 30 01:32:51 2022 +0100
+++ b/view/package_tree.xml Thu Feb 03 16:17:53 2022 +0100
@@ -3,7 +3,7 @@
this repository contains the full copyright notices and license terms. -->
<data>
<xpath expr="/tree/field[@name='type']" position="after">
- <field name="total_weight"/>
- <field name="total_volume"/>
+ <field name="total_weight" symbol="weight_uom"/>
+ <field name="total_volume" symbol="volume_uom"/>
</xpath>
</data>
diff -r 28194f054878 -r a3e3a447615f view/shipment_list.xml
--- a/view/shipment_list.xml Sun Jan 30 01:32:51 2022 +0100
+++ b/view/shipment_list.xml Thu Feb 03 16:17:53 2022 +0100
@@ -3,7 +3,7 @@
this repository contains the full copyright notices and license terms. -->
<data>
<xpath expr="/tree/field[@name='state']" position="before">
- <field name="weight"/>
- <field name="volume"/>
+ <field name="weight" symbol="weight_uom"/>
+ <field name="volume" symbol="volume_uom"/>
</xpath>
</data>