-class crm_base(object):
- """ Base utility mixin class for crm objects,
- Object subclassing this should define colums:
- date_open
- date_closed
- user_id
- partner_id
- partner_address_id
- """
- def _get_default_partner_address(self, cr, uid, context=None):
- """Gives id of default address for current user
- :param context: if portal in context is false return false anyway
- """
- if context is None:
- context = {}
- if not context.get('portal'):
- return False
- # was user.address_id.id, but address_id has been removed
- user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
- if hasattr(user, 'partner_address_id') and user.partner_address_id:
- return user.partner_address_id
- return False
-
- def _get_default_partner(self, cr, uid, context=None):
- """Gives id of partner for current user
- :param context: if portal in context is false return false anyway
- """
- 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 hasattr(user, 'partner_address_id') and user.partner_address_id:
- return user.partner_address_id
- return user.company_id.partner_id.id
-
- def _get_default_email(self, cr, uid, context=None):
- """Gives default email address for current user
- :param context: if portal in context is false return false anyway
- """
- if not context.get('portal', False):
- return False
- user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
- return user.user_email
-
- def _get_default_user(self, cr, uid, context=None):
- """Gives current user id
- :param context: if portal in context is false return false anyway
- """
- 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
- """
- 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 ids: List of case IDs
- :param add: Id of Partner's address
- :param email: Partner's email ID
- """
- data = {'value': {'email_from': False, 'phone':False}}
- if add:
- address = self.pool.get('res.partner.address').browse(cr, uid, add)
- data['value'] = {'email_from': address and address.email or False ,
- 'phone': address and address.phone or False}
- if 'phone' not in self._columns:
- del data['value']['phone']
- return data
-
- def onchange_partner_id(self, cr, uid, ids, part, email=False):
- """This function returns value of partner address based on partner
- :param ids: List of case IDs
- :param part: Partner's id
- :param 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 ids: List of case Ids
- """
- 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 ids: List of case Ids
- """
- 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 ids: List of case Ids
- """
- cases = self.browse(cr, uid, ids)
- cases[0].state # to fill the browse record cache
- self.write(cr, uid, ids, {'state': 'cancel', 'active': True})
- # We use the cache of cases to keep the old case state
- self._action(cr, uid, cases, 'cancel')
- return True
-
- def case_pending(self, cr, uid, ids, *args):
- """Marks case as pending
- :param ids: List of case Ids
- """
- 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 ids: List of case Ids
- """
- cases = self.browse(cr, uid, ids)
- cases[0].state # to fill the browse record cache
- 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 stage_find(self, cr, uid, section_id, domain=[], order='sequence'):
- domain = list(domain)
- if section_id:
- domain.append(('section_ids', '=', section_id))
- stage_ids = self.pool.get('crm.case.stage').search(cr, uid, domain, order=order)
- if stage_ids:
- return stage_ids[0]
- return False
-
- def stage_set(self, cr, uid, ids, stage_id, context=None):
- value = {}
- if hasattr(self,'onchange_stage_id'):
- value = self.onchange_stage_id(cr, uid, ids, stage_id)['value']
- value['stage_id'] = stage_id
- return self.write(cr, uid, ids, value, context=context)
-
- def stage_change(self, cr, uid, ids, op, order, context=None):
- if context is None:
- context = {}
- for case in self.browse(cr, uid, ids, context=context):
- seq = 0
- if case.stage_id:
- seq = case.stage_id.sequence
- section_id = None
- if case.section_id:
- section_id = case.section_id.id
- next_stage_id = self.stage_find(cr, uid, section_id, [('sequence',op,seq)],order)
- if next_stage_id:
- return self.stage_set(cr, uid, [case.id], next_stage_id, context=context)
- return False
-
- def stage_next(self, cr, uid, ids, context=None):
- """This function computes next stage for case from its current stage
- using available stage for that case type
- """
- return self.stage_change(cr, uid, ids, '>','sequence', context)
-
- def stage_previous(self, cr, uid, ids, context=None):
- """This function computes previous stage for case from its current
- stage using available stage for that case type
- """
- return self.stage_change(cr, uid, ids, '<', 'sequence desc', context)
-
- def copy(self, cr, uid, id, default=None, context=None):
- """Overrides orm copy method to avoid copying messages,
- as well as date_closed and date_open columns if they
- exist."""
- if default is None:
- default = {}
-
- default.update({ 'message_ids': [], })
- if hasattr(self, '_columns'):
- if self._columns.get('date_closed'):
- default.update({ 'date_closed': False, })
- if self._columns.get('date_open'):
- default.update({ 'date_open': False })
- return super(crm_case, self).copy(cr, uid, id, default, context=context)
-
-
- def case_open(self, cr, uid, ids, *args):
- """Opens Case"""
- 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"""
- 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_escalate(self, cr, uid, ids, *args):
- """Escalates case to parent level"""
- cases = self.browse(cr, uid, ids)
- for case in cases:
- data = {'active': True}
- if case.section_id.parent_id:
- data['section_id'] = case.section_id.parent_id.id
- if case.section_id.parent_id.change_responsible:
- if case.section_id.parent_id.user_id:
- data['user_id'] = case.section_id.parent_id.user_id.id
- else:
- raise osv.except_osv(_('Error !'), _('You can not escalate, you are already at the top level regarding your sales-team category.'))
- self.write(cr, uid, [case.id], data)
- cases = self.browse(cr, uid, ids)
- self._action(cr, uid, cases, 'escalate')
- return True
-
- def case_cancel(self, cr, uid, ids, *args):
- """Cancels Case"""
- 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"""
- 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"""
- state = 'draft'
- if 'crm.phonecall' in args:
- state = 'open'
- cases = self.browse(cr, uid, ids)
- cases[0].state # to fill the browse record cache
- self.write(cr, uid, ids, {'state': state, 'active': True})
- self._action(cr, uid, cases, state)
- return True
-
- def remind_partner(self, cr, uid, ids, context=None, attach=False):
- return self.remind_user(cr, uid, ids, context, attach,
- destination=False)
-
- def remind_user(self, cr, uid, ids, context=None, attach=False, destination=True):
- mail_message = self.pool.get('mail.message')
- for case in self.browse(cr, uid, ids, context=context):
- if not destination and not case.email_from:
- return False
- if not case.user_id.user_email:
- return False
- if destination and case.section_id.user_id:
- case_email = case.section_id.user_id.user_email
- else:
- case_email = case.user_id.user_email
-
- src = case_email
- dest = case.user_id.user_email or ""
- body = case.description or ""
- for message in case.message_ids:
- if message.email_from and message.body_text:
- body = message.body_text
- break
-
- if not destination:
- src, dest = dest, case.email_from
- if body and case.user_id.signature:
- if body:
- body += '\n\n%s' % (case.user_id.signature)
- else:
- body = '\n\n%s' % (case.user_id.signature)
-
- body = self.format_body(body)
-
- attach_to_send = {}
-
- if attach:
- attach_ids = self.pool.get('ir.attachment').search(cr, uid, [('res_model', '=', self._name), ('res_id', '=', case.id)])
- attach_to_send = self.pool.get('ir.attachment').read(cr, uid, attach_ids, ['datas_fname', 'datas'])
- attach_to_send = dict(map(lambda x: (x['datas_fname'], base64.decodestring(x['datas'])), attach_to_send))
-
- # Send an email
- subject = "Reminder: [%s] %s" % (str(case.id), case.name, )
- mail_message.schedule_with_attach(cr, uid,
- src,
- [dest],
- subject,
- body,
- model=self._name,
- reply_to=case.section_id.reply_to,
- res_id=case.id,
- attachments=attach_to_send,
- context=context
- )
- return True
-
- def _check(self, cr, uid, ids=False, context=None):
- """Function called by the scheduler to process cases for date actions
- Only works on not done and cancelled cases
- """
- cr.execute('select * from crm_case \
- where (date_action_last<%s or date_action_last is null) \
- and (date_action_next<=%s or date_action_next is null) \
- and state not in (\'cancel\',\'done\')',
- (time.strftime("%Y-%m-%d %H:%M:%S"),
- time.strftime('%Y-%m-%d %H:%M:%S')))
-
- ids2 = map(lambda x: x[0], cr.fetchall() or [])
- cases = self.browse(cr, uid, ids2, context=context)
- return self._action(cr, uid, cases, False, context=context)
-
- def format_body(self, body):
- return self.pool.get('base.action.rule').format_body(body)
-
- def format_mail(self, obj, body):
- return self.pool.get('base.action.rule').format_mail(obj, body)
-
- def message_thread_followers(self, cr, uid, ids, context=None):
- res = {}
- for case in self.browse(cr, uid, ids, context=context):
- l=[]
- if case.email_cc:
- l.append(case.email_cc)
- if case.user_id and case.user_id.user_email:
- l.append(case.user_id.user_email)
- res[case.id] = l
- return res