[IMP]stock:add message when a lot quantity is more than available quantity
[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     #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 Production lots"
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 UoM')),
169         'product_id': fields.many2one('product.product', 'Product', required=True, select=True),
170         'product_uom': fields.many2one('product.uom', 'UoM'),
171         'line_ids': fields.one2many('stock.move.split.lines', 'wizard_id', 'Production Lots'),
172         'line_exist_ids': fields.one2many('stock.move.split.lines', 'wizard_exist_id', 'Production Lots'),
173         '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."),
174         'location_id': fields.many2one('stock.location', 'Source Location')
175      }
176
177     def split_lot(self, cr, uid, ids, context=None):
178         """ To split a lot"""
179         if context is None:
180             context = {}
181         res = self.split(cr, uid, ids, context.get('active_ids'), context=context)
182         return {'type': 'ir.actions.act_window_close'}
183
184     def split(self, cr, uid, ids, move_ids, context=None):
185         """ To split stock moves into production lot
186
187         :param move_ids: the ID or list of IDs of stock move we want to split
188         """
189         if context is None:
190             context = {}
191         assert context.get('active_model') == 'stock.move',\
192              'Incorrect use of the stock move split wizard'
193         inventory_id = context.get('inventory_id', False)
194         prodlot_obj = self.pool.get('stock.production.lot')
195         inventory_obj = self.pool.get('stock.inventory')
196         move_obj = self.pool.get('stock.move')
197         new_move = []
198         for data in self.browse(cr, uid, ids, context=context):
199             for move in move_obj.browse(cr, uid, move_ids, context=context):
200                 move_qty = move.product_qty
201                 quantity_rest = move.product_qty
202                 uos_qty_rest = move.product_uos_qty
203                 new_move = []
204                 if data.use_exist:
205                     lines = [l for l in data.line_exist_ids if l]
206                 else:
207                     lines = [l for l in data.line_ids if l]
208                 total_move_qty = 0.0
209                 for line in lines:
210                     quantity = line.quantity
211                     total_move_qty += quantity
212                     if total_move_qty > move_qty:
213                         raise osv.except_osv(_('Processing Error'), _('Processing quantity %d for %s is larger than the available quantity %d!')\
214                                      %(total_move_qty, move.product_id.name, move_qty))
215                     if quantity <= 0 or move_qty == 0:
216                         continue
217                     quantity_rest -= quantity
218                     uos_qty = quantity / move_qty * move.product_uos_qty
219                     uos_qty_rest = quantity_rest / move_qty * move.product_uos_qty
220                     if quantity_rest < 0:
221                         quantity_rest = quantity
222                         self.pool.get('stock.move').log(cr, uid, move.id, _('Unable to assign all lots to this move!'))
223                         return False
224                     default_val = {
225                         'product_qty': quantity,
226                         'product_uos_qty': uos_qty,
227                         'state': move.state
228                     }
229                     if quantity_rest > 0:
230                         current_move = move_obj.copy(cr, uid, move.id, default_val, context=context)
231                         if inventory_id and current_move:
232                             inventory_obj.write(cr, uid, inventory_id, {'move_ids': [(4, current_move)]}, context=context)
233                         new_move.append(current_move)
234
235                     if quantity_rest == 0:
236                         current_move = move.id
237                     prodlot_id = False
238                     if data.use_exist:
239                         prodlot_id = line.prodlot_id.id
240                     if not prodlot_id:
241                         prodlot_id = prodlot_obj.create(cr, uid, {
242                             'name': line.name,
243                             'product_id': move.product_id.id},
244                         context=context)
245
246                     move_obj.write(cr, uid, [current_move], {'prodlot_id': prodlot_id, 'state':move.state})
247
248                     update_val = {}
249                     if quantity_rest > 0:
250                         update_val['product_qty'] = quantity_rest
251                         update_val['product_uos_qty'] = uos_qty_rest
252                         update_val['state'] = move.state
253                         move_obj.write(cr, uid, [move.id], update_val)
254
255         return new_move
256
257 split_in_production_lot()
258
259 class stock_move_split_lines_exist(osv.osv_memory):
260     _name = "stock.move.split.lines"
261     _description = "Stock move Split lines"
262     _columns = {
263         'name': fields.char('Production Lot', size=64),
264         'quantity': fields.float('Quantity', digits_compute=dp.get_precision('Product UoM')),
265         'wizard_id': fields.many2one('stock.move.split', 'Parent Wizard'),
266         'wizard_exist_id': fields.many2one('stock.move.split', 'Parent Wizard (for existing lines)'),
267         'prodlot_id': fields.many2one('stock.production.lot', 'Production Lot'),
268     }
269     _defaults = {
270         'quantity': 1.0,
271     }
272
273     def onchange_lot_id(self, cr, uid, ids, prodlot_id=False, product_qty=False,
274                         loc_id=False, product_id=False, uom_id=False):
275         return self.pool.get('stock.move').onchange_lot_id(cr, uid, [], prodlot_id, product_qty,
276                         loc_id, product_id, uom_id)
277
278 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: