2009-10-13 05:58:37 +00:00
# -*- coding: utf-8 -*-
2006-12-07 13:41:40 +00:00
##############################################################################
2009-12-01 07:19:53 +00:00
#
2009-02-04 09:46:57 +00:00
# OpenERP, Open Source Management Solution
2010-01-12 09:18:39 +00:00
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
2008-06-16 11:00:21 +00:00
#
2008-11-03 19:18:56 +00:00
# This program is free software: you can redistribute it and/or modify
2009-10-14 11:15:34 +00:00
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
2006-12-07 13:41:40 +00:00
#
2008-11-03 19:18:56 +00:00
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2009-10-14 11:15:34 +00:00
# GNU Affero General Public License for more details.
2006-12-07 13:41:40 +00:00
#
2009-10-14 11:15:34 +00:00
# You should have received a copy of the GNU Affero General Public License
2009-12-01 07:19:53 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2006-12-07 13:41:40 +00:00
#
##############################################################################
import time
2009-09-24 10:46:21 +00:00
import re
import os
2006-12-07 13:41:40 +00:00
import mx . DateTime
2007-04-12 20:08:18 +00:00
import base64
2009-09-24 10:46:21 +00:00
2008-07-08 08:13:12 +00:00
from tools . translate import _
2006-12-07 13:41:40 +00:00
2009-09-24 10:46:21 +00:00
import tools
from osv import fields , osv , orm
from osv . orm import except_orm
2006-12-07 13:41:40 +00:00
MAX_LEVEL = 15
AVAILABLE_STATES = [
2008-07-22 15:11:28 +00:00
( ' draft ' , ' Draft ' ) ,
( ' open ' , ' Open ' ) ,
2009-10-29 06:16:57 +00:00
( ' cancel ' , ' Cancelled ' ) ,
( ' done ' , ' Closed ' ) ,
2008-07-22 15:11:28 +00:00
( ' pending ' , ' Pending ' )
2006-12-07 13:41:40 +00:00
]
AVAILABLE_PRIORITIES = [
2008-07-22 15:11:28 +00:00
( ' 5 ' , ' Lowest ' ) ,
( ' 4 ' , ' Low ' ) ,
( ' 3 ' , ' Normal ' ) ,
( ' 2 ' , ' High ' ) ,
( ' 1 ' , ' Highest ' )
2006-12-07 13:41:40 +00:00
]
2007-12-17 20:16:01 +00:00
icon_lst = {
2008-07-22 15:11:28 +00:00
' form ' : ' STOCK_NEW ' ,
' tree ' : ' STOCK_JUSTIFY_FILL ' ,
' calendar ' : ' STOCK_SELECT_COLOR '
2007-12-17 20:16:01 +00:00
}
2006-12-07 13:41:40 +00:00
class crm_case_section ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = " crm.case.section "
_description = " Case Section "
2010-01-09 14:30:02 +00:00
_order = " name "
2008-07-22 15:11:28 +00:00
_columns = {
2008-11-20 16:18:41 +00:00
' name ' : fields . char ( ' Case Section ' , size = 64 , required = True , translate = True ) ,
2008-07-22 15:11:28 +00:00
' code ' : fields . char ( ' Section Code ' , size = 8 ) ,
2009-12-21 12:21:32 +00:00
' active ' : fields . boolean ( ' Active ' , help = " If the active field is set to true, it will allow you to hide the case section without removing it. " ) ,
2009-03-11 08:04:07 +00:00
' allow_unlink ' : fields . boolean ( ' Allow Delete ' , help = " Allows to delete non draft cases " ) ,
2009-01-27 11:15:46 +00:00
' user_id ' : fields . many2one ( ' res.users ' , ' Responsible User ' ) ,
' reply_to ' : fields . char ( ' Reply-To ' , size = 64 , help = " The email address put in the ' Reply-To ' of all emails sent by Open ERP about cases in this section " ) ,
2008-07-22 15:11:28 +00:00
' parent_id ' : fields . many2one ( ' crm.case.section ' , ' Parent Section ' ) ,
2010-01-08 11:05:05 +00:00
' child_ids ' : fields . one2many ( ' crm.case.section ' , ' parent_id ' , ' Child Sections ' ) ,
2008-07-22 15:11:28 +00:00
}
_defaults = {
' active ' : lambda * a : 1 ,
2009-03-11 08:04:07 +00:00
' allow_unlink ' : lambda * a : 1 ,
2008-07-22 15:11:28 +00:00
}
_sql_constraints = [
( ' code_uniq ' , ' unique (code) ' , ' The code of the section must be unique ! ' )
]
def _check_recursion ( self , cr , uid , ids ) :
level = 100
while len ( ids ) :
2009-10-21 07:24:49 +00:00
cr . execute ( ' select distinct parent_id from crm_case_section where id in ( ' + ' , ' . join ( map ( str , ids ) ) + ' ) ' )
2008-07-22 15:11:28 +00:00
ids = filter ( None , map ( lambda x : x [ 0 ] , cr . fetchall ( ) ) )
if not level :
return False
level - = 1
return True
_constraints = [
2009-02-11 09:35:48 +00:00
( _check_recursion , ' Error ! You cannot create recursive sections. ' , [ ' parent_id ' ] )
2008-07-22 15:11:28 +00:00
]
def name_get ( self , cr , uid , ids , context = { } ) :
if not len ( ids ) :
return [ ]
reads = self . read ( cr , uid , ids , [ ' name ' , ' parent_id ' ] , context )
res = [ ]
for record in reads :
name = record [ ' name ' ]
if record [ ' parent_id ' ] :
name = record [ ' parent_id ' ] [ 1 ] + ' / ' + name
res . append ( ( record [ ' id ' ] , name ) )
return res
2006-12-07 13:41:40 +00:00
crm_case_section ( )
2010-01-18 10:03:52 +00:00
class crm_case_categ ( osv . osv ) :
_name = " crm.case.categ "
_description = " Category of case "
2010-01-18 14:42:02 +00:00
2010-01-18 10:03:52 +00:00
_columns = {
' name ' : fields . char ( ' Case Category Name ' , size = 64 , required = True , translate = True ) ,
' probability ' : fields . float ( ' Probability ( % ) ' , required = True ) ,
' section_id ' : fields . many2one ( ' crm.case.section ' , ' Case Section ' ) ,
' object_id ' : fields . many2one ( ' ir.model ' , ' Object Name ' ) ,
}
2010-01-18 14:42:02 +00:00
def _find_object_id ( self , cr , uid , context = None ) :
object_id = context and context . get ( ' object_id ' , False ) or False
ids = self . pool . get ( ' ir.model ' ) . search ( cr , uid , [ ( ' model ' , ' = ' , object_id ) ] )
return ids and ids [ 0 ]
2010-01-18 10:03:52 +00:00
_defaults = {
2010-01-18 14:42:02 +00:00
' probability ' : lambda * args : 0.0 ,
' object_id ' : _find_object_id
2010-01-18 10:03:52 +00:00
}
2010-01-18 14:42:02 +00:00
#
2010-01-18 10:03:52 +00:00
crm_case_categ ( )
class crm_case_resource_type ( osv . osv ) :
_name = " crm.case.resource.type "
_description = " Resource Type of case "
_rec_name = " name "
_columns = {
' name ' : fields . char ( ' Case Resource Type ' , size = 64 , required = True , translate = True ) ,
' section_id ' : fields . many2one ( ' crm.case.section ' , ' Case Section ' ) ,
' object_id ' : fields . many2one ( ' ir.model ' , ' Object Name ' ) ,
}
2010-01-18 14:42:02 +00:00
def _find_object_id ( self , cr , uid , context = None ) :
object_id = context and context . get ( ' object_id ' , False ) or False
ids = self . pool . get ( ' ir.model ' ) . search ( cr , uid , [ ( ' model ' , ' = ' , object_id ) ] )
return ids and ids [ 0 ]
_defaults = {
' object_id ' : _find_object_id
}
2010-01-18 10:03:52 +00:00
crm_case_resource_type ( )
class crm_case_stage ( osv . osv ) :
_name = " crm.case.stage "
_description = " Stage of case "
_rec_name = ' name '
_order = " sequence "
_columns = {
' name ' : fields . char ( ' Stage Name ' , size = 64 , required = True , translate = True ) ,
' section_id ' : fields . many2one ( ' crm.case.section ' , ' Case Section ' ) ,
' sequence ' : fields . integer ( ' Sequence ' , help = " Gives the sequence order when displaying a list of case stages. " ) ,
' object_id ' : fields . many2one ( ' ir.model ' , ' Object Name ' ) ,
}
2010-01-18 14:42:02 +00:00
def _find_object_id ( self , cr , uid , context = None ) :
object_id = context and context . get ( ' object_id ' , False ) or False
ids = self . pool . get ( ' ir.model ' ) . search ( cr , uid , [ ( ' model ' , ' = ' , object_id ) ] )
return ids and ids [ 0 ]
2010-01-18 10:03:52 +00:00
_defaults = {
2010-01-18 14:42:02 +00:00
' sequence ' : lambda * args : 1 ,
' object_id ' : _find_object_id
2010-01-18 10:03:52 +00:00
}
2010-01-18 14:42:02 +00:00
2010-01-18 10:03:52 +00:00
crm_case_stage ( )
2010-01-13 09:37:24 +00:00
2006-12-07 13:41:40 +00:00
class crm_case_rule ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = " crm.case.rule "
_description = " Case Rule "
_columns = {
' name ' : fields . char ( ' Rule Name ' , size = 64 , required = True ) ,
2009-12-21 12:21:32 +00:00
' active ' : fields . boolean ( ' Active ' , help = " If the active field is set to true, it will allow you to hide the case rule without removing it. " ) ,
2009-12-21 13:14:12 +00:00
' sequence ' : fields . integer ( ' Sequence ' , help = " Gives the sequence order when displaying a list of case rules. " ) ,
2008-07-22 15:11:28 +00:00
2008-10-29 13:34:45 +00:00
' trg_state_from ' : fields . selection ( [ ( ' ' , ' ' ) , ( ' escalate ' , ' Escalate ' ) ] + AVAILABLE_STATES , ' Case State ' , size = 16 ) ,
' trg_state_to ' : fields . selection ( [ ( ' ' , ' ' ) , ( ' escalate ' , ' Escalate ' ) ] + AVAILABLE_STATES , ' Button Pressed ' , size = 16 ) ,
2008-07-22 15:11:28 +00:00
' trg_date_type ' : fields . selection ( [
( ' none ' , ' None ' ) ,
( ' create ' , ' Creation Date ' ) ,
( ' action_last ' , ' Last Action Date ' ) ,
( ' deadline ' , ' Deadline ' ) ,
2008-12-08 17:08:40 +00:00
( ' date ' , ' Date ' ) ,
2008-07-22 15:11:28 +00:00
] , ' Trigger Date ' , size = 16 ) ,
2009-09-24 10:46:21 +00:00
' trg_date_range ' : fields . integer ( ' Delay after trigger date ' , help = " Delay After Trigger Date, specifies you can put a negative number " \
" if you need a delay before the trigger date, like sending a reminder 15 minutes before a meeting. " ) ,
2008-07-22 15:11:28 +00:00
' trg_date_range_type ' : fields . selection ( [ ( ' minutes ' , ' Minutes ' ) , ( ' hour ' , ' Hours ' ) , ( ' day ' , ' Days ' ) , ( ' month ' , ' Months ' ) ] , ' Delay type ' ) ,
' trg_section_id ' : fields . many2one ( ' crm.case.section ' , ' Section ' ) ,
2010-01-13 09:37:24 +00:00
#'trg_categ_id': fields.many2one('crm.case.categ', 'Category', domain="[('section_id','=',trg_section_id)]"),
2008-07-22 15:11:28 +00:00
' trg_user_id ' : fields . many2one ( ' res.users ' , ' Responsible ' ) ,
' trg_partner_id ' : fields . many2one ( ' res.partner ' , ' Partner ' ) ,
' trg_partner_categ_id ' : fields . many2one ( ' res.partner.category ' , ' Partner Category ' ) ,
2008-10-29 13:34:45 +00:00
' trg_priority_from ' : fields . selection ( [ ( ' ' , ' ' ) ] + AVAILABLE_PRIORITIES , ' Minimum Priority ' ) ,
' trg_priority_to ' : fields . selection ( [ ( ' ' , ' ' ) ] + AVAILABLE_PRIORITIES , ' Maximim Priority ' ) ,
2009-03-19 12:05:49 +00:00
' trg_max_history ' : fields . integer ( ' Maximum Communication History ' ) ,
2008-07-22 15:11:28 +00:00
' act_method ' : fields . char ( ' Call Object Method ' , size = 64 ) ,
' act_state ' : fields . selection ( [ ( ' ' , ' ' ) ] + AVAILABLE_STATES , ' Set state to ' , size = 16 ) ,
' act_section_id ' : fields . many2one ( ' crm.case.section ' , ' Set section to ' ) ,
' act_user_id ' : fields . many2one ( ' res.users ' , ' Set responsible to ' ) ,
' act_priority ' : fields . selection ( [ ( ' ' , ' ' ) ] + AVAILABLE_PRIORITIES , ' Set priority to ' ) ,
2009-09-24 10:46:21 +00:00
' act_email_cc ' : fields . char ( ' Add watchers (Cc) ' , size = 250 , help = " These people will receive a copy of the future communication between partner and users by email " ) ,
2008-07-22 15:11:28 +00:00
2008-10-29 13:34:45 +00:00
' act_remind_partner ' : fields . boolean ( ' Remind Partner ' , help = " Check this if you want the rule to send a reminder by email to the partner. " ) ,
' act_remind_user ' : fields . boolean ( ' Remind responsible ' , help = " Check this if you want the rule to send a reminder by email to the user. " ) ,
' act_remind_attach ' : fields . boolean ( ' Remind with attachment ' , help = " Check this if you want that all documents attached to the case be attached to the reminder email sent. " ) ,
2008-07-22 15:11:28 +00:00
2009-09-24 10:46:21 +00:00
' act_mail_to_user ' : fields . boolean ( ' Mail to responsible ' , help = " Check this if you want the rule to send an email to the responsible person. " ) ,
' act_mail_to_partner ' : fields . boolean ( ' Mail to partner ' , help = " Check this if you want the rule to send an email to the partner. " ) ,
' act_mail_to_watchers ' : fields . boolean ( ' Mail to watchers (CC) ' , help = " Check this if you want the rule to mark CC(mail to any other person defined in actions). " ) ,
' act_mail_to_email ' : fields . char ( ' Mail to these emails ' , size = 128 , help = " Email-id of the persons whom mail is to be sent " ) ,
' act_mail_body ' : fields . text ( ' Mail body ' , help = " Content of mail " ) ,
' regex_name ' : fields . char ( ' Regular Expression on Case Name ' , size = 128 ) ,
' regex_history ' : fields . char ( ' Regular Expression on Case History ' , size = 128 ) ,
' server_action_id ' : fields . many2one ( ' ir.actions.server ' , ' Server Action ' , help = " Describes the action name. " \
" eg:on which object which ation to be taken on basis of which condition " ) ,
2008-07-22 15:11:28 +00:00
}
_defaults = {
' active ' : lambda * a : 1 ,
' trg_date_type ' : lambda * a : ' none ' ,
' trg_date_range_type ' : lambda * a : ' day ' ,
' act_mail_to_user ' : lambda * a : 0 ,
' act_remind_partner ' : lambda * a : 0 ,
' act_remind_user ' : lambda * a : 0 ,
' act_mail_to_partner ' : lambda * a : 0 ,
' act_mail_to_watchers ' : lambda * a : 0 ,
}
_order = ' sequence '
def _check ( self , cr , uid , ids = False , context = { } ) :
'''
2009-04-21 12:14:23 +00:00
Function called by the scheduler to process cases for date actions
Only works on not done and cancelled cases
2008-07-22 15:11:28 +00:00
'''
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 [ ] )
case_obj = self . pool . get ( ' crm.case ' )
cases = case_obj . browse ( cr , uid , ids2 , context )
return case_obj . _action ( cr , uid , cases , False , context = context )
2008-10-27 14:01:29 +00:00
def _check_mail ( self , cr , uid , ids , context = None ) :
caseobj = self . pool . get ( ' crm.case ' )
emptycase = orm . browse_null ( )
for rule in self . browse ( cr , uid , ids ) :
if rule . act_mail_body :
try :
caseobj . format_mail ( emptycase , rule . act_mail_body )
except ( ValueError , KeyError , TypeError ) :
return False
return True
2009-02-04 09:46:57 +00:00
2008-10-27 14:01:29 +00:00
_constraints = [
( _check_mail , ' Error: The mail is not well formated ' , [ ' act_mail_body ' ] ) ,
]
2006-12-07 13:41:40 +00:00
crm_case_rule ( )
def _links_get ( self , cr , uid , context = { } ) :
2008-07-22 15:11:28 +00:00
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 ]
2006-12-07 13:41:40 +00:00
2010-01-11 07:07:47 +00:00
2006-12-07 13:41:40 +00:00
class crm_case ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = " crm.case "
_description = " Case "
def _email_last ( self , cursor , user , ids , name , arg , context = None ) :
res = { }
for case in self . browse ( cursor , user , ids ) :
if case . history_line :
res [ case . id ] = case . history_line [ 0 ] . description
else :
res [ case . id ] = False
return res
2010-01-13 13:09:57 +00:00
2008-07-22 15:11:28 +00:00
def copy ( self , cr , uid , id , default = None , context = { } ) :
if not default : default = { }
2010-01-13 13:09:57 +00:00
default . update ( { ' state ' : ' draft ' , ' id ' : False } )
2008-07-22 15:11:28 +00:00
return super ( crm_case , self ) . copy ( cr , uid , id , default , context )
2010-01-13 13:09:57 +00:00
def _get_log_ids ( self , cr , uid , ids , field_names , arg , context = { } ) :
result = { }
history_obj = False
model_obj = self . pool . get ( ' ir.model ' )
if ' history_line ' in field_names :
history_obj = self . pool . get ( ' crm.case.history ' )
name = ' history_line '
if ' log_ids ' in field_names :
history_obj = self . pool . get ( ' crm.case.log ' )
name = ' log_ids '
if not history_obj :
return result
for case in self . browse ( cr , uid , ids , context ) :
model_ids = model_obj . search ( cr , uid , [ ( ' model ' , ' = ' , case . _name ) ] )
history_ids = history_obj . search ( cr , uid , [ ( ' model_id ' , ' = ' , model_ids [ 0 ] ) , ( ' res_id ' , ' = ' , case . id ) ] )
if history_ids :
result [ case . id ] = { name : history_ids }
else :
result [ case . id ] = { name : [ ] }
return result
2008-07-22 15:11:28 +00:00
_columns = {
2008-12-04 07:38:03 +00:00
' id ' : fields . integer ( ' ID ' , readonly = True ) ,
2009-03-09 07:13:23 +00:00
' name ' : fields . char ( ' Description ' , size = 64 , required = True ) ,
2009-12-21 12:21:32 +00:00
' active ' : fields . boolean ( ' Active ' , help = " If the active field is set to true, it will allow you to hide the case without removing it. " ) ,
2008-07-22 15:11:28 +00:00
' description ' : fields . text ( ' Your action ' ) ,
2009-12-29 11:02:22 +00:00
' section_id ' : fields . many2one ( ' crm.case.section ' , ' Section ' , select = True , help = ' Section to which Case belongs to. Define Responsible user and Email account for mail gateway. ' ) ,
2009-09-24 10:46:21 +00:00
' email_from ' : fields . char ( ' Partner Email ' , size = 128 , help = " These people will receive email. " ) ,
' email_cc ' : fields . char ( ' Watchers Emails ' , size = 252 , help = " These people will receive a copy of the future " \
" communication between partner and users by email " ) ,
2008-07-22 15:11:28 +00:00
' email_last ' : fields . function ( _email_last , method = True ,
string = ' Latest E-Mail ' , type = ' text ' ) ,
' partner_id ' : fields . many2one ( ' res.partner ' , ' Partner ' ) ,
' partner_address_id ' : fields . many2one ( ' res.partner.address ' , ' Partner Contact ' , domain = " [( ' partner_id ' , ' = ' ,partner_id)] " ) ,
' date ' : fields . datetime ( ' Date ' ) ,
2008-12-04 07:38:03 +00:00
' create_date ' : fields . datetime ( ' Created ' , readonly = True ) ,
2008-07-22 15:11:28 +00:00
' date_deadline ' : fields . datetime ( ' Deadline ' ) ,
2008-10-31 14:09:59 +00:00
' user_id ' : fields . many2one ( ' res.users ' , ' Responsible ' ) ,
2010-01-13 13:09:57 +00:00
' history_line ' : fields . function ( _get_log_ids , method = True , type = ' one2many ' , multi = " history_line " , relation = " crm.case.history " , string = " Communication " ) ,
' log_ids ' : fields . function ( _get_log_ids , method = True , type = ' one2many ' , multi = " log_ids " , relation = " crm.case.log " , string = " Logs History " ) ,
2009-11-23 13:52:38 +00:00
' state ' : fields . selection ( AVAILABLE_STATES , ' State ' , size = 16 , readonly = True ,
2009-11-25 11:40:55 +00:00
help = ' The state is set to \' Draft \' , when a case is created. \
\nIf the case is in progress the state is set to \' Open \' . \
\nWhen the case is over , the state is set to \' Done \' . \
\nIf the case needs to be reviewed then the state is set to \' Pending \' . ' ) ,
2008-07-22 15:11:28 +00:00
' date_action_last ' : fields . datetime ( ' Last Action ' , readonly = 1 ) ,
' date_action_next ' : fields . datetime ( ' Next Action ' , readonly = 1 ) ,
2009-11-24 17:15:16 +00:00
' company_id ' : fields . many2one ( ' res.company ' , ' Company ' ) ,
2008-07-22 15:11:28 +00:00
}
def _get_default_partner_address ( self , cr , uid , 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 ) :
if not context . get ( ' portal ' , False ) :
return False
user = self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid , context )
if not user . address_id :
return False
return user . address_id . partner_id . id
def _get_default_email ( self , cr , uid , context ) :
if not context . get ( ' portal ' , False ) :
return False
user = self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid , context )
if not user . address_id :
return False
return user . address_id . email
def _get_default_user ( self , cr , uid , context ) :
if context . get ( ' portal ' , False ) :
return False
return uid
2010-01-08 11:05:05 +00:00
def _get_section ( self , cr , uid , context ) :
2010-01-11 09:20:00 +00:00
user = self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid , context = context )
return user . context_section_id
2010-01-08 11:05:05 +00:00
2008-07-22 15:11:28 +00:00
_defaults = {
' active ' : lambda * a : 1 ,
' user_id ' : _get_default_user ,
' partner_id ' : _get_default_partner ,
' partner_address_id ' : _get_default_partner_address ,
' email_from ' : _get_default_email ,
2009-06-05 08:01:29 +00:00
' state ' : lambda * a : ' draft ' ,
2009-03-11 13:34:51 +00:00
' date ' : lambda * a : time . strftime ( ' % Y- % m- %d % H: % M: % S ' ) ,
2010-01-08 11:05:05 +00:00
' section_id ' : _get_section ,
2008-07-22 15:11:28 +00:00
}
2010-01-11 11:11:20 +00:00
_order = ' date_deadline desc, date desc,id desc '
2008-07-22 15:11:28 +00:00
2008-10-31 15:30:22 +00:00
def unlink ( self , cr , uid , ids , context = { } ) :
for case in self . browse ( cr , uid , ids , context ) :
2009-03-11 08:04:07 +00:00
if ( not case . section_id . allow_unlink ) and ( case . state < > ' draft ' ) :
2008-10-31 15:30:22 +00:00
raise osv . except_osv ( _ ( ' Warning ! ' ) ,
_ ( ' You can not delete this case. You should better cancel it. ' ) )
return super ( crm_case , self ) . unlink ( cr , uid , ids , context )
2010-01-11 07:07:47 +00:00
def stage_next ( self , cr , uid , ids , context = { } ) :
ok = False
2010-01-19 06:35:04 +00:00
sid = self . pool . get ( ' crm.case.stage ' ) . search ( cr , uid , [ ( ' object_id.model ' , ' = ' , self . _name ) ] , context = context )
2010-01-11 07:07:47 +00:00
s = { }
previous = { }
for stage in self . pool . get ( ' crm.case.stage ' ) . browse ( cr , uid , sid , context = context ) :
section = stage . section_id . id or False
s . setdefault ( section , { } )
s [ section ] [ previous . get ( section , False ) ] = stage . id
previous [ section ] = stage . id
for case in self . browse ( cr , uid , ids , context ) :
section = ( case . section_id . id or False )
if section in s :
st = case . stage_id . id or False
if st in s [ section ] :
self . write ( cr , uid , [ case . id ] , { ' stage_id ' : s [ section ] [ st ] } )
return True
def onchange_case_id ( self , cr , uid , ids , case_id , name , partner_id , context = { } ) :
if not case_id :
return { }
case = self . browse ( cr , uid , case_id , context = context )
value = { }
if not name :
value [ ' name ' ] = case . name
if ( not partner_id ) and case . partner_id :
value [ ' partner_id ' ] = case . partner_id . id
if case . partner_address_id :
value [ ' partner_address_id ' ] = case . partner_address_id . id
if case . email_from :
value [ ' email_from ' ] = case . email_from
return { ' value ' : value }
2008-07-22 15:11:28 +00:00
def _action ( self , cr , uid , cases , state_to , scrit = None , context = { } ) :
if not scrit :
scrit = [ ]
action_ids = self . pool . get ( ' crm.case.rule ' ) . search ( cr , uid , scrit )
level = MAX_LEVEL
while len ( action_ids ) and level :
newactions = [ ]
actions = self . pool . get ( ' crm.case.rule ' ) . browse ( cr , uid , action_ids , context )
for case in cases :
for action in actions :
ok = True
ok = ok and ( not action . trg_state_from or action . trg_state_from == case . state )
ok = ok and ( not action . trg_state_to or action . trg_state_to == state_to )
ok = ok and ( not action . trg_section_id or action . trg_section_id . id == case . section_id . id )
ok = ok and ( not action . trg_categ_id or action . trg_categ_id . id == case . categ_id . id )
ok = ok and ( not action . trg_user_id . id or action . trg_user_id . id == case . user_id . id )
ok = ok and ( not action . trg_partner_id . id or action . trg_partner_id . id == case . partner_id . id )
2009-03-19 12:05:49 +00:00
ok = ok and ( not action . trg_max_history or action . trg_max_history < = ( len ( case . history_line ) + 1 ) )
2008-07-22 15:11:28 +00:00
ok = ok and (
not action . trg_partner_categ_id . id or
(
case . partner_id . id and
( action . trg_partner_categ_id . id in map ( lambda x : x . id , case . partner_id . category_id or [ ] ) )
)
)
ok = ok and ( not action . trg_priority_from or action . trg_priority_from > = case . priority )
ok = ok and ( not action . trg_priority_to or action . trg_priority_to < = case . priority )
2009-09-24 10:46:21 +00:00
reg_name = action . regex_name
result_name = True
if reg_name :
ptrn = re . compile ( str ( reg_name ) )
_result = ptrn . search ( str ( case . name ) )
if not _result :
result_name = False
2009-12-01 07:19:53 +00:00
regex_n = not reg_name or result_name
2009-09-24 10:46:21 +00:00
ok = ok and regex_n
2009-12-01 07:19:53 +00:00
2009-09-24 10:46:21 +00:00
reg_history = action . regex_history
result_history = True
if reg_history :
ptrn = re . compile ( str ( reg_history ) )
if case . history_line :
_result = ptrn . search ( str ( case . history_line [ 0 ] . description ) )
if not _result :
result_history = False
2009-12-01 07:19:53 +00:00
regex_h = not reg_history or result_history
2009-09-24 10:46:21 +00:00
ok = ok and regex_h
2009-12-01 07:19:53 +00:00
2008-07-22 15:11:28 +00:00
if not ok :
continue
base = False
if action . trg_date_type == ' create ' :
base = mx . DateTime . strptime ( case . create_date [ : 19 ] , ' % Y- % m- %d % H: % M: % S ' )
elif action . trg_date_type == ' action_last ' :
if case . date_action_last :
base = mx . DateTime . strptime ( case . date_action_last , ' % Y- % m- %d % H: % M: % S ' )
else :
base = mx . DateTime . strptime ( case . create_date [ : 19 ] , ' % Y- % m- %d % H: % M: % S ' )
elif action . trg_date_type == ' deadline ' and case . date_deadline :
2009-03-05 12:24:31 +00:00
base = mx . DateTime . strptime ( case . date_deadline , ' % Y- % m- %d % H: % M: % S ' )
2008-12-08 17:08:40 +00:00
elif action . trg_date_type == ' date ' and case . date :
2009-03-05 12:24:31 +00:00
base = mx . DateTime . strptime ( case . date , ' % Y- % m- %d % H: % M: % S ' )
2008-07-22 15:11:28 +00:00
if base :
fnct = {
' minutes ' : lambda interval : mx . DateTime . RelativeDateTime ( minutes = interval ) ,
' day ' : lambda interval : mx . DateTime . RelativeDateTime ( days = interval ) ,
' hour ' : lambda interval : mx . DateTime . RelativeDateTime ( hours = interval ) ,
' month ' : lambda interval : mx . DateTime . RelativeDateTime ( months = interval ) ,
}
d = base + fnct [ action . trg_date_range_type ] ( action . trg_date_range )
dt = d . strftime ( ' % Y- % m- %d % H: % M: % S ' )
ok = ( dt < = time . strftime ( ' % Y- % m- %d % H: % M: % S ' ) ) and \
( ( not case . date_action_next ) or \
( dt > = case . date_action_next and \
case . date_action_last < case . date_action_next ) )
if not ok :
if not case . date_action_next or dt < case . date_action_next :
case . date_action_next = dt
self . write ( cr , uid , [ case . id ] , { ' date_action_next ' : dt } , context )
else :
ok = action . trg_date_type == ' none '
if ok :
2009-09-24 10:46:21 +00:00
if action . server_action_id :
context . update ( { ' active_id ' : case . id , ' active_ids ' : [ case . id ] } )
2009-12-01 07:19:53 +00:00
self . pool . get ( ' ir.actions.server ' ) . run ( cr , uid , [ action . server_action_id . id ] , context )
2008-07-22 15:11:28 +00:00
write = { }
if action . act_state :
case . state = action . act_state
write [ ' state ' ] = action . act_state
if action . act_section_id :
case . section_id = action . act_section_id
write [ ' section_id ' ] = action . act_section_id . id
if action . act_user_id :
case . user_id = action . act_user_id
write [ ' user_id ' ] = action . act_user_id . id
if action . act_priority :
case . priority = action . act_priority
write [ ' priority ' ] = action . act_priority
if action . act_email_cc :
if ' @ ' in ( case . email_cc or ' ' ) :
2009-08-10 06:20:35 +00:00
emails = case . email_cc . split ( " , " )
if action . act_email_cc not in emails : # and '<'+str(action.act_email_cc)+">" not in emails:
write [ ' email_cc ' ] = case . email_cc + ' , ' + action . act_email_cc
2008-07-22 15:11:28 +00:00
else :
write [ ' email_cc ' ] = action . act_email_cc
write [ ' date_action_last ' ] = time . strftime ( ' % Y- % m- %d % H: % M: % S ' )
self . write ( cr , uid , [ case . id ] , write , context )
caseobj = self . pool . get ( ' crm.case ' )
if action . act_remind_user :
caseobj . remind_user ( cr , uid , [ case . id ] , context , attach = action . act_remind_attach )
if action . act_remind_partner :
caseobj . remind_partner ( cr , uid , [ case . id ] , context , attach = action . act_remind_attach )
if action . act_method :
getattr ( caseobj , ' act_method ' ) ( cr , uid , [ case . id ] , action , context )
emails = [ ]
if action . act_mail_to_user :
if case . user_id and case . user_id . address_id :
emails . append ( case . user_id . address_id . email )
if action . act_mail_to_partner :
emails . append ( case . email_from )
if action . act_mail_to_watchers :
2009-06-04 07:15:49 +00:00
emails + = ( action . act_email_cc or ' ' ) . split ( ' , ' )
2008-07-22 15:11:28 +00:00
if action . act_mail_to_email :
emails + = ( action . act_mail_to_email or ' ' ) . split ( ' , ' )
emails = filter ( None , emails )
if len ( emails ) and action . act_mail_body :
2009-12-01 07:19:53 +00:00
emails = list ( set ( emails ) )
2008-07-22 15:11:28 +00:00
self . email_send ( cr , uid , case , emails , action . act_mail_body )
break
action_ids = newactions
level - = 1
return True
2009-02-04 09:46:57 +00:00
2009-01-29 16:55:34 +00:00
def format_body ( self , body ) :
2009-09-24 10:46:21 +00:00
return body and tools . ustr ( body . encode ( ' ascii ' , ' replace ' ) ) or ' '
2008-07-22 15:11:28 +00:00
2008-10-27 14:01:29 +00:00
def format_mail ( self , case , body ) :
2008-07-22 15:11:28 +00:00
data = {
' case_id ' : case . id ,
' case_subject ' : case . name ,
' case_date ' : case . date ,
2009-07-29 11:59:08 +00:00
' case_description ' : case . description ,
2008-07-22 15:11:28 +00:00
2008-10-27 13:05:17 +00:00
' case_user ' : ( case . user_id and case . user_id . name ) or ' / ' ,
2008-07-22 15:11:28 +00:00
' case_user_email ' : ( case . user_id and case . user_id . address_id and case . user_id . address_id . email ) or ' / ' ,
' case_user_phone ' : ( case . user_id and case . user_id . address_id and case . user_id . address_id . phone ) or ' / ' ,
' email_from ' : case . email_from ,
' partner ' : ( case . partner_id and case . partner_id . name ) or ' / ' ,
' partner_email ' : ( case . partner_address_id and case . partner_address_id . email ) or ' / ' ,
}
2009-04-07 10:16:26 +00:00
return self . format_body ( body % data )
2008-10-27 14:01:29 +00:00
2008-07-22 15:11:28 +00:00
2009-11-05 13:42:43 +00:00
def __history ( self , cr , uid , cases , keyword , history = False , email = False , details = None , context = { } ) :
2010-01-13 13:09:57 +00:00
model_obj = self . pool . get ( ' ir.model ' )
2008-07-22 15:11:28 +00:00
for case in cases :
2010-01-13 13:09:57 +00:00
model_ids = model_obj . search ( cr , uid , [ ( ' model ' , ' = ' , case . _name ) ] )
2008-07-22 15:11:28 +00:00
data = {
2010-01-13 13:09:57 +00:00
' name ' : keyword ,
2008-07-22 15:11:28 +00:00
' user_id ' : uid ,
2009-01-12 10:42:43 +00:00
' date ' : time . strftime ( ' % Y- % m- %d % H: % M: % S ' ) ,
2010-01-13 13:09:57 +00:00
' model_id ' : model_ids and model_ids [ 0 ] or False ,
' res_id ' : case . id ,
2008-10-31 14:09:59 +00:00
' section_id ' : case . section_id . id
2008-07-22 15:11:28 +00:00
}
obj = self . pool . get ( ' crm.case.log ' )
if history and case . description :
obj = self . pool . get ( ' crm.case.history ' )
2009-11-05 13:42:43 +00:00
data [ ' description ' ] = details or case . description
2008-07-22 15:11:28 +00:00
data [ ' email ' ] = email or \
( case . user_id and case . user_id . address_id and \
case . user_id . address_id . email ) or False
2010-01-13 13:09:57 +00:00
res = obj . create ( cr , uid , data , context )
2008-07-22 15:11:28 +00:00
return True
2008-11-02 15:49:28 +00:00
_history = __history
2008-07-22 15:11:28 +00:00
def create ( self , cr , uid , * args , * * argv ) :
res = super ( crm_case , self ) . create ( cr , uid , * args , * * argv )
cases = self . browse ( cr , uid , [ res ] )
cases [ 0 ] . state # to fill the browse record cache
self . _action ( cr , uid , cases , ' draft ' )
return res
def remind_partner ( self , cr , uid , ids , context = { } , attach = False ) :
return self . remind_user ( cr , uid , ids , context , attach ,
destination = False )
def remind_user ( self , cr , uid , ids , context = { } , attach = False ,
destination = True ) :
for case in self . browse ( cr , uid , ids ) :
2009-09-24 10:46:21 +00:00
if not case . section_id . reply_to :
raise osv . except_osv ( _ ( ' Error! ' ) , ( " Reply TO is not specified in Section " ) )
if not case . email_from :
2009-12-01 07:19:53 +00:00
raise osv . except_osv ( _ ( ' Error! ' ) , ( " Partner Email is not specified in Case " ) )
2008-10-06 15:30:37 +00:00
if case . section_id . reply_to and case . email_from :
2008-07-22 15:11:28 +00:00
src = case . email_from
2009-11-26 13:54:00 +00:00
if not src :
raise osv . except_osv ( _ ( ' Error! ' ) ,
_ ( " No E-Mail ID Found for the Responsible Partner or missing reply address in section! " ) )
2008-10-06 15:30:37 +00:00
dest = case . section_id . reply_to
2008-07-22 15:11:28 +00:00
body = case . email_last or case . description
if not destination :
src , dest = dest , src
if case . user_id . signature :
body + = ' \n \n %s ' % ( case . user_id . signature )
dest = [ dest ]
2008-11-24 11:15:30 +00:00
attach_to_send = None
if attach :
attach_ids = self . pool . get ( ' ir.attachment ' ) . search ( cr , uid , [ ( ' res_model ' , ' = ' , ' crm.case ' ) , ( ' res_id ' , ' = ' , case . id ) ] )
attach_to_send = self . pool . get ( ' ir.attachment ' ) . read ( cr , uid , attach_ids , [ ' datas_fname ' , ' datas ' ] )
attach_to_send = map ( lambda x : ( x [ ' datas_fname ' ] , base64 . decodestring ( x [ ' datas ' ] ) ) , attach_to_send )
2009-12-01 07:19:53 +00:00
2008-11-24 11:15:30 +00:00
# Send an email
2009-09-24 10:46:21 +00:00
flag = tools . email_send (
2009-02-04 09:46:57 +00:00
src ,
dest ,
2008-11-24 11:15:30 +00:00
" Reminder: [ %s ] %s " % ( str ( case . id ) , case . name , ) ,
2009-01-29 16:55:34 +00:00
self . format_body ( body ) ,
2009-02-04 09:46:57 +00:00
reply_to = case . section_id . reply_to ,
2008-11-24 11:15:30 +00:00
tinycrm = str ( case . id ) ,
attach = attach_to_send
)
2009-09-24 10:46:21 +00:00
if flag :
2009-12-01 07:19:53 +00:00
raise osv . except_osv ( _ ( ' Email! ' ) , ( " Email Successfully Sent " ) )
else :
2009-09-24 10:46:21 +00:00
raise osv . except_osv ( _ ( ' Email Fail! ' ) , ( " Email is not sent successfully " ) )
2008-07-22 15:11:28 +00:00
return True
def add_reply ( self , cursor , user , ids , context = None ) :
for case in self . browse ( cursor , user , ids , context = context ) :
2010-01-13 13:09:57 +00:00
if case . email_last :
description = email_last
2008-07-22 15:11:28 +00:00
self . write ( cursor , user , case . id , {
' description ' : ' > ' + description . replace ( ' \n ' , ' \n > ' ) ,
} , context = context )
return True
def case_log ( self , cr , uid , ids , context = { } , email = False , * args ) :
cases = self . browse ( cr , uid , ids )
2008-12-08 17:08:40 +00:00
self . __history ( cr , uid , cases , _ ( ' Historize ' ) , history = True , email = email )
2008-07-22 15:11:28 +00:00
return self . write ( cr , uid , ids , { ' description ' : False , ' som ' : False ,
' canal_id ' : False } )
def case_log_reply ( self , cr , uid , ids , context = { } , email = False , * args ) :
cases = self . browse ( cr , uid , ids )
for case in cases :
if not case . email_from :
raise osv . except_osv ( _ ( ' Error! ' ) ,
_ ( ' You must put a Partner eMail to use this action! ' ) )
2009-03-24 11:58:22 +00:00
if not case . user_id :
raise osv . except_osv ( _ ( ' Error! ' ) ,
_ ( ' You must define a responsible user for this case in order to use this action! ' ) )
2009-02-04 09:46:57 +00:00
if not case . description :
raise osv . except_osv ( _ ( ' Error! ' ) ,
_ ( ' Can not send mail with empty body,you should have description in the body ' ) )
2008-12-08 17:08:40 +00:00
self . __history ( cr , uid , cases , _ ( ' Send ' ) , history = True , email = False )
2008-07-22 15:11:28 +00:00
for case in cases :
self . write ( cr , uid , [ case . id ] , {
' description ' : False ,
' som ' : False ,
' canal_id ' : False ,
} )
emails = [ case . email_from ] + ( case . email_cc or ' ' ) . split ( ' , ' )
emails = filter ( None , emails )
2009-02-04 09:38:58 +00:00
body = case . description or ' '
2008-07-22 15:11:28 +00:00
if case . user_id . signature :
body + = ' \n \n %s ' % ( case . user_id . signature )
2009-12-01 07:19:53 +00:00
2009-04-09 14:19:47 +00:00
emailfrom = case . user_id . address_id and case . user_id . address_id . email or False
if not emailfrom :
raise osv . except_osv ( _ ( ' Error! ' ) ,
2009-05-18 05:20:17 +00:00
_ ( " No E-Mail ID Found for your Company address! " ) )
2009-12-01 07:19:53 +00:00
2008-11-24 11:15:30 +00:00
tools . email_send (
2009-04-09 14:19:47 +00:00
emailfrom ,
2008-11-24 11:15:30 +00:00
emails ,
2009-02-04 09:46:57 +00:00
' [ ' + str ( case . id ) + ' ] ' + case . name ,
2009-01-29 16:55:34 +00:00
self . format_body ( body ) ,
2009-02-04 09:46:57 +00:00
reply_to = case . section_id . reply_to ,
2008-11-24 11:15:30 +00:00
tinycrm = str ( case . id )
)
2008-07-22 15:11:28 +00:00
return True
def onchange_partner_id ( self , cr , uid , ids , part , email = False ) :
if not part :
return { ' value ' : { ' partner_address_id ' : False } }
addr = self . pool . get ( ' res.partner ' ) . address_get ( cr , uid , [ part ] , [ ' contact ' ] )
data = { ' partner_address_id ' : addr [ ' contact ' ] }
if addr [ ' contact ' ] and not email :
data [ ' email_from ' ] = self . pool . get ( ' res.partner.address ' ) . browse ( cr , uid , addr [ ' contact ' ] ) . email
return { ' value ' : data }
2010-01-13 10:31:48 +00:00
2008-07-22 15:11:28 +00:00
def onchange_partner_address_id ( self , cr , uid , ids , part , email = False ) :
if not part :
return { ' value ' : { } }
data = { }
if not email :
data [ ' email_from ' ] = self . pool . get ( ' res.partner.address ' ) . browse ( cr , uid , part ) . email
return { ' value ' : data }
def case_close ( self , cr , uid , ids , * args ) :
cases = self . browse ( cr , uid , ids )
cases [ 0 ] . state # to fill the browse record cache
2008-12-08 17:08:40 +00:00
self . __history ( cr , uid , cases , _ ( ' Close ' ) )
2008-07-22 15:11:28 +00:00
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 ) :
cases = self . browse ( cr , uid , ids )
for case in cases :
data = { ' active ' : True , ' user_id ' : False }
if case . section_id . parent_id :
data [ ' section_id ' ] = case . section_id . parent_id . id
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 this case. \n You are already at the top level. ' ) )
self . write ( cr , uid , ids , data )
2009-03-24 10:31:09 +00:00
cases = self . browse ( cr , uid , ids )
2008-12-08 17:08:40 +00:00
self . __history ( cr , uid , cases , _ ( ' Escalate ' ) )
2008-07-22 15:11:28 +00:00
self . _action ( cr , uid , cases , ' escalate ' )
return True
def case_open ( self , cr , uid , ids , * args ) :
cases = self . browse ( cr , uid , ids )
2008-12-08 17:08:40 +00:00
self . __history ( cr , uid , cases , _ ( ' Open ' ) )
2008-07-22 15:11:28 +00:00
for case in cases :
data = { ' state ' : ' open ' , ' active ' : True }
if not case . user_id :
data [ ' user_id ' ] = uid
self . write ( cr , uid , ids , data )
self . _action ( cr , uid , cases , ' open ' )
2010-01-08 11:05:05 +00:00
return True
2008-07-22 15:11:28 +00:00
def case_cancel ( self , cr , uid , ids , * args ) :
cases = self . browse ( cr , uid , ids )
cases [ 0 ] . state # to fill the browse record cache
2008-12-08 17:08:40 +00:00
self . __history ( cr , uid , cases , _ ( ' Cancel ' ) )
2008-07-22 15:11:28 +00:00
self . write ( cr , uid , ids , { ' state ' : ' cancel ' , ' active ' : True } )
self . _action ( cr , uid , cases , ' cancel ' )
return True
def case_pending ( self , cr , uid , ids , * args ) :
cases = self . browse ( cr , uid , ids )
cases [ 0 ] . state # to fill the browse record cache
2008-12-08 17:08:40 +00:00
self . __history ( cr , uid , cases , _ ( ' Pending ' ) )
2008-07-22 15:11:28 +00:00
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 ) :
cases = self . browse ( cr , uid , ids )
cases [ 0 ] . state # to fill the browse record cache
2008-12-08 17:08:40 +00:00
self . __history ( cr , uid , cases , _ ( ' Draft ' ) )
2008-07-22 15:11:28 +00:00
self . write ( cr , uid , ids , { ' state ' : ' draft ' , ' active ' : True } )
self . _action ( cr , uid , cases , ' draft ' )
return True
2006-12-07 13:41:40 +00:00
crm_case ( )
2010-01-11 07:07:47 +00:00
2007-06-18 07:30:01 +00:00
class crm_case_log ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = " crm.case.log "
2008-10-31 14:09:59 +00:00
_description = " Case Communication History "
2008-07-22 15:11:28 +00:00
_order = " id desc "
_columns = {
2009-09-24 10:46:21 +00:00
' name ' : fields . char ( ' Status ' , size = 64 ) ,
2008-07-22 15:11:28 +00:00
' som ' : fields . many2one ( ' res.partner.som ' , ' State of Mind ' ) ,
' date ' : fields . datetime ( ' Date ' ) ,
' canal_id ' : fields . many2one ( ' res.partner.canal ' , ' Channel ' ) ,
2008-10-31 14:09:59 +00:00
' section_id ' : fields . many2one ( ' crm.case.section ' , ' Section ' ) ,
2008-07-22 15:11:28 +00:00
' user_id ' : fields . many2one ( ' res.users ' , ' User Responsible ' , readonly = True ) ,
2010-01-13 13:09:57 +00:00
' model_id ' : fields . many2one ( ' ir.model ' , " Model " ) ,
' res_id ' : fields . integer ( ' Resource ID ' ) ,
2008-07-22 15:11:28 +00:00
}
_defaults = {
' date ' : lambda * a : time . strftime ( ' % Y- % m- %d % H: % M: % S ' ) ,
}
2007-06-18 07:30:01 +00:00
crm_case_log ( )
class crm_case_history ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = " crm.case.history "
_description = " Case history "
_order = " id desc "
_inherits = { ' crm.case.log ' : " log_id " }
2009-03-19 12:05:49 +00:00
def create ( self , cr , user , vals , context = None ) :
2010-01-13 13:09:57 +00:00
if vals . has_key ( ' res_id ' ) and vals [ ' res_id ' ] :
case_obj = self . pool . get ( vals [ ' model_id ' ] )
cases = case_obj . browse ( cr , user , [ vals [ ' res_id ' ] ] )
2009-03-19 12:05:49 +00:00
case_obj . _action ( cr , user , cases , ' ' )
return super ( crm_case_history , self ) . create ( cr , user , vals , context )
2008-07-22 15:11:28 +00:00
def _note_get ( self , cursor , user , ids , name , arg , context = None ) :
res = { }
for hist in self . browse ( cursor , user , ids , context or { } ) :
res [ hist . id ] = ( hist . email or ' / ' ) + ' ( ' + str ( hist . date ) + ' ) \n '
res [ hist . id ] + = ( hist . description or ' ' )
return res
_columns = {
' description ' : fields . text ( ' Description ' ) ,
' note ' : fields . function ( _note_get , method = True , string = " Description " , type = " text " ) ,
' email ' : fields . char ( ' Email ' , size = 84 ) ,
' log_id ' : fields . many2one ( ' crm.case.log ' , ' Log ' , ondelete = ' cascade ' ) ,
}
2006-12-07 13:41:40 +00:00
crm_case_history ( )
2009-10-29 11:27:13 +00:00
class crm_email_add_cc_wizard ( osv . osv_memory ) :
_name = " crm.email.add.cc "
_description = " Email Add CC "
_columns = {
' name ' : fields . selection ( [ ( ' user ' , ' User ' ) , ( ' partner ' , ' Partner ' ) , ( ' email ' , ' Email Address ' ) ] , ' Send to ' , required = True ) ,
' user_id ' : fields . many2one ( ' res.users ' , " User " ) ,
' partner_id ' : fields . many2one ( ' res.partner ' , " Partner " ) ,
' email ' : fields . char ( ' Email ' , size = 32 ) ,
2009-11-05 13:42:43 +00:00
' subject ' : fields . char ( ' Subject ' , size = 32 ) ,
2009-10-29 11:27:13 +00:00
}
2009-12-01 07:19:53 +00:00
2009-10-29 11:27:13 +00:00
def change_email ( self , cr , uid , ids , user , partner ) :
if ( not partner and not user ) :
return { ' value ' : { ' email ' : False } }
email = False
if partner :
addr = self . pool . get ( ' res.partner ' ) . address_get ( cr , uid , [ partner ] , [ ' contact ' ] )
if addr :
email = self . pool . get ( ' res.partner.address ' ) . read ( cr , uid , addr [ ' contact ' ] , [ ' email ' ] ) [ ' email ' ]
elif user :
addr = self . pool . get ( ' res.users ' ) . read ( cr , uid , user , [ ' address_id ' ] ) [ ' address_id ' ]
if addr :
email = self . pool . get ( ' res.partner.address ' ) . read ( cr , uid , addr [ 0 ] , [ ' email ' ] ) [ ' email ' ]
return { ' value ' : { ' email ' : email } }
2009-12-01 07:19:53 +00:00
2009-10-29 11:27:13 +00:00
def add_cc ( self , cr , uid , ids , context = { } ) :
2009-11-05 13:42:43 +00:00
data = self . read ( cr , uid , ids [ 0 ] )
email = data [ ' email ' ]
subject = data [ ' subject ' ]
2009-10-29 11:27:13 +00:00
if not context :
return { }
history_line = self . pool . get ( ' crm.case.history ' ) . browse ( cr , uid , context [ ' active_id ' ] )
crm_case = self . pool . get ( ' crm.case ' )
2009-11-05 13:42:43 +00:00
case = history_line . log_id . case_id
2009-12-01 07:19:53 +00:00
body = history_line . description . replace ( ' \n ' , ' \n > ' )
2009-11-05 13:42:43 +00:00
flag = tools . email_send (
case . user_id . address_id . email ,
[ case . email_from ] ,
subject or ' [ ' + str ( case . id ) + ' ] ' + case . name ,
crm_case . format_body ( body ) ,
email_cc = [ email ] ,
tinycrm = str ( case . id )
)
if flag :
2009-12-01 07:19:53 +00:00
crm_case . write ( cr , uid , case . id , { ' email_cc ' : case . email_cc and case . email_cc + ' , ' + email or email } )
else :
raise osv . except_osv ( _ ( ' Email Fail! ' ) , ( " Lastest Email is not sent successfully " ) )
2009-10-29 11:27:13 +00:00
return { }
2009-12-01 07:19:53 +00:00
2009-10-29 11:27:13 +00:00
crm_email_add_cc_wizard ( )
2008-07-23 14:41:47 +00:00
2010-01-11 09:20:00 +00:00
def _section_get ( self , cr , uid , context = { } ) :
obj = self . pool . get ( ' crm.case.section ' )
ids = obj . search ( cr , uid , [ ] )
res = obj . read ( cr , uid , ids , [ ' id ' , ' name ' ] , context )
res = [ ( str ( r [ ' id ' ] ) , r [ ' name ' ] ) for r in res ]
return res
2010-01-08 11:05:05 +00:00
2010-01-11 09:20:00 +00:00
class users ( osv . osv ) :
_inherit = ' res.users '
_description = " Users "
2010-01-08 11:05:05 +00:00
_columns = {
2010-01-11 09:20:00 +00:00
' context_section_id ' : fields . selection ( _section_get , ' Sales Section ' ) ,
}
users ( )
2010-01-08 11:05:05 +00:00