[MERGE] forward port of branch 7.0 up to 922a52d
[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
25
26 class StockMove(osv.osv):
27     _inherit = 'stock.move'
28     
29     _columns = {
30         'production_id': fields.many2one('mrp.production', 'Production', select=True),
31     }
32
33     def create_chained_picking(self, cr, uid, moves, context=None):
34         new_moves = super(StockMove, self).create_chained_picking(cr, uid, moves, context=context)
35         self.write(cr, uid, [x.id for x in new_moves], {'production_id': False}, context=context)
36         return new_moves
37     
38     def _action_explode(self, cr, uid, move, context=None):
39         """ Explodes pickings.
40         @param move: Stock moves
41         @return: True
42         """
43         bom_obj = self.pool.get('mrp.bom')
44         move_obj = self.pool.get('stock.move')
45         procurement_obj = self.pool.get('procurement.order')
46         product_obj = self.pool.get('product.product')
47         processed_ids = [move.id]
48         if move.product_id.supply_method == 'produce':
49             bis = bom_obj.search(cr, uid, [
50                 ('product_id','=',move.product_id.id),
51                 ('bom_id','=',False),
52                 ('type','=','phantom')])
53             if bis:
54                 factor = move.product_qty
55                 bom_point = bom_obj.browse(cr, uid, bis[0], context=context)
56                 res = bom_obj._bom_explode(cr, uid, bom_point, factor, [])
57                 state = 'confirmed'
58                 if move.state == 'assigned':
59                     state = 'assigned'
60                 for line in res[0]: 
61                     valdef = {
62                         'picking_id': move.picking_id.id,
63                         'product_id': line['product_id'],
64                         'product_uom': line['product_uom'],
65                         'product_qty': line['product_qty'],
66                         'product_uos': line['product_uos'],
67                         'product_uos_qty': line['product_uos_qty'],
68                         'move_dest_id': move.id,
69                         'state': state,
70                         'name': line['name'],
71                         'move_history_ids': [(6,0,[move.id])],
72                         'move_history_ids2': [(6,0,[])],
73                         'procurements': [],
74                     }
75                     mid = move_obj.copy(cr, uid, move.id, default=valdef)
76                     processed_ids.append(mid)
77                     prodobj = product_obj.browse(cr, uid, line['product_id'], context=context)
78                     proc_id = procurement_obj.create(cr, uid, {
79                         'name': (move.picking_id.origin or ''),
80                         'origin': (move.picking_id.origin or ''),
81                         'date_planned': move.date,
82                         'product_id': line['product_id'],
83                         'product_qty': line['product_qty'],
84                         'product_uom': line['product_uom'],
85                         'product_uos_qty': line['product_uos'] and line['product_uos_qty'] or False,
86                         'product_uos':  line['product_uos'],
87                         'location_id': move.location_id.id,
88                         'procure_method': prodobj.procure_method,
89                         'move_id': mid,
90                     })
91                     procurement_obj.signal_button_confirm(cr, uid, [proc_id])
92                     
93                 move_obj.write(cr, uid, [move.id], {
94                     'location_dest_id': move.location_id.id, # dummy move for the kit
95                     'auto_validate': True,
96                     'picking_id': False,
97                     'state': 'confirmed'
98                 })
99                 procurement_ids = procurement_obj.search(cr, uid, [('move_id','=',move.id)], context)
100                 procurement_obj.signal_button_confirm(cr, uid, procurement_ids)
101                 procurement_obj.signal_button_wait_done(cr, uid, procurement_ids)
102         return processed_ids
103     
104     def action_consume(self, cr, uid, ids, product_qty, location_id=False, context=None):
105         """ Consumed product with specific quatity from specific source location.
106         @param product_qty: Consumed product quantity
107         @param location_id: Source location
108         @return: Consumed lines
109         """       
110         res = []
111         production_obj = self.pool.get('mrp.production')
112         for move in self.browse(cr, uid, ids):
113             move.action_confirm(context)
114             new_moves = super(StockMove, self).action_consume(cr, uid, [move.id], product_qty, location_id, context=context)
115             production_ids = production_obj.search(cr, uid, [('move_lines', 'in', [move.id])])
116             for prod in production_obj.browse(cr, uid, production_ids, context=context):
117                 if prod.state == 'confirmed':
118                     production_obj.force_production(cr, uid, [prod.id])
119             production_obj.signal_button_produce(cr, uid, production_ids)                
120             for new_move in new_moves:
121                 if new_move == move.id:
122                     #This move is already there in move lines of production order
123                     continue
124                 production_obj.write(cr, uid, production_ids, {'move_lines': [(4, new_move)]})
125                 res.append(new_move)
126         return res
127     
128     def action_scrap(self, cr, uid, ids, product_qty, location_id, context=None):
129         """ Move the scrap/damaged product into scrap location
130         @param product_qty: Scraped product quantity
131         @param location_id: Scrap location
132         @return: Scraped lines
133         """  
134         res = []
135         production_obj = self.pool.get('mrp.production')
136         for move in self.browse(cr, uid, ids, context=context):
137             new_moves = super(StockMove, self).action_scrap(cr, uid, [move.id], product_qty, location_id, context=context)
138             #If we are not scrapping our whole move, tracking and lot references must not be removed
139             #self.write(cr, uid, [move.id], {'prodlot_id': False, 'tracking_id': False})
140             production_ids = production_obj.search(cr, uid, [('move_lines', 'in', [move.id])])
141             for prod_id in production_ids:
142                 production_obj.signal_button_produce(cr, uid, [prod_id])
143             for new_move in new_moves:
144                 production_obj.write(cr, uid, production_ids, {'move_lines': [(4, new_move)]})
145                 res.append(new_move)
146         return res
147
148
149
150 class StockPicking(osv.osv):
151     _inherit = 'stock.picking'
152
153     #
154     # Explode picking by replacing phantom BoMs
155     #
156     def action_explode(self, cr, uid, move_ids, *args):
157         """Explodes moves by expanding kit components"""
158         move_obj = self.pool.get('stock.move')
159         todo = list(super(StockPicking, self).action_explode(cr, uid, move_ids, *args))
160         for move in move_obj.browse(cr, uid, move_ids):
161             result = move_obj._action_explode(cr, uid, move)
162             moves = move_obj.browse(cr, uid, result)
163             todo.extend(move.id for move in moves if move.state not in ['confirmed', 'assigned', 'done'])
164         return list(set(todo))
165
166
167
168 class split_in_production_lot(osv.osv_memory):
169     _inherit = "stock.move.split"
170
171     def split(self, cr, uid, ids, move_ids, context=None):
172         """ Splits move lines into given quantities.
173         @param move_ids: Stock moves.
174         @return: List of new moves.
175         """
176         new_moves = super(split_in_production_lot, self).split(cr, uid, ids, move_ids, context=context)
177         production_obj = self.pool.get('mrp.production')
178         production_ids = production_obj.search(cr, uid, [('move_lines', 'in', move_ids)])
179         production_obj.write(cr, uid, production_ids, {'move_lines': [(4, m) for m in new_moves]})
180         return new_moves
181
182
183 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: