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