[FIX] Empty all current line(s) when you change template. Before, the display do...
[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 openerp.osv import osv
22 from openerp.tools.translate import _
23
24 class procurement_order(osv.osv):
25     _inherit = 'procurement.order'
26     def check_buy(self, cr, uid, ids, context=None):
27         for procurement in self.browse(cr, uid, ids, context=context):
28             for line in procurement.product_id.flow_pull_ids:
29                 if line.location_id==procurement.location_id:
30                     return line.type_proc=='buy'
31         return super(procurement_order, self).check_buy(cr, uid, ids)
32
33     def check_produce(self, cr, uid, ids, context=None):
34         for procurement in self.browse(cr, uid, ids, context=context):
35             for line in procurement.product_id.flow_pull_ids:
36                 if line.location_id==procurement.location_id:
37                     return line.type_proc=='produce'
38         return super(procurement_order, self).check_produce(cr, uid, ids)
39
40     def check_move(self, cr, uid, ids, context=None):
41         for procurement in self.browse(cr, uid, ids, context=context):
42             for line in procurement.product_id.flow_pull_ids:
43                 if line.location_id==procurement.location_id:
44                     return (line.type_proc=='move') and (line.location_src_id)
45         return False
46
47     def action_move_create(self, cr, uid, ids, context=None):
48         proc_obj = self.pool.get('procurement.order')
49         move_obj = self.pool.get('stock.move')
50         picking_obj=self.pool.get('stock.picking')
51         for proc in proc_obj.browse(cr, uid, ids, context=context):
52             line = None
53             for line in proc.product_id.flow_pull_ids:
54                 if line.location_id == proc.location_id:
55                     break
56             assert line, 'Line cannot be False if we are on this state of the workflow'
57             origin = (proc.origin or proc.name or '').split(':')[0] +':'+line.name
58             picking_id = picking_obj.create(cr, uid, {
59                 'origin': origin,
60                 'company_id': line.company_id and line.company_id.id or False,
61                 'type': line.picking_type,
62                 'stock_journal_id': line.journal_id and line.journal_id.id or False,
63                 'move_type': 'one',
64                 'partner_id': line.partner_address_id.id,
65                 '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),
66                 'invoice_state': line.invoice_state,
67             })
68             move_id = move_obj.create(cr, uid, {
69                 'name': line.name,
70                 'picking_id': picking_id,
71                 'company_id':  line.company_id and line.company_id.id or False,
72                 'product_id': proc.product_id.id,
73                 'date': proc.date_planned,
74                 'product_qty': proc.product_qty,
75                 'product_uom': proc.product_uom.id,
76                 'product_uos_qty': (proc.product_uos and proc.product_uos_qty)\
77                         or proc.product_qty,
78                 'product_uos': (proc.product_uos and proc.product_uos.id)\
79                         or proc.product_uom.id,
80                 'partner_id': line.partner_address_id.id,
81                 'location_id': line.location_src_id.id,
82                 'location_dest_id': line.location_id.id,
83                 'move_dest_id': proc.move_id and proc.move_id.id or False, # to verif, about history ?
84                 'tracking_id': False,
85                 'cancel_cascade': line.cancel_cascade,
86                 'state': 'confirmed',
87                 '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),
88             })
89             if proc.move_id and proc.move_id.state in ('confirmed'):
90                 move_obj.write(cr,uid, [proc.move_id.id],  {
91                     'state':'waiting'
92                 }, context=context)
93             proc_id = proc_obj.create(cr, uid, {
94                 'name': line.name,
95                 'origin': origin,
96                 '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),
97                 'company_id':  line.company_id and line.company_id.id or False,
98                 'date_planned': proc.date_planned,
99                 'product_id': proc.product_id.id,
100                 'product_qty': proc.product_qty,
101                 'product_uom': proc.product_uom.id,
102                 'product_uos_qty': (proc.product_uos and proc.product_uos_qty)\
103                         or proc.product_qty,
104                 'product_uos': (proc.product_uos and proc.product_uos.id)\
105                         or proc.product_uom.id,
106                 'location_id': line.location_src_id.id,
107                 'procure_method': line.procure_method,
108                 'move_id': move_id,
109             })
110             self.pool.get('stock.picking').signal_button_confirm(cr, uid, [picking_id])
111             self.signal_button_confirm(cr, uid, [proc_id])
112             if proc.move_id:
113                 move_obj.write(cr, uid, [proc.move_id.id],
114                     {'location_id':proc.location_id.id})
115             msg = _('Pulled from another location.')
116             self.write(cr, uid, [proc.id], {'state':'running', 'message': msg})
117             self.message_post(cr, uid, [proc.id], body=msg, context=context)
118             # trigger direct processing (the new procurement shares the same planned date as the original one, which is already being processed)
119             self.signal_button_check(cr, uid, [proc_id]) # TODO is it necessary to interleave the calls?
120         return False
121
122
123 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: