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