[IMP]: Improved views, code according to new specifications. Improved tooltips
[odoo/odoo.git] / addons / account / res_config.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
25 class account_configuration(osv.osv_memory):
26     _inherit = 'res.config'
27
28     _columns = {
29             'tax_policy': fields.selection([
30                 ('no_tax', 'No Tax'),
31                 ('global_on_order', 'Global On Order'),
32                 ('on_order_line', 'On Order Lines'),
33             ], 'Taxes', required=True, 
34             help="""
35                 If you want to apply global tax on sale order then select 'Global On Order' it will add 'Global On Order' group to employees.
36                 If you want to apply different taxes for sale order lines then select 'On Order Lines' it will add 'On Order Lines' group to employees.
37             """),
38             'tax_value': fields.float('Value'),
39     }
40     
41     _defaults = {
42             'tax_policy': 'global_on_order',
43     }
44
45     def get_default_tax_policy(self, cr, uid, ids, context=None):
46         applied_groups = self.get_default_applied_groups(cr, uid, ids, context=context)
47         if applied_groups.get('group_sale_taxes_global_on_order'):
48             applied_groups.update({'tax_policy': 'global_on_order'})
49         elif applied_groups.get('group_sale_taxes_on_order_line'):
50             applied_groups.update({'tax_policy': 'on_order_line'})
51         else:
52             applied_groups.update({'tax_policy': 'no_tax'})
53         return applied_groups
54
55     def _check_default_tax(self, cr, uid, context=None):
56         ir_values_obj = self.pool.get('ir.values')
57         for tax in ir_values_obj.get(cr, uid, 'default', False, ['product.product']):
58             if tax[1] == 'taxes_id':
59                 return tax[2]
60         return False
61
62     def default_get(self, cr, uid, fields_list, context=None):
63         ir_values_obj = self.pool.get('ir.values')
64         res = super(account_configuration, self).default_get(cr, uid, fields_list, context=context)
65         res.update({'tax_value': 15.0})
66         tax_id = self._check_default_tax(cr, uid, context)
67         if tax_id:
68             res.update({'tax_value': tax_id and tax_id[0]})
69         return res
70     
71     def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
72         ir_values_obj = self.pool.get('ir.values')
73         res = super(account_configuration, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu)
74         if self._check_default_tax(cr, uid, context) and res['fields'].get('tax_value'):
75             res['fields']['tax_value'] = {'domain': [], 'views': {}, 'context': {}, 'selectable': True, 'type': 'many2one', 'relation': 'account.tax', 'string': 'Value'}
76         return res
77
78     def set_tax_policy(self, cr, uid, ids, vals, context=None):
79         data_obj = self.pool.get('ir.model.data')
80         users_obj = self.pool.get('res.users')
81         groups_obj = self.pool.get('res.groups')
82         ir_values_obj = self.pool.get('ir.values')
83         dummy,user_group_id = data_obj.get_object_reference(cr, uid, 'base', 'group_user')
84         tax_policy = vals.get('tax_policy')
85         order_group_id = data_obj.get_object(cr, uid, 'base', 'group_sale_taxes_global_on_order').id
86         order_line_group_id = data_obj.get_object(cr, uid, 'base', 'group_sale_taxes_on_order_line').id
87         group_id = False
88         remove_group_id = False
89
90         if tax_policy == 'global_on_order':
91             group_id = order_group_id
92             remove_group_id = order_line_group_id
93         elif tax_policy == 'on_order_line':
94             group_id = order_line_group_id
95             remove_group_id = order_group_id
96
97         if group_id:
98             groups_obj.write(cr, uid, [user_group_id], {'implied_ids': [(4,group_id)]})
99             users_obj.write(cr, uid, [uid], {'groups_id': [(4,group_id)]})
100             ir_values_obj.set(cr, uid, 'default', False, 'groups_id', ['res.users'], [(4,group_id)])
101             groups_obj.write(cr, uid, [user_group_id], {'implied_ids': [(3,remove_group_id)]})
102             users_obj.write(cr, uid, [uid], {'groups_id': [(3,remove_group_id)]})
103             ir_values_obj.set(cr, uid, 'default', False, 'groups_id', ['res.users'], [(3,remove_group_id)])
104         else:
105             groups = [order_group_id, order_line_group_id]
106             for group_id in groups:
107                 groups_obj.write(cr, uid, [user_group_id], {'implied_ids': [(3,group_id)]})
108                 users_obj.write(cr, uid, [uid], {'groups_id': [(3,group_id)]})
109                 ir_values_obj.set(cr, uid, 'default', False, 'groups_id', ['res.users'], [(3,group_id)])
110
111 account_configuration()