[MERGE] purchase: refactoring of picking creation, courtesy of Akretion

bzr revid: odo@openerp.com-20111109181256-hk2xo3xm5zg6uftv
This commit is contained in:
Olivier Dony 2011-11-09 19:12:56 +01:00
commit c74af1ef44
1 changed files with 77 additions and 52 deletions

View File

@ -424,58 +424,84 @@ class purchase_order(osv.osv):
self.log(cr, uid, id, message)
return True
def action_picking_create(self,cr, uid, ids, *args):
picking_id = False
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)
def _prepare_order_picking(self, cr, uid, order, *args):
pick_name = self.pool.get('ir.sequence').get(cr, uid, 'stock.picking.in')
istate = 'none'
if order.invoice_method=='picking':
istate = '2binvoiced'
return {
'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' : [],
}
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.
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: sale order line records to procure
: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 ommitted.
:return: True
"""
if not picking_id:
picking_id = self.pool.get('stock.picking').create(cr, uid, self._prepare_order_picking(cr, uid, order, args))
todo_moves = []
for order_line in order_lines:
if not order_line.product_id:
continue
if order_line.product_id.product_tmpl_id.type in ('product', 'consu'):
move = self.pool.get('stock.move').create(cr, uid, self._prepare_order_line_move(cr, uid, order, order_line, picking_id, args))
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
def action_picking_create(self,cr, uid, ids, *args):
for order in self.browse(cr, uid, ids):
picking_id = self._create_pickings(cr, uid, order, [order_line for order_line in order.order_line], None, args)
return picking_id #FIXME this is brittle to assume there is only 1 picking_id, but has been kept for API compatibility
def copy(self, cr, uid, id, default=None, context=None):
if not default:
@ -490,7 +516,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.