[IMP] Crm: Add tracking mixin to manage utm campaign and add this mixin to mass_maili...
[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', 'Very Low'),
27     ('1', 'Low'),
28     ('2', 'Normal'),
29     ('3', 'High'),
30     ('4', 'Very High'),
31 ]
32
33
34 class crm_tracking_medium(osv.Model):
35     # OLD crm.case.channel
36     _name = "crm.tracking.medium"
37     _description = "Channels"
38     _order = 'name'
39     _columns = {
40         'name': fields.char('Channel Name', required=True),
41         'active': fields.boolean('Active'),
42     }
43     _defaults = {
44         'active': lambda *a: 1,
45     }
46
47
48 class crm_tracking_campaign(osv.Model):
49     # OLD crm.case.resource.type
50     _name = "crm.tracking.campaign"
51     _description = "Campaign"
52     _rec_name = "name"
53     _columns = {
54         'name': fields.char('Campaign Name', required=True, translate=True),
55         'section_id': fields.many2one('crm.case.section', 'Sales Team'),
56     }
57
58
59 class crm_tracking_source(osv.Model):
60     _name = "crm.tracking.source"
61     _description = "Source"
62     _rec_name = "name"
63     _columns = {
64         'name': fields.char('Source Name', required=True, translate=True),
65     }
66
67
68 class crm_tracking_mixin(osv.AbstractModel):
69     """Mixin class for objects which can be tracked by marketing. """
70     _name = 'crm.tracking.mixin'
71
72     _columns = {
73         'campaign_id': fields.many2one('crm.tracking.campaign', 'Campaign',  # old domain ="['|',('section_id','=',section_id),('section_id','=',False)]"
74                                        help="This is a name that helps you keep track of your different campaign efforts Example: Fall_Drive, Christmas_Special"),
75         'source_id': fields.many2one('crm.tracking.source', 'Source', help="This is the source of the link Example: Search Engine, another domain, or name of email list"),
76         'medium_id': fields.many2one('crm.tracking.medium', 'Channel', help="This is the method of delivery. EX: Postcard, Email, or Banner Ad"),
77     }
78
79     def tracking_fields(self):
80         return [('utm_campaign', 'campaign_id'), ('utm_source', 'source_id'), ('utm_medium', 'medium_id')]
81
82     def tracking_get_values(self, cr, uid, vals, context=None):
83         for key, field in self.tracking_fields():
84             column = self._all_columns[field].column
85             value = vals.get(field) or (request and request.session.get(key))  # params.get sould be always in session by the dispatch from ir_http
86             if column._type in ['many2one'] and isinstance(value, basestring):  # if we receive a string for a many2one, we search / create  the id
87                 if value:
88                     Model = self.pool[column._obj]
89                     rel_id = Model.name_search(cr, uid, value, context=context)
90                     if not rel_id:
91                         rel_id = Model.create(cr, uid, {'name': value}, context=context)
92                 vals[field] = rel_id
93             # Here the code for other cases that many2one
94             else:
95                 vals[field] = value
96         return vals
97
98     def _get_default_track(self, cr, uid, field, context=None):
99         return self.tracking_get_values(cr, uid, {}, context=context).get(field)
100
101     _defaults = {
102         'source_id': lambda self, cr, uid, ctx: self._get_default_track(cr, uid, 'source_id', ctx),
103         'campaign_id': lambda self, cr, uid, ctx: self._get_default_track(cr, uid, 'campaign_id', ctx),
104         'medium_id': lambda self, cr, uid, ctx: self._get_default_track(cr, uid, 'medium_id', ctx),
105     }
106
107
108 class crm_case_stage(osv.osv):
109     """ Model for case stages. This models the main stages of a document
110         management flow. Main CRM objects (leads, opportunities, project
111         issues, ...) will now use only stages, instead of state and stages.
112         Stages are for example used to display the kanban view of records.
113     """
114     _name = "crm.case.stage"
115     _description = "Stage of case"
116     _rec_name = 'name'
117     _order = "sequence"
118
119     _columns = {
120         'name': fields.char('Stage Name', required=True, translate=True),
121         'sequence': fields.integer('Sequence', help="Used to order stages. Lower is better."),
122         'probability': fields.float('Probability (%)', required=True, help="This percentage depicts the default/average probability of the Case for this stage to be a success"),
123         'on_change': fields.boolean('Change Probability Automatically', help="Setting this stage will change the probability automatically on the opportunity."),
124         'requirements': fields.text('Requirements'),
125         'section_ids': fields.many2many('crm.case.section', 'section_stage_rel', 'stage_id', 'section_id', string='Sections',
126                         help="Link between stages and sales teams. When set, this limitate the current stage to the selected sales teams."),
127         'case_default': fields.boolean('Default to New Sales Team',
128                         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."),
129         'fold': fields.boolean('Folded in Kanban View',
130                                help='This stage is folded in the kanban view when'
131                                'there are no records in that stage to display.'),
132         'type': fields.selection([('lead', 'Lead'),
133                                     ('opportunity', 'Opportunity'),
134                                     ('both', 'Both')],
135                                     string='Type', required=True,
136                                     help="This field is used to distinguish stages related to Leads from stages related to Opportunities, or to specify stages available for both types."),
137     }
138
139     _defaults = {
140         'sequence': 1,
141         'probability': 0.0,
142         'on_change': True,
143         'fold': False,
144         'type': 'both',
145         'case_default': True,
146     }
147
148 class crm_case_categ(osv.osv):
149     """ Category of Case """
150     _name = "crm.case.categ"
151     _description = "Category of Case"
152     _columns = {
153         'name': fields.char('Name', required=True, translate=True),
154         'section_id': fields.many2one('crm.case.section', 'Sales Team'),
155         'object_id': fields.many2one('ir.model', 'Object Name'),
156     }
157     def _find_object_id(self, cr, uid, context=None):
158         """Finds id for case object"""
159         context = context or {}
160         object_id = context.get('object_id', False)
161         ids = self.pool.get('ir.model').search(cr, uid, ['|',('id', '=', object_id),('model', '=', context.get('object_name', False))])
162         return ids and ids[0] or False
163     _defaults = {
164         'object_id' : _find_object_id
165     }
166
167 class crm_payment_mode(osv.osv):
168     """ Payment Mode for Fund """
169     _name = "crm.payment.mode"
170     _description = "CRM Payment Mode"
171     _columns = {
172         'name': fields.char('Name', required=True),
173         'section_id': fields.many2one('crm.case.section', 'Sales Team'),
174     }
175
176
177 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: