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