[FIX] Stock : Fixed the problem suggested by Buildbot
[odoo/odoo.git] / addons / stock / wizard / stock_split_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 osv, fields
23
24 class stock_split_move_line(osv.osv_memory):
25     _name = 'stock.move.line.split'
26     _description = "Split Moves"
27     
28     def default_get(self, cr, uid, fields, context=None):
29         """ To get default values for the object.
30          @param self: The object pointer.
31          @param cr: A database cursor
32          @param uid: ID of the user currently logged in
33          @param fields: List of fields for which we want default values 
34          @param context: A standard dictionary 
35          @return: A dictionary which of fields with values. 
36         """ 
37         if context is None:
38             context = {}
39         res = super(stock_split_move_line, self).default_get(cr, uid, fields, context=context)
40         record_id = context and context.get('active_id', False) or False
41         pick_obj = self.pool.get('stock.picking')
42         pick = pick_obj.browse(cr, uid, record_id, context=context)
43         for m in [line for line in pick.move_lines]:
44             res['move%s'%(m.id)] = m.product_qty
45         return res
46     
47     def view_init(self, cr, uid, fields_list, context=None):
48         """ Creates view dynamically and adding fields at runtime.
49          @param self: The object pointer.
50          @param cr: A database cursor
51          @param uid: ID of the user currently logged in
52          @param context: A standard dictionary 
53          @return: New arch of view with new columns.
54         """
55         res = super(stock_split_move_line, self).view_init(cr, uid, fields_list, context=context)
56         record_id = context and context.get('active_id', False) or False
57         if record_id:
58             pick_obj = self.pool.get('stock.picking')
59             try:
60                 pick = pick_obj.browse(cr, uid, record_id, context=context)
61                 for m in [line for line in pick.move_lines]:
62                     if 'move%s' % m.id not in self._columns:
63                         self._columns['move%s' % m.id] = fields.float(string=m.product_id.name)
64             except:
65                 return res
66         return res
67     
68     def fields_view_get(self, cr, uid, view_id=None, view_type='form', 
69                         context=None, toolbar=False, submenu=False):
70         """ Changes the view dynamically
71          @param self: The object pointer.
72          @param cr: A database cursor
73          @param uid: ID of the user currently logged in
74          @param context: A standard dictionary 
75          @return: New arch of view.
76         """
77         res = super(stock_split_move_line, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar,submenu=False)
78         record_id = context and context.get('active_id', False) or False
79         assert record_id,'Active ID not found'
80         pick_obj = self.pool.get('stock.picking')
81         pick = pick_obj.browse(cr, uid, record_id, context=context)
82         arch_lst = ['<?xml version="1.0"?>', '<form string="Split lines">', '<label string="Indicate here the quantity of the new line. A quantity of zero will not split the line." colspan="4"/>']
83         for m in [line for line in pick.move_lines]:
84             quantity = m.product_qty
85             arch_lst.append('<field name="move%s" />\n<newline />' % (m.id,))
86             res['fields']['move%s' % m.id] = {'string' : m.product_id.name, 'type' : 'float', 'required' : True}
87         arch_lst.append('<group col="2" colspan="4">')
88         arch_lst.append('<button icon="gtk-cancel" special="cancel" string="Cancel" />')
89         arch_lst.append('<button name="split_lines" string="Split" colspan="1" type="object" icon="gtk-apply" />')
90         arch_lst.append('</group>')
91         arch_lst.append('</form>')
92         res['arch'] = '\n'.join(arch_lst)
93         return res
94     
95     def split_lines(self, cr, uid, ids, context=None):
96         """ Splits moves in quantity given in the wizard.
97          @param self: The object pointer.
98          @param cr: A database cursor
99          @param uid: ID of the user currently logged in
100          @param ids: List of ids selected 
101          @param context: A standard dictionary 
102          @return: A dictionary which of fields with values. 
103         """ 
104         if context is None:
105             context = {}
106         move_obj = self.pool.get('stock.move')
107         record_id = context and context.get('active_id', False) or False
108         pick_obj = self.pool.get('stock.picking')
109         pick = pick_obj.browse(cr, uid, record_id, context=context)
110         data = self.read(cr, uid, ids[0])
111         for move in pick.move_lines:
112             quantity = data['move%s' % move.id]
113             if 0 < quantity < move.product_qty:
114                 new_qty = move.product_qty - quantity
115                 new_uos_qty = new_qty / move.product_qty * move.product_uos_qty
116                 new_obj = move_obj.copy(cr, uid, move.id, {'product_qty' : new_qty, 'product_uos_qty': new_uos_qty, 'state':move.state})
117                 uos_qty = quantity / move.product_qty * move.product_uos_qty
118                 move_obj.write(cr, uid, [move.id], {'product_qty' : quantity, 'product_uos_qty': uos_qty})
119         return {'type': 'ir.actions.act_window_close'}
120     
121 stock_split_move_line()
122
123 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
124