[IMP] When preparing pack operations, use the smallest UoM of the moves if they are...
[odoo/odoo.git] / addons / stock / stock.py
index bd15c45..2d49d26 100644 (file)
 #
 ##############################################################################
 
-from datetime import datetime
-from dateutil.relativedelta import relativedelta
+from datetime import date, datetime
+from dateutil import relativedelta
+import json
 import time
-from operator import itemgetter
-from itertools import groupby
 
-from openerp.osv import fields, osv, orm
+from openerp.osv import fields, osv
+from openerp.tools.float_utils import float_compare, float_round
 from openerp.tools.translate import _
-from openerp import netsvc
-from openerp import tools
-from openerp.tools import float_compare, DEFAULT_SERVER_DATETIME_FORMAT
+from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT, DEFAULT_SERVER_DATE_FORMAT
+from openerp import SUPERUSER_ID, api
 import openerp.addons.decimal_precision as dp
+from openerp.addons.procurement import procurement
 import logging
-_logger = logging.getLogger(__name__)
 
+
+_logger = logging.getLogger(__name__)
 #----------------------------------------------------------
 # Incoterms
 #----------------------------------------------------------
@@ -41,46 +42,30 @@ class stock_incoterms(osv.osv):
     _name = "stock.incoterms"
     _description = "Incoterms"
     _columns = {
-        'name': fields.char('Name', size=64, required=True, help="Incoterms are series of sales terms.They are used to divide transaction costs and responsibilities between buyer and seller and reflect state-of-the-art transportation practices."),
-        'code': fields.char('Code', size=3, required=True, help="Code for Incoterms"),
-        'active': fields.boolean('Active', help="By unchecking the active field, you may hide an INCOTERM without deleting it."),
+        'name': fields.char('Name', required=True, help="Incoterms are series of sales terms. They are used to divide transaction costs and responsibilities between buyer and seller and reflect state-of-the-art transportation practices."),
+        'code': fields.char('Code', size=3, required=True, help="Incoterm Standard Code"),
+        'active': fields.boolean('Active', help="By unchecking the active field, you may hide an INCOTERM you will not use."),
     }
     _defaults = {
         'active': True,
     }
 
-stock_incoterms()
-
-class stock_journal(osv.osv):
-    _name = "stock.journal"
-    _description = "Stock Journal"
-    _columns = {
-        'name': fields.char('Stock Journal', size=32, required=True),
-        'user_id': fields.many2one('res.users', 'Responsible'),
-    }
-    _defaults = {
-        'user_id': lambda s, c, u, ctx: u
-    }
-
-stock_journal()
-
 #----------------------------------------------------------
 # Stock Location
 #----------------------------------------------------------
+
 class stock_location(osv.osv):
     _name = "stock.location"
-    _description = "Location"
+    _description = "Inventory Locations"
     _parent_name = "location_id"
     _parent_store = True
-    _parent_order = 'posz,name'
+    _parent_order = 'name'
     _order = 'parent_left'
+    _rec_name = 'complete_name'
 
-    # TODO: implement name_search() in a way that matches the results of name_get!
-
-    def name_get(self, cr, uid, ids, context=None):
-        # always return the full hierarchical name
-        res = self._complete_name(cr, uid, ids, 'complete_name', None, context=context)
-        return res.items()
+    def _location_owner(self, cr, uid, location, context=None):
+        ''' Return the company owning the location if any '''
+        return location and (location.usage == 'internal') and location.company_id or False
 
     def _complete_name(self, cr, uid, ids, name, args, context=None):
         """ Forms complete name of location from parent location to child location.
@@ -88,523 +73,595 @@ class stock_location(osv.osv):
         """
         res = {}
         for m in self.browse(cr, uid, ids, context=context):
-            names = [m.name]
+            res[m.id] = m.name
             parent = m.location_id
             while parent:
-                names.append(parent.name)
+                res[m.id] = parent.name + ' / ' + res[m.id]
                 parent = parent.location_id
-            res[m.id] = ' / '.join(reversed(names))
         return res
 
     def _get_sublocations(self, cr, uid, ids, context=None):
         """ return all sublocations of the given stock locations (included) """
-        return self.search(cr, uid, [('id', 'child_of', ids)], context=context)
+        if context is None:
+            context = {}
+        context_with_inactive = context.copy()
+        context_with_inactive['active_test'] = False
+        return self.search(cr, uid, [('id', 'child_of', ids)], context=context_with_inactive)
 
-    def _product_value(self, cr, uid, ids, field_names, arg, context=None):
-        """Computes stock value (real and virtual) for a product, as well as stock qty (real and virtual).
-        @param field_names: Name of field
-        @return: Dictionary of values
-        """
-        prod_id = context and context.get('product_id', False)
+    def _name_get(self, cr, uid, location, context=None):
+        name = location.name
+        while location.location_id and location.usage != 'view':
+            location = location.location_id
+            name = location.name + '/' + name
+        return name
 
-        if not prod_id:
-            return dict([(i, {}.fromkeys(field_names, 0.0)) for i in ids])
-
-        product_product_obj = self.pool.get('product.product')
-
-        cr.execute('select distinct product_id, location_id from stock_move where location_id in %s', (tuple(ids), ))
-        dict1 = cr.dictfetchall()
-        cr.execute('select distinct product_id, location_dest_id as location_id from stock_move where location_dest_id in %s', (tuple(ids), ))
-        dict2 = cr.dictfetchall()
-        res_products_by_location = sorted(dict1+dict2, key=itemgetter('location_id'))
-        products_by_location = dict((k, [v['product_id'] for v in itr]) for k, itr in groupby(res_products_by_location, itemgetter('location_id')))
-
-        result = dict([(i, {}.fromkeys(field_names, 0.0)) for i in ids])
-        result.update(dict([(i, {}.fromkeys(field_names, 0.0)) for i in list(set([aaa['location_id'] for aaa in res_products_by_location]))]))
-
-        currency_id = self.pool.get('res.users').browse(cr, uid, uid).company_id.currency_id.id
-        currency_obj = self.pool.get('res.currency')
-        currency = currency_obj.browse(cr, uid, currency_id, context=context)
-        for loc_id, product_ids in products_by_location.items():
-            if prod_id:
-                product_ids = [prod_id]
-            c = (context or {}).copy()
-            c['location'] = loc_id
-            for prod in product_product_obj.browse(cr, uid, product_ids, context=c):
-                for f in field_names:
-                    if f == 'stock_real':
-                        if loc_id not in result:
-                            result[loc_id] = {}
-                        result[loc_id][f] += prod.qty_available
-                    elif f == 'stock_virtual':
-                        result[loc_id][f] += prod.virtual_available
-                    elif f == 'stock_real_value':
-                        amount = prod.qty_available * prod.standard_price
-                        amount = currency_obj.round(cr, uid, currency, amount)
-                        result[loc_id][f] += amount
-                    elif f == 'stock_virtual_value':
-                        amount = prod.virtual_available * prod.standard_price
-                        amount = currency_obj.round(cr, uid, currency, amount)
-                        result[loc_id][f] += amount
-        return result
+    def name_get(self, cr, uid, ids, context=None):
+        res = []
+        for location in self.browse(cr, uid, ids, context=context):
+            res.append((location.id, self._name_get(cr, uid, location, context=context)))
+        return res
 
     _columns = {
-        'name': fields.char('Location Name', size=64, required=True, translate=True),
+        'name': fields.char('Location Name', required=True, translate=True),
         'active': fields.boolean('Active', help="By unchecking the active field, you may hide a location without deleting it."),
-        'usage': fields.selection([('supplier', 'Supplier Location'), ('view', 'View'), ('internal', 'Internal Location'), ('customer', 'Customer Location'), ('inventory', 'Inventory'), ('procurement', 'Procurement'), ('production', 'Production'), ('transit', 'Transit Location for Inter-Companies Transfers')], 'Location Type', required=True,
-                 help="""* Supplier Location: Virtual location representing the source location for products coming from your suppliers
+        'usage': fields.selection([
+                        ('supplier', 'Supplier Location'),
+                        ('view', 'View'),
+                        ('internal', 'Internal Location'),
+                        ('customer', 'Customer Location'),
+                        ('inventory', 'Inventory'),
+                        ('procurement', 'Procurement'),
+                        ('production', 'Production'),
+                        ('transit', 'Transit Location')],
+                'Location Type', required=True,
+                help="""* Supplier Location: Virtual location representing the source location for products coming from your suppliers
                        \n* View: Virtual location used to create a hierarchical structures for your warehouse, aggregating its child locations ; can't directly contain products
                        \n* Internal Location: Physical locations inside your own warehouses,
                        \n* Customer Location: Virtual location representing the destination location for products sent to your customers
                        \n* Inventory: Virtual location serving as counterpart for inventory operations used to correct stock levels (Physical inventories)
                        \n* Procurement: Virtual location serving as temporary counterpart for procurement operations when the source (supplier or production) is not known yet. This location should be empty when the procurement scheduler has finished running.
                        \n* Production: Virtual counterpart location for production operations: this location consumes the raw material and produces finished products
-                      """, select = True),
-         # temporarily removed, as it's unused: 'allocation_method': fields.selection([('fifo', 'FIFO'), ('lifo', 'LIFO'), ('nearest', 'Nearest')], 'Allocation Method', required=True),
-        'complete_name': fields.function(_complete_name, type='char', size=256, string="Location Name",
-                            store={'stock.location': (_get_sublocations, ['name', 'location_id'], 10)}),
-
-        'stock_real': fields.function(_product_value, type='float', string='Real Stock', multi="stock"),
-        'stock_virtual': fields.function(_product_value, type='float', string='Virtual Stock', multi="stock"),
-
+                       \n* Transit Location: Counterpart location that should be used in inter-companies or inter-warehouses operations
+                      """, select=True),
+        'complete_name': fields.function(_complete_name, type='char', string="Location Name",
+                            store={'stock.location': (_get_sublocations, ['name', 'location_id', 'active'], 10)}),
         'location_id': fields.many2one('stock.location', 'Parent Location', select=True, ondelete='cascade'),
         'child_ids': fields.one2many('stock.location', 'location_id', 'Contains'),
 
-        'chained_journal_id': fields.many2one('stock.journal', 'Chaining Journal',help="Inventory Journal in which the chained move will be written, if the Chaining Type is not Transparent (no journal is used if left empty)"),
-        'chained_location_id': fields.many2one('stock.location', 'Chained Location If Fixed'),
-        'chained_location_type': fields.selection([('none', 'None'), ('customer', 'Customer'), ('fixed', 'Fixed Location')],
-            'Chained Location Type', required=True,
-            help="Determines whether this location is chained to another location, i.e. any incoming product in this location \n" \
-                "should next go to the chained location. The chained location is determined according to the type :"\
-                "\n* None: No chaining at all"\
-                "\n* Customer: The chained location will be taken from the Customer Location field on the Partner form of the Partner that is specified in the Picking list of the incoming products." \
-                "\n* Fixed Location: The chained location is taken from the next field: Chained Location if Fixed." \
-                ),
-        'chained_auto_packing': fields.selection(
-            [('auto', 'Automatic Move'), ('manual', 'Manual Operation'), ('transparent', 'Automatic No Step Added')],
-            'Chaining Type',
-            required=True,
-            help="This is used only if you select a chained location type.\n" \
-                "The 'Automatic Move' value will create a stock move after the current one that will be "\
-                "validated automatically. With 'Manual Operation', the stock move has to be validated "\
-                "by a worker. With 'Automatic No Step Added', the location is replaced in the original move."
-            ),
-        'chained_picking_type': fields.selection([('out', 'Sending Goods'), ('in', 'Getting Goods'), ('internal', 'Internal')], 'Shipping Type', help="Shipping Type of the Picking List that will contain the chained move (leave empty to automatically detect the type based on the source and destination locations)."),
-        'chained_company_id': fields.many2one('res.company', 'Chained Company', help='The company the Picking List containing the chained move will belong to (leave empty to use the default company determination rules'),
-        'chained_delay': fields.integer('Chaining Lead Time',help="Delay between original move and chained move in days"),
-        'partner_id': fields.many2one('res.partner', 'Location Address',help="Address of  customer or supplier."),
-        'icon': fields.selection(tools.icons, 'Icon', size=64,help="Icon show in  hierarchical tree view"),
+        'partner_id': fields.many2one('res.partner', 'Owner', help="Owner of the location if not internal"),
 
         'comment': fields.text('Additional Information'),
-        'posx': fields.integer('Corridor (X)',help="Optional localization details, for information purpose only"),
+        'posx': fields.integer('Corridor (X)', help="Optional localization details, for information purpose only"),
         'posy': fields.integer('Shelves (Y)', help="Optional localization details, for information purpose only"),
         'posz': fields.integer('Height (Z)', help="Optional localization details, for information purpose only"),
 
         'parent_left': fields.integer('Left Parent', select=1),
         'parent_right': fields.integer('Right Parent', select=1),
-        'stock_real_value': fields.function(_product_value, type='float', string='Real Stock Value', multi="stock", digits_compute=dp.get_precision('Account')),
-        'stock_virtual_value': fields.function(_product_value, type='float', string='Virtual Stock Value', multi="stock", digits_compute=dp.get_precision('Account')),
-        'company_id': fields.many2one('res.company', 'Company', select=1, help='Let this field empty if this location is shared between all companies'),
-        'scrap_location': fields.boolean('Scrap Location', help='Check this box to allow using this location to put scrapped/damaged goods.'),
-        'valuation_in_account_id': fields.many2one('account.account', 'Stock Valuation Account (Incoming)', domain = [('type','=','other')],
-                                                   help="Used for real-time inventory valuation. When set on a virtual location (non internal type), "
-                                                        "this account will be used to hold the value of products being moved from an internal location "
-                                                        "into this location, instead of the generic Stock Output Account set on the product. "
-                                                        "This has no effect for internal locations."),
-        'valuation_out_account_id': fields.many2one('account.account', 'Stock Valuation Account (Outgoing)', domain = [('type','=','other')],
-                                                   help="Used for real-time inventory valuation. When set on a virtual location (non internal type), "
-                                                        "this account will be used to hold the value of products being moved out of this location "
-                                                        "and into an internal location, instead of the generic Stock Output Account set on the product. "
-                                                        "This has no effect for internal locations."),
+
+        'company_id': fields.many2one('res.company', 'Company', select=1, help='Let this field empty if this location is shared between companies'),
+        'scrap_location': fields.boolean('Is a Scrap Location?', help='Check this box to allow using this location to put scrapped/damaged goods.'),
+        'removal_strategy_id': fields.many2one('product.removal', 'Removal Strategy', help="Defines the default method used for suggesting the exact location (shelf) where to take the products from, which lot etc. for this location. This method can be enforced at the product category level, and a fallback is made on the parent locations if none is set here."),
+        'putaway_strategy_id': fields.many2one('product.putaway', 'Put Away Strategy', help="Defines the default method used for suggesting the exact location (shelf) where to store the products. This method can be enforced at the product category level, and a fallback is made on the parent locations if none is set here."),
+        'loc_barcode': fields.char('Location Barcode'),
     }
     _defaults = {
         'active': True,
         'usage': 'internal',
-        'chained_location_type': 'none',
-        'chained_auto_packing': 'manual',
         'company_id': lambda self, cr, uid, c: self.pool.get('res.company')._company_default_get(cr, uid, 'stock.location', context=c),
         'posx': 0,
         'posy': 0,
         'posz': 0,
-        'icon': False,
         'scrap_location': False,
     }
-
-    def chained_location_get(self, cr, uid, location, partner=None, product=None, context=None):
-        """ Finds chained location
-        @param location: Location id
-        @param partner: Partner id
-        @param product: Product id
-        @return: List of values
+    _sql_constraints = [('loc_barcode_company_uniq', 'unique (loc_barcode,company_id)', 'The barcode for a location must be unique per company !')]
+
+    def create(self, cr, uid, default, context=None):
+        if not default.get('loc_barcode', False):
+            default.update({'loc_barcode': default.get('complete_name', False)})
+        return super(stock_location, self).create(cr, uid, default, context=context)
+
+    def get_putaway_strategy(self, cr, uid, location, product, context=None):
+        ''' Returns the location where the product has to be put, if any compliant putaway strategy is found. Otherwise returns None.'''
+        putaway_obj = self.pool.get('product.putaway')
+        loc = location
+        while loc:
+            if loc.putaway_strategy_id:
+                res = putaway_obj.putaway_apply(cr, uid, loc.putaway_strategy_id, product, context=context)
+                if res:
+                    return res
+            loc = loc.location_id
+
+    def _default_removal_strategy(self, cr, uid, context=None):
+        return 'fifo'
+
+    def get_removal_strategy(self, cr, uid, location, product, context=None):
+        ''' Returns the removal strategy to consider for the given product and location.
+            :param location: browse record (stock.location)
+            :param product: browse record (product.product)
+            :rtype: char
+        '''
+        if product.categ_id.removal_strategy_id:
+            return product.categ_id.removal_strategy_id.method
+        loc = location
+        while loc:
+            if loc.removal_strategy_id:
+                return loc.removal_strategy_id.method
+            loc = loc.location_id
+        return self._default_removal_strategy(cr, uid, context=context)
+
+
+    def get_warehouse(self, cr, uid, location, context=None):
         """
-        result = None
-        if location.chained_location_type == 'customer':
-            if partner:
-                result = partner.property_stock_customer
-            else:
-                loc_id = self.pool['res.partner'].default_get(cr, uid, ['property_stock_customer'], context=context)['property_stock_customer']
-                result = self.pool['stock.location'].browse(cr, uid, loc_id, context=context)
-        elif location.chained_location_type == 'fixed':
-            result = location.chained_location_id
-        if result:
-            return result, location.chained_auto_packing, location.chained_delay, location.chained_journal_id and location.chained_journal_id.id or False, location.chained_company_id and location.chained_company_id.id or False, location.chained_picking_type, False
-        return result
-
-    def picking_type_get(self, cr, uid, from_location, to_location, context=None):
-        """ Gets type of picking.
-        @param from_location: Source location
-        @param to_location: Destination location
-        @return: Location type
+            Returns warehouse id of warehouse that contains location
+            :param location: browse record (stock.location)
         """
-        result = 'internal'
-        if (from_location.usage=='internal') and (to_location and to_location.usage in ('customer', 'supplier')):
-            result = 'out'
-        elif (from_location.usage in ('supplier', 'customer')) and (to_location.usage == 'internal'):
-            result = 'in'
-        return result
+        wh_obj = self.pool.get("stock.warehouse")
+        whs = wh_obj.search(cr, uid, [('view_location_id.parent_left', '<=', location.parent_left), 
+                                ('view_location_id.parent_right', '>=', location.parent_left)], context=context)
+        return whs and whs[0] or False
 
-    def _product_get_all_report(self, cr, uid, ids, product_ids=False, context=None):
-        return self._product_get_report(cr, uid, ids, product_ids, context, recursive=True)
+#----------------------------------------------------------
+# Routes
+#----------------------------------------------------------
 
-    def _product_get_report(self, cr, uid, ids, product_ids=False,
-            context=None, recursive=False):
-        """ Finds the product quantity and price for particular location.
-        @param product_ids: Ids of product
-        @param recursive: True or False
-        @return: Dictionary of values
-        """
-        if context is None:
-            context = {}
-        product_obj = self.pool.get('product.product')
-        # Take the user company and pricetype
-        context['currency_id'] = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
+class stock_location_route(osv.osv):
+    _name = 'stock.location.route'
+    _description = "Inventory Routes"
+    _order = 'sequence'
 
-        # To be able to offer recursive or non-recursive reports we need to prevent recursive quantities by default
-        context['compute_child'] = False
+    _columns = {
+        'name': fields.char('Route Name', required=True),
+        'sequence': fields.integer('Sequence'),
+        'pull_ids': fields.one2many('procurement.rule', 'route_id', 'Pull Rules', copy=True),
+        'active': fields.boolean('Active', help="If the active field is set to False, it will allow you to hide the route without removing it."),
+        'push_ids': fields.one2many('stock.location.path', 'route_id', 'Push Rules', copy=True),
+        'product_selectable': fields.boolean('Applicable on Product'),
+        'product_categ_selectable': fields.boolean('Applicable on Product Category'),
+        'warehouse_selectable': fields.boolean('Applicable on Warehouse'),
+        'supplied_wh_id': fields.many2one('stock.warehouse', 'Supplied Warehouse'),
+        'supplier_wh_id': fields.many2one('stock.warehouse', 'Supplier Warehouse'),
+        'company_id': fields.many2one('res.company', 'Company', select=1, help='Let this field empty if this route is shared between all companies'),
+    }
 
-        if not product_ids:
-            product_ids = product_obj.search(cr, uid, [], context={'active_test': False})
+    _defaults = {
+        'sequence': lambda self, cr, uid, ctx: 0,
+        'active': True,
+        'product_selectable': True,
+        'company_id': lambda self, cr, uid, c: self.pool.get('res.company')._company_default_get(cr, uid, 'stock.location.route', context=c),
+    }
 
-        products = product_obj.browse(cr, uid, product_ids, context=context)
-        products_by_uom = {}
-        products_by_id = {}
-        for product in products:
-            products_by_uom.setdefault(product.uom_id.id, [])
-            products_by_uom[product.uom_id.id].append(product)
-            products_by_id.setdefault(product.id, [])
-            products_by_id[product.id] = product
+    def write(self, cr, uid, ids, vals, context=None):
+        '''when a route is deactivated, deactivate also its pull and push rules'''
+        if isinstance(ids, (int, long)):
+            ids = [ids]
+        res = super(stock_location_route, self).write(cr, uid, ids, vals, context=context)
+        if 'active' in vals:
+            push_ids = []
+            pull_ids = []
+            for route in self.browse(cr, uid, ids, context=context):
+                if route.push_ids:
+                    push_ids += [r.id for r in route.push_ids if r.active != vals['active']]
+                if route.pull_ids:
+                    pull_ids += [r.id for r in route.pull_ids if r.active != vals['active']]
+            if push_ids:
+                self.pool.get('stock.location.path').write(cr, uid, push_ids, {'active': vals['active']}, context=context)
+            if pull_ids:
+                self.pool.get('procurement.rule').write(cr, uid, pull_ids, {'active': vals['active']}, context=context)
+        return res
 
-        result = {}
-        result['product'] = []
-        for id in ids:
-            quantity_total = 0.0
-            total_price = 0.0
-            for uom_id in products_by_uom.keys():
-                fnc = self._product_get
-                if recursive:
-                    fnc = self._product_all_get
-                ctx = context.copy()
-                ctx['uom'] = uom_id
-                qty = fnc(cr, uid, id, [x.id for x in products_by_uom[uom_id]],
-                        context=ctx)
-                for product_id in qty.keys():
-                    if not qty[product_id]:
-                        continue
-                    product = products_by_id[product_id]
-                    quantity_total += qty[product_id]
-
-                    # Compute based on pricetype
-                    # Choose the right filed standard_price to read
-                    amount_unit = product.price_get('standard_price', context=context)[product.id]
-                    price = qty[product_id] * amount_unit
-
-                    total_price += price
-                    result['product'].append({
-                        'price': amount_unit,
-                        'prod_name': product.name,
-                        'code': product.default_code, # used by lot_overview_all report!
-                        'variants': product.variants or '',
-                        'uom': product.uom_id.name,
-                        'prod_qty': qty[product_id],
-                        'price_value': price,
-                    })
-        result['total'] = quantity_total
-        result['total_price'] = total_price
-        return result
+#----------------------------------------------------------
+# Quants
+#----------------------------------------------------------
 
-    def _product_get_multi_location(self, cr, uid, ids, product_ids=False, context=None,
-                                    states=['done'], what=('in', 'out')):
-        """
-        @param product_ids: Ids of product
-        @param states: List of states
-        @param what: Tuple of
-        @return:
-        """
-        product_obj = self.pool.get('product.product')
-        if context is None:
-            context = {}
-        context.update({
-            'states': states,
-            'what': what,
-            'location': ids
-        })
-        return product_obj.get_product_available(cr, uid, product_ids, context=context)
+class stock_quant(osv.osv):
+    """
+    Quants are the smallest unit of stock physical instances
+    """
+    _name = "stock.quant"
+    _description = "Quants"
 
-    def _product_get(self, cr, uid, id, product_ids=False, context=None, states=None):
-        """
-        @param product_ids:
-        @param states:
-        @return:
-        """
-        if states is None:
-            states = ['done']
-        ids = id and [id] or []
-        return self._product_get_multi_location(cr, uid, ids, product_ids, context=context, states=states)
-
-    def _product_all_get(self, cr, uid, id, product_ids=False, context=None, states=None):
-        if states is None:
-            states = ['done']
-        # build the list of ids of children of the location given by id
-        ids = id and [id] or []
-        location_ids = self.search(cr, uid, [('location_id', 'child_of', ids)])
-        return self._product_get_multi_location(cr, uid, location_ids, product_ids, context, states)
-
-    def _product_virtual_get(self, cr, uid, id, product_ids=False, context=None, states=None):
-        if states is None:
-            states = ['done']
-        return self._product_all_get(cr, uid, id, product_ids, context, ['confirmed', 'waiting', 'assigned', 'done'])
-
-    def _product_reserve(self, cr, uid, ids, product_id, product_qty, context=None, lock=False):
-        """
-        Attempt to find a quantity ``product_qty`` (in the product's default uom or the uom passed in ``context``) of product ``product_id``
-        in locations with id ``ids`` and their child locations. If ``lock`` is True, the stock.move lines
-        of product with id ``product_id`` in the searched location will be write-locked using Postgres's
-        "FOR UPDATE NOWAIT" option until the transaction is committed or rolled back, to prevent reservin
-        twice the same products.
-        If ``lock`` is True and the lock cannot be obtained (because another transaction has locked some of
-        the same stock.move lines), a log line will be output and False will be returned, as if there was
-        not enough stock.
-
-        :param product_id: Id of product to reserve
-        :param product_qty: Quantity of product to reserve (in the product's default uom or the uom passed in ``context``)
-        :param lock: if True, the stock.move lines of product with id ``product_id`` in all locations (and children locations) with ``ids`` will
-                     be write-locked using postgres's "FOR UPDATE NOWAIT" option until the transaction is committed or rolled back. This is
-                     to prevent reserving twice the same products.
-        :param context: optional context dictionary: if a 'uom' key is present it will be used instead of the default product uom to
-                        compute the ``product_qty`` and in the return value.
-        :return: List of tuples in the form (qty, location_id) with the (partial) quantities that can be taken in each location to
-                 reach the requested product_qty (``qty`` is expressed in the default uom of the product), of False if enough
-                 products could not be found, or the lock could not be obtained (and ``lock`` was True).
+    def _get_quant_name(self, cr, uid, ids, name, args, context=None):
+        """ Forms complete name of location from parent location to child location.
+        @return: Dictionary of values
         """
-        result = []
-        amount = 0.0
-        if context is None:
-            context = {}
-        uom_obj = self.pool.get('product.uom')
-        uom_rounding = self.pool.get('product.product').browse(cr, uid, product_id, context=context).uom_id.rounding
-        if context.get('uom'):
-            uom_rounding = uom_obj.browse(cr, uid, context.get('uom'), context=context).rounding
-
-        locations_ids = self.search(cr, uid, [('location_id', 'child_of', ids)])
-        if locations_ids:
-            # Fetch only the locations in which this product has ever been processed (in or out)
-            cr.execute("""SELECT l.id FROM stock_location l WHERE l.id in %s AND
-                        EXISTS (SELECT 1 FROM stock_move m WHERE m.product_id = %s
-                                AND ((state = 'done' AND m.location_dest_id = l.id)
-                                    OR (state in ('done','assigned') AND m.location_id = l.id)))
-                       """, (tuple(locations_ids), product_id,))
-            locations_ids = [i for (i,) in cr.fetchall()]
-        for id in locations_ids:
-            if lock:
-                try:
-                    # Must lock with a separate select query because FOR UPDATE can't be used with
-                    # aggregation/group by's (when individual rows aren't identifiable).
-                    # We use a SAVEPOINT to be able to rollback this part of the transaction without
-                    # failing the whole transaction in case the LOCK cannot be acquired.
-                    cr.execute("SAVEPOINT stock_location_product_reserve")
-                    cr.execute("""SELECT id FROM stock_move
-                                  WHERE product_id=%s AND
-                                          (
-                                            (location_dest_id=%s AND
-                                             location_id<>%s AND
-                                             state='done')
-                                            OR
-                                            (location_id=%s AND
-                                             location_dest_id<>%s AND
-                                             state in ('done', 'assigned'))
-                                          )
-                                  FOR UPDATE of stock_move NOWAIT""", (product_id, id, id, id, id), log_exceptions=False)
-                except Exception:
-                    # Here it's likely that the FOR UPDATE NOWAIT failed to get the LOCK,
-                    # so we ROLLBACK to the SAVEPOINT to restore the transaction to its earlier
-                    # state, we return False as if the products were not available, and log it:
-                    cr.execute("ROLLBACK TO stock_location_product_reserve")
-                    _logger.warning("Failed attempt to reserve %s x product %s, likely due to another transaction already in progress. Next attempt is likely to work. Detailed error available at DEBUG level.", product_qty, product_id)
-                    _logger.debug("Trace of the failed product reservation attempt: ", exc_info=True)
-                    return False
-
-            # XXX TODO: rewrite this with one single query, possibly even the quantity conversion
-            cr.execute("""SELECT product_uom, sum(product_qty) AS product_qty
-                          FROM stock_move
-                          WHERE location_dest_id=%s AND
-                                location_id<>%s AND
-                                product_id=%s AND
-                                state='done'
-                          GROUP BY product_uom
-                       """,
-                       (id, id, product_id))
-            results = cr.dictfetchall()
-            cr.execute("""SELECT product_uom,-sum(product_qty) AS product_qty
-                          FROM stock_move
-                          WHERE location_id=%s AND
-                                location_dest_id<>%s AND
-                                product_id=%s AND
-                                state in ('done', 'assigned')
-                          GROUP BY product_uom
-                       """,
-                       (id, id, product_id))
-            results += cr.dictfetchall()
-            total = 0.0
-            results2 = 0.0
-            for r in results:
-                amount = uom_obj._compute_qty(cr, uid, r['product_uom'], r['product_qty'], context.get('uom', False))
-                results2 += amount
-                total += amount
-            if total <= 0.0:
-                continue
-
-            amount = results2
-            compare_qty = float_compare(amount, 0, precision_rounding=uom_rounding)
-            if compare_qty == 1:
-                if amount > min(total, product_qty):
-                    amount = min(product_qty, total)
-                result.append((amount, id))
-                product_qty -= amount
-                total -= amount
-                if product_qty <= 0.0:
-                    return result
-                if total <= 0.0:
-                    continue
-        return False
-
-stock_location()
-
+        res = {}
+        for q in self.browse(cr, uid, ids, context=context):
 
-class stock_tracking(osv.osv):
-    _name = "stock.tracking"
-    _description = "Packs"
+            res[q.id] = q.product_id.code or ''
+            if q.lot_id:
+                res[q.id] = q.lot_id.name
+            res[q.id] += ': ' + str(q.qty) + q.product_id.uom_id.name
+        return res
 
-    def checksum(sscc):
-        salt = '31' * 8 + '3'
-        sum = 0
-        for sscc_part, salt_part in zip(sscc, salt):
-            sum += int(sscc_part) * int(salt_part)
-        return (10 - (sum % 10)) % 10
-    checksum = staticmethod(checksum)
+    def _calc_inventory_value(self, cr, uid, ids, name, attr, context=None):
+        context = dict(context or {})
+        res = {}
+        uid_company_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.id
+        for quant in self.browse(cr, uid, ids, context=context):
+            context.pop('force_company', None)
+            if quant.company_id.id != uid_company_id:
+                #if the company of the quant is different than the current user company, force the company in the context
+                #then re-do a browse to read the property fields for the good company.
+                context['force_company'] = quant.company_id.id
+                quant = self.browse(cr, uid, quant.id, context=context)
+            res[quant.id] = self._get_inventory_value(cr, uid, quant, context=context)
+        return res
 
-    def make_sscc(self, cr, uid, context=None):
-        sequence = self.pool.get('ir.sequence').get(cr, uid, 'stock.lot.tracking')
-        try:
-            return sequence + str(self.checksum(sequence))
-        except Exception:
-            return sequence
+    def _get_inventory_value(self, cr, uid, quant, context=None):
+        return quant.product_id.standard_price * quant.qty
 
     _columns = {
-        'name': fields.char('Pack Reference', size=64, required=True, select=True, help="By default, the pack reference is generated following the sscc standard. (Serial number + 1 check digit)"),
-        'active': fields.boolean('Active', help="By unchecking the active field, you may hide a pack without deleting it."),
-        'serial': fields.char('Additional Reference', size=64, select=True, help="Other reference or serial number"),
-        'move_ids': fields.one2many('stock.move', 'tracking_id', 'Moves for this pack', readonly=True),
-        'date': fields.datetime('Creation Date', required=True),
+        'name': fields.function(_get_quant_name, type='char', string='Identifier'),
+        'product_id': fields.many2one('product.product', 'Product', required=True, ondelete="restrict", readonly=True, select=True),
+        'location_id': fields.many2one('stock.location', 'Location', required=True, ondelete="restrict", readonly=True, select=True),
+        'qty': fields.float('Quantity', required=True, help="Quantity of products in this quant, in the default unit of measure of the product", readonly=True, select=True),
+        'package_id': fields.many2one('stock.quant.package', string='Package', help="The package containing this quant", readonly=True, select=True),
+        'packaging_type_id': fields.related('package_id', 'packaging_id', type='many2one', relation='product.packaging', string='Type of packaging', readonly=True, store=True),
+        'reservation_id': fields.many2one('stock.move', 'Reserved for Move', help="The move the quant is reserved for", readonly=True, select=True),
+        'lot_id': fields.many2one('stock.production.lot', 'Lot', readonly=True, select=True),
+        'cost': fields.float('Unit Cost'),
+        'owner_id': fields.many2one('res.partner', 'Owner', help="This is the owner of the quant", readonly=True, select=True),
+
+        'create_date': fields.datetime('Creation Date', readonly=True),
+        'in_date': fields.datetime('Incoming Date', readonly=True, select=True),
+
+        'history_ids': fields.many2many('stock.move', 'stock_quant_move_rel', 'quant_id', 'move_id', 'Moves', help='Moves that operate(d) on this quant'),
+        'company_id': fields.many2one('res.company', 'Company', help="The company to which the quants belong", required=True, readonly=True, select=True),
+        'inventory_value': fields.function(_calc_inventory_value, string="Inventory Value", type='float', readonly=True),
+
+        # Used for negative quants to reconcile after compensated by a new positive one
+        'propagated_from_id': fields.many2one('stock.quant', 'Linked Quant', help='The negative quant this is coming from', readonly=True, select=True),
+        'negative_move_id': fields.many2one('stock.move', 'Move Negative Quant', help='If this is a negative quant, this will be the move that caused this negative quant.', readonly=True),
+        'negative_dest_location_id': fields.related('negative_move_id', 'location_dest_id', type='many2one', relation='stock.location', string="Negative Destination Location", readonly=True, 
+                                                    help="Technical field used to record the destination location of a move that created a negative quant"),
     }
+
     _defaults = {
-        'active': 1,
-        'name': make_sscc,
-        'date': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
+        'company_id': lambda self, cr, uid, c: self.pool.get('res.company')._company_default_get(cr, uid, 'stock.quant', context=c),
     }
 
-    def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=100):
-        if not args:
-            args = []
-        ids = self.search(cr, user, [('serial', '=', name)]+ args, limit=limit, context=context)
-        ids += self.search(cr, user, [('name', operator, name)]+ args, limit=limit, context=context)
-        return self.name_get(cr, user, ids, context)
-
-    def name_get(self, cr, uid, ids, context=None):
-        """Append the serial to the name"""
-        if not len(ids):
-            return []
-        res = [ (r['id'], r['serial'] and '%s [%s]' % (r['name'], r['serial'])
-                                      or r['name'] )
-                for r in self.read(cr, uid, ids, ['name', 'serial'],
-                                   context=context) ]
+    def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None, orderby=False, lazy=True):
+        ''' Overwrite the read_group in order to sum the function field 'inventory_value' in group by'''
+        res = super(stock_quant, self).read_group(cr, uid, domain, fields, groupby, offset=offset, limit=limit, context=context, orderby=orderby, lazy=lazy)
+        if 'inventory_value' in fields:
+            for line in res:
+                if '__domain' in line:
+                    lines = self.search(cr, uid, line['__domain'], context=context)
+                    inv_value = 0.0
+                    for line2 in self.browse(cr, uid, lines, context=context):
+                        inv_value += line2.inventory_value
+                    line['inventory_value'] = inv_value
         return res
 
-    def unlink(self, cr, uid, ids, context=None):
-        raise osv.except_osv(_('Error!'), _('You cannot remove a lot line.'))
+    def action_view_quant_history(self, cr, uid, ids, context=None):
+        '''
+        This function returns an action that display the history of the quant, which
+        mean all the stock moves that lead to this quant creation with this quant quantity.
+        '''
+        mod_obj = self.pool.get('ir.model.data')
+        act_obj = self.pool.get('ir.actions.act_window')
 
-    def action_traceability(self, cr, uid, ids, context=None):
-        """ It traces the information of a product
-        @param self: The object pointer.
-        @param cr: A database cursor
-        @param uid: ID of the user currently logged in
-        @param ids: List of IDs selected
-        @param context: A standard dictionary
-        @return: A dictionary of values
+        result = mod_obj.get_object_reference(cr, uid, 'stock', 'action_move_form2')
+        id = result and result[1] or False
+        result = act_obj.read(cr, uid, [id], context={})[0]
+
+        move_ids = []
+        for quant in self.browse(cr, uid, ids, context=context):
+            move_ids += [move.id for move in quant.history_ids]
+
+        result['domain'] = "[('id','in',[" + ','.join(map(str, move_ids)) + "])]"
+        return result
+
+    def quants_reserve(self, cr, uid, quants, move, link=False, context=None):
+        '''This function reserves quants for the given move (and optionally given link). If the total of quantity reserved is enough, the move's state
+        is also set to 'assigned'
+
+        :param quants: list of tuple(quant browse record or None, qty to reserve). If None is given as first tuple element, the item will be ignored. Negative quants should not be received as argument
+        :param move: browse record
+        :param link: browse record (stock.move.operation.link)
+        '''
+        toreserve = []
+        reserved_availability = move.reserved_availability
+        #split quants if needed
+        for quant, qty in quants:
+            if qty <= 0.0 or (quant and quant.qty <= 0.0):
+                raise osv.except_osv(_('Error!'), _('You can not reserve a negative quantity or a negative quant.'))
+            if not quant:
+                continue
+            self._quant_split(cr, uid, quant, qty, context=context)
+            toreserve.append(quant.id)
+            reserved_availability += quant.qty
+        #reserve quants
+        if toreserve:
+            self.write(cr, SUPERUSER_ID, toreserve, {'reservation_id': move.id}, context=context)
+            #if move has a picking_id, write on that picking that pack_operation might have changed and need to be recomputed
+            if move.picking_id:
+                self.pool.get('stock.picking').write(cr, uid, [move.picking_id.id], {'recompute_pack_op': True}, context=context)
+        #check if move'state needs to be set as 'assigned'
+        rounding = move.product_id.uom_id.rounding
+        if float_compare(reserved_availability, move.product_qty, precision_rounding=rounding) == 0 and move.state in ('confirmed', 'waiting')  :
+            self.pool.get('stock.move').write(cr, uid, [move.id], {'state': 'assigned'}, context=context)
+        elif float_compare(reserved_availability, 0, precision_rounding=rounding) > 0 and not move.partially_available:
+            self.pool.get('stock.move').write(cr, uid, [move.id], {'partially_available': True}, context=context)
+
+    def quants_move(self, cr, uid, quants, move, location_to, location_from=False, lot_id=False, owner_id=False, src_package_id=False, dest_package_id=False, context=None):
+        """Moves all given stock.quant in the given destination location.  Unreserve from current move.
+        :param quants: list of tuple(browse record(stock.quant) or None, quantity to move)
+        :param move: browse record (stock.move)
+        :param location_to: browse record (stock.location) depicting where the quants have to be moved
+        :param location_from: optional browse record (stock.location) explaining where the quant has to be taken (may differ from the move source location in case a removal strategy applied). This parameter is only used to pass to _quant_create if a negative quant must be created
+        :param lot_id: ID of the lot that must be set on the quants to move
+        :param owner_id: ID of the partner that must own the quants to move
+        :param src_package_id: ID of the package that contains the quants to move
+        :param dest_package_id: ID of the package that must be set on the moved quant
+        """
+        quants_reconcile = []
+        to_move_quants = []
+        self._check_location(cr, uid, location_to, context=context)
+        for quant, qty in quants:
+            if not quant:
+                #If quant is None, we will create a quant to move (and potentially a negative counterpart too)
+                quant = self._quant_create(cr, uid, qty, move, lot_id=lot_id, owner_id=owner_id, src_package_id=src_package_id, dest_package_id=dest_package_id, force_location_from=location_from, force_location_to=location_to, context=context)
+            else:
+                self._quant_split(cr, uid, quant, qty, context=context)
+                quant.refresh()
+                to_move_quants.append(quant)
+            quants_reconcile.append(quant)
+        if to_move_quants:
+            to_recompute_move_ids = [x.reservation_id.id for x in to_move_quants if x.reservation_id and x.reservation_id.id != move.id]
+            self.move_quants_write(cr, uid, to_move_quants, move, location_to, dest_package_id, context=context)
+            self.pool.get('stock.move').recalculate_move_state(cr, uid, to_recompute_move_ids, context=context)
+        if location_to.usage == 'internal':
+            if self.search(cr, uid, [('product_id', '=', move.product_id.id), ('qty','<', 0)], limit=1, context=context):
+                for quant in quants_reconcile:
+                    quant.refresh()
+                    self._quant_reconcile_negative(cr, uid, quant, move, context=context)
+
+    def move_quants_write(self, cr, uid, quants, move, location_dest_id, dest_package_id, context=None):
+        vals = {'location_id': location_dest_id.id,
+                'history_ids': [(4, move.id)],
+                'package_id': dest_package_id,
+                'reservation_id': False}
+        self.write(cr, SUPERUSER_ID, [q.id for q in quants], vals, context=context)
+
+    def quants_get_prefered_domain(self, cr, uid, location, product, qty, domain=None, prefered_domain_list=[], restrict_lot_id=False, restrict_partner_id=False, context=None):
+        ''' This function tries to find quants in the given location for the given domain, by trying to first limit
+            the choice on the quants that match the first item of prefered_domain_list as well. But if the qty requested is not reached
+            it tries to find the remaining quantity by looping on the prefered_domain_list (tries with the second item and so on).
+            Make sure the quants aren't found twice => all the domains of prefered_domain_list should be orthogonal
+        '''
+        if domain is None:
+            domain = []
+        quants = [(None, qty)]
+        #don't look for quants in location that are of type production, supplier or inventory.
+        if location.usage in ['inventory', 'production', 'supplier']:
+            return quants
+        res_qty = qty
+        if not prefered_domain_list:
+            return self.quants_get(cr, uid, location, product, qty, domain=domain, restrict_lot_id=restrict_lot_id, restrict_partner_id=restrict_partner_id, context=context)
+        for prefered_domain in prefered_domain_list:
+            res_qty_cmp = float_compare(res_qty, 0, precision_rounding=product.uom_id.rounding)
+            if res_qty_cmp > 0:
+                #try to replace the last tuple (None, res_qty) with something that wasn't chosen at first because of the prefered order
+                quants.pop()
+                tmp_quants = self.quants_get(cr, uid, location, product, res_qty, domain=domain + prefered_domain, restrict_lot_id=restrict_lot_id, restrict_partner_id=restrict_partner_id, context=context)
+                for quant in tmp_quants:
+                    if quant[0]:
+                        res_qty -= quant[1]
+                quants += tmp_quants
+        return quants
+
+    def quants_get(self, cr, uid, location, product, qty, domain=None, restrict_lot_id=False, restrict_partner_id=False, context=None):
+        """
+        Use the removal strategies of product to search for the correct quants
+        If you inherit, put the super at the end of your method.
+
+        :location: browse record of the parent location where the quants have to be found
+        :product: browse record of the product to find
+        :qty in UoM of product
+        """
+        result = []
+        domain = domain or [('qty', '>', 0.0)]
+        if restrict_partner_id:
+            domain += [('owner_id', '=', restrict_partner_id)]
+        if restrict_lot_id:
+            domain += [('lot_id', '=', restrict_lot_id)]
+        if location:
+            removal_strategy = self.pool.get('stock.location').get_removal_strategy(cr, uid, location, product, context=context)
+            result += self.apply_removal_strategy(cr, uid, location, product, qty, domain, removal_strategy, context=context)
+        return result
+
+    def apply_removal_strategy(self, cr, uid, location, product, quantity, domain, removal_strategy, context=None):
+        if removal_strategy == 'fifo':
+            order = 'in_date, id'
+            return self._quants_get_order(cr, uid, location, product, quantity, domain, order, context=context)
+        elif removal_strategy == 'lifo':
+            order = 'in_date desc, id desc'
+            return self._quants_get_order(cr, uid, location, product, quantity, domain, order, context=context)
+        raise osv.except_osv(_('Error!'), _('Removal strategy %s not implemented.' % (removal_strategy,)))
+
+    def _quant_create(self, cr, uid, qty, move, lot_id=False, owner_id=False, src_package_id=False, dest_package_id=False,
+                      force_location_from=False, force_location_to=False, context=None):
+        '''Create a quant in the destination location and create a negative quant in the source location if it's an internal location.
+        '''
+        if context is None:
+            context = {}
+        price_unit = self.pool.get('stock.move').get_price_unit(cr, uid, move, context=context)
+        location = force_location_to or move.location_dest_id
+        rounding = move.product_id.uom_id.rounding
+        vals = {
+            'product_id': move.product_id.id,
+            'location_id': location.id,
+            'qty': float_round(qty, precision_rounding=rounding),
+            'cost': price_unit,
+            'history_ids': [(4, move.id)],
+            'in_date': datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT),
+            'company_id': move.company_id.id,
+            'lot_id': lot_id,
+            'owner_id': owner_id,
+            'package_id': dest_package_id,
+        }
+
+        if move.location_id.usage == 'internal':
+            #if we were trying to move something from an internal location and reach here (quant creation),
+            #it means that a negative quant has to be created as well.
+            negative_vals = vals.copy()
+            negative_vals['location_id'] = force_location_from and force_location_from.id or move.location_id.id
+            negative_vals['qty'] = float_round(-qty, precision_rounding=rounding)
+            negative_vals['cost'] = price_unit
+            negative_vals['negative_move_id'] = move.id
+            negative_vals['package_id'] = src_package_id
+            negative_quant_id = self.create(cr, SUPERUSER_ID, negative_vals, context=context)
+            vals.update({'propagated_from_id': negative_quant_id})
+
+        #create the quant as superuser, because we want to restrict the creation of quant manually: we should always use this method to create quants
+        quant_id = self.create(cr, SUPERUSER_ID, vals, context=context)
+        return self.browse(cr, uid, quant_id, context=context)
+
+    def _quant_split(self, cr, uid, quant, qty, context=None):
+        context = context or {}
+        rounding = quant.product_id.uom_id.rounding
+        if float_compare(abs(quant.qty), abs(qty), precision_rounding=rounding) <= 0: # if quant <= qty in abs, take it entirely
+            return False
+        qty_round = float_round(qty, precision_rounding=rounding)
+        new_qty_round = float_round(quant.qty - qty, precision_rounding=rounding)
+        new_quant = self.copy(cr, SUPERUSER_ID, quant.id, default={'qty': new_qty_round, 'history_ids': [(4, x.id) for x in quant.history_ids]}, context=context)
+        self.write(cr, SUPERUSER_ID, quant.id, {'qty': qty_round}, context=context)
+        quant.refresh()
+        return self.browse(cr, uid, new_quant, context=context)
+
+    def _get_latest_move(self, cr, uid, quant, context=None):
+        move = False
+        for m in quant.history_ids:
+            if not move or m.date > move.date:
+                move = m
+        return move
+
+    @api.cr_uid_ids_context
+    def _quants_merge(self, cr, uid, solved_quant_ids, solving_quant, context=None):
+        path = []
+        for move in solving_quant.history_ids:
+            path.append((4, move.id))
+        self.write(cr, SUPERUSER_ID, solved_quant_ids, {'history_ids': path}, context=context)
+
+    def _quant_reconcile_negative(self, cr, uid, quant, move, context=None):
+        """
+            When new quant arrive in a location, try to reconcile it with
+            negative quants. If it's possible, apply the cost of the new
+            quant to the conter-part of the negative quant.
         """
-        return self.pool.get('action.traceability').action_traceability(cr,uid,ids,context)
+        solving_quant = quant
+        dom = [('qty', '<', 0)]
+        if quant.lot_id:
+            dom += [('lot_id', '=', quant.lot_id.id)]
+        dom += [('owner_id', '=', quant.owner_id.id)]
+        dom += [('package_id', '=', quant.package_id.id)]
+        dom += [('id', '!=', quant.propagated_from_id.id)]
+        quants = self.quants_get(cr, uid, quant.location_id, quant.product_id, quant.qty, dom, context=context)
+        product_uom_rounding = quant.product_id.uom_id.rounding
+        for quant_neg, qty in quants:
+            if not quant_neg or not solving_quant:
+                continue
+            to_solve_quant_ids = self.search(cr, uid, [('propagated_from_id', '=', quant_neg.id)], context=context)
+            if not to_solve_quant_ids:
+                continue
+            solving_qty = qty
+            solved_quant_ids = []
+            for to_solve_quant in self.browse(cr, uid, to_solve_quant_ids, context=context):
+                if float_compare(solving_qty, 0, precision_rounding=product_uom_rounding) <= 0:
+                    continue
+                solved_quant_ids.append(to_solve_quant.id)
+                self._quant_split(cr, uid, to_solve_quant, min(solving_qty, to_solve_quant.qty), context=context)
+                solving_qty -= min(solving_qty, to_solve_quant.qty)
+            remaining_solving_quant = self._quant_split(cr, uid, solving_quant, qty, context=context)
+            remaining_neg_quant = self._quant_split(cr, uid, quant_neg, -qty, context=context)
+            #if the reconciliation was not complete, we need to link together the remaining parts
+            if remaining_neg_quant:
+                remaining_to_solve_quant_ids = self.search(cr, uid, [('propagated_from_id', '=', quant_neg.id), ('id', 'not in', solved_quant_ids)], context=context)
+                if remaining_to_solve_quant_ids:
+                    self.write(cr, SUPERUSER_ID, remaining_to_solve_quant_ids, {'propagated_from_id': remaining_neg_quant.id}, context=context)
+            if solving_quant.propagated_from_id and solved_quant_ids:
+                self.write(cr, uid, solved_quant_ids, {'propagated_from_id': solving_quant.propagated_from_id.id}, context=context)
+            #delete the reconciled quants, as it is replaced by the solved quants
+            self.unlink(cr, SUPERUSER_ID, [quant_neg.id], context=context)
+            if solved_quant_ids:
+                #price update + accounting entries adjustments
+                self._price_update(cr, uid, solved_quant_ids, solving_quant.cost, context=context)
+                #merge history (and cost?)
+                self._quants_merge(cr, uid, solved_quant_ids, solving_quant, context=context)
+            self.unlink(cr, SUPERUSER_ID, [solving_quant.id], context=context)
+            solving_quant = remaining_solving_quant
+
+    def _price_update(self, cr, uid, ids, newprice, context=None):
+        self.write(cr, SUPERUSER_ID, ids, {'cost': newprice}, context=context)
+
+    def quants_unreserve(self, cr, uid, move, context=None):
+        related_quants = [x.id for x in move.reserved_quant_ids]
+        if related_quants:
+            #if move has a picking_id, write on that picking that pack_operation might have changed and need to be recomputed
+            if move.picking_id:
+                self.pool.get('stock.picking').write(cr, uid, [move.picking_id.id], {'recompute_pack_op': True}, context=context)
+            if move.partially_available:
+                self.pool.get("stock.move").write(cr, uid, [move.id], {'partially_available': False}, context=context)
+            self.write(cr, SUPERUSER_ID, related_quants, {'reservation_id': False}, context=context)
+
+    def _quants_get_order(self, cr, uid, location, product, quantity, domain=[], orderby='in_date', context=None):
+        ''' Implementation of removal strategies
+            If it can not reserve, it will return a tuple (None, qty)
+        '''
+        if context is None:
+            context = {}
+        domain += location and [('location_id', 'child_of', location.id)] or []
+        domain += [('product_id', '=', product.id)]
+        if context.get('force_company'):
+            domain += [('company_id', '=', context.get('force_company'))]
+        else:
+            domain += [('company_id', '=', self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.id)]
+        res = []
+        offset = 0
+        while float_compare(quantity, 0, precision_rounding=product.uom_id.rounding) > 0:
+            quants = self.search(cr, uid, domain, order=orderby, limit=10, offset=offset, context=context)
+            if not quants:
+                res.append((None, quantity))
+                break
+            for quant in self.browse(cr, uid, quants, context=context):
+                rounding = product.uom_id.rounding
+                if float_compare(quantity, abs(quant.qty), precision_rounding=rounding) >= 0:
+                    res += [(quant, abs(quant.qty))]
+                    quantity -= abs(quant.qty)
+                elif float_compare(quantity, 0.0, precision_rounding=rounding) != 0:
+                    res += [(quant, quantity)]
+                    quantity = 0
+                    break
+            offset += 10
+        return res
 
-stock_tracking()
+    def _check_location(self, cr, uid, location, context=None):
+        if location.usage == 'view':
+            raise osv.except_osv(_('Error'), _('You cannot move to a location of type view %s.') % (location.name))
+        return True
 
 #----------------------------------------------------------
 # Stock Picking
 #----------------------------------------------------------
+
 class stock_picking(osv.osv):
     _name = "stock.picking"
     _inherit = ['mail.thread']
     _description = "Picking List"
-    _order = "id desc"
-
-    def _set_maximum_date(self, cr, uid, ids, name, value, arg, context=None):
-        """ Calculates planned date if it is greater than 'value'.
-        @param name: Name of field
-        @param value: Value of field
-        @param arg: User defined argument
-        @return: True or False
-        """
-        if not value:
-            return False
-        if isinstance(ids, (int, long)):
-            ids = [ids]
-        for pick in self.browse(cr, uid, ids, context=context):
-            sql_str = """update stock_move set
-                    date_expected='%s'
-                where
-                    picking_id=%d """ % (value, pick.id)
-            if pick.max_date:
-                sql_str += " and (date_expected='" + pick.max_date + "')"
-            cr.execute(sql_str)
-        return True
+    _order = "priority desc, date asc, id desc"
 
-    def _set_minimum_date(self, cr, uid, ids, name, value, arg, context=None):
-        """ Calculates planned date if it is less than 'value'.
-        @param name: Name of field
-        @param value: Value of field
-        @param arg: User defined argument
-        @return: True or False
-        """
-        if not value:
-            return False
-        if isinstance(ids, (int, long)):
-            ids = [ids]
-        for pick in self.browse(cr, uid, ids, context=context):
-            sql_str = """update stock_move set
-                    date_expected='%s'
-                where
-                    picking_id=%s """ % (value, pick.id)
-            if pick.min_date:
-                sql_str += " and (date_expected='" + pick.min_date + "')"
-            cr.execute(sql_str)
-        return True
+    def _set_min_date(self, cr, uid, id, field, value, arg, context=None):
+        move_obj = self.pool.get("stock.move")
+        if value:
+            move_ids = [move.id for move in self.browse(cr, uid, id, context=context).move_lines]
+            move_obj.write(cr, uid, move_ids, {'date_expected': value}, context=context)
+
+    def _set_priority(self, cr, uid, id, field, value, arg, context=None):
+        move_obj = self.pool.get("stock.move")
+        if value:
+            move_ids = [move.id for move in self.browse(cr, uid, id, context=context).move_lines]
+            move_obj.write(cr, uid, move_ids, {'priority': value}, context=context)
 
     def get_min_max_date(self, cr, uid, ids, field_name, arg, context=None):
         """ Finds minimum and maximum dates for picking.
@@ -612,890 +669,873 @@ class stock_picking(osv.osv):
         """
         res = {}
         for id in ids:
-            res[id] = {'min_date': False, 'max_date': False}
+            res[id] = {'min_date': False, 'max_date': False, 'priority': '1'}
         if not ids:
             return res
         cr.execute("""select
                 picking_id,
                 min(date_expected),
-                max(date_expected)
+                max(date_expected),
+                max(priority)
             from
                 stock_move
             where
                 picking_id IN %s
             group by
-                picking_id""",(tuple(ids),))
-        for pick, dt1, dt2 in cr.fetchall():
+                picking_id""", (tuple(ids),))
+        for pick, dt1, dt2, prio in cr.fetchall():
             res[pick]['min_date'] = dt1
             res[pick]['max_date'] = dt2
+            res[pick]['priority'] = prio
         return res
 
     def create(self, cr, user, vals, context=None):
-        if ('name' not in vals) or (vals.get('name')=='/'):
-            seq_obj_name =  self._name
-            vals['name'] = self.pool.get('ir.sequence').get(cr, user, seq_obj_name)
-        new_id = super(stock_picking, self).create(cr, user, vals, context)
-        return new_id
+        context = context or {}
+        if ('name' not in vals) or (vals.get('name') in ('/', False)):
+            ptype_id = vals.get('picking_type_id', context.get('default_picking_type_id', False))
+            sequence_id = self.pool.get('stock.picking.type').browse(cr, user, ptype_id, context=context).sequence_id.id
+            vals['name'] = self.pool.get('ir.sequence').get_id(cr, user, sequence_id, 'id', context=context)
+        return super(stock_picking, self).create(cr, user, vals, context)
+
+    def _state_get(self, cr, uid, ids, field_name, arg, context=None):
+        '''The state of a picking depends on the state of its related stock.move
+            draft: the picking has no line or any one of the lines is draft
+            done, draft, cancel: all lines are done / draft / cancel
+            confirmed, waiting, assigned, partially_available depends on move_type (all at once or partial)
+        '''
+        res = {}
+        for pick in self.browse(cr, uid, ids, context=context):
+            if (not pick.move_lines) or any([x.state == 'draft' for x in pick.move_lines]):
+                res[pick.id] = 'draft'
+                continue
+            if all([x.state == 'cancel' for x in pick.move_lines]):
+                res[pick.id] = 'cancel'
+                continue
+            if all([x.state in ('cancel', 'done') for x in pick.move_lines]):
+                res[pick.id] = 'done'
+                continue
+
+            order = {'confirmed': 0, 'waiting': 1, 'assigned': 2}
+            order_inv = {0: 'confirmed', 1: 'waiting', 2: 'assigned'}
+            lst = [order[x.state] for x in pick.move_lines if x.state not in ('cancel', 'done')]
+            if pick.move_type == 'one':
+                res[pick.id] = order_inv[min(lst)]
+            else:
+                #we are in the case of partial delivery, so if all move are assigned, picking
+                #should be assign too, else if one of the move is assigned, or partially available, picking should be
+                #in partially available state, otherwise, picking is in waiting or confirmed state
+                res[pick.id] = order_inv[max(lst)]
+                if not all(x == 2 for x in lst):
+                    if any(x == 2 for x in lst):
+                        res[pick.id] = 'partially_available'
+                    else:
+                        #if all moves aren't assigned, check if we have one product partially available
+                        for move in pick.move_lines:
+                            if move.partially_available:
+                                res[pick.id] = 'partially_available'
+                                break
+        return res
+
+    def _get_pickings(self, cr, uid, ids, context=None):
+        res = set()
+        for move in self.browse(cr, uid, ids, context=context):
+            if move.picking_id:
+                res.add(move.picking_id.id)
+        return list(res)
+
+    def _get_pack_operation_exist(self, cr, uid, ids, field_name, arg, context=None):
+        res = {}
+        for pick in self.browse(cr, uid, ids, context=context):
+            res[pick.id] = False
+            if pick.pack_operation_ids:
+                res[pick.id] = True
+        return res
+
+    def _get_quant_reserved_exist(self, cr, uid, ids, field_name, arg, context=None):
+        res = {}
+        for pick in self.browse(cr, uid, ids, context=context):
+            res[pick.id] = False
+            for move in pick.move_lines:
+                if move.reserved_quant_ids:
+                    res[pick.id] = True
+                    continue
+        return res
+
+    def check_group_lot(self, cr, uid, context=None):
+        """ This function will return true if we have the setting to use lots activated. """
+        return self.pool.get('res.users').has_group(cr, uid, 'stock.group_production_lot')
+
+    def check_group_pack(self, cr, uid, context=None):
+        """ This function will return true if we have the setting to use package activated. """
+        return self.pool.get('res.users').has_group(cr, uid, 'stock.group_tracking_lot')
+
+    def action_assign_owner(self, cr, uid, ids, context=None):
+        for picking in self.browse(cr, uid, ids, context=context):
+            packop_ids = [op.id for op in picking.pack_operation_ids]
+            self.pool.get('stock.pack.operation').write(cr, uid, packop_ids, {'owner_id': picking.owner_id.id}, context=context)
 
     _columns = {
-        'name': fields.char('Reference', size=64, select=True, states={'done':[('readonly', True)], 'cancel':[('readonly',True)]}),
-        'origin': fields.char('Source Document', size=64, states={'done':[('readonly', True)], 'cancel':[('readonly',True)]}, help="Reference of the document", select=True),
-        'backorder_id': fields.many2one('stock.picking', 'Back Order of', states={'done':[('readonly', True)], 'cancel':[('readonly',True)]}, help="If this shipment was split, then this field links to the shipment which contains the already processed part.", select=True),
-        'type': fields.selection([('out', 'Sending Goods'), ('in', 'Getting Goods'), ('internal', 'Internal')], 'Shipping Type', required=True, select=True, help="Shipping type specify, goods coming in or going out."),
-        'note': fields.text('Notes', states={'done':[('readonly', True)], 'cancel':[('readonly',True)]}),
-        'stock_journal_id': fields.many2one('stock.journal','Stock Journal', select=True, states={'done':[('readonly', True)], 'cancel':[('readonly',True)]}),
-        'location_id': fields.many2one('stock.location', 'Location', states={'done':[('readonly', True)], 'cancel':[('readonly',True)]}, help="Keep empty if you produce at the location where the finished products are needed." \
-                "Set a location if you produce at a fixed location. This can be a partner location " \
-                "if you subcontract the manufacturing operations.", select=True),
-        'location_dest_id': fields.many2one('stock.location', 'Dest. Location', states={'done':[('readonly', True)], 'cancel':[('readonly',True)]}, help="Location where the system will stock the finished products.", select=True),
-        'move_type': fields.selection([('direct', 'Partial'), ('one', 'All at once')], 'Delivery Method', required=True, states={'done':[('readonly', True)], 'cancel':[('readonly',True)]}, help="It specifies goods to be deliver partially or all at once"),
-        'state': fields.selection([
-            ('draft', 'Draft'),
-            ('cancel', 'Cancelled'),
-            ('auto', 'Waiting Another Operation'),
-            ('confirmed', 'Waiting Availability'),
-            ('assigned', 'Ready to Transfer'),
-            ('done', 'Transferred'),
-            ], 'Status', readonly=True, select=True, track_visibility='onchange', help="""
-            * Draft: not confirmed yet and will not be scheduled until confirmed\n
-            * Waiting Another Operation: waiting for another move to proceed before it becomes automatically available (e.g. in Make-To-Order flows)\n
-            * Waiting Availability: still waiting for the availability of products\n
-            * Ready to Transfer: products reserved, simply waiting for confirmation.\n
-            * Transferred: has been processed, can't be modified or cancelled anymore\n
-            * Cancelled: has been cancelled, can't be confirmed anymore"""
+        'name': fields.char('Reference', select=True, states={'done': [('readonly', True)], 'cancel': [('readonly', True)]}, copy=False),
+        'origin': fields.char('Source Document', states={'done': [('readonly', True)], 'cancel': [('readonly', True)]}, help="Reference of the document", select=True),
+        'backorder_id': fields.many2one('stock.picking', 'Back Order of', states={'done': [('readonly', True)], 'cancel': [('readonly', True)]}, help="If this shipment was split, then this field links to the shipment which contains the already processed part.", select=True, copy=False),
+        'note': fields.text('Notes', states={'done': [('readonly', True)], 'cancel': [('readonly', True)]}),
+        'move_type': fields.selection([('direct', 'Partial'), ('one', 'All at once')], 'Delivery Method', required=True, states={'done': [('readonly', True)], 'cancel': [('readonly', True)]}, help="It specifies goods to be deliver partially or all at once"),
+        'state': fields.function(_state_get, type="selection", copy=False,
+            store={
+                'stock.picking': (lambda self, cr, uid, ids, ctx: ids, ['move_type'], 20),
+                'stock.move': (_get_pickings, ['state', 'picking_id', 'partially_available'], 20)},
+            selection=[
+                ('draft', 'Draft'),
+                ('cancel', 'Cancelled'),
+                ('waiting', 'Waiting Another Operation'),
+                ('confirmed', 'Waiting Availability'),
+                ('partially_available', 'Partially Available'),
+                ('assigned', 'Ready to Transfer'),
+                ('done', 'Transferred'),
+                ], string='Status', readonly=True, select=True, track_visibility='onchange',
+            help="""
+                * Draft: not confirmed yet and will not be scheduled until confirmed\n
+                * Waiting Another Operation: waiting for another move to proceed before it becomes automatically available (e.g. in Make-To-Order flows)\n
+                * Waiting Availability: still waiting for the availability of products\n
+                * Partially Available: some products are available and reserved\n
+                * Ready to Transfer: products reserved, simply waiting for confirmation.\n
+                * Transferred: has been processed, can't be modified or cancelled anymore\n
+                * Cancelled: has been cancelled, can't be confirmed anymore"""
         ),
-        'min_date': fields.function(get_min_max_date, fnct_inv=_set_minimum_date, multi="min_max_date",
-                 store=True, type='datetime', string='Scheduled Time', select=1, help="Scheduled time for the shipment to be processed"),
-        'date': fields.datetime('Creation Date', help="Creation date, usually the time of the order.", select=True, states={'done':[('readonly', True)], 'cancel':[('readonly',True)]}),
-        'date_done': fields.datetime('Date of Transfer', help="Date of Completion", states={'done':[('readonly', True)], 'cancel':[('readonly',True)]}),
-        'max_date': fields.function(get_min_max_date, fnct_inv=_set_maximum_date, multi="min_max_date",
-                 store=True, type='datetime', string='Max. Expected Date', select=2),
-        'move_lines': fields.one2many('stock.move', 'picking_id', 'Internal Moves', states={'done': [('readonly', True)], 'cancel': [('readonly', True)]}),
+        'priority': fields.function(get_min_max_date, multi="min_max_date", fnct_inv=_set_priority, type='selection', selection=procurement.PROCUREMENT_PRIORITIES, string='Priority',
+                                    store={'stock.move': (_get_pickings, ['priority', 'picking_id'], 20)}, states={'done': [('readonly', True)], 'cancel': [('readonly', True)]}, select=1, help="Priority for this picking. Setting manually a value here would set it as priority for all the moves",
+                                    track_visibility='onchange', required=True),
+        'min_date': fields.function(get_min_max_date, multi="min_max_date", fnct_inv=_set_min_date,
+                 store={'stock.move': (_get_pickings, ['date_expected', 'picking_id'], 20)}, type='datetime', states={'done': [('readonly', True)], 'cancel': [('readonly', True)]}, string='Scheduled Date', select=1, help="Scheduled time for the first part of the shipment to be processed. Setting manually a value here would set it as expected date for all the stock moves.", track_visibility='onchange'),
+        'max_date': fields.function(get_min_max_date, multi="min_max_date",
+                 store={'stock.move': (_get_pickings, ['date_expected', 'picking_id'], 20)}, type='datetime', string='Max. Expected Date', select=2, help="Scheduled time for the last part of the shipment to be processed"),
+        'date': fields.datetime('Creation Date', help="Creation Date, usually the time of the order", select=True, states={'done': [('readonly', True)], 'cancel': [('readonly', True)]}, track_visibility='onchange'),
+        'date_done': fields.datetime('Date of Transfer', help="Date of Completion", states={'done': [('readonly', True)], 'cancel': [('readonly', True)]}, copy=False),
+        'move_lines': fields.one2many('stock.move', 'picking_id', 'Internal Moves', states={'done': [('readonly', True)], 'cancel': [('readonly', True)]}, copy=True),
+        'quant_reserved_exist': fields.function(_get_quant_reserved_exist, type='boolean', string='Quant already reserved ?', help='technical field used to know if there is already at least one quant reserved on moves of a given picking'),
+        'partner_id': fields.many2one('res.partner', 'Partner', states={'done': [('readonly', True)], 'cancel': [('readonly', True)]}),
+        'company_id': fields.many2one('res.company', 'Company', required=True, select=True, states={'done': [('readonly', True)], 'cancel': [('readonly', True)]}),
+        'pack_operation_ids': fields.one2many('stock.pack.operation', 'picking_id', states={'done': [('readonly', True)], 'cancel': [('readonly', True)]}, string='Related Packing Operations'),
+        'pack_operation_exist': fields.function(_get_pack_operation_exist, type='boolean', string='Pack Operation Exists?', help='technical field for attrs in view'),
+        'picking_type_id': fields.many2one('stock.picking.type', 'Picking Type', states={'done': [('readonly', True)], 'cancel': [('readonly', True)]}, required=True),
+        'picking_type_code': fields.related('picking_type_id', 'code', type='char', string='Picking Type Code', help="Technical field used to display the correct label on print button in the picking view"),
+
+        'owner_id': fields.many2one('res.partner', 'Owner', states={'done': [('readonly', True)], 'cancel': [('readonly', True)]}, help="Default Owner"),
+        # Used to search on pickings
         'product_id': fields.related('move_lines', 'product_id', type='many2one', relation='product.product', string='Product'),
-        'auto_picking': fields.boolean('Auto-Picking', states={'done':[('readonly', True)], 'cancel':[('readonly',True)]}),
-        'partner_id': fields.many2one('res.partner', 'Partner', states={'done':[('readonly', True)], 'cancel':[('readonly',True)]}),
-        'invoice_state': fields.selection([
-            ("invoiced", "Invoiced"),
-            ("2binvoiced", "To Be Invoiced"),
-            ("none", "Not Applicable")], "Invoice Control",
-            select=True, required=True, readonly=True, track_visibility='onchange', states={'draft': [('readonly', False)]}),
-        'company_id': fields.many2one('res.company', 'Company', required=True, select=True, states={'done':[('readonly', True)], 'cancel':[('readonly',True)]}),
+        'recompute_pack_op': fields.boolean('Recompute pack operation?', help='True if reserved quants changed, which mean we might need to recompute the package operations', copy=False),
+        'location_id': fields.related('move_lines', 'location_id', type='many2one', relation='stock.location', string='Location', readonly=True),
+        'location_dest_id': fields.related('move_lines', 'location_dest_id', type='many2one', relation='stock.location', string='Destination Location', readonly=True),
+        'group_id': fields.related('move_lines', 'group_id', type='many2one', relation='procurement.group', string='Procurement Group', readonly=True,
+              store={
+                  'stock.picking': (lambda self, cr, uid, ids, ctx: ids, ['move_lines'], 10),
+                  'stock.move': (_get_pickings, ['group_id', 'picking_id'], 10),
+              }),
     }
+
     _defaults = {
-        'name': lambda self, cr, uid, context: '/',
+        'name': '/',
         'state': 'draft',
         'move_type': 'direct',
-        'type': 'internal',
-        'invoice_state': 'none',
-        'date': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
-        'company_id': lambda self, cr, uid, c: self.pool.get('res.company')._company_default_get(cr, uid, 'stock.picking', context=c)
+        'priority': '1',  # normal
+        'date': fields.datetime.now,
+        'company_id': lambda self, cr, uid, c: self.pool.get('res.company')._company_default_get(cr, uid, 'stock.picking', context=c),
+        'recompute_pack_op': True,
     }
     _sql_constraints = [
-        ('name_uniq', 'unique(name, company_id)', 'Reference must be unique per Company!'),
+        ('name_uniq', 'unique(name, company_id)', 'Reference must be unique per company!'),
     ]
 
-    def action_process(self, cr, uid, ids, context=None):
-        if context is None:
-            context = {}
-        """Open the partial picking wizard"""
-        context.update({
-            'active_model': self._name,
-            'active_ids': ids,
-            'active_id': len(ids) and ids[0] or False
-        })
-        return {
-            'view_type': 'form',
-            'view_mode': 'form',
-            'res_model': 'stock.partial.picking',
-            'type': 'ir.actions.act_window',
-            'target': 'new',
-            'context': context,
-            'nodestroy': True,
-        }
-
-    def copy(self, cr, uid, id, default=None, context=None):
-        if default is None:
-            default = {}
-        default = default.copy()
-        picking_obj = self.browse(cr, uid, id, context=context)
-        if ('name' not in default) or (picking_obj.name == '/'):
-            seq_obj_name = 'stock.picking.' + picking_obj.type
-            default['name'] = self.pool.get('ir.sequence').get(cr, uid, seq_obj_name)
-            default.setdefault('origin', False)
-            default.setdefault('backorder_id', False)
-        if 'invoice_state' not in default and picking_obj.invoice_state == 'invoiced':
-            default['invoice_state'] = '2binvoiced'
-        res = super(stock_picking, self).copy(cr, uid, id, default, context)
-        return res
-
-    def fields_view_get(self, cr, uid, view_id=None, view_type=False, context=None, toolbar=False, submenu=False):
-        if view_type == 'form' and not view_id:
-            mod_obj = self.pool.get('ir.model.data')
-            if self._name == "stock.picking.in":
-                model, view_id = mod_obj.get_object_reference(cr, uid, 'stock', 'view_picking_in_form')
-            if self._name == "stock.picking.out":
-                model, view_id = mod_obj.get_object_reference(cr, uid, 'stock', 'view_picking_out_form')
-        return super(stock_picking, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
+    def do_print_picking(self, cr, uid, ids, context=None):
+        '''This function prints the picking list'''
+        context = dict(context or {}, active_ids=ids)
+        return self.pool.get("report").get_action(cr, uid, ids, 'stock.report_picking', context=context)
 
-    def onchange_partner_in(self, cr, uid, ids, partner_id=None, context=None):
-        return {}
-
-    def action_explode(self, cr, uid, moves, context=None):
-        """Hook to allow other modules to split the moves of a picking."""
-        return moves
 
     def action_confirm(self, cr, uid, ids, context=None):
-        """ Confirms picking.
-        @return: True
-        """
-        pickings = self.browse(cr, uid, ids, context=context)
-        self.write(cr, uid, ids, {'state': 'confirmed'})
         todo = []
-        for picking in pickings:
+        todo_force_assign = []
+        for picking in self.browse(cr, uid, ids, context=context):
+            if picking.location_id.usage in ('supplier', 'inventory', 'production'):
+                todo_force_assign.append(picking.id)
             for r in picking.move_lines:
                 if r.state == 'draft':
                     todo.append(r.id)
-        todo = self.action_explode(cr, uid, todo, context)
         if len(todo):
             self.pool.get('stock.move').action_confirm(cr, uid, todo, context=context)
-        return True
 
-    def test_auto_picking(self, cr, uid, ids):
-        # TODO: Check locations to see if in the same location ?
+        if todo_force_assign:
+            self.force_assign(cr, uid, todo_force_assign, context=context)
         return True
 
-    def action_assign(self, cr, uid, ids, *args):
-        """ Changes state of picking to available if all moves are confirmed.
+    def action_assign(self, cr, uid, ids, context=None):
+        """ Check availability of picking moves.
+        This has the effect of changing the state and reserve quants on available moves, and may
+        also impact the state of the picking as it is computed based on move's states.
         @return: True
         """
-        wf_service = netsvc.LocalService("workflow")
-        for pick in self.browse(cr, uid, ids):
+        for pick in self.browse(cr, uid, ids, context=context):
             if pick.state == 'draft':
-                wf_service.trg_validate(uid, 'stock.picking', pick.id, 'button_confirm', cr)
-            move_ids = [x.id for x in pick.move_lines if x.state == 'confirmed']
+                self.action_confirm(cr, uid, [pick.id], context=context)
+            pick.refresh()
+            #skip the moves that don't need to be checked
+            move_ids = [x.id for x in pick.move_lines if x.state not in ('draft', 'cancel', 'done')]
             if not move_ids:
-                raise osv.except_osv(_('Warning!'),_('Not enough stock, unable to reserve the products.'))
-            self.pool.get('stock.move').action_assign(cr, uid, move_ids)
+                raise osv.except_osv(_('Warning!'), _('Nothing to check the availability for.'))
+            self.pool.get('stock.move').action_assign(cr, uid, move_ids, context=context)
         return True
 
-    def force_assign(self, cr, uid, ids, *args):
+    def force_assign(self, cr, uid, ids, context=None):
         """ Changes state of picking to available if moves are confirmed or waiting.
         @return: True
         """
-        wf_service = netsvc.LocalService("workflow")
-        for pick in self.browse(cr, uid, ids):
-            move_ids = [x.id for x in pick.move_lines if x.state in ['confirmed','waiting']]
-            self.pool.get('stock.move').force_assign(cr, uid, move_ids)
-            wf_service.trg_write(uid, 'stock.picking', pick.id, cr)
-        return True
-
-    def draft_force_assign(self, cr, uid, ids, *args):
-        """ Confirms picking directly from draft state.
-        @return: True
-        """
-        wf_service = netsvc.LocalService("workflow")
-        for pick in self.browse(cr, uid, ids):
-            if not pick.move_lines:
-                raise osv.except_osv(_('Error!'),_('You cannot process picking without stock moves.'))
-            wf_service.trg_validate(uid, 'stock.picking', pick.id,
-                'button_confirm', cr)
-        return True
-
-    def draft_validate(self, cr, uid, ids, context=None):
-        """ Validates picking directly from draft state.
-        @return: True
-        """
-        wf_service = netsvc.LocalService("workflow")
-        self.draft_force_assign(cr, uid, ids)
         for pick in self.browse(cr, uid, ids, context=context):
-            move_ids = [x.id for x in pick.move_lines]
-            self.pool.get('stock.move').force_assign(cr, uid, move_ids)
-            wf_service.trg_write(uid, 'stock.picking', pick.id, cr)
-        return self.action_process(
-            cr, uid, ids, context=context)
-    def cancel_assign(self, cr, uid, ids, *args):
-        """ Cancels picking and moves.
-        @return: True
-        """
-        wf_service = netsvc.LocalService("workflow")
-        for pick in self.browse(cr, uid, ids):
-            move_ids = [x.id for x in pick.move_lines]
-            self.pool.get('stock.move').cancel_assign(cr, uid, move_ids)
-            wf_service.trg_write(uid, 'stock.picking', pick.id, cr)
-        return True
-
-    def action_assign_wkf(self, cr, uid, ids, context=None):
-        """ Changes picking state to assigned.
-        @return: True
-        """
-        self.write(cr, uid, ids, {'state': 'assigned'})
-        return True
-
-    def test_finished(self, cr, uid, ids):
-        """ Tests whether the move is in done or cancel state or not.
-        @return: True or False
-        """
-        move_ids = self.pool.get('stock.move').search(cr, uid, [('picking_id', 'in', ids)])
-        for move in self.pool.get('stock.move').browse(cr, uid, move_ids):
-            if move.state not in ('done', 'cancel'):
-
-                if move.product_qty != 0.0:
-                    return False
-                else:
-                    move.write({'state': 'done'})
+            move_ids = [x.id for x in pick.move_lines if x.state in ['confirmed', 'waiting']]
+            self.pool.get('stock.move').force_assign(cr, uid, move_ids, context=context)
+        #pack_operation might have changed and need to be recomputed
+        self.write(cr, uid, ids, {'recompute_pack_op': True}, context=context)
         return True
 
-    def test_assigned(self, cr, uid, ids):
-        """ Tests whether the move is in assigned state or not.
-        @return: True or False
-        """
-        #TOFIX: assignment of move lines should be call before testing assigment otherwise picking never gone in assign state
-        ok = True
-        for pick in self.browse(cr, uid, ids):
-            mt = pick.move_type
-            # incomming shipments are always set as available if they aren't chained
-            if pick.type == 'in':
-                if all([x.state != 'waiting' for x in pick.move_lines]):
-                    return True
-            for move in pick.move_lines:
-                if (move.state in ('confirmed', 'draft')) and (mt == 'one'):
-                    return False
-                if (mt == 'direct') and (move.state == 'assigned') and (move.product_qty):
-                    return True
-                ok = ok and (move.state in ('cancel', 'done', 'assigned'))
-        return ok
-
     def action_cancel(self, cr, uid, ids, context=None):
-        """ Changes picking state to cancel.
-        @return: True
-        """
         for pick in self.browse(cr, uid, ids, context=context):
             ids2 = [move.id for move in pick.move_lines]
             self.pool.get('stock.move').action_cancel(cr, uid, ids2, context)
-        self.write(cr, uid, ids, {'state': 'cancel', 'invoice_state': 'none'})
         return True
 
-    #
-    # TODO: change and create a move if not parents
-    #
     def action_done(self, cr, uid, ids, context=None):
-        """Changes picking state to done.
-        
-        This method is called at the end of the workflow by the activity "done".
-        @return: True
-        """
-        self.write(cr, uid, ids, {'state': 'done', 'date_done': time.strftime('%Y-%m-%d %H:%M:%S')})
-        return True
+        """Changes picking state to done by processing the Stock Moves of the Picking
 
-    def action_move(self, cr, uid, ids, context=None):
-        """Process the Stock Moves of the Picking
-        
-        This method is called by the workflow by the activity "move".
-        Normally that happens when the signal button_done is received (button 
-        "Done" pressed on a Picking view). 
+        Normally that happens when the button "Done" is pressed on a Picking view.
         @return: True
         """
         for pick in self.browse(cr, uid, ids, context=context):
             todo = []
             for move in pick.move_lines:
                 if move.state == 'draft':
-                    self.pool.get('stock.move').action_confirm(cr, uid, [move.id],
-                        context=context)
-                    todo.append(move.id)
-                elif move.state in ('assigned','confirmed'):
+                    todo.extend(self.pool.get('stock.move').action_confirm(cr, uid, [move.id], context=context))
+                elif move.state in ('assigned', 'confirmed'):
                     todo.append(move.id)
             if len(todo):
-                self.pool.get('stock.move').action_done(cr, uid, todo,
-                        context=context)
+                self.pool.get('stock.move').action_done(cr, uid, todo, context=context)
         return True
 
-    def get_currency_id(self, cr, uid, picking):
-        return False
+    def unlink(self, cr, uid, ids, context=None):
+        #on picking deletion, cancel its move then unlink them too
+        move_obj = self.pool.get('stock.move')
+        context = context or {}
+        for pick in self.browse(cr, uid, ids, context=context):
+            move_ids = [move.id for move in pick.move_lines]
+            move_obj.action_cancel(cr, uid, move_ids, context=context)
+            move_obj.unlink(cr, uid, move_ids, context=context)
+        return super(stock_picking, self).unlink(cr, uid, ids, context=context)
+
+    def write(self, cr, uid, ids, vals, context=None):
+        res = super(stock_picking, self).write(cr, uid, ids, vals, context=context)
+        #if we changed the move lines or the pack operations, we need to recompute the remaining quantities of both
+        if 'move_lines' in vals or 'pack_operation_ids' in vals:
+            self.do_recompute_remaining_quantities(cr, uid, ids, context=context)
+        return res
 
-    def _get_partner_to_invoice(self, cr, uid, picking, context=None):
-        """ Gets the partner that will be invoiced
-            Note that this function is inherited in the sale and purchase modules
-            @param picking: object of the picking for which we are selecting the partner to invoice
-            @return: object of the partner to invoice
+    def _create_backorder(self, cr, uid, picking, backorder_moves=[], context=None):
+        """ Move all non-done lines into a new backorder picking. If the key 'do_only_split' is given in the context, then move all lines not in context.get('split', []) instead of all non-done lines.
         """
-        return picking.partner_id and picking.partner_id.id
+        if not backorder_moves:
+            backorder_moves = picking.move_lines
+        backorder_move_ids = [x.id for x in backorder_moves if x.state not in ('done', 'cancel')]
+        if 'do_only_split' in context and context['do_only_split']:
+            backorder_move_ids = [x.id for x in backorder_moves if x.id not in context.get('split', [])]
+
+        if backorder_move_ids:
+            backorder_id = self.copy(cr, uid, picking.id, {
+                'name': '/',
+                'move_lines': [],
+                'pack_operation_ids': [],
+                'backorder_id': picking.id,
+            })
+            backorder = self.browse(cr, uid, backorder_id, context=context)
+            self.message_post(cr, uid, picking.id, body=_("Back order <em>%s</em> <b>created</b>.") % (backorder.name), context=context)
+            move_obj = self.pool.get("stock.move")
+            move_obj.write(cr, uid, backorder_move_ids, {'picking_id': backorder_id}, context=context)
+
+            self.write(cr, uid, [picking.id], {'date_done': time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)}, context=context)
+            self.action_confirm(cr, uid, [backorder_id], context=context)
+            return backorder_id
+        return False
+
+    @api.cr_uid_ids_context
+    def recheck_availability(self, cr, uid, picking_ids, context=None):
+        self.action_assign(cr, uid, picking_ids, context=context)
+        self.do_prepare_partial(cr, uid, picking_ids, context=context)
 
-    def _get_comment_invoice(self, cr, uid, picking):
+    def _get_top_level_packages(self, cr, uid, quants_suggested_locations, context=None):
+        """This method searches for the higher level packages that can be moved as a single operation, given a list of quants
+           to move and their suggested destination, and returns the list of matching packages.
         """
-        @return: comment string for invoice
+        # Try to find as much as possible top-level packages that can be moved
+        pack_obj = self.pool.get("stock.quant.package")
+        quant_obj = self.pool.get("stock.quant")
+        top_lvl_packages = set()
+        quants_to_compare = quants_suggested_locations.keys()
+        for pack in list(set([x.package_id for x in quants_suggested_locations.keys() if x and x.package_id])):
+            loop = True
+            test_pack = pack
+            good_pack = False
+            pack_destination = False
+            while loop:
+                pack_quants = pack_obj.get_content(cr, uid, [test_pack.id], context=context)
+                all_in = True
+                for quant in quant_obj.browse(cr, uid, pack_quants, context=context):
+                    # If the quant is not in the quants to compare and not in the common location
+                    if not quant in quants_to_compare:
+                        all_in = False
+                        break
+                    else:
+                        #if putaway strat apply, the destination location of each quant may be different (and thus the package should not be taken as a single operation)
+                        if not pack_destination:
+                            pack_destination = quants_suggested_locations[quant]
+                        elif pack_destination != quants_suggested_locations[quant]:
+                            all_in = False
+                            break
+                if all_in:
+                    good_pack = test_pack
+                    if test_pack.parent_id:
+                        test_pack = test_pack.parent_id
+                    else:
+                        #stop the loop when there's no parent package anymore
+                        loop = False
+                else:
+                    #stop the loop when the package test_pack is not totally reserved for moves of this picking
+                    #(some quants may be reserved for other picking or not reserved at all)
+                    loop = False
+            if good_pack:
+                top_lvl_packages.add(good_pack)
+        return list(top_lvl_packages)
+
+    def _prepare_pack_ops(self, cr, uid, picking, quants, forced_qties, context=None):
+        """ returns a list of dict, ready to be used in create() of stock.pack.operation.
+
+        :param picking: browse record (stock.picking)
+        :param quants: browse record list (stock.quant). List of quants associated to the picking
+        :param forced_qties: dictionary showing for each product (keys) its corresponding quantity (value) that is not covered by the quants associated to the picking
         """
-        return picking.note or ''
+        def _picking_putaway_apply(product):
+            location = False
+            # Search putaway strategy
+            if product_putaway_strats.get(product.id):
+                location = product_putaway_strats[product.id]
+            else:
+                location = self.pool.get('stock.location').get_putaway_strategy(cr, uid, picking.location_dest_id, product, context=context)
+                product_putaway_strats[product.id] = location
+            return location or picking.location_dest_id.id
+
+        # If we encounter an UoM that is smaller than the default UoM or the one already chosen, use the new one instead.
+        product_uom = {} # Determines UoM used in pack operations
+        for move in picking.move_lines:
+            if not product_uom.get(move.product_id.id):
+                product_uom[move.product_id.id] = move.product_id.uom_id.id
+            if move.product_uom.id != move.product_id.uom_id.id and move.product_uom.factor > product_uom[move.product_id.id]:
+                product_uom[move.product_id.id] = move.product_uom.id
+
+        pack_obj = self.pool.get("stock.quant.package")
+        quant_obj = self.pool.get("stock.quant")
+        vals = []
+        qtys_grouped = {}
+        #for each quant of the picking, find the suggested location
+        quants_suggested_locations = {}
+        product_putaway_strats = {}
+        for quant in quants:
+            if quant.qty <= 0:
+                continue
+            suggested_location_id = _picking_putaway_apply(quant.product_id)
+            quants_suggested_locations[quant] = suggested_location_id
+
+        #find the packages we can movei as a whole
+        top_lvl_packages = self._get_top_level_packages(cr, uid, quants_suggested_locations, context=context)
+        # and then create pack operations for the top-level packages found
+        for pack in top_lvl_packages:
+            pack_quant_ids = pack_obj.get_content(cr, uid, [pack.id], context=context)
+            pack_quants = quant_obj.browse(cr, uid, pack_quant_ids, context=context)
+            vals.append({
+                    'picking_id': picking.id,
+                    'package_id': pack.id,
+                    'product_qty': 1.0,
+                    'location_id': pack.location_id.id,
+                    'location_dest_id': quants_suggested_locations[pack_quants[0]],
+                })
+            #remove the quants inside the package so that they are excluded from the rest of the computation
+            for quant in pack_quants:
+                del quants_suggested_locations[quant]
+
+        # Go through all remaining reserved quants and group by product, package, lot, owner, source location and dest location
+        for quant, dest_location_id in quants_suggested_locations.items():
+            key = (quant.product_id.id, quant.package_id.id, quant.lot_id.id, quant.owner_id.id, quant.location_id.id, dest_location_id)
+            if qtys_grouped.get(key):
+                qtys_grouped[key] += quant.qty
+            else:
+                qtys_grouped[key] = quant.qty
 
-    def _get_price_unit_invoice(self, cr, uid, move_line, type, context=None):
-        """ Gets price unit for invoice
-        @param move_line: Stock move lines
-        @param type: Type of invoice
-        @return: The price unit for the move line
-        """
-        if context is None:
-            context = {}
+        # Do the same for the forced quantities (in cases of force_assign or incomming shipment for example)
+        for product, qty in forced_qties.items():
+            if qty <= 0:
+                continue
+            suggested_location_id = _picking_putaway_apply(product)
+            key = (product.id, False, False, False, picking.location_id.id, suggested_location_id)
+            if qtys_grouped.get(key):
+                qtys_grouped[key] += qty
+            else:
+                qtys_grouped[key] = qty
 
-        if type in ('in_invoice', 'in_refund'):
-            # Take the user company and pricetype
-            context['currency_id'] = move_line.company_id.currency_id.id
-            amount_unit = move_line.product_id.price_get('standard_price', context=context)[move_line.product_id.id]
-            return amount_unit
-        else:
-            return move_line.product_id.list_price
-
-    def _get_discount_invoice(self, cr, uid, move_line):
-        '''Return the discount for the move line'''
-        return 0.0
-
-    def _get_taxes_invoice(self, cr, uid, move_line, type):
-        """ Gets taxes on invoice
-        @param move_line: Stock move lines
-        @param type: Type of invoice
-        @return: Taxes Ids for the move line
+        # Create the necessary operations for the grouped quants and remaining qtys
+        uom_obj = self.pool.get('product.uom')
+        for key, qty in qtys_grouped.items():
+            product = self.pool.get("product.product").browse(cr, uid, key[0], context=context)
+            uom_id = product.uom_id.id
+            qty_uom = qty
+            if product_uom.get(key[0]):
+                uom_id = product_uom[key[0]]
+                qty_uom = uom_obj._compute_qty(cr, uid, product.uom_id.id, qty, uom_id)
+            vals.append({
+                'picking_id': picking.id,
+                'product_qty': qty_uom,
+                'product_id': key[0],
+                'package_id': key[1],
+                'lot_id': key[2],
+                'owner_id': key[3],
+                'location_id': key[4],
+                'location_dest_id': key[5],
+                'product_uom_id': uom_id,
+            })
+        return vals
+
+    @api.cr_uid_ids_context
+    def open_barcode_interface(self, cr, uid, picking_ids, context=None):
+        final_url="/barcode/web/#action=stock.ui&picking_id="+str(picking_ids[0])
+        return {'type': 'ir.actions.act_url', 'url':final_url, 'target': 'self',}
+
+    @api.cr_uid_ids_context
+    def do_partial_open_barcode(self, cr, uid, picking_ids, context=None):
+        self.do_prepare_partial(cr, uid, picking_ids, context=context)
+        return self.open_barcode_interface(cr, uid, picking_ids, context=context)
+
+    @api.cr_uid_ids_context
+    def do_prepare_partial(self, cr, uid, picking_ids, context=None):
+        context = context or {}
+        pack_operation_obj = self.pool.get('stock.pack.operation')
+        #used to avoid recomputing the remaining quantities at each new pack operation created
+        ctx = context.copy()
+        ctx['no_recompute'] = True
+
+        #get list of existing operations and delete them
+        existing_package_ids = pack_operation_obj.search(cr, uid, [('picking_id', 'in', picking_ids)], context=context)
+        if existing_package_ids:
+            pack_operation_obj.unlink(cr, uid, existing_package_ids, context)
+        for picking in self.browse(cr, uid, picking_ids, context=context):
+            forced_qties = {}  # Quantity remaining after calculating reserved quants
+            picking_quants = []
+            #Calculate packages, reserved quants, qtys of this picking's moves
+            for move in picking.move_lines:
+                if move.state not in ('assigned', 'confirmed'):
+                    continue
+                move_quants = move.reserved_quant_ids
+                picking_quants += move_quants
+                forced_qty = (move.state == 'assigned') and move.product_qty - sum([x.qty for x in move_quants]) or 0
+                #if we used force_assign() on the move, or if the move is incoming, forced_qty > 0
+                if float_compare(forced_qty, 0, precision_rounding=move.product_id.uom_id.rounding) > 0:
+                    if forced_qties.get(move.product_id):
+                        forced_qties[move.product_id] += forced_qty
+                    else:
+                        forced_qties[move.product_id] = forced_qty
+            for vals in self._prepare_pack_ops(cr, uid, picking, picking_quants, forced_qties, context=context):
+                pack_operation_obj.create(cr, uid, vals, context=ctx)
+        #recompute the remaining quantities all at once
+        self.do_recompute_remaining_quantities(cr, uid, picking_ids, context=context)
+        self.write(cr, uid, picking_ids, {'recompute_pack_op': False}, context=context)
+
+    @api.cr_uid_ids_context
+    def do_unreserve(self, cr, uid, picking_ids, context=None):
         """
-        if type in ('in_invoice', 'in_refund'):
-            taxes = move_line.product_id.supplier_taxes_id
-        else:
-            taxes = move_line.product_id.taxes_id
-
-        if move_line.picking_id and move_line.picking_id.partner_id and move_line.picking_id.partner_id.id:
-            return self.pool.get('account.fiscal.position').map_tax(
-                cr,
-                uid,
-                move_line.picking_id.partner_id.property_account_position,
-                taxes
-            )
-        else:
-            return map(lambda x: x.id, taxes)
-
-    def _get_account_analytic_invoice(self, cr, uid, picking, move_line):
-        return False
+          Will remove all quants for picking in picking_ids
+        """
+        moves_to_unreserve = []
+        pack_line_to_unreserve = []
+        for picking in self.browse(cr, uid, picking_ids, context=context):
+            moves_to_unreserve += [m.id for m in picking.move_lines if m.state not in ('done', 'cancel')]
+            pack_line_to_unreserve += [p.id for p in picking.pack_operation_ids]
+        if moves_to_unreserve:
+            if pack_line_to_unreserve:
+                self.pool.get('stock.pack.operation').unlink(cr, uid, pack_line_to_unreserve, context=context)
+            self.pool.get('stock.move').do_unreserve(cr, uid, moves_to_unreserve, context=context)
+
+    def recompute_remaining_qty(self, cr, uid, picking, context=None):
+        def _create_link_for_index(operation_id, index, product_id, qty_to_assign, quant_id=False):
+            move_dict = prod2move_ids[product_id][index]
+            qty_on_link = min(move_dict['remaining_qty'], qty_to_assign)
+            self.pool.get('stock.move.operation.link').create(cr, uid, {'move_id': move_dict['move'].id, 'operation_id': operation_id, 'qty': qty_on_link, 'reserved_quant_id': quant_id}, context=context)
+            if move_dict['remaining_qty'] == qty_on_link:
+                prod2move_ids[product_id].pop(index)
+            else:
+                move_dict['remaining_qty'] -= qty_on_link
+            return qty_on_link
+
+        def _create_link_for_quant(operation_id, quant, qty):
+            """create a link for given operation and reserved move of given quant, for the max quantity possible, and returns this quantity"""
+            if not quant.reservation_id.id:
+                return _create_link_for_product(operation_id, quant.product_id.id, qty)
+            qty_on_link = 0
+            for i in range(0, len(prod2move_ids[quant.product_id.id])):
+                if prod2move_ids[quant.product_id.id][i]['move'].id != quant.reservation_id.id:
+                    continue
+                qty_on_link = _create_link_for_index(operation_id, i, quant.product_id.id, qty, quant_id=quant.id)
+                break
+            return qty_on_link
+
+        def _create_link_for_product(operation_id, product_id, qty):
+            '''method that creates the link between a given operation and move(s) of given product, for the given quantity.
+            Returns True if it was possible to create links for the requested quantity (False if there was not enough quantity on stock moves)'''
+            qty_to_assign = qty
+            prod_obj = self.pool.get("product.product")
+            product = prod_obj.browse(cr, uid, product_id)
+            rounding = product.uom_id.rounding
+            qtyassign_cmp = float_compare(qty_to_assign, 0.0, precision_rounding=rounding)
+            if prod2move_ids.get(product_id):
+                while prod2move_ids[product_id] and qtyassign_cmp > 0:
+                    qty_on_link = _create_link_for_index(operation_id, 0, product_id, qty_to_assign, quant_id=False)
+                    qty_to_assign -= qty_on_link
+                    qtyassign_cmp = float_compare(qty_to_assign, 0.0, precision_rounding=rounding)
+            return qtyassign_cmp == 0
 
-    def _invoice_line_hook(self, cr, uid, move_line, invoice_line_id):
-        '''Call after the creation of the invoice line'''
-        return
-
-    def _invoice_hook(self, cr, uid, picking, invoice_id):
-        '''Call after the creation of the invoice'''
-        return
-
-    def _get_invoice_type(self, pick):
-        src_usage = dest_usage = None
-        inv_type = None
-        if pick.invoice_state == '2binvoiced':
-            if pick.move_lines:
-                src_usage = pick.move_lines[0].location_id.usage
-                dest_usage = pick.move_lines[0].location_dest_id.usage
-            if pick.type == 'out' and dest_usage == 'supplier':
-                inv_type = 'in_refund'
-            elif pick.type == 'out' and dest_usage == 'customer':
-                inv_type = 'out_invoice'
-            elif pick.type == 'in' and src_usage == 'supplier':
-                inv_type = 'in_invoice'
-            elif pick.type == 'in' and src_usage == 'customer':
-                inv_type = 'out_refund'
+        uom_obj = self.pool.get('product.uom')
+        package_obj = self.pool.get('stock.quant.package')
+        quant_obj = self.pool.get('stock.quant')
+        quants_in_package_done = set()
+        prod2move_ids = {}
+        still_to_do = []
+        #make a dictionary giving for each product, the moves and related quantity that can be used in operation links
+        for move in picking.move_lines:
+            if not prod2move_ids.get(move.product_id.id):
+                prod2move_ids[move.product_id.id] = [{'move': move, 'remaining_qty': move.product_qty}]
             else:
-                inv_type = 'out_invoice'
-        return inv_type
-
-    def _prepare_invoice_group(self, cr, uid, picking, partner, invoice, context=None):
-        """ Builds the dict for grouped invoices
-            @param picking: picking object
-            @param partner: object of the partner to invoice (not used here, but may be usefull if this function is inherited)
-            @param invoice: object of the invoice that we are updating
-            @return: dict that will be used to update the invoice
+                prod2move_ids[move.product_id.id].append({'move': move, 'remaining_qty': move.product_qty})
+
+        need_rereserve = False
+        #sort the operations in order to give higher priority to those with a package, then a serial number
+        operations = picking.pack_operation_ids
+        operations = sorted(operations, key=lambda x: ((x.package_id and not x.product_id) and -4 or 0) + (x.package_id and -2 or 0) + (x.lot_id and -1 or 0))
+        #delete existing operations to start again from scratch
+        cr.execute("DELETE FROM stock_move_operation_link WHERE operation_id in %s", (tuple([x.id for x in operations]),))
+        #1) first, try to create links when quants can be identified without any doubt
+        for ops in operations:
+            #for each operation, create the links with the stock move by seeking on the matching reserved quants,
+            #and deffer the operation if there is some ambiguity on the move to select
+            if ops.package_id and not ops.product_id:
+                #entire package
+                quant_ids = package_obj.get_content(cr, uid, [ops.package_id.id], context=context)
+                for quant in quant_obj.browse(cr, uid, quant_ids, context=context):
+                    remaining_qty_on_quant = quant.qty
+                    if quant.reservation_id:
+                        #avoid quants being counted twice
+                        quants_in_package_done.add(quant.id)
+                        qty_on_link = _create_link_for_quant(ops.id, quant, quant.qty)
+                        remaining_qty_on_quant -= qty_on_link
+                    if remaining_qty_on_quant:
+                        still_to_do.append((ops, quant.product_id.id, remaining_qty_on_quant))
+                        need_rereserve = True
+            elif ops.product_id.id:
+                #Check moves with same product
+                qty_to_assign = uom_obj._compute_qty_obj(cr, uid, ops.product_uom_id, ops.product_qty, ops.product_id.uom_id, round=False, context=context)
+                for move_dict in prod2move_ids.get(ops.product_id.id, []):
+                    move = move_dict['move']
+                    for quant in move.reserved_quant_ids:
+                        if not qty_to_assign > 0:
+                            break
+                        if quant.id in quants_in_package_done:
+                            continue
+
+                        #check if the quant is matching the operation details
+                        if ops.package_id:
+                            flag = quant.package_id and bool(package_obj.search(cr, uid, [('id', 'child_of', [ops.package_id.id])], context=context)) or False
+                        else:
+                            flag = not quant.package_id.id
+                        flag = flag and ((ops.lot_id and ops.lot_id.id == quant.lot_id.id) or not ops.lot_id)
+                        flag = flag and (ops.owner_id.id == quant.owner_id.id)
+                        if flag:
+                            max_qty_on_link = min(quant.qty, qty_to_assign)
+                            qty_on_link = _create_link_for_quant(ops.id, quant, max_qty_on_link)
+                            qty_to_assign -= qty_on_link
+                qty_assign_cmp = float_compare(qty_to_assign, 0, precision_rounding=ops.product_id.uom_id.rounding)
+                if qty_assign_cmp > 0:
+                    #qty reserved is less than qty put in operations. We need to create a link but it's deferred after we processed
+                    #all the quants (because they leave no choice on their related move and needs to be processed with higher priority)
+                    still_to_do += [(ops, ops.product_id.id, qty_to_assign)]
+                    need_rereserve = True
+
+        #2) then, process the remaining part
+        all_op_processed = True
+        for ops, product_id, remaining_qty in still_to_do:
+            all_op_processed = all_op_processed and _create_link_for_product(ops.id, product_id, remaining_qty)
+        return (need_rereserve, all_op_processed)
+
+    def picking_recompute_remaining_quantities(self, cr, uid, picking, context=None):
+        need_rereserve = False
+        all_op_processed = True
+        if picking.pack_operation_ids:
+            need_rereserve, all_op_processed = self.recompute_remaining_qty(cr, uid, picking, context=context)
+        return need_rereserve, all_op_processed
+
+    @api.cr_uid_ids_context
+    def do_recompute_remaining_quantities(self, cr, uid, picking_ids, context=None):
+        for picking in self.browse(cr, uid, picking_ids, context=context):
+            if picking.pack_operation_ids:
+                self.recompute_remaining_qty(cr, uid, picking, context=context)
+
+    def _prepare_values_extra_move(self, cr, uid, op, product, remaining_qty, context=None):
         """
-        comment = self._get_comment_invoice(cr, uid, picking)
-        return {
-            'name': (invoice.name or '') + ', ' + (picking.name or ''),
-            'origin': (invoice.origin or '') + ', ' + (picking.name or '') + (picking.origin and (':' + picking.origin) or ''),
-            'comment': (comment and (invoice.comment and invoice.comment + "\n" + comment or comment)) or (invoice.comment and invoice.comment or ''),
-            'date_invoice': context.get('date_inv', False),
-        }
+        Creates an extra move when there is no corresponding original move to be copied
+        """
+        uom_obj = self.pool.get("product.uom")
+        uom_id = product.uom_id.id
+        qty = remaining_qty
+        if op.product_id and op.product_uom_id and op.product_uom_id.id != product.uom_id.id:
+            if op.product_uom_id.factor > product.uom_id.factor: #If the pack operation's is a smaller unit
+                uom_id = op.product_uom_id.id
+                qty = uom_obj._compute_qty_obj(cr, uid, product.uom_id, remaining_qty, op.product_uom_id)
+        picking = op.picking_id
+        res = {
+            'picking_id': picking.id,
+            'location_id': picking.location_id.id,
+            'location_dest_id': picking.location_dest_id.id,
+            'product_id': product.id,
+            'product_uom': uom_id,
+            'product_uom_qty': qty,
+            'name': _('Extra Move: ') + product.name,
+            'state': 'draft',
+            }
+        return res
+
+    def _create_extra_moves(self, cr, uid, picking, context=None):
+        '''This function creates move lines on a picking, at the time of do_transfer, based on
+        unexpected product transfers (or exceeding quantities) found in the pack operations.
+        '''
+        move_obj = self.pool.get('stock.move')
+        operation_obj = self.pool.get('stock.pack.operation')
+        moves = []
+        for op in picking.pack_operation_ids:
+            for product_id, remaining_qty in operation_obj._get_remaining_prod_quantities(cr, uid, op, context=context).items():
+                product = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
+                if float_compare(remaining_qty, 0, precision_rounding=product.uom_id.rounding) > 0:
+                    vals = self._prepare_values_extra_move(cr, uid, op, product, remaining_qty, context=context)
+                    moves.append(move_obj.create(cr, uid, vals, context=context))
+        if moves:
+            move_obj.action_confirm(cr, uid, moves, context=context)
+        return moves
 
-    def _prepare_invoice(self, cr, uid, picking, partner, inv_type, journal_id, context=None):
-        """ Builds the dict containing the values for the invoice
-            @param picking: picking object
-            @param partner: object of the partner to invoice
-            @param inv_type: type of the invoice ('out_invoice', 'in_invoice', ...)
-            @param journal_id: ID of the accounting journal
-            @return: dict that will be used to create the invoice object
+    def rereserve_pick(self, cr, uid, ids, context=None):
         """
-        if isinstance(partner, int):
-            partner = self.pool.get('res.partner').browse(cr, uid, partner, context=context)
-        if inv_type in ('out_invoice', 'out_refund'):
-            account_id = partner.property_account_receivable.id
-            payment_term = partner.property_payment_term.id or False
-        else:
-            account_id = partner.property_account_payable.id
-            payment_term = partner.property_supplier_payment_term.id or False
-        comment = self._get_comment_invoice(cr, uid, picking)
-        invoice_vals = {
-            'name': picking.name,
-            'origin': (picking.name or '') + (picking.origin and (':' + picking.origin) or ''),
-            'type': inv_type,
-            'account_id': account_id,
-            'partner_id': partner.id,
-            'comment': comment,
-            'payment_term': payment_term,
-            'fiscal_position': partner.property_account_position.id,
-            'date_invoice': context.get('date_inv', False),
-            'company_id': picking.company_id.id,
-            'user_id': uid,
-        }
-        cur_id = self.get_currency_id(cr, uid, picking)
-        if cur_id:
-            invoice_vals['currency_id'] = cur_id
-        if journal_id:
-            invoice_vals['journal_id'] = journal_id
-        return invoice_vals
-
-    def _prepare_invoice_line(self, cr, uid, group, picking, move_line, invoice_id,
-        invoice_vals, context=None):
-        """ Builds the dict containing the values for the invoice line
-            @param group: True or False
-            @param picking: picking object
-            @param: move_line: move_line object
-            @param: invoice_id: ID of the related invoice
-            @param: invoice_vals: dict used to created the invoice
-            @return: dict that will be used to create the invoice line
+        This can be used to provide a button that rereserves taking into account the existing pack operations
         """
-        if group:
-            name = (picking.name or '') + '-' + move_line.name
-        else:
-            name = move_line.name
-        origin = move_line.picking_id.name or ''
-        if move_line.picking_id.origin:
-            origin += ':' + move_line.picking_id.origin
-
-        if invoice_vals['type'] in ('out_invoice', 'out_refund'):
-            account_id = move_line.product_id.property_account_income.id
-            if not account_id:
-                account_id = move_line.product_id.categ_id.\
-                        property_account_income_categ.id
+        for pick in self.browse(cr, uid, ids, context=context):
+            self.rereserve_quants(cr, uid, pick, move_ids = [x.id for x in pick.move_lines], context=context)
+
+    def rereserve_quants(self, cr, uid, picking, move_ids=[], context=None):
+        """ Unreserve quants then try to reassign quants."""
+        stock_move_obj = self.pool.get('stock.move')
+        if not move_ids:
+            self.do_unreserve(cr, uid, [picking.id], context=context)
+            self.action_assign(cr, uid, [picking.id], context=context)
         else:
-            account_id = move_line.product_id.property_account_expense.id
-            if not account_id:
-                account_id = move_line.product_id.categ_id.\
-                        property_account_expense_categ.id
-        if invoice_vals['fiscal_position']:
-            fp_obj = self.pool.get('account.fiscal.position')
-            fiscal_position = fp_obj.browse(cr, uid, invoice_vals['fiscal_position'], context=context)
-            account_id = fp_obj.map_account(cr, uid, fiscal_position, account_id)
-        # set UoS if it's a sale and the picking doesn't have one
-        uos_id = move_line.product_uos and move_line.product_uos.id or False
-        if not uos_id and invoice_vals['type'] in ('out_invoice', 'out_refund'):
-            uos_id = move_line.product_uom.id
-
-        return {
-            'name': name,
-            'origin': origin,
-            'invoice_id': invoice_id,
-            'uos_id': uos_id,
-            'product_id': move_line.product_id.id,
-            'account_id': account_id,
-            'price_unit': self._get_price_unit_invoice(cr, uid, move_line, invoice_vals['type']),
-            'discount': self._get_discount_invoice(cr, uid, move_line),
-            'quantity': move_line.product_uos_qty or move_line.product_qty,
-            'invoice_line_tax_id': [(6, 0, self._get_taxes_invoice(cr, uid, move_line, invoice_vals['type']))],
-            'account_analytic_id': self._get_account_analytic_invoice(cr, uid, picking, move_line),
-        }
+            stock_move_obj.do_unreserve(cr, uid, move_ids, context=context)
+            stock_move_obj.action_assign(cr, uid, move_ids, context=context)
 
-    def action_invoice_create(self, cr, uid, ids, journal_id=False,
-            group=False, type='out_invoice', context=None):
-        """ Creates invoice based on the invoice state selected for picking.
-        @param journal_id: Id of journal
-        @param group: Whether to create a group invoice or not
-        @param type: Type invoice to be created
-        @return: Ids of created invoices for the pickings
-        """
-        if context is None:
+    @api.cr_uid_ids_context
+    def do_enter_transfer_details(self, cr, uid, picking, context=None):
+        if not context:
             context = {}
 
-        invoice_obj = self.pool.get('account.invoice')
-        invoice_line_obj = self.pool.get('account.invoice.line')
-        partner_obj = self.pool.get('res.partner')
-        invoices_group = {}
-        res = {}
-        inv_type = type
-        for picking in self.browse(cr, uid, ids, context=context):
-            if picking.invoice_state != '2binvoiced':
-                continue
-            partner = self._get_partner_to_invoice(cr, uid, picking, context=context)
-            if isinstance(partner, int):
-                partner = partner_obj.browse(cr, uid, [partner], context=context)[0]
-            if not partner:
-                raise osv.except_osv(_('Error, no partner!'),
-                    _('Please put a partner on the picking list if you want to generate invoice.'))
-
-            if not inv_type:
-                inv_type = self._get_invoice_type(picking)
-
-            invoice_vals = self._prepare_invoice(cr, uid, picking, partner, inv_type, journal_id, context=context)
-            if group and partner.id in invoices_group:
-                invoice_id = invoices_group[partner.id]
-                invoice = invoice_obj.browse(cr, uid, invoice_id)
-                invoice_vals_group = self._prepare_invoice_group(cr, uid, picking, partner, invoice, context=context)
-                invoice_obj.write(cr, uid, [invoice_id], invoice_vals_group, context=context)
-            else:
-                invoice_id = invoice_obj.create(cr, uid, invoice_vals, context=context)
-                invoices_group[partner.id] = invoice_id
-            res[picking.id] = invoice_id
-            for move_line in picking.move_lines:
-                if move_line.state == 'cancel':
-                    continue
-                if move_line.scrapped:
-                    # do no invoice scrapped products
-                    continue
-                vals = self._prepare_invoice_line(cr, uid, group, picking, move_line,
-                                invoice_id, invoice_vals, context=context)
-                if vals:
-                    invoice_line_id = invoice_line_obj.create(cr, uid, vals, context=context)
-                    self._invoice_line_hook(cr, uid, move_line, invoice_line_id)
-
-            invoice_obj.button_compute(cr, uid, [invoice_id], context=context,
-                    set_total=(inv_type in ('in_invoice', 'in_refund')))
-            self.write(cr, uid, [picking.id], {
-                'invoice_state': 'invoiced',
-                }, context=context)
-            self._invoice_hook(cr, uid, picking, invoice_id)
-        self.write(cr, uid, res.keys(), {
-            'invoice_state': 'invoiced',
-            }, context=context)
-        return res
+        context.update({
+            'active_model': self._name,
+            'active_ids': picking,
+            'active_id': len(picking) and picking[0] or False
+        })
 
-    def test_done(self, cr, uid, ids, context=None):
-        """ Test whether the move lines are done or not.
-        @return: True or False
-        """
-        ok = False
-        for pick in self.browse(cr, uid, ids, context=context):
-            if not pick.move_lines:
-                return True
-            for move in pick.move_lines:
-                if move.state not in ('cancel','done'):
-                    return False
-                if move.state=='done':
-                    ok = True
-        return ok
+        created_id = self.pool['stock.transfer_details'].create(cr, uid, {'picking_id': len(picking) and picking[0] or False}, context)
+        return self.pool['stock.transfer_details'].wizard_view(cr, uid, created_id, context)
 
-    def test_cancel(self, cr, uid, ids, context=None):
-        """ Test whether the move lines are canceled or not.
-        @return: True or False
-        """
-        for pick in self.browse(cr, uid, ids, context=context):
-            for move in pick.move_lines:
-                if move.state not in ('cancel',):
-                    return False
-        return True
 
-    def allow_cancel(self, cr, uid, ids, context=None):
-        for pick in self.browse(cr, uid, ids, context=context):
-            if not pick.move_lines:
-                return True
-            for move in pick.move_lines:
-                if move.state == 'done':
-                    raise osv.except_osv(_('Error!'), _('You cannot cancel the picking as some moves have been done. You should cancel the picking lines.'))
+    @api.cr_uid_ids_context
+    def do_transfer(self, cr, uid, picking_ids, context=None):
+        """
+            If no pack operation, we do simple action_done of the picking
+            Otherwise, do the pack operations
+        """
+        if not context:
+            context = {}
+        stock_move_obj = self.pool.get('stock.move')
+        for picking in self.browse(cr, uid, picking_ids, context=context):
+            if not picking.pack_operation_ids:
+                self.action_done(cr, uid, [picking.id], context=context)
+                continue
+            else:
+                need_rereserve, all_op_processed = self.picking_recompute_remaining_quantities(cr, uid, picking, context=context)
+                #create extra moves in the picking (unexpected product moves coming from pack operations)
+                todo_move_ids = []
+                if not all_op_processed:
+                    todo_move_ids += self._create_extra_moves(cr, uid, picking, context=context)
+
+                picking.refresh()
+                #split move lines eventually
+
+                toassign_move_ids = []
+                for move in picking.move_lines:
+                    remaining_qty = move.remaining_qty
+                    if move.state in ('done', 'cancel'):
+                        #ignore stock moves cancelled or already done
+                        continue
+                    elif move.state == 'draft':
+                        toassign_move_ids.append(move.id)
+                    if float_compare(remaining_qty, 0,  precision_rounding = move.product_id.uom_id.rounding) == 0:
+                        if move.state in ('draft', 'assigned', 'confirmed'):
+                            todo_move_ids.append(move.id)
+                    elif float_compare(remaining_qty,0, precision_rounding = move.product_id.uom_id.rounding) > 0 and \
+                                float_compare(remaining_qty, move.product_qty, precision_rounding = move.product_id.uom_id.rounding) < 0:
+                        new_move = stock_move_obj.split(cr, uid, move, remaining_qty, context=context)
+                        todo_move_ids.append(move.id)
+                        #Assign move as it was assigned before
+                        toassign_move_ids.append(new_move)
+                if need_rereserve or not all_op_processed: 
+                    if not picking.location_id.usage in ("supplier", "production", "inventory"):
+                        self.rereserve_quants(cr, uid, picking, move_ids=todo_move_ids, context=context)
+                    self.do_recompute_remaining_quantities(cr, uid, [picking.id], context=context)
+                if todo_move_ids and not context.get('do_only_split'):
+                    self.pool.get('stock.move').action_done(cr, uid, todo_move_ids, context=context)
+                elif context.get('do_only_split'):
+                    context = dict(context, split=todo_move_ids)
+            picking.refresh()
+            self._create_backorder(cr, uid, picking, context=context)
+            if toassign_move_ids:
+                stock_move_obj.action_assign(cr, uid, toassign_move_ids, context=context)
         return True
 
-    def unlink(self, cr, uid, ids, context=None):
-        move_obj = self.pool.get('stock.move')
+    @api.cr_uid_ids_context
+    def do_split(self, cr, uid, picking_ids, context=None):
+        """ just split the picking (create a backorder) without making it 'done' """
         if context is None:
             context = {}
-        for pick in self.browse(cr, uid, ids, context=context):
-            if pick.state in ['done','cancel']:
-                # retrieve the string value of field in user's language
-                state = dict(self.fields_get(cr, uid, context=context)['state']['selection']).get(pick.state, pick.state)
-                raise osv.except_osv(_('Error!'), _('You cannot remove the picking which is in %s state!')%(state,))
-            else:
-                ids2 = [move.id for move in pick.move_lines]
-                ctx = context.copy()
-                ctx.update({'call_unlink':True})
-                if pick.state != 'draft':
-                    #Cancelling the move in order to affect Virtual stock of product
-                    move_obj.action_cancel(cr, uid, ids2, ctx)
-                #Removing the move
-                move_obj.unlink(cr, uid, ids2, ctx)
-
-        return super(stock_picking, self).unlink(cr, uid, ids, context=context)
+        ctx = context.copy()
+        ctx['do_only_split'] = True
+        return self.do_transfer(cr, uid, picking_ids, context=ctx)
 
-    # FIXME: needs refactoring, this code is partially duplicated in stock_move.do_partial()!
-    def do_partial(self, cr, uid, ids, partial_datas, context=None):
-        """ Makes partial picking and moves done.
-        @param partial_datas : Dictionary containing details of partial picking
-                          like partner_id, partner_id, delivery_date,
-                          delivery moves with product_id, product_qty, uom
-        @return: Dictionary of values
-        """
+    def get_next_picking_for_ui(self, cr, uid, context=None):
+        """ returns the next pickings to process. Used in the barcode scanner UI"""
         if context is None:
             context = {}
-        else:
-            context = dict(context)
-        res = {}
-        move_obj = self.pool.get('stock.move')
-        product_obj = self.pool.get('product.product')
-        currency_obj = self.pool.get('res.currency')
-        uom_obj = self.pool.get('product.uom')
-        sequence_obj = self.pool.get('ir.sequence')
-        wf_service = netsvc.LocalService("workflow")
-        for pick in self.browse(cr, uid, ids, context=context):
-            new_picking = None
-            complete, too_many, too_few = [], [], []
-            move_product_qty, prodlot_ids, product_avail, partial_qty, uos_qty, product_uoms = {}, {}, {}, {}, {}, {}
-            for move in pick.move_lines:
-                if move.state in ('done', 'cancel'):
-                    continue
-                partial_data = partial_datas.get('move%s'%(move.id), {})
-                product_qty = partial_data.get('product_qty',0.0)
-                move_product_qty[move.id] = product_qty
-                product_uom = partial_data.get('product_uom', move.product_uom.id)
-                product_price = partial_data.get('product_price',0.0)
-                product_currency = partial_data.get('product_currency',False)
-                prodlot_id = partial_data.get('prodlot_id')
-                prodlot_ids[move.id] = prodlot_id
-                product_uoms[move.id] = product_uom
-                partial_qty[move.id] = uom_obj._compute_qty(cr, uid, product_uoms[move.id], product_qty, move.product_uom.id)
-                uos_qty[move.id] = move.product_id._compute_uos_qty(product_uom, product_qty, move.product_uos) if product_qty else 0.0
-                if move.product_qty == partial_qty[move.id]:
-                    complete.append(move)
-                elif move.product_qty > partial_qty[move.id]:
-                    too_few.append(move)
-                else:
-                    too_many.append(move)
-
-                # Average price computation
-                if (pick.type == 'in') and (move.product_id.cost_method == 'average'):
-                    product = product_obj.browse(cr, uid, move.product_id.id)
-                    move_currency_id = move.company_id.currency_id.id
-                    context['currency_id'] = move_currency_id
-                    qty = uom_obj._compute_qty(cr, uid, product_uom, product_qty, product.uom_id.id)
-
-                    if product.id not in product_avail:
-                        # keep track of stock on hand including processed lines not yet marked as done
-                        product_avail[product.id] = product.qty_available
-
-                    if qty > 0:
-                        new_price = currency_obj.compute(cr, uid, product_currency,
-                                move_currency_id, product_price, round=False)
-                        new_price = uom_obj._compute_price(cr, uid, product_uom, new_price,
-                                product.uom_id.id)
-                        if product_avail[product.id] <= 0:
-                            product_avail[product.id] = 0
-                            new_std_price = new_price
-                        else:
-                            # Get the standard price
-                            amount_unit = product.price_get('standard_price', context=context)[product.id]
-                            new_std_price = ((amount_unit * product_avail[product.id])\
-                                + (new_price * qty))/(product_avail[product.id] + qty)
-                        # Write the field according to price type field
-                        product_obj.write(cr, uid, [product.id], {'standard_price': new_std_price})
-
-                        # Record the values that were chosen in the wizard, so they can be
-                        # used for inventory valuation if real-time valuation is enabled.
-                        move_obj.write(cr, uid, [move.id],
-                                {'price_unit': product_price,
-                                 'price_currency_id': product_currency})
-
-                        product_avail[product.id] += qty
-
-            # every line of the picking is empty, do not generate anything
-            empty_picking = not any(q for q in move_product_qty.values() if q > 0)
-
-            for move in too_few:
-                product_qty = move_product_qty[move.id]
-                if not new_picking and not empty_picking:
-                    new_picking_name = pick.name
-                    self.write(cr, uid, [pick.id], 
-                               {'name': sequence_obj.get(cr, uid,
-                                            'stock.picking.%s'%(pick.type)),
-                               })
-                    pick.refresh()
-                    new_picking = self.copy(cr, uid, pick.id,
-                            {
-                                'name': new_picking_name,
-                                'move_lines' : [],
-                                'state':'draft',
-                            })
-                if product_qty != 0:
-                    defaults = {
-                            'product_qty' : product_qty,
-                            'product_uos_qty': uos_qty[move.id],
-                            'picking_id' : new_picking,
-                            'state': 'assigned',
-                            'move_dest_id': False,
-                            'price_unit': move.price_unit,
-                            'product_uom': product_uoms[move.id]
-                    }
-                    prodlot_id = prodlot_ids[move.id]
-                    if prodlot_id:
-                        defaults.update(prodlot_id=prodlot_id)
-                    move_obj.copy(cr, uid, move.id, defaults)
-                move_obj.write(cr, uid, [move.id],
-                        {
-                            'product_qty': move.product_qty - partial_qty[move.id],
-                            'product_uos_qty': move.product_uos_qty - uos_qty[move.id],
-                            'prodlot_id': False,
-                            'tracking_id': False,
-                        })
-
-            if new_picking:
-                move_obj.write(cr, uid, [c.id for c in complete], {'picking_id': new_picking})
-            for move in complete:
-                defaults = {'product_uom': product_uoms[move.id], 'product_qty': move_product_qty[move.id]}
-                if prodlot_ids.get(move.id):
-                    defaults.update({'prodlot_id': prodlot_ids[move.id]})
-                move_obj.write(cr, uid, [move.id], defaults)
-            for move in too_many:
-                product_qty = move_product_qty[move.id]
-                defaults = {
-                    'product_qty' : product_qty,
-                    'product_uos_qty': uos_qty[move.id],
-                    'product_uom': product_uoms[move.id]
-                }
-                prodlot_id = prodlot_ids.get(move.id)
-                if prodlot_ids.get(move.id):
-                    defaults.update(prodlot_id=prodlot_id)
-                if new_picking:
-                    defaults.update(picking_id=new_picking)
-                move_obj.write(cr, uid, [move.id], defaults)
-
-            # At first we confirm the new picking (if necessary)
-            if new_picking:
-                wf_service.trg_validate(uid, 'stock.picking', new_picking, 'button_confirm', cr)
-                # Then we finish the good picking
-                self.write(cr, uid, [pick.id], {'backorder_id': new_picking})
-                self.action_move(cr, uid, [new_picking], context=context)
-                wf_service.trg_validate(uid, 'stock.picking', new_picking, 'button_done', cr)
-                wf_service.trg_write(uid, 'stock.picking', pick.id, cr)
-                delivered_pack_id = new_picking
-                self.message_post(cr, uid, new_picking, body=_("Back order <em>%s</em> has been <b>created</b>.") % (pick.name), context=context)
-            elif empty_picking:
-                delivered_pack_id = pick.id
-            else:
-                self.action_move(cr, uid, [pick.id], context=context)
-                wf_service.trg_validate(uid, 'stock.picking', pick.id, 'button_done', cr)
-                delivered_pack_id = pick.id
+        domain = [('state', 'in', ('assigned', 'partially_available'))]
+        if context.get('default_picking_type_id'):
+            domain.append(('picking_type_id', '=', context['default_picking_type_id']))
+        return self.search(cr, uid, domain, context=context)
+
+    def action_done_from_ui(self, cr, uid, picking_id, context=None):
+        """ called when button 'done' is pushed in the barcode scanner UI """
+        #write qty_done into field product_qty for every package_operation before doing the transfer
+        pack_op_obj = self.pool.get('stock.pack.operation')
+        for operation in self.browse(cr, uid, picking_id, context=context).pack_operation_ids:
+            pack_op_obj.write(cr, uid, operation.id, {'product_qty': operation.qty_done}, context=context)
+        self.do_transfer(cr, uid, [picking_id], context=context)
+        #return id of next picking to work on
+        return self.get_next_picking_for_ui(cr, uid, context=context)
+
+    @api.cr_uid_ids_context
+    def action_pack(self, cr, uid, picking_ids, operation_filter_ids=None, context=None):
+        """ Create a package with the current pack_operation_ids of the picking that aren't yet in a pack.
+        Used in the barcode scanner UI and the normal interface as well. 
+        operation_filter_ids is used by barcode scanner interface to specify a subset of operation to pack"""
+        if operation_filter_ids == None:
+            operation_filter_ids = []
+        stock_operation_obj = self.pool.get('stock.pack.operation')
+        package_obj = self.pool.get('stock.quant.package')
+        stock_move_obj = self.pool.get('stock.move')
+        for picking_id in picking_ids:
+            operation_search_domain = [('picking_id', '=', picking_id), ('result_package_id', '=', False)]
+            if operation_filter_ids != []:
+                operation_search_domain.append(('id', 'in', operation_filter_ids))
+            operation_ids = stock_operation_obj.search(cr, uid, operation_search_domain, context=context)
+            pack_operation_ids = []
+            if operation_ids:
+                for operation in stock_operation_obj.browse(cr, uid, operation_ids, context=context):
+                    #If we haven't done all qty in operation, we have to split into 2 operation
+                    op = operation
+                    if (operation.qty_done < operation.product_qty):
+                        new_operation = stock_operation_obj.copy(cr, uid, operation.id, {'product_qty': operation.qty_done,'qty_done': operation.qty_done}, context=context)
+                        stock_operation_obj.write(cr, uid, operation.id, {'product_qty': operation.product_qty - operation.qty_done,'qty_done': 0, 'lot_id': False}, context=context)
+                        op = stock_operation_obj.browse(cr, uid, new_operation, context=context)
+                    pack_operation_ids.append(op.id)
+                    if op.product_id and op.location_id and op.location_dest_id:
+                        stock_move_obj.check_tracking_product(cr, uid, op.product_id, op.lot_id.id, op.location_id, op.location_dest_id, context=context)
+                package_id = package_obj.create(cr, uid, {}, context=context)
+                stock_operation_obj.write(cr, uid, pack_operation_ids, {'result_package_id': package_id}, context=context)
+        return True
 
-            delivered_pack = self.browse(cr, uid, delivered_pack_id, context=context)
-            res[pick.id] = {'delivered_picking': delivered_pack.id or False}
+    def process_product_id_from_ui(self, cr, uid, picking_id, product_id, op_id, increment=True, context=None):
+        return self.pool.get('stock.pack.operation')._search_and_increment(cr, uid, picking_id, [('product_id', '=', product_id),('id', '=', op_id)], increment=increment, context=context)
 
-        return res
-    
-    # views associated to each picking type
-    _VIEW_LIST = {
-        'out': 'view_picking_out_form',
-        'in': 'view_picking_in_form',
-        'internal': 'view_picking_form',
-    }
-    def _get_view_id(self, cr, uid, type):
-        """Get the view id suiting the given type
-        
-        @param type: the picking type as a string
-        @return: view i, or False if no view found
-        """
-        res = self.pool.get('ir.model.data').get_object_reference(cr, uid, 
-            'stock', self._VIEW_LIST.get(type, 'view_picking_form'))            
-        return res and res[1] or False
+    def process_barcode_from_ui(self, cr, uid, picking_id, barcode_str, visible_op_ids, context=None):
+        '''This function is called each time there barcode scanner reads an input'''
+        lot_obj = self.pool.get('stock.production.lot')
+        package_obj = self.pool.get('stock.quant.package')
+        product_obj = self.pool.get('product.product')
+        stock_operation_obj = self.pool.get('stock.pack.operation')
+        stock_location_obj = self.pool.get('stock.location')
+        answer = {'filter_loc': False, 'operation_id': False}
+        #check if the barcode correspond to a location
+        matching_location_ids = stock_location_obj.search(cr, uid, [('loc_barcode', '=', barcode_str)], context=context)
+        if matching_location_ids:
+            #if we have a location, return immediatly with the location name
+            location = stock_location_obj.browse(cr, uid, matching_location_ids[0], context=None)
+            answer['filter_loc'] = stock_location_obj._name_get(cr, uid, location, context=None)
+            answer['filter_loc_id'] = matching_location_ids[0]
+            return answer
+        #check if the barcode correspond to a product
+        matching_product_ids = product_obj.search(cr, uid, ['|', ('ean13', '=', barcode_str), ('default_code', '=', barcode_str)], context=context)
+        if matching_product_ids:
+            op_id = stock_operation_obj._search_and_increment(cr, uid, picking_id, [('product_id', '=', matching_product_ids[0])], filter_visible=True, visible_op_ids=visible_op_ids, increment=True, context=context)
+            answer['operation_id'] = op_id
+            return answer
+        #check if the barcode correspond to a lot
+        matching_lot_ids = lot_obj.search(cr, uid, [('name', '=', barcode_str)], context=context)
+        if matching_lot_ids:
+            lot = lot_obj.browse(cr, uid, matching_lot_ids[0], context=context)
+            op_id = stock_operation_obj._search_and_increment(cr, uid, picking_id, [('product_id', '=', lot.product_id.id), ('lot_id', '=', lot.id)], filter_visible=True, visible_op_ids=visible_op_ids, increment=True, context=context)
+            answer['operation_id'] = op_id
+            return answer
+        #check if the barcode correspond to a package
+        matching_package_ids = package_obj.search(cr, uid, [('name', '=', barcode_str)], context=context)
+        if matching_package_ids:
+            op_id = stock_operation_obj._search_and_increment(cr, uid, picking_id, [('package_id', '=', matching_package_ids[0])], filter_visible=True, visible_op_ids=visible_op_ids, increment=True, context=context)
+            answer['operation_id'] = op_id
+            return answer
+        return answer
 
 
 class stock_production_lot(osv.osv):
-
-    def name_get(self, cr, uid, ids, context=None):
-        if not ids:
-            return []
-        reads = self.read(cr, uid, ids, ['name', 'prefix', 'ref'], context)
-        res = []
-        for record in reads:
-            name = record['name']
-            prefix = record['prefix']
-            if prefix:
-                name = prefix + '/' + name
-            if record['ref']:
-                name = '%s [%s]' % (name, record['ref'])
-            res.append((record['id'], name))
-        return res
-
-    def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=100):
-        args = args or []
-        ids = []
-        if name:
-            ids = self.search(cr, uid, [('prefix', '=', name)] + args, limit=limit, context=context)
-            if not ids:
-                ids = self.search(cr, uid, [('name', operator, name)] + args, limit=limit, context=context)
-        else:
-            ids = self.search(cr, uid, args, limit=limit, context=context)
-        return self.name_get(cr, uid, ids, context)
-
     _name = 'stock.production.lot'
-    _description = 'Serial Number'
-
-    def _get_stock(self, cr, uid, ids, field_name, arg, context=None):
-        """ Gets stock of products for locations
-        @return: Dictionary of values
-        """
-        if context is None:
-            context = {}
-        if 'location_id' not in context:
-            locations = self.pool.get('stock.location').search(cr, uid, [('usage', '=', 'internal')], context=context)
-        else:
-            locations = context['location_id'] and [context['location_id']] or []
-
-        if isinstance(ids, (int, long)):
-            ids = [ids]
-
-        res = {}.fromkeys(ids, 0.0)
-        if locations:
-            cr.execute('''select
-                    prodlot_id,
-                    sum(qty)
-                from
-                    stock_report_prodlots
-                where
-                    location_id IN %s and prodlot_id IN %s group by prodlot_id''',(tuple(locations),tuple(ids),))
-            res.update(dict(cr.fetchall()))
-
-        return res
-
-    def _stock_search(self, cr, uid, obj, name, args, context=None):
-        """ Searches Ids of products
-        @return: Ids of locations
-        """
-        locations = self.pool.get('stock.location').search(cr, uid, [('usage', '=', 'internal')])
-        cr.execute('''select
-                prodlot_id,
-                sum(qty)
-            from
-                stock_report_prodlots
-            where
-                location_id IN %s group by prodlot_id
-            having  sum(qty) '''+ str(args[0][1]) + str(args[0][2]),(tuple(locations),))
-        res = cr.fetchall()
-        ids = [('id', 'in', map(lambda x: x[0], res))]
-        return ids
-
+    _inherit = ['mail.thread']
+    _description = 'Lot/Serial'
     _columns = {
-        'name': fields.char('Serial Number', size=64, required=True, help="Unique Serial Number, will be displayed as: PREFIX/SERIAL [INT_REF]"),
-        'ref': fields.char('Internal Reference', size=256, help="Internal reference number in case it differs from the manufacturer's serial number"),
-        'prefix': fields.char('Prefix', size=64, help="Optional prefix to prepend when displaying this serial number: PREFIX/SERIAL [INT_REF]"),
+        'name': fields.char('Serial Number', required=True, help="Unique Serial Number"),
+        'ref': fields.char('Internal Reference', help="Internal reference number in case it differs from the manufacturer's serial number"),
         'product_id': fields.many2one('product.product', 'Product', required=True, domain=[('type', '<>', 'service')]),
-        'date': fields.datetime('Creation Date', required=True),
-        'stock_available': fields.function(_get_stock, fnct_search=_stock_search, type="float", string="Available", select=True,
-            help="Current quantity of products with this Serial Number available in company warehouses",
-            digits_compute=dp.get_precision('Product Unit of Measure')),
-        'revisions': fields.one2many('stock.production.lot.revision', 'lot_id', 'Revisions'),
-        'company_id': fields.many2one('res.company', 'Company', select=True),
-        'move_ids': fields.one2many('stock.move', 'prodlot_id', 'Moves for this serial number', readonly=True),
+        'quant_ids': fields.one2many('stock.quant', 'lot_id', 'Quants', readonly=True),
+        'create_date': fields.datetime('Creation Date'),
     }
     _defaults = {
-        'date': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
         'name': lambda x, y, z, c: x.pool.get('ir.sequence').get(y, z, 'stock.lot.serial'),
         'product_id': lambda x, y, z, c: c.get('product_id', False),
     }
     _sql_constraints = [
-        ('name_ref_uniq', 'unique (name, ref, product_id, company_id)', 'The combination of Serial Number, internal reference, Product and Company must be unique !'),
+        ('name_ref_uniq', 'unique (name, ref, product_id)', 'The combination of serial number, internal reference and product must be unique !'),
     ]
+
     def action_traceability(self, cr, uid, ids, context=None):
-        """ It traces the information of a product
+        """ It traces the information of lots
         @param self: The object pointer.
         @param cr: A database cursor
         @param uid: ID of the user currently logged in
@@ -1503,84 +1543,42 @@ class stock_production_lot(osv.osv):
         @param context: A standard dictionary
         @return: A dictionary of values
         """
-        value=self.pool.get('action.traceability').action_traceability(cr,uid,ids,context)
-        return value
-
-    def copy(self, cr, uid, id, default=None, context=None):
-        context = context or {}
-        default = default and default.copy() or {}
-        default.update(date=time.strftime('%Y-%m-%d %H:%M:%S'), move_ids=[])
-        return super(stock_production_lot, self).copy(cr, uid, id, default=default, context=context)
-
-stock_production_lot()
-
-class stock_production_lot_revision(osv.osv):
-    _name = 'stock.production.lot.revision'
-    _description = 'Serial Number Revision'
-
-    _columns = {
-        'name': fields.char('Revision Name', size=64, required=True),
-        'description': fields.text('Description'),
-        'date': fields.date('Revision Date'),
-        'indice': fields.char('Revision Number', size=16),
-        'author_id': fields.many2one('res.users', 'Author'),
-        'lot_id': fields.many2one('stock.production.lot', 'Serial Number', select=True, ondelete='cascade'),
-        'company_id': fields.related('lot_id','company_id',type='many2one',relation='res.company',string='Company', store=True, readonly=True),
-    }
-
-    _defaults = {
-        'author_id': lambda x, y, z, c: z,
-        'date': fields.date.context_today,
-    }
+        quant_obj = self.pool.get("stock.quant")
+        quants = quant_obj.search(cr, uid, [('lot_id', 'in', ids)], context=context)
+        moves = set()
+        for quant in quant_obj.browse(cr, uid, quants, context=context):
+            moves |= {move.id for move in quant.history_ids}
+        if moves:
+            return {
+                'domain': "[('id','in',[" + ','.join(map(str, list(moves))) + "])]",
+                'name': _('Traceability'),
+                'view_mode': 'tree,form',
+                'view_type': 'form',
+                'context': {'tree_view_ref': 'stock.view_move_tree'},
+                'res_model': 'stock.move',
+                'type': 'ir.actions.act_window',
+                    }
+        return False
 
-stock_production_lot_revision()
 
 # ----------------------------------------------------
 # Move
 # ----------------------------------------------------
 
-#
-# Fields:
-#   location_dest_id is only used for predicting futur stocks
-#
 class stock_move(osv.osv):
-
-    def _getSSCC(self, cr, uid, context=None):
-        cr.execute('select id from stock_tracking where create_uid=%s order by id desc limit 1', (uid,))
-        res = cr.fetchone()
-        return (res and res[0]) or False
-
     _name = "stock.move"
     _description = "Stock Move"
     _order = 'date_expected desc, id'
     _log_create = False
 
-    def action_partial_move(self, cr, uid, ids, context=None):
-        if context is None: context = {}
-        if context.get('active_model') != self._name:
-            context.update(active_ids=ids, active_model=self._name)
-        partial_id = self.pool.get("stock.partial.move").create(
-            cr, uid, {}, context=context)
-        return {
-            'name':_("Products to Process"),
-            'view_mode': 'form',
-            'view_id': False,
-            'view_type': 'form',
-            'res_model': 'stock.partial.move',
-            'res_id': partial_id,
-            'type': 'ir.actions.act_window',
-            'nodestroy': True,
-            'target': 'new',
-            'domain': '[]',
-            'context': context
-        }
-
+    def get_price_unit(self, cr, uid, move, context=None):
+        """ Returns the unit price to store on the quant """
+        return move.price_unit or move.product_id.standard_price
 
     def name_get(self, cr, uid, ids, context=None):
         res = []
         for line in self.browse(cr, uid, ids, context=context):
-            name = line.location_id.name+' > '+line.location_dest_id.name
-            # optional prefixes
+            name = line.location_id.name + ' > ' + line.location_dest_id.name
             if line.product_id.code:
                 name = line.product_id.code + ': ' + name
             if line.picking_id.origin:
@@ -1588,42 +1586,122 @@ class stock_move(osv.osv):
             res.append((line.id, name))
         return res
 
-    def _check_tracking(self, cr, uid, ids, context=None):
-        """ Checks if serial number is assigned to stock move or not.
-        @return: True or False
-        """
+    def _quantity_normalize(self, cr, uid, ids, name, args, context=None):
+        uom_obj = self.pool.get('product.uom')
+        res = {}
+        for m in self.browse(cr, uid, ids, context=context):
+            res[m.id] = uom_obj._compute_qty_obj(cr, uid, m.product_uom, m.product_uom_qty, m.product_id.uom_id, context=context)
+        return res
+
+    def _get_remaining_qty(self, cr, uid, ids, field_name, args, context=None):
+        uom_obj = self.pool.get('product.uom')
+        res = {}
         for move in self.browse(cr, uid, ids, context=context):
-            if not move.prodlot_id and \
-               (move.state == 'done' and \
-               ( \
-                   (move.product_id.track_production and move.location_id.usage == 'production') or \
-                   (move.product_id.track_production and move.location_dest_id.usage == 'production') or \
-                   (move.product_id.track_incoming and move.location_id.usage == 'supplier') or \
-                   (move.product_id.track_outgoing and move.location_dest_id.usage == 'customer') or \
-                   (move.product_id.track_incoming and move.location_id.usage == 'inventory') \
-               )):
-                return False
-        return True
+            qty = move.product_qty
+            for record in move.linked_move_operation_ids:
+                qty -= record.qty
+            # Keeping in product default UoM
+            res[move.id] = qty
+        return res
 
-    def _check_product_lot(self, cr, uid, ids, context=None):
-        """ Checks whether move is done or not and production lot is assigned to that move.
-        @return: True or False
-        """
+    def _get_lot_ids(self, cr, uid, ids, field_name, args, context=None):
+        res = dict.fromkeys(ids, False)
         for move in self.browse(cr, uid, ids, context=context):
-            if move.prodlot_id and move.state == 'done' and (move.prodlot_id.product_id.id != move.product_id.id):
-                return False
-        return True
+            if move.state == 'done':
+                res[move.id] = [q.lot_id.id for q in move.quant_ids if q.lot_id]
+            else:
+                res[move.id] = [q.lot_id.id for q in move.reserved_quant_ids if q.lot_id]
+        return res
+
+    def _get_product_availability(self, cr, uid, ids, field_name, args, context=None):
+        quant_obj = self.pool.get('stock.quant')
+        res = dict.fromkeys(ids, False)
+        for move in self.browse(cr, uid, ids, context=context):
+            if move.state == 'done':
+                res[move.id] = move.product_qty
+            else:
+                sublocation_ids = self.pool.get('stock.location').search(cr, uid, [('id', 'child_of', [move.location_id.id])], context=context)
+                quant_ids = quant_obj.search(cr, uid, [('location_id', 'in', sublocation_ids), ('product_id', '=', move.product_id.id), ('reservation_id', '=', False)], context=context)
+                availability = 0
+                for quant in quant_obj.browse(cr, uid, quant_ids, context=context):
+                    availability += quant.qty
+                res[move.id] = min(move.product_qty, availability)
+        return res
+
+    def _get_string_qty_information(self, cr, uid, ids, field_name, args, context=None):
+        settings_obj = self.pool.get('stock.config.settings')
+        uom_obj = self.pool.get('product.uom')
+        res = dict.fromkeys(ids, '')
+        for move in self.browse(cr, uid, ids, context=context):
+            if move.state in ('draft', 'done', 'cancel') or move.location_id.usage != 'internal':
+                res[move.id] = ''  # 'not applicable' or 'n/a' could work too
+                continue
+            total_available = min(move.product_qty, move.reserved_availability + move.availability)
+            total_available = uom_obj._compute_qty_obj(cr, uid, move.product_id.uom_id, total_available, move.product_uom, context=context)
+            info = str(total_available)
+            #look in the settings if we need to display the UoM name or not
+            config_ids = settings_obj.search(cr, uid, [], limit=1, order='id DESC', context=context)
+            if config_ids:
+                stock_settings = settings_obj.browse(cr, uid, config_ids[0], context=context)
+                if stock_settings.group_uom:
+                    info += ' ' + move.product_uom.name
+            if move.reserved_availability:
+                if move.reserved_availability != total_available:
+                    #some of the available quantity is assigned and some are available but not reserved
+                    reserved_available = uom_obj._compute_qty_obj(cr, uid, move.product_id.uom_id, move.reserved_availability, move.product_uom, context=context)
+                    info += _(' (%s reserved)') % str(reserved_available)
+                else:
+                    #all available quantity is assigned
+                    info += _(' (reserved)')
+            res[move.id] = info
+        return res
+
+    def _get_reserved_availability(self, cr, uid, ids, field_name, args, context=None):
+        res = dict.fromkeys(ids, 0)
+        for move in self.browse(cr, uid, ids, context=context):
+            res[move.id] = sum([quant.qty for quant in move.reserved_quant_ids])
+        return res
+
+    def _get_move(self, cr, uid, ids, context=None):
+        res = set()
+        for quant in self.browse(cr, uid, ids, context=context):
+            if quant.reservation_id:
+                res.add(quant.reservation_id.id)
+        return list(res)
+
+    def _get_move_ids(self, cr, uid, ids, context=None):
+        res = []
+        for picking in self.browse(cr, uid, ids, context=context):
+            res += [x.id for x in picking.move_lines]
+        return res
+
+    def _get_moves_from_prod(self, cr, uid, ids, context=None):
+        if ids:
+            return self.pool.get('stock.move').search(cr, uid, [('product_id', 'in', ids)], context=context)
+        return []
+
+    def _set_product_qty(self, cr, uid, id, field, value, arg, context=None):
+        """ The meaning of product_qty field changed lately and is now a functional field computing the quantity
+            in the default product UoM. This code has been added to raise an error if a write is made given a value
+            for `product_qty`, where the same write should set the `product_uom_qty` field instead, in order to
+            detect errors.
+        """
+        raise osv.except_osv(_('Programming Error!'), _('The requested operation cannot be processed because of a programming error setting the `product_qty` field instead of the `product_uom_qty`.'))
 
     _columns = {
         'name': fields.char('Description', required=True, select=True),
-        'priority': fields.selection([('0', 'Not urgent'), ('1', 'Urgent')], 'Priority'),
+        'priority': fields.selection(procurement.PROCUREMENT_PRIORITIES, 'Priority'),
         'create_date': fields.datetime('Creation Date', readonly=True, select=True),
         'date': fields.datetime('Date', required=True, select=True, help="Move date: scheduled date until move is done, then date of actual move processing", states={'done': [('readonly', True)]}),
-        'date_expected': fields.datetime('Scheduled Date', states={'done': [('readonly', True)]},required=True, select=True, help="Scheduled date for the processing of this move"),
-        'product_id': fields.many2one('product.product', 'Product', required=True, select=True, domain=[('type','<>','service')],states={'done': [('readonly', True)]}),
-
-        'product_qty': fields.float('Quantity', digits_compute=dp.get_precision('Product Unit of Measure'),
-            required=True,states={'done': [('readonly', True)]},
+        'date_expected': fields.datetime('Expected Date', states={'done': [('readonly', True)]}, required=True, select=True, help="Scheduled date for the processing of this move"),
+        'product_id': fields.many2one('product.product', 'Product', required=True, select=True, domain=[('type', '<>', 'service')], states={'done': [('readonly', True)]}),
+        'product_qty': fields.function(_quantity_normalize, fnct_inv=_set_product_qty, type='float', digits=0, store={
+                'stock.move': (lambda self, cr, uid, ids, ctx: ids, ['product_id', 'product_uom_qty', 'product_uom'], 20),
+                'product.product': (_get_moves_from_prod, ['uom_id'], 20),
+            }, string='Quantity',
+            help='Quantity in the default UoM of the product'),
+        'product_uom_qty': fields.float('Quantity', digits_compute=dp.get_precision('Product Unit of Measure'),
+            required=True, states={'done': [('readonly', True)]},
             help="This is the quantity of products from an inventory "
                 "point of view. For moves in the state 'done', this is the "
                 "quantity of products that were actually moved. For other "
@@ -1632,24 +1710,22 @@ class stock_move(osv.osv):
                 "backorder. Changing this quantity on assigned moves affects "
                 "the product reservation, and should be done with care."
         ),
-        'product_uom': fields.many2one('product.uom', 'Unit of Measure', required=True,states={'done': [('readonly', True)]}),
+        'product_uom': fields.many2one('product.uom', 'Unit of Measure', required=True, states={'done': [('readonly', True)]}),
         'product_uos_qty': fields.float('Quantity (UOS)', digits_compute=dp.get_precision('Product Unit of Measure'), states={'done': [('readonly', True)]}),
         'product_uos': fields.many2one('product.uom', 'Product UOS', states={'done': [('readonly', True)]}),
-        'product_packaging': fields.many2one('product.packaging', 'Packaging', help="It specifies attributes of packaging like type, quantity of packaging,etc."),
 
-        'location_id': fields.many2one('stock.location', 'Source Location', required=True, select=True,states={'done': [('readonly', True)]}, help="Sets a location if you produce at a fixed location. This can be a partner location if you subcontract the manufacturing operations."),
-        'location_dest_id': fields.many2one('stock.location', 'Destination Location', required=True,states={'done': [('readonly', True)]}, select=True, help="Location where the system will stock the finished products."),
+        'product_packaging': fields.many2one('product.packaging', 'Prefered Packaging', help="It specifies attributes of packaging like type, quantity of packaging,etc."),
+
+        'location_id': fields.many2one('stock.location', 'Source Location', required=True, select=True, states={'done': [('readonly', True)]}, help="Sets a location if you produce at a fixed location. This can be a partner location if you subcontract the manufacturing operations."),
+        'location_dest_id': fields.many2one('stock.location', 'Destination Location', required=True, states={'done': [('readonly', True)]}, select=True, help="Location where the system will stock the finished products."),
+
         'partner_id': fields.many2one('res.partner', 'Destination Address ', states={'done': [('readonly', True)]}, help="Optional address where goods are to be delivered, specifically used for allotment"),
 
-        'prodlot_id': fields.many2one('stock.production.lot', 'Serial Number', states={'done': [('readonly', True)]}, help="Serial number is used to put a serial number on the production", select=True),
-        'tracking_id': fields.many2one('stock.tracking', 'Pack', select=True, states={'done': [('readonly', True)]}, help="Logistical shipping unit: pallet, box, pack ..."),
 
-        'auto_validate': fields.boolean('Auto Validate'),
+        'move_dest_id': fields.many2one('stock.move', 'Destination Move', help="Optional: next stock move when chaining them", select=True, copy=False),
+        'move_orig_ids': fields.one2many('stock.move', 'move_dest_id', 'Original Move', help="Optional: previous stock move when chaining them", select=True),
 
-        'move_dest_id': fields.many2one('stock.move', 'Destination Move', help="Optional: next stock move when chaining them", select=True),
-        'move_history_ids': fields.many2many('stock.move', 'stock_move_history_ids', 'parent_id', 'child_id', 'Move History (child moves)'),
-        'move_history_ids2': fields.many2many('stock.move', 'stock_move_history_ids', 'child_id', 'parent_id', 'Move History (parent moves)'),
-        'picking_id': fields.many2one('stock.picking', 'Reference', select=True,states={'done': [('readonly', True)]}),
+        'picking_id': fields.many2one('stock.picking', 'Reference', select=True, states={'done': [('readonly', True)]}),
         'note': fields.text('Notes'),
         'state': fields.selection([('draft', 'New'),
                                    ('cancel', 'Cancelled'),
@@ -1657,206 +1733,213 @@ class stock_move(osv.osv):
                                    ('confirmed', 'Waiting Availability'),
                                    ('assigned', 'Available'),
                                    ('done', 'Done'),
-                                   ], 'Status', readonly=True, select=True,
+                                   ], 'Status', readonly=True, select=True, copy=False,
                  help= "* New: When the stock move is created and not yet confirmed.\n"\
                        "* Waiting Another Move: This state can be seen when a move is waiting for another one, for example in a chained flow.\n"\
                        "* Waiting Availability: This state is reached when the procurement resolution is not straight forward. It may need the scheduler to run, a component to me manufactured...\n"\
                        "* Available: When products are reserved, it is set to \'Available\'.\n"\
                        "* Done: When the shipment is processed, the state is \'Done\'."),
-        'price_unit': fields.float('Unit Price', digits_compute= dp.get_precision('Product Price'), help="Technical field used to record the product cost set by the user during a picking confirmation (when average price costing method is used)"),
-        'price_currency_id': fields.many2one('res.currency', 'Currency for average price', help="Technical field used to record the currency chosen by the user during a picking confirmation (when average price costing method is used)"),
+        'partially_available': fields.boolean('Partially Available', readonly=True, help="Checks if the move has some stock reserved", copy=False),
+        'price_unit': fields.float('Unit Price', help="Technical field used to record the product cost set by the user during a picking confirmation (when costing method used is 'average price' or 'real'). Value given in company currency and in product uom."),  # as it's a technical field, we intentionally don't provide the digits attribute
+
         'company_id': fields.many2one('res.company', 'Company', required=True, select=True),
-        'backorder_id': fields.related('picking_id','backorder_id',type='many2one', relation="stock.picking", string="Back Order of", select=True),
-        'origin': fields.related('picking_id','origin',type='char', size=64, relation="stock.picking", string="Source", store=True),
+        'split_from': fields.many2one('stock.move', string="Move Split From", help="Technical field used to track the origin of a split move, which can be useful in case of debug", copy=False),
+        'backorder_id': fields.related('picking_id', 'backorder_id', type='many2one', relation="stock.picking", string="Back Order of", select=True),
+        'origin': fields.char("Source"),
+        'procure_method': fields.selection([('make_to_stock', 'Default: Take From Stock'), ('make_to_order', 'Advanced: Apply Procurement Rules')], 'Supply Method', required=True, 
+                                           help="""By default, the system will take from the stock in the source location and passively wait for availability. The other possibility allows you to directly create a procurement on the source location (and thus ignore its current stock) to gather products. If we want to chain moves and have this one to wait for the previous, this second option should be chosen."""),
 
         # used for colors in tree views:
-        'scrapped': fields.related('location_dest_id','scrap_location',type='boolean',relation='stock.location',string='Scrapped', readonly=True),
-        'type': fields.related('picking_id', 'type', type='selection', selection=[('out', 'Sending Goods'), ('in', 'Getting Goods'), ('internal', 'Internal')], string='Shipping Type'),
+        'scrapped': fields.related('location_dest_id', 'scrap_location', type='boolean', relation='stock.location', string='Scrapped', readonly=True),
+
+        'quant_ids': fields.many2many('stock.quant', 'stock_quant_move_rel', 'move_id', 'quant_id', 'Moved Quants'),
+        'reserved_quant_ids': fields.one2many('stock.quant', 'reservation_id', 'Reserved quants'),
+        'linked_move_operation_ids': fields.one2many('stock.move.operation.link', 'move_id', string='Linked Operations', readonly=True, help='Operations that impact this move for the computation of the remaining quantities'),
+        'remaining_qty': fields.function(_get_remaining_qty, type='float', string='Remaining Quantity', digits=0,
+                                         states={'done': [('readonly', True)]}, help="Remaining Quantity in default UoM according to operations matched with this move"),
+        'procurement_id': fields.many2one('procurement.order', 'Procurement'),
+        'group_id': fields.many2one('procurement.group', 'Procurement Group'),
+        'rule_id': fields.many2one('procurement.rule', 'Procurement Rule', help='The pull rule that created this stock move'),
+        'push_rule_id': fields.many2one('stock.location.path', 'Push Rule', help='The push rule that created this stock move'),
+        'propagate': fields.boolean('Propagate cancel and split', help='If checked, when this move is cancelled, cancel the linked move too'),
+        'picking_type_id': fields.many2one('stock.picking.type', 'Picking Type'),
+        'inventory_id': fields.many2one('stock.inventory', 'Inventory'),
+        'lot_ids': fields.function(_get_lot_ids, type='many2many', relation='stock.production.lot', string='Lots'),
+        'origin_returned_move_id': fields.many2one('stock.move', 'Origin return move', help='move that created the return move', copy=False),
+        'returned_move_ids': fields.one2many('stock.move', 'origin_returned_move_id', 'All returned moves', help='Optional: all returned moves created from this move'),
+        'reserved_availability': fields.function(_get_reserved_availability, type='float', string='Quantity Reserved', readonly=True, help='Quantity that has already been reserved for this move'),
+        'availability': fields.function(_get_product_availability, type='float', string='Quantity Available', readonly=True, help='Quantity in stock that can still be reserved for this move'),
+        'string_availability_info': fields.function(_get_string_qty_information, type='text', string='Availability', readonly=True, help='Show various information on stock availability for this move'),
+        'restrict_lot_id': fields.many2one('stock.production.lot', 'Lot', help="Technical field used to depict a restriction on the lot of quants to consider when marking this move as 'done'"),
+        'restrict_partner_id': fields.many2one('res.partner', 'Owner ', help="Technical field used to depict a restriction on the ownership of quants to consider when marking this move as 'done'"),
+        'route_ids': fields.many2many('stock.location.route', 'stock_location_route_move', 'move_id', 'route_id', 'Destination route', help="Preferred route to be followed by the procurement order"),
+        'warehouse_id': fields.many2one('stock.warehouse', 'Warehouse', help="Technical field depicting the warehouse to consider for the route selection on the next procurement (if any)."),
     }
 
-    def _check_location(self, cr, uid, ids, context=None):
-        for record in self.browse(cr, uid, ids, context=context):
-            if (record.state=='done') and (record.location_id.usage == 'view'):
-                raise osv.except_osv(_('Error'), _('You cannot move product %s from a location of type view %s.')% (record.product_id.name, record.location_id.name))
-            if (record.state=='done') and (record.location_dest_id.usage == 'view' ):
-                raise osv.except_osv(_('Error'), _('You cannot move product %s to a location of type view %s.')% (record.product_id.name, record.location_dest_id.name))
-        return True
-
-    _constraints = [
-        (_check_tracking,
-            'You must assign a serial number for this product.',
-            ['prodlot_id']),
-        (_check_location, 'You cannot move products from or to a location of the type view.',
-            ['location_id','location_dest_id']),
-        (_check_product_lot,
-            'You try to assign a lot which is not from the same product.',
-            ['prodlot_id'])]
-
     def _default_location_destination(self, cr, uid, context=None):
-        """ Gets default address of partner for destination location
-        @return: Address id or False
-        """
-        mod_obj = self.pool.get('ir.model.data')
-        picking_type = context.get('picking_type')
-        location_id = False
-        if context is None:
-            context = {}
-        if context.get('move_line', []):
-            if context['move_line'][0]:
-                if isinstance(context['move_line'][0], (tuple, list)):
-                    location_id = context['move_line'][0][2] and context['move_line'][0][2].get('location_dest_id',False)
-                else:
-                    move_list = self.pool.get('stock.move').read(cr, uid, context['move_line'][0], ['location_dest_id'])
-                    location_id = move_list and move_list['location_dest_id'][0] or False
-        elif context.get('address_out_id', False):
-            property_out = self.pool.get('res.partner').browse(cr, uid, context['address_out_id'], context).property_stock_customer
-            location_id = property_out and property_out.id or False
-        else:
-            location_xml_id = False
-            if picking_type in ('in', 'internal'):
-                location_xml_id = 'stock_location_stock'
-            elif picking_type == 'out':
-                location_xml_id = 'stock_location_customers'
-            if location_xml_id:
-                try:
-                    location_model, location_id = mod_obj.get_object_reference(cr, uid, 'stock', location_xml_id)
-                    with tools.mute_logger('openerp.osv.orm'):
-                        self.pool.get('stock.location').check_access_rule(cr, uid, [location_id], 'read', context=context)
-                except (orm.except_orm, ValueError):
-                    location_id = False
-
-        return location_id
+        context = context or {}
+        if context.get('default_picking_type_id', False):
+            pick_type = self.pool.get('stock.picking.type').browse(cr, uid, context['default_picking_type_id'], context=context)
+            return pick_type.default_location_dest_id and pick_type.default_location_dest_id.id or False
+        return False
 
     def _default_location_source(self, cr, uid, context=None):
-        """ Gets default address of partner for source location
-        @return: Address id or False
-        """
-        mod_obj = self.pool.get('ir.model.data')
-        picking_type = context.get('picking_type')
-        location_id = False
-
-        if context is None:
-            context = {}
-        if context.get('move_line', []):
-            try:
-                location_id = context['move_line'][0][2]['location_id']
-            except:
-                pass
-        elif context.get('address_in_id', False):
-            part_obj_add = self.pool.get('res.partner').browse(cr, uid, context['address_in_id'], context=context)
-            if part_obj_add:
-                location_id = part_obj_add.property_stock_supplier.id
-        else:
-            location_xml_id = False
-            if picking_type == 'in':
-                location_xml_id = 'stock_location_suppliers'
-            elif picking_type in ('out', 'internal'):
-                location_xml_id = 'stock_location_stock'
-            if location_xml_id:
-                try:
-                    location_model, location_id = mod_obj.get_object_reference(cr, uid, 'stock', location_xml_id)
-                    with tools.mute_logger('openerp.osv.orm'):
-                        self.pool.get('stock.location').check_access_rule(cr, uid, [location_id], 'read', context=context)
-                except (orm.except_orm, ValueError):
-                    location_id = False
-
-        return location_id
+        context = context or {}
+        if context.get('default_picking_type_id', False):
+            pick_type = self.pool.get('stock.picking.type').browse(cr, uid, context['default_picking_type_id'], context=context)
+            return pick_type.default_location_src_id and pick_type.default_location_src_id.id or False
+        return False
 
     def _default_destination_address(self, cr, uid, context=None):
         user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
         return user.company_id.partner_id.id
 
-    def _default_move_type(self, cr, uid, context=None):
-        """ Gets default type of move
-        @return: type
-        """
-        if context is None:
-            context = {}
-        picking_type = context.get('picking_type')
-        type = 'internal'
-        if picking_type == 'in':
-            type = 'in'
-        elif picking_type == 'out':
-            type = 'out'
-        return type
-
     _defaults = {
         'location_id': _default_location_source,
         'location_dest_id': _default_location_destination,
         'partner_id': _default_destination_address,
-        'type': _default_move_type,
         'state': 'draft',
         'priority': '1',
-        'product_qty': 1.0,
-        'scrapped' :  False,
-        'date': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
-        'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'stock.move', context=c),
-        'date_expected': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
+        'product_uom_qty': 1.0,
+        'scrapped': False,
+        'date': fields.datetime.now,
+        'company_id': lambda self, cr, uid, c: self.pool.get('res.company')._company_default_get(cr, uid, 'stock.move', context=c),
+        'date_expected': fields.datetime.now,
+        'procure_method': 'make_to_stock',
+        'propagate': True,
+        'partially_available': False,
     }
 
+    def _check_uom(self, cr, uid, ids, context=None):
+        for move in self.browse(cr, uid, ids, context=context):
+            if move.product_id.uom_id.category_id.id != move.product_uom.category_id.id:
+                return False
+        return True
+
+    _constraints = [
+        (_check_uom,
+            'You try to move a product using a UoM that is not compatible with the UoM of the product moved. Please use an UoM in the same UoM category.',
+            ['product_uom']),
+    ]
+
+    @api.cr_uid_ids_context
+    def do_unreserve(self, cr, uid, move_ids, context=None):
+        quant_obj = self.pool.get("stock.quant")
+        for move in self.browse(cr, uid, move_ids, context=context):
+            if move.state in ('done', 'cancel'):
+                raise osv.except_osv(_('Operation Forbidden!'), _('Cannot unreserve a done move'))
+            quant_obj.quants_unreserve(cr, uid, move, context=context)
+            if self.find_move_ancestors(cr, uid, move, context=context):
+                self.write(cr, uid, [move.id], {'state': 'waiting'}, context=context)
+            else:
+                self.write(cr, uid, [move.id], {'state': 'confirmed'}, context=context)
+
+    def _prepare_procurement_from_move(self, cr, uid, move, context=None):
+        origin = (move.group_id and (move.group_id.name + ":") or "") + (move.rule_id and move.rule_id.name or move.origin or "/")
+        group_id = move.group_id and move.group_id.id or False
+        if move.rule_id:
+            if move.rule_id.group_propagation_option == 'fixed' and move.rule_id.group_id:
+                group_id = move.rule_id.group_id.id
+            elif move.rule_id.group_propagation_option == 'none':
+                group_id = False
+        return {
+            'name': move.rule_id and move.rule_id.name or "/",
+            'origin': origin,
+            'company_id': move.company_id and move.company_id.id or False,
+            'date_planned': move.date,
+            'product_id': move.product_id.id,
+            'product_qty': move.product_qty,
+            'product_uom': move.product_uom.id,
+            'product_uos_qty': (move.product_uos and move.product_uos_qty) or move.product_qty,
+            'product_uos': (move.product_uos and move.product_uos.id) or move.product_uom.id,
+            'location_id': move.location_id.id,
+            'move_dest_id': move.id,
+            'group_id': group_id,
+            'route_ids': [(4, x.id) for x in move.route_ids],
+            'warehouse_id': move.warehouse_id.id or (move.picking_type_id and move.picking_type_id.warehouse_id.id or False),
+            'priority': move.priority,
+        }
+
+    def _push_apply(self, cr, uid, moves, context=None):
+        push_obj = self.pool.get("stock.location.path")
+        for move in moves:
+            #1) if the move is already chained, there is no need to check push rules
+            #2) if the move is a returned move, we don't want to check push rules, as returning a returned move is the only decent way
+            #   to receive goods without triggering the push rules again (which would duplicate chained operations)
+            if not move.move_dest_id and not move.origin_returned_move_id:
+                domain = [('location_from_id', '=', move.location_dest_id.id)]
+                #priority goes to the route defined on the product and product category
+                route_ids = [x.id for x in move.product_id.route_ids + move.product_id.categ_id.total_route_ids]
+                rules = push_obj.search(cr, uid, domain + [('route_id', 'in', route_ids)], order='route_sequence, sequence', context=context)
+                if not rules:
+                    #then we search on the warehouse if a rule can apply
+                    wh_route_ids = []
+                    if move.warehouse_id:
+                        wh_route_ids = [x.id for x in move.warehouse_id.route_ids]
+                    elif move.picking_type_id and move.picking_type_id.warehouse_id:
+                        wh_route_ids = [x.id for x in move.picking_type_id.warehouse_id.route_ids]
+                    if wh_route_ids:
+                        rules = push_obj.search(cr, uid, domain + [('route_id', 'in', wh_route_ids)], order='route_sequence, sequence', context=context)
+                    if not rules:
+                        #if no specialized push rule has been found yet, we try to find a general one (without route)
+                        rules = push_obj.search(cr, uid, domain + [('route_id', '=', False)], order='sequence', context=context)
+                if rules:
+                    rule = push_obj.browse(cr, uid, rules[0], context=context)
+                    push_obj._apply(cr, uid, rule, move, context=context)
+        return True
+
+    def _create_procurement(self, cr, uid, move, context=None):
+        """ This will create a procurement order """
+        return self.pool.get("procurement.order").create(cr, uid, self._prepare_procurement_from_move(cr, uid, move, context=context))
+
     def write(self, cr, uid, ids, vals, context=None):
+        if context is None:
+            context = {}
         if isinstance(ids, (int, long)):
             ids = [ids]
-        if uid != 1:
-            frozen_fields = set(['product_qty', 'product_uom', 'product_uos_qty', 'product_uos', 'location_id', 'location_dest_id', 'product_id'])
+        # Check that we do not modify a stock.move which is done
+        frozen_fields = set(['product_qty', 'product_uom', 'product_uos_qty', 'product_uos', 'location_id', 'location_dest_id', 'product_id'])
+        for move in self.browse(cr, uid, ids, context=context):
+            if move.state == 'done':
+                if frozen_fields.intersection(vals):
+                    raise osv.except_osv(_('Operation Forbidden!'),
+                        _('Quantities, Units of Measure, Products and Locations cannot be modified on stock moves that have already been processed (except by the Administrator).'))
+        propagated_changes_dict = {}
+        #propagation of quantity change
+        if vals.get('product_uom_qty'):
+            propagated_changes_dict['product_uom_qty'] = vals['product_uom_qty']
+        if vals.get('product_uom_id'):
+            propagated_changes_dict['product_uom_id'] = vals['product_uom_id']
+        #propagation of expected date:
+        propagated_date_field = False
+        if vals.get('date_expected'):
+            #propagate any manual change of the expected date
+            propagated_date_field = 'date_expected'
+        elif (vals.get('state', '') == 'done' and vals.get('date')):
+            #propagate also any delta observed when setting the move as done
+            propagated_date_field = 'date'
+
+        if not context.get('do_not_propagate', False) and (propagated_date_field or propagated_changes_dict):
+            #any propagation is (maybe) needed
             for move in self.browse(cr, uid, ids, context=context):
-                if move.state == 'done':
-                    if frozen_fields.intersection(vals):
-                        raise osv.except_osv(_('Operation Forbidden!'),
-                                             _('Quantities, Units of Measure, Products and Locations cannot be modified on stock moves that have already been processed (except by the Administrator).'))
-        return  super(stock_move, self).write(cr, uid, ids, vals, context=context)
-
-    def copy_data(self, cr, uid, id, default=None, context=None):
-        if default is None:
-            default = {}
-        default = default.copy()
-        default.setdefault('tracking_id', False)
-        default.setdefault('prodlot_id', False)
-        default.setdefault('move_history_ids', [])
-        default.setdefault('move_history_ids2', [])
-        return super(stock_move, self).copy_data(cr, uid, id, default, context=context)
-
-    def _auto_init(self, cursor, context=None):
-        res = super(stock_move, self)._auto_init(cursor, context=context)
-        cursor.execute('SELECT indexname \
-                FROM pg_indexes \
-                WHERE indexname = \'stock_move_location_id_location_dest_id_product_id_state\'')
-        if not cursor.fetchone():
-            cursor.execute('CREATE INDEX stock_move_location_id_location_dest_id_product_id_state \
-                    ON stock_move (product_id, state, location_id, location_dest_id)')
-        return res
-
-    def onchange_lot_id(self, cr, uid, ids, prodlot_id=False, product_qty=False,
-                        loc_id=False, product_id=False, uom_id=False, context=None):
-        """ On change of production lot gives a warning message.
-        @param prodlot_id: Changed production lot id
-        @param product_qty: Quantity of product
-        @param loc_id: Location id
-        @param product_id: Product id
-        @return: Warning message
-        """
-        if not prodlot_id or not loc_id:
-            return {}
-        ctx = context and context.copy() or {}
-        ctx['location_id'] = loc_id
-        ctx.update({'raise-exception': True})
-        uom_obj = self.pool.get('product.uom')
-        product_obj = self.pool.get('product.product')
-        product_uom = product_obj.browse(cr, uid, product_id, context=ctx).uom_id
-        prodlot = self.pool.get('stock.production.lot').browse(cr, uid, prodlot_id, context=ctx)
-        location = self.pool.get('stock.location').browse(cr, uid, loc_id, context=ctx)
-        uom = uom_obj.browse(cr, uid, uom_id, context=ctx)
-        amount_actual = uom_obj._compute_qty_obj(cr, uid, product_uom, prodlot.stock_available, uom, context=ctx)
-        warning = {}
-        if (location.usage == 'internal') and (product_qty > (amount_actual or 0.0)):
-            warning = {
-                'title': _('Insufficient Stock for Serial Number !'),
-                'message': _('You are moving %.2f %s but only %.2f %s available for this serial number.') % (product_qty, uom.name, amount_actual, uom.name)
-            }
-        return {'warning': warning}
-
-    def onchange_quantity(self, cr, uid, ids, product_id, product_qty,
-                          product_uom, product_uos):
+                if move.move_dest_id and move.propagate:
+                    if 'date_expected' in propagated_changes_dict:
+                        propagated_changes_dict.pop('date_expected')
+                    if propagated_date_field:
+                        current_date = datetime.strptime(move.date_expected, DEFAULT_SERVER_DATETIME_FORMAT)
+                        new_date = datetime.strptime(vals.get(propagated_date_field), DEFAULT_SERVER_DATETIME_FORMAT)
+                        delta = new_date - current_date
+                        if abs(delta.days) >= move.company_id.propagation_minimum_delta:
+                            old_move_date = datetime.strptime(move.move_dest_id.date_expected, DEFAULT_SERVER_DATETIME_FORMAT)
+                            new_move_date = (old_move_date + relativedelta.relativedelta(days=delta.days or 0)).strftime(DEFAULT_SERVER_DATETIME_FORMAT)
+                            propagated_changes_dict['date_expected'] = new_move_date
+                    #For pushed moves as well as for pulled moves, propagate by recursive call of write().
+                    #Note that, for pulled moves we intentionally don't propagate on the procurement.
+                    if propagated_changes_dict:
+                        self.write(cr, uid, [move.move_dest_id.id], propagated_changes_dict, context=context)
+        return super(stock_move, self).write(cr, uid, ids, vals, context=context)
+
+    def onchange_quantity(self, cr, uid, ids, product_id, product_qty, product_uom, product_uos):
         """ On change of product quantity finds UoM and UoS quantities
         @param product_id: Product id
         @param product_qty: Changed Quantity of product
@@ -1865,26 +1948,26 @@ class stock_move(osv.osv):
         @return: Dictionary of values
         """
         result = {
-                  'product_uos_qty': 0.00
-          }
+            'product_uos_qty': 0.00
+        }
         warning = {}
 
-        if (not product_id) or (product_qty <=0.0):
+        if (not product_id) or (product_qty <= 0.0):
             result['product_qty'] = 0.0
             return {'value': result}
 
         product_obj = self.pool.get('product.product')
         uos_coeff = product_obj.read(cr, uid, product_id, ['uos_coeff'])
-        
-        # Warn if the quantity was decreased 
+
+        # Warn if the quantity was decreased
         if ids:
             for move in self.read(cr, uid, ids, ['product_qty']):
                 if product_qty < move['product_qty']:
                     warning.update({
-                       'title': _('Information'),
-                       'message': _("By changing this quantity here, you accept the "
-                                "new quantity as complete: OpenERP will not "
-                                "automatically generate a back order.") })
+                        'title': _('Information'),
+                        'message': _("By changing this quantity here, you accept the "
+                                "new quantity as complete: Odoo will not "
+                                "automatically generate a back order.")})
                 break
 
         if product_uos and product_uom and (product_uom != product_uos):
@@ -1904,10 +1987,10 @@ class stock_move(osv.osv):
         @return: Dictionary of values
         """
         result = {
-                  'product_qty': 0.00
-          }
+            'product_uom_qty': 0.00
+        }
 
-        if (not product_id) or (product_uos_qty <=0.0):
+        if (not product_id) or (product_uos_qty <= 0.0):
             result['product_uos_qty'] = 0.0
             return {'value': result}
 
@@ -1918,13 +2001,12 @@ class stock_move(osv.osv):
         # The clients should call onchange_quantity too anyway
 
         if product_uos and product_uom and (product_uom != product_uos):
-            result['product_qty'] = product_uos_qty / uos_coeff['uos_coeff']
+            result['product_uom_qty'] = product_uos_qty / uos_coeff['uos_coeff']
         else:
-            result['product_qty'] = product_uos_qty
+            result['product_uom_qty'] = product_uos_qty
         return {'value': result}
 
-    def onchange_product_id(self, cr, uid, ids, prod_id=False, loc_id=False,
-                            loc_dest_id=False, partner_id=False):
+    def onchange_product_id(self, cr, uid, ids, prod_id=False, loc_id=False, loc_dest_id=False, partner_id=False):
         """ On change of product id, if finds UoM, UoS, quantity and UoS quantity.
         @param prod_id: Changed Product id
         @param loc_id: Source location id
@@ -1943,14 +2025,13 @@ class stock_move(osv.osv):
         ctx = {'lang': lang}
 
         product = self.pool.get('product.product').browse(cr, uid, [prod_id], context=ctx)[0]
-        uos_id  = product.uos_id and product.uos_id.id or False
+        uos_id = product.uos_id and product.uos_id.id or False
         result = {
             'name': product.partner_ref,
             'product_uom': product.uom_id.id,
             'product_uos': uos_id,
-            'product_qty': 1.00,
-            'product_uos_qty' : self.pool.get('stock.move').onchange_quantity(cr, uid, ids, prod_id, 1.00, product.uom_id.id, uos_id)['value']['product_uos_qty'],
-            'prodlot_id' : False,
+            'product_uom_qty': 1.00,
+            'product_uos_qty': self.pool.get('stock.move').onchange_quantity(cr, uid, ids, prod_id, 1.00, product.uom_id.id, uos_id)['value']['product_uos_qty'],
         }
         if loc_id:
             result['location_id'] = loc_id
@@ -1958,33 +2039,30 @@ class stock_move(osv.osv):
             result['location_dest_id'] = loc_dest_id
         return {'value': result}
 
-    def onchange_move_type(self, cr, uid, ids, type, context=None):
-        """ On change of move type gives sorce and destination location.
-        @param type: Move Type
-        @return: Dictionary of values
+    @api.cr_uid_ids_context
+    def _picking_assign(self, cr, uid, move_ids, procurement_group, location_from, location_to, context=None):
+        """Assign a picking on the given move_ids, which is a list of move supposed to share the same procurement_group, location_from and location_to
+        (and company). Those attributes are also given as parameters.
         """
-        mod_obj = self.pool.get('ir.model.data')
-        location_source_id = 'stock_location_stock'
-        location_dest_id = 'stock_location_stock'
-        if type == 'in':
-            location_source_id = 'stock_location_suppliers'
-            location_dest_id = 'stock_location_stock'
-        elif type == 'out':
-            location_source_id = 'stock_location_stock'
-            location_dest_id = 'stock_location_customers'
-        try:
-            source_location = mod_obj.get_object_reference(cr, uid, 'stock', location_source_id)
-            with tools.mute_logger('openerp.osv.orm'):
-                self.pool.get('stock.location').check_access_rule(cr, uid, [source_location[1]], 'read', context=context)
-        except (orm.except_orm, ValueError):
-            source_location = False
-        try:
-            dest_location = mod_obj.get_object_reference(cr, uid, 'stock', location_dest_id)
-            with tools.mute_logger('openerp.osv.orm'):
-                self.pool.get('stock.location').check_access_rule(cr, uid, [dest_location[1]], 'read', context=context)
-        except (orm.except_orm, ValueError):
-            dest_location = False
-        return {'value':{'location_id': source_location and source_location[1] or False, 'location_dest_id': dest_location and dest_location[1] or False}}
+        pick_obj = self.pool.get("stock.picking")
+        picks = pick_obj.search(cr, uid, [
+                ('group_id', '=', procurement_group),
+                ('location_id', '=', location_from),
+                ('location_dest_id', '=', location_to),
+                ('state', 'in', ['draft', 'confirmed', 'waiting'])], context=context)
+        if picks:
+            pick = picks[0]
+        else:
+            move = self.browse(cr, uid, move_ids, context=context)[0]
+            values = {
+                'origin': move.origin,
+                'company_id': move.company_id and move.company_id.id or False,
+                'move_type': move.group_id and move.group_id.move_type or 'direct',
+                'partner_id': move.partner_id.id or False,
+                'picking_type_id': move.picking_type_id and move.picking_type_id.id or False,
+            }
+            pick = pick_obj.create(cr, uid, values, context=context)
+        return self.write(cr, uid, move_ids, {'picking_id': pick}, context=context)
 
     def onchange_date(self, cr, uid, ids, date, date_expected, context=None):
         """ On change of Scheduled Date gives a Move date.
@@ -1993,529 +2071,327 @@ class stock_move(osv.osv):
         @return: Move Date
         """
         if not date_expected:
-            date_expected = time.strftime('%Y-%m-%d %H:%M:%S')
-        return {'value':{'date': date_expected}}
+            date_expected = time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
+        return {'value': {'date': date_expected}}
 
-    def _chain_compute(self, cr, uid, moves, context=None):
-        """ Finds whether the location has chained location type or not.
-        @param moves: Stock moves
-        @return: Dictionary containing destination location with chained location type.
+    def attribute_price(self, cr, uid, move, context=None):
         """
-        result = {}
-        for m in moves:
-            dest = self.pool.get('stock.location').chained_location_get(
-                cr,
-                uid,
-                m.location_dest_id,
-                m.picking_id and m.picking_id.partner_id and m.picking_id.partner_id,
-                m.product_id,
-                context
-            )
-            if dest:
-                if dest[1] == 'transparent':
-                    newdate = (datetime.strptime(m.date, '%Y-%m-%d %H:%M:%S') + relativedelta(days=dest[2] or 0)).strftime('%Y-%m-%d')
-                    self.write(cr, uid, [m.id], {
-                        'date': newdate,
-                        'location_dest_id': dest[0].id})
-                    if m.picking_id and (dest[3] or dest[5]):
-                        self.pool.get('stock.picking').write(cr, uid, [m.picking_id.id], {
-                            'stock_journal_id': dest[3] or m.picking_id.stock_journal_id.id,
-                            'type': dest[5] or m.picking_id.type
-                        }, context=context)
-                    m.location_dest_id = dest[0]
-                    res2 = self._chain_compute(cr, uid, [m], context=context)
-                    for pick_id in res2.keys():
-                        result.setdefault(pick_id, [])
-                        result[pick_id] += res2[pick_id]
-                else:
-                    result.setdefault(m.picking_id, [])
-                    result[m.picking_id].append( (m, dest) )
-        return result
-
-    def _prepare_chained_picking(self, cr, uid, picking_name, picking, picking_type, moves_todo, context=None):
-        """Prepare the definition (values) to create a new chained picking.
-
-           :param str picking_name: desired new picking name
-           :param browse_record picking: source picking (being chained to)
-           :param str picking_type: desired new picking type
-           :param list moves_todo: specification of the stock moves to be later included in this
-               picking, in the form::
-
-                   [[move, (dest_location, auto_packing, chained_delay, chained_journal,
-                                  chained_company_id, chained_picking_type)],
-                    ...
-                   ]
-
-               See also :meth:`stock_location.chained_location_get`.
+            Attribute price to move, important in inter-company moves or receipts with only one partner
         """
-        res_company = self.pool.get('res.company')
-        return {
-                    'name': picking_name,
-                    'origin': tools.ustr(picking.origin or ''),
-                    'type': picking_type,
-                    'note': picking.note,
-                    'move_type': picking.move_type,
-                    'auto_picking': moves_todo[0][1][1] == 'auto',
-                    'stock_journal_id': moves_todo[0][1][3],
-                    'company_id': moves_todo[0][1][4] or res_company._company_default_get(cr, uid, 'stock.company', context=context),
-                    'partner_id': picking.partner_id.id,
-                    'invoice_state': 'none',
-                    'date': picking.date,
-                }
-
-    def _create_chained_picking(self, cr, uid, picking_name, picking, picking_type, moves_todo, context=None):
-        picking_obj = self.pool.get('stock.picking')
-        return picking_obj.create(cr, uid, self._prepare_chained_picking(cr, uid, picking_name, picking, picking_type, moves_todo, context=context))
-
-    def create_chained_picking(self, cr, uid, moves, context=None):
-        res_obj = self.pool.get('res.company')
-        location_obj = self.pool.get('stock.location')
-        move_obj = self.pool.get('stock.move')
-        wf_service = netsvc.LocalService("workflow")
-        new_moves = []
-        if context is None:
-            context = {}
-        seq_obj = self.pool.get('ir.sequence')
-        for picking, chained_moves in self._chain_compute(cr, uid, moves, context=context).items():
-            # We group the moves by automatic move type, so it creates different pickings for different types
-            moves_by_type = {}
-            for move in chained_moves:
-                moves_by_type.setdefault(move[1][1], []).append(move)
-            for todo in moves_by_type.values():
-                ptype = todo[0][1][5] and todo[0][1][5] or location_obj.picking_type_get(cr, uid, todo[0][0].location_dest_id, todo[0][1][0])
-                if picking:
-                    # name of new picking according to its type
-                    if ptype == 'internal':
-                        new_pick_name = seq_obj.get(cr, uid,'stock.picking')
-                    else :
-                        new_pick_name = seq_obj.get(cr, uid, 'stock.picking.' + ptype)
-                    pickid = self._create_chained_picking(cr, uid, new_pick_name, picking, ptype, todo, context=context)
-                    # Need to check name of old picking because it always considers picking as "OUT" when created from Sales Order
-                    old_ptype = location_obj.picking_type_get(cr, uid, picking.move_lines[0].location_id, picking.move_lines[0].location_dest_id)
-                    if old_ptype != picking.type:
-                        old_pick_name = seq_obj.get(cr, uid, 'stock.picking.' + old_ptype)
-                        self.pool.get('stock.picking').write(cr, uid, [picking.id], {'name': old_pick_name, 'type': old_ptype}, context=context)
-                else:
-                    pickid = False
-                for move, (loc, dummy, delay, dummy, company_id, ptype, invoice_state) in todo:
-                    new_id = move_obj.copy(cr, uid, move.id, {
-                        'location_id': move.location_dest_id.id,
-                        'location_dest_id': loc.id,
-                        'date': time.strftime('%Y-%m-%d'),
-                        'picking_id': pickid,
-                        'state': 'waiting',
-                        'company_id': company_id or res_obj._company_default_get(cr, uid, 'stock.company', context=context)  ,
-                        'move_history_ids': [],
-                        'date_expected': (datetime.strptime(move.date, '%Y-%m-%d %H:%M:%S') + relativedelta(days=delay or 0)).strftime('%Y-%m-%d'),
-                        'move_history_ids2': []}
-                    )
-                    move_obj.write(cr, uid, [move.id], {
-                        'move_dest_id': new_id,
-                        'move_history_ids': [(4, new_id)]
-                    })
-                    new_moves.append(self.browse(cr, uid, [new_id])[0])
-                if pickid:
-                    wf_service.trg_validate(uid, 'stock.picking', pickid, 'button_confirm', cr)
-        if new_moves:
-            new_moves += self.create_chained_picking(cr, uid, new_moves, context)
-        return new_moves
+        if not move.price_unit:
+            price = move.product_id.standard_price
+            self.write(cr, uid, [move.id], {'price_unit': price})
 
     def action_confirm(self, cr, uid, ids, context=None):
-        """ Confirms stock move.
+        """ Confirms stock move or put it in waiting if it's linked to another move.
         @return: List of ids.
         """
+        if isinstance(ids, (int, long)):
+            ids = [ids]
+        states = {
+            'confirmed': [],
+            'waiting': []
+        }
+        to_assign = {}
+        for move in self.browse(cr, uid, ids, context=context):
+            self.attribute_price(cr, uid, move, context=context)
+            state = 'confirmed'
+            #if the move is preceeded, then it's waiting (if preceeding move is done, then action_assign has been called already and its state is already available)
+            if move.move_orig_ids:
+                state = 'waiting'
+            #if the move is split and some of the ancestor was preceeded, then it's waiting as well
+            elif move.split_from:
+                move2 = move.split_from
+                while move2 and state != 'waiting':
+                    if move2.move_orig_ids:
+                        state = 'waiting'
+                    move2 = move2.split_from
+            states[state].append(move.id)
+
+            if not move.picking_id and move.picking_type_id:
+                key = (move.group_id.id, move.location_id.id, move.location_dest_id.id)
+                if key not in to_assign:
+                    to_assign[key] = []
+                to_assign[key].append(move.id)
+
+        for move in self.browse(cr, uid, states['confirmed'], context=context):
+            if move.procure_method == 'make_to_order':
+                self._create_procurement(cr, uid, move, context=context)
+                states['waiting'].append(move.id)
+                states['confirmed'].remove(move.id)
+
+        for state, write_ids in states.items():
+            if len(write_ids):
+                self.write(cr, uid, write_ids, {'state': state})
+        #assign picking in batch for all confirmed move that share the same details
+        for key, move_ids in to_assign.items():
+            procurement_group, location_from, location_to = key
+            self._picking_assign(cr, uid, move_ids, procurement_group, location_from, location_to, context=context)
         moves = self.browse(cr, uid, ids, context=context)
-        self.write(cr, uid, ids, {'state': 'confirmed'})
-        self.create_chained_picking(cr, uid, moves, context)
-        return []
-
-    def action_assign(self, cr, uid, ids, *args):
-        """ Changes state to confirmed or waiting.
-        @return: List of values
-        """
-        todo = []
-        for move in self.browse(cr, uid, ids):
-            if move.state in ('confirmed', 'waiting'):
-                todo.append(move.id)
-        res = self.check_assign(cr, uid, todo)
-        return res
+        self._push_apply(cr, uid, moves, context=context)
+        return ids
 
     def force_assign(self, cr, uid, ids, context=None):
         """ Changes the state to assigned.
         @return: True
         """
-        self.write(cr, uid, ids, {'state': 'assigned'})
-        wf_service = netsvc.LocalService('workflow')
-        for move in self.browse(cr, uid, ids, context):
-            if move.picking_id:
-                wf_service.trg_write(uid, 'stock.picking', move.picking_id.id, cr)
-        return True
-
-    def cancel_assign(self, cr, uid, ids, context=None):
-        """ Changes the state to confirmed.
-        @return: True
+        return self.write(cr, uid, ids, {'state': 'assigned'}, context=context)
+
+    def check_tracking_product(self, cr, uid, product, lot_id, location, location_dest, context=None):
+        check = False
+        if product.track_all and not location_dest.usage == 'inventory':
+            check = True
+        elif product.track_incoming and location.usage in ('supplier', 'transit', 'inventory') and location_dest.usage == 'internal':
+            check = True
+        elif product.track_outgoing and location_dest.usage in ('customer', 'transit') and location.usage == 'internal':
+            check = True
+        if check and not lot_id:
+            raise osv.except_osv(_('Warning!'), _('You must assign a serial number for the product %s') % (product.name))
+
+
+    def check_tracking(self, cr, uid, move, lot_id, context=None):
+        """ Checks if serial number is assigned to stock move or not and raise an error if it had to.
         """
-        self.write(cr, uid, ids, {'state': 'confirmed'})
-
-        # fix for bug lp:707031
-        # called write of related picking because changing move availability does
-        # not trigger workflow of picking in order to change the state of picking
-        wf_service = netsvc.LocalService('workflow')
-        for move in self.browse(cr, uid, ids, context):
-            if move.picking_id:
-                wf_service.trg_write(uid, 'stock.picking', move.picking_id.id, cr)
-        return True
+        self.check_tracking_product(cr, uid, move.product_id, lot_id, move.location_id, move.location_dest_id, context=context)
+        
 
-    #
-    # Duplicate stock.move
-    #
-    def check_assign(self, cr, uid, ids, context=None):
+    def action_assign(self, cr, uid, ids, context=None):
         """ Checks the product type and accordingly writes the state.
-        @return: No. of moves done
         """
-        done = []
-        count = 0
-        pickings = {}
-        if context is None:
-            context = {}
+        context = context or {}
+        quant_obj = self.pool.get("stock.quant")
+        to_assign_moves = []
+        main_domain = {}
+        todo_moves = []
+        operations = set()
         for move in self.browse(cr, uid, ids, context=context):
-            if move.product_id.type == 'consu' or move.location_id.usage == 'supplier':
-                if move.state in ('confirmed', 'waiting'):
-                    done.append(move.id)
-                pickings[move.picking_id.id] = 1
+            if move.state not in ('confirmed', 'waiting', 'assigned'):
+                continue
+            if move.location_id.usage in ('supplier', 'inventory', 'production'):
+                to_assign_moves.append(move.id)
+                #in case the move is returned, we want to try to find quants before forcing the assignment
+                if not move.origin_returned_move_id:
+                    continue
+            if move.product_id.type == 'consu':
+                to_assign_moves.append(move.id)
                 continue
-            if move.state in ('confirmed', 'waiting'):
-                # Important: we must pass lock=True to _product_reserve() to avoid race conditions and double reservations
-                res = self.pool.get('stock.location')._product_reserve(cr, uid, [move.location_id.id], move.product_id.id, move.product_qty, {'uom': move.product_uom.id}, lock=True)
-                if res:
-                    #_product_available_test depends on the next status for correct functioning
-                    #the test does not work correctly if the same product occurs multiple times
-                    #in the same order. This is e.g. the case when using the button 'split in two' of
-                    #the stock outgoing form
-                    self.write(cr, uid, [move.id], {'state':'assigned'})
-                    done.append(move.id)
-                    pickings[move.picking_id.id] = 1
-                    r = res.pop(0)
-                    product_uos_qty = self.pool.get('stock.move').onchange_quantity(cr, uid, [move.id], move.product_id.id, r[0], move.product_id.uom_id.id, move.product_id.uos_id.id)['value']['product_uos_qty']
-                    cr.execute('update stock_move set location_id=%s, product_qty=%s, product_uos_qty=%s where id=%s', (r[1], r[0],product_uos_qty, move.id))
-
-                    while res:
-                        r = res.pop(0)
-                        product_uos_qty = self.pool.get('stock.move').onchange_quantity(cr, uid, [move.id], move.product_id.id, r[0], move.product_id.uom_id.id, move.product_id.uos_id.id)['value']['product_uos_qty']
-                        move_id = self.copy(cr, uid, move.id, {'product_uos_qty': product_uos_qty, 'product_qty': r[0], 'location_id': r[1]})
-                        done.append(move_id)
-        if done:
-            count += len(done)
-            self.write(cr, uid, done, {'state': 'assigned'})
-
-        if count:
-            for pick_id in pickings:
-                wf_service = netsvc.LocalService("workflow")
-                wf_service.trg_write(uid, 'stock.picking', pick_id, cr)
-        return count
-
-    def setlast_tracking(self, cr, uid, ids, context=None):
-        assert len(ids) == 1, "1 ID expected, got %s" % (ids, )
-        tracking_obj = self.pool['stock.tracking']
-        move = self.browse(cr, uid, ids[0], context=context)
-        picking_id = move.picking_id.id
-        if picking_id:
-            move_ids = self.search(cr, uid, [
-                ('picking_id', '=', picking_id),
-                ('tracking_id', '!=', False)
-                ], limit=1, order='tracking_id DESC', context=context)
-            if move_ids:
-                tracking_move = self.browse(cr, uid, move_ids[0],
-                                            context=context)
-                tracking_id = tracking_move.tracking_id.id
             else:
-                tracking_id = tracking_obj.create(cr, uid, {}, context=context)
-            self.write(cr, uid, move.id,
-                       {'tracking_id': tracking_id},
-                       context=context)
-        return True
+                todo_moves.append(move)
+
+                #we always keep the quants already assigned and try to find the remaining quantity on quants not assigned only
+                main_domain[move.id] = [('reservation_id', '=', False), ('qty', '>', 0)]
+
+                #if the move is preceeded, restrict the choice of quants in the ones moved previously in original move
+                ancestors = self.find_move_ancestors(cr, uid, move, context=context)
+                if move.state == 'waiting' and not ancestors:
+                    #if the waiting move hasn't yet any ancestor (PO/MO not confirmed yet), don't find any quant available in stock
+                    main_domain[move.id] += [('id', '=', False)]
+                elif ancestors:
+                    main_domain[move.id] += [('history_ids', 'in', ancestors)]
+
+                #if the move is returned from another, restrict the choice of quants to the ones that follow the returned move
+                if move.origin_returned_move_id:
+                    main_domain[move.id] += [('history_ids', 'in', move.origin_returned_move_id.id)]
+                for link in move.linked_move_operation_ids:
+                    operations.add(link.operation_id)
+        # Check all ops and sort them: we want to process first the packages, then operations with lot then the rest
+        operations = list(operations)
+        operations.sort(key=lambda x: ((x.package_id and not x.product_id) and -4 or 0) + (x.package_id and -2 or 0) + (x.lot_id and -1 or 0))
+        for ops in operations:
+            #first try to find quants based on specific domains given by linked operations
+            for record in ops.linked_move_operation_ids:
+                move = record.move_id
+                if move.id in main_domain:
+                    domain = main_domain[move.id] + self.pool.get('stock.move.operation.link').get_specific_domain(cr, uid, record, context=context)
+                    qty = record.qty
+                    if qty:
+                        quants = quant_obj.quants_get_prefered_domain(cr, uid, ops.location_id, move.product_id, qty, domain=domain, prefered_domain_list=[], restrict_lot_id=move.restrict_lot_id.id, restrict_partner_id=move.restrict_partner_id.id, context=context)
+                        quant_obj.quants_reserve(cr, uid, quants, move, record, context=context)
+        for move in todo_moves:
+            if move.linked_move_operation_ids:
+                continue
+            move.refresh()
+            #then if the move isn't totally assigned, try to find quants without any specific domain
+            if move.state != 'assigned':
+                qty_already_assigned = move.reserved_availability
+                qty = move.product_qty - qty_already_assigned
+                quants = quant_obj.quants_get_prefered_domain(cr, uid, move.location_id, move.product_id, qty, domain=main_domain[move.id], prefered_domain_list=[], restrict_lot_id=move.restrict_lot_id.id, restrict_partner_id=move.restrict_partner_id.id, context=context)
+                quant_obj.quants_reserve(cr, uid, quants, move, context=context)
+
+        #force assignation of consumable products and incoming from supplier/inventory/production
+        if to_assign_moves:
+            self.force_assign(cr, uid, to_assign_moves, context=context)
 
-    #
-    # Cancel move => cancel others move and pickings
-    #
     def action_cancel(self, cr, uid, ids, context=None):
         """ Cancels the moves and if all moves are cancelled it cancels the picking.
         @return: True
         """
-        if not len(ids):
-            return True
-        if context is None:
-            context = {}
-        pickings = set()
+        procurement_obj = self.pool.get('procurement.order')
+        context = context or {}
+        procs_to_check = []
         for move in self.browse(cr, uid, ids, context=context):
-            if move.state in ('confirmed', 'waiting', 'assigned', 'draft'):
-                if move.picking_id:
-                    pickings.add(move.picking_id.id)
-            if move.move_dest_id and move.move_dest_id.state == 'waiting':
-                self.write(cr, uid, [move.move_dest_id.id], {'state': 'confirmed'}, context=context)
-                if context.get('call_unlink',False) and move.move_dest_id.picking_id:
-                    wf_service = netsvc.LocalService("workflow")
-                    wf_service.trg_write(uid, 'stock.picking', move.move_dest_id.picking_id.id, cr)
-        self.write(cr, uid, ids, {'state': 'cancel', 'move_dest_id': False}, context=context)
-        if not context.get('call_unlink',False):
-            for pick in self.pool.get('stock.picking').browse(cr, uid, list(pickings), context=context):
-                if all(move.state == 'cancel' for move in pick.move_lines):
-                    self.pool.get('stock.picking').write(cr, uid, [pick.id], {'state': 'cancel'}, context=context)
-
-        wf_service = netsvc.LocalService("workflow")
-        for id in ids:
-            wf_service.trg_trigger(uid, 'stock.move', id, cr)
-        return True
-
-    def _get_accounting_data_for_valuation(self, cr, uid, move, context=None):
-        """
-        Return the accounts and journal to use to post Journal Entries for the real-time
-        valuation of the move.
-
-        :param context: context dictionary that can explicitly mention the company to consider via the 'force_company' key
-        :raise: osv.except_osv() is any mandatory account or journal is not defined.
-        """
-        product_obj=self.pool.get('product.product')
-        accounts = product_obj.get_product_accounts(cr, uid, move.product_id.id, context)
-        if move.location_id.valuation_out_account_id:
-            acc_src = move.location_id.valuation_out_account_id.id
-        else:
-            acc_src = accounts['stock_account_input']
-
-        if move.location_dest_id.valuation_in_account_id:
-            acc_dest = move.location_dest_id.valuation_in_account_id.id
-        else:
-            acc_dest = accounts['stock_account_output']
-
-        acc_valuation = accounts.get('property_stock_valuation_account_id', False)
-        journal_id = accounts['stock_journal']
-
-        if acc_dest == acc_valuation:
-            raise osv.except_osv(_('Error!'),  _('Cannot create Journal Entry, Output Account of this product and Valuation account on category of this product are same.'))
-
-        if acc_src == acc_valuation:
-            raise osv.except_osv(_('Error!'),  _('Cannot create Journal Entry, Input Account of this product and Valuation account on category of this product are same.'))
-
-        if not acc_src:
-            raise osv.except_osv(_('Error!'),  _('Please define stock input account for this product or its category: "%s" (id: %d)') % \
-                                    (move.product_id.name, move.product_id.id,))
-        if not acc_dest:
-            raise osv.except_osv(_('Error!'),  _('Please define stock output account for this product or its category: "%s" (id: %d)') % \
-                                    (move.product_id.name, move.product_id.id,))
-        if not journal_id:
-            raise osv.except_osv(_('Error!'), _('Please define journal on the product category: "%s" (id: %d)') % \
-                                    (move.product_id.categ_id.name, move.product_id.categ_id.id,))
-        if not acc_valuation:
-            raise osv.except_osv(_('Error!'), _('Please define inventory valuation account on the product category: "%s" (id: %d)') % \
-                                    (move.product_id.categ_id.name, move.product_id.categ_id.id,))
-        return journal_id, acc_src, acc_dest, acc_valuation
-
-    def _get_reference_accounting_values_for_valuation(self, cr, uid, move, context=None):
-        """
-        Return the reference amount and reference currency representing the inventory valuation for this move.
-        These reference values should possibly be converted before being posted in Journals to adapt to the primary
-        and secondary currencies of the relevant accounts.
-        """
-        product_uom_obj = self.pool.get('product.uom')
-
-        # by default the reference currency is that of the move's company
-        reference_currency_id = move.company_id.currency_id.id
-
-        default_uom = move.product_id.uom_id.id
-        qty = product_uom_obj._compute_qty(cr, uid, move.product_uom.id, move.product_qty, default_uom)
-
-        # if product is set to average price and a specific value was entered in the picking wizard,
-        # we use it
-        if move.location_dest_id.usage != 'internal' and move.product_id.cost_method == 'average':
-            reference_amount = qty * move.product_id.standard_price
-        elif move.product_id.cost_method == 'average' and move.price_unit:
-            reference_amount = qty * move.price_unit
-            reference_currency_id = move.price_currency_id.id or reference_currency_id
-
-        # Otherwise we default to the company's valuation price type, considering that the values of the
-        # valuation field are expressed in the default currency of the move's company.
-        else:
-            if context is None:
-                context = {}
-            currency_ctx = dict(context, currency_id = move.company_id.currency_id.id)
-            amount_unit = move.product_id.price_get('standard_price', context=currency_ctx)[move.product_id.id]
-            reference_amount = amount_unit * qty
-
-        return reference_amount, reference_currency_id
-
+            if move.state == 'done':
+                raise osv.except_osv(_('Operation Forbidden!'),
+                        _('You cannot cancel a stock move that has been set to \'Done\'.'))
+            if move.reserved_quant_ids:
+                self.pool.get("stock.quant").quants_unreserve(cr, uid, move, context=context)
+            if context.get('cancel_procurement'):
+                if move.propagate:
+                    procurement_ids = procurement_obj.search(cr, uid, [('move_dest_id', '=', move.id)], context=context)
+                    procurement_obj.cancel(cr, uid, procurement_ids, context=context)
+            else:
+                if move.move_dest_id:
+                    if move.propagate:
+                        self.action_cancel(cr, uid, [move.move_dest_id.id], context=context)
+                    elif move.move_dest_id.state == 'waiting':
+                        #If waiting, the chain will be broken and we are not sure if we can still wait for it (=> could take from stock instead)
+                        self.write(cr, uid, [move.move_dest_id.id], {'state': 'confirmed'}, context=context)
+                if move.procurement_id:
+                    # Does the same as procurement check, only eliminating a refresh
+                    procs_to_check.append(move.procurement_id.id)
+                    
+        res = self.write(cr, uid, ids, {'state': 'cancel', 'move_dest_id': False}, context=context)
+        if procs_to_check:
+            procurement_obj.check(cr, uid, procs_to_check, context=context)
+        return res
 
-    def _create_product_valuation_moves(self, cr, uid, move, context=None):
-        """
-        Generate the appropriate accounting moves if the product being moves is subject
-        to real_time valuation tracking, and the source or destination location is
-        a transit location or is outside of the company.
-        """
-        if move.product_id.valuation == 'real_time': # FIXME: product valuation should perhaps be a property?
-            if context is None:
-                context = {}
-            src_company_ctx = dict(context,force_company=move.location_id.company_id.id)
-            dest_company_ctx = dict(context,force_company=move.location_dest_id.company_id.id)
-            # do not take the company of the one of the user
-            # used to select the correct period
-            company_ctx = dict(context, company_id=move.company_id.id)
-            account_moves = []
-            # Outgoing moves (or cross-company output part)
-            if move.location_id.company_id \
-                and (move.location_id.usage == 'internal' and move.location_dest_id.usage != 'internal'\
-                     or move.location_id.company_id != move.location_dest_id.company_id):
-                journal_id, acc_src, acc_dest, acc_valuation = self._get_accounting_data_for_valuation(cr, uid, move, src_company_ctx)
-                reference_amount, reference_currency_id = self._get_reference_accounting_values_for_valuation(cr, uid, move, src_company_ctx)
-                #returning goods to supplier
-                if move.location_dest_id.usage == 'supplier':
-                    account_moves += [(journal_id, self._create_account_move_line(cr, uid, move, acc_valuation, acc_src, reference_amount, reference_currency_id, context))]
-                else:
-                    account_moves += [(journal_id, self._create_account_move_line(cr, uid, move, acc_valuation, acc_dest, reference_amount, reference_currency_id, context))]
-
-            # Incoming moves (or cross-company input part)
-            if move.location_dest_id.company_id \
-                and (move.location_id.usage != 'internal' and move.location_dest_id.usage == 'internal'\
-                     or move.location_id.company_id != move.location_dest_id.company_id):
-                journal_id, acc_src, acc_dest, acc_valuation = self._get_accounting_data_for_valuation(cr, uid, move, dest_company_ctx)
-                reference_amount, reference_currency_id = self._get_reference_accounting_values_for_valuation(cr, uid, move, src_company_ctx)
-                #goods return from customer
-                if move.location_id.usage == 'customer':
-                    account_moves += [(journal_id, self._create_account_move_line(cr, uid, move, acc_dest, acc_valuation, reference_amount, reference_currency_id, context))]
+    def _check_package_from_moves(self, cr, uid, ids, context=None):
+        pack_obj = self.pool.get("stock.quant.package")
+        packs = set()
+        for move in self.browse(cr, uid, ids, context=context):
+            packs |= set([q.package_id for q in move.quant_ids if q.package_id and q.qty > 0])
+        return pack_obj._check_location_constraint(cr, uid, list(packs), context=context)
+
+    def find_move_ancestors(self, cr, uid, move, context=None):
+        '''Find the first level ancestors of given move '''
+        ancestors = []
+        move2 = move
+        while move2:
+            ancestors += [x.id for x in move2.move_orig_ids]
+            #loop on the split_from to find the ancestor of split moves only if the move has not direct ancestor (priority goes to them)
+            move2 = not move2.move_orig_ids and move2.split_from or False
+        return ancestors
+
+    @api.cr_uid_ids_context
+    def recalculate_move_state(self, cr, uid, move_ids, context=None):
+        '''Recompute the state of moves given because their reserved quants were used to fulfill another operation'''
+        for move in self.browse(cr, uid, move_ids, context=context):
+            vals = {}
+            reserved_quant_ids = move.reserved_quant_ids
+            if len(reserved_quant_ids) > 0 and not move.partially_available:
+                vals['partially_available'] = True
+            if len(reserved_quant_ids) == 0 and move.partially_available:
+                vals['partially_available'] = False
+            if move.state == 'assigned':
+                if self.find_move_ancestors(cr, uid, move, context=context):
+                    vals['state'] = 'waiting'
                 else:
-                    account_moves += [(journal_id, self._create_account_move_line(cr, uid, move, acc_src, acc_valuation, reference_amount, reference_currency_id, context))]
-
-            move_obj = self.pool.get('account.move')
-            for j_id, move_lines in account_moves:
-                move_obj.create(cr, uid,
-                        {
-                         'journal_id': j_id,
-                         'line_id': move_lines,
-                         'company_id': move.company_id.id,
-                         'ref': move.picking_id and move.picking_id.name}, context=company_ctx)
+                    vals['state'] = 'confirmed'
+            if vals:
+                self.write(cr, uid, [move.id], vals, context=context)
 
     def action_done(self, cr, uid, ids, context=None):
-        """ Makes the move done and if all moves are done, it will finish the picking.
-        @return:
+        """ Process completely the moves given as ids and if all moves are done, it will finish the picking.
         """
-        picking_ids = []
-        move_ids = []
-        wf_service = netsvc.LocalService("workflow")
-        if context is None:
-            context = {}
-
-        todo = []
-        for move in self.browse(cr, uid, ids, context=context):
-            if move.state=="draft":
-                todo.append(move.id)
+        context = context or {}
+        picking_obj = self.pool.get("stock.picking")
+        quant_obj = self.pool.get("stock.quant")
+        todo = [move.id for move in self.browse(cr, uid, ids, context=context) if move.state == "draft"]
         if todo:
-            self.action_confirm(cr, uid, todo, context=context)
-            todo = []
-
+            ids = self.action_confirm(cr, uid, todo, context=context)
+        pickings = set()
+        procurement_ids = []
+        #Search operations that are linked to the moves
+        operations = set()
+        move_qty = {}
         for move in self.browse(cr, uid, ids, context=context):
-            if move.state in ['done','cancel']:
-                continue
-            move_ids.append(move.id)
-
-            if move.picking_id:
-                picking_ids.append(move.picking_id.id)
-            if move.move_dest_id.id and (move.state != 'done'):
-                # Downstream move should only be triggered if this move is the last pending upstream move
-                other_upstream_move_ids = self.search(cr, uid, [('id','not in',move_ids),('state','not in',['done','cancel']),
-                                            ('move_dest_id','=',move.move_dest_id.id)], context=context)
-                if not other_upstream_move_ids:
-                    self.write(cr, uid, [move.id], {'move_history_ids': [(4, move.move_dest_id.id)]})
-                    if move.move_dest_id.state in ('waiting', 'confirmed'):
-                        self.force_assign(cr, uid, [move.move_dest_id.id], context=context)
-                        if move.move_dest_id.picking_id:
-                            wf_service.trg_write(uid, 'stock.picking', move.move_dest_id.picking_id.id, cr)
-                        if move.move_dest_id.auto_validate:
-                            self.action_done(cr, uid, [move.move_dest_id.id], context=context)
-
-            self._create_product_valuation_moves(cr, uid, move, context=context)
-            if move.state not in ('confirmed','done','assigned'):
-                todo.append(move.id)
-
-        if todo:
-            self.action_confirm(cr, uid, todo, context=context)
-
-        self.write(cr, uid, move_ids, {'state': 'done', 'date': time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)}, context=context)
-        for id in move_ids:
-             wf_service.trg_trigger(uid, 'stock.move', id, cr)
-
-        for pick_id in picking_ids:
-            wf_service.trg_write(uid, 'stock.picking', pick_id, cr)
-
+            move_qty[move.id] = move.product_qty
+            for link in move.linked_move_operation_ids:
+                operations.add(link.operation_id)
+
+        #Sort operations according to entire packages first, then package + lot, package only, lot only
+        operations = list(operations)
+        operations.sort(key=lambda x: ((x.package_id and not x.product_id) and -4 or 0) + (x.package_id and -2 or 0) + (x.lot_id and -1 or 0))
+
+        for ops in operations:
+            if ops.picking_id:
+                pickings.add(ops.picking_id.id)
+            main_domain = [('qty', '>', 0)]
+            for record in ops.linked_move_operation_ids:
+                move = record.move_id
+                self.check_tracking(cr, uid, move, not ops.product_id and ops.package_id.id or ops.lot_id.id, context=context)
+                prefered_domain = [('reservation_id', '=', move.id)]
+                fallback_domain = [('reservation_id', '=', False)]
+                fallback_domain2 = ['&', ('reservation_id', '!=', move.id), ('reservation_id', '!=', False)]
+                prefered_domain_list = [prefered_domain] + [fallback_domain] + [fallback_domain2]
+                dom = main_domain + self.pool.get('stock.move.operation.link').get_specific_domain(cr, uid, record, context=context)
+                quants = quant_obj.quants_get_prefered_domain(cr, uid, ops.location_id, move.product_id, record.qty, domain=dom, prefered_domain_list=prefered_domain_list,
+                                                          restrict_lot_id=move.restrict_lot_id.id, restrict_partner_id=move.restrict_partner_id.id, context=context)
+                if ops.result_package_id.id:
+                    #if a result package is given, all quants go there
+                    quant_dest_package_id = ops.result_package_id.id
+                elif ops.product_id and ops.package_id:
+                    #if a package and a product is given, we will remove quants from the pack.
+                    quant_dest_package_id = False
+                else:
+                    #otherwise we keep the current pack of the quant, which may mean None
+                    quant_dest_package_id = ops.package_id.id
+                quant_obj.quants_move(cr, uid, quants, move, ops.location_dest_id, location_from=ops.location_id, lot_id=ops.lot_id.id, owner_id=ops.owner_id.id, src_package_id=ops.package_id.id, dest_package_id=quant_dest_package_id, context=context)
+                # Handle pack in pack
+                if not ops.product_id and ops.package_id and ops.result_package_id.id != ops.package_id.parent_id.id:
+                    self.pool.get('stock.quant.package').write(cr, SUPERUSER_ID, [ops.package_id.id], {'parent_id': ops.result_package_id.id}, context=context)
+                if not move_qty.get(move.id):
+                    raise osv.except_osv(_("Error"), _("The roundings of your Unit of Measures %s on the move vs. %s on the product don't allow to do these operations or you are not transferring the picking at once. ") % (move.product_uom.name, move.product_id.uom_id.name))
+                move_qty[move.id] -= record.qty
+        #Check for remaining qtys and unreserve/check move_dest_id in
+        move_dest_ids = set()
+        for move in self.browse(cr, uid, ids, context=context):
+            move_qty_cmp = float_compare(move_qty[move.id], 0, precision_rounding=move.product_id.uom_id.rounding)
+            if move_qty_cmp > 0:  # (=In case no pack operations in picking)
+                main_domain = [('qty', '>', 0)]
+                prefered_domain = [('reservation_id', '=', move.id)]
+                fallback_domain = [('reservation_id', '=', False)]
+                fallback_domain2 = ['&', ('reservation_id', '!=', move.id), ('reservation_id', '!=', False)]
+                prefered_domain_list = [prefered_domain] + [fallback_domain] + [fallback_domain2]
+                self.check_tracking(cr, uid, move, move.restrict_lot_id.id, context=context)
+                qty = move_qty[move.id]
+                quants = quant_obj.quants_get_prefered_domain(cr, uid, move.location_id, move.product_id, qty, domain=main_domain, prefered_domain_list=prefered_domain_list, restrict_lot_id=move.restrict_lot_id.id, restrict_partner_id=move.restrict_partner_id.id, context=context)
+                quant_obj.quants_move(cr, uid, quants, move, move.location_dest_id, lot_id=move.restrict_lot_id.id, owner_id=move.restrict_partner_id.id, context=context)
+
+            # If the move has a destination, add it to the list to reserve
+            if move.move_dest_id and move.move_dest_id.state in ('waiting', 'confirmed'):
+                move_dest_ids.add(move.move_dest_id.id)
+
+            if move.procurement_id:
+                procurement_ids.append(move.procurement_id.id)
+
+            #unreserve the quants and make them available for other operations/moves
+            quant_obj.quants_unreserve(cr, uid, move, context=context)
+        # Check the packages have been placed in the correct locations
+        self._check_package_from_moves(cr, uid, ids, context=context)
+        #set the move as done
+        self.write(cr, uid, ids, {'state': 'done', 'date': time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)}, context=context)
+        self.pool.get('procurement.order').check(cr, uid, procurement_ids, context=context)
+        #assign destination moves
+        if move_dest_ids:
+            self.action_assign(cr, uid, list(move_dest_ids), context=context)
+        #check picking state to set the date_done is needed
+        done_picking = []
+        for picking in picking_obj.browse(cr, uid, list(pickings), context=context):
+            if picking.state == 'done' and not picking.date_done:
+                done_picking.append(picking.id)
+        if done_picking:
+            picking_obj.write(cr, uid, done_picking, {'date_done': time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)}, context=context)
         return True
 
-    def _create_account_move_line(self, cr, uid, move, src_account_id, dest_account_id, reference_amount, reference_currency_id, context=None):
-        """
-        Generate the account.move.line values to post to track the stock valuation difference due to the
-        processing of the given stock move.
-        """
-        # prepare default values considering that the destination accounts have the reference_currency_id as their main currency
-        partner_id = (move.picking_id.partner_id and self.pool.get('res.partner')._find_accounting_partner(move.picking_id.partner_id).id) or False
-        debit_line_vals = {
-                    'name': move.name,
-                    'product_id': move.product_id and move.product_id.id or False,
-                    'quantity': move.product_qty,
-                    'ref': move.picking_id and move.picking_id.name or False,
-                    'date': time.strftime('%Y-%m-%d'),
-                    'partner_id': partner_id,
-                    'debit': reference_amount,
-                    'account_id': dest_account_id,
-        }
-        credit_line_vals = {
-                    'name': move.name,
-                    'product_id': move.product_id and move.product_id.id or False,
-                    'quantity': move.product_qty,
-                    'ref': move.picking_id and move.picking_id.name or False,
-                    'date': time.strftime('%Y-%m-%d'),
-                    'partner_id': partner_id,
-                    'credit': reference_amount,
-                    'account_id': src_account_id,
-        }
-
-        # if we are posting to accounts in a different currency, provide correct values in both currencies correctly
-        # when compatible with the optional secondary currency on the account.
-        # Financial Accounts only accept amounts in secondary currencies if there's no secondary currency on the account
-        # or if it's the same as that of the secondary amount being posted.
-        account_obj = self.pool.get('account.account')
-        src_acct, dest_acct = account_obj.browse(cr, uid, [src_account_id, dest_account_id], context=context)
-        src_main_currency_id = src_acct.company_id.currency_id.id
-        dest_main_currency_id = dest_acct.company_id.currency_id.id
-        cur_obj = self.pool.get('res.currency')
-        if reference_currency_id != src_main_currency_id:
-            # fix credit line:
-            credit_line_vals['credit'] = cur_obj.compute(cr, uid, reference_currency_id, src_main_currency_id, reference_amount, context=context)
-            if (not src_acct.currency_id) or src_acct.currency_id.id == reference_currency_id:
-                credit_line_vals.update(currency_id=reference_currency_id, amount_currency=-reference_amount)
-        if reference_currency_id != dest_main_currency_id:
-            # fix debit line:
-            debit_line_vals['debit'] = cur_obj.compute(cr, uid, reference_currency_id, dest_main_currency_id, reference_amount, context=context)
-            if (not dest_acct.currency_id) or dest_acct.currency_id.id == reference_currency_id:
-                debit_line_vals.update(currency_id=reference_currency_id, amount_currency=reference_amount)
-
-        return [(0, 0, debit_line_vals), (0, 0, credit_line_vals)]
-
     def unlink(self, cr, uid, ids, context=None):
-        if context is None:
-            context = {}
-        ctx = context.copy()
+        context = context or {}
         for move in self.browse(cr, uid, ids, context=context):
-            if move.state != 'draft' and not ctx.get('call_unlink', False):
+            if move.state not in ('draft', 'cancel'):
                 raise osv.except_osv(_('User Error!'), _('You can only delete draft moves.'))
-        return super(stock_move, self).unlink(
-            cr, uid, ids, context=ctx)
-
-    # _create_lot function is not used anywhere
-    def _create_lot(self, cr, uid, ids, product_id, prefix=False):
-        """ Creates production lot
-        @return: Production lot id
-        """
-        prodlot_obj = self.pool.get('stock.production.lot')
-        prodlot_id = prodlot_obj.create(cr, uid, {'prefix': prefix, 'product_id': product_id})
-        return prodlot_id
+        return super(stock_move, self).unlink(cr, uid, ids, context=context)
 
-    def action_scrap(self, cr, uid, ids, quantity, location_id, context=None):
+    def action_scrap(self, cr, uid, ids, quantity, location_id, restrict_lot_id=False, restrict_partner_id=False, context=None):
         """ Move the scrap/damaged product into scrap location
         @param cr: the database cursor
         @param uid: the user id
@@ -2525,7 +2401,7 @@ class stock_move(osv.osv):
         @param context: context arguments
         @return: Scraped lines
         """
-        #quantity should in MOVE UOM
+        #quantity should be given in MOVE UOM
         if quantity <= 0:
             raise osv.except_osv(_('Warning!'), _('Please provide a positive quantity to scrap.'))
         res = []
@@ -2533,20 +2409,21 @@ class stock_move(osv.osv):
             source_location = move.location_id
             if move.state == 'done':
                 source_location = move.location_dest_id
-            if source_location.usage != 'internal':
+            #Previously used to prevent scraping from virtual location but not necessary anymore
+            #if source_location.usage != 'internal':
                 #restrict to scrap from a virtual location because it's meaningless and it may introduce errors in stock ('creating' new products from nowhere)
-                raise osv.except_osv(_('Error!'), _('Forbidden operation: it is not allowed to scrap products from a virtual location.'))
+                #raise osv.except_osv(_('Error!'), _('Forbidden operation: it is not allowed to scrap products from a virtual location.'))
             move_qty = move.product_qty
             uos_qty = quantity / move_qty * move.product_uos_qty
             default_val = {
                 'location_id': source_location.id,
-                'product_qty': quantity,
+                'product_uom_qty': quantity,
                 'product_uos_qty': uos_qty,
                 'state': move.state,
                 'scrapped': True,
                 'location_dest_id': location_id,
-                'tracking_id': move.tracking_id.id,
-                'prodlot_id': move.prodlot_id.id,
+                'restrict_lot_id': restrict_lot_id,
+                'restrict_partner_id': restrict_partner_id,
             }
             new_move = self.copy(cr, uid, move.id, default_val)
 
@@ -2561,272 +2438,164 @@ class stock_move(osv.osv):
         self.action_done(cr, uid, res, context=context)
         return res
 
-    # action_split function is not used anywhere
-    # FIXME: deprecate this method
-    def action_split(self, cr, uid, ids, quantity, split_by_qty=1, prefix=False, with_lot=True, context=None):
-        """ Split Stock Move lines into production lot which specified split by quantity.
-        @param cr: the database cursor
-        @param uid: the user id
-        @param ids: ids of stock move object to be splited
-        @param split_by_qty : specify split by qty
-        @param prefix : specify prefix of production lot
-        @param with_lot : if true, prodcution lot will assign for split line otherwise not.
-        @param context: context arguments
-        @return: Splited move lines
-        """
-
-        if context is None:
-            context = {}
-        if quantity <= 0:
-            raise osv.except_osv(_('Warning!'), _('Please provide proper quantity.'))
-
-        res = []
+    def split(self, cr, uid, move, qty, restrict_lot_id=False, restrict_partner_id=False, context=None):
+        """ Splits qty from move move into a new move
+        :param move: browse record
+        :param qty: float. quantity to split (given in product UoM)
+        :param restrict_lot_id: optional production lot that can be given in order to force the new move to restrict its choice of quants to this lot.
+        :param restrict_partner_id: optional partner that can be given in order to force the new move to restrict its choice of quants to the ones belonging to this partner.
+        :param context: dictionay. can contains the special key 'source_location_id' in order to force the source location when copying the move
 
-        for move in self.browse(cr, uid, ids, context=context):
-            if split_by_qty <= 0 or quantity == 0:
-                return res
+        returns the ID of the backorder move created
+        """
+        if move.state in ('done', 'cancel'):
+            raise osv.except_osv(_('Error'), _('You cannot split a move done'))
+        if move.state == 'draft':
+            #we restrict the split of a draft move because if not confirmed yet, it may be replaced by several other moves in
+            #case of phantom bom (with mrp module). And we don't want to deal with this complexity by copying the product that will explode.
+            raise osv.except_osv(_('Error'), _('You cannot split a draft move. It needs to be confirmed first.'))
 
-            uos_qty = split_by_qty / move.product_qty * move.product_uos_qty
+        if move.product_qty <= qty or qty == 0:
+            return move.id
 
-            quantity_rest = quantity % split_by_qty
-            uos_qty_rest = split_by_qty / move.product_qty * move.product_uos_qty
+        uom_obj = self.pool.get('product.uom')
+        context = context or {}
 
-            update_val = {
-                'product_qty': split_by_qty,
-                'product_uos_qty': uos_qty,
-            }
-            for idx in range(int(quantity//split_by_qty)):
-                if not idx and move.product_qty<=quantity:
-                    current_move = move.id
-                else:
-                    current_move = self.copy(cr, uid, move.id, {'state': move.state})
-                res.append(current_move)
-                if with_lot:
-                    update_val['prodlot_id'] = self._create_lot(cr, uid, [current_move], move.product_id.id)
+        uom_qty = uom_obj._compute_qty_obj(cr, uid, move.product_id.uom_id, qty, move.product_uom)
+        uos_qty = uom_qty * move.product_uos_qty / move.product_uom_qty
+
+        defaults = {
+            'product_uom_qty': uom_qty,
+            'product_uos_qty': uos_qty,
+            'procure_method': 'make_to_stock',
+            'restrict_lot_id': restrict_lot_id,
+            'restrict_partner_id': restrict_partner_id,
+            'split_from': move.id,
+            'procurement_id': move.procurement_id.id,
+            'move_dest_id': move.move_dest_id.id,
+        }
+        if context.get('source_location_id'):
+            defaults['location_id'] = context['source_location_id']
+        new_move = self.copy(cr, uid, move.id, defaults)
 
-                self.write(cr, uid, [current_move], update_val)
+        ctx = context.copy()
+        ctx['do_not_propagate'] = True
+        self.write(cr, uid, [move.id], {
+            'product_uom_qty': move.product_uom_qty - uom_qty,
+            'product_uos_qty': move.product_uos_qty - uos_qty,
+        }, context=ctx)
 
+        if move.move_dest_id and move.propagate and move.move_dest_id.state not in ('done', 'cancel'):
+            new_move_prop = self.split(cr, uid, move.move_dest_id, qty, context=context)
+            self.write(cr, uid, [new_move], {'move_dest_id': new_move_prop}, context=context)
+        #returning the first element of list returned by action_confirm is ok because we checked it wouldn't be exploded (and
+        #thus the result of action_confirm should always be a list of 1 element length)
+        return self.action_confirm(cr, uid, [new_move], context=context)[0]
 
-            if quantity_rest > 0:
-                idx = int(quantity//split_by_qty)
-                update_val['product_qty'] = quantity_rest
-                update_val['product_uos_qty'] = uos_qty_rest
-                if not idx and move.product_qty<=quantity:
-                    current_move = move.id
-                else:
-                    current_move = self.copy(cr, uid, move.id, {'state': move.state})
 
-                res.append(current_move)
+    def get_code_from_locs(self, cr, uid, move, location_id=False, location_dest_id=False, context=None):
+        """
+        Returns the code the picking type should have.  This can easily be used
+        to check if a move is internal or not
+        move, location_id and location_dest_id are browse records
+        """
+        code = 'internal'
+        src_loc = location_id or move.location_id
+        dest_loc = location_dest_id or move.location_dest_id
+        if src_loc.usage == 'internal' and dest_loc.usage != 'internal':
+            code = 'outgoing'
+        if src_loc.usage != 'internal' and dest_loc.usage == 'internal':
+            code = 'incoming'
+        return code
 
 
-                if with_lot:
-                    update_val['prodlot_id'] = self._create_lot(cr, uid, [current_move], move.product_id.id)
+class stock_inventory(osv.osv):
+    _name = "stock.inventory"
+    _description = "Inventory"
 
-                self.write(cr, uid, [current_move], update_val)
+    def _get_move_ids_exist(self, cr, uid, ids, field_name, arg, context=None):
+        res = {}
+        for inv in self.browse(cr, uid, ids, context=context):
+            res[inv.id] = False
+            if inv.move_ids:
+                res[inv.id] = True
         return res
 
-    def action_consume(self, cr, uid, ids, quantity, location_id=False, context=None):
-        """ Consumed product with specific quatity from specific source location
-        @param cr: the database cursor
-        @param uid: the user id
-        @param ids: ids of stock move object to be consumed
-        @param quantity : specify consume quantity
-        @param location_id : specify source location
-        @param context: context arguments
-        @return: Consumed lines
+    def _get_available_filters(self, cr, uid, context=None):
         """
-        #quantity should in MOVE UOM
-        if context is None:
-            context = {}
-        if quantity <= 0:
-            raise osv.except_osv(_('Warning!'), _('Please provide proper quantity.'))
-        res = []
-        for move in self.browse(cr, uid, ids, context=context):
-            move_qty = move.product_qty
-            if move_qty <= 0:
-                raise osv.except_osv(_('Error!'), _('Cannot consume a move with negative or zero quantity.'))
-            quantity_rest = move.product_qty
-            quantity_rest -= quantity
-            uos_qty_rest = quantity_rest / move_qty * move.product_uos_qty
-            if quantity_rest <= 0:
-                quantity_rest = 0
-                uos_qty_rest = 0
-                quantity = move.product_qty
+           This function will return the list of filter allowed according to the options checked
+           in 'Settings\Warehouse'.
 
-            uos_qty = quantity / move_qty * move.product_uos_qty
-            if float_compare(quantity_rest, 0, precision_rounding=move.product_id.uom_id.rounding):
-                default_val = {
-                    'product_qty': quantity,
-                    'product_uos_qty': uos_qty,
-                    'state': move.state,
-                    'location_id': location_id or move.location_id.id,
-                }
-                current_move = self.copy(cr, uid, move.id, default_val)
-                res += [current_move]
-                update_val = {}
-                update_val['product_qty'] = quantity_rest
-                update_val['product_uos_qty'] = uos_qty_rest
-                self.write(cr, uid, [move.id], update_val)
-
-            else:
-                quantity_rest = quantity
-                uos_qty_rest =  uos_qty
-                res += [move.id]
-                update_val = {
-                        'product_qty' : quantity_rest,
-                        'product_uos_qty' : uos_qty_rest,
-                        'location_id': location_id or move.location_id.id,
-                }
-                self.write(cr, uid, [move.id], update_val)
-
-        self.action_done(cr, uid, res, context=context)
-
-        return res
-
-    # FIXME: needs refactoring, this code is partially duplicated in stock_picking.do_partial()!
-    def do_partial(self, cr, uid, ids, partial_datas, context=None):
-        """ Makes partial pickings and moves done.
-        @param partial_datas: Dictionary containing details of partial picking
-                          like partner_id, delivery_date, delivery
-                          moves with product_id, product_qty, uom
+           :rtype: list of tuple
         """
+        #default available choices
+        res_filter = [('none', _('All products')), ('product', _('One product only'))]
+        settings_obj = self.pool.get('stock.config.settings')
+        config_ids = settings_obj.search(cr, uid, [], limit=1, order='id DESC', context=context)
+        #If we don't have updated config until now, all fields are by default false and so should be not dipslayed
+        if not config_ids:
+            return res_filter
+
+        stock_settings = settings_obj.browse(cr, uid, config_ids[0], context=context)
+        if stock_settings.group_stock_tracking_owner:
+            res_filter.append(('owner', _('One owner only')))
+            res_filter.append(('product_owner', _('One product for a specific owner')))
+        if stock_settings.group_stock_tracking_lot:
+            res_filter.append(('lot', _('One Lot/Serial Number')))
+        if stock_settings.group_stock_packaging:
+            res_filter.append(('pack', _('A Pack')))
+        return res_filter
+
+    def _get_total_qty(self, cr, uid, ids, field_name, args, context=None):
         res = {}
-        picking_obj = self.pool.get('stock.picking')
-        product_obj = self.pool.get('product.product')
-        currency_obj = self.pool.get('res.currency')
-        uom_obj = self.pool.get('product.uom')
-        wf_service = netsvc.LocalService("workflow")
-
-        if context is None:
-            context = {}
+        for inv in self.browse(cr, uid, ids, context=context):
+            res[inv.id] = sum([x.product_qty for x in inv.line_ids])
+        return res
 
-        complete, too_many, too_few = [], [], []
-        move_product_qty = {}
-        prodlot_ids = {}
-        for move in self.browse(cr, uid, ids, context=context):
-            if move.state in ('done', 'cancel'):
-                continue
-            partial_data = partial_datas.get('move%s'%(move.id), False)
-            assert partial_data, _('Missing partial picking data for move #%s.') % (move.id)
-            product_qty = partial_data.get('product_qty',0.0)
-            move_product_qty[move.id] = product_qty
-            product_uom = partial_data.get('product_uom',False)
-            product_price = partial_data.get('product_price',0.0)
-            product_currency = partial_data.get('product_currency',False)
-            prodlot_ids[move.id] = partial_data.get('prodlot_id')
-            if move.product_qty == product_qty:
-                complete.append(move)
-            elif move.product_qty > product_qty:
-                too_few.append(move)
-            else:
-                too_many.append(move)
-
-            # Average price computation
-            if (move.picking_id.type == 'in') and (move.product_id.cost_method == 'average'):
-                product = product_obj.browse(cr, uid, move.product_id.id)
-                move_currency_id = move.company_id.currency_id.id
-                context['currency_id'] = move_currency_id
-                qty = uom_obj._compute_qty(cr, uid, product_uom, product_qty, product.uom_id.id)
-                if qty > 0:
-                    new_price = currency_obj.compute(cr, uid, product_currency,
-                            move_currency_id, product_price, round=False)
-                    new_price = uom_obj._compute_price(cr, uid, product_uom, new_price,
-                            product.uom_id.id)
-                    if product.qty_available <= 0:
-                        new_std_price = new_price
-                    else:
-                        # Get the standard price
-                        amount_unit = product.price_get('standard_price', context=context)[product.id]
-                        new_std_price = ((amount_unit * product.qty_available)\
-                            + (new_price * qty))/(product.qty_available + qty)
-
-                    product_obj.write(cr, uid, [product.id],{'standard_price': new_std_price})
-
-                    # Record the values that were chosen in the wizard, so they can be
-                    # used for inventory valuation if real-time valuation is enabled.
-                    self.write(cr, uid, [move.id],
-                                {'price_unit': product_price,
-                                 'price_currency_id': product_currency,
-                                })
-
-        for move in too_few:
-            product_qty = move_product_qty[move.id]
-            if product_qty != 0:
-                defaults = {
-                            'product_qty' : product_qty,
-                            'product_uos_qty': product_qty,
-                            'picking_id' : move.picking_id.id,
-                            'state': 'assigned',
-                            'move_dest_id': False,
-                            'price_unit': move.price_unit,
-                            }
-                prodlot_id = prodlot_ids[move.id]
-                if prodlot_id:
-                    defaults.update(prodlot_id=prodlot_id)
-                new_move = self.copy(cr, uid, move.id, defaults)
-                complete.append(self.browse(cr, uid, new_move))
-            self.write(cr, uid, [move.id],
-                    {
-                        'product_qty': move.product_qty - product_qty,
-                        'product_uos_qty': move.product_qty - product_qty,
-                        'prodlot_id': False,
-                        'tracking_id': False,
-                    })
-
-
-        for move in too_many:
-            self.write(cr, uid, [move.id],
-                    {
-                        'product_qty': move.product_qty,
-                        'product_uos_qty': move.product_qty,
-                    })
-            complete.append(move)
-
-        for move in complete:
-            if prodlot_ids.get(move.id):
-                self.write(cr, uid, [move.id],{'prodlot_id': prodlot_ids.get(move.id)})
-            self.action_done(cr, uid, [move.id], context=context)
-            if  move.picking_id.id :
-                # TOCHECK : Done picking if all moves are done
-                cr.execute("""
-                    SELECT move.id FROM stock_picking pick
-                    RIGHT JOIN stock_move move ON move.picking_id = pick.id AND move.state = %s
-                    WHERE pick.id = %s""",
-                            ('done', move.picking_id.id))
-                res = cr.fetchall()
-                if len(res) == len(move.picking_id.move_lines):
-                    picking_obj.action_move(cr, uid, [move.picking_id.id])
-                    wf_service.trg_validate(uid, 'stock.picking', move.picking_id.id, 'button_done', cr)
-
-        return [move.id for move in complete]
-
-stock_move()
+    INVENTORY_STATE_SELECTION = [
+        ('draft', 'Draft'),
+        ('cancel', 'Cancelled'),
+        ('confirm', 'In Progress'),
+        ('done', 'Validated'),
+    ]
 
-class stock_inventory(osv.osv):
-    _name = "stock.inventory"
-    _description = "Inventory"
     _columns = {
-        'name': fields.char('Inventory Reference', size=64, required=True, readonly=True, states={'draft': [('readonly', False)]}),
-        'date': fields.datetime('Creation Date', required=True, readonly=True, states={'draft': [('readonly', False)]}),
-        'date_done': fields.datetime('Date done'),
-        'inventory_line_id': fields.one2many('stock.inventory.line', 'inventory_id', 'Inventories', readonly=True, states={'draft': [('readonly', False)]}),
-        'move_ids': fields.many2many('stock.move', 'stock_inventory_move_rel', 'inventory_id', 'move_id', 'Created Moves'),
-        'state': fields.selection( (('draft', 'Draft'), ('cancel','Cancelled'), ('confirm','Confirmed'), ('done', 'Done')), 'Status', readonly=True, select=True),
-        'company_id': fields.many2one('res.company', 'Company', required=True, select=True, readonly=True, states={'draft':[('readonly',False)]}),
-
+        'name': fields.char('Inventory Reference', required=True, readonly=True, states={'draft': [('readonly', False)]}, help="Inventory Name."),
+        'date': fields.datetime('Inventory Date', required=True, readonly=True, help="The date that will be used for the stock level check of the products and the validation of the stock move related to this inventory."),
+        'line_ids': fields.one2many('stock.inventory.line', 'inventory_id', 'Inventories', readonly=False, states={'done': [('readonly', True)]}, help="Inventory Lines.", copy=True),
+        'move_ids': fields.one2many('stock.move', 'inventory_id', 'Created Moves', help="Inventory Moves.", states={'done': [('readonly', True)]}),
+        'state': fields.selection(INVENTORY_STATE_SELECTION, 'Status', readonly=True, select=True, copy=False),
+        'company_id': fields.many2one('res.company', 'Company', required=True, select=True, readonly=True, states={'draft': [('readonly', False)]}),
+        'location_id': fields.many2one('stock.location', 'Inventoried Location', required=True, readonly=True, states={'draft': [('readonly', False)]}),
+        'product_id': fields.many2one('product.product', 'Inventoried Product', readonly=True, states={'draft': [('readonly', False)]}, help="Specify Product to focus your inventory on a particular Product."),
+        'package_id': fields.many2one('stock.quant.package', 'Inventoried Pack', readonly=True, states={'draft': [('readonly', False)]}, help="Specify Pack to focus your inventory on a particular Pack."),
+        'partner_id': fields.many2one('res.partner', 'Inventoried Owner', readonly=True, states={'draft': [('readonly', False)]}, help="Specify Owner to focus your inventory on a particular Owner."),
+        'lot_id': fields.many2one('stock.production.lot', 'Inventoried Lot/Serial Number', readonly=True, states={'draft': [('readonly', False)]}, help="Specify Lot/Serial Number to focus your inventory on a particular Lot/Serial Number.", copy=False),
+        'move_ids_exist': fields.function(_get_move_ids_exist, type='boolean', string=' Stock Move Exists?', help='technical field for attrs in view'),
+        'filter': fields.selection(_get_available_filters, 'Selection Filter', required=True),
+        'total_qty': fields.function(_get_total_qty, type="float"),
     }
+
+    def _default_stock_location(self, cr, uid, context=None):
+        try:
+            warehouse = self.pool.get('ir.model.data').get_object(cr, uid, 'stock', 'warehouse0')
+            return warehouse.lot_stock_id.id
+        except:
+            return False
+
     _defaults = {
-        'date': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
+        'date': fields.datetime.now,
         'state': 'draft',
-        'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'stock.inventory', context=c)
+        'company_id': lambda self, cr, uid, c: self.pool.get('res.company')._company_default_get(cr, uid, 'stock.inventory', context=c),
+        'location_id': _default_stock_location,
+        'filter': 'none',
     }
 
-    def copy(self, cr, uid, id, default=None, context=None):
-        if default is None:
-            default = {}
-        default = default.copy()
-        default.update({'move_ids': [], 'date_done': False})
-        return super(stock_inventory, self).copy(cr, uid, id, default, context=context)
+    def reset_real_qty(self, cr, uid, ids, context=None):
+        inventory = self.browse(cr, uid, ids[0], context=context)
+        line_ids = [line.id for line in inventory.line_ids]
+        self.pool.get('stock.inventory.line').write(cr, uid, line_ids, {'product_qty': 0})
+        return True
 
     def _inventory_line_hook(self, cr, uid, inventory_line, move_vals):
         """ Creates a stock move from an inventory line
@@ -2840,59 +2609,64 @@ class stock_inventory(osv.osv):
         """ Finish the inventory
         @return: True
         """
-        if context is None:
-            context = {}
-        move_obj = self.pool.get('stock.move')
         for inv in self.browse(cr, uid, ids, context=context):
-            move_obj.action_done(cr, uid, [x.id for x in inv.move_ids], context=context)
-            self.write(cr, uid, [inv.id], {'state':'done', 'date_done': time.strftime('%Y-%m-%d %H:%M:%S')}, context=context)
+            for inventory_line in inv.line_ids:
+                if inventory_line.product_qty < 0 and inventory_line.product_qty != inventory_line.theoretical_qty:
+                    raise osv.except_osv(_('Warning'), _('You cannot set a negative product quantity in an inventory line:\n\t%s - qty: %s' % (inventory_line.product_id.name, inventory_line.product_qty)))
+            self.action_check(cr, uid, [inv.id], context=context)
+            inv.refresh()
+            self.write(cr, uid, [inv.id], {'state': 'done'}, context=context)
+            self.post_inventory(cr, uid, inv, context=context)
         return True
 
-    def action_confirm(self, cr, uid, ids, context=None):
-        """ Confirm the inventory and writes its finished date
+    def post_inventory(self, cr, uid, inv, context=None):
+        #The inventory is posted as a single step which means quants cannot be moved from an internal location to another using an inventory
+        #as they will be moved to inventory loss, and other quants will be created to the encoded quant location. This is a normal behavior
+        #as quants cannot be reuse from inventory location (users can still manually move the products before/after the inventory if they want).
+        move_obj = self.pool.get('stock.move')
+        move_obj.action_done(cr, uid, [x.id for x in inv.move_ids], context=context)
+
+    def _create_stock_move(self, cr, uid, inventory, todo_line, context=None):
+        stock_move_obj = self.pool.get('stock.move')
+        product_obj = self.pool.get('product.product')
+        inventory_location_id = product_obj.browse(cr, uid, todo_line['product_id'], context=context).property_stock_inventory.id
+        vals = {
+            'name': _('INV:') + (inventory.name or ''),
+            'product_id': todo_line['product_id'],
+            'product_uom': todo_line['product_uom_id'],
+            'date': inventory.date,
+            'company_id': inventory.company_id.id,
+            'inventory_id': inventory.id,
+            'state': 'assigned',
+            'restrict_lot_id': todo_line.get('prod_lot_id'),
+            'restrict_partner_id': todo_line.get('partner_id'),
+         }
+
+        if todo_line['product_qty'] < 0:
+            #found more than expected
+            vals['location_id'] = inventory_location_id
+            vals['location_dest_id'] = todo_line['location_id']
+            vals['product_uom_qty'] = -todo_line['product_qty']
+        else:
+            #found less than expected
+            vals['location_id'] = todo_line['location_id']
+            vals['location_dest_id'] = inventory_location_id
+            vals['product_uom_qty'] = todo_line['product_qty']
+        return stock_move_obj.create(cr, uid, vals, context=context)
+
+    def action_check(self, cr, uid, ids, context=None):
+        """ Checks the inventory and computes the stock move to do
         @return: True
         """
-        if context is None:
-            context = {}
-        # to perform the correct inventory corrections we need analyze stock location by
-        # location, never recursively, so we use a special context
-        product_context = dict(context, compute_child=False)
-
-        location_obj = self.pool.get('stock.location')
-        for inv in self.browse(cr, uid, ids, context=context):
-            move_ids = []
-            for line in inv.inventory_line_id:
-                pid = line.product_id.id
-                product_context.update(uom=line.product_uom.id, to_date=inv.date, date=inv.date, prodlot_id=line.prod_lot_id.id)
-                amount = location_obj._product_get(cr, uid, line.location_id.id, [pid], product_context)[pid]
-                change = line.product_qty - amount
-                lot_id = line.prod_lot_id.id
-                if change:
-                    location_id = line.product_id.property_stock_inventory.id
-                    value = {
-                        'name': _('INV:') + (line.inventory_id.name or ''),
-                        'product_id': line.product_id.id,
-                        'product_uom': line.product_uom.id,
-                        'prodlot_id': lot_id,
-                        'date': inv.date,
-                    }
-
-                    if change > 0:
-                        value.update( {
-                            'product_qty': change,
-                            'location_id': location_id,
-                            'location_dest_id': line.location_id.id,
-                        })
-                    else:
-                        value.update( {
-                            'product_qty': -change,
-                            'location_id': line.location_id.id,
-                            'location_dest_id': location_id,
-                        })
-                    move_ids.append(self._inventory_line_hook(cr, uid, line, value))
-            self.write(cr, uid, [inv.id], {'state': 'confirm', 'move_ids': [(6, 0, move_ids)]})
-            self.pool.get('stock.move').action_confirm(cr, uid, move_ids, context=context)
-        return True
+        inventory_line_obj = self.pool.get('stock.inventory.line')
+        stock_move_obj = self.pool.get('stock.move')
+        for inventory in self.browse(cr, uid, ids, context=context):
+            #first remove the existing stock moves linked to this inventory
+            move_ids = [move.id for move in inventory.move_ids]
+            stock_move_obj.unlink(cr, uid, move_ids, context=context)
+            for line in inventory.line_ids:
+                #compare the checked quantities on inventory lines to the theorical one
+                inventory_line_obj._resolve_inventory_line(cr, uid, line, context=context)
 
     def action_cancel_draft(self, cr, uid, ids, context=None):
         """ Cancels the stock move and change inventory state to draft.
@@ -2900,75 +2674,185 @@ class stock_inventory(osv.osv):
         """
         for inv in self.browse(cr, uid, ids, context=context):
             self.pool.get('stock.move').action_cancel(cr, uid, [x.id for x in inv.move_ids], context=context)
-            self.write(cr, uid, [inv.id], {'state':'draft'}, context=context)
+            self.write(cr, uid, [inv.id], {'state': 'draft'}, context=context)
         return True
 
     def action_cancel_inventory(self, cr, uid, ids, context=None):
-        """ Cancels both stock move and inventory
-        @return: True
-        """
-        move_obj = self.pool.get('stock.move')
-        account_move_obj = self.pool.get('account.move')
-        for inv in self.browse(cr, uid, ids, context=context):
-            move_obj.action_cancel(cr, uid, [x.id for x in inv.move_ids], context=context)
-            for move in inv.move_ids:
-                 account_move_ids = account_move_obj.search(cr, uid, [('name', '=', move.name)])
-                 if account_move_ids:
-                     account_move_data_l = account_move_obj.read(cr, uid, account_move_ids, ['state'], context=context)
-                     for account_move in account_move_data_l:
-                         if account_move['state'] == 'posted':
-                             raise osv.except_osv(_('User Error!'),
-                                                  _('In order to cancel this inventory, you must first unpost related journal entries.'))
-                         account_move_obj.unlink(cr, uid, [account_move['id']], context=context)
-            self.write(cr, uid, [inv.id], {'state': 'cancel'}, context=context)
-        return True
+        self.action_cancel_draft(cr, uid, ids, context=context)
+
+    def prepare_inventory(self, cr, uid, ids, context=None):
+        inventory_line_obj = self.pool.get('stock.inventory.line')
+        for inventory in self.browse(cr, uid, ids, context=context):
+            # If there are inventory lines already (e.g. from import), respect those and set their theoretical qty
+            line_ids = [line.id for line in inventory.line_ids]
+            if not line_ids:
+                #compute the inventory lines and create them
+                vals = self._get_inventory_lines(cr, uid, inventory, context=context)
+                for product_line in vals:
+                    inventory_line_obj.create(cr, uid, product_line, context=context)
+            else:
+                # On import calculate theoretical quantity
+                quant_obj = self.pool.get("stock.quant")
+                for line in inventory.line_ids:
+                    dom = [('company_id', '=', line.company_id.id), ('location_id', 'child_of', line.location_id.id), ('lot_id', '=', line.prod_lot_id.id),
+                        ('product_id','=', line.product_id.id), ('owner_id', '=', line.partner_id.id)]
+                    if line.package_id:
+                        dom += [('package_id', '=', line.package_id.id)]
+                    quants = quant_obj.search(cr, uid, dom, context=context)
+                    tot_qty = 0
+                    for quant in quant_obj.browse(cr, uid, quants, context=context):
+                        tot_qty += quant.qty
+                    inventory_line_obj.write(cr, uid, [line.id], {'theoretical_qty': tot_qty}, context=context)
+
+        return self.write(cr, uid, ids, {'state': 'confirm', 'date': time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)})
+
+    def _get_inventory_lines(self, cr, uid, inventory, context=None):
+        location_obj = self.pool.get('stock.location')
+        product_obj = self.pool.get('product.product')
+        location_ids = location_obj.search(cr, uid, [('id', 'child_of', [inventory.location_id.id])], context=context)
+        domain = ' location_id in %s'
+        args = (tuple(location_ids),)
+        if inventory.partner_id:
+            domain += ' and owner_id = %s'
+            args += (inventory.partner_id.id,)
+        if inventory.lot_id:
+            domain += ' and lot_id = %s'
+            args += (inventory.lot_id.id,)
+        if inventory.product_id:
+            domain += ' and product_id = %s'
+            args += (inventory.product_id.id,)
+        if inventory.package_id:
+            domain += ' and package_id = %s'
+            args += (inventory.package_id.id,)
+
+        cr.execute('''
+           SELECT product_id, sum(qty) as product_qty, location_id, lot_id as prod_lot_id, package_id, owner_id as partner_id
+           FROM stock_quant WHERE''' + domain + '''
+           GROUP BY product_id, location_id, lot_id, package_id, partner_id
+        ''', args)
+        vals = []
+        for product_line in cr.dictfetchall():
+            #replace the None the dictionary by False, because falsy values are tested later on
+            for key, value in product_line.items():
+                if not value:
+                    product_line[key] = False
+            product_line['inventory_id'] = inventory.id
+            product_line['theoretical_qty'] = product_line['product_qty']
+            if product_line['product_id']:
+                product = product_obj.browse(cr, uid, product_line['product_id'], context=context)
+                product_line['product_uom_id'] = product.uom_id.id
+            vals.append(product_line)
+        return vals
 
-stock_inventory()
 
 class stock_inventory_line(osv.osv):
     _name = "stock.inventory.line"
     _description = "Inventory Line"
-    _rec_name = "inventory_id"
+    _order = "inventory_id, location_name, product_code, product_name, prodlot_name"
+
+    def _get_product_name_change(self, cr, uid, ids, context=None):
+        return self.pool.get('stock.inventory.line').search(cr, uid, [('product_id', 'in', ids)], context=context)
+
+    def _get_location_change(self, cr, uid, ids, context=None):
+        return self.pool.get('stock.inventory.line').search(cr, uid, [('location_id', 'in', ids)], context=context)
+
+    def _get_prodlot_change(self, cr, uid, ids, context=None):
+        return self.pool.get('stock.inventory.line').search(cr, uid, [('prod_lot_id', 'in', ids)], context=context)
+
     _columns = {
         'inventory_id': fields.many2one('stock.inventory', 'Inventory', ondelete='cascade', select=True),
-        'location_id': fields.many2one('stock.location', 'Location', required=True),
+        'location_id': fields.many2one('stock.location', 'Location', required=True, select=True),
         'product_id': fields.many2one('product.product', 'Product', required=True, select=True),
-        'product_uom': fields.many2one('product.uom', 'Product Unit of Measure', required=True),
-        'product_qty': fields.float('Quantity', digits_compute=dp.get_precision('Product Unit of Measure')),
-        'company_id': fields.related('inventory_id','company_id',type='many2one',relation='res.company',string='Company',store=True, select=True, readonly=True),
+        'package_id': fields.many2one('stock.quant.package', 'Pack', select=True),
+        'product_uom_id': fields.many2one('product.uom', 'Product Unit of Measure', required=True),
+        'product_qty': fields.float('Checked Quantity', digits_compute=dp.get_precision('Product Unit of Measure')),
+        'company_id': fields.related('inventory_id', 'company_id', type='many2one', relation='res.company', string='Company', store=True, select=True, readonly=True),
         'prod_lot_id': fields.many2one('stock.production.lot', 'Serial Number', domain="[('product_id','=',product_id)]"),
-        'state': fields.related('inventory_id','state',type='char',string='Status',readonly=True),
+        'state': fields.related('inventory_id', 'state', type='char', string='Status', readonly=True),
+        'theoretical_qty': fields.float('Theoretical Quantity', digits_compute=dp.get_precision('Product Unit of Measure'), readonly=True),
+        'partner_id': fields.many2one('res.partner', 'Owner'),
+        'product_name': fields.related('product_id', 'name', type='char', string='Product Name', store={
+                                                                                            'product.product': (_get_product_name_change, ['name', 'default_code'], 20),
+                                                                                            'stock.inventory.line': (lambda self, cr, uid, ids, c={}: ids, ['product_id'], 20),}),
+        'product_code': fields.related('product_id', 'default_code', type='char', string='Product Code', store={
+                                                                                            'product.product': (_get_product_name_change, ['name', 'default_code'], 20),
+                                                                                            'stock.inventory.line': (lambda self, cr, uid, ids, c={}: ids, ['product_id'], 20),}),
+        'location_name': fields.related('location_id', 'complete_name', type='char', string='Location Name', store={
+                                                                                            'stock.location': (_get_location_change, ['name', 'location_id', 'active'], 20),
+                                                                                            'stock.inventory.line': (lambda self, cr, uid, ids, c={}: ids, ['location_id'], 20),}),
+        'prodlot_name': fields.related('prod_lot_id', 'name', type='char', string='Serial Number Name', store={
+                                                                                            'stock.production.lot': (_get_prodlot_change, ['name'], 20),
+                                                                                            'stock.inventory.line': (lambda self, cr, uid, ids, c={}: ids, ['prod_lot_id'], 20),}),
     }
 
-    def _default_stock_location(self, cr, uid, context=None):
-        try:
-            location_model, location_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'stock', 'stock_location_stock')
-            with tools.mute_logger('openerp.osv.orm'):
-                self.pool.get('stock.location').check_access_rule(cr, uid, [location_id], 'read', context=context)
-        except (orm.except_orm, ValueError):
-            location_id = False
-        return location_id
-
     _defaults = {
-        'location_id': _default_stock_location
+        'product_qty': 1,
     }
 
-    def on_change_product_id(self, cr, uid, ids, location_id, product, uom=False, to_date=False):
-        """ Changes UoM and name if product_id changes.
+    def _resolve_inventory_line(self, cr, uid, inventory_line, context=None):
+        stock_move_obj = self.pool.get('stock.move')
+        diff = inventory_line.theoretical_qty - inventory_line.product_qty
+        if not diff:
+            return
+        #each theorical_lines where difference between theoretical and checked quantities is not 0 is a line for which we need to create a stock move
+        vals = {
+            'name': _('INV:') + (inventory_line.inventory_id.name or ''),
+            'product_id': inventory_line.product_id.id,
+            'product_uom': inventory_line.product_uom_id.id,
+            'date': inventory_line.inventory_id.date,
+            'company_id': inventory_line.inventory_id.company_id.id,
+            'inventory_id': inventory_line.inventory_id.id,
+            'state': 'confirmed',
+            'restrict_lot_id': inventory_line.prod_lot_id.id,
+            'restrict_partner_id': inventory_line.partner_id.id,
+         }
+        inventory_location_id = inventory_line.product_id.property_stock_inventory.id
+        if diff < 0:
+            #found more than expected
+            vals['location_id'] = inventory_location_id
+            vals['location_dest_id'] = inventory_line.location_id.id
+            vals['product_uom_qty'] = -diff
+        else:
+            #found less than expected
+            vals['location_id'] = inventory_line.location_id.id
+            vals['location_dest_id'] = inventory_location_id
+            vals['product_uom_qty'] = diff
+        return stock_move_obj.create(cr, uid, vals, context=context)
+
+    def restrict_change(self, cr, uid, ids, theoretical_qty, context=None):
+        if ids and theoretical_qty:
+            #if the user try to modify a line prepared by openerp, reject the change and display an error message explaining how he should do
+            old_value = self.browse(cr, uid, ids[0], context=context)
+            return {
+                'value': {
+                    'product_id': old_value.product_id.id,
+                    'product_uom_id': old_value.product_uom_id.id,
+                    'location_id': old_value.location_id.id,
+                    'prod_lot_id': old_value.prod_lot_id.id,
+                    'package_id': old_value.package_id.id,
+                    'partner_id': old_value.partner_id.id,
+                    },
+                'warning': {
+                    'title': _('Error'),
+                    'message': _('You can only change the checked quantity of an existing inventory line. If you want modify a data, please set the checked quantity to 0 and create a new inventory line.')
+                }
+            }
+        return {}
+
+    def on_change_product_id(self, cr, uid, ids, product, uom, theoretical_qty, context=None):
+        """ Changes UoM
         @param location_id: Location id
         @param product: Changed product_id
         @param uom: UoM product
         @return:  Dictionary of changed values
         """
+        if ids and theoretical_qty:
+            return self.restrict_change(cr, uid, ids, theoretical_qty, context=context)
         if not product:
-            return {'value': {'product_qty': 0.0, 'product_uom': False, 'prod_lot_id': False}}
-        obj_product = self.pool.get('product.product').browse(cr, uid, product)
-        uom = uom or obj_product.uom_id.id
-        amount = self.pool.get('stock.location')._product_get(cr, uid, location_id, [product], {'uom': uom, 'to_date': to_date, 'compute_child': False})[product]
-        result = {'product_qty': amount, 'product_uom': uom, 'prod_lot_id': False}
-        return {'value': result}
+            return {'value': {'product_uom_id': False}}
+        obj_product = self.pool.get('product.product').browse(cr, uid, product, context=context)
+        return {'value': {'product_uom_id': uom or obj_product.uom_id.id}}
 
-stock_inventory_line()
 
 #----------------------------------------------------------
 # Stock Warehouse
@@ -2976,192 +2860,1449 @@ stock_inventory_line()
 class stock_warehouse(osv.osv):
     _name = "stock.warehouse"
     _description = "Warehouse"
+
     _columns = {
-        'name': fields.char('Name', size=128, required=True, select=True),
-        'company_id': fields.many2one('res.company', 'Company', required=True, select=True),
-        'partner_id': fields.many2one('res.partner', 'Owner Address'),
-        'lot_input_id': fields.many2one('stock.location', 'Location Input', required=True, domain=[('usage','<>','view')]),
-        'lot_stock_id': fields.many2one('stock.location', 'Location Stock', required=True, domain=[('usage','=','internal')]),
-        'lot_output_id': fields.many2one('stock.location', 'Location Output', required=True, domain=[('usage','<>','view')]),
+        'name': fields.char('Warehouse Name', required=True, select=True),
+        'company_id': fields.many2one('res.company', 'Company', required=True, readonly=True, select=True),
+        'partner_id': fields.many2one('res.partner', 'Address'),
+        'view_location_id': fields.many2one('stock.location', 'View Location', required=True, domain=[('usage', '=', 'view')]),
+        'lot_stock_id': fields.many2one('stock.location', 'Location Stock', domain=[('usage', '=', 'internal')], required=True),
+        'code': fields.char('Short Name', size=5, required=True, help="Short name used to identify your warehouse"),
+        'route_ids': fields.many2many('stock.location.route', 'stock_route_warehouse', 'warehouse_id', 'route_id', 'Routes', domain="[('warehouse_selectable', '=', True)]", help='Defaults routes through the warehouse'),
+        'reception_steps': fields.selection([
+            ('one_step', 'Receive goods directly in stock (1 step)'),
+            ('two_steps', 'Unload in input location then go to stock (2 steps)'),
+            ('three_steps', 'Unload in input location, go through a quality control before being admitted in stock (3 steps)')], 'Incoming Shipments', 
+                                            help="Default incoming route to follow", required=True),
+        'delivery_steps': fields.selection([
+            ('ship_only', 'Ship directly from stock (Ship only)'),
+            ('pick_ship', 'Bring goods to output location before shipping (Pick + Ship)'),
+            ('pick_pack_ship', 'Make packages into a dedicated location, then bring them to the output location for shipping (Pick + Pack + Ship)')], 'Outgoing Shippings', 
+                                           help="Default outgoing route to follow", required=True),
+        'wh_input_stock_loc_id': fields.many2one('stock.location', 'Input Location'),
+        'wh_qc_stock_loc_id': fields.many2one('stock.location', 'Quality Control Location'),
+        'wh_output_stock_loc_id': fields.many2one('stock.location', 'Output Location'),
+        'wh_pack_stock_loc_id': fields.many2one('stock.location', 'Packing Location'),
+        'mto_pull_id': fields.many2one('procurement.rule', 'MTO rule'),
+        'pick_type_id': fields.many2one('stock.picking.type', 'Pick Type'),
+        'pack_type_id': fields.many2one('stock.picking.type', 'Pack Type'),
+        'out_type_id': fields.many2one('stock.picking.type', 'Out Type'),
+        'in_type_id': fields.many2one('stock.picking.type', 'In Type'),
+        'int_type_id': fields.many2one('stock.picking.type', 'Internal Type'),
+        'crossdock_route_id': fields.many2one('stock.location.route', 'Crossdock Route'),
+        'reception_route_id': fields.many2one('stock.location.route', 'Receipt Route'),
+        'delivery_route_id': fields.many2one('stock.location.route', 'Delivery Route'),
+        'resupply_from_wh': fields.boolean('Resupply From Other Warehouses'),
+        'resupply_wh_ids': fields.many2many('stock.warehouse', 'stock_wh_resupply_table', 'supplied_wh_id', 'supplier_wh_id', 'Resupply Warehouses'),
+        'resupply_route_ids': fields.one2many('stock.location.route', 'supplied_wh_id', 'Resupply Routes', 
+                                              help="Routes will be created for these resupply warehouses and you can select them on products and product categories"),
+        'default_resupply_wh_id': fields.many2one('stock.warehouse', 'Default Resupply Warehouse', help="Goods will always be resupplied from this warehouse"),
     }
 
-    def _default_lot_input_stock_id(self, cr, uid, context=None):
-        try:
-            lot_input_stock_model, lot_input_stock_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'stock', 'stock_location_stock')
-            with tools.mute_logger('openerp.osv.orm'):
-                self.pool.get('stock.location').check_access_rule(cr, uid, [lot_input_stock_id], 'read', context=context)
-        except (ValueError, orm.except_orm):
-            # the user does not have read access on the location or it does not exists
-            lot_input_stock_id = False
-        return lot_input_stock_id
-
-    def _default_lot_output_id(self, cr, uid, context=None):
+    def onchange_filter_default_resupply_wh_id(self, cr, uid, ids, default_resupply_wh_id, resupply_wh_ids, context=None):
+        resupply_wh_ids = set([x['id'] for x in (self.resolve_2many_commands(cr, uid, 'resupply_wh_ids', resupply_wh_ids, ['id']))])
+        if default_resupply_wh_id: #If we are removing the default resupply, we don't have default_resupply_wh_id 
+            resupply_wh_ids.add(default_resupply_wh_id)
+        resupply_wh_ids = list(resupply_wh_ids)        
+        return {'value': {'resupply_wh_ids': resupply_wh_ids}}
+
+    def _get_external_transit_location(self, cr, uid, warehouse, context=None):
+        ''' returns browse record of inter company transit location, if found'''
+        data_obj = self.pool.get('ir.model.data')
+        location_obj = self.pool.get('stock.location')
         try:
-            lot_output_model, lot_output_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'stock', 'stock_location_output')
-            with tools.mute_logger('openerp.osv.orm'):
-                self.pool.get('stock.location').check_access_rule(cr, uid, [lot_output_id], 'read', context=context)
-        except (ValueError, orm.except_orm):
-            # the user does not have read access on the location or it does not exists
-            lot_output_id = False
-        return lot_output_id
+            inter_wh_loc = data_obj.get_object_reference(cr, uid, 'stock', 'stock_location_inter_wh')[1]
+        except:
+            return False
+        return location_obj.browse(cr, uid, inter_wh_loc, context=context)
+
+    def _get_inter_wh_route(self, cr, uid, warehouse, wh, context=None):
+        return {
+            'name': _('%s: Supply Product from %s') % (warehouse.name, wh.name),
+            'warehouse_selectable': False,
+            'product_selectable': True,
+            'product_categ_selectable': True,
+            'supplied_wh_id': warehouse.id,
+            'supplier_wh_id': wh.id,
+        }
+
+    def _create_resupply_routes(self, cr, uid, warehouse, supplier_warehouses, default_resupply_wh, context=None):
+        route_obj = self.pool.get('stock.location.route')
+        pull_obj = self.pool.get('procurement.rule')
+        #create route selectable on the product to resupply the warehouse from another one
+        external_transit_location = self._get_external_transit_location(cr, uid, warehouse, context=context)
+        internal_transit_location = warehouse.company_id.internal_transit_location_id
+        input_loc = warehouse.wh_input_stock_loc_id
+        if warehouse.reception_steps == 'one_step':
+            input_loc = warehouse.lot_stock_id
+        for wh in supplier_warehouses:
+            transit_location = wh.company_id.id == warehouse.company_id.id and internal_transit_location or external_transit_location
+            if transit_location:
+                output_loc = wh.wh_output_stock_loc_id
+                if wh.delivery_steps == 'ship_only':
+                    output_loc = wh.lot_stock_id
+                    # Create extra MTO rule (only for 'ship only' because in the other cases MTO rules already exists)
+                    mto_pull_vals = self._get_mto_pull_rule(cr, uid, wh, [(output_loc, transit_location, wh.out_type_id.id)], context=context)[0]
+                    pull_obj.create(cr, uid, mto_pull_vals, context=context)
+                inter_wh_route_vals = self._get_inter_wh_route(cr, uid, warehouse, wh, context=context)
+                inter_wh_route_id = route_obj.create(cr, uid, vals=inter_wh_route_vals, context=context)
+                values = [(output_loc, transit_location, wh.out_type_id.id, wh), (transit_location, input_loc, warehouse.in_type_id.id, warehouse)]
+                pull_rules_list = self._get_supply_pull_rules(cr, uid, warehouse, values, inter_wh_route_id, context=context)
+                for pull_rule in pull_rules_list:
+                    pull_obj.create(cr, uid, vals=pull_rule, context=context)
+                #if the warehouse is also set as default resupply method, assign this route automatically to the warehouse
+                if default_resupply_wh and default_resupply_wh.id == wh.id:
+                    self.write(cr, uid, [warehouse.id], {'route_ids': [(4, inter_wh_route_id)]}, context=context)
 
     _defaults = {
         'company_id': lambda self, cr, uid, c: self.pool.get('res.company')._company_default_get(cr, uid, 'stock.inventory', context=c),
-        'lot_input_id': _default_lot_input_stock_id,
-        'lot_stock_id': _default_lot_input_stock_id,
-        'lot_output_id': _default_lot_output_id,
+        'reception_steps': 'one_step',
+        'delivery_steps': 'ship_only',
     }
+    _sql_constraints = [
+        ('warehouse_name_uniq', 'unique(name, company_id)', 'The name of the warehouse must be unique per company!'),
+        ('warehouse_code_uniq', 'unique(code, company_id)', 'The code of the warehouse must be unique per company!'),
+    ]
 
-stock_warehouse()
+    def _get_partner_locations(self, cr, uid, ids, context=None):
+        ''' returns a tuple made of the browse record of customer location and the browse record of supplier location'''
+        data_obj = self.pool.get('ir.model.data')
+        location_obj = self.pool.get('stock.location')
+        try:
+            customer_loc = data_obj.get_object_reference(cr, uid, 'stock', 'stock_location_customers')[1]
+            supplier_loc = data_obj.get_object_reference(cr, uid, 'stock', 'stock_location_suppliers')[1]
+        except:
+            customer_loc = location_obj.search(cr, uid, [('usage', '=', 'customer')], context=context)
+            customer_loc = customer_loc and customer_loc[0] or False
+            supplier_loc = location_obj.search(cr, uid, [('usage', '=', 'supplier')], context=context)
+            supplier_loc = supplier_loc and supplier_loc[0] or False
+        if not (customer_loc and supplier_loc):
+            raise osv.except_osv(_('Error!'), _('Can\'t find any customer or supplier location.'))
+        return location_obj.browse(cr, uid, [customer_loc, supplier_loc], context=context)
+
+    def switch_location(self, cr, uid, ids, warehouse, new_reception_step=False, new_delivery_step=False, context=None):
+        location_obj = self.pool.get('stock.location')
 
-#----------------------------------------------------------
-# "Empty" Classes that are used to vary from the original stock.picking  (that are dedicated to the internal pickings)
-#   in order to offer a different usability with different views, labels, available reports/wizards...
-#----------------------------------------------------------
-class stock_picking_in(osv.osv):
-    _name = "stock.picking.in"
-    _inherit = "stock.picking"
-    _table = "stock_picking"
-    _description = "Incoming Shipments"
-
-    def search(self, cr, user, args, offset=0, limit=None, order=None, context=None, count=False):
-        return self.pool.get('stock.picking').search(cr, user, args, offset, limit, order, context, count)
-
-    def read(self, cr, uid, ids, fields=None, context=None, load='_classic_read'):
-        return self.pool.get('stock.picking').read(cr, uid, ids, fields=fields, context=context, load=load)
-
-    def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None, orderby=False):
-        return self.pool['stock.picking'].read_group(cr, uid, domain, fields, groupby, offset=offset, limit=limit, context=context, orderby=orderby)
-
-    def check_access_rights(self, cr, uid, operation, raise_exception=True):
-        #override in order to redirect the check of acces rights on the stock.picking object
-        return self.pool.get('stock.picking').check_access_rights(cr, uid, operation, raise_exception=raise_exception)
-
-    def check_access_rule(self, cr, uid, ids, operation, context=None):
-        #override in order to redirect the check of acces rules on the stock.picking object
-        return self.pool.get('stock.picking').check_access_rule(cr, uid, ids, operation, context=context)
-
-    def _workflow_trigger(self, cr, uid, ids, trigger, context=None):
-        #override in order to trigger the workflow of stock.picking at the end of create, write and unlink operation
-        #instead of it's own workflow (which is not existing)
-        return self.pool.get('stock.picking')._workflow_trigger(cr, uid, ids, trigger, context=context)
-
-    def _workflow_signal(self, cr, uid, ids, signal, context=None):
-        #override in order to fire the workflow signal on given stock.picking workflow instance
-        #instead of it's own workflow (which is not existing)
-        return self.pool.get('stock.picking')._workflow_signal(cr, uid, ids, signal, context=context)
-
-    def message_post(self, *args, **kwargs):
-        """Post the message on stock.picking to be able to see it in the form view when using the chatter"""
-        return self.pool.get('stock.picking').message_post(*args, **kwargs)
-
-    def message_subscribe(self, *args, **kwargs):
-        """Send the subscribe action on stock.picking model as it uses _name in request"""
-        return self.pool.get('stock.picking').message_subscribe(*args, **kwargs)
-
-    def message_unsubscribe(self, *args, **kwargs):
-        """Send the unsubscribe action on stock.picking model to match with subscribe"""
-        return self.pool.get('stock.picking').message_unsubscribe(*args, **kwargs)
-
-    def default_get(self, cr, uid, fields_list, context=None):
-        # merge defaults from stock.picking with possible defaults defined on stock.picking.in
-        defaults = self.pool['stock.picking'].default_get(cr, uid, fields_list, context=context)
-        in_defaults = super(stock_picking_in, self).default_get(cr, uid, fields_list, context=context)
-        defaults.update(in_defaults)
-        return defaults
+        new_reception_step = new_reception_step or warehouse.reception_steps
+        new_delivery_step = new_delivery_step or warehouse.delivery_steps
+        if warehouse.reception_steps != new_reception_step:
+            location_obj.write(cr, uid, [warehouse.wh_input_stock_loc_id.id, warehouse.wh_qc_stock_loc_id.id], {'active': False}, context=context)
+            if new_reception_step != 'one_step':
+                location_obj.write(cr, uid, warehouse.wh_input_stock_loc_id.id, {'active': True}, context=context)
+            if new_reception_step == 'three_steps':
+                location_obj.write(cr, uid, warehouse.wh_qc_stock_loc_id.id, {'active': True}, context=context)
+
+        if warehouse.delivery_steps != new_delivery_step:
+            location_obj.write(cr, uid, [warehouse.wh_output_stock_loc_id.id, warehouse.wh_pack_stock_loc_id.id], {'active': False}, context=context)
+            if new_delivery_step != 'ship_only':
+                location_obj.write(cr, uid, warehouse.wh_output_stock_loc_id.id, {'active': True}, context=context)
+            if new_delivery_step == 'pick_pack_ship':
+                location_obj.write(cr, uid, warehouse.wh_pack_stock_loc_id.id, {'active': True}, context=context)
+        return True
+
+    def _get_reception_delivery_route(self, cr, uid, warehouse, route_name, context=None):
+        return {
+            'name': self._format_routename(cr, uid, warehouse, route_name, context=context),
+            'product_categ_selectable': True,
+            'product_selectable': False,
+            'sequence': 10,
+        }
+
+    def _get_supply_pull_rules(self, cr, uid, supplied_warehouse, values, new_route_id, context=None):
+        pull_rules_list = []
+        for from_loc, dest_loc, pick_type_id, warehouse in values:
+            pull_rules_list.append({
+                'name': self._format_rulename(cr, uid, warehouse, from_loc, dest_loc, context=context),
+                'location_src_id': from_loc.id,
+                'location_id': dest_loc.id,
+                'route_id': new_route_id,
+                'action': 'move',
+                'picking_type_id': pick_type_id,
+                'procure_method': warehouse.lot_stock_id.id != from_loc.id and 'make_to_order' or 'make_to_stock', # first part of the resuply route is MTS
+                'warehouse_id': supplied_warehouse.id,
+                'propagate_warehouse_id': warehouse.id,
+            })
+        return pull_rules_list
+
+    def _get_push_pull_rules(self, cr, uid, warehouse, active, values, new_route_id, context=None):
+        first_rule = True
+        push_rules_list = []
+        pull_rules_list = []
+        for from_loc, dest_loc, pick_type_id in values:
+            push_rules_list.append({
+                'name': self._format_rulename(cr, uid, warehouse, from_loc, dest_loc, context=context),
+                'location_from_id': from_loc.id,
+                'location_dest_id': dest_loc.id,
+                'route_id': new_route_id,
+                'auto': 'manual',
+                'picking_type_id': pick_type_id,
+                'active': active,
+                'warehouse_id': warehouse.id,
+            })
+            pull_rules_list.append({
+                'name': self._format_rulename(cr, uid, warehouse, from_loc, dest_loc, context=context),
+                'location_src_id': from_loc.id,
+                'location_id': dest_loc.id,
+                'route_id': new_route_id,
+                'action': 'move',
+                'picking_type_id': pick_type_id,
+                'procure_method': first_rule is True and 'make_to_stock' or 'make_to_order',
+                'active': active,
+                'warehouse_id': warehouse.id,
+            })
+            first_rule = False
+        return push_rules_list, pull_rules_list
+
+    def _get_mto_route(self, cr, uid, context=None):
+        route_obj = self.pool.get('stock.location.route')
+        data_obj = self.pool.get('ir.model.data')
+        try:
+            mto_route_id = data_obj.get_object_reference(cr, uid, 'stock', 'route_warehouse0_mto')[1]
+        except:
+            mto_route_id = route_obj.search(cr, uid, [('name', 'like', _('Make To Order'))], context=context)
+            mto_route_id = mto_route_id and mto_route_id[0] or False
+        if not mto_route_id:
+            raise osv.except_osv(_('Error!'), _('Can\'t find any generic Make To Order route.'))
+        return mto_route_id
+
+    def _check_remove_mto_resupply_rules(self, cr, uid, warehouse, context=None):
+        """ Checks that the moves from the different """
+        pull_obj = self.pool.get('procurement.rule')
+        mto_route_id = self._get_mto_route(cr, uid, context=context)
+        rules = pull_obj.search(cr, uid, ['&', ('location_src_id', '=', warehouse.lot_stock_id.id), ('location_id.usage', '=', 'transit')], context=context)
+        pull_obj.unlink(cr, uid, rules, context=context)
+
+    def _get_mto_pull_rule(self, cr, uid, warehouse, values, context=None):
+        mto_route_id = self._get_mto_route(cr, uid, context=context)
+        res = []
+        for value in values:
+            from_loc, dest_loc, pick_type_id = value
+            res += [{
+            'name': self._format_rulename(cr, uid, warehouse, from_loc, dest_loc, context=context) + _(' MTO'),
+            'location_src_id': from_loc.id,
+            'location_id': dest_loc.id,
+            'route_id': mto_route_id,
+            'action': 'move',
+            'picking_type_id': pick_type_id,
+            'procure_method': 'make_to_order',
+            'active': True,
+            'warehouse_id': warehouse.id,
+            }]
+        return res
+
+    def _get_crossdock_route(self, cr, uid, warehouse, route_name, context=None):
+        return {
+            'name': self._format_routename(cr, uid, warehouse, route_name, context=context),
+            'warehouse_selectable': False,
+            'product_selectable': True,
+            'product_categ_selectable': True,
+            'active': warehouse.delivery_steps != 'ship_only' and warehouse.reception_steps != 'one_step',
+            'sequence': 20,
+        }
+
+    def create_routes(self, cr, uid, ids, warehouse, context=None):
+        wh_route_ids = []
+        route_obj = self.pool.get('stock.location.route')
+        pull_obj = self.pool.get('procurement.rule')
+        push_obj = self.pool.get('stock.location.path')
+        routes_dict = self.get_routes_dict(cr, uid, ids, warehouse, context=context)
+        #create reception route and rules
+        route_name, values = routes_dict[warehouse.reception_steps]
+        route_vals = self._get_reception_delivery_route(cr, uid, warehouse, route_name, context=context)
+        reception_route_id = route_obj.create(cr, uid, route_vals, context=context)
+        wh_route_ids.append((4, reception_route_id))
+        push_rules_list, pull_rules_list = self._get_push_pull_rules(cr, uid, warehouse, True, values, reception_route_id, context=context)
+        #create the push/pull rules
+        for push_rule in push_rules_list:
+            push_obj.create(cr, uid, vals=push_rule, context=context)
+        for pull_rule in pull_rules_list:
+            #all pull rules in reception route are mto, because we don't want to wait for the scheduler to trigger an orderpoint on input location
+            pull_rule['procure_method'] = 'make_to_order'
+            pull_obj.create(cr, uid, vals=pull_rule, context=context)
+
+        #create MTS route and pull rules for delivery and a specific route MTO to be set on the product
+        route_name, values = routes_dict[warehouse.delivery_steps]
+        route_vals = self._get_reception_delivery_route(cr, uid, warehouse, route_name, context=context)
+        #create the route and its pull rules
+        delivery_route_id = route_obj.create(cr, uid, route_vals, context=context)
+        wh_route_ids.append((4, delivery_route_id))
+        dummy, pull_rules_list = self._get_push_pull_rules(cr, uid, warehouse, True, values, delivery_route_id, context=context)
+        for pull_rule in pull_rules_list:
+            pull_obj.create(cr, uid, vals=pull_rule, context=context)
+        #create MTO pull rule and link it to the generic MTO route
+        mto_pull_vals = self._get_mto_pull_rule(cr, uid, warehouse, values, context=context)[0]
+        mto_pull_id = pull_obj.create(cr, uid, mto_pull_vals, context=context)
+
+        #create a route for cross dock operations, that can be set on products and product categories
+        route_name, values = routes_dict['crossdock']
+        crossdock_route_vals = self._get_crossdock_route(cr, uid, warehouse, route_name, context=context)
+        crossdock_route_id = route_obj.create(cr, uid, vals=crossdock_route_vals, context=context)
+        wh_route_ids.append((4, crossdock_route_id))
+        dummy, pull_rules_list = self._get_push_pull_rules(cr, uid, warehouse, warehouse.delivery_steps != 'ship_only' and warehouse.reception_steps != 'one_step', values, crossdock_route_id, context=context)
+        for pull_rule in pull_rules_list:
+            # Fixed cross-dock is logically mto
+            pull_rule['procure_method'] = 'make_to_order'
+            pull_obj.create(cr, uid, vals=pull_rule, context=context)
+
+        #create route selectable on the product to resupply the warehouse from another one
+        self._create_resupply_routes(cr, uid, warehouse, warehouse.resupply_wh_ids, warehouse.default_resupply_wh_id, context=context)
+
+        #return routes and mto pull rule to store on the warehouse
+        return {
+            'route_ids': wh_route_ids,
+            'mto_pull_id': mto_pull_id,
+            'reception_route_id': reception_route_id,
+            'delivery_route_id': delivery_route_id,
+            'crossdock_route_id': crossdock_route_id,
+        }
+
+    def change_route(self, cr, uid, ids, warehouse, new_reception_step=False, new_delivery_step=False, context=None):
+        picking_type_obj = self.pool.get('stock.picking.type')
+        pull_obj = self.pool.get('procurement.rule')
+        push_obj = self.pool.get('stock.location.path')
+        route_obj = self.pool.get('stock.location.route')
+        new_reception_step = new_reception_step or warehouse.reception_steps
+        new_delivery_step = new_delivery_step or warehouse.delivery_steps
+
+        #change the default source and destination location and (de)activate picking types
+        input_loc = warehouse.wh_input_stock_loc_id
+        if new_reception_step == 'one_step':
+            input_loc = warehouse.lot_stock_id
+        output_loc = warehouse.wh_output_stock_loc_id
+        if new_delivery_step == 'ship_only':
+            output_loc = warehouse.lot_stock_id
+        picking_type_obj.write(cr, uid, warehouse.in_type_id.id, {'default_location_dest_id': input_loc.id}, context=context)
+        picking_type_obj.write(cr, uid, warehouse.out_type_id.id, {'default_location_src_id': output_loc.id}, context=context)
+        picking_type_obj.write(cr, uid, warehouse.pick_type_id.id, {'active': new_delivery_step != 'ship_only'}, context=context)
+        picking_type_obj.write(cr, uid, warehouse.pack_type_id.id, {'active': new_delivery_step == 'pick_pack_ship'}, context=context)
+
+        routes_dict = self.get_routes_dict(cr, uid, ids, warehouse, context=context)
+        #update delivery route and rules: unlink the existing rules of the warehouse delivery route and recreate it
+        pull_obj.unlink(cr, uid, [pu.id for pu in warehouse.delivery_route_id.pull_ids], context=context)
+        route_name, values = routes_dict[new_delivery_step]
+        route_obj.write(cr, uid, warehouse.delivery_route_id.id, {'name': self._format_routename(cr, uid, warehouse, route_name, context=context)}, context=context)
+        dummy, pull_rules_list = self._get_push_pull_rules(cr, uid, warehouse, True, values, warehouse.delivery_route_id.id, context=context)
+        #create the pull rules
+        for pull_rule in pull_rules_list:
+            pull_obj.create(cr, uid, vals=pull_rule, context=context)
+
+        #update receipt route and rules: unlink the existing rules of the warehouse receipt route and recreate it
+        pull_obj.unlink(cr, uid, [pu.id for pu in warehouse.reception_route_id.pull_ids], context=context)
+        push_obj.unlink(cr, uid, [pu.id for pu in warehouse.reception_route_id.push_ids], context=context)
+        route_name, values = routes_dict[new_reception_step]
+        route_obj.write(cr, uid, warehouse.reception_route_id.id, {'name': self._format_routename(cr, uid, warehouse, route_name, context=context)}, context=context)
+        push_rules_list, pull_rules_list = self._get_push_pull_rules(cr, uid, warehouse, True, values, warehouse.reception_route_id.id, context=context)
+        #create the push/pull rules
+        for push_rule in push_rules_list:
+            push_obj.create(cr, uid, vals=push_rule, context=context)
+        for pull_rule in pull_rules_list:
+            #all pull rules in receipt route are mto, because we don't want to wait for the scheduler to trigger an orderpoint on input location
+            pull_rule['procure_method'] = 'make_to_order'
+            pull_obj.create(cr, uid, vals=pull_rule, context=context)
+
+        route_obj.write(cr, uid, warehouse.crossdock_route_id.id, {'active': new_reception_step != 'one_step' and new_delivery_step != 'ship_only'}, context=context)
+
+        #change MTO rule
+        dummy, values = routes_dict[new_delivery_step]
+        mto_pull_vals = self._get_mto_pull_rule(cr, uid, warehouse, values, context=context)[0]
+        pull_obj.write(cr, uid, warehouse.mto_pull_id.id, mto_pull_vals, context=context)
+        return True
+
+    def create_sequences_and_picking_types(self, cr, uid, warehouse, context=None):
+        seq_obj = self.pool.get('ir.sequence')
+        picking_type_obj = self.pool.get('stock.picking.type')
+        #create new sequences
+        in_seq_id = seq_obj.create(cr, SUPERUSER_ID, values={'name': warehouse.name + _(' Sequence in'), 'prefix': warehouse.code + '/IN/', 'padding': 5}, context=context)
+        out_seq_id = seq_obj.create(cr, SUPERUSER_ID, values={'name': warehouse.name + _(' Sequence out'), 'prefix': warehouse.code + '/OUT/', 'padding': 5}, context=context)
+        pack_seq_id = seq_obj.create(cr, SUPERUSER_ID, values={'name': warehouse.name + _(' Sequence packing'), 'prefix': warehouse.code + '/PACK/', 'padding': 5}, context=context)
+        pick_seq_id = seq_obj.create(cr, SUPERUSER_ID, values={'name': warehouse.name + _(' Sequence picking'), 'prefix': warehouse.code + '/PICK/', 'padding': 5}, context=context)
+        int_seq_id = seq_obj.create(cr, SUPERUSER_ID, values={'name': warehouse.name + _(' Sequence internal'), 'prefix': warehouse.code + '/INT/', 'padding': 5}, context=context)
+
+        wh_stock_loc = warehouse.lot_stock_id
+        wh_input_stock_loc = warehouse.wh_input_stock_loc_id
+        wh_output_stock_loc = warehouse.wh_output_stock_loc_id
+        wh_pack_stock_loc = warehouse.wh_pack_stock_loc_id
+
+        #fetch customer and supplier locations, for references
+        customer_loc, supplier_loc = self._get_partner_locations(cr, uid, warehouse.id, context=context)
+
+        #create in, out, internal picking types for warehouse
+        input_loc = wh_input_stock_loc
+        if warehouse.reception_steps == 'one_step':
+            input_loc = wh_stock_loc
+        output_loc = wh_output_stock_loc
+        if warehouse.delivery_steps == 'ship_only':
+            output_loc = wh_stock_loc
+
+        #choose the next available color for the picking types of this warehouse
+        color = 0
+        available_colors = [c%9 for c in range(3, 12)]  # put flashy colors first
+        all_used_colors = self.pool.get('stock.picking.type').search_read(cr, uid, [('warehouse_id', '!=', False), ('color', '!=', False)], ['color'], order='color')
+        #don't use sets to preserve the list order
+        for x in all_used_colors:
+            if x['color'] in available_colors:
+                available_colors.remove(x['color'])
+        if available_colors:
+            color = available_colors[0]
+
+        #order the picking types with a sequence allowing to have the following suit for each warehouse: reception, internal, pick, pack, ship. 
+        max_sequence = self.pool.get('stock.picking.type').search_read(cr, uid, [], ['sequence'], order='sequence desc')
+        max_sequence = max_sequence and max_sequence[0]['sequence'] or 0
+
+        in_type_id = picking_type_obj.create(cr, uid, vals={
+            'name': _('Receipts'),
+            'warehouse_id': warehouse.id,
+            'code': 'incoming',
+            'sequence_id': in_seq_id,
+            'default_location_src_id': supplier_loc.id,
+            'default_location_dest_id': input_loc.id,
+            'sequence': max_sequence + 1,
+            'color': color}, context=context)
+        out_type_id = picking_type_obj.create(cr, uid, vals={
+            'name': _('Delivery Orders'),
+            'warehouse_id': warehouse.id,
+            'code': 'outgoing',
+            'sequence_id': out_seq_id,
+            'return_picking_type_id': in_type_id,
+            'default_location_src_id': output_loc.id,
+            'default_location_dest_id': customer_loc.id,
+            'sequence': max_sequence + 4,
+            'color': color}, context=context)
+        picking_type_obj.write(cr, uid, [in_type_id], {'return_picking_type_id': out_type_id}, context=context)
+        int_type_id = picking_type_obj.create(cr, uid, vals={
+            'name': _('Internal Transfers'),
+            'warehouse_id': warehouse.id,
+            'code': 'internal',
+            'sequence_id': int_seq_id,
+            'default_location_src_id': wh_stock_loc.id,
+            'default_location_dest_id': wh_stock_loc.id,
+            'active': True,
+            'sequence': max_sequence + 2,
+            'color': color}, context=context)
+        pack_type_id = picking_type_obj.create(cr, uid, vals={
+            'name': _('Pack'),
+            'warehouse_id': warehouse.id,
+            'code': 'internal',
+            'sequence_id': pack_seq_id,
+            'default_location_src_id': wh_pack_stock_loc.id,
+            'default_location_dest_id': output_loc.id,
+            'active': warehouse.delivery_steps == 'pick_pack_ship',
+            'sequence': max_sequence + 3,
+            'color': color}, context=context)
+        pick_type_id = picking_type_obj.create(cr, uid, vals={
+            'name': _('Pick'),
+            'warehouse_id': warehouse.id,
+            'code': 'internal',
+            'sequence_id': pick_seq_id,
+            'default_location_src_id': wh_stock_loc.id,
+            'default_location_dest_id': wh_pack_stock_loc.id,
+            'active': warehouse.delivery_steps != 'ship_only',
+            'sequence': max_sequence + 2,
+            'color': color}, context=context)
+
+        #write picking types on WH
+        vals = {
+            'in_type_id': in_type_id,
+            'out_type_id': out_type_id,
+            'pack_type_id': pack_type_id,
+            'pick_type_id': pick_type_id,
+            'int_type_id': int_type_id,
+        }
+        super(stock_warehouse, self).write(cr, uid, warehouse.id, vals=vals, context=context)
+
+
+    def create(self, cr, uid, vals, context=None):
+        if context is None:
+            context = {}
+        if vals is None:
+            vals = {}
+        data_obj = self.pool.get('ir.model.data')
+        seq_obj = self.pool.get('ir.sequence')
+        picking_type_obj = self.pool.get('stock.picking.type')
+        location_obj = self.pool.get('stock.location')
+
+        #create view location for warehouse
+        loc_vals = {
+                'name': _(vals.get('code')),
+                'usage': 'view',
+                'location_id': data_obj.get_object_reference(cr, uid, 'stock', 'stock_location_locations')[1],
+        }
+        if vals.get('company_id'):
+            loc_vals['company_id'] = vals.get('company_id')
+        wh_loc_id = location_obj.create(cr, uid, loc_vals, context=context)
+        vals['view_location_id'] = wh_loc_id
+        #create all location
+        def_values = self.default_get(cr, uid, {'reception_steps', 'delivery_steps'})
+        reception_steps = vals.get('reception_steps',  def_values['reception_steps'])
+        delivery_steps = vals.get('delivery_steps', def_values['delivery_steps'])
+        context_with_inactive = context.copy()
+        context_with_inactive['active_test'] = False
+        sub_locations = [
+            {'name': _('Stock'), 'active': True, 'field': 'lot_stock_id'},
+            {'name': _('Input'), 'active': reception_steps != 'one_step', 'field': 'wh_input_stock_loc_id'},
+            {'name': _('Quality Control'), 'active': reception_steps == 'three_steps', 'field': 'wh_qc_stock_loc_id'},
+            {'name': _('Output'), 'active': delivery_steps != 'ship_only', 'field': 'wh_output_stock_loc_id'},
+            {'name': _('Packing Zone'), 'active': delivery_steps == 'pick_pack_ship', 'field': 'wh_pack_stock_loc_id'},
+        ]
+        for values in sub_locations:
+            loc_vals = {
+                'name': values['name'],
+                'usage': 'internal',
+                'location_id': wh_loc_id,
+                'active': values['active'],
+            }
+            if vals.get('company_id'):
+                loc_vals['company_id'] = vals.get('company_id')
+            location_id = location_obj.create(cr, uid, loc_vals, context=context_with_inactive)
+            vals[values['field']] = location_id
+
+        #create WH
+        new_id = super(stock_warehouse, self).create(cr, uid, vals=vals, context=context)
+        warehouse = self.browse(cr, uid, new_id, context=context)
+        self.create_sequences_and_picking_types(cr, uid, warehouse, context=context)
+        warehouse.refresh()
+
+        #create routes and push/pull rules
+        new_objects_dict = self.create_routes(cr, uid, new_id, warehouse, context=context)
+        self.write(cr, uid, warehouse.id, new_objects_dict, context=context)
+        return new_id
+
+    def _format_rulename(self, cr, uid, obj, from_loc, dest_loc, context=None):
+        return obj.code + ': ' + from_loc.name + ' -> ' + dest_loc.name
+
+    def _format_routename(self, cr, uid, obj, name, context=None):
+        return obj.name + ': ' + name
+
+    def get_routes_dict(self, cr, uid, ids, warehouse, context=None):
+        #fetch customer and supplier locations, for references
+        customer_loc, supplier_loc = self._get_partner_locations(cr, uid, ids, context=context)
+
+        return {
+            'one_step': (_('Receipt in 1 step'), []),
+            'two_steps': (_('Receipt in 2 steps'), [(warehouse.wh_input_stock_loc_id, warehouse.lot_stock_id, warehouse.int_type_id.id)]),
+            'three_steps': (_('Receipt in 3 steps'), [(warehouse.wh_input_stock_loc_id, warehouse.wh_qc_stock_loc_id, warehouse.int_type_id.id), (warehouse.wh_qc_stock_loc_id, warehouse.lot_stock_id, warehouse.int_type_id.id)]),
+            'crossdock': (_('Cross-Dock'), [(warehouse.wh_input_stock_loc_id, warehouse.wh_output_stock_loc_id, warehouse.int_type_id.id), (warehouse.wh_output_stock_loc_id, customer_loc, warehouse.out_type_id.id)]),
+            'ship_only': (_('Ship Only'), [(warehouse.lot_stock_id, customer_loc, warehouse.out_type_id.id)]),
+            'pick_ship': (_('Pick + Ship'), [(warehouse.lot_stock_id, warehouse.wh_output_stock_loc_id, warehouse.pick_type_id.id), (warehouse.wh_output_stock_loc_id, customer_loc, warehouse.out_type_id.id)]),
+            'pick_pack_ship': (_('Pick + Pack + Ship'), [(warehouse.lot_stock_id, warehouse.wh_pack_stock_loc_id, warehouse.pick_type_id.id), (warehouse.wh_pack_stock_loc_id, warehouse.wh_output_stock_loc_id, warehouse.pack_type_id.id), (warehouse.wh_output_stock_loc_id, customer_loc, warehouse.out_type_id.id)]),
+        }
+
+    def _handle_renaming(self, cr, uid, warehouse, name, code, context=None):
+        location_obj = self.pool.get('stock.location')
+        route_obj = self.pool.get('stock.location.route')
+        pull_obj = self.pool.get('procurement.rule')
+        push_obj = self.pool.get('stock.location.path')
+        #rename location
+        location_id = warehouse.lot_stock_id.location_id.id
+        location_obj.write(cr, uid, location_id, {'name': code}, context=context)
+        #rename route and push-pull rules
+        for route in warehouse.route_ids:
+            route_obj.write(cr, uid, route.id, {'name': route.name.replace(warehouse.name, name, 1)}, context=context)
+            for pull in route.pull_ids:
+                pull_obj.write(cr, uid, pull.id, {'name': pull.name.replace(warehouse.name, name, 1)}, context=context)
+            for push in route.push_ids:
+                push_obj.write(cr, uid, push.id, {'name': pull.name.replace(warehouse.name, name, 1)}, context=context)
+        #change the mto pull rule name
+        if warehouse.mto_pull_id.id:
+            pull_obj.write(cr, uid, warehouse.mto_pull_id.id, {'name': warehouse.mto_pull_id.name.replace(warehouse.name, name, 1)}, context=context)
+
+    def _check_delivery_resupply(self, cr, uid, warehouse, new_location, change_to_multiple, context=None):
+        """ Will check if the resupply routes from this warehouse follow the changes of number of delivery steps """
+        #Check routes that are being delivered by this warehouse and change the rule going to transit location
+        route_obj = self.pool.get("stock.location.route")
+        pull_obj = self.pool.get("procurement.rule")
+        routes = route_obj.search(cr, uid, [('supplier_wh_id','=', warehouse.id)], context=context)
+        pulls = pull_obj.search(cr, uid, ['&', ('route_id', 'in', routes), ('location_id.usage', '=', 'transit')], context=context)
+        if pulls:
+            pull_obj.write(cr, uid, pulls, {'location_src_id': new_location, 'procure_method': change_to_multiple and "make_to_order" or "make_to_stock"}, context=context)
+        # Create or clean MTO rules
+        mto_route_id = self._get_mto_route(cr, uid, context=context)
+        if not change_to_multiple:
+            # If single delivery we should create the necessary MTO rules for the resupply 
+            # pulls = pull_obj.search(cr, uid, ['&', ('route_id', '=', mto_route_id), ('location_id.usage', '=', 'transit'), ('location_src_id', '=', warehouse.lot_stock_id.id)], context=context)
+            pull_recs = pull_obj.browse(cr, uid, pulls, context=context)
+            transfer_locs = list(set([x.location_id for x in pull_recs]))
+            vals = [(warehouse.lot_stock_id , x, warehouse.out_type_id.id) for x in transfer_locs]
+            mto_pull_vals = self._get_mto_pull_rule(cr, uid, warehouse, vals, context=context)
+            for mto_pull_val in mto_pull_vals:
+                pull_obj.create(cr, uid, mto_pull_val, context=context)
+        else:
+            # We need to delete all the MTO pull rules, otherwise they risk to be used in the system
+            pulls = pull_obj.search(cr, uid, ['&', ('route_id', '=', mto_route_id), ('location_id.usage', '=', 'transit'), ('location_src_id', '=', warehouse.lot_stock_id.id)], context=context)
+            if pulls:
+                pull_obj.unlink(cr, uid, pulls, context=context)
+
+    def _check_reception_resupply(self, cr, uid, warehouse, new_location, context=None):
+        """
+            Will check if the resupply routes to this warehouse follow the changes of number of receipt steps
+        """
+        #Check routes that are being delivered by this warehouse and change the rule coming from transit location
+        route_obj = self.pool.get("stock.location.route")
+        pull_obj = self.pool.get("procurement.rule")
+        routes = route_obj.search(cr, uid, [('supplied_wh_id','=', warehouse.id)], context=context)
+        pulls= pull_obj.search(cr, uid, ['&', ('route_id', 'in', routes), ('location_src_id.usage', '=', 'transit')])
+        if pulls:
+            pull_obj.write(cr, uid, pulls, {'location_id': new_location}, context=context)
+
+    def _check_resupply(self, cr, uid, warehouse, reception_new, delivery_new, context=None):
+        if reception_new:
+            old_val = warehouse.reception_steps
+            new_val = reception_new
+            change_to_one = (old_val != 'one_step' and new_val == 'one_step')
+            change_to_multiple = (old_val == 'one_step' and new_val != 'one_step')
+            if change_to_one or change_to_multiple:
+                new_location = change_to_one and warehouse.lot_stock_id.id or warehouse.wh_input_stock_loc_id.id
+                self._check_reception_resupply(cr, uid, warehouse, new_location, context=context)
+        if delivery_new:
+            old_val = warehouse.delivery_steps
+            new_val = delivery_new
+            change_to_one = (old_val != 'ship_only' and new_val == 'ship_only')
+            change_to_multiple = (old_val == 'ship_only' and new_val != 'ship_only')
+            if change_to_one or change_to_multiple:
+                new_location = change_to_one and warehouse.lot_stock_id.id or warehouse.wh_output_stock_loc_id.id 
+                self._check_delivery_resupply(cr, uid, warehouse, new_location, change_to_multiple, context=context)
+
+    def write(self, cr, uid, ids, vals, context=None):
+        if context is None:
+            context = {}
+        if isinstance(ids, (int, long)):
+            ids = [ids]
+        seq_obj = self.pool.get('ir.sequence')
+        route_obj = self.pool.get('stock.location.route')
+        context_with_inactive = context.copy()
+        context_with_inactive['active_test'] = False
+        for warehouse in self.browse(cr, uid, ids, context=context_with_inactive):
+            #first of all, check if we need to delete and recreate route
+            if vals.get('reception_steps') or vals.get('delivery_steps'):
+                #activate and deactivate location according to reception and delivery option
+                self.switch_location(cr, uid, warehouse.id, warehouse, vals.get('reception_steps', False), vals.get('delivery_steps', False), context=context)
+                # switch between route
+                self.change_route(cr, uid, ids, warehouse, vals.get('reception_steps', False), vals.get('delivery_steps', False), context=context_with_inactive)
+                # Check if we need to change something to resupply warehouses and associated MTO rules
+                self._check_resupply(cr, uid, warehouse, vals.get('reception_steps'), vals.get('delivery_steps'), context=context)
+                warehouse.refresh()
+            if vals.get('code') or vals.get('name'):
+                name = warehouse.name
+                #rename sequence
+                if vals.get('name'):
+                    name = vals.get('name', warehouse.name)
+                self._handle_renaming(cr, uid, warehouse, name, vals.get('code', warehouse.code), context=context_with_inactive)
+                if warehouse.in_type_id:
+                    seq_obj.write(cr, uid, warehouse.in_type_id.sequence_id.id, {'name': name + _(' Sequence in'), 'prefix': vals.get('code', warehouse.code) + '\IN\\'}, context=context)
+                    seq_obj.write(cr, uid, warehouse.out_type_id.sequence_id.id, {'name': name + _(' Sequence out'), 'prefix': vals.get('code', warehouse.code) + '\OUT\\'}, context=context)
+                    seq_obj.write(cr, uid, warehouse.pack_type_id.sequence_id.id, {'name': name + _(' Sequence packing'), 'prefix': vals.get('code', warehouse.code) + '\PACK\\'}, context=context)
+                    seq_obj.write(cr, uid, warehouse.pick_type_id.sequence_id.id, {'name': name + _(' Sequence picking'), 'prefix': vals.get('code', warehouse.code) + '\PICK\\'}, context=context)
+                    seq_obj.write(cr, uid, warehouse.int_type_id.sequence_id.id, {'name': name + _(' Sequence internal'), 'prefix': vals.get('code', warehouse.code) + '\INT\\'}, context=context)
+        if vals.get('resupply_wh_ids') and not vals.get('resupply_route_ids'):
+            for cmd in vals.get('resupply_wh_ids'):
+                if cmd[0] == 6:
+                    new_ids = set(cmd[2])
+                    old_ids = set([wh.id for wh in warehouse.resupply_wh_ids])
+                    to_add_wh_ids = new_ids - old_ids
+                    if to_add_wh_ids:
+                        supplier_warehouses = self.browse(cr, uid, list(to_add_wh_ids), context=context)
+                        self._create_resupply_routes(cr, uid, warehouse, supplier_warehouses, warehouse.default_resupply_wh_id, context=context)
+                    to_remove_wh_ids = old_ids - new_ids
+                    if to_remove_wh_ids:
+                        to_remove_route_ids = route_obj.search(cr, uid, [('supplied_wh_id', '=', warehouse.id), ('supplier_wh_id', 'in', list(to_remove_wh_ids))], context=context)
+                        if to_remove_route_ids:
+                            route_obj.unlink(cr, uid, to_remove_route_ids, context=context)
+                else:
+                    #not implemented
+                    pass
+        if 'default_resupply_wh_id' in vals:
+            if vals.get('default_resupply_wh_id') == warehouse.id:
+                raise osv.except_osv(_('Warning'),_('The default resupply warehouse should be different than the warehouse itself!'))
+            if warehouse.default_resupply_wh_id:
+                #remove the existing resupplying route on the warehouse
+                to_remove_route_ids = route_obj.search(cr, uid, [('supplied_wh_id', '=', warehouse.id), ('supplier_wh_id', '=', warehouse.default_resupply_wh_id.id)], context=context)
+                for inter_wh_route_id in to_remove_route_ids:
+                    self.write(cr, uid, [warehouse.id], {'route_ids': [(3, inter_wh_route_id)]})
+            if vals.get('default_resupply_wh_id'):
+                #assign the new resupplying route on all products
+                to_assign_route_ids = route_obj.search(cr, uid, [('supplied_wh_id', '=', warehouse.id), ('supplier_wh_id', '=', vals.get('default_resupply_wh_id'))], context=context)
+                for inter_wh_route_id in to_assign_route_ids:
+                    self.write(cr, uid, [warehouse.id], {'route_ids': [(4, inter_wh_route_id)]})
+
+        return super(stock_warehouse, self).write(cr, uid, ids, vals=vals, context=context)
+
+    def get_all_routes_for_wh(self, cr, uid, warehouse, context=None):
+        route_obj = self.pool.get("stock.location.route")
+        all_routes = [route.id for route in warehouse.route_ids]
+        all_routes += route_obj.search(cr, uid, [('supplied_wh_id', '=', warehouse.id)], context=context)
+        all_routes += [warehouse.mto_pull_id.route_id.id]
+        return all_routes
+
+    def view_all_routes_for_wh(self, cr, uid, ids, context=None):
+        all_routes = []
+        for wh in self.browse(cr, uid, ids, context=context):
+            all_routes += self.get_all_routes_for_wh(cr, uid, wh, context=context)
+
+        domain = [('id', 'in', all_routes)]
+        return {
+            'name': _('Warehouse\'s Routes'),
+            'domain': domain,
+            'res_model': 'stock.location.route',
+            'type': 'ir.actions.act_window',
+            'view_id': False,
+            'view_mode': 'tree,form',
+            'view_type': 'form',
+            'limit': 20
+        }
+
+
+class stock_location_path(osv.osv):
+    _name = "stock.location.path"
+    _description = "Pushed Flows"
+    _order = "name"
+
+    def _get_rules(self, cr, uid, ids, context=None):
+        res = []
+        for route in self.browse(cr, uid, ids, context=context):
+            res += [x.id for x in route.push_ids]
+        return res
 
     _columns = {
-        'backorder_id': fields.many2one('stock.picking.in', 'Back Order of', states={'done':[('readonly', True)], 'cancel':[('readonly',True)]}, help="If this shipment was split, then this field links to the shipment which contains the already processed part.", select=True),
-        'state': fields.selection(
-            [('draft', 'Draft'),
-            ('auto', 'Waiting Another Operation'),
-            ('confirmed', 'Waiting Availability'),
-            ('assigned', 'Ready to Receive'),
-            ('done', 'Received'),
-            ('cancel', 'Cancelled'),],
-            'Status', readonly=True, select=True,
-            help="""* Draft: not confirmed yet and will not be scheduled until confirmed\n
-                 * Waiting Another Operation: waiting for another move to proceed before it becomes automatically available (e.g. in Make-To-Order flows)\n
-                 * Waiting Availability: still waiting for the availability of products\n
-                 * Ready to Receive: products reserved, simply waiting for confirmation.\n
-                 * Received: has been processed, can't be modified or cancelled anymore\n
-                 * Cancelled: has been cancelled, can't be confirmed anymore"""),
+        'name': fields.char('Operation Name', required=True),
+        'company_id': fields.many2one('res.company', 'Company'),
+        'route_id': fields.many2one('stock.location.route', 'Route'),
+        'location_from_id': fields.many2one('stock.location', 'Source Location', ondelete='cascade', select=1, required=True),
+        'location_dest_id': fields.many2one('stock.location', 'Destination Location', ondelete='cascade', select=1, required=True),
+        'delay': fields.integer('Delay (days)', help="Number of days to do this transition"),
+        'picking_type_id': fields.many2one('stock.picking.type', 'Type of the new Operation', required=True, help="This is the picking type associated with the different pickings"), 
+        'auto': fields.selection(
+            [('auto','Automatic Move'), ('manual','Manual Operation'),('transparent','Automatic No Step Added')],
+            'Automatic Move',
+            required=True, select=1,
+            help="This is used to define paths the product has to follow within the location tree.\n" \
+                "The 'Automatic Move' value will create a stock move after the current one that will be "\
+                "validated automatically. With 'Manual Operation', the stock move has to be validated "\
+                "by a worker. With 'Automatic No Step Added', the location is replaced in the original move."
+            ),
+        'propagate': fields.boolean('Propagate cancel and split', help='If checked, when the previous move is cancelled or split, the move generated by this move will too'),
+        'active': fields.boolean('Active', help="If unchecked, it will allow you to hide the rule without removing it."),
+        'warehouse_id': fields.many2one('stock.warehouse', 'Warehouse'),
+        'route_sequence': fields.related('route_id', 'sequence', string='Route Sequence',
+            store={
+                'stock.location.route': (_get_rules, ['sequence'], 10),
+                'stock.location.path': (lambda self, cr, uid, ids, c={}: ids, ['route_id'], 10),
+        }),
+        'sequence': fields.integer('Sequence'),
     }
     _defaults = {
-        'type': 'in',
+        'auto': 'auto',
+        'delay': 0,
+        'company_id': lambda self, cr, uid, c: self.pool.get('res.company')._company_default_get(cr, uid, 'procurement.order', context=c),
+        'propagate': True,
+        'active': True,
     }
 
-class stock_picking_out(osv.osv):
-    _name = "stock.picking.out"
-    _inherit = "stock.picking"
-    _table = "stock_picking"
-    _description = "Delivery Orders"
+    def _prepare_push_apply(self, cr, uid, rule, move, context=None):
+        newdate = (datetime.strptime(move.date_expected, DEFAULT_SERVER_DATETIME_FORMAT) + relativedelta.relativedelta(days=rule.delay or 0)).strftime(DEFAULT_SERVER_DATETIME_FORMAT)
+        return {
+                'location_id': move.location_dest_id.id,
+                'location_dest_id': rule.location_dest_id.id,
+                'date': newdate,
+                'company_id': rule.company_id and rule.company_id.id or False,
+                'date_expected': newdate,
+                'picking_id': False,
+                'picking_type_id': rule.picking_type_id and rule.picking_type_id.id or False,
+                'propagate': rule.propagate,
+                'push_rule_id': rule.id,
+                'warehouse_id': rule.warehouse_id and rule.warehouse_id.id or False,
+            }
 
-    def search(self, cr, user, args, offset=0, limit=None, order=None, context=None, count=False):
-        return self.pool.get('stock.picking').search(cr, user, args, offset, limit, order, context, count)
+    def _apply(self, cr, uid, rule, move, context=None):
+        move_obj = self.pool.get('stock.move')
+        newdate = (datetime.strptime(move.date_expected, DEFAULT_SERVER_DATETIME_FORMAT) + relativedelta.relativedelta(days=rule.delay or 0)).strftime(DEFAULT_SERVER_DATETIME_FORMAT)
+        if rule.auto == 'transparent':
+            old_dest_location = move.location_dest_id.id
+            move_obj.write(cr, uid, [move.id], {
+                'date': newdate,
+                'date_expected': newdate,
+                'location_dest_id': rule.location_dest_id.id
+            })
+            move.refresh()
+            #avoid looping if a push rule is not well configured
+            if rule.location_dest_id.id != old_dest_location:
+                #call again push_apply to see if a next step is defined
+                move_obj._push_apply(cr, uid, [move], context=context)
+        else:
+            vals = self._prepare_push_apply(cr, uid, rule, move, context=context)
+            move_id = move_obj.copy(cr, uid, move.id, vals, context=context)
+            move_obj.write(cr, uid, [move.id], {
+                'move_dest_id': move_id,
+            })
+            move_obj.action_confirm(cr, uid, [move_id], context=None)
+
+
+# -------------------------
+# Packaging related stuff
+# -------------------------
+
+from openerp.report import report_sxw
+
+class stock_package(osv.osv):
+    """
+    These are the packages, containing quants and/or other packages
+    """
+    _name = "stock.quant.package"
+    _description = "Physical Packages"
+    _parent_name = "parent_id"
+    _parent_store = True
+    _parent_order = 'name'
+    _order = 'parent_left'
 
-    def read(self, cr, uid, ids, fields=None, context=None, load='_classic_read'):
-        return self.pool.get('stock.picking').read(cr, uid, ids, fields=fields, context=context, load=load)
+    def name_get(self, cr, uid, ids, context=None):
+        res = self._complete_name(cr, uid, ids, 'complete_name', None, context=context)
+        return res.items()
 
-    def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None, orderby=False):
-        return self.pool['stock.picking'].read_group(cr, uid, domain, fields, groupby, offset=offset, limit=limit, context=context, orderby=orderby)
+    def _complete_name(self, cr, uid, ids, name, args, context=None):
+        """ Forms complete name of location from parent location to child location.
+        @return: Dictionary of values
+        """
+        res = {}
+        for m in self.browse(cr, uid, ids, context=context):
+            res[m.id] = m.name
+            parent = m.parent_id
+            while parent:
+                res[m.id] = parent.name + ' / ' + res[m.id]
+                parent = parent.parent_id
+        return res
 
-    def check_access_rights(self, cr, uid, operation, raise_exception=True):
-        #override in order to redirect the check of acces rights on the stock.picking object
-        return self.pool.get('stock.picking').check_access_rights(cr, uid, operation, raise_exception=raise_exception)
+    def _get_packages(self, cr, uid, ids, context=None):
+        """Returns packages from quants for store"""
+        res = set()
+        for quant in self.browse(cr, uid, ids, context=context):
+            if quant.package_id:
+                res.add(quant.package_id.id)
+        return list(res)
+
+    def _get_packages_to_relocate(self, cr, uid, ids, context=None):
+        res = set()
+        for pack in self.browse(cr, uid, ids, context=context):
+            res.add(pack.id)
+            if pack.parent_id:
+                res.add(pack.parent_id.id)
+        return list(res)
+
+    def _get_package_info(self, cr, uid, ids, name, args, context=None):
+        default_company_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.id
+        res = dict((res_id, {'location_id': False, 'company_id': default_company_id, 'owner_id': False}) for res_id in ids)
+        for pack in self.browse(cr, uid, ids, context=context):
+            if pack.quant_ids:
+                res[pack.id]['location_id'] = pack.quant_ids[0].location_id.id
+                res[pack.id]['owner_id'] = pack.quant_ids[0].owner_id and pack.quant_ids[0].owner_id.id or False
+                res[pack.id]['company_id'] = pack.quant_ids[0].company_id.id
+            elif pack.children_ids:
+                res[pack.id]['location_id'] = pack.children_ids[0].location_id and pack.children_ids[0].location_id.id or False
+                res[pack.id]['owner_id'] = pack.children_ids[0].owner_id and pack.children_ids[0].owner_id.id or False
+                res[pack.id]['company_id'] = pack.children_ids[0].company_id and pack.children_ids[0].company_id.id or False
+        return res
 
-    def check_access_rule(self, cr, uid, ids, operation, context=None):
-        #override in order to redirect the check of acces rules on the stock.picking object
-        return self.pool.get('stock.picking').check_access_rule(cr, uid, ids, operation, context=context)
+    _columns = {
+        'name': fields.char('Package Reference', select=True, copy=False),
+        'complete_name': fields.function(_complete_name, type='char', string="Package Name",),
+        'parent_left': fields.integer('Left Parent', select=1),
+        'parent_right': fields.integer('Right Parent', select=1),
+        'packaging_id': fields.many2one('product.packaging', 'Packaging', help="This field should be completed only if everything inside the package share the same product, otherwise it doesn't really makes sense.", select=True),
+        'ul_id': fields.many2one('product.ul', 'Logistic Unit'),
+        'location_id': fields.function(_get_package_info, type='many2one', relation='stock.location', string='Location', multi="package",
+                                    store={
+                                       'stock.quant': (_get_packages, ['location_id'], 10),
+                                       'stock.quant.package': (_get_packages_to_relocate, ['quant_ids', 'children_ids', 'parent_id'], 10),
+                                    }, readonly=True, select=True),
+        'quant_ids': fields.one2many('stock.quant', 'package_id', 'Bulk Content', readonly=True),
+        'parent_id': fields.many2one('stock.quant.package', 'Parent Package', help="The package containing this item", ondelete='restrict', readonly=True),
+        'children_ids': fields.one2many('stock.quant.package', 'parent_id', 'Contained Packages', readonly=True),
+        'company_id': fields.function(_get_package_info, type="many2one", relation='res.company', string='Company', multi="package", 
+                                    store={
+                                       'stock.quant': (_get_packages, ['company_id'], 10),
+                                       'stock.quant.package': (_get_packages_to_relocate, ['quant_ids', 'children_ids', 'parent_id'], 10),
+                                    }, readonly=True, select=True),
+        'owner_id': fields.function(_get_package_info, type='many2one', relation='res.partner', string='Owner', multi="package",
+                                store={
+                                       'stock.quant': (_get_packages, ['owner_id'], 10),
+                                       'stock.quant.package': (_get_packages_to_relocate, ['quant_ids', 'children_ids', 'parent_id'], 10),
+                                    }, readonly=True, select=True),
+    }
+    _defaults = {
+        'name': lambda self, cr, uid, context: self.pool.get('ir.sequence').get(cr, uid, 'stock.quant.package') or _('Unknown Pack')
+    }
 
-    def _workflow_trigger(self, cr, uid, ids, trigger, context=None):
-        #override in order to trigger the workflow of stock.picking at the end of create, write and unlink operation
-        #instead of it's own workflow (which is not existing)
-        return self.pool.get('stock.picking')._workflow_trigger(cr, uid, ids, trigger, context=context)
+    def _check_location_constraint(self, cr, uid, packs, context=None):
+        '''checks that all quants in a package are stored in the same location. This function cannot be used
+           as a constraint because it needs to be checked on pack operations (they may not call write on the
+           package)
+        '''
+        quant_obj = self.pool.get('stock.quant')
+        for pack in packs:
+            parent = pack
+            while parent.parent_id:
+                parent = parent.parent_id
+            quant_ids = self.get_content(cr, uid, [parent.id], context=context)
+            quants = [x for x in quant_obj.browse(cr, uid, quant_ids, context=context) if x.qty > 0]
+            location_id = quants and quants[0].location_id.id or False
+            if not [quant.location_id.id == location_id for quant in quants]:
+                raise osv.except_osv(_('Error'), _('Everything inside a package should be in the same location'))
+        return True
 
-    def _workflow_signal(self, cr, uid, ids, signal, context=None):
-        #override in order to fire the workflow signal on given stock.picking workflow instance
-        #instead of it's own workflow (which is not existing)
-        return self.pool.get('stock.picking')._workflow_signal(cr, uid, ids, signal, context=context)
+    def action_print(self, cr, uid, ids, context=None):
+        context = dict(context or {}, active_ids=ids)
+        return self.pool.get("report").get_action(cr, uid, ids, 'stock.report_package_barcode_small', context=context)
+    
+    
+    def unpack(self, cr, uid, ids, context=None):
+        quant_obj = self.pool.get('stock.quant')
+        for package in self.browse(cr, uid, ids, context=context):
+            quant_ids = [quant.id for quant in package.quant_ids]
+            quant_obj.write(cr, uid, quant_ids, {'package_id': package.parent_id.id or False}, context=context)
+            children_package_ids = [child_package.id for child_package in package.children_ids]
+            self.write(cr, uid, children_package_ids, {'parent_id': package.parent_id.id or False}, context=context)
+        #delete current package since it contains nothing anymore
+        self.unlink(cr, uid, ids, context=context)
+        return self.pool.get('ir.actions.act_window').for_xml_id(cr, uid, 'stock', 'action_package_view', context=context)
+
+    def get_content(self, cr, uid, ids, context=None):
+        child_package_ids = self.search(cr, uid, [('id', 'child_of', ids)], context=context)
+        return self.pool.get('stock.quant').search(cr, uid, [('package_id', 'in', child_package_ids)], context=context)
+
+    def get_content_package(self, cr, uid, ids, context=None):
+        quants_ids = self.get_content(cr, uid, ids, context=context)
+        res = self.pool.get('ir.actions.act_window').for_xml_id(cr, uid, 'stock', 'quantsact', context=context)
+        res['domain'] = [('id', 'in', quants_ids)]
+        return res
 
-    def message_post(self, *args, **kwargs):
-        """Post the message on stock.picking to be able to see it in the form view when using the chatter"""
-        return self.pool.get('stock.picking').message_post(*args, **kwargs)
+    def _get_product_total_qty(self, cr, uid, package_record, product_id, context=None):
+        ''' find the total of given product 'product_id' inside the given package 'package_id'''
+        quant_obj = self.pool.get('stock.quant')
+        all_quant_ids = self.get_content(cr, uid, [package_record.id], context=context)
+        total = 0
+        for quant in quant_obj.browse(cr, uid, all_quant_ids, context=context):
+            if quant.product_id.id == product_id:
+                total += quant.qty
+        return total
+
+    def _get_all_products_quantities(self, cr, uid, package_id, context=None):
+        '''This function computes the different product quantities for the given package
+        '''
+        quant_obj = self.pool.get('stock.quant')
+        res = {}
+        for quant in quant_obj.browse(cr, uid, self.get_content(cr, uid, package_id, context=context)):
+            if quant.product_id.id not in res:
+                res[quant.product_id.id] = 0
+            res[quant.product_id.id] += quant.qty
+        return res
 
-    def message_subscribe(self, *args, **kwargs):
-        """Send the subscribe action on stock.picking model as it uses _name in request"""
-        return self.pool.get('stock.picking').message_subscribe(*args, **kwargs)
+    def copy_pack(self, cr, uid, id, default_pack_values=None, default=None, context=None):
+        stock_pack_operation_obj = self.pool.get('stock.pack.operation')
+        if default is None:
+            default = {}
+        new_package_id = self.copy(cr, uid, id, default_pack_values, context=context)
+        default['result_package_id'] = new_package_id
+        op_ids = stock_pack_operation_obj.search(cr, uid, [('result_package_id', '=', id)], context=context)
+        for op_id in op_ids:
+            stock_pack_operation_obj.copy(cr, uid, op_id, default, context=context)
+
+
+class stock_pack_operation(osv.osv):
+    _name = "stock.pack.operation"
+    _description = "Packing Operation"
+
+    def _get_remaining_prod_quantities(self, cr, uid, operation, context=None):
+        '''Get the remaining quantities per product on an operation with a package. This function returns a dictionary'''
+        #if the operation doesn't concern a package, it's not relevant to call this function
+        if not operation.package_id or operation.product_id:
+            return {operation.product_id.id: operation.remaining_qty}
+        #get the total of products the package contains
+        res = self.pool.get('stock.quant.package')._get_all_products_quantities(cr, uid, operation.package_id.id, context=context)
+        #reduce by the quantities linked to a move
+        for record in operation.linked_move_operation_ids:
+            if record.move_id.product_id.id not in res:
+                res[record.move_id.product_id.id] = 0
+            res[record.move_id.product_id.id] -= record.qty
+        return res
 
-    def message_unsubscribe(self, *args, **kwargs):
-        """Send the unsubscribe action on stock.picking model to match with subscribe"""
-        return self.pool.get('stock.picking').message_unsubscribe(*args, **kwargs)
+    def _get_remaining_qty(self, cr, uid, ids, name, args, context=None):
+        uom_obj = self.pool.get('product.uom')
+        res = {}
+        for ops in self.browse(cr, uid, ids, context=context):
+            res[ops.id] = 0
+            if ops.package_id and not ops.product_id:
+                #dont try to compute the remaining quantity for packages because it's not relevant (a package could include different products).
+                #should use _get_remaining_prod_quantities instead
+                continue
+            else:
+                qty = ops.product_qty
+                if ops.product_uom_id:
+                    qty = uom_obj._compute_qty_obj(cr, uid, ops.product_uom_id, ops.product_qty, ops.product_id.uom_id, context=context)
+                for record in ops.linked_move_operation_ids:
+                    qty -= record.qty
+                res[ops.id] = qty
+        return res
 
-    def default_get(self, cr, uid, fields_list, context=None):
-        # merge defaults from stock.picking with possible defaults defined on stock.picking.out
-        defaults = self.pool['stock.picking'].default_get(cr, uid, fields_list, context=context)
-        out_defaults = super(stock_picking_out, self).default_get(cr, uid, fields_list, context=context)
-        defaults.update(out_defaults)
-        return defaults
+    def product_id_change(self, cr, uid, ids, product_id, product_uom_id, product_qty, context=None):
+        res = self.on_change_tests(cr, uid, ids, product_id, product_uom_id, product_qty, context=context)
+        if product_id and not product_uom_id:
+            product = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
+            res['value']['product_uom_id'] = product.uom_id.id
+        return res
+
+    def on_change_tests(self, cr, uid, ids, product_id, product_uom_id, product_qty, context=None):
+        res = {'value': {}}
+        uom_obj = self.pool.get('product.uom')
+        if product_id:
+            product = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
+            product_uom_id = product_uom_id or product.uom_id.id
+            selected_uom = uom_obj.browse(cr, uid, product_uom_id, context=context)
+            if selected_uom.category_id.id != product.uom_id.category_id.id:
+                res['warning'] = {
+                    'title': _('Warning: wrong UoM!'),
+                    'message': _('The selected UoM for product %s is not compatible with the UoM set on the product form. \nPlease choose an UoM within the same UoM category.') % (product.name)
+                }
+            if product_qty and 'warning' not in res:
+                rounded_qty = uom_obj._compute_qty(cr, uid, product_uom_id, product_qty, product_uom_id, round=True)
+                if rounded_qty != product_qty:
+                    res['warning'] = {
+                        'title': _('Warning: wrong quantity!'),
+                        'message': _('The chosen quantity for product %s is not compatible with the UoM rounding. It will be automatically converted at confirmation') % (product.name)
+                    }
+        return res
 
     _columns = {
-        'backorder_id': fields.many2one('stock.picking.out', 'Back Order of', states={'done':[('readonly', True)], 'cancel':[('readonly',True)]}, help="If this shipment was split, then this field links to the shipment which contains the already processed part.", select=True),
-        'state': fields.selection(
-            [('draft', 'Draft'),
-            ('auto', 'Waiting Another Operation'),
-            ('confirmed', 'Waiting Availability'),
-            ('assigned', 'Ready to Deliver'),
-            ('done', 'Delivered'),
-            ('cancel', 'Cancelled'),],
-            'Status', readonly=True, select=True,
-            help="""* Draft: not confirmed yet and will not be scheduled until confirmed\n
-                 * Waiting Another Operation: waiting for another move to proceed before it becomes automatically available (e.g. in Make-To-Order flows)\n
-                 * Waiting Availability: still waiting for the availability of products\n
-                 * Ready to Deliver: products reserved, simply waiting for confirmation.\n
-                 * Delivered: has been processed, can't be modified or cancelled anymore\n
-                 * Cancelled: has been cancelled, can't be confirmed anymore"""),
+        'picking_id': fields.many2one('stock.picking', 'Stock Picking', help='The stock operation where the packing has been made', required=True),
+        'product_id': fields.many2one('product.product', 'Product', ondelete="CASCADE"),  # 1
+        'product_uom_id': fields.many2one('product.uom', 'Product Unit of Measure'),
+        'product_qty': fields.float('Quantity', digits_compute=dp.get_precision('Product Unit of Measure'), required=True),
+        'qty_done': fields.float('Quantity Processed', digits_compute=dp.get_precision('Product Unit of Measure')),
+        'package_id': fields.many2one('stock.quant.package', 'Source Package'),  # 2
+        'lot_id': fields.many2one('stock.production.lot', 'Lot/Serial Number'),
+        'result_package_id': fields.many2one('stock.quant.package', 'Destination Package', help="If set, the operations are packed into this package", required=False, ondelete='cascade'),
+        'date': fields.datetime('Date', required=True),
+        'owner_id': fields.many2one('res.partner', 'Owner', help="Owner of the quants"),
+        #'update_cost': fields.boolean('Need cost update'),
+        'cost': fields.float("Cost", help="Unit Cost for this product line"),
+        'currency': fields.many2one('res.currency', string="Currency", help="Currency in which Unit cost is expressed", ondelete='CASCADE'),
+        'linked_move_operation_ids': fields.one2many('stock.move.operation.link', 'operation_id', string='Linked Moves', readonly=True, help='Moves impacted by this operation for the computation of the remaining quantities'),
+        'remaining_qty': fields.function(_get_remaining_qty, type='float', digits = 0, string="Remaining Qty", help="Remaining quantity in default UoM according to moves matched with this operation. "),
+        'location_id': fields.many2one('stock.location', 'Source Location', required=True),
+        'location_dest_id': fields.many2one('stock.location', 'Destination Location', required=True),
+        'processed': fields.selection([('true','Yes'), ('false','No')],'Has been processed?', required=True),
     }
+
     _defaults = {
-        'type': 'out',
+        'date': fields.date.context_today,
+        'qty_done': 0,
+        'processed': lambda *a: 'false',
+    }
+
+    def write(self, cr, uid, ids, vals, context=None):
+        context = context or {}
+        res = super(stock_pack_operation, self).write(cr, uid, ids, vals, context=context)
+        if isinstance(ids, (int, long)):
+            ids = [ids]
+        if not context.get("no_recompute"):
+            pickings = vals.get('picking_id') and [vals['picking_id']] or list(set([x.picking_id.id for x in self.browse(cr, uid, ids, context=context)]))
+            self.pool.get("stock.picking").do_recompute_remaining_quantities(cr, uid, pickings, context=context)
+        return res
+
+    def create(self, cr, uid, vals, context=None):
+        context = context or {}
+        res_id = super(stock_pack_operation, self).create(cr, uid, vals, context=context)
+        if vals.get("picking_id") and not context.get("no_recompute"):
+            self.pool.get("stock.picking").do_recompute_remaining_quantities(cr, uid, [vals['picking_id']], context=context)
+        return res_id
+
+    def action_drop_down(self, cr, uid, ids, context=None):
+        ''' Used by barcode interface to say that pack_operation has been moved from src location 
+            to destination location, if qty_done is less than product_qty than we have to split the
+            operation in two to process the one with the qty moved
+        '''
+        processed_ids = []
+        move_obj = self.pool.get("stock.move")
+        for pack_op in self.browse(cr, uid, ids, context=None):
+            if pack_op.product_id and pack_op.location_id and pack_op.location_dest_id:
+                move_obj.check_tracking_product(cr, uid, pack_op.product_id, pack_op.lot_id.id, pack_op.location_id, pack_op.location_dest_id, context=context)
+            op = pack_op.id
+            if pack_op.qty_done < pack_op.product_qty:
+                # we split the operation in two
+                op = self.copy(cr, uid, pack_op.id, {'product_qty': pack_op.qty_done, 'qty_done': pack_op.qty_done}, context=context)
+                self.write(cr, uid, [pack_op.id], {'product_qty': pack_op.product_qty - pack_op.qty_done, 'qty_done': 0, 'lot_id': False}, context=context)
+            processed_ids.append(op)
+        self.write(cr, uid, processed_ids, {'processed': 'true'}, context=context)
+
+    def create_and_assign_lot(self, cr, uid, id, name, context=None):
+        ''' Used by barcode interface to create a new lot and assign it to the operation
+        '''
+        obj = self.browse(cr,uid,id,context)
+        product_id = obj.product_id.id
+        val = {'product_id': product_id}
+        new_lot_id = False
+        if name:
+            lots = self.pool.get('stock.production.lot').search(cr, uid, ['&', ('name', '=', name), ('product_id', '=', product_id)], context=context)
+            if lots:
+                new_lot_id = lots[0]
+            val.update({'name': name})
+
+        if not new_lot_id:
+            new_lot_id = self.pool.get('stock.production.lot').create(cr, uid, val, context=context)
+        self.write(cr, uid, id, {'lot_id': new_lot_id}, context=context)
+
+    def _search_and_increment(self, cr, uid, picking_id, domain, filter_visible=False, visible_op_ids=False, increment=True, context=None):
+        '''Search for an operation with given 'domain' in a picking, if it exists increment the qty (+1) otherwise create it
+
+        :param domain: list of tuple directly reusable as a domain
+        context can receive a key 'current_package_id' with the package to consider for this operation
+        returns True
+        '''
+        if context is None:
+            context = {}
+
+        #if current_package_id is given in the context, we increase the number of items in this package
+        package_clause = [('result_package_id', '=', context.get('current_package_id', False))]
+        existing_operation_ids = self.search(cr, uid, [('picking_id', '=', picking_id)] + domain + package_clause, context=context)
+        todo_operation_ids = []
+        if existing_operation_ids:
+            if filter_visible:
+                todo_operation_ids = [val for val in existing_operation_ids if val in visible_op_ids]
+            else:
+                todo_operation_ids = existing_operation_ids
+        if todo_operation_ids:
+            #existing operation found for the given domain and picking => increment its quantity
+            operation_id = todo_operation_ids[0]
+            op_obj = self.browse(cr, uid, operation_id, context=context)
+            qty = op_obj.qty_done
+            if increment:
+                qty += 1
+            else:
+                qty -= 1 if qty >= 1 else 0
+                if qty == 0 and op_obj.product_qty == 0:
+                    #we have a line with 0 qty set, so delete it
+                    self.unlink(cr, uid, [operation_id], context=context)
+                    return False
+            self.write(cr, uid, [operation_id], {'qty_done': qty}, context=context)
+        else:
+            #no existing operation found for the given domain and picking => create a new one
+            picking_obj = self.pool.get("stock.picking")
+            picking = picking_obj.browse(cr, uid, picking_id, context=context)
+            values = {
+                'picking_id': picking_id,
+                'product_qty': 0,
+                'location_id': picking.location_id.id, 
+                'location_dest_id': picking.location_dest_id.id,
+                'qty_done': 1,
+                }
+            for key in domain:
+                var_name, dummy, value = key
+                uom_id = False
+                if var_name == 'product_id':
+                    uom_id = self.pool.get('product.product').browse(cr, uid, value, context=context).uom_id.id
+                update_dict = {var_name: value}
+                if uom_id:
+                    update_dict['product_uom_id'] = uom_id
+                values.update(update_dict)
+            operation_id = self.create(cr, uid, values, context=context)
+        return operation_id
+
+
+class stock_move_operation_link(osv.osv):
+    """
+    Table making the link between stock.moves and stock.pack.operations to compute the remaining quantities on each of these objects
+    """
+    _name = "stock.move.operation.link"
+    _description = "Link between stock moves and pack operations"
+
+    _columns = {
+        'qty': fields.float('Quantity', help="Quantity of products to consider when talking about the contribution of this pack operation towards the remaining quantity of the move (and inverse). Given in the product main uom."),
+        'operation_id': fields.many2one('stock.pack.operation', 'Operation', required=True, ondelete="cascade"),
+        'move_id': fields.many2one('stock.move', 'Move', required=True, ondelete="cascade"),
+        'reserved_quant_id': fields.many2one('stock.quant', 'Reserved Quant', help="Technical field containing the quant that created this link between an operation and a stock move. Used at the stock_move_obj.action_done() time to avoid seeking a matching quant again"),
+    }
+
+    def get_specific_domain(self, cr, uid, record, context=None):
+        '''Returns the specific domain to consider for quant selection in action_assign() or action_done() of stock.move,
+        having the record given as parameter making the link between the stock move and a pack operation'''
+
+        op = record.operation_id
+        domain = []
+        if op.package_id and op.product_id:
+            #if removing a product from a box, we restrict the choice of quants to this box
+            domain.append(('package_id', '=', op.package_id.id))
+        elif op.package_id:
+            #if moving a box, we allow to take everything from inside boxes as well
+            domain.append(('package_id', 'child_of', [op.package_id.id]))
+        else:
+            #if not given any information about package, we don't open boxes
+            domain.append(('package_id', '=', False))
+        #if lot info is given, we restrict choice to this lot otherwise we can take any
+        if op.lot_id:
+            domain.append(('lot_id', '=', op.lot_id.id))
+        #if owner info is given, we restrict to this owner otherwise we restrict to no owner
+        if op.owner_id:
+            domain.append(('owner_id', '=', op.owner_id.id))
+        else:
+            domain.append(('owner_id', '=', False))
+        return domain
+
+class stock_warehouse_orderpoint(osv.osv):
+    """
+    Defines Minimum stock rules.
+    """
+    _name = "stock.warehouse.orderpoint"
+    _description = "Minimum Inventory Rule"
+
+    def subtract_procurements(self, cr, uid, orderpoint, context=None):
+        '''This function returns quantity of product that needs to be deducted from the orderpoint computed quantity because there's already a procurement created with aim to fulfill it.
+        '''
+        qty = 0
+        uom_obj = self.pool.get("product.uom")
+        for procurement in orderpoint.procurement_ids:
+            if procurement.state in ('cancel', 'done'):
+                continue
+            procurement_qty = uom_obj._compute_qty_obj(cr, uid, procurement.product_uom, procurement.product_qty, procurement.product_id.uom_id, context=context)
+            for move in procurement.move_ids:
+                #need to add the moves in draft as they aren't in the virtual quantity + moves that have not been created yet
+                if move.state not in ('draft'):
+                    #if move is already confirmed, assigned or done, the virtual stock is already taking this into account so it shouldn't be deducted
+                    procurement_qty -= move.product_qty
+            qty += procurement_qty
+        return qty
+
+    def _check_product_uom(self, cr, uid, ids, context=None):
+        '''
+        Check if the UoM has the same category as the product standard UoM
+        '''
+        if not context:
+            context = {}
+
+        for rule in self.browse(cr, uid, ids, context=context):
+            if rule.product_id.uom_id.category_id.id != rule.product_uom.category_id.id:
+                return False
+        return True
+
+    def action_view_proc_to_process(self, cr, uid, ids, context=None):
+        act_obj = self.pool.get('ir.actions.act_window')
+        mod_obj = self.pool.get('ir.model.data')
+        proc_ids = self.pool.get('procurement.order').search(cr, uid, [('orderpoint_id', 'in', ids), ('state', 'not in', ('done', 'cancel'))], context=context)
+        result = mod_obj.get_object_reference(cr, uid, 'procurement', 'do_view_procurements')
+        if not result:
+            return False
+
+        result = act_obj.read(cr, uid, [result[1]], context=context)[0]
+        result['domain'] = "[('id', 'in', [" + ','.join(map(str, proc_ids)) + "])]"
+        return result
+
+    _columns = {
+        'name': fields.char('Name', required=True, copy=False),
+        'active': fields.boolean('Active', help="If the active field is set to False, it will allow you to hide the orderpoint without removing it."),
+        'logic': fields.selection([('max', 'Order to Max'), ('price', 'Best price (not yet active!)')], 'Reordering Mode', required=True),
+        'warehouse_id': fields.many2one('stock.warehouse', 'Warehouse', required=True, ondelete="cascade"),
+        'location_id': fields.many2one('stock.location', 'Location', required=True, ondelete="cascade"),
+        'product_id': fields.many2one('product.product', 'Product', required=True, ondelete='cascade', domain=[('type', '=', 'product')]),
+        'product_uom': fields.related('product_id', 'uom_id', type='many2one', relation='product.uom', string='Product Unit of Measure', readonly=True, required=True),
+        'product_min_qty': fields.float('Minimum Quantity', required=True,
+            digits_compute=dp.get_precision('Product Unit of Measure'),
+            help="When the virtual stock goes below the Min Quantity specified for this field, Odoo generates "\
+            "a procurement to bring the forecasted quantity to the Max Quantity."),
+        'product_max_qty': fields.float('Maximum Quantity', required=True,
+            digits_compute=dp.get_precision('Product Unit of Measure'),
+            help="When the virtual stock goes below the Min Quantity, Odoo generates "\
+            "a procurement to bring the forecasted quantity to the Quantity specified as Max Quantity."),
+        'qty_multiple': fields.float('Qty Multiple', required=True,
+            digits_compute=dp.get_precision('Product Unit of Measure'),
+            help="The procurement quantity will be rounded up to this multiple.  If it is 0, the exact quantity will be used.  "),
+        'procurement_ids': fields.one2many('procurement.order', 'orderpoint_id', 'Created Procurements'),
+        'group_id': fields.many2one('procurement.group', 'Procurement Group', help="Moves created through this orderpoint will be put in this procurement group. If none is given, the moves generated by procurement rules will be grouped into one big picking.", copy=False),
+        'company_id': fields.many2one('res.company', 'Company', required=True),
+    }
+    _defaults = {
+        'active': lambda *a: 1,
+        'logic': lambda *a: 'max',
+        'qty_multiple': lambda *a: 1,
+        'name': lambda self, cr, uid, context: self.pool.get('ir.sequence').get(cr, uid, 'stock.orderpoint') or '',
+        'product_uom': lambda self, cr, uid, context: context.get('product_uom', False),
+        'company_id': lambda self, cr, uid, context: self.pool.get('res.company')._company_default_get(cr, uid, 'stock.warehouse.orderpoint', context=context)
+    }
+    _sql_constraints = [
+        ('qty_multiple_check', 'CHECK( qty_multiple >= 0 )', 'Qty Multiple must be greater than or equal to zero.'),
+    ]
+    _constraints = [
+        (_check_product_uom, 'You have to select a product unit of measure in the same category than the default unit of measure of the product', ['product_id', 'product_uom']),
+    ]
+
+    def default_get(self, cr, uid, fields, context=None):
+        warehouse_obj = self.pool.get('stock.warehouse')
+        res = super(stock_warehouse_orderpoint, self).default_get(cr, uid, fields, context)
+        # default 'warehouse_id' and 'location_id'
+        if 'warehouse_id' not in res:
+            warehouse_ids = res.get('company_id') and warehouse_obj.search(cr, uid, [('company_id', '=', res['company_id'])], limit=1, context=context) or []
+            res['warehouse_id'] = warehouse_ids and warehouse_ids[0] or False
+        if 'location_id' not in res:
+            res['location_id'] = res.get('warehouse_id') and warehouse_obj.browse(cr, uid, res['warehouse_id'], context).lot_stock_id.id or False
+        return res
+
+    def onchange_warehouse_id(self, cr, uid, ids, warehouse_id, context=None):
+        """ Finds location id for changed warehouse.
+        @param warehouse_id: Changed id of warehouse.
+        @return: Dictionary of values.
+        """
+        if warehouse_id:
+            w = self.pool.get('stock.warehouse').browse(cr, uid, warehouse_id, context=context)
+            v = {'location_id': w.lot_stock_id.id}
+            return {'value': v}
+        return {}
+
+    def onchange_product_id(self, cr, uid, ids, product_id, context=None):
+        """ Finds UoM for changed product.
+        @param product_id: Changed id of product.
+        @return: Dictionary of values.
+        """
+        if product_id:
+            prod = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
+            d = {'product_uom': [('category_id', '=', prod.uom_id.category_id.id)]}
+            v = {'product_uom': prod.uom_id.id}
+            return {'value': v, 'domain': d}
+        return {'domain': {'product_uom': []}}
+
+class stock_picking_type(osv.osv):
+    _name = "stock.picking.type"
+    _description = "The picking type determines the picking view"
+    _order = 'sequence'
+
+    def open_barcode_interface(self, cr, uid, ids, context=None):
+        final_url = "/barcode/web/#action=stock.ui&picking_type_id=" + str(ids[0]) if len(ids) else '0'
+        return {'type': 'ir.actions.act_url', 'url': final_url, 'target': 'self'}
+
+    def _get_tristate_values(self, cr, uid, ids, field_name, arg, context=None):
+        picking_obj = self.pool.get('stock.picking')
+        res = {}
+        for picking_type_id in ids:
+            #get last 10 pickings of this type
+            picking_ids = picking_obj.search(cr, uid, [('picking_type_id', '=', picking_type_id), ('state', '=', 'done')], order='date_done desc', limit=10, context=context)
+            tristates = []
+            for picking in picking_obj.browse(cr, uid, picking_ids, context=context):
+                if picking.date_done > picking.date:
+                    tristates.insert(0, {'tooltip': picking.name or '' + ": " + _('Late'), 'value': -1})
+                elif picking.backorder_id:
+                    tristates.insert(0, {'tooltip': picking.name or '' + ": " + _('Backorder exists'), 'value': 0})
+                else:
+                    tristates.insert(0, {'tooltip': picking.name or '' + ": " + _('OK'), 'value': 1})
+            res[picking_type_id] = json.dumps(tristates)
+        return res
+
+    def _get_picking_count(self, cr, uid, ids, field_names, arg, context=None):
+        obj = self.pool.get('stock.picking')
+        domains = {
+            'count_picking_draft': [('state', '=', 'draft')],
+            'count_picking_waiting': [('state', '=', 'confirmed')],
+            'count_picking_ready': [('state', 'in', ('assigned', 'partially_available'))],
+            'count_picking': [('state', 'in', ('assigned', 'waiting', 'confirmed', 'partially_available'))],
+            'count_picking_late': [('min_date', '<', time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)), ('state', 'in', ('assigned', 'waiting', 'confirmed', 'partially_available'))],
+            'count_picking_backorders': [('backorder_id', '!=', False), ('state', 'in', ('confirmed', 'assigned', 'waiting', 'partially_available'))],
+        }
+        result = {}
+        for field in domains:
+            data = obj.read_group(cr, uid, domains[field] +
+                [('state', 'not in', ('done', 'cancel')), ('picking_type_id', 'in', ids)],
+                ['picking_type_id'], ['picking_type_id'], context=context)
+            count = dict(map(lambda x: (x['picking_type_id'] and x['picking_type_id'][0], x['picking_type_id_count']), data))
+            for tid in ids:
+                result.setdefault(tid, {})[field] = count.get(tid, 0)
+        for tid in ids:
+            if result[tid]['count_picking']:
+                result[tid]['rate_picking_late'] = result[tid]['count_picking_late'] * 100 / result[tid]['count_picking']
+                result[tid]['rate_picking_backorders'] = result[tid]['count_picking_backorders'] * 100 / result[tid]['count_picking']
+            else:
+                result[tid]['rate_picking_late'] = 0
+                result[tid]['rate_picking_backorders'] = 0
+        return result
+
+    def onchange_picking_code(self, cr, uid, ids, picking_code=False):
+        if not picking_code:
+            return False
+        
+        obj_data = self.pool.get('ir.model.data')
+        stock_loc = obj_data.xmlid_to_res_id(cr, uid, 'stock.stock_location_stock')
+        
+        result = {
+            'default_location_src_id': stock_loc,
+            'default_location_dest_id': stock_loc,
+        }
+        if picking_code == 'incoming':
+            result['default_location_src_id'] = obj_data.xmlid_to_res_id(cr, uid, 'stock.stock_location_suppliers')
+        elif picking_code == 'outgoing':
+            result['default_location_dest_id'] = obj_data.xmlid_to_res_id(cr, uid, 'stock.stock_location_customers')
+        return {'value': result}
+
+    def _get_name(self, cr, uid, ids, field_names, arg, context=None):
+        return dict(self.name_get(cr, uid, ids, context=context))
+
+    def name_get(self, cr, uid, ids, context=None):
+        """Overides orm name_get method to display 'Warehouse_name: PickingType_name' """
+        if context is None:
+            context = {}
+        if not isinstance(ids, list):
+            ids = [ids]
+        res = []
+        if not ids:
+            return res
+        for record in self.browse(cr, uid, ids, context=context):
+            name = record.name
+            if record.warehouse_id:
+                name = record.warehouse_id.name + ': ' +name
+            if context.get('special_shortened_wh_name'):
+                if record.warehouse_id:
+                    name = record.warehouse_id.name
+                else:
+                    name = _('Customer') + ' (' + record.name + ')'
+            res.append((record.id, name))
+        return res
+
+    def _default_warehouse(self, cr, uid, context=None):
+        user = self.pool.get('res.users').browse(cr, uid, uid, context)
+        res = self.pool.get('stock.warehouse').search(cr, uid, [('company_id', '=', user.company_id.id)], limit=1, context=context)
+        return res and res[0] or False
+
+    _columns = {
+        'name': fields.char('Picking Type Name', translate=True, required=True),
+        'complete_name': fields.function(_get_name, type='char', string='Name'),
+        'color': fields.integer('Color'),
+        'sequence': fields.integer('Sequence', help="Used to order the 'All Operations' kanban view"),
+        'sequence_id': fields.many2one('ir.sequence', 'Reference Sequence', required=True),
+        'default_location_src_id': fields.many2one('stock.location', 'Default Source Location'),
+        'default_location_dest_id': fields.many2one('stock.location', 'Default Destination Location'),
+        'code': fields.selection([('incoming', 'Suppliers'), ('outgoing', 'Customers'), ('internal', 'Internal')], 'Type of Operation', required=True),
+        'return_picking_type_id': fields.many2one('stock.picking.type', 'Picking Type for Returns'),
+        'warehouse_id': fields.many2one('stock.warehouse', 'Warehouse', ondelete='cascade'),
+        'active': fields.boolean('Active'),
+
+        # Statistics for the kanban view
+        'last_done_picking': fields.function(_get_tristate_values,
+            type='char',
+            string='Last 10 Done Pickings'),
+
+        'count_picking_draft': fields.function(_get_picking_count,
+            type='integer', multi='_get_picking_count'),
+        'count_picking_ready': fields.function(_get_picking_count,
+            type='integer', multi='_get_picking_count'),
+        'count_picking': fields.function(_get_picking_count,
+            type='integer', multi='_get_picking_count'),
+        'count_picking_waiting': fields.function(_get_picking_count,
+            type='integer', multi='_get_picking_count'),
+        'count_picking_late': fields.function(_get_picking_count,
+            type='integer', multi='_get_picking_count'),
+        'count_picking_backorders': fields.function(_get_picking_count,
+            type='integer', multi='_get_picking_count'),
+
+        'rate_picking_late': fields.function(_get_picking_count,
+            type='integer', multi='_get_picking_count'),
+        'rate_picking_backorders': fields.function(_get_picking_count,
+            type='integer', multi='_get_picking_count'),
+
+    }
+    _defaults = {
+        'warehouse_id': _default_warehouse,
+        'active': True,
     }
 
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: