[MERGE]: Merge with latest trunk-addons
[odoo/odoo.git] / addons / stock_location / procurement_pull.py
1 ##############################################################################
2 #
3 #    OpenERP, Open Source Management Solution
4 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
5 #
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.
10 #
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.
15 #
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/>.
18 #
19 ##############################################################################
20
21 from osv import osv
22 import netsvc
23 from tools.translate import _
24
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, context=context):
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)
33
34     def check_produce(self, cr, uid, ids, context=None):
35         for procurement in self.browse(cr, uid, ids, context=context):
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)
40
41     def check_move(self, cr, uid, ids, context=None):
42         for procurement in self.browse(cr, uid, ids, context=context):
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)
46         return False
47
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):
54             line = None
55             for line in proc.product_id.flow_pull_ids:
56                 if line.location_id == proc.location_id:
57                     break
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, {
61                 'origin': origin,
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,
65                 'move_type': 'one',
66                 'partner_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,
69             })
70             move_id = move_obj.create(cr, uid, {
71                 'name': line.name,
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)\
79                         or proc.product_qty,
80                 'product_uos': (proc.product_uos and proc.product_uos.id)\
81                         or proc.product_uom.id,
82                 'partner_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 ?
86                 'tracking_id': False,
87                 'cancel_cascade': line.cancel_cascade,
88                 'state': 'confirmed',
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),
90             })
91             if proc.move_id and proc.move_id.state in ('confirmed'):
92                 move_obj.write(cr,uid, [proc.move_id.id],  {
93                     'state':'waiting'
94                 }, context=context)
95             proc_id = proc_obj.create(cr, uid, {
96                 'name': line.name,
97                 'origin': origin,
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)\
105                         or proc.product_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,
110                 'move_id': move_id,
111             })
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)
115             if proc.move_id:
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})
119             self.running_send_note(cr, uid, [proc.id], context=context)
120
121             # trigger direct processing (the new procurement shares the same planned date as the original one, which is already being processed)
122             wf_service.trg_validate(uid, 'procurement.order', proc_id, 'button_check', cr)
123         return False
124
125 procurement_order()
126
127 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: