[IMP] Make sure consumed_for is set before doing action_done

bzr revid: jco@openerp.com-20140121093027-2h6xlgouhg49sr76
This commit is contained in:
Josse Colpaert 2014-01-21 10:30:27 +01:00
parent 24f6ed8973
commit 96120dcdbd
3 changed files with 53 additions and 36 deletions

View File

@ -796,29 +796,8 @@ class mrp_production(osv.osv):
if (produced_product.scrapped) or (produced_product.product_id.id != production.product_id.id):
continue
produced_qty += produced_product.product_qty
if production_mode in ['consume','consume_produce']:
consumed_data = {}
# Calculate already consumed qtys
for consumed in production.move_lines2:
if consumed.scrapped:
continue
if not consumed_data.get(consumed.product_id.id, False):
consumed_data[consumed.product_id.id] = 0
consumed_data[consumed.product_id.id] += consumed.product_qty
consumed_moves = []
for consume in wiz.consume_lines:
# Search move for the product
corresponding_move = [x for x in production.move_lines if x.product_id.id == consume.product_id.id]
if corresponding_move:
corresponding_move = corresponding_move[0]
if corresponding_move.product_qty > consume.product_qty:
consumed_moves.append(stock_mov_obj.action_consume(cr, uid, [corresponding_move.id], consume.product_qty, corresponding_move.location_id.id,
restrict_lot_id = consume.lot_id.id, context=context))
else:
consumed_moves.append(stock_mov_obj.action_consume(cr, uid, [corresponding_move.id], corresponding_move.product_qty, corresponding_move.location_id.id,
restrict_lot_id = consume.lot_id.id, context=context))
main_production_move = False
if production_mode == 'consume_produce':
# To produce remaining qty of final product
#vals = {'state':'confirmed'}
@ -842,9 +821,39 @@ class mrp_production(osv.osv):
raise osv.except_osv(_('Warning!'), _('You are going to produce total %s quantities of "%s".\nBut you can only produce up to total %s quantities.') % ((subproduct_factor * production_qty), prod_name, rest_qty))
if rest_qty > 0 :
new_moves = stock_mov_obj.action_consume(cr, uid, [produce_product.id], (subproduct_factor * production_qty), location_id = produce_product.location_id.id, restrict_lot_id = wiz.lot_id.id, context=context)
if produce_product.product_id.id == production.bom_id.product_id.id:
stock_mov_obj.write(cr, uid, new_moves, {'consumed_for': produce_product.id}, context=context)
if produce_product.product_id.id == production.product_id.id and new_moves:
main_production_move = new_moves[0]
# if produce_product.product_id.id == production.bom_id.product_id.id:
# stock_mov_obj.write(cr, uid, new_moves, {'consumed_for': produce_product.id}, context=context)
if production_mode in ['consume','consume_produce']:
consumed_data = {}
# Calculate already consumed qtys
for consumed in production.move_lines2:
if consumed.scrapped:
continue
if not consumed_data.get(consumed.product_id.id, False):
consumed_data[consumed.product_id.id] = 0
consumed_data[consumed.product_id.id] += consumed.product_qty
consumed_moves = []
for consume in wiz.consume_lines:
# Search move for the product
corresponding_move = [x for x in production.move_lines if x.product_id.id == consume.product_id.id]
if corresponding_move:
corresponding_move = corresponding_move[0]
default_values = {}
if main_production_move:
default_values = {'consumed_for': main_production_move}
if corresponding_move.product_qty > consume.product_qty:
consumed_moves.append(stock_mov_obj.action_consume(cr, uid, [corresponding_move.id], consume.product_qty, corresponding_move.location_id.id,
restrict_lot_id = consume.lot_id.id, default_values = default_values, context=context))
else:
consumed_moves.append(stock_mov_obj.action_consume(cr, uid, [corresponding_move.id], corresponding_move.product_qty, corresponding_move.location_id.id,
restrict_lot_id = consume.lot_id.id, default_values = default_values, context=context))
self.message_post(cr, uid, production_id, body=_("%s produced") % self._description, context=context)
self.signal_button_produce_done(cr, uid, [production_id])

View File

@ -39,7 +39,7 @@ class StockMove(osv.osv):
raise osv.except_osv(_('Warning!'), _('You must assign a serial number for the product %s') % (move.product_id.name))
if move.raw_material_production_id and move.location_dest_id.usage == 'production' and move.raw_material_production_id.product_id.track_production and not move.consumed_for:
raise osv.except_osv(_('Warning!'), _('You should not process it like this as both your raw materials and finished product require to track manufacturing. In that case, you should be able to make the link between the raw materials and the finished products and this is not possible with this method. '))
def _action_explode(self, cr, uid, move, context=None):
""" Explodes pickings.
@param move: Stock moves
@ -103,18 +103,20 @@ class StockMove(osv.osv):
procurement_obj.signal_button_wait_done(cr, uid, procurement_ids)
return processed_ids
def action_consume(self, cr, uid, ids, product_qty, location_id=False, restrict_lot_id = False, restrict_partner_id = False, context=None):
def action_consume(self, cr, uid, ids, product_qty, location_id=False, restrict_lot_id = False, default_values = None, context=None):
""" Consumed product with specific quantity from specific source location.
@param product_qty: Consumed product quantity
@param location_id: Source location
@return: Consumed lines
"""
if default_values is None:
default_values = {}
res = []
production_obj = self.pool.get('mrp.production')
for move in self.browse(cr, uid, ids, context=context):
self.action_confirm(cr, uid, [move.id], context=context)
new_moves = super(StockMove, self).action_consume(cr, uid, [move.id], product_qty, location_id, restrict_lot_id = restrict_lot_id,
restrict_partner_id = restrict_partner_id, context=context)
default_values = default_values, context=context)
production_ids = production_obj.search(cr, uid, [('move_lines', 'in', [move.id])])
for prod in production_obj.browse(cr, uid, production_ids, context=context):
if prod.state == 'confirmed':

View File

@ -1890,13 +1890,15 @@ class stock_move(osv.osv):
self.action_done(cr, uid, res, context=context)
return res
def action_consume(self, cr, uid, ids, quantity, location_id=False, restrict_lot_id=False, restrict_partner_id=False, context=None):
def action_consume(self, cr, uid, ids, quantity, location_id=False, restrict_lot_id=False, default_values = None, context=None):
""" Consumed product with specific quantity from specific source location. This correspond to a split of the move (or write if the quantity to consume is >= than the quantity of the move) followed by an action_done
@param ids: ids of stock move object to be consumed
@param quantity : specify consume quantity (given in move UoM)
@param location_id : specify source location
@return: Consumed lines
"""
if default_values is None:
default_values = {}
uom_obj = self.pool.get('product.uom')
if context is None:
context = {}
@ -1914,21 +1916,24 @@ class stock_move(osv.osv):
if location_id:
ctx['source_location_id'] = location_id
res.append(self.split(cr, uid, move, move_qty - quantity_rest, restrict_lot_id=restrict_lot_id,
restrict_partner_id = restrict_partner_id, context=ctx))
default_values = default_values, context=ctx))
else:
res.append(move.id)
if location_id:
self.write(cr, uid, [move.id], {'location_id': location_id, 'restrict_lot_id': restrict_lot_id,
'restrict_partner_id': restrict_partner_id}, context=context)
vals = default_values.copy()
vals.update({'location_id': location_id, 'restrict_lot_id': restrict_lot_id})
self.write(cr, uid, [move.id], vals, context=context)
self.action_done(cr, uid, res, context=context)
return res
def split(self, cr, uid, move, qty, restrict_lot_id=False, restrict_partner_id = False, context=None):
def split(self, cr, uid, move, qty, restrict_lot_id=False, default_values = None, context=None):
""" Splits qty from move move into a new move
:param move: browse record
:param qty: float. quantity to split (given in product UoM)
:param context: dictionay. can contains the special key 'source_location_id' in order to force the source location when copying the move
"""
if default_values is None:
default_values = {}
if move.product_qty <= qty or qty == 0:
return move.id
@ -1941,7 +1946,9 @@ class stock_move(osv.osv):
if move.state in ('done', 'cancel'):
raise osv.except_osv(_('Error'), _('You cannot split a move done'))
defaults = {
defaults = default_values.copy()
defaults.update({
'product_uom_qty': uom_qty,
'product_uos_qty': uos_qty,
'state': move.state,
@ -1949,8 +1956,7 @@ class stock_move(osv.osv):
'move_dest_id': False,
'reserved_quant_ids': [],
'restrict_lot_id': restrict_lot_id,
'restrict_partner_id': restrict_partner_id
}
})
if context.get('source_location_id'):
defaults['location_id'] = context['source_location_id']
new_move = self.copy(cr, uid, move.id, defaults)