[IMP] Always pass company_id in context, for lifo do id desc and round=False for compute_qty

bzr revid: jco@openerp.com-20130528085654-tkzhgg9td1yywgut
This commit is contained in:
Josse Colpaert 2013-05-28 10:56:54 +02:00
parent d8692c37ca
commit 8e1dba620a
3 changed files with 17 additions and 14 deletions

View File

@ -157,7 +157,7 @@ class product_uom(osv.osv):
('factor_gt_zero', 'CHECK (factor!=0)', 'The conversion ratio for a unit of measure cannot be 0!')
]
def _compute_qty(self, cr, uid, from_uom_id, qty, to_uom_id=False):
def _compute_qty(self, cr, uid, from_uom_id, qty, to_uom_id=False, round=True):
if not from_uom_id or not qty or not to_uom_id:
return qty
uoms = self.browse(cr, uid, [from_uom_id, to_uom_id])
@ -165,9 +165,9 @@ class product_uom(osv.osv):
from_unit, to_unit = uoms[0], uoms[-1]
else:
from_unit, to_unit = uoms[-1], uoms[0]
return self._compute_qty_obj(cr, uid, from_unit, qty, to_unit)
return self._compute_qty_obj(cr, uid, from_unit, qty, to_unit, round=round)
def _compute_qty_obj(self, cr, uid, from_unit, qty, to_unit, context=None):
def _compute_qty_obj(self, cr, uid, from_unit, qty, to_unit, round=True, context=None):
if context is None:
context = {}
if from_unit.category_id.id <> to_unit.category_id.id:
@ -177,7 +177,10 @@ class product_uom(osv.osv):
return qty
amount = qty / from_unit.factor
if to_unit:
amount = rounding(amount * to_unit.factor, to_unit.rounding)
if round:
amount = rounding(amount * to_unit.factor, to_unit.rounding)
else:
amount = amount * to_unit.factor
return amount
def _compute_price(self, cr, uid, from_uom_id, price, to_uom_id=False):

View File

@ -2694,11 +2694,10 @@ class stock_move(osv.osv):
company_id = move.company_id.id
ctx = context.copy()
user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
if company_id != user.company_id.id:
ctx['force_company'] = move.company_id.id
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_uom_qty = uom_obj._compute_qty(cr, uid, product_uom, product_qty, product.uom_id.id)
product_uom_qty = uom_obj._compute_qty(cr, uid, product_uom, product_qty, product.uom_id.id, round=False)
if not product.id in product_avail:
product_avail[product.id] = product.qty_available
@ -2708,7 +2707,7 @@ class stock_move(osv.osv):
fifo = (cost_method != 'lifo')
tuples = product_obj.get_stock_matchings_fifolifo(cr, uid, [product.id], product_qty, fifo,
product_uom, move.company_id.currency_id.id, context=context) #TODO Would be better to use price_currency_id for migration?
product_uom, move.company_id.currency_id.id, context=ctx) #TODO Would be better to use price_currency_id for migration?
price_amount = 0.0
amount = 0.0
#Write stock matchings
@ -2760,7 +2759,7 @@ class stock_move(osv.osv):
for out_mov in self.browse(cr, uid, moves, context=ctx):
if qty_to_go <= 0.0:
break
out_qty_converted = uom_obj._compute_qty(cr, uid, out_mov.product_uom.id, out_mov.qty_remaining, move.product_uom.id)
out_qty_converted = uom_obj._compute_qty(cr, uid, out_mov.product_uom.id, out_mov.qty_remaining, move.product_uom.id, round=False)
qty = 0.0
if out_qty_converted <= qty_to_go:
qty = out_qty_converted

View File

@ -56,7 +56,7 @@ class product_product (osv.osv):
total_price = 0.0
for move in move_obj.browse(cr, uid, mov_ids, context=context):
total_price += move.product_qty * move.price_unit
qty += uom_obj._compute_qty(cr, uid, move.product_uom.id, move.product_qty, prod.uom_id.id)
qty += uom_obj._compute_qty(cr, uid, move.product_uom.id, move.product_qty, prod.uom_id.id, round=False)
if qty > 0.0:
prod_obj.write(cr, uid, [prod.id], {'standard_price': total_price / qty}, context=context)
res = super(product_product, self).write(cr, uid, ids, vals, context=context)
@ -92,8 +92,9 @@ class product_product (osv.osv):
if fifo:
order = 'date, id'
else:
order = 'date desc, id'
move_in_ids = move_obj.search(cr, uid, [('qty_remaining', '>', 0.0),
order = 'date desc, id desc' #id also for yml tests
move_in_ids = move_obj.search(cr, uid, [('company_id', '=', company_id),
('qty_remaining', '>', 0.0),
('state', '=', 'done'),
('location_id.usage', '!=', 'internal'),
('location_dest_id.usage', '=', 'internal'),
@ -105,7 +106,7 @@ class product_product (osv.osv):
#Convert to UoM of product each time
uom_from = move.product_uom.id
qty_from = move.qty_remaining
product_qty = uom_obj._compute_qty(cr, uid, uom_from, qty_from, product_uom_id)
product_qty = uom_obj._compute_qty(cr, uid, uom_from, qty_from, product_uom_id, round=False)
#Convert currency from in move currency id to out move currency
if move.price_currency_id and (move.price_currency_id.id != currency_id):
new_price = currency_obj.compute(cr, uid, move.price_currency_id.id, currency_id,
@ -160,7 +161,7 @@ class stock_move(osv.osv):
matches = match_obj.search(cr, uid, [('move_in_id', '=', move.id)], context=context)
qty = move.product_qty
for match in match_obj.browse(cr, uid, matches, context=context):
qty -= uom_obj._compute_qty(cr, uid, match.move_out_id.product_uom.id, match.qty, move.product_uom.id)
qty -= uom_obj._compute_qty(cr, uid, match.move_out_id.product_uom.id, match.qty, move.product_uom.id, round=False)
res[move.id] = qty
elif move_out:
#Search all matchings, but from the out side