Use quantity if unit is weight as fallback for missing measurement

Same design as for stock_shipment_measurements.

issue6239
review29181003
This commit is contained in:
Cédric Krier
2017-02-14 23:38:51 +01:00
parent bcddc90ba6
commit 4c283ec503
4 changed files with 39 additions and 44 deletions
+2
View File
@@ -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)
+27
View File
@@ -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
+5 -15
View File
@@ -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
+5 -29
View File
@@ -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