[IMP] CSS of etherpad
[odoo/odoo.git] / addons / crm / crm.py
index 08d26b6..00936ba 100644 (file)
@@ -21,6 +21,7 @@
 
 import base64
 import time
+from lxml import etree
 from osv import fields
 from osv import osv
 import tools
@@ -57,7 +58,7 @@ class crm_case_channel(osv.osv):
 
 class crm_case_stage(osv.osv):
     """ Model for case stages. This models the main stages of a document
-        management flow. Main CRM objects (leads, opportunities, project 
+        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.
     """
@@ -74,7 +75,9 @@ class crm_case_stage(osv.osv):
         'requirements': fields.text('Requirements'),
         'section_ids':fields.many2many('crm.case.section', 'section_stage_rel', 'stage_id', 'section_id', string='Sections',
                         help="Link between stages and sales teams. When set, this limitate the current stage to the selected sales teams."),
-        'state': fields.selection(AVAILABLE_STATES, 'State', required=True, help="The related state for the stage. The state of your document will automatically change regarding the selected stage. For example, if a stage is related to the state 'Close', when your document reaches this stage, it will be automatically have the 'closed' state."),
+        '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('Common to All Teams',
                         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('Hide in Views when Empty',
@@ -89,14 +92,17 @@ class crm_case_stage(osv.osv):
     _defaults = {
         'sequence': lambda *args: 1,
         'probability': lambda *args: 0.0,
-        'state': 'draft',
+        'state': 'open',
         '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"
 
@@ -109,7 +115,6 @@ class crm_case_section(osv.osv):
         '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."),
-        'allow_unlink': fields.boolean('Allow Delete', help="Allows to delete non draft cases"),
         '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'),
@@ -120,16 +125,19 @@ class crm_case_section(osv.osv):
         '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."),
     }
-    
+
     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': lambda *a: 1,
-        'allow_unlink': lambda *a: 1,
-        'stage_ids': _get_stage_common
+        'active': 1,
+        'stage_ids': _get_stage_common,
+        'alias_domain': False, # always hide alias during creation
     }
 
     _sql_constraints = [
@@ -156,6 +164,27 @@ class crm_case_section(osv.osv):
             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"
@@ -168,8 +197,9 @@ class crm_case_categ(osv.osv):
 
     def _find_object_id(self, cr, uid, context=None):
         """Finds id for case object"""
-        object_id = context and context.get('object_id', False) or False
-        ids = self.pool.get('ir.model').search(cr, uid, [('id', '=', object_id)])
+        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 = {
@@ -186,7 +216,6 @@ class crm_case_resource_type(osv.osv):
         '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')
@@ -199,7 +228,7 @@ class crm_payment_mode(osv.osv):
     _name = "crm.payment.mode"
     _description = "CRM Payment Mode"
     _columns = {
-        'name': fields.char('Payment Mode', size=64, required=True),
+        'name': fields.char('Name', size=64, required=True),
         'section_id': fields.many2one('crm.case.section', 'Sales Team'),
     }