1 ##############################################################################
3 # OpenERP, Open Source Management Solution
4 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Affero General Public License as
8 # published by the Free Software Foundation, either version 3 of the
9 # License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Affero General Public License for more details.
16 # You should have received a copy of the GNU Affero General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 ##############################################################################
23 from tools.translate import _
25 class procurement_order(osv.osv):
26 _inherit = 'procurement.order'
27 def check_buy(self, cr, uid, ids, context=None):
28 for procurement in self.browse(cr, uid, ids):
29 for line in procurement.product_id.flow_pull_ids:
30 if line.location_id==procurement.location_id:
31 return line.type_proc=='buy'
32 return super(procurement_order, self).check_buy(cr, uid, ids)
34 def check_produce(self, cr, uid, ids, context=None):
35 for procurement in self.browse(cr, uid, ids):
36 for line in procurement.product_id.flow_pull_ids:
37 if line.location_id==procurement.location_id:
38 return line.type_proc=='produce'
39 return super(procurement_order, self).check_produce(cr, uid, ids)
41 def check_move(self, cr, uid, ids, context=None):
42 for procurement in self.browse(cr, uid, ids):
43 for line in procurement.product_id.flow_pull_ids:
44 if line.location_id==procurement.location_id:
45 return (line.type_proc=='move') and (line.location_src_id)
48 def action_move_create(self, cr, uid, ids,context=None):
49 proc_obj = self.pool.get('procurement.order')
50 move_obj = self.pool.get('stock.move')
51 picking_obj=self.pool.get('stock.picking')
52 wf_service = netsvc.LocalService("workflow")
53 for proc in proc_obj.browse(cr, uid, ids, context=context):
55 for line in proc.product_id.flow_pull_ids:
56 if line.location_id == proc.location_id:
58 assert line, 'Line can not be False if we are on this state of the workflow'
59 origin = (proc.origin or proc.name or '').split(':')[0] +':'+line.name
60 picking_id = picking_obj.create(cr, uid, {
62 'company_id': line.company_id and line.company_id.id or False,
63 'type': line.picking_type,
64 'stock_journal_id': line.journal_id and line.journal_id.id or False,
66 'address_id': line.partner_address_id.id,
67 '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),
68 'invoice_state': line.invoice_state,
70 move_id = move_obj.create(cr, uid, {
72 'picking_id': picking_id,
73 'company_id': line.company_id and line.company_id.id or False,
74 'product_id': proc.product_id.id,
75 'date': proc.date_planned,
76 'product_qty': proc.product_qty,
77 'product_uom': proc.product_uom.id,
78 'product_uos_qty': (proc.product_uos and proc.product_uos_qty)\
80 'product_uos': (proc.product_uos and proc.product_uos.id)\
81 or proc.product_uom.id,
82 'address_id': line.partner_address_id.id,
83 'location_id': line.location_src_id.id,
84 'location_dest_id': line.location_id.id,
85 'move_dest_id': proc.move_id and proc.move_id.id or False, # to verif, about history ?
87 'cancel_cascade': line.cancel_cascade,
89 '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),
91 if proc.move_id and proc.move_id.state in ('confirmed'):
92 move_obj.write(cr,uid, [proc.move_id.id], {
95 proc_id = proc_obj.create(cr, uid, {
98 '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),
99 'company_id': line.company_id and line.company_id.id or False,
100 'date_planned': proc.date_planned,
101 'product_id': proc.product_id.id,
102 'product_qty': proc.product_qty,
103 'product_uom': proc.product_uom.id,
104 'product_uos_qty': (proc.product_uos and proc.product_uos_qty)\
106 'product_uos': (proc.product_uos and proc.product_uos.id)\
107 or proc.product_uom.id,
108 'location_id': line.location_src_id.id,
109 'procure_method': line.procure_method,
112 wf_service = netsvc.LocalService("workflow")
113 wf_service.trg_validate(uid, 'stock.picking', picking_id, 'button_confirm', cr)
114 wf_service.trg_validate(uid, 'procurement.order', proc_id, 'button_confirm', cr)
116 move_obj.write(cr, uid, [proc.move_id.id],
117 {'location_id':proc.location_id.id})
118 self.write(cr, uid, [proc.id], {'state':'running', 'message':_('Pulled from another location via procurement %d')%proc_id})
120 # trigger direct processing (the new procurement shares the same planned date as the original one, which is already being processed)
121 wf_service.trg_validate(uid, 'procurement.order', proc_id, 'button_check', cr)