diff --git a/addons/stock/stock.py b/addons/stock/stock.py index 8c93c5a5ae0..3f491af644c 100644 --- a/addons/stock/stock.py +++ b/addons/stock/stock.py @@ -1243,14 +1243,28 @@ class stock_picking(osv.osv): too_few.append(move) else: too_many.append(move) - + + #Change cost price according to + user = self.pool.get('res.users').browse(cr, uid, uid, context=context) + product = product_obj.browse(cr, uid, move.product_id.id, context=context) + company_id = move.company_id.id + ctx = context.copy() + if company_id != user.company_id.id: + #test if I could do this with a read instead + ctx['force_company'] = move.company_id.id + product = product_obj.browse(cr, uid, move.product_id.id, context=ctx) + cost_method = product.cost_method + product_price = product.standard_price + else: + cost_method = move.product_id.cost_method + # Average price computation - if (pick.type == 'in') and (move.product_id.cost_method == 'average'): - product = product_obj.browse(cr, uid, move.product_id.id) + if (pick.type == 'in') and (cost_method == 'average'): + 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: @@ -1264,12 +1278,12 @@ class stock_picking(osv.osv): if product.qty_available <= 0: new_std_price = new_price else: - # Get the standard price + # Get the standard price -> This should be company_dependent 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}) + # Write the field according to price type field, company dependence in ctx + product_obj.write(cr, uid, [product.id], {'standard_price': new_std_price}, context=ctx) # Record the values that were chosen in the wizard, so they can be # used for inventory valuation if real-time valuation is enabled. @@ -1278,7 +1292,7 @@ class stock_picking(osv.osv): 'price_currency_id': product_currency}) #Average price computation when returning products - if (pick.type == 'out') and (move.product_id.cost_method == 'average'): + if (pick.type == 'out') and (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? @@ -1292,6 +1306,7 @@ class stock_picking(osv.osv): 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) @@ -1299,10 +1314,12 @@ class stock_picking(osv.osv): 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}) + #Check new stock is not zero or negative: + if (product_avail[product.id]- qty) > 0: + 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.