[MERGE] trunk
[odoo/odoo.git] / addons / crm / crm.py
index fd9b778..3992770 100644 (file)
@@ -44,8 +44,221 @@ AVAILABLE_PRIORITIES = [
     ('5', 'Lowest'),
 ]
 
-class crm_case(object):
-    """A simple python class to be used for common functions """
+class crm_base(object):
+    """
+        Base classe for crm object,
+        Object that inherit from this class should have
+            date_open
+            date_closed
+            user_id
+            partner_id
+            partner_address_id
+        as field to be compatible with this class
+        
+    """
+    def _get_default_partner_address(self, cr, uid, context=None):
+
+        """Gives id of default address for current user
+        @param self: The object pointer
+        @param cr: the current row, from the database cursor,
+        @param uid: the current user’s ID for security checks,
+        @param context: A standard dictionary for contextual values
+        """
+        if context is None:
+            context = {}
+        if not context.get('portal', False):
+            return False
+        return self.pool.get('res.users').browse(cr, uid, uid, context).address_id.id
+
+    def _get_default_partner(self, cr, uid, context=None):
+        """Gives id of partner for current user
+        @param self: The object pointer
+        @param cr: the current row, from the database cursor,
+        @param uid: the current user’s ID for security checks,
+        @param context: A standard dictionary for contextual values
+        """
+        if context is None:
+            context = {}
+        if not context.get('portal', False):
+            return False
+        user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
+        if not user.address_id:
+            return False
+        return user.address_id.partner_id.id
+    
+    def _get_default_email(self, cr, uid, context=None):
+        """Gives default email address for current user
+        @param self: The object pointer
+        @param cr: the current row, from the database cursor,
+        @param uid: the current user’s ID for security checks,
+        @param context: A standard dictionary for contextual values
+        """
+        if not context.get('portal', False):
+            return False
+        user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
+        if not user.address_id:
+            return False
+        return user.address_id.email
+    
+    def _get_default_user(self, cr, uid, context=None):
+        """Gives current user id
+        @param self: The object pointer
+        @param cr: the current row, from the database cursor,
+        @param uid: the current user’s ID for security checks,
+        @param context: A standard dictionary for contextual values
+        """
+        if context and context.get('portal', False):
+            return False
+        return uid
+
+    def _get_section(self, cr, uid, context=None):
+        """Gives section id for current User
+        @param self: The object pointer
+        @param cr: the current row, from the database cursor,
+        @param uid: the current user’s ID for security checks,
+        @param context: A standard dictionary for contextual values
+        """
+        user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
+        return user.context_section_id.id or False
+    
+    def onchange_partner_address_id(self, cr, uid, ids, add, email=False):
+        """This function returns value of partner email based on Partner Address
+        @param self: The object pointer
+        @param cr: the current row, from the database cursor,
+        @param uid: the current user’s ID for security checks,
+        @param ids: List of case IDs
+        @param add: Id of Partner's address
+        @email: Partner's email ID
+        """
+        if not add:
+            return {'value': {'email_from': False}}
+        address = self.pool.get('res.partner.address').browse(cr, uid, add)
+        if address.email:
+            return {'value': {'email_from': address.email, 'phone': address.phone}}
+        else:
+            return {'value': {'phone': address.phone}}
+        
+    def onchange_partner_id(self, cr, uid, ids, part, email=False):
+        """This function returns value of partner address based on partner
+        @param self: The object pointer
+        @param cr: the current row, from the database cursor,
+        @param uid: the current user’s ID for security checks,
+        @param ids: List of case IDs
+        @param part: Partner's id
+        @email: Partner's email ID
+        """
+        data={}
+        if  part:
+            addr = self.pool.get('res.partner').address_get(cr, uid, [part], ['contact'])
+            data = {'partner_address_id': addr['contact']}
+            data.update(self.onchange_partner_address_id(cr, uid, ids, addr['contact'])['value'])
+        return {'value': data}
+    
+    
+    def case_open(self, cr, uid, ids, *args):
+        """Opens Case
+        @param self: The object pointer
+        @param cr: the current row, from the database cursor,
+        @param uid: the current user’s ID for security checks,
+        @param ids: List of case Ids
+        @param *args: Tuple Value for additional Params
+        """
+
+        cases = self.browse(cr, uid, ids)
+        for case in cases:
+            data = {'state': 'open', 'active': True}
+            if not case.user_id:
+                data['user_id'] = uid
+            self.write(cr, uid, case.id, data)
+
+
+        self._action(cr, uid, cases, 'open')
+        return True
+
+    def case_close(self, cr, uid, ids, *args):
+        """Closes Case
+        @param self: The object pointer
+        @param cr: the current row, from the database cursor,
+        @param uid: the current user’s ID for security checks,
+        @param ids: List of case Ids
+        @param *args: Tuple Value for additional Params
+        """
+        cases = self.browse(cr, uid, ids)
+        cases[0].state # to fill the browse record cache
+        self.write(cr, uid, ids, {'state': 'done',
+                                  'date_closed': time.strftime('%Y-%m-%d %H:%M:%S'),
+                                  })
+        #
+        # We use the cache of cases to keep the old case state
+        #
+        self._action(cr, uid, cases, 'done')
+        return True
+
+    def case_cancel(self, cr, uid, ids, *args):
+        """Cancels Case
+        @param self: The object pointer
+        @param cr: the current row, from the database cursor,
+        @param uid: the current user’s ID for security checks,
+        @param ids: List of case Ids
+        @param *args: Tuple Value for additional Params
+        """
+        cases = self.browse(cr, uid, ids)
+        cases[0].state # to fill the browse record cache
+        self.write(cr, uid, ids, {'state': 'cancel',
+                                  'active': True})
+        self._action(cr, uid, cases, 'cancel')
+        for case in cases:
+            message = _("The case '%s' has been cancelled.") % (case.name,)
+            self.log(cr, uid, case.id, message)
+        return True
+
+    def case_pending(self, cr, uid, ids, *args):
+        """Marks case as pending
+        @param self: The object pointer
+        @param cr: the current row, from the database cursor,
+        @param uid: the current user’s ID for security checks,
+        @param ids: List of case Ids
+        @param *args: Tuple Value for additional Params
+        """
+        cases = self.browse(cr, uid, ids)
+        cases[0].state # to fill the browse record cache
+        self.write(cr, uid, ids, {'state': 'pending', 'active': True})
+        self._action(cr, uid, cases, 'pending')
+        return True
+
+    def case_reset(self, cr, uid, ids, *args):
+        """Resets case as draft
+        @param self: The object pointer
+        @param cr: the current row, from the database cursor,
+        @param uid: the current user’s ID for security checks,
+        @param ids: List of case Ids
+        @param *args: Tuple Value for additional Params
+        """
+        cases = self.browse(cr, uid, ids)
+        cases[0].state # to fill the browse record cache
+        self._history(cr, uid, cases, _('Draft'))
+        self.write(cr, uid, ids, {'state': 'draft', 'active': True})
+        self._action(cr, uid, cases, 'draft')
+        return True
+    
+    def _action(self, cr, uid, cases, state_to, scrit=None, context=None):
+        if context is None:
+            context = {}
+        context['state_to'] = state_to
+        rule_obj = self.pool.get('base.action.rule')
+        model_obj = self.pool.get('ir.model')
+        model_ids = model_obj.search(cr, uid, [('model','=',self._name)])
+        rule_ids = rule_obj.search(cr, uid, [('model_id','=',model_ids[0])])
+        return rule_obj._action(cr, uid, rule_ids, cases, scrit=scrit, context=context)
+
+class crm_case(crm_base):
+    """
+        A simple python class to be used for common functions 
+        Object that inherit from this class should inherit from mailgate.thread
+        And need a stage_id field
+        
+        And object that inherit (orm inheritance) from a class the overwrite copy 
+    """
 
     def _find_lost_stage(self, cr, uid, type, section_id):
         return self._find_percent_stage(cr, uid, 0.0, type, section_id)
@@ -105,35 +318,7 @@ class crm_case(object):
             return {'value':{}}
         return {'value':{'probability': stage.probability}}
 
-    def _get_default_partner_address(self, cr, uid, context=None):
-
-        """Gives id of default address for current user
-        @param self: The object pointer
-        @param cr: the current row, from the database cursor,
-        @param uid: the current user’s ID for security checks,
-        @param context: A standard dictionary for contextual values
-        """
-        if context is None:
-            context = {}
-        if not context.get('portal', False):
-            return False
-        return self.pool.get('res.users').browse(cr, uid, uid, context).address_id.id
-
-    def _get_default_partner(self, cr, uid, context=None):
-        """Gives id of partner for current user
-        @param self: The object pointer
-        @param cr: the current row, from the database cursor,
-        @param uid: the current user’s ID for security checks,
-        @param context: A standard dictionary for contextual values
-        """
-        if context is None:
-            context = {}
-        if not context.get('portal', False):
-            return False
-        user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
-        if not user.address_id:
-            return False
-        return user.address_id.partner_id.id
+    
 
     def copy(self, cr, uid, id, default=None, context=None):
         """
@@ -145,6 +330,7 @@ class crm_case(object):
         @param default: Dictionary of default values for copy.
         @param context: A standard dictionary for contextual values
         """
+        
         if context is None:
             context = {}
         if default is None:
@@ -164,40 +350,9 @@ class crm_case(object):
                 })
         return super(osv.osv, self).copy(cr, uid, id, default, context=context)
 
-    def _get_default_email(self, cr, uid, context=None):
-        """Gives default email address for current user
-        @param self: The object pointer
-        @param cr: the current row, from the database cursor,
-        @param uid: the current user’s ID for security checks,
-        @param context: A standard dictionary for contextual values
-        """
-        if not context.get('portal', False):
-            return False
-        user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
-        if not user.address_id:
-            return False
-        return user.address_id.email
+    
 
-    def _get_default_user(self, cr, uid, context=None):
-        """Gives current user id
-        @param self: The object pointer
-        @param cr: the current row, from the database cursor,
-        @param uid: the current user’s ID for security checks,
-        @param context: A standard dictionary for contextual values
-        """
-        if context and context.get('portal', False):
-            return False
-        return uid
-
-    def _get_section(self, cr, uid, context=None):
-        """Gives section id for current User
-        @param self: The object pointer
-        @param cr: the current row, from the database cursor,
-        @param uid: the current user’s ID for security checks,
-        @param context: A standard dictionary for contextual values
-        """
-        user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
-        return user.context_section_id.id or False
+    
 
     def _find_next_stage(self, cr, uid, stage_list, index, current_seq, stage_pool, context=None):
         if index + 1 == len(stage_list):
@@ -274,38 +429,9 @@ class crm_case(object):
         @param context: A standard dictionary for contextual values"""
         return self.stage_change(cr, uid, ids, context=context, order='sequence desc')
 
-    def onchange_partner_id(self, cr, uid, ids, part, email=False):
-        """This function returns value of partner address based on partner
-        @param self: The object pointer
-        @param cr: the current row, from the database cursor,
-        @param uid: the current user’s ID for security checks,
-        @param ids: List of case IDs
-        @param part: Partner's id
-        @email: Partner's email ID
-        """
-        data={}
-        if  part:
-            addr = self.pool.get('res.partner').address_get(cr, uid, [part], ['contact'])
-            data = {'partner_address_id': addr['contact']}
-            data.update(self.onchange_partner_address_id(cr, uid, ids, addr['contact'])['value'])
-        return {'value': data}
+    
 
-    def onchange_partner_address_id(self, cr, uid, ids, add, email=False):
-        """This function returns value of partner email based on Partner Address
-        @param self: The object pointer
-        @param cr: the current row, from the database cursor,
-        @param uid: the current user’s ID for security checks,
-        @param ids: List of case IDs
-        @param add: Id of Partner's address
-        @email: Partner's email ID
-        """
-        if not add:
-            return {'value': {'email_from': False}}
-        address = self.pool.get('res.partner.address').browse(cr, uid, add)
-        if address.email:
-            return {'value': {'email_from': address.email, 'phone': address.phone}}
-        else:
-            return {'value': {'phone': address.phone}}
+    
 
     def _history(self, cr, uid, cases, keyword, history=False, subject=None, email=False, details=None, email_from=False, message_id=False, attach=[], context=None):
         mailgate_pool = self.pool.get('mailgate.thread')
@@ -522,15 +648,7 @@ class crm_case(object):
         cases = self.browse(cr, uid, ids2, context=context)
         return self._action(cr, uid, cases, False, context=context)
 
-    def _action(self, cr, uid, cases, state_to, scrit=None, context=None):
-        if context is None:
-            context = {}
-        context['state_to'] = state_to
-        rule_obj = self.pool.get('base.action.rule')
-        model_obj = self.pool.get('ir.model')
-        model_ids = model_obj.search(cr, uid, [('model','=',self._name)])
-        rule_ids = rule_obj.search(cr, uid, [('model_id','=',model_ids[0])])
-        return rule_obj._action(cr, uid, rule_ids, cases, scrit=scrit, context=context)
+    
 
     def format_body(self, body):
         return self.pool.get('base.action.rule').format_body(body)