[FIX] (5.0 - 6.0)Stock : Split moves wizard should respect current language
[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):
29         """ 
30          To get default values for the object.
31          @param self: The object pointer.
32          @param cr: A database cursor
33          @param uid: ID of the user currently logged in
34          @param fields: List of fields for which we want default values 
35          @param context: A standard dictionary 
36          @return: A dictionary which of fields with values. 
37         """ 
38         res = super(stock_split_move_line, self).default_get(cr, uid, fields, context=context)
39         record_id = context and context.get('active_id', False) or False
40         pick_obj = self.pool.get('stock.picking')
41         pick = pick_obj.browse(cr, uid, record_id, context=context)
42         for m in [line for line in pick.move_lines]:
43             res['move%s'%(m.id)] = m.product_qty
44         return res
45     
46     def view_init(self, cr, uid, fields_list, context=None):
47         """ 
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         """ 
71          Changes the view dynamically
72          @param self: The object pointer.
73          @param cr: A database cursor
74          @param uid: ID of the user currently logged in
75          @param context: A standard dictionary 
76          @return: New arch of view.
77         """
78         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)
79         record_id = context and context.get('active_id', False) or False
80         assert record_id,'Active ID not found'
81         pick_obj = self.pool.get('stock.picking')
82         pick = pick_obj.browse(cr, uid, record_id, context=context)
83         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"/>']
84         for m in [line for line in pick.move_lines]:
85             quantity = m.product_qty
86             arch_lst.append('<field name="move%s" />\n<newline />' % (m.id,))
87             res['fields']['move%s' % m.id] = {'string' : m.product_id.name, 'type' : 'float', 'required' : True}
88         arch_lst.append('<group col="2" colspan="4">')
89         arch_lst.append('<button icon="gtk-cancel" special="cancel" string="Cancel" />')
90         arch_lst.append('<button name="split_lines" string="Split" colspan="1" type="object" icon="gtk-apply" />')
91         arch_lst.append('</group>')
92         arch_lst.append('</form>')
93         res['arch'] = '\n'.join(arch_lst)
94         return res
95     
96     def split_lines(self, cr, uid, ids, context):
97         """ 
98          Splits moves in quantity given in the wizard.
99          @param self: The object pointer.
100          @param cr: A database cursor
101          @param uid: ID of the user currently logged in
102          @param ids: List of ids selected 
103          @param context: A standard dictionary 
104          @return: A dictionary which of fields with values. 
105         """ 
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         move_ids = [m.id for m in [line for line in pick.move_lines]]
112         for move in move_obj.browse(cr, uid, move_ids, context=context):
113             quantity = data['move%s' % move.id]
114             if 0 < quantity < move.product_qty:
115                 new_qty = move.product_qty - quantity
116                 new_uos_qty = new_qty / move.product_qty * move.product_uos_qty
117                 new_obj = move_obj.copy(cr, uid, move.id, {'product_qty' : new_qty, 'product_uos_qty': new_uos_qty, 'state':move.state})
118                 uos_qty = quantity / move.product_qty * move.product_uos_qty
119                 move_obj.write(cr, uid, [move.id], {'product_qty' : quantity, 'product_uos_qty': uos_qty})
120         return {}
121     
122 stock_split_move_line()
123
124 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
125