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