2010-05-28 09:55:05 +00:00
##############################################################################
2012-03-30 08:11:36 +00:00
#
2010-05-28 09:55:05 +00:00
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
2012-03-30 08:11:36 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2010-05-28 09:55:05 +00:00
#
##############################################################################
2012-12-06 14:56:32 +00:00
from openerp . osv import osv
from openerp import netsvc
from openerp . tools . translate import _
2010-05-28 09:55:05 +00:00
2010-05-28 12:37:09 +00:00
class procurement_order ( osv . osv ) :
_inherit = ' procurement.order '
2010-05-28 09:55:05 +00:00
def check_buy ( self , cr , uid , ids , context = None ) :
2010-11-22 10:37:53 +00:00
for procurement in self . browse ( cr , uid , ids , context = context ) :
2010-05-28 09:55:05 +00:00
for line in procurement . product_id . flow_pull_ids :
if line . location_id == procurement . location_id :
return line . type_proc == ' buy '
2010-05-28 12:37:09 +00:00
return super ( procurement_order , self ) . check_buy ( cr , uid , ids )
2010-05-28 09:55:05 +00:00
def check_produce ( self , cr , uid , ids , context = None ) :
2010-11-22 10:37:53 +00:00
for procurement in self . browse ( cr , uid , ids , context = context ) :
2010-05-28 09:55:05 +00:00
for line in procurement . product_id . flow_pull_ids :
if line . location_id == procurement . location_id :
return line . type_proc == ' produce '
2010-05-28 12:37:09 +00:00
return super ( procurement_order , self ) . check_produce ( cr , uid , ids )
2010-05-28 09:55:05 +00:00
def check_move ( self , cr , uid , ids , context = None ) :
2010-11-22 10:37:53 +00:00
for procurement in self . browse ( cr , uid , ids , context = context ) :
2010-05-28 09:55:05 +00:00
for line in procurement . product_id . flow_pull_ids :
if line . location_id == procurement . location_id :
return ( line . type_proc == ' move ' ) and ( line . location_src_id )
return False
2012-03-05 18:40:03 +00:00
def action_move_create ( self , cr , uid , ids , context = None ) :
2010-05-28 12:37:09 +00:00
proc_obj = self . pool . get ( ' procurement.order ' )
2010-05-28 09:55:05 +00:00
move_obj = self . pool . get ( ' stock.move ' )
2010-10-07 04:29:19 +00:00
picking_obj = self . pool . get ( ' stock.picking ' )
2010-05-28 09:55:05 +00:00
wf_service = netsvc . LocalService ( " workflow " )
for proc in proc_obj . browse ( cr , uid , ids , context = context ) :
line = None
for line in proc . product_id . flow_pull_ids :
2010-10-13 21:59:50 +00:00
if line . location_id == proc . location_id :
2010-05-28 09:55:05 +00:00
break
2012-07-27 06:55:57 +00:00
assert line , ' Line cannot be False if we are on this state of the workflow '
2010-05-28 09:55:05 +00:00
origin = ( proc . origin or proc . name or ' ' ) . split ( ' : ' ) [ 0 ] + ' : ' + line . name
2010-10-13 21:59:50 +00:00
picking_id = picking_obj . create ( cr , uid , {
2010-05-28 09:55:05 +00:00
' origin ' : origin ,
2010-06-21 09:04:28 +00:00
' company_id ' : line . company_id and line . company_id . id or False ,
2010-05-28 09:55:05 +00:00
' type ' : line . picking_type ,
2010-06-15 08:14:32 +00:00
' stock_journal_id ' : line . journal_id and line . journal_id . id or False ,
2010-05-28 09:55:05 +00:00
' move_type ' : ' one ' ,
2012-03-30 08:11:36 +00:00
' partner_id ' : line . partner_address_id . id ,
2010-10-13 21:59:50 +00:00
' note ' : _ ( ' Picking for pulled procurement coming from original location %s , pull rule %s , via original Procurement %s (# %d ) ' ) % ( proc . location_id . name , line . name , proc . name , proc . id ) ,
2010-06-10 13:00:36 +00:00
' invoice_state ' : line . invoice_state ,
2010-05-28 09:55:05 +00:00
} )
2010-10-13 21:59:50 +00:00
move_id = move_obj . create ( cr , uid , {
2010-05-28 09:55:05 +00:00
' name ' : line . name ,
' picking_id ' : picking_id ,
2010-06-21 09:04:28 +00:00
' company_id ' : line . company_id and line . company_id . id or False ,
2010-05-28 09:55:05 +00:00
' product_id ' : proc . product_id . id ,
2010-09-21 12:50:29 +00:00
' date ' : proc . date_planned ,
2010-05-28 09:55:05 +00:00
' product_qty ' : proc . product_qty ,
' product_uom ' : proc . product_uom . id ,
' product_uos_qty ' : ( proc . product_uos and proc . product_uos_qty ) \
or proc . product_qty ,
' product_uos ' : ( proc . product_uos and proc . product_uos . id ) \
or proc . product_uom . id ,
2012-03-30 08:11:36 +00:00
' partner_id ' : line . partner_address_id . id ,
2010-05-28 09:55:05 +00:00
' location_id ' : line . location_src_id . id ,
' location_dest_id ' : line . location_id . id ,
' move_dest_id ' : proc . move_id and proc . move_id . id or False , # to verif, about history ?
' tracking_id ' : False ,
' cancel_cascade ' : line . cancel_cascade ,
' state ' : ' confirmed ' ,
2010-10-13 21:59:50 +00:00
' note ' : _ ( ' Move for pulled procurement coming from original location %s , pull rule %s , via original Procurement %s (# %d ) ' ) % ( proc . location_id . name , line . name , proc . name , proc . id ) ,
2010-05-28 09:55:05 +00:00
} )
if proc . move_id and proc . move_id . state in ( ' confirmed ' ) :
2010-10-07 04:29:19 +00:00
move_obj . write ( cr , uid , [ proc . move_id . id ] , {
2010-05-28 09:55:05 +00:00
' state ' : ' waiting '
} , context = context )
2010-10-07 04:29:19 +00:00
proc_id = proc_obj . create ( cr , uid , {
2010-05-28 09:55:05 +00:00
' name ' : line . name ,
' origin ' : origin ,
2010-10-13 21:59:50 +00:00
' note ' : _ ( ' Pulled procurement coming from original location %s , pull rule %s , via original Procurement %s (# %d ) ' ) % ( proc . location_id . name , line . name , proc . name , proc . id ) ,
2010-06-21 09:04:28 +00:00
' company_id ' : line . company_id and line . company_id . id or False ,
2010-05-28 09:55:05 +00:00
' date_planned ' : proc . date_planned ,
' product_id ' : proc . product_id . id ,
' product_qty ' : proc . product_qty ,
' product_uom ' : proc . product_uom . id ,
' product_uos_qty ' : ( proc . product_uos and proc . product_uos_qty ) \
or proc . product_qty ,
' product_uos ' : ( proc . product_uos and proc . product_uos . id ) \
or proc . product_uom . id ,
' location_id ' : line . location_src_id . id ,
' procure_method ' : line . procure_method ,
' move_id ' : move_id ,
} )
wf_service = netsvc . LocalService ( " workflow " )
wf_service . trg_validate ( uid , ' stock.picking ' , picking_id , ' button_confirm ' , cr )
2010-05-28 12:37:09 +00:00
wf_service . trg_validate ( uid , ' procurement.order ' , proc_id , ' button_confirm ' , cr )
2010-05-28 09:55:05 +00:00
if proc . move_id :
2010-10-07 04:29:19 +00:00
move_obj . write ( cr , uid , [ proc . move_id . id ] ,
2010-05-28 09:55:05 +00:00
{ ' location_id ' : proc . location_id . id } )
2012-10-04 13:38:29 +00:00
msg = _ ( ' Pulled from another location. ' )
2012-10-03 12:19:04 +00:00
self . write ( cr , uid , [ proc . id ] , { ' state ' : ' running ' , ' message ' : msg } )
self . message_post ( cr , uid , [ proc . id ] , body = msg , context = context )
2010-10-13 21:59:50 +00:00
# trigger direct processing (the new procurement shares the same planned date as the original one, which is already being processed)
wf_service . trg_validate ( uid , ' procurement.order ' , proc_id , ' button_check ' , cr )
2010-05-28 09:55:05 +00:00
return False
2010-10-13 21:59:50 +00:00
2011-11-22 08:51:38 +00:00
procurement_order ( )
2011-11-24 15:50:58 +00:00
2011-11-22 08:51:38 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: