[FIX] mrp: perform location chaining for kit exploded moves
[odoo/odoo.git] / addons / mrp / stock.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from openerp.osv import fields
23 from openerp.osv import osv
24 from openerp import netsvc
25
26
27 class StockMove(osv.osv):
28     _inherit = 'stock.move'
29     
30     _columns = {
31         'production_id': fields.many2one('mrp.production', 'Production', select=True),
32     }
33
34     def create_chained_picking(self, cr, uid, moves, context=None):
35         new_moves = super(StockMove, self).create_chained_picking(cr, uid, moves, context=context)
36         self.write(cr, uid, [x.id for x in new_moves], {'production_id': False}, context=context)
37         return new_moves
38     
39     def _action_explode(self, cr, uid, move, context=None):
40         """ Explodes pickings.
41         @param move: Stock moves
42         @return: True
43         """
44         bom_obj = self.pool.get('mrp.bom')
45         move_obj = self.pool.get('stock.move')
46         procurement_obj = self.pool.get('procurement.order')
47         product_obj = self.pool.get('product.product')
48         wf_service = netsvc.LocalService("workflow")
49         processed_ids = [move.id]
50         if move.product_id.supply_method == 'produce':
51             bis = bom_obj.search(cr, uid, [
52                 ('product_id','=',move.product_id.id),
53                 ('bom_id','=',False),
54                 ('type','=','phantom')])
55             if bis:
56                 factor = move.product_qty
57                 bom_point = bom_obj.browse(cr, uid, bis[0], context=context)
58                 res = bom_obj._bom_explode(cr, uid, bom_point, factor, [])
59                 for line in res[0]: 
60                     valdef = {
61                         'picking_id': move.picking_id.id,
62                         'product_id': line['product_id'],
63                         'product_uom': line['product_uom'],
64                         'product_qty': line['product_qty'],
65                         'product_uos': line['product_uos'],
66                         'product_uos_qty': line['product_uos_qty'],
67                         'move_dest_id': move.id,
68                         'state': 'draft',  #will be confirmed below
69                         'name': line['name'],
70                         'move_history_ids': [(6,0,[move.id])],
71                         'move_history_ids2': [(6,0,[])],
72                         'procurements': [],
73                     }
74                     mid = move_obj.copy(cr, uid, move.id, default=valdef)
75                     processed_ids.append(mid)
76                     prodobj = product_obj.browse(cr, uid, line['product_id'], context=context)
77                     proc_id = procurement_obj.create(cr, uid, {
78                         'name': (move.picking_id.origin or ''),
79                         'origin': (move.picking_id.origin or ''),
80                         'date_planned': move.date,
81                         'product_id': line['product_id'],
82                         'product_qty': line['product_qty'],
83                         'product_uom': line['product_uom'],
84                         'product_uos_qty': line['product_uos'] and line['product_uos_qty'] or False,
85                         'product_uos':  line['product_uos'],
86                         'location_id': move.location_id.id,
87                         'procure_method': prodobj.procure_method,
88                         'move_id': mid,
89                     })
90                     wf_service.trg_validate(uid, 'procurement.order', proc_id, 'button_confirm', cr)
91                 move_obj.write(cr, uid, [move.id], {
92                     'location_dest_id': move.location_id.id, # dummy move for the kit
93                     'auto_validate': True,
94                     'picking_id': False,
95                     'state': 'confirmed'
96                 })
97                 for m in procurement_obj.search(cr, uid, [('move_id','=',move.id)], context):
98                     wf_service.trg_validate(uid, 'procurement.order', m, 'button_confirm', cr)
99                     wf_service.trg_validate(uid, 'procurement.order', m, 'button_wait_done', cr)
100         if processed_ids and move.state == 'assigned':
101             # Set the state of resulting moves according to 'assigned' as the original move is assigned
102             move_obj.write(cr, uid, list(set(processed_ids) - set([move.id])), {'state': 'assigned'}, context=context)
103         return processed_ids
104     
105     def action_consume(self, cr, uid, ids, product_qty, location_id=False, context=None):
106         """ Consumed product with specific quatity from specific source location.
107         @param product_qty: Consumed product quantity
108         @param location_id: Source location
109         @return: Consumed lines
110         """       
111         res = []
112         production_obj = self.pool.get('mrp.production')
113         wf_service = netsvc.LocalService("workflow")
114         for move in self.browse(cr, uid, ids):
115             move.action_confirm(context)
116             new_moves = super(StockMove, self).action_consume(cr, uid, [move.id], product_qty, location_id, context=context)
117             production_ids = production_obj.search(cr, uid, [('move_lines', 'in', [move.id])])
118             for prod in production_obj.browse(cr, uid, production_ids, context=context):
119                 if prod.state == 'confirmed':
120                     production_obj.force_production(cr, uid, [prod.id])
121                 wf_service.trg_validate(uid, 'mrp.production', prod.id, 'button_produce', cr)
122             for new_move in new_moves:
123                 if new_move == move.id:
124                     #This move is already there in move lines of production order
125                     continue
126                 production_obj.write(cr, uid, production_ids, {'move_lines': [(4, new_move)]})
127                 res.append(new_move)
128         return res
129     
130     def action_scrap(self, cr, uid, ids, product_qty, location_id, context=None):
131         """ Move the scrap/damaged product into scrap location
132         @param product_qty: Scraped product quantity
133         @param location_id: Scrap location
134         @return: Scraped lines
135         """  
136         res = []
137         production_obj = self.pool.get('mrp.production')
138         wf_service = netsvc.LocalService("workflow")
139         for move in self.browse(cr, uid, ids, context=context):
140             new_moves = super(StockMove, self).action_scrap(cr, uid, [move.id], product_qty, location_id, context=context)
141             #If we are not scrapping our whole move, tracking and lot references must not be removed
142             #self.write(cr, uid, [move.id], {'prodlot_id': False, 'tracking_id': False})
143             production_ids = production_obj.search(cr, uid, [('move_lines', 'in', [move.id])])
144             for prod_id in production_ids:
145                 wf_service.trg_validate(uid, 'mrp.production', prod_id, 'button_produce', cr)
146             for new_move in new_moves:
147                 production_obj.write(cr, uid, production_ids, {'move_lines': [(4, new_move)]})
148                 res.append(new_move)
149         return res
150
151 StockMove()
152
153
154 class StockPicking(osv.osv):
155     _inherit = 'stock.picking'
156
157     #
158     # Explode picking by replacing phantom BoMs
159     #
160     def action_explode(self, cr, uid, move_ids, *args):
161         """Explodes moves by expanding kit components"""
162         move_obj = self.pool.get('stock.move')
163         todo = list(super(StockPicking, self).action_explode(cr, uid, move_ids, *args))
164         for move in move_obj.browse(cr, uid, move_ids):
165             result = move_obj._action_explode(cr, uid, move)
166             moves = move_obj.browse(cr, uid, result)
167             todo.extend(move.id for move in moves if move.state not in ['confirmed', 'assigned', 'done'])
168         return list(set(todo))
169
170 StockPicking()
171
172
173 class split_in_production_lot(osv.osv_memory):
174     _inherit = "stock.move.split"
175
176     def split(self, cr, uid, ids, move_ids, context=None):
177         """ Splits move lines into given quantities.
178         @param move_ids: Stock moves.
179         @return: List of new moves.
180         """
181         new_moves = super(split_in_production_lot, self).split(cr, uid, ids, move_ids, context=context)
182         production_obj = self.pool.get('mrp.production')
183         production_ids = production_obj.search(cr, uid, [('move_lines', 'in', move_ids)])
184         production_obj.write(cr, uid, production_ids, {'move_lines': [(4, m) for m in new_moves]})
185         return new_moves
186
187 split_in_production_lot()
188
189 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: