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
#
##############################################################################
2010-02-16 13:57:41 +00:00
from datetime import datetime
from datetime import timedelta
from osv import fields
from osv import osv
from tools . translate import _
2010-03-10 06:21:30 +00:00
import time
import tools
2010-02-16 13:57:41 +00:00
2009-09-24 10:46:21 +00:00
2006-12-07 13:41:40 +00:00
MAX_LEVEL = 15
AVAILABLE_STATES = [
2010-03-19 11:15:35 +00:00
( ' draft ' , ' Draft ' ) ,
( ' open ' , ' Open ' ) ,
( ' cancel ' , ' Cancelled ' ) ,
( ' done ' , ' Closed ' ) ,
( ' pending ' , ' Pending ' )
2006-12-07 13:41:40 +00:00
]
AVAILABLE_PRIORITIES = [
2010-03-19 11:15:35 +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 = {
2010-03-19 11:15:35 +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 ) :
2010-03-19 11:15:35 +00:00
""" Cases Section """
2008-07-22 15:11:28 +00:00
_name = " crm.case.section "
2010-02-28 13:21:56 +00:00
_description = " Sales Teams "
2010-01-09 14:30:02 +00:00
_order = " name "
2010-03-19 11:15:35 +00:00
2008-07-22 15:11:28 +00:00
_columns = {
2010-03-19 11:15:35 +00:00
' name ' : fields . char ( ' Sales Team ' , size = 64 , required = True , translate = 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. " ) ,
' allow_unlink ' : fields . boolean ( ' Allow Delete ' , help = " Allows to delete non draft cases " ) ,
' 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 sales team " ) ,
' parent_id ' : fields . many2one ( ' crm.case.section ' , ' Parent Section ' ) ,
' child_ids ' : fields . one2many ( ' crm.case.section ' , ' parent_id ' , ' Child Sections ' ) ,
2008-07-22 15:11:28 +00:00
}
2010-03-19 11:15:35 +00:00
2008-07-22 15:11:28 +00:00
_defaults = {
2010-03-19 11:15:35 +00:00
' active ' : lambda * a : 1 ,
' allow_unlink ' : lambda * a : 1 ,
2008-07-22 15:11:28 +00:00
}
2010-03-19 11:15:35 +00:00
2008-07-22 15:11:28 +00:00
_sql_constraints = [
( ' code_uniq ' , ' unique (code) ' , ' The code of the section must be unique ! ' )
]
2010-03-19 11:15:35 +00:00
2008-07-22 15:11:28 +00:00
def _check_recursion ( self , cr , uid , ids ) :
2010-03-19 11:15:35 +00:00
"""
Checks for recursion level for sections
@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 section ids
"""
2008-07-22 15:11:28 +00:00
level = 100
2010-03-19 11:15:35 +00:00
2008-07-22 15:11:28 +00:00
while len ( ids ) :
2010-03-19 11:15:35 +00:00
cr . execute ( ' select distinct parent_id from crm_case_section where id =ANY( %s ) ' , ( ids , ) )
ids = filter ( None , map ( lambda x : x [ 0 ] , cr . fetchall ( ) ) )
2008-07-22 15:11:28 +00:00
if not level :
return False
level - = 1
2010-03-19 11:15:35 +00:00
2008-07-22 15:11:28 +00:00
return True
2010-03-19 11:15:35 +00:00
2008-07-22 15:11:28 +00:00
_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
]
2010-03-19 11:15:35 +00:00
2008-07-22 15:11:28 +00:00
def name_get ( self , cr , uid , ids , context = { } ) :
2010-03-19 11:15:35 +00:00
""" Overrides orm """
2008-07-22 15:11:28 +00:00
if not len ( ids ) :
return [ ]
2010-03-19 11:15:35 +00:00
reads = self . read ( cr , uid , ids , [ ' name ' , ' parent_id ' ] , context )
2008-07-22 15:11:28 +00:00
res = [ ]
for record in reads :
name = record [ ' name ' ]
if record [ ' parent_id ' ] :
2010-03-19 11:15:35 +00:00
name = record [ ' parent_id ' ] [ 1 ] + ' / ' + name
2008-07-22 15:11:28 +00:00
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 = {
2010-03-19 11:15:35 +00:00
' name ' : fields . char ( ' Case Category Name ' , size = 64 , required = True , translate = True ) ,
' section_id ' : fields . many2one ( ' crm.case.section ' , ' Sales Team ' ) ,
' object_id ' : fields . many2one ( ' ir.model ' , ' Object Name ' ) ,
2010-01-18 10:03:52 +00:00
}
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
2010-03-19 11:15:35 +00:00
ids = self . pool . get ( ' ir.model ' ) . search ( cr , uid , [ ( ' model ' , ' = ' , object_id ) ] )
2010-01-18 14:42:02 +00:00
return ids and ids [ 0 ]
2010-02-23 10:35:15 +00:00
_defaults = {
2010-01-18 14:42:02 +00:00
' 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 = {
2010-03-19 11:15:35 +00:00
' name ' : fields . char ( ' Case Resource Type ' , size = 64 , required = True , translate = True ) ,
' section_id ' : fields . many2one ( ' crm.case.section ' , ' Sales Team ' ) ,
' object_id ' : fields . many2one ( ' ir.model ' , ' Object Name ' ) ,
2010-01-18 10:03:52 +00:00
}
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
2010-03-19 11:15:35 +00:00
ids = self . pool . get ( ' ir.model ' ) . search ( cr , uid , [ ( ' model ' , ' = ' , object_id ) ] )
2010-01-18 14:42:02 +00:00
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 = {
2010-03-19 11:15:35 +00:00
' name ' : fields . char ( ' Stage Name ' , size = 64 , required = True , translate = True ) ,
' section_id ' : fields . many2one ( ' crm.case.section ' , ' Sales Team ' ) ,
' sequence ' : fields . integer ( ' Sequence ' , help = " Gives the sequence order when displaying a list of case stages. " ) ,
' object_id ' : fields . many2one ( ' ir.model ' , ' Object Name ' ) ,
' probability ' : fields . float ( ' Probability ( % ) ' , required = True ) ,
' on_change ' : fields . boolean ( ' Change Probability Automatically ' , help = " Change Probability on next and previous stages. " ) ,
2010-03-17 06:21:16 +00:00
' requirements ' : fields . text ( ' Requirements ' )
2010-01-18 10:03:52 +00:00
}
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
2010-03-19 11:15:35 +00:00
ids = self . pool . get ( ' ir.model ' ) . search ( cr , uid , [ ( ' model ' , ' = ' , object_id ) ] )
2010-01-18 14:42:02 +00:00
return ids and ids [ 0 ]
2010-01-18 10:03:52 +00:00
_defaults = {
2010-03-19 11:15:35 +00:00
' sequence ' : lambda * args : 1 ,
' probability ' : lambda * args : 0.0 ,
2010-01-18 14:42:02 +00:00
' 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
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 = { } ) :
2010-03-19 11:15:35 +00:00
if not default :
default = { }
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 ) :
2010-03-19 11:15:35 +00:00
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 ) ] )
2010-01-13 13:09:57 +00:00
if history_ids :
2010-03-19 11:15:35 +00:00
result [ case . id ] = { name : history_ids }
2010-01-13 13:09:57 +00:00
else :
2010-03-19 11:15:35 +00:00
result [ case . id ] = { name : [ ] }
2010-01-13 13:09:57 +00:00
return result
2008-07-22 15:11:28 +00:00
_columns = {
2010-03-19 11:15:35 +00:00
' id ' : fields . integer ( ' ID ' , readonly = True ) ,
' name ' : fields . char ( ' Description ' , size = 1024 , required = True ) ,
' active ' : fields . boolean ( ' Active ' , help = " If the active field is set to true, it will allow you to hide the case without removing it. " ) ,
' description ' : fields . text ( ' Description ' ) ,
' section_id ' : fields . many2one ( ' crm.case.section ' , ' Sales Team ' , select = True , help = ' Sales team to which Case belongs to. Define Responsible user and Email account for mail gateway. ' ) ,
' email_from ' : fields . char ( ' Email ' , size = 128 , help = " These people will receive email. " ) ,
2010-02-28 11:20:56 +00:00
' email_cc ' : fields . text ( ' Watchers Emails ' , size = 252 , help = " These people will receive a copy of the future " \
2010-03-19 11:15:35 +00:00
" communication between partner and users by email " ) ,
' probability ' : fields . float ( ' Probability ' ) ,
' 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)] " ) ,
' create_date ' : fields . datetime ( ' Creation Date ' , readonly = True ) ,
' write_date ' : fields . datetime ( ' Update Date ' , readonly = True ) ,
' date_deadline ' : fields . date ( ' Deadline ' ) ,
' user_id ' : fields . many2one ( ' res.users ' , ' Responsible ' ) ,
' 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 " ) ,
' stage_id ' : fields . many2one ( ' crm.case.stage ' , ' Stage ' , domain = " [( ' section_id ' , ' = ' ,section_id),( ' object_id.model ' , ' = ' , ' crm.opportunity ' )] " ) ,
' 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 \' . \
2010-03-19 11:15:35 +00:00
\nIf the case needs to be reviewed then the state is set to \' Pending \' . ' ) ,
' company_id ' : fields . many2one ( ' res.company ' , ' Company ' ) ,
2008-07-22 15:11:28 +00:00
}
def _get_default_partner_address ( self , cr , uid , context ) :
2010-03-19 11:15:35 +00:00
if not context . get ( ' portal ' , False ) :
2008-07-22 15:11:28 +00:00
return False
return self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid , context ) . address_id . id
def _get_default_partner ( self , cr , uid , context ) :
2010-03-19 11:15:35 +00:00
if not context . get ( ' portal ' , False ) :
2008-07-22 15:11:28 +00:00
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 ) :
2010-03-19 11:15:35 +00:00
if not context . get ( ' portal ' , False ) :
2008-07-22 15:11:28 +00:00
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-03-19 11:15:35 +00:00
user = self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid , context = context )
2010-02-15 07:54:47 +00:00
return user . context_section_id . id or False
2010-01-08 11:05:05 +00:00
2008-07-22 15:11:28 +00:00
_defaults = {
2010-03-19 11:15:35 +00:00
' 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 ,
' state ' : lambda * a : ' draft ' ,
' date_deadline ' : lambda * a : ( datetime . today ( ) + timedelta ( days = 3 ) ) . strftime ( ' % Y- % m- %d % H: % M: % S ' ) ,
' section_id ' : _get_section ,
' company_id ' : lambda s , cr , uid , c : s . pool . get ( ' res.company ' ) . _company_default_get ( cr , uid , ' crm.case ' , context = c ) ,
2008-07-22 15:11:28 +00:00
}
2010-03-08 11:51:00 +00:00
_order = ' date_deadline desc, create_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 ' ) :
2010-03-19 11:15:35 +00:00
raise osv . except_osv ( _ ( ' Warning ! ' ) ,
2008-10-31 15:30:22 +00:00
_ ( ' 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 = { } ) :
2010-02-16 12:43:27 +00:00
s = self . get_stage_dict ( cr , uid , ids , context = context )
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
2010-03-09 13:04:06 +00:00
if st in s [ section ] :
self . write ( cr , uid , [ case . id ] , { ' stage_id ' : s [ section ] [ st ] } )
2010-02-16 12:43:27 +00:00
return True
def get_stage_dict ( self , cr , uid , ids , context = { } ) :
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
2010-02-16 12:43:27 +00:00
return s
def stage_previous ( self , cr , uid , ids , context = { } ) :
s = self . get_stage_dict ( cr , uid , ids , context = context )
2010-01-11 07:07:47 +00:00
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
2010-02-16 12:43:27 +00:00
s [ section ] = dict ( [ ( v , k ) for ( k , v ) in s [ section ] . iteritems ( ) ] )
2010-03-10 15:23:52 +00:00
if st in s [ section ] :
2010-03-09 13:04:06 +00:00
self . write ( cr , uid , [ case . id ] , { ' stage_id ' : s [ section ] [ st ] } )
2010-03-10 15:23:52 +00:00
return True
2010-02-02 07:50:19 +00:00
2010-01-11 07:07:47 +00:00
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 }
2010-03-09 14:14:54 +00:00
def history ( self , cr , uid , ids , keyword , history = False , email = False , details = None , context = { } ) :
cases = self . browse ( cr , uid , ids , context = context )
2010-03-19 11:15:35 +00:00
return self . __history ( cr , uid , cases , keyword = keyword , \
history = history , email = email , details = details , \
2010-03-09 14:14:54 +00:00
context = context )
2010-01-11 07:07:47 +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-02-15 07:54:47 +00:00
model_obj = self . pool . get ( ' ir.model ' )
2008-07-22 15:11:28 +00:00
for case in cases :
2010-03-19 11:15:35 +00:00
model_ids = model_obj . search ( cr , uid , [ ( ' model ' , ' = ' , case . _name ) ] )
2008-07-22 15:11:28 +00:00
data = {
2010-03-19 11:15:35 +00:00
' name ' : keyword ,
' user_id ' : uid ,
' date ' : time . strftime ( ' % Y- % m- %d % H: % M: % S ' ) ,
' 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 ' )
2010-03-17 09:52:54 +00:00
if history :
2008-07-22 15:11:28 +00:00
obj = self . pool . get ( ' crm.case.history ' )
2009-11-05 13:42:43 +00:00
data [ ' description ' ] = details or case . description
2010-03-22 07:34:06 +00:00
data [ ' email ' ] = email or False
data [ ' email_from ' ] = ( case . user_id and case . user_id . address_id and \
case . user_id . address_id . email ) or tools . config . get ( ' email_from ' , 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
2010-03-19 11:15:35 +00:00
self . _action ( cr , uid , cases , ' draft ' )
2008-07-22 15:11:28 +00:00
return res
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 , {
2010-03-19 11:15:35 +00:00
' description ' : ' > ' + description . replace ( ' \n ' , ' \n > ' ) ,
2008-07-22 15:11:28 +00:00
} , context = context )
return True
2010-03-19 11:15:35 +00:00
def case_log ( self , cr , uid , ids , context = { } , email = False , * args ) :
2008-07-22 15:11:28 +00:00
cases = self . browse ( cr , uid , ids )
2008-12-08 17:08:40 +00:00
self . __history ( cr , uid , cases , _ ( ' Historize ' ) , history = True , email = email )
2010-03-19 11:15:35 +00:00
return self . write ( cr , uid , ids , { ' description ' : False , ' som ' : False ,
2008-07-22 15:11:28 +00:00
' 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 :
2010-03-19 11:15:35 +00:00
raise osv . except_osv ( _ ( ' Error! ' ) ,
2008-07-22 15:11:28 +00:00
_ ( ' You must put a Partner eMail to use this action! ' ) )
2009-03-24 11:58:22 +00:00
if not case . user_id :
2010-03-19 11:15:35 +00:00
raise osv . except_osv ( _ ( ' Error! ' ) ,
2009-03-24 11:58:22 +00:00
_ ( ' 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 :
2010-03-19 11:15:35 +00:00
raise osv . except_osv ( _ ( ' Error! ' ) ,
2009-02-04 09:46:57 +00:00
_ ( ' 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 ] , {
2010-03-19 11:15:35 +00:00
' description ' : False ,
' som ' : False ,
' canal_id ' : False ,
2008-07-22 15:11:28 +00:00
} )
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 :
2010-03-19 11:15:35 +00:00
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 (
2010-03-19 11:15:35 +00:00
emailfrom ,
emails ,
' [ ' + str ( case . id ) + ' ] ' + case . name ,
self . format_body ( body ) ,
reply_to = case . section_id . reply_to ,
openobject_id = str ( case . id )
2008-11-24 11:15:30 +00:00
)
2008-07-22 15:11:28 +00:00
return True
def onchange_partner_id ( self , cr , uid , ids , part , email = False ) :
if not part :
2010-03-19 11:15:35 +00:00
return { ' value ' : { ' partner_address_id ' : False ,
' email_from ' : False ,
2010-02-28 11:20:56 +00:00
} }
2008-07-22 15:11:28 +00:00
addr = self . pool . get ( ' res.partner ' ) . address_get ( cr , uid , [ part ] , [ ' contact ' ] )
2010-02-05 09:08:29 +00:00
data = { ' partner_address_id ' : addr [ ' contact ' ] }
data . update ( self . onchange_partner_address_id ( cr , uid , ids , addr [ ' contact ' ] ) [ ' value ' ] )
2010-03-19 11:15:35 +00:00
return { ' value ' : data }
2008-07-22 15:11:28 +00:00
2010-02-05 06:39:34 +00:00
def onchange_partner_address_id ( self , cr , uid , ids , add , email = False ) :
2008-07-22 15:11:28 +00:00
data = { }
2010-02-05 06:39:34 +00:00
if not add :
2010-02-05 09:08:29 +00:00
return { ' value ' : { ' email_from ' : False , ' partner_name2 ' : False } }
2010-03-19 11:15:35 +00:00
address = self . pool . get ( ' res.partner.address ' ) . browse ( cr , uid , add )
2010-02-05 09:08:29 +00:00
data [ ' email_from ' ] = address . email
2010-02-05 06:39:34 +00:00
return { ' value ' : data }
2008-07-22 15:11:28 +00:00
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 ' ) )
2010-03-19 11:15:35 +00:00
self . write ( cr , uid , ids , { ' state ' : ' done ' , ' date_closed ' : time . strftime ( ' % Y- % m- %d % H: % M: % S ' ) } )
2008-07-22 15:11:28 +00:00
#
# We use the cache of cases to keep the old case state
#
2010-03-19 11:15:35 +00:00
self . _action ( cr , uid , cases , ' done ' )
2008-07-22 15:11:28 +00:00
return True
def case_escalate ( self , cr , uid , ids , * args ) :
cases = self . browse ( cr , uid , ids )
for case in cases :
2010-03-19 11:15:35 +00:00
data = { ' active ' : True , ' user_id ' : False }
2008-07-22 15:11:28 +00:00
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 ' ) )
2010-03-19 11:15:35 +00:00
self . _action ( cr , uid , cases , ' escalate ' )
2008-07-22 15:11:28 +00:00
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 :
2010-03-19 11:15:35 +00:00
data = { ' state ' : ' open ' , ' active ' : True }
2008-07-22 15:11:28 +00:00
if not case . user_id :
data [ ' user_id ' ] = uid
self . write ( cr , uid , ids , data )
2010-03-19 11:15:35 +00:00
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 ' ) )
2010-03-19 11:15:35 +00:00
self . write ( cr , uid , ids , { ' state ' : ' cancel ' , ' active ' : True } )
self . _action ( cr , uid , cases , ' cancel ' )
2008-07-22 15:11:28 +00:00
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 ' ) )
2010-03-19 11:15:35 +00:00
self . write ( cr , uid , ids , { ' state ' : ' pending ' , ' active ' : True } )
self . _action ( cr , uid , cases , ' pending ' )
2008-07-22 15:11:28 +00:00
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 ' ) )
2010-03-19 11:15:35 +00:00
self . write ( cr , uid , ids , { ' state ' : ' draft ' , ' active ' : True } )
self . _action ( cr , uid , cases , ' draft ' )
2010-02-15 14:42:24 +00:00
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 = {
2010-03-19 11:15:35 +00:00
' name ' : fields . char ( ' Status ' , size = 64 ) ,
' som ' : fields . many2one ( ' res.partner.som ' , ' State of Mind ' ) ,
' date ' : fields . datetime ( ' Date ' ) ,
' canal_id ' : fields . many2one ( ' res.partner.canal ' , ' Channel ' ) ,
' section_id ' : fields . many2one ( ' crm.case.section ' , ' Section ' ) ,
' user_id ' : fields . many2one ( ' res.users ' , ' User Responsible ' , readonly = True ) ,
' model_id ' : fields . many2one ( ' ir.model ' , " Model " ) ,
' res_id ' : fields . integer ( ' Resource ID ' ) ,
2008-07-22 15:11:28 +00:00
}
_defaults = {
2010-03-19 11:15:35 +00:00
' date ' : lambda * a : time . strftime ( ' % Y- % m- %d % H: % M: % S ' ) ,
2008-07-22 15:11:28 +00:00
}
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 "
2010-03-19 11:15:35 +00:00
_inherits = { ' crm.case.log ' : " log_id " }
2009-03-19 12:05:49 +00:00
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 = {
2010-03-19 11:15:35 +00:00
' description ' : fields . text ( ' Description ' ) ,
' note ' : fields . function ( _note_get , method = True , string = " Description " , type = " text " ) ,
' email ' : fields . char ( ' Email ' , size = 84 ) ,
2010-03-22 07:34:06 +00:00
' email_from ' : fields . char ( ' From Email ' , size = 84 ) ,
2010-03-19 11:15:35 +00:00
' log_id ' : fields . many2one ( ' crm.case.log ' , ' Log ' , ondelete = ' cascade ' ) ,
2008-07-22 15:11:28 +00:00
}
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 = {
2010-03-19 11:15:35 +00:00
' 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 ) ,
' 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 ) :
2010-03-19 11:15:35 +00:00
return { ' value ' : { ' email ' : False } }
2009-10-29 11:27:13 +00:00
email = False
if partner :
addr = self . pool . get ( ' res.partner ' ) . address_get ( cr , uid , [ partner ] , [ ' contact ' ] )
if addr :
2010-03-19 11:15:35 +00:00
email = self . pool . get ( ' res.partner.address ' ) . read ( cr , uid , addr [ ' contact ' ] , [ ' email ' ] ) [ ' email ' ]
2009-10-29 11:27:13 +00:00
elif user :
addr = self . pool . get ( ' res.users ' ) . read ( cr , uid , user , [ ' address_id ' ] ) [ ' address_id ' ]
if addr :
2010-03-19 11:15:35 +00:00
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 ' ] )
2010-02-05 07:22:14 +00:00
model = history_line . log_id . model_id . model
model_pool = self . pool . get ( model )
case = model_pool . browse ( cr , uid , history_line . log_id . res_id )
2010-03-19 11:15:35 +00:00
body = history_line . description . replace ( ' \n ' , ' \n > ' )
2009-11-05 13:42:43 +00:00
flag = tools . email_send (
2010-03-19 11:15:35 +00:00
case . user_id . address_id . email ,
[ case . email_from ] ,
subject or ' [ ' + str ( case . id ) + ' ] ' + case . name ,
model_pool . format_body ( body ) ,
email_cc = [ email ] ,
openobject_id = str ( case . id ) ,
subtype = " html "
2009-11-05 13:42:43 +00:00
)
if flag :
2010-03-19 11:15:35 +00:00
model_pool . write ( cr , uid , case . id , { ' email_cc ' : case . email_cc and case . email_cc + ' , ' + email or email } )
2009-12-01 07:19:53 +00:00
else :
2010-03-19 11:15:35 +00:00
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
class users ( osv . osv ) :
_inherit = ' res.users '
_description = " Users "
2010-01-08 11:05:05 +00:00
_columns = {
2010-03-19 11:15:35 +00:00
' context_section_id ' : fields . many2one ( ' crm.case.section ' , ' Sales Team ' ) ,
2010-02-01 08:21:18 +00:00
}
2010-01-11 09:20:00 +00:00
users ( )
2010-03-05 10:40:27 +00:00
class res_partner ( osv . osv ) :
_inherit = ' res.partner '
_columns = {
2010-03-19 11:15:35 +00:00
' section_id ' : fields . many2one ( ' crm.case.section ' , ' Sales Team ' ) ,
2010-03-05 10:40:27 +00:00
}
res_partner ( )
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: