[MERGE] purchase: refactoring of picking creation, courtesy of Raphaël Valyi, Akretion

bzr revid: odo@openerp.com-20111110225802-ocpesmmtz4q68r8r
This commit is contained in:
Olivier Dony 2011-11-10 23:58:02 +01:00
commit f8de95825c
1 changed files with 74 additions and 51 deletions

View File

@ -425,58 +425,82 @@ class purchase_order(osv.osv):
self.log(cr, uid, id, message)
return True
def _prepare_order_picking(self, cr, uid, order, *args):
return {
'name': self.pool.get('ir.sequence').get(cr, uid, 'stock.picking.in'),
'origin': order.name + ((order.origin and (':' + order.origin)) or ''),
'date': order.date_order,
'type': 'in',
'address_id': order.dest_address_id.id or order.partner_address_id.id,
'invoice_state': '2binvoiced' if order.invoice_method == 'picking' else 'none',
'purchase_id': order.id,
'company_id': order.company_id.id,
'move_lines' : [],
}
def _prepare_order_line_move(self, cr, uid, order, order_line, picking_id, *args):
return {
'name': order.name + ': ' + (order_line.name or ''),
'product_id': order_line.product_id.id,
'product_qty': order_line.product_qty,
'product_uos_qty': order_line.product_qty,
'product_uom': order_line.product_uom.id,
'product_uos': order_line.product_uom.id,
'date': order_line.date_planned,
'date_expected': order_line.date_planned,
'location_id': order.partner_id.property_stock_supplier.id,
'location_dest_id': order.location_id.id,
'picking_id': picking_id,
'address_id': order.dest_address_id.id or order.partner_address_id.id,
'move_dest_id': order_line.move_dest_id.id,
'state': 'draft',
'purchase_line_id': order_line.id,
'company_id': order.company_id.id,
'price_unit': order_line.price_unit
}
def _create_pickings(self, cr, uid, order, order_lines, picking_id=False, *args):
"""Creates pickings and appropriate stock moves for given order lines, then
confirms the moves, makes them available, and confirms the picking.
If ``picking_id`` is provided, the stock moves will be added to it, otherwise
a standard outgoing picking will be created to wrap the stock moves, as returned
by :meth:`~._prepare_order_picking`.
Modules that wish to customize the procurements or partition the stock moves over
multiple stock pickings may override this method and call ``super()`` with
different subsets of ``order_lines`` and/or preset ``picking_id`` values.
:param browse_record order: purchase order to which the order lines belong
:param list(browse_record) order_lines: purchase order line records for which picking
and moves should be created.
:param int picking_id: optional ID of a stock picking to which the created stock moves
will be added. A new picking will be created if omitted.
:return: list of IDs of pickings used/created for the given order lines (usually just one)
"""
if not picking_id:
picking_id = self.pool.get('stock.picking').create(cr, uid, self._prepare_order_picking(cr, uid, order, *args))
todo_moves = []
stock_move = self.pool.get('stock.move')
wf_service = netsvc.LocalService("workflow")
for order_line in order_lines:
if not order_line.product_id:
continue
if order_line.product_id.type in ('product', 'consu'):
move = stock_move.create(cr, uid, self._prepare_order_line_move(cr, uid, order, order_line, picking_id, *args))
if order_line.move_dest_id:
order_line.move_dest_id.write({'location_id': order.location_id.id})
todo_moves.append(move)
stock_move.action_confirm(cr, uid, todo_moves)
stock_move.force_assign(cr, uid, todo_moves)
wf_service.trg_validate(uid, 'stock.picking', picking_id, 'button_confirm', cr)
return [picking_id]
def action_picking_create(self,cr, uid, ids, *args):
picking_id = False
picking_ids = []
for order in self.browse(cr, uid, ids):
loc_id = order.partner_id.property_stock_supplier.id
istate = 'none'
if order.invoice_method=='picking':
istate = '2binvoiced'
pick_name = self.pool.get('ir.sequence').get(cr, uid, 'stock.picking.in')
picking_id = self.pool.get('stock.picking').create(cr, uid, {
'name': pick_name,
'origin': order.name+((order.origin and (':'+order.origin)) or ''),
'date': order.date_order,
'type': 'in',
'address_id': order.dest_address_id.id or order.partner_address_id.id,
'invoice_state': istate,
'purchase_id': order.id,
'company_id': order.company_id.id,
'move_lines' : [],
})
todo_moves = []
for order_line in order.order_line:
if not order_line.product_id:
continue
if order_line.product_id.product_tmpl_id.type in ('product', 'consu'):
dest = order.location_id.id
move = self.pool.get('stock.move').create(cr, uid, {
'name': order.name + ': ' +(order_line.name or ''),
'product_id': order_line.product_id.id,
'product_qty': order_line.product_qty,
'product_uos_qty': order_line.product_qty,
'product_uom': order_line.product_uom.id,
'product_uos': order_line.product_uom.id,
'date': order_line.date_planned,
'date_expected': order_line.date_planned,
'location_id': loc_id,
'location_dest_id': dest,
'picking_id': picking_id,
'address_id': order.dest_address_id.id or order.partner_address_id.id,
'move_dest_id': order_line.move_dest_id.id,
'state': 'draft',
'purchase_line_id': order_line.id,
'company_id': order.company_id.id,
'price_unit': order_line.price_unit
})
if order_line.move_dest_id:
self.pool.get('stock.move').write(cr, uid, [order_line.move_dest_id.id], {'location_id':order.location_id.id})
todo_moves.append(move)
self.pool.get('stock.move').action_confirm(cr, uid, todo_moves)
self.pool.get('stock.move').force_assign(cr, uid, todo_moves)
wf_service = netsvc.LocalService("workflow")
wf_service.trg_validate(uid, 'stock.picking', picking_id, 'button_confirm', cr)
return picking_id
picking_ids.extend(self._create_pickings(cr, uid, order, order.order_line, None, *args))
return picking_ids
def copy(self, cr, uid, id, default=None, context=None):
if not default:
@ -491,7 +515,6 @@ class purchase_order(osv.osv):
})
return super(purchase_order, self).copy(cr, uid, id, default, context)
def do_merge(self, cr, uid, ids, context=None):
"""
To merge similar type of purchase orders.