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