2009-12-30 10:20:44 +00:00
# -*- coding: utf-8 -*-
##############################################################################
2010-01-08 11:05:05 +00:00
#
2009-12-30 10:20:44 +00:00
# OpenERP, Open Source Management Solution
2012-05-30 11:18:31 +00:00
# Copyright (C) 2004-today OpenERP SA (<http://www.openerp.com>)
2009-12-30 10:20:44 +00:00
#
# This program is free software: you can redistribute it and/or modify
# 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.
#
# 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
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
2010-01-08 11:05:05 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2009-12-30 10:20:44 +00:00
#
##############################################################################
2012-05-29 14:23:33 +00:00
from base_status . base_stage import base_stage
2012-05-30 11:18:31 +00:00
import binascii
2010-04-28 11:41:59 +00:00
from crm import crm
2012-05-30 11:18:31 +00:00
from osv import fields , osv
import time
2011-02-15 09:42:10 +00:00
import tools
2011-03-15 13:46:32 +00:00
from tools . translate import _
2011-02-24 10:13:05 +00:00
2011-02-15 09:42:10 +00:00
CRM_CLAIM_PENDING_STATES = (
crm . AVAILABLE_STATES [ 2 ] [ 0 ] , # Cancelled
crm . AVAILABLE_STATES [ 3 ] [ 0 ] , # Done
crm . AVAILABLE_STATES [ 4 ] [ 0 ] , # Pending
)
2010-07-16 10:41:21 +00:00
2012-05-30 13:00:58 +00:00
class crm_claim_stage ( osv . osv ) :
""" Model for claim stages. This models the main stages of a claim
management flow . Main CRM objects ( leads , opportunities , project
issues , . . . ) will now use only stages , instead of state and stages .
Stages are for example used to display the kanban view of records .
2010-03-19 10:06:28 +00:00
"""
2012-05-30 13:00:58 +00:00
_name = " crm.claim.stage "
_description = " Claim stages "
_rec_name = ' name '
_order = " sequence "
_columns = {
' name ' : fields . char ( ' Stage Name ' , size = 64 , required = True , translate = True ) ,
' sequence ' : fields . integer ( ' Sequence ' , help = " Used to order stages. Lower is better. " ) ,
' section_ids ' : fields . many2many ( ' crm.case.section ' , ' section_claim_stage_rel ' , ' stage_id ' , ' section_id ' , string = ' Sections ' ,
help = " Link between stages and sales teams. When set, this limitate the current stage to the selected sales teams. " ) ,
' state ' : fields . selection ( crm . AVAILABLE_STATES , ' State ' , required = True , help = " The related state for the stage. The state of your document will automatically change regarding the selected stage. For example, if a stage is related to the state ' Close ' , when your document reaches this stage, it will be automatically have the ' closed ' state. " ) ,
2012-05-30 13:56:59 +00:00
' case_refused ' : fields . boolean ( ' Refused stage ' ,
help = ' Refused stages are specific stages for done. ' ) ,
2012-05-30 13:00:58 +00:00
' case_default ' : fields . boolean ( ' Common to All Teams ' ,
help = " If you check this field, this stage will be proposed by default on each sales team. It will not assign this stage to existing teams. " ) ,
' fold ' : fields . boolean ( ' Hide in Views when Empty ' ,
help = " This stage is not visible, for example in status bar or kanban view, when there are no records in that stage to display. " ) ,
}
_defaults = {
' sequence ' : lambda * args : 1 ,
' state ' : ' draft ' ,
' fold ' : False ,
2012-05-30 13:56:59 +00:00
' case_refused ' : False ,
2012-05-30 13:00:58 +00:00
}
2011-05-17 05:56:46 +00:00
2012-05-29 14:23:33 +00:00
class crm_claim ( base_stage , osv . osv ) :
2012-05-22 15:07:06 +00:00
""" Crm claim
2010-03-19 10:06:28 +00:00
"""
2009-12-30 10:20:44 +00:00
_name = " crm.claim "
2010-07-22 11:00:56 +00:00
_description = " Claim "
2010-11-27 16:37:12 +00:00
_order = " priority,date desc "
2011-07-22 18:23:37 +00:00
_inherit = [ ' mail.thread ' ]
2012-07-03 13:14:53 +00:00
_mail_compose_message = True
2010-01-08 11:05:05 +00:00
_columns = {
2011-02-07 05:51:57 +00:00
' id ' : fields . integer ( ' ID ' , readonly = True ) ,
' name ' : fields . char ( ' Claim Subject ' , size = 128 , required = True ) ,
2011-11-23 13:24:10 +00:00
' active ' : fields . boolean ( ' Active ' ) ,
2010-11-27 16:37:12 +00:00
' action_next ' : fields . char ( ' Next Action ' , size = 200 ) ,
' date_action_next ' : fields . datetime ( ' Next Action Date ' ) ,
2011-02-07 05:51:57 +00:00
' description ' : fields . text ( ' Description ' ) ,
' resolution ' : fields . text ( ' Resolution ' ) ,
' create_date ' : fields . datetime ( ' Creation Date ' , readonly = True ) ,
' write_date ' : fields . datetime ( ' Update Date ' , readonly = True ) ,
' date_deadline ' : fields . date ( ' Deadline ' ) ,
' date_closed ' : fields . datetime ( ' Closed ' , readonly = True ) ,
2011-12-09 06:03:08 +00:00
' date ' : fields . datetime ( ' Claim Date ' , select = True ) ,
2011-02-07 05:51:57 +00:00
' ref ' : fields . reference ( ' Reference ' , selection = crm . _links_get , size = 128 ) ,
2010-05-06 06:44:00 +00:00
' categ_id ' : fields . many2one ( ' crm.case.categ ' , ' Category ' , \
2010-03-22 10:40:26 +00:00
domain = " [( ' section_id ' , ' = ' ,section_id), \
2011-02-07 05:51:57 +00:00
( ' object_id.model ' , ' = ' , ' crm.claim ' ) ] " ),
' priority ' : fields . selection ( crm . AVAILABLE_PRIORITIES , ' Priority ' ) ,
2010-09-28 21:53:48 +00:00
' type_action ' : fields . selection ( [ ( ' correction ' , ' Corrective Action ' ) , ( ' prevention ' , ' Preventive Action ' ) ] , ' Action Type ' ) ,
2011-02-07 05:51:57 +00:00
' user_id ' : fields . many2one ( ' res.users ' , ' Responsible ' ) ,
' user_fault ' : fields . char ( ' Trouble Responsible ' , size = 64 ) ,
2010-05-06 06:44:00 +00:00
' section_id ' : fields . many2one ( ' crm.case.section ' , ' Sales Team ' , \
2010-05-12 13:16:01 +00:00
select = True , help = " Sales team to which Case belongs to. " \
" Define Responsible user and Email account for " \
2011-02-07 05:51:57 +00:00
" mail gateway. " ) ,
' company_id ' : fields . many2one ( ' res.company ' , ' Company ' ) ,
' partner_id ' : fields . many2one ( ' res.partner ' , ' Partner ' ) ,
' email_cc ' : fields . text ( ' Watchers Emails ' , size = 252 , help = " These email addresses will be added to the CC field of all inbound and outbound emails for this record before being sent. Separate multiple email addresses with a comma " ) ,
' email_from ' : fields . char ( ' Email ' , size = 128 , help = " These people will receive email. " ) ,
' partner_phone ' : fields . char ( ' Phone ' , size = 32 ) ,
2012-05-30 13:00:58 +00:00
' stage_id ' : fields . many2one ( ' crm.claim.stage ' , ' Stage ' ,
2012-05-30 11:18:31 +00:00
domain = " [ ' | ' , ( ' section_ids ' , ' = ' , section_id), ( ' case_default ' , ' = ' , True)] " ) ,
2011-02-07 05:51:57 +00:00
' cause ' : fields . text ( ' Root Cause ' ) ,
2012-05-21 15:56:32 +00:00
' state ' : fields . related ( ' stage_id ' , ' state ' , type = " selection " , store = True ,
selection = crm . AVAILABLE_STATES , string = " State " , readonly = True ,
help = ' The state is set to \' Draft \' , when a case is created. \
If the case is in progress the state is set to \' Open \' . \
When the case is over , the state is set to \' Done \' . \
If the case needs to be reviewed then the state is \
set to \' Pending \' . ' ) ,
2010-01-11 11:11:20 +00:00
}
2011-02-07 05:51:57 +00:00
2010-01-11 11:11:20 +00:00
_defaults = {
2012-05-25 07:33:12 +00:00
' user_id ' : lambda s , cr , uid , c : s . _get_default_user ( cr , uid , c ) ,
' partner_id ' : lambda s , cr , uid , c : s . _get_default_partner ( cr , uid , c ) ,
' email_from ' : lambda s , cr , uid , c : s . _get_default_email ( cr , uid , c ) ,
' section_id ' : lambda s , cr , uid , c : s . _get_default_section_id ( cr , uid , c ) ,
2012-05-24 16:18:56 +00:00
' date ' : fields . datetime . now ,
2011-02-07 05:51:57 +00:00
' company_id ' : lambda s , cr , uid , c : s . pool . get ( ' res.company ' ) . _company_default_get ( cr , uid , ' crm.case ' , context = c ) ,
2010-12-09 12:56:35 +00:00
' priority ' : lambda * a : crm . AVAILABLE_PRIORITIES [ 2 ] [ 0 ] ,
2011-11-23 13:24:10 +00:00
' active ' : lambda * a : 1
2009-12-30 10:20:44 +00:00
}
2011-02-07 05:51:57 +00:00
2012-05-24 16:18:56 +00:00
def stage_find ( self , cr , uid , cases , section_id , domain = [ ] , order = ' sequence ' , context = None ) :
""" Override of the base.stage method
Parameter of the stage search taken from the lead :
- section_id : if set , stages must belong to this section or
be a default case
"""
if isinstance ( cases , ( int , long ) ) :
cases = self . browse ( cr , uid , cases , context = context )
2012-05-31 14:26:47 +00:00
# collect all section_ids
section_ids = [ ]
2012-05-24 16:18:56 +00:00
if section_id :
2012-05-31 14:26:47 +00:00
section_ids . append ( section_id )
for claim in cases :
if claim . section_id :
section_ids . append ( claim . section_id . id )
# OR all section_ids and OR with case_default
search_domain = [ ]
if section_ids :
search_domain + = [ ( ' | ' ) ] * len ( section_ids )
for section_id in section_ids :
search_domain . append ( ( ' section_ids ' , ' = ' , section_id ) )
search_domain . append ( ( ' case_default ' , ' = ' , True ) )
# AND with the domain in parameter
search_domain + = list ( domain )
# perform search, return the first found
stage_ids = self . pool . get ( ' crm.claim.stage ' ) . search ( cr , uid , search_domain , order = order , context = context )
2012-05-24 16:18:56 +00:00
if stage_ids :
return stage_ids [ 0 ]
return False
2012-03-21 09:28:23 +00:00
2012-04-30 10:53:49 +00:00
def create ( self , cr , uid , vals , context = None ) :
obj_id = super ( crm_claim , self ) . create ( cr , uid , vals , context )
self . create_send_note ( cr , uid , [ obj_id ] , context = context )
return obj_id
2012-05-30 13:56:59 +00:00
def case_refuse ( self , cr , uid , ids , context = None ) :
""" Mark the case as refused: state=done and case_refused=True """
for lead in self . browse ( cr , uid , ids ) :
stage_id = self . stage_find ( cr , uid , [ lead ] , lead . section_id . id or False , [ ' & ' , ( ' state ' , ' = ' , ' done ' ) , ( ' case_refused ' , ' = ' , True ) ] , context = context )
if stage_id :
self . case_set ( cr , uid , [ lead . id ] , values_to_update = { } , new_stage_id = stage_id , context = context )
return self . case_refuse_send_note ( cr , uid , ids , context = context )
2012-04-30 10:53:49 +00:00
2010-06-24 13:15:38 +00:00
def onchange_partner_id ( self , cr , uid , ids , part , email = False ) :
""" This function returns value of partner address based on partner
2011-07-22 18:23:37 +00:00
: param part : Partner ' s id
: param email : ignored
2010-06-24 13:15:38 +00:00
"""
if not part :
2012-03-06 12:08:33 +00:00
return { ' value ' : { ' email_from ' : False ,
' partner_phone ' : False
}
}
2012-03-07 05:47:51 +00:00
address = self . pool . get ( ' res.partner ' ) . browse ( cr , uid , part )
2011-11-14 22:08:36 +00:00
return { ' value ' : { ' email_from ' : address . email , ' partner_phone ' : address . phone } }
2012-06-04 14:12:54 +00:00
# -------------------------------------------------------
# Mail gateway
# -------------------------------------------------------
2011-07-22 18:23:37 +00:00
def message_new ( self , cr , uid , msg , custom_values = None , context = None ) :
2012-06-04 14:12:54 +00:00
""" Overrides mail_thread message_new that is called by the mailgateway
through message_process .
This override updates the document according to the email .
"""
if custom_values is None : custom_values = { }
custom_values . update ( {
' name ' : msg . get ( ' subject ' ) or _ ( " No Subject " ) ,
' description ' : msg . get ( ' body_text ' ) ,
' email_from ' : msg . get ( ' from ' ) ,
2011-02-15 09:42:10 +00:00
' email_cc ' : msg . get ( ' cc ' ) ,
2012-06-04 14:12:54 +00:00
} )
if msg . get ( ' priority ' ) :
custom_values [ ' priority ' ] = msg . get ( ' priority ' )
return super ( crm_claim , self ) . message_new ( cr , uid , msg , custom_values = custom_values , context = context )
def message_update ( self , cr , uid , ids , msg , update_vals = None , context = None ) :
""" Overrides mail_thread message_update that is called by the mailgateway
through message_process .
This method updates the document according to the email .
"""
2011-02-15 09:42:10 +00:00
if isinstance ( ids , ( str , int , long ) ) :
ids = [ ids ]
2012-06-04 14:12:54 +00:00
if update_vals is None : update_vals = { }
2011-07-22 18:23:37 +00:00
2011-02-15 09:42:10 +00:00
if msg . get ( ' priority ' ) in dict ( crm . AVAILABLE_PRIORITIES ) :
2012-06-04 14:12:54 +00:00
update_vals [ ' priority ' ] = msg . get ( ' priority ' )
2011-02-15 09:42:10 +00:00
maps = {
' cost ' : ' planned_cost ' ,
' revenue ' : ' planned_revenue ' ,
' probability ' : ' probability '
}
2011-07-22 18:23:37 +00:00
for line in msg [ ' body_text ' ] . split ( ' \n ' ) :
2011-02-15 09:42:10 +00:00
line = line . strip ( )
res = tools . misc . command_re . match ( line )
if res and maps . get ( res . group ( 1 ) . lower ( ) ) :
key = maps . get ( res . group ( 1 ) . lower ( ) )
2012-06-04 14:12:54 +00:00
update_vals [ key ] = res . group ( 2 ) . lower ( )
return super ( crm_claim , self ) . message_update ( cr , uid , ids , msg , update_vals = update_vals , context = context )
2011-02-15 09:42:10 +00:00
2012-06-01 09:23:52 +00:00
# ---------------------------------------------------
# OpenChatter methods and notifications
# ---------------------------------------------------
def case_get_note_msg_prefix ( self , cr , uid , id , context = None ) :
""" Override of default prefix for notifications. """
return ' Claim '
2012-06-01 11:36:25 +00:00
def create_send_note ( self , cr , uid , ids , context = None ) :
2012-06-01 14:40:59 +00:00
msg = _ ( ' Claim has been <b>created</b>. ' )
2012-08-17 10:03:02 +00:00
return self . message_post ( cr , uid , ids , body = msg , context = context )
2012-06-01 11:36:25 +00:00
2012-06-01 09:23:52 +00:00
def case_refuse_send_note ( self , cr , uid , ids , context = None ) :
2012-06-01 14:40:59 +00:00
msg = _ ( ' Claim has been <b>refused</b>. ' )
2012-08-17 10:03:02 +00:00
return self . message_post ( cr , uid , ids , body = msg , context = context )
2012-06-01 09:23:52 +00:00
def stage_set_send_note ( self , cr , uid , ids , stage_id , context = None ) :
""" Override of the (void) default notification method. """
stage_name = self . pool . get ( ' crm.claim.stage ' ) . name_get ( cr , uid , [ stage_id ] , context = context ) [ 0 ] [ 1 ]
2012-08-17 10:03:02 +00:00
return self . message_post ( cr , uid , ids , body = _ ( " Stage changed to <b> %s </b>. " ) % ( stage_name ) , context = context )
2012-06-01 09:23:52 +00:00
2011-05-17 05:56:46 +00:00
class res_partner ( osv . osv ) :
_inherit = ' res.partner '
_columns = {
' claims_ids ' : fields . one2many ( ' crm.claim ' , ' partner_id ' , ' Claims ' ) ,
}
2010-12-14 13:19:59 +00:00
2010-04-28 11:41:59 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: