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