From 4c283ec503b6b0e00364ff49bde34659cb4ebffd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Tue, 14 Feb 2017 23:38:51 +0100 Subject: [PATCH] Use quantity if unit is weight as fallback for missing measurement Same design as for stock_shipment_measurements. issue6239 review29181003 --- CHANGELOG | 2 ++ common.py | 27 +++++++++++++++++++++++++++ sale.py | 20 +++++--------------- stock.py | 34 +++++----------------------------- 4 files changed, 39 insertions(+), 44 deletions(-) create mode 100644 common.py diff --git a/CHANGELOG b/CHANGELOG index 0f962c865a..3138514e7a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Use quantity if unit is weight as fallback for missing measurement + Version 4.2.0 - 2016-11-28 * Bug fixes (see mercurial logs for details) diff --git a/common.py b/common.py new file mode 100644 index 0000000000..c22a0f1408 --- /dev/null +++ b/common.py @@ -0,0 +1,27 @@ +# 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 trytond.pool import Pool + + +def parcel_weight(parcel, carrier_uom, uom_field='uom'): + pool = Pool() + Uom = pool.get('product.uom') + + weight = 0 + for line in parcel: + product = getattr(line, 'product', None) + quantity = getattr(line, 'quantity', None) + uom = getattr(line, uom_field, None) + + if not all([product, quantity, uom]): + continue + + if product.weight is not None: + internal_quantity = Uom.compute_qty( + uom, quantity, product.default_uom, round=False) + weight += Uom.compute_qty( + product.weight_uom, internal_quantity * product.weight, + carrier_uom, round=False) + elif uom.category == carrier_uom.category: + weight += Uom.compute_qty(uom, quantity, carrier_uom, round=False) + return weight diff --git a/sale.py b/sale.py index c658331298..8584c11620 100644 --- a/sale.py +++ b/sale.py @@ -3,7 +3,9 @@ from functools import partial from itertools import groupby -from trytond.pool import Pool, PoolMeta +from trytond.pool import PoolMeta + +from .common import parcel_weight __all__ = ['Sale'] @@ -19,8 +21,6 @@ class Sale: return None def _get_carrier_context(self): - Uom = Pool().get('product.uom') - context = super(Sale, self)._get_carrier_context() if self.carrier.carrier_cost_method != 'weight': @@ -34,16 +34,6 @@ class Sale: lines = sorted(lines, key=keyfunc) for key, parcel in groupby(lines, key=keyfunc): - weight = 0 - for line in parcel: - if (getattr(line, 'product', None) - and getattr(line, 'quantity', None) - and getattr(line, 'unit', None)): - quantity = Uom.compute_qty(line.unit, line.quantity, - line.product.default_uom, round=False) - if line.product.weight: - weight += Uom.compute_qty(line.product.weight_uom, - line.product.weight * quantity, - self.carrier.weight_uom, round=False) - weights.append(weight) + weights.append(parcel_weight( + parcel, self.carrier.weight_uom, 'unit')) return context diff --git a/stock.py b/stock.py index e8b6286533..977c0fcde2 100644 --- a/stock.py +++ b/stock.py @@ -3,7 +3,9 @@ from itertools import groupby from functools import partial -from trytond.pool import Pool, PoolMeta +from trytond.pool import PoolMeta + +from .common import parcel_weight __all__ = ['ShipmentIn', 'ShipmentOut'] @@ -19,8 +21,6 @@ class ShipmentIn: return None def _get_carrier_context(self): - Uom = Pool().get('product.uom') - context = super(ShipmentIn, self)._get_carrier_context() if not self.carrier: return context @@ -35,18 +35,7 @@ class ShipmentIn: lines = sorted(lines, key=keyfunc) for key, parcel in groupby(lines, key=keyfunc): - weight = 0 - for line in parcel: - if (getattr(line, 'product', None) - and getattr(line, 'quantity', None) - and getattr(line, 'uom', None) - and line.product.weight): - quantity = Uom.compute_qty(line.uom, line.quantity, - line.product.default_uom, round=False) - weight += Uom.compute_qty(line.product.weight_uom, - line.product.weight * quantity, - self.carrier.weight_uom, round=False) - weights.append(weight) + weights.append(parcel_weight(parcel, self.carrier.weight_uom)) return context @@ -61,8 +50,6 @@ class ShipmentOut: return None def _get_carrier_context(self): - Uom = Pool().get('product.uom') - context = super(ShipmentOut, self)._get_carrier_context() if not self.carrier: return context @@ -77,16 +64,5 @@ class ShipmentOut: lines = sorted(lines, key=keyfunc) for key, parcel in groupby(lines, key=keyfunc): - weight = 0 - for line in parcel: - if (line.product - and line.quantity - and line.uom - and line.product.weight): - quantity = Uom.compute_qty(line.uom, line.quantity, - line.product.default_uom, round=False) - weight += Uom.compute_qty(line.product.weight_uom, - line.product.weight * quantity, - self.carrier.weight_uom, round=False) - weights.append(weight) + weights.append(parcel_weight(parcel, self.carrier.weight_uom)) return context