[MERGE] forward port of branch 8.0 up to 591e329
[odoo/odoo.git] / addons / crm / crm.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-today OpenERP SA (<http://www.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.osv import osv, fields
23 from openerp.http import request
24
25 AVAILABLE_PRIORITIES = [
26     ('0', 'Normal'),
27     ('1', 'Low'),
28     ('2', 'High'),
29     ('3', 'Very High'),
30 ]
31
32
33 class crm_tracking_medium(osv.Model):
34     # OLD crm.case.channel
35     _name = "crm.tracking.medium"
36     _description = "Channels"
37     _order = 'name'
38     _columns = {
39         'name': fields.char('Channel Name', required=True),
40         'active': fields.boolean('Active'),
41     }
42     _defaults = {
43         'active': lambda *a: 1,
44     }
45
46
47 class crm_tracking_campaign(osv.Model):
48     # OLD crm.case.resource.type
49     _name = "crm.tracking.campaign"
50     _description = "Campaign"
51     _rec_name = "name"
52     _columns = {
53         'name': fields.char('Campaign Name', required=True, translate=True),
54         'team_id': fields.many2one('crm.team', 'Sales Team', oldname='section_id'),
55     }
56
57
58 class crm_tracking_source(osv.Model):
59     _name = "crm.tracking.source"
60     _description = "Source"
61     _rec_name = "name"
62     _columns = {
63         'name': fields.char('Source Name', required=True, translate=True),
64     }
65
66
67 class crm_tracking_mixin(osv.AbstractModel):
68     """Mixin class for objects which can be tracked by marketing. """
69     _name = 'crm.tracking.mixin'
70
71     _columns = {
72         'campaign_id': fields.many2one('crm.tracking.campaign', 'Campaign',  # old domain ="['|',('team_id','=',team_id),('team_id','=',False)]"
73                                        help="This is a name that helps you keep track of your different campaign efforts Ex: Fall_Drive, Christmas_Special"),
74         'source_id': fields.many2one('crm.tracking.source', 'Source', help="This is the source of the link Ex: Search Engine, another domain, or name of email list"),
75         'medium_id': fields.many2one('crm.tracking.medium', 'Channel', help="This is the method of delivery. Ex: Postcard, Email, or Banner Ad", oldname='channel_id'),
76     }
77
78     def tracking_fields(self):
79         return [('utm_campaign', 'campaign_id'), ('utm_source', 'source_id'), ('utm_medium', 'medium_id')]
80
81     def tracking_get_values(self, cr, uid, vals, context=None):
82         for key, fname in self.tracking_fields():
83             field = self._fields[fname]
84             value = vals.get(fname) or (request and request.httprequest.cookies.get(key))  # params.get should be always in session by the dispatch from ir_http
85             if field.type == 'many2one' and isinstance(value, basestring):
86                 # if we receive a string for a many2one, we search/create the id
87                 if value:
88                     Model = self.pool[field.comodel_name]
89                     rel_id = Model.name_search(cr, uid, value, context=context)
90                     if rel_id:
91                         rel_id = rel_id[0][0]
92                     else:
93                         rel_id = Model.create(cr, uid, {'name': value}, context=context)
94                 vals[fname] = rel_id
95             else:
96                 # Here the code for others cases that many2one
97                 vals[fname] = value
98         return vals
99
100     def _get_default_track(self, cr, uid, field, context=None):
101         return self.tracking_get_values(cr, uid, {}, context=context).get(field)
102
103     _defaults = {
104         'source_id': lambda self, cr, uid, ctx: self._get_default_track(cr, uid, 'source_id', ctx),
105         'campaign_id': lambda self, cr, uid, ctx: self._get_default_track(cr, uid, 'campaign_id', ctx),
106         'medium_id': lambda self, cr, uid, ctx: self._get_default_track(cr, uid, 'medium_id', ctx),
107     }
108
109
110 class crm_stage(osv.Model):
111     """ Model for case stages. This models the main stages of a document
112         management flow. Main CRM objects (leads, opportunities, project
113         issues, ...) will now use only stages, instead of state and stages.
114         Stages are for example used to display the kanban view of records.
115     """
116     _name = "crm.stage"
117     _description = "Stage of case"
118     _rec_name = 'name'
119     _order = "sequence"
120
121     _columns = {
122         'name': fields.char('Stage Name', required=True, translate=True),
123         'sequence': fields.integer('Sequence', help="Used to order stages. Lower is better."),
124         'probability': fields.float('Probability (%)', required=True, help="This percentage depicts the default/average probability of the Case for this stage to be a success"),
125         'on_change': fields.boolean('Change Probability Automatically', help="Setting this stage will change the probability automatically on the opportunity."),
126         'requirements': fields.text('Requirements'),
127         'team_ids': fields.many2many('crm.team', 'crm_team_stage_rel', 'stage_id', 'team_id', string='Teams',
128                         help="Link between stages and sales teams. When set, this limitate the current stage to the selected sales teams."),
129         'case_default': fields.boolean('Default to New Sales Team',
130                         help="If you check this field, this stage will be proposed by default on each sales team. It will not assign this stage to existing teams."),
131         'legend_priority': fields.text(
132             'Priority Management Explanation', translate=True,
133             help='Explanation text to help users using the star and priority mechanism on stages or issues that are in this stage.'),
134         'fold': fields.boolean('Folded in Kanban View',
135                                help='This stage is folded in the kanban view when'
136                                'there are no records in that stage to display.'),
137         'type': fields.selection([('lead', 'Lead'), ('opportunity', 'Opportunity'), ('both', 'Both')],
138                                  string='Type', required=True,
139                                  help="This field is used to distinguish stages related to Leads from stages related to Opportunities, or to specify stages available for both types."),
140     }
141
142     _defaults = {
143         'sequence': 1,
144         'probability': 0.0,
145         'on_change': True,
146         'fold': False,
147         'type': 'both',
148         'case_default': True,
149     }
150
151 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: