code below is for V6 and updates both stock valuation and GL correctly to keep aligned.
# Average price computation #2/4/2013 - have raised bug report 1163060 (see also 610738) to get the change made below #included in the core product. The problem is that purchase returns were #not being catered for - code below does. #the bug exists in V7 so can be fixed there.
if pick.type == 'in': if product.id in product_avail: product_avail[product.id] += qty
#for a return, get the price from the move. if none, use average cost if pick.type == 'out': if move.price_unit: product_price = move.price_unit else: product_price = product.price_get('standard_price', context)[product.id]
# if qty > 0: if pick.type == 'in': if product.qty_available == 0 or (product.qty_available + qty) == 0 : new_std_price = new_price else: # Get the standard price amount_unit = product.price_get('standard_price', context)[product.id] new_std_price = ((amount_unit * product.qty_available)\ + (new_price * qty))/(product.qty_available + qty)
else: if product.qty_available == 0 or (product.qty_available - qty) == 0: new_std_price = new_price else: # Get the standard price amount_unit = product.price_get('standard_price', context)[product.id] new_std_price = ((amount_unit * product.qty_available)\ + (new_price * (0-qty)))/(product.qty_available -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})
#for a return, get the price from the move. if none, use average cost if move.price_unit: product_price = move.price_unit else: product_price = product.price_get('standard_price', context)[product.id]
if product.qty_available == 0 or (product.qty_available + qty) == 0 : new_std_price = new_price else: # Get the standard price amount_unit = product.price_get('standard_price', context)[product.id] new_std_price = ((amount_unit * product.qty_available)\ + (new_price * qty))/(product.qty_available + 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})
#writing the average cost to the move record
#so that it can be used for a sale return
code below is for V6 and updates both stock valuation and GL correctly to keep aligned.
# Average price computation
#2/4/ 2013 - have raised bug report 1163060 (see also 610738) to get the change made below
#included in the core product. The problem is that purchase returns were
#not being catered for - code below does.
#the bug exists in V7 so can be fixed there.
if pick.purchase_id 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:
if pick.type == 'in':
if product. qty_available == 0 or (product. qty_available + qty) == 0 :
new_std_ price = new_price
else:
# Get the standard price
amount_ unit = product. price_get( 'standard_ price', context) [product. id]
new_std_ price = ((amount_unit * product. qty_available) \
+ (new_price * qty))/( product. qty_available + qty)
#so that it can be used for a sale return