73086c95565fa3e5295cf1e775f28e7ee1bef054
[odoo/odoo.git] / addons / crm / res_config.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Business Applications
5 #    Copyright (C) 2004-TODAY OpenERP S.A. (<http://openerp.com>).
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 openerp import SUPERUSER_ID
23 from openerp.osv import fields, osv
24
25
26 class crm_configuration(osv.TransientModel):
27     _name = 'sale.config.settings'
28     _inherit = ['sale.config.settings', 'fetchmail.config.settings']
29
30     def set_group_multi_salesteams(self, cr, uid, ids, context=None):
31         """ This method is automatically called by res_config as it begins
32             with set. It is used to implement the 'one group or another'
33             behavior. We have to perform some group manipulation by hand    
34             because in res_config.execute(), set_* methods are called
35             after group_*; therefore writing on an hidden res_config file
36             could not work.
37             If group_multi_salesteams is checked: remove group_mono_salesteams
38             from group_user, remove the users. Otherwise, just add
39             group_mono_salesteams in group_user.
40             The inverse logic about group_multi_salesteams is managed by the
41             normal behavior of 'group_multi_salesteams' field.
42         """
43         def ref(xml_id):
44             mod, xml = xml_id.split('.', 1)
45             return self.pool['ir.model.data'].get_object(cr, uid, mod, xml, context)
46
47         for obj in self.browse(cr, uid, ids, context=context):
48             config_group = ref('base.group_mono_salesteams')
49             base_group = ref('base.group_user')
50             if obj.group_multi_salesteams:
51                 base_group.write({'implied_ids': [(3, config_group.id)]})
52                 config_group.write({'users': [(3, u.id) for u in base_group.users]})
53             else:
54                 base_group.write({'implied_ids': [(4, config_group.id)]})
55         return True
56
57     _columns = {
58         'group_fund_raising': fields.boolean("Manage Fund Raising",
59             implied_group='crm.group_fund_raising',
60             help="""Allows you to trace and manage your activities for fund raising."""),
61         'module_crm_claim': fields.boolean("Manage Customer Claims",
62             help='Allows you to track your customers/suppliers claims and grievances.\n'
63                  '-This installs the module crm_claim.'),
64         'module_crm_helpdesk': fields.boolean("Manage Helpdesk and Support",
65             help='Allows you to communicate with Customer, process Customer query, and provide better help and support.\n'
66                  '-This installs the module crm_helpdesk.'),
67         'group_multi_salesteams': fields.boolean("Organize Sales activities into multiple Sales Teams",
68             implied_group='base.group_multi_salesteams',
69             help="""Allows you to use Sales Teams to manage your leads and opportunities."""),
70         'alias_prefix': fields.char('Default Alias Name for Leads'),
71         'alias_domain' : fields.char('Alias Domain'),
72         'group_scheduled_calls_menu': fields.boolean("Show Scheduled Calls Menu",
73             implied_group='crm.group_scheduled_calls_menu',
74             help="""Allow to show 'Scheduled Calls' menu under 'Sales/Phone Calls' menu""")
75     }
76
77     _defaults = {
78         'alias_domain': lambda self, cr, uid, context: self.pool['mail.alias']._get_alias_domain(cr, SUPERUSER_ID, [1], None, None)[1],
79     }
80
81     def _find_default_lead_alias_id(self, cr, uid, context=None):
82         alias_id = self.pool['ir.model.data'].xmlid_to_res_id(cr, uid, 'crm.mail_alias_lead_info')
83         if not alias_id:
84             alias_ids = self.pool['mail.alias'].search(
85                 cr, uid, [
86                     ('alias_model_id.model', '=', 'crm.lead'),
87                     ('alias_force_thread_id', '=', False),
88                     ('alias_parent_model_id.model', '=', 'crm.case.section'),
89                     ('alias_parent_thread_id', '=', False),
90                     ('alias_defaults', '=', '{}')
91                 ], context=context)
92             alias_id = alias_ids and alias_ids[0] or False
93         return alias_id
94
95     def get_default_alias_prefix(self, cr, uid, ids, context=None):
96         alias_name = False
97         alias_id = self._find_default_lead_alias_id(cr, uid, context=context)
98         if alias_id:
99             alias_name = self.pool['mail.alias'].browse(cr, uid, alias_id, context=context).alias_name
100         return {'alias_prefix': alias_name}
101
102     def set_default_alias_prefix(self, cr, uid, ids, context=None):
103         mail_alias = self.pool['mail.alias']
104         for record in self.browse(cr, uid, ids, context=context):
105             alias_id = self._find_default_lead_alias_id(cr, uid, context=context)
106             if not alias_id:
107                 create_ctx = dict(context, alias_model_name='crm.lead', alias_parent_model_name='crm.case.section')
108                 alias_id = self.pool['mail.alias'].create(cr, uid, {'alias_name': record.alias_prefix}, context=create_ctx)
109             else:
110                 mail_alias.write(cr, uid, alias_id, {'alias_name': record.alias_prefix}, context=context)
111         return True