'Set Serial/Lot number in pack operations to mark the pack operation done.'

def set_pack_operation_lot(self, picking=None):

    StockProductionLot = self.env['stock.production.lot']
    PosPackOperationLot = self.env['pos.pack.operation.lot']
    for order in self:
        for pack_operation in (picking or self.picking_id).pack_operation_ids:
            qty = 0
            qty_done = 0
            pack_lots = []
            pos_pack_lots = PosPackOperationLot.search([('order_id', '=', order.id), ('product_id', '=', pack_operation.product_id.id)])
            pack_lot_names = [pos_pack.lot_name for pos_pack in pos_pack_lots]
            if pack_lot_names:
                for lot_name in list(set(pack_lot_names)):
                    stock_production_lot = StockProductionLot.search([('name', '=', lot_name), ('product_id', '=', pack_operation.product_id.id)])
                    if stock_production_lot:
                        if (stock_production_lot.product_id.tracking == 'lot'):
                            qty = pack_lot_names.count(lot_name)
                        else:
                            qty = 1.0
                        qty_done += qty
                        pack_lots.append({'lot_id': stock_production_lot.id, 'qty': qty})
            else:
                qty_done = pack_operation.product_qty
            pack_operation.write({'pack_lot_ids': map((lambda x: (0, 0, x)), pack_lots), 'qty_done': qty_done})
