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