[MERGE] lp:~openerp-dev/openobject-addons/trunk-usability-processing_incomming_shipme...
[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.browse(cr, uid, ids, context=context):
104             move_obj.action_consume(cr, uid, move_ids,
105                              data.product_qty, data.location_id.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.browse(cr, uid, ids):
165             move_obj.action_scrap(cr, uid, move_ids,
166                              data.product_qty, data.location_id.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
189         res = super(split_in_production_lot, self).default_get(cr, uid, fields, context=context)
190         if context.get('active_id'):
191             move = self.pool.get('stock.move').browse(cr, uid, context['active_id'], context=context)
192             if 'product_id' in fields:
193                 res.update({'product_id': move.product_id.id})
194             if 'product_uom' in fields:
195                 res.update({'product_uom': move.product_uom.id})
196             if 'qty' in fields:
197                 res.update({'qty': move.product_qty})
198             if 'use_exist' in fields:
199                 res.update({'use_exist': (move.picking_id and move.picking_id.type=='out' and True) or False})
200         return res
201
202     _columns = {
203         'qty': fields.float('Quantity', digits_compute=dp.get_precision('Product UoM')),
204         'product_id': fields.many2one('product.product', 'Product', required=True, select=True),
205         'product_uom': fields.many2one('product.uom', 'UoM'),
206         'line_ids': fields.one2many('stock.move.split.lines', 'lot_id', 'Production Lots'),
207         'line_exist_ids': fields.one2many('stock.move.split.lines.exist', 'lot_id', 'Production Lots'),
208         '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."),
209      }
210
211     def split_lot(self, cr, uid, ids, context=None):
212         """ To split a lot
213         @param self: The object pointer.
214         @param cr: A database cursor
215         @param uid: ID of the user currently logged in
216         @param ids: An ID or list of IDs if we want more than one
217         @param context: A standard dictionary
218         @return:
219         """
220         if context is None:
221             context = {}
222         self.split(cr, uid, ids, context.get('active_ids'), context=context)
223         return {'type': 'ir.actions.act_window_close'}
224
225     def split(self, cr, uid, ids, move_ids, context=None):
226         """ To split stock moves into production lot
227         @param self: The object pointer.
228         @param cr: A database cursor
229         @param uid: ID of the user currently logged in
230         @param ids: the ID or list of IDs if we want more than one
231         @param move_ids: the ID or list of IDs of stock move we want to split
232         @param context: A standard dictionary
233         @return:
234         """
235         if context is None:
236             context = {}
237         inventory_id = context.get('inventory_id', False)
238         prodlot_obj = self.pool.get('stock.production.lot')
239         inventory_obj = self.pool.get('stock.inventory')
240         move_obj = self.pool.get('stock.move')
241         new_move = []
242         for data in self.browse(cr, uid, ids, context=context):
243             for move in move_obj.browse(cr, uid, move_ids, context=context):
244                 move_qty = move.product_qty
245                 quantity_rest = move.product_qty
246                 uos_qty_rest = move.product_uos_qty
247                 new_move = []
248                 if data.use_exist:
249                     lines = [l for l in data.line_exist_ids if l]
250                 else:
251                     lines = [l for l in data.line_ids if l]
252                 for line in lines:
253                     quantity = line.quantity
254                     if quantity <= 0 or move_qty == 0:
255                         continue
256                     quantity_rest -= quantity
257                     uos_qty = quantity / move_qty * move.product_uos_qty
258                     uos_qty_rest = quantity_rest / move_qty * move.product_uos_qty
259                     if quantity_rest < 0:
260                         quantity_rest = quantity
261                         break
262                     default_val = {
263                         'product_qty': quantity,
264                         'product_uos_qty': uos_qty,
265                         'state': move.state
266                     }
267                     if quantity_rest > 0:
268                         current_move = move_obj.copy(cr, uid, move.id, default_val, context=context)
269                         if inventory_id and current_move:
270                             inventory_obj.write(cr, uid, inventory_id, {'move_ids': [(4, current_move)]}, context=context)
271                         new_move.append(current_move)
272
273                     if quantity_rest == 0:
274                         current_move = move.id
275                     prodlot_id = False
276                     if data.use_exist:
277                         prodlot_id = line.prodlot_id.id
278                     if not prodlot_id:
279                         prodlot_id = prodlot_obj.create(cr, uid, {
280                             'name': line.name,
281                             'product_id': move.product_id.id},
282                         context=context)
283
284                     move_obj.write(cr, uid, [current_move], {'prodlot_id': prodlot_id, 'state':move.state})
285
286                     update_val = {}
287                     if quantity_rest > 0:
288                         update_val['product_qty'] = quantity_rest
289                         update_val['product_uos_qty'] = uos_qty_rest
290                         update_val['state'] = move.state
291                         move_obj.write(cr, uid, [move.id], update_val)
292
293         return new_move
294
295 split_in_production_lot()
296
297 class stock_move_split_lines_exist(osv.osv_memory):
298     _name = "stock.move.split.lines.exist"
299     _description = "Exist Split lines"
300     _columns = {
301         'name': fields.char('Tracking serial', size=64),
302         'quantity': fields.float('Quantity', digits_compute=dp.get_precision('Product UoM')),
303         'lot_id': fields.many2one('stock.move.split', 'Lot'),
304         'prodlot_id': fields.many2one('stock.production.lot', 'Production Lot'),
305     }
306     _defaults = {
307         'quantity': 1.0,
308     }
309
310 stock_move_split_lines_exist()
311
312 class stock_move_split_lines(osv.osv_memory):
313     _name = "stock.move.split.lines"
314     _description = "Split lines"
315     _columns = {
316         'name': fields.char('Tracking serial', size=64),
317         'quantity': fields.float('Quantity', digits_compute=dp.get_precision('Product UoM')),
318         'use_exist' : fields.boolean('Existing Lot'),
319         'lot_id': fields.many2one('stock.move.split', 'Lot'),
320         'action': fields.selection([('split','Split'),('keepinone','Keep in one lot')],'Action'),
321     }
322     _defaults = {
323         'quantity': 1.00,
324         'action' : 'split',
325     }
326 stock_move_split_lines()