Comment 9 for bug 519220

Revision history for this message
Akshay Jain (akshay-jain-7983) wrote :

Hey Jay,
I think there is some trouble in code of weight calculation. It does not take into account the uos coeff because the weight is ofcourse for default UOM and not UOS and therefore UOS coefficient should be used (it is used in weight calculation in sale module). I forgot to remind you about this but now since we r discussing the coefficients (in another thread) so please rectify the code for delivery module as well. Also it is much better to have each stock.move has a weight field that represents weight of the individual move in a picking and then the picking can just sum up the weights of the move. Also Stock picking view can be modified accordingly to show the weight of each move individually.

This is the code that i did in stock.py file of delivery module:

class stock_move(osv.osv):
    _name = "stock.move"
    _description = "Stock Move"
    _inherit = 'stock.move'

    def _cal_weight(self, cr, uid, ids, name, args, context=None):
 res = {}
 for move in self.browse(cr, uid, ids, context):
     weight = 0.00
     if move.product_id and move.product_uos_qty > 0.00 and move.product_id.weight > 0.00:
         weight += (move.product_id.weight * move.product_uos_qty / move.product_id.uos_coeff)
     res[move.id] = weight
 return res

    _columns = {
 'weight': fields.function(_cal_weight, method=True, type='float', string='Weight',digits=(16, int(tools.config['price_accuracy'])),
           store={
    'stock.move': (lambda self, cr, uid, ids, c={}: ids, ['product_id','product_uos_qty'], 10),
    }),
    }
stock_move()

# Overloaded stock_picking to manage carriers :
class stock_picking(osv.osv):
    _name = "stock.picking"
    _description = "Picking list"
    _inherit = 'stock.picking'

    def _cal_weight(self, cr, uid, ids, name, args, context=None):
        res = {}
        data_picking = self.browse(cr, uid, ids, context)
        for picking in data_picking:
            total_weight = 0.00
            if picking.move_lines:
                for move in picking.move_lines:
             if move.weight > 0.00:
                        total_weight += move.weight
            res[picking.id] = total_weight
        return res

    def _get_picking_line(self, cr, uid, ids, context=None):
        result = {}
        for line in self.pool.get('stock.move').browse(cr, uid, ids, context=context):
            result[line.picking_id.id] = True
        return result.keys()

    _columns = {
        'carrier_id':fields.many2one("delivery.carrier","Carrier"),
        'volume': fields.float('Volume'),
        'weight': fields.function(_cal_weight, method=True, type='float', string='Weight',digits=(16, int(tools.config['price_accuracy'])),
                  store={
                 'stock.picking': (lambda self, cr, uid, ids, c={}: ids, ['move_lines'], 20),
                 'stock.move': (_get_picking_line, ['weight'], 20),
                 }),
        }
.
.
.
<more code here>
stock_picking()

I hope this code also makes it to stable version :)