[MERGE] merge with latest stable
[odoo/odoo.git] / addons / stock / stock.py
index 9d6a4a7..49785fd 100644 (file)
@@ -111,46 +111,42 @@ class stock_location(osv.osv):
         @param field_names: Name of field
         @return: Dictionary of values
         """
-        prod_id = context and context.get('product_id', False)
-
-        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]))]))
-
+        if not context:
+            context = {}
+        
+        #Find currency
         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()
+        #Find list of product
+        prod_id = context.get('product_id', False)
+        if prod_id:
+            product_ids = [prod_id]
+        else:
+            cr.execute('select distinct product_id from stock_move where location_id in %s', (tuple(ids), ))
+            moves = cr.dictfetchall()
+            product_ids = [move['product_id'] for move in moves]
+            
+        #Compute result for all location
+        result = dict([(i, dict.fromkeys(field_names, 0.0)) for i in ids])
+        for loc_id in ids:
+            c = context.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
+            for prod in self.pool.get('product.product').browse(cr, uid, product_ids, context=c):
+                if 'stock_real' in field_names:
+                    result[loc_id]['stock_real'] += prod.qty_available
+                elif 'stock_virtual' in field_names:
+                    result[loc_id]['stock_virtual'] += prod.virtual_available
+                elif 'stock_real_value' in field_names:
+                    amount = prod.qty_available * prod.standard_price
+                    amount = currency_obj.round(cr, uid, currency, amount)
+                    result[loc_id]['stock_real_value'] += amount
+                elif 'stock_virtual_value' in field_names:
+                    amount = prod.virtual_available * prod.standard_price
+                    amount = currency_obj.round(cr, uid, currency, amount)
+                    result[loc_id]['stock_virtual_value'] += amount
         return result
-
+    
     _columns = {
         'name': fields.char('Location Name', size=64, required=True, translate=True),
         'active': fields.boolean('Active', help="By unchecking the active field, you may hide a location without deleting it."),
@@ -1047,6 +1043,9 @@ class stock_picking(osv.osv):
             for move_line in picking.move_lines:
                 if move_line.state == 'cancel':
                     continue
+                if move_line.scrapped:
+                    # do no invoice scrapped products
+                    continue
                 origin = move_line.picking_id.name or ''
                 if move_line.picking_id.origin:
                     origin += ':' + move_line.picking_id.origin
@@ -1056,14 +1055,12 @@ class stock_picking(osv.osv):
                     name = move_line.name
 
                 if inv_type in ('out_invoice', 'out_refund'):
-                    note = move_line.product_id.description_sale
                     account_id = move_line.product_id.product_tmpl_id.\
                             property_account_income.id
                     if not account_id:
                         account_id = move_line.product_id.categ_id.\
                                 property_account_income_categ.id
                 else:
-                    note = move_line.product_id.description_purchase
                     account_id = move_line.product_id.product_tmpl_id.\
                             property_account_expense.id
                     if not account_id:
@@ -1081,19 +1078,21 @@ class stock_picking(osv.osv):
                 if not uos_id and inv_type in ('out_invoice', 'out_refund'):
                     uos_id = move_line.product_uom.id
                 account_id = self.pool.get('account.fiscal.position').map_account(cr, uid, partner.property_account_position, account_id)
+                if move_line.price_unit != 0 and price_unit != move_line.price_unit:
+                    price_unit = move_line.price_unit
                 invoice_line_id = invoice_line_obj.create(cr, uid, {
                     'name': name,
-                    'note': note,
                     'origin': origin,
                     'invoice_id': invoice_id,
                     'uos_id': uos_id,
                     'product_id': move_line.product_id.id,
                     'account_id': account_id,
-                    'price_unit': move_line.price_unit,
+                    'price_unit': price_unit,
                     'discount': discount,
                     'quantity': move_line.product_uos_qty or move_line.product_qty,
                     'invoice_line_tax_id': [(6, 0, tax_ids)],
                     'account_analytic_id': account_analytic_id,
+                    'note': move_line.note
                 }, context=context)
                 self._invoice_line_hook(cr, uid, move_line, invoice_line_id)
 
@@ -2071,7 +2070,7 @@ class stock_move(osv.osv):
                 context = {}
             currency_ctx = dict(context, currency_id = move.company_id.currency_id.id)
             amount_unit = move.product_id.price_get('standard_price', currency_ctx)[move.product_id.id]
-            reference_amount = amount_unit * qty or 1.0
+            reference_amount = amount_unit * qty
 
         return reference_amount, reference_currency_id