average price computation

Bug #1163060 reported by Kevin McMenamin
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Odoo Addons (MOVED TO GITHUB)
New
Undecided
Unassigned

Bug Description

This affects both V6 and V7

stock/stock.py - do_partial - average price computation

The first if statement limits the average price computation to a pcik type of 'in'.

This means that a stock return for a purchase does not update average cost.
For example:

Receive 10 @ 20.00 SOH = 10 Avg Cost = $20.00 Value 200.00
Receive 10 @ 15.00 SOH = 20 Avg Cost = $17.50 Value 350.00
Return to Supplier
 Per curr logic: 5 @ $20.00 SOH = 15 Avg Cost = $17.50
 Should be: 5 @ $20.00 SOH = 15 Avg Cost = $ 16.67

Note - the return is correctly picking up the cost from the inwards move, so does that outwards move (ie the return) at the cost applicable for the receipt. Howver line 1256 means that this move is not catered for in the average cost computation

I suggest the following logic:

if pick.purchase_id and move.product_id.cost_method == 'average'
  if pick.type == 'in'
    [do as per current]
  else
  [similar logic except is an out not an in for qty calculations]

NOTE: I have checked the sale returns logic. The cost of the return move uses the current average cost of the product rather than the cost of the orinal move. While this is technically incorrect, both the average cost and the journal entries created align, so at least it doesn't make an immediate error. Ideally, the sale return move should use the cost from the original sale move, in which case the average cost will need to be recalculated as well.

Revision history for this message
Kevin McMenamin (kevin-mcmenamin) wrote :

also, new_price is calculated by two different formula in subsequqnt lines of code - presumably the second claculation overwrites the first, so why is the first calcuation even there?

Revision history for this message
Kevin McMenamin (kevin-mcmenamin) wrote :

ignore the above comment - I see how the logic works now

Revision history for this message
Kevin McMenamin (kevin-mcmenamin) wrote :
Download full text (3.3 KiB)

I have tested the code below for V6

# Average price computation
                #2/4/2013 - have raised bug report 1163060 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 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]

                    new_price = currency_obj.compute(cr, uid, product_currency,
                                move_currency_id, product_price)
                    new_price = uom_obj._compute_price(cr, uid, product_uom, new_price,
                                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)

                    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.
               ...

Read more...

Revision history for this message
Raphaël Valyi - http://www.akretion.com (rvalyi) wrote :

Hello Kevin,

you are probably hitting this acknowledged bug again https://bugs.launchpad.net/openobject-addons/+bug/610738
That would be cool if you could confirm, so we set that one as a duplicate and then keep the discussion in the unique place of the original bug about this.
Thank you for investigating, this is an important issue for an ERP indeed. Well at least that should be dealt with importance...

Revision history for this message
Amit Parik (amit-parik) wrote :

@Raphael : thanks for the remind the lp:610738, this issue is already reported there.

And its not working since 5.0, 6.0... and issue its persists on trunk, 7.0 as well.

See the FP's comment#9 on lp:610738 which is average costs must be changed only when you receive or produce products, not when you send the products. The rule is (Qty Rcvd * PO Price + Existing Qty * Current Cost) / (Qty Rcvd + Existing Qty).

We have done lots of discussion over there, also community are working on it, better we can discuss over there.

So I am marking this as a duplicate of lp:610738 and change the title of lp:610738 for latest.

Thank you!

Revision history for this message
Stephan Keller (r-skeller) wrote :

@Kevin: thanks for the code suggestion, I think it will work.
Stephan.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.