[MERGE] forward port of branch 8.0 up to 591e329
[odoo/odoo.git] / addons / crm / crm.py
index 714a2bc..45f84b8 100644 (file)
 #
 ##############################################################################
 
-import base64
-import time
-from lxml import etree
-from openerp.osv import fields
-from openerp.osv import osv
-from openerp import tools
-from openerp.tools.translate import _
-
-MAX_LEVEL = 15
-AVAILABLE_STATES = [
-    ('draft', 'New'),
-    ('cancel', 'Cancelled'),
-    ('open', 'In Progress'),
-    ('pending', 'Pending'),
-    ('done', 'Closed')
-]
+from openerp.osv import osv, fields
+from openerp.http import request
 
 AVAILABLE_PRIORITIES = [
-    ('1', 'Highest'),
+    ('0', 'Normal'),
+    ('1', 'Low'),
     ('2', 'High'),
-    ('3', 'Normal'),
-    ('4', 'Low'),
-    ('5', 'Lowest'),
+    ('3', 'Very High'),
 ]
 
-class crm_case_channel(osv.osv):
-    _name = "crm.case.channel"
+
+class crm_tracking_medium(osv.Model):
+    # OLD crm.case.channel
+    _name = "crm.tracking.medium"
     _description = "Channels"
     _order = 'name'
     _columns = {
-        'name': fields.char('Channel Name', size=64, required=True),
+        'name': fields.char('Channel Name', required=True),
         'active': fields.boolean('Active'),
     }
     _defaults = {
         'active': lambda *a: 1,
     }
 
-class crm_case_stage(osv.osv):
+
+class crm_tracking_campaign(osv.Model):
+    # OLD crm.case.resource.type
+    _name = "crm.tracking.campaign"
+    _description = "Campaign"
+    _rec_name = "name"
+    _columns = {
+        'name': fields.char('Campaign Name', required=True, translate=True),
+        'team_id': fields.many2one('crm.team', 'Sales Team', oldname='section_id'),
+    }
+
+
+class crm_tracking_source(osv.Model):
+    _name = "crm.tracking.source"
+    _description = "Source"
+    _rec_name = "name"
+    _columns = {
+        'name': fields.char('Source Name', required=True, translate=True),
+    }
+
+
+class crm_tracking_mixin(osv.AbstractModel):
+    """Mixin class for objects which can be tracked by marketing. """
+    _name = 'crm.tracking.mixin'
+
+    _columns = {
+        'campaign_id': fields.many2one('crm.tracking.campaign', 'Campaign',  # old domain ="['|',('team_id','=',team_id),('team_id','=',False)]"
+                                       help="This is a name that helps you keep track of your different campaign efforts Ex: Fall_Drive, Christmas_Special"),
+        '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"),
+        'medium_id': fields.many2one('crm.tracking.medium', 'Channel', help="This is the method of delivery. Ex: Postcard, Email, or Banner Ad", oldname='channel_id'),
+    }
+
+    def tracking_fields(self):
+        return [('utm_campaign', 'campaign_id'), ('utm_source', 'source_id'), ('utm_medium', 'medium_id')]
+
+    def tracking_get_values(self, cr, uid, vals, context=None):
+        for key, fname in self.tracking_fields():
+            field = self._fields[fname]
+            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
+            if field.type == 'many2one' and isinstance(value, basestring):
+                # if we receive a string for a many2one, we search/create the id
+                if value:
+                    Model = self.pool[field.comodel_name]
+                    rel_id = Model.name_search(cr, uid, value, context=context)
+                    if rel_id:
+                        rel_id = rel_id[0][0]
+                    else:
+                        rel_id = Model.create(cr, uid, {'name': value}, context=context)
+                vals[fname] = rel_id
+            else:
+                # Here the code for others cases that many2one
+                vals[fname] = value
+        return vals
+
+    def _get_default_track(self, cr, uid, field, context=None):
+        return self.tracking_get_values(cr, uid, {}, context=context).get(field)
+
+    _defaults = {
+        'source_id': lambda self, cr, uid, ctx: self._get_default_track(cr, uid, 'source_id', ctx),
+        'campaign_id': lambda self, cr, uid, ctx: self._get_default_track(cr, uid, 'campaign_id', ctx),
+        'medium_id': lambda self, cr, uid, ctx: self._get_default_track(cr, uid, 'medium_id', ctx),
+    }
+
+
+class crm_stage(osv.Model):
     """ Model for case stages. This models the main stages of a document
         management flow. Main CRM objects (leads, opportunities, project
         issues, ...) will now use only stages, instead of state and stages.
         Stages are for example used to display the kanban view of records.
     """
-    _name = "crm.case.stage"
+    _name = "crm.stage"
     _description = "Stage of case"
     _rec_name = 'name'
     _order = "sequence"
 
     _columns = {
-        'name': fields.char('Stage Name', size=64, required=True, translate=True),
+        'name': fields.char('Stage Name', required=True, translate=True),
         'sequence': fields.integer('Sequence', help="Used to order stages. Lower is better."),
         'probability': fields.float('Probability (%)', required=True, help="This percentage depicts the default/average probability of the Case for this stage to be a success"),
         'on_change': fields.boolean('Change Probability Automatically', help="Setting this stage will change the probability automatically on the opportunity."),
         'requirements': fields.text('Requirements'),
-        'section_ids':fields.many2many('crm.case.section', 'section_stage_rel', 'stage_id', 'section_id', string='Sections',
+        'team_ids': fields.many2many('crm.team', 'crm_team_stage_rel', 'stage_id', 'team_id', string='Teams',
                         help="Link between stages and sales teams. When set, this limitate the current stage to the selected sales teams."),
-        'state': fields.selection(AVAILABLE_STATES, 'Related Status', required=True,
-            help="The status of your document will automatically change regarding the selected stage. " \
-                "For example, if a stage is related to the status 'Close', when your document reaches this stage, it is automatically closed."),
         'case_default': fields.boolean('Default to New Sales Team',
                         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."),
-        'fold': fields.boolean('Fold by Default',
-                        help="This stage is not visible, for example in status bar or kanban view, when there are no records in that stage to display."),
-        'type': fields.selection([  ('lead','Lead'),
-                                    ('opportunity', 'Opportunity'),
-                                    ('both', 'Both')],
-                                    string='Type', size=16, required=True,
-                                    help="This field is used to distinguish stages related to Leads from stages related to Opportunities, or to specify stages available for both types."),
+        'legend_priority': fields.text(
+            'Priority Management Explanation', translate=True,
+            help='Explanation text to help users using the star and priority mechanism on stages or issues that are in this stage.'),
+        'fold': fields.boolean('Folded in Kanban View',
+                               help='This stage is folded in the kanban view when'
+                               'there are no records in that stage to display.'),
+        'type': fields.selection([('lead', 'Lead'), ('opportunity', 'Opportunity'), ('both', 'Both')],
+                                 string='Type', required=True,
+                                 help="This field is used to distinguish stages related to Leads from stages related to Opportunities, or to specify stages available for both types."),
     }
 
     _defaults = {
-        'sequence': lambda *args: 1,
-        'probability': lambda *args: 0.0,
-        'state': 'open',
+        'sequence': 1,
+        'probability': 0.0,
+        'on_change': True,
         'fold': False,
         'type': 'both',
         'case_default': True,
     }
 
-class crm_case_section(osv.osv):
-    """ Model for sales teams. """
-    _name = "crm.case.section"
-    _inherits = {'mail.alias': 'alias_id'}
-    _inherit = "mail.thread"
-    _description = "Sales Teams"
-    _order = "complete_name"
-
-    def get_full_name(self, cr, uid, ids, field_name, arg, context=None):
-        return  dict(self.name_get(cr, uid, ids, context=context))
-
-    _columns = {
-        'name': fields.char('Sales Team', size=64, required=True, translate=True),
-        'complete_name': fields.function(get_full_name, type='char', size=256, readonly=True, store=True),
-        'code': fields.char('Code', size=8),
-        'active': fields.boolean('Active', help="If the active field is set to "\
-                        "true, it will allow you to hide the sales team without removing it."),
-        'change_responsible': fields.boolean('Reassign Escalated', help="When escalating to this team override the salesman with the team leader."),
-        'user_id': fields.many2one('res.users', 'Team Leader'),
-        'member_ids': fields.many2many('res.users', 'sale_member_rel', 'section_id', 'member_id', 'Team Members'),
-        'reply_to': fields.char('Reply-To', size=64, help="The email address put in the 'Reply-To' of all emails sent by OpenERP about cases in this sales team"),
-        'parent_id': fields.many2one('crm.case.section', 'Parent Team'),
-        'child_ids': fields.one2many('crm.case.section', 'parent_id', 'Child Teams'),
-        'resource_calendar_id': fields.many2one('resource.calendar', "Working Time", help="Used to compute open days"),
-        'note': fields.text('Description'),
-        'working_hours': fields.float('Working Hours', digits=(16, 2)),
-        'stage_ids': fields.many2many('crm.case.stage', 'section_stage_rel', 'section_id', 'stage_id', 'Stages'),
-        'alias_id': fields.many2one('mail.alias', 'Alias', ondelete="cascade", required=True,
-                                    help="The email address associated with this team. New emails received will automatically "
-                                         "create new leads assigned to the team."),
-        'open_lead_ids': fields.one2many('crm.lead', 'section_id', 'Open Leads', readonly=True,
-            domain=['&', ('type', '!=', 'opportunity'), ('state', 'not in', ['done', 'cancel'])]),
-        'open_opportunity_ids': fields.one2many('crm.lead', 'section_id', 'Open Opportunities', readonly=True,
-            domain=['&', '|', ('type', '=', 'opportunity'), ('type', '=', 'both'), ('state', 'not in', ['done', 'cancel'])]),
-        'color': fields.integer('Color Index'),
-        'use_leads': fields.boolean('Leads',
-            help="If checked, this sales team will be available in the sales teams menu and you will be able to manage leads"),
-    }
-
-    def _get_stage_common(self, cr, uid, context):
-        ids = self.pool.get('crm.case.stage').search(cr, uid, [('case_default','=',1)], context=context)
-        return ids
-
-    _defaults = {
-        'active': 1,
-        'stage_ids': _get_stage_common,
-        'use_leads': True,
-    }
-
-    _sql_constraints = [
-        ('code_uniq', 'unique (code)', 'The code of the sales team must be unique !')
-    ]
-
-    _constraints = [
-        (osv.osv._check_recursion, 'Error ! You cannot create recursive Sales team.', ['parent_id'])
-    ]
-
-    def name_get(self, cr, uid, ids, context=None):
-        """Overrides orm name_get method"""
-        if not isinstance(ids, list) :
-            ids = [ids]
-        res = []
-        if not ids:
-            return res
-        reads = self.read(cr, uid, ids, ['name', 'parent_id'], context)
-
-        for record in reads:
-            name = record['name']
-            if record['parent_id']:
-                name = record['parent_id'][1] + ' / ' + name
-            res.append((record['id'], name))
-        return res
-
-    def create(self, cr, uid, vals, context=None):
-        mail_alias = self.pool.get('mail.alias')
-        if not vals.get('alias_id'):
-            vals.pop('alias_name', None) # prevent errors during copy()
-            alias_id = mail_alias.create_unique_alias(cr, uid,
-                    {'alias_name': vals['name']},
-                    model_name="crm.lead",
-                    context=context)
-            vals['alias_id'] = alias_id
-        res = super(crm_case_section, self).create(cr, uid, vals, context)
-        mail_alias.write(cr, uid, [vals['alias_id']], {'alias_defaults': {'section_id': res, 'type':'lead'}}, context)
-        return res
-
-    def unlink(self, cr, uid, ids, context=None):
-        # Cascade-delete mail aliases as well, as they should not exist without the sales team.
-        mail_alias = self.pool.get('mail.alias')
-        alias_ids = [team.alias_id.id for team in self.browse(cr, uid, ids, context=context) if team.alias_id ]
-        res = super(crm_case_section, self).unlink(cr, uid, ids, context=context)
-        mail_alias.unlink(cr, uid, alias_ids, context=context)
-        return res
-
-class crm_case_categ(osv.osv):
-    """ Category of Case """
-    _name = "crm.case.categ"
-    _description = "Category of Case"
-    _columns = {
-        'name': fields.char('Name', size=64, required=True, translate=True),
-        'section_id': fields.many2one('crm.case.section', 'Sales Team'),
-        'object_id': fields.many2one('ir.model', 'Object Name'),
-    }
-    def _find_object_id(self, cr, uid, context=None):
-        """Finds id for case object"""
-        context = context or {}
-        object_id = context.get('object_id', False)
-        ids = self.pool.get('ir.model').search(cr, uid, ['|',('id', '=', object_id),('model', '=', context.get('object_name', False))])
-        return ids and ids[0] or False
-    _defaults = {
-        'object_id' : _find_object_id
-    }
-
-class crm_case_resource_type(osv.osv):
-    """ Resource Type of case """
-    _name = "crm.case.resource.type"
-    _description = "Campaign"
-    _rec_name = "name"
-    _columns = {
-        'name': fields.char('Campaign Name', size=64, required=True, translate=True),
-        'section_id': fields.many2one('crm.case.section', 'Sales Team'),
-    }
-
-def _links_get(self, cr, uid, context=None):
-    """Gets links value for reference field"""
-    obj = self.pool.get('res.request.link')
-    ids = obj.search(cr, uid, [])
-    res = obj.read(cr, uid, ids, ['object', 'name'], context)
-    return [(r['object'], r['name']) for r in res]
-
-class crm_payment_mode(osv.osv):
-    """ Payment Mode for Fund """
-    _name = "crm.payment.mode"
-    _description = "CRM Payment Mode"
-    _columns = {
-        'name': fields.char('Name', size=64, required=True),
-        'section_id': fields.many2one('crm.case.section', 'Sales Team'),
-    }
-
-
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: