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