[FIX] stock: Changed field name for scrap_location in stock.move.
[odoo/odoo.git] / addons / stock / wizard / stock_move.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, osv
23 from tools.translate import _
24
25 class stock_move_track(osv.osv_memory):
26     _name = "stock.move.track"
27     _description = "Track moves"
28     
29     _columns = {
30         'tracking_prefix': fields.char('Tracking prefix', size=64), 
31         'quantity': fields.float("Quantity per lot")
32               }
33
34     _defaults = {
35         'quantity': lambda *x: 1
36                  }
37     
38     def track_lines(self, cr, uid, ids, context={}):
39         """ 
40              To track stock moves lines
41             
42              @param self: The object pointer.
43              @param cr: A database cursor
44              @param uid: ID of the user currently logged in
45              @param ids: the ID or list of IDs if we want more than one 
46              @param context: A standard dictionary 
47              
48              @return: 
49         
50         """                
51         datas = self.read(cr, uid, ids)[0]
52         move_obj = self.pool.get('stock.move')
53         move_obj._track_lines(cr, uid, context['active_id'], datas, context=context)
54         return {}
55
56 stock_move_track()
57
58 class stock_move_consume(osv.osv_memory):
59     _name = "stock.move.consume"
60     _description = "Consume Products"
61     
62     _columns = {
63         'product_id': fields.many2one('product.product', 'Product', required=True, select=True), 
64         'product_qty': fields.float('Quantity', required=True), 
65         'product_uom': fields.many2one('product.uom', 'Product UOM', required=True), 
66         'location_id': fields.many2one('stock.location', 'Location', required=True)
67     }
68
69     def default_get(self, cr, uid, fields, context=None):
70         """ 
71              Get default values
72             
73              @param self: The object pointer.
74              @param cr: A database cursor
75              @param uid: ID of the user currently logged in
76              @param fields: List of fields for default value 
77              @param context: A standard dictionary 
78              
79              @return: default values of fields
80         
81         """
82         res = super(stock_move_consume, self).default_get(cr, uid, fields, context=context)        
83         move = self.pool.get('stock.move').browse(cr, uid, context['active_id'], context=context)
84         if 'product_id' in fields:
85             res.update({'product_id': move.product_id.id})     
86         if 'product_uom' in fields:
87             res.update({'product_uom': move.product_uom.id})    
88         if 'product_qty' in fields:
89             res.update({'product_qty': move.product_qty})
90         if 'location_id' in fields:
91             res.update({'location_id': move.location_id.id})
92         
93         return res   
94             
95     def do_move_consume(self, cr, uid, ids, context={}):
96         """ 
97              To move consumed products
98             
99              @param self: The object pointer.
100              @param cr: A database cursor
101              @param uid: ID of the user currently logged in
102              @param ids: the ID or list of IDs if we want more than one 
103              @param context: A standard dictionary 
104              
105              @return: 
106         
107         """              
108         move_obj = self.pool.get('stock.move')
109         move_ids = context['active_ids']
110         for data in self.read(cr, uid, ids):            
111             move_obj.action_consume(cr, uid, move_ids, 
112                              data['product_qty'], data['location_id'], 
113                              context=context)
114         return {}
115
116 stock_move_consume()
117
118
119 class stock_move_scrap(osv.osv_memory):
120     _name = "stock.move.scrap"
121     _description = "Scrap Products"
122     _inherit = "stock.move.consume"
123     
124     _defaults = {
125         'location_id': lambda *x: False
126     }
127
128     def default_get(self, cr, uid, fields, context=None):
129         """ 
130              Get default values
131             
132              @param self: The object pointer.
133              @param cr: A database cursor
134              @param uid: ID of the user currently logged in
135              @param fields: List of fields for default value 
136              @param context: A standard dictionary 
137              
138              @return: default values of fields
139         
140         """
141         res = super(stock_move_consume, self).default_get(cr, uid, fields, context=context)        
142         move = self.pool.get('stock.move').browse(cr, uid, context['active_id'], context=context)
143         location_obj = self.pool.get('stock.location')
144         scrpaed_location_ids = location_obj.search(cr, uid, [('scrap_location','=',True)])
145         
146         if 'product_id' in fields:
147             res.update({'product_id': move.product_id.id})     
148         if 'product_uom' in fields:
149             res.update({'product_uom': move.product_uom.id})    
150         if 'product_qty' in fields:
151             res.update({'product_qty': move.product_qty})
152         if 'location_id' in fields:
153             if scrpaed_location_ids:
154                 res.update({'location_id': scrpaed_location_ids[0]})
155             else:
156                 res.update({'location_id': False})
157         
158         return res
159         
160     def move_scrap(self, cr, uid, ids, context={}):
161         """ 
162              To move scraped products
163             
164              @param self: The object pointer.
165              @param cr: A database cursor
166              @param uid: ID of the user currently logged in
167              @param ids: the ID or list of IDs if we want more than one 
168              @param context: A standard dictionary 
169              
170              @return: 
171         
172         """              
173         move_obj = self.pool.get('stock.move')        
174         move_ids = context['active_ids']
175         for data in self.read(cr, uid, ids):
176             move_obj.action_scrap(cr, uid, move_ids, 
177                              data['product_qty'], data['location_id'], 
178                              context=context)
179         return {}
180
181 stock_move_scrap()
182
183
184 class split_in_production_lot(osv.osv_memory):
185     _name = "stock.move.split"
186     _description = "Split in Production lots"
187     
188     def default_get(self, cr, uid, fields, context=None):
189         """ 
190              Get default values
191             
192              @param self: The object pointer.
193              @param cr: A database cursor
194              @param uid: ID of the user currently logged in
195              @param fields: List of fields for default value 
196              @param context: A standard dictionary 
197              
198              @return: default values of fields
199         
200         """
201         
202         res = super(split_in_production_lot, self).default_get(cr, uid, fields, context=context)        
203         move = self.pool.get('stock.move').browse(cr, uid, context['active_id'], context=context)  
204         if 'product_id' in fields:
205             res.update({'product_id': move.product_id.id})  
206         return res
207     
208     _columns = {
209         'product_id': fields.many2one('product.product', 'Product', required=True, select=True),
210         'line_ids': fields.one2many('stock.move.split.lines', 'lot_id', 'Lots Number')
211      }
212     def split_lot(self, cr, uid, ids, context=None):
213         """ 
214              To split a lot
215             
216              @param self: The object pointer.
217              @param cr: A database cursor
218              @param uid: ID of the user currently logged in
219              @param ids: the ID or list of IDs if we want more than one 
220              @param context: A standard dictionary 
221              
222              @return: 
223         
224         """                    
225         self.split(cr, uid, ids, context.get('active_ids'), context=context)
226         return {}
227
228     def split(self, cr, uid, ids, move_ids, context=None):
229         """ 
230              To split stock moves into production lot
231             
232              @param self: The object pointer.
233              @param cr: A database cursor
234              @param uid: ID of the user currently logged in
235              @param ids: the ID or list of IDs if we want more than one 
236              @param move_ids: the ID or list of IDs of stock move we want to split
237              @param context: A standard dictionary 
238              
239              @return: 
240         
241         """                    
242         prodlot_obj = self.pool.get('stock.production.lot')
243         ir_sequence_obj = self.pool.get('ir.sequence')
244         move_obj = self.pool.get('stock.move')
245         new_move = []        
246         for data in self.browse(cr, uid, ids):
247             for move in move_obj.browse(cr, uid, move_ids):
248                 move_qty = move.product_qty
249                 quantity_rest = move.product_qty
250                 uos_qty_rest = move.product_uos_qty
251                 new_move = []                            
252                 for line in data.line_ids:
253                     quantity = line.quantity
254                     
255
256                     if quantity <= 0 or move_qty == 0:
257                         continue
258                     quantity_rest -= quantity
259                     uos_qty = quantity / move_qty * move.product_uos_qty
260                     uos_qty_rest = quantity_rest / move_qty * move.product_uos_qty
261                     if quantity_rest <= 0:
262                         quantity_rest = quantity
263                         break
264                     default_val = {
265                         'product_qty': quantity, 
266                         'product_uos_qty': uos_qty, 
267                         'state': move.state
268                     }
269                     current_move = move_obj.copy(cr, uid, move.id, default_val)
270                     new_move.append(current_move)
271                     prodlot_id = False
272                     if line.use_exist and line.name:
273                         prodlot_id = prodlot_obj.search(cr, uid, [('prefix','=',line.name),('product_id','=',data.product_id.id)])
274                         if prodlot_id:
275                             prodlot_id = prodlot_id[0]                    
276                     if not prodlot_id:
277                         sequence = ir_sequence_obj.get(cr, uid, 'stock.lot.serial')
278                         prodlot_id = prodlot_obj.create(cr, uid, {'name': sequence, 'prefix' : line.name}, 
279                                                  {'product_id': move.product_id.id})                    
280                     move_obj.write(cr, uid, [current_move], {'prodlot_id': prodlot_id})
281                     prodlot = prodlot_obj.browse(cr, uid, prodlot_id) 
282                     ref = '%d' % (move.id)
283                     if prodlot.ref:
284                         ref = '%s, %s' % (prodlot.ref, ref) 
285                     prodlot_obj.write(cr, uid, [prodlot_id], {'ref': ref})
286                     
287                     update_val = {}
288                     if quantity_rest > 0:                        
289                         update_val['product_qty'] = quantity_rest
290                         update_val['product_uos_qty'] = uos_qty_rest                          
291                         move_obj.write(cr, uid, [move.id], update_val)
292         return new_move
293 split_in_production_lot()
294
295 class stock_move_split_lines(osv.osv_memory):
296     _name = "stock.move.split.lines"
297     _description = "Split lines"
298     
299     _columns = {
300         'name': fields.char('Tracking serial', size=64), 
301         'quantity': fields.integer('Quantity'), 
302         'use_exist' : fields.boolean('Use Exist'),
303         'lot_id': fields.many2one('stock.move.split', 'Lot'),
304         'action': fields.selection([('split','Split'),('keepinone','Keep in one lot')],'Action'),
305     }
306     _defaults = {
307         'quantity': lambda *x: 1,
308         'action' : lambda *x: 'split', 
309     }
310
311 stock_move_split_lines()