Files
tryton/stock.py
T
Albert Cervera i Areny 9f2c98f288 Avoid crash when weight is NULL
review1171003
issue3433
(transplanted from e9b490ba230159872772c6bd7c231c87e3ce11f6)
2013-10-20 15:51:29 +02:00

92 lines
3.0 KiB
Python

#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 itertools import groupby
from functools import partial
from trytond.pool import Pool, PoolMeta
__all__ = ['ShipmentIn', 'ShipmentOut']
__metaclass__ = PoolMeta
class ShipmentIn:
__name__ = 'stock.shipment.in'
def _group_parcel_key(self, lines, line):
"""
The key to group lines by parcel
"""
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
if self.carrier.carrier_cost_method != 'weight':
return context
context = context.copy()
weights = []
context['weights'] = weights
lines = self.incoming_moves or []
keyfunc = partial(self._group_parcel_key, lines)
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)
return context
class ShipmentOut:
__name__ = 'stock.shipment.out'
def _group_parcel_key(self, lines, line):
"""
The key to group lines by parcel
"""
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
if self.carrier.carrier_cost_method != 'weight':
return context
context = context.copy()
weights = []
context['weights'] = weights
lines = self.inventory_moves or []
keyfunc = partial(self._group_parcel_key, lines)
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)
return context