[Fix] Fix buildbot warning
[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 osv import fields
23 from osv import osv
24 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         if move.product_id.supply_method == 'produce' and move.product_id.procure_method == 'make_to_order':
50             bis = bom_obj.search(cr, uid, [
51                 ('product_id','=',move.product_id.id),
52                 ('bom_id','=',False),
53                 ('type','=','phantom')])
54             if bis:
55                 factor = move.product_qty
56                 bom_point = bom_obj.browse(cr, uid, bis[0], context=context)
57                 res = bom_obj._bom_explode(cr, uid, bom_point, factor, [])
58                 dest = move.product_id.product_tmpl_id.property_stock_production.id
59                 state = 'confirmed'
60                 if move.state == 'assigned':
61                     state = 'assigned'
62                 for line in res[0]:                    
63                     valdef = {
64                         'picking_id': move.picking_id.id,
65                         'product_id': line['product_id'],
66                         'product_uom': line['product_uom'],
67                         'product_qty': line['product_qty'],
68                         'product_uos': line['product_uos'],
69                         'product_uos_qty': line['product_uos_qty'],
70                         'move_dest_id': move.id,
71                         'state': state,
72                         'name': line['name'],
73                         'location_dest_id': dest,
74                         'move_history_ids': [(6,0,[move.id])],
75                         'move_history_ids2': [(6,0,[])],
76                         'procurements': [],
77                     }
78                     mid = move_obj.copy(cr, uid, move.id, default=valdef)
79                     prodobj = product_obj.browse(cr, uid, line['product_id'], context=context)
80                     proc_id = procurement_obj.create(cr, uid, {
81                         'name': (move.picking_id.origin or ''),
82                         'origin': (move.picking_id.origin or ''),
83                         'date_planned': move.date,
84                         'product_id': line['product_id'],
85                         'product_qty': line['product_qty'],
86                         'product_uom': line['product_uom'],
87                         'product_uos_qty': line['product_uos'] and line['product_uos_qty'] or False,
88                         'product_uos':  line['product_uos'],
89                         'location_id': move.location_id.id,
90                         'procure_method': prodobj.procure_method,
91                         'move_id': mid,
92                     })
93                     wf_service.trg_validate(uid, 'procurement.order', proc_id, 'button_confirm', cr)
94                 move_obj.write(cr, uid, [move.id], {
95                     'location_id': move.location_dest_id.id,
96                     'auto_validate': True,
97                     'picking_id': False,
98                     'state': 'waiting'
99                 })
100                 for m in procurement_obj.search(cr, uid, [('move_id','=',move.id)], context):
101                     wf_service.trg_validate(uid, 'procurement.order', m, 'button_wait_done', cr)
102         return True
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         wf_service = netsvc.LocalService("workflow")
113         for move in self.browse(cr, uid, ids):
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                 wf_service.trg_validate(uid, 'mrp.production', prod.id, 'button_produce', cr)
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         wf_service = netsvc.LocalService("workflow")
137         for move in self.browse(cr, uid, ids, context=context):
138             new_moves = super(StockMove, self).action_scrap(cr, uid, [move.id], product_qty, location_id, context=context)
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                 wf_service.trg_validate(uid, 'mrp.production', prod_id, 'button_produce', cr)
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 {}
147
148 StockMove()
149
150
151 class StockPicking(osv.osv):
152     _inherit = 'stock.picking'
153
154     #
155     # Explode picking by replacing phantom BoMs
156     #
157     def action_explode(self, cr, uid, picks, *args):
158         """ Explodes picking by replacing phantom BoMs
159         @param picks: Picking ids. 
160         @param *args: Arguments
161         @return: Picking ids.
162         """  
163         move_obj = self.pool.get('stock.move')
164         for move in move_obj.browse(cr, uid, picks):
165             move_obj._action_explode(cr, uid, move)
166         return picks
167
168 StockPicking()
169
170
171 class spilt_in_production_lot(osv.osv_memory):
172     _inherit = "stock.move.split"
173     
174     def split(self, cr, uid, ids, move_ids, context=None):
175         """ Splits move lines into given quantities.
176         @param move_ids: Stock moves.
177         @return: List of new moves.
178         """  
179         production_obj = self.pool.get('mrp.production')
180         move_obj = self.pool.get('stock.move')  
181         res = []
182         for move in move_obj.browse(cr, uid, move_ids, context=context):
183             new_moves = super(spilt_in_production_lot, self).split(cr, uid, ids, move_ids, context=context)
184             production_ids = production_obj.search(cr, uid, [('move_lines', 'in', [move.id])])
185             for new_move in new_moves:
186                 production_obj.write(cr, uid, production_ids, {'move_lines': [(4, new_move)]})                
187         return res
188     
189 spilt_in_production_lot()
190 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: