[IMP] Return of products average, cost method company dependent, quantity fields...
authorJosse Colpaert <jco@openerp.com>
Fri, 19 Apr 2013 10:17:01 +0000 (12:17 +0200)
committerJosse Colpaert <jco@openerp.com>
Fri, 19 Apr 2013 10:17:01 +0000 (12:17 +0200)
bzr revid: jco@openerp.com-20130419101701-mc61w0q5hvwq596w

addons/product/product.py
addons/stock/product.py
addons/stock/stock.py

index 52e5391..a7ed3b2 100644 (file)
@@ -303,12 +303,12 @@ class product_template(osv.osv):
         'standard_price': fields.property('', type = 'float', view_load=True, 
                                           help="Cost price of the product used for standard stock valuation in accounting and used as a base price on purchase orders.", 
                                           groups="base.group_user", string="Cost"),
-    #fields.float('Cost', digits_compute=dp.get_precision('Product Price'), help="Cost price of the product used for standard stock valuation in accounting and used as a base price on purchase orders.", groups="base.group_user"),
         'volume': fields.float('Volume', help="The volume in m3."),
         'weight': fields.float('Gross Weight', digits_compute=dp.get_precision('Stock Weight'), help="The gross weight in Kg."),
         'weight_net': fields.float('Net Weight', digits_compute=dp.get_precision('Stock Weight'), help="The net weight in Kg."),
-        'cost_method': fields.selection([('standard','Standard Price'), ('average','Average Price')], 'Costing Method', required=True,
-            help="Standard Price: The cost price is manually updated at the end of a specific period (usually every year). \nAverage Price: The cost price is recomputed at each incoming shipment."),
+        'cost_method': fields.property('', type='selection', view_load=True, selection = [('standard','Standard Price'), ('average','Average Price')],
+            help="Standard Price: The cost price is manually updated at the end of a specific period (usually every year). \nAverage Price: The cost price is recomputed at each incoming shipment.", 
+            string="Costing Method"),
         'warranty': fields.float('Warranty'),
         'sale_ok': fields.boolean('Can be Sold', help="Specify if the product can be selected in a sales order line."),
         'state': fields.selection([('',''),
index 29445f6..c0f7026 100644 (file)
@@ -202,7 +202,6 @@ class product_product(osv.osv):
         """
         if context is None:
             context = {}
-        
         location_obj = self.pool.get('stock.location')
         warehouse_obj = self.pool.get('stock.warehouse')
         shop_obj = self.pool.get('sale.shop')
@@ -277,12 +276,18 @@ class product_product(osv.osv):
         if date_values:
             where.append(tuple(date_values))
 
+        #It depends on the company of the user
+        user = self.pool.get("res.users").browse(cr, uid, uid, context=context)
+        where.append(user.company_id.id)
+
         prodlot_id = context.get('prodlot_id', False)
         prodlot_clause = ''
         if prodlot_id:
             prodlot_clause = ' and prodlot_id = %s '
             where += [prodlot_id]
 
+
+
         # TODO: perhaps merge in one query.
         if 'in' in what:
             # all moves from a location out of the set to a location in the set
@@ -293,6 +298,7 @@ class product_product(osv.osv):
                 'and location_dest_id IN %s '\
                 'and product_id IN %s '\
                 'and state IN %s ' + (date_str and 'and '+date_str+' ' or '') +' '\
+                'and company_id = %s '\
                 + prodlot_clause + 
                 'group by product_id,product_uom',tuple(where))
             results = cr.fetchall()
@@ -305,6 +311,7 @@ class product_product(osv.osv):
                 'and location_dest_id NOT IN %s '\
                 'and product_id  IN %s '\
                 'and state in %s ' + (date_str and 'and '+date_str+' ' or '') + ' '\
+                'and company_id = %s '\
                 + prodlot_clause + 
                 'group by product_id,product_uom',tuple(where))
             results2 = cr.fetchall()
index 764d9de..8c93c5a 100644 (file)
@@ -1277,6 +1277,38 @@ class stock_picking(osv.osv):
                                 {'price_unit': product_price,
                                  'price_currency_id': product_currency})
 
+                #Average price computation when returning products
+                if (pick.type == 'out') and (move.product_id.cost_method == 'average'):
+                    orig_move_id = move_obj.search(cr, uid, [('move_history_ids2', 'in', [move.id])], context=context)
+                    if len(orig_move_id) == 1:
+                        #Is it ok to repeat code here?
+                        move_orig = move_obj.browse(cr, uid, orig_move_id[0], context=context)
+                        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 in product_avail:
+                            product_avail[product.id] += qty
+                        else:
+                            product_avail[product.id] = product.qty_available
+                        if qty > 0:
+                            new_price = currency_obj.compute(cr, uid, product_currency,
+                                move_currency_id, move_orig.price_unit)
+                            new_price = uom_obj._compute_price(cr, uid, product_uom, new_price,
+                                product.uom_id.id)
+                            # 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': move_orig.price_unit,
+                                 'price_currency_id': product_currency})
 
             for move in too_few:
                 product_qty = move_product_qty[move.id]