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