[MERGE] procurement, product, stock: improve warehouse (polish3)
[odoo/odoo.git] / addons / stock / wizard / stock_splitinto.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_split_into(osv.osv_memory):
27     _name = "stock.split.into"
28     _description = "Split into"
29     _columns = {
30         'quantity': fields.float('Quantity',digits_compute=dp.get_precision('Product UoM')),
31     }
32     _defaults = {
33         'quantity': lambda *x: 0,
34     }
35
36     def split(self, cr, uid, data, context=None):
37         if context is None:
38             context = {}
39
40         inventory_id = context.get('inventory_id', False)
41         rec_id = context and context.get('active_ids', False)
42         move_obj = self.pool.get('stock.move')
43         track_obj = self.pool.get('stock.tracking')
44         inventory_obj = self.pool.get('stock.inventory')
45         quantity = self.browse(cr, uid, data[0], context=context).quantity or 0.0
46         for move in move_obj.browse(cr, uid, rec_id, context=context):
47             quantity_rest = move.product_qty - quantity
48             #if move.tracking_id :
49             #    raise osv.except_osv(_('Error!'),  _('The current move line is already assigned to a pack, please remove it first if you really want to change it ' \
50             #                        'for this product: "%s" (id: %d)') % \
51             #                        (move.product_id.name, move.product_id.id,))
52             if quantity > move.product_qty:
53                 raise osv.except_osv(_('Error!'),  _('Total quantity after split exceeds the quantity to split ' \
54                                     'for this product: "%s" (id: %d)') % \
55                                     (move.product_id.name, move.product_id.id,))
56             if quantity > 0:
57                 move_obj.setlast_tracking(cr, uid, [move.id], context=context)
58                 move_obj.write(cr, uid, [move.id], {
59                     'product_qty': quantity,
60                     'product_uos_qty': quantity,
61                     'product_uos': move.product_uom.id,
62                 })
63
64             if quantity_rest>0:
65                 quantity_rest = move.product_qty - quantity
66                 tracking_id = track_obj.create(cr, uid, {}, context=context)
67                 if quantity == 0.0:
68                     move_obj.write(cr, uid, [move.id], {'tracking_id': tracking_id}, context=context)
69                 else:
70                     default_val = {
71                         'product_qty': quantity_rest,
72                         'product_uos_qty': quantity_rest,
73                         'tracking_id': tracking_id,
74                         'state': move.state,
75                         'product_uos': move.product_uom.id
76                     }
77                     current_move = move_obj.copy(cr, uid, move.id, default_val, context=context)
78                     if inventory_id and current_move:
79                         inventory_obj.write(cr, uid, inventory_id, {'move_ids': [(4, current_move)]}, context=context)
80
81
82         return {'type': 'ir.actions.act_window_close'}
83 stock_split_into()
84
85
86 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: