1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-today OpenERP SA (<http://www.openerp.com>)
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
22 from base_status.base_stage import base_stage
25 from crm import wizard
26 from osv import fields, osv
29 from tools.translate import _
31 wizard.mail_compose_message.SUPPORTED_MODELS.append('crm.claim')
33 CRM_CLAIM_PENDING_STATES = (
34 crm.AVAILABLE_STATES[2][0], # Cancelled
35 crm.AVAILABLE_STATES[3][0], # Done
36 crm.AVAILABLE_STATES[4][0], # Pending
39 class crm_claim_stage(osv.osv):
40 """ Model for claim stages. This models the main stages of a claim
41 management flow. Main CRM objects (leads, opportunities, project
42 issues, ...) will now use only stages, instead of state and stages.
43 Stages are for example used to display the kanban view of records.
45 _name = "crm.claim.stage"
46 _description = "Claim stages"
51 'name': fields.char('Stage Name', size=64, required=True, translate=True),
52 'sequence': fields.integer('Sequence', help="Used to order stages. Lower is better."),
53 'section_ids':fields.many2many('crm.case.section', 'section_claim_stage_rel', 'stage_id', 'section_id', string='Sections',
54 help="Link between stages and sales teams. When set, this limitate the current stage to the selected sales teams."),
55 '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."),
56 'case_refused': fields.boolean('Refused stage',
57 help='Refused stages are specific stages for done.'),
58 'case_default': fields.boolean('Common to All Teams',
59 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."),
60 'fold': fields.boolean('Hide in Views when Empty',
61 help="This stage is not visible, for example in status bar or kanban view, when there are no records in that stage to display."),
65 'sequence': lambda *args: 1,
68 'case_refused': False,
71 class crm_claim(base_stage, osv.osv):
75 _description = "Claim"
76 _order = "priority,date desc"
77 _inherit = ['mail.thread']
79 'id': fields.integer('ID', readonly=True),
80 'name': fields.char('Claim Subject', size=128, required=True),
81 'active': fields.boolean('Active'),
82 'action_next': fields.char('Next Action', size=200),
83 'date_action_next': fields.datetime('Next Action Date'),
84 'description': fields.text('Description'),
85 'resolution': fields.text('Resolution'),
86 'create_date': fields.datetime('Creation Date' , readonly=True),
87 'write_date': fields.datetime('Update Date' , readonly=True),
88 'date_deadline': fields.date('Deadline'),
89 'date_closed': fields.datetime('Closed', readonly=True),
90 'date': fields.datetime('Claim Date', select=True),
91 'ref' : fields.reference('Reference', selection=crm._links_get, size=128),
92 'categ_id': fields.many2one('crm.case.categ', 'Category', \
93 domain="[('section_id','=',section_id),\
94 ('object_id.model', '=', 'crm.claim')]"),
95 'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority'),
96 'type_action': fields.selection([('correction','Corrective Action'),('prevention','Preventive Action')], 'Action Type'),
97 'user_id': fields.many2one('res.users', 'Responsible'),
98 'user_fault': fields.char('Trouble Responsible', size=64),
99 'section_id': fields.many2one('crm.case.section', 'Sales Team', \
100 select=True, help="Sales team to which Case belongs to."\
101 "Define Responsible user and Email account for"\
103 'company_id': fields.many2one('res.company', 'Company'),
104 'partner_id': fields.many2one('res.partner', 'Partner'),
105 '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"),
106 'email_from': fields.char('Email', size=128, help="These people will receive email."),
107 'partner_phone': fields.char('Phone', size=32),
108 'stage_id': fields.many2one ('crm.claim.stage', 'Stage',
109 domain="['|', ('section_ids', '=', section_id), ('case_default', '=', True)]"),
110 'cause': fields.text('Root Cause'),
111 'state': fields.related('stage_id', 'state', type="selection", store=True,
112 selection=crm.AVAILABLE_STATES, string="State", readonly=True,
113 help='The state is set to \'Draft\', when a case is created.\
114 If the case is in progress the state is set to \'Open\'.\
115 When the case is over, the state is set to \'Done\'.\
116 If the case needs to be reviewed then the state is \
117 set to \'Pending\'.'),
121 'user_id': lambda s, cr, uid, c: s._get_default_user(cr, uid, c),
122 'partner_id': lambda s, cr, uid, c: s._get_default_partner(cr, uid, c),
123 'email_from': lambda s, cr, uid, c: s._get_default_email(cr, uid, c),
124 'section_id': lambda s, cr, uid, c: s._get_default_section_id(cr, uid, c),
125 'date': fields.datetime.now,
126 'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.case', context=c),
127 'priority': lambda *a: crm.AVAILABLE_PRIORITIES[2][0],
128 'active': lambda *a: 1
131 def stage_find(self, cr, uid, cases, section_id, domain=[], order='sequence', context=None):
132 """ Override of the base.stage method
133 Parameter of the stage search taken from the lead:
134 - section_id: if set, stages must belong to this section or
137 if isinstance(cases, (int, long)):
138 cases = self.browse(cr, uid, cases, context=context)
139 # collect all section_ids
142 section_ids.append(section_id)
145 section_ids.append(claim.section_id.id)
146 # OR all section_ids and OR with case_default
149 search_domain += [('|')] * len(section_ids)
150 for section_id in section_ids:
151 search_domain.append(('section_ids', '=', section_id))
152 search_domain.append(('case_default', '=', True))
153 # AND with the domain in parameter
154 search_domain += list(domain)
155 # perform search, return the first found
156 stage_ids = self.pool.get('crm.claim.stage').search(cr, uid, search_domain, order=order, context=context)
161 def create(self, cr, uid, vals, context=None):
162 obj_id = super(crm_claim, self).create(cr, uid, vals, context)
163 self.create_send_note(cr, uid, [obj_id], context=context)
166 def case_refuse(self, cr, uid, ids, context=None):
167 """ Mark the case as refused: state=done and case_refused=True """
168 for lead in self.browse(cr, uid, ids):
169 stage_id = self.stage_find(cr, uid, [lead], lead.section_id.id or False, ['&', ('state', '=', 'done'), ('case_refused', '=', True)], context=context)
171 self.case_set(cr, uid, [lead.id], values_to_update={}, new_stage_id=stage_id, context=context)
172 return self.case_refuse_send_note(cr, uid, ids, context=context)
174 def onchange_partner_id(self, cr, uid, ids, part, email=False):
175 """This function returns value of partner address based on partner
176 :param part: Partner's id
177 :param email: ignored
180 return {'value': {'email_from': False,
181 'partner_phone': False
184 address = self.pool.get('res.partner').browse(cr, uid, part)
185 return {'value': {'email_from': address.email, 'partner_phone': address.phone}}
187 def message_new(self, cr, uid, msg, custom_values=None, context=None):
188 """Automatically called when new email message arrives"""
189 res_id = super(crm_claim,self).message_new(cr, uid, msg, custom_values=custom_values, context=context)
190 subject = msg.get('subject')
191 body = msg.get('body_text')
192 msg_from = msg.get('from')
193 priority = msg.get('priority')
196 'email_from': msg_from,
197 'email_cc': msg.get('cc'),
202 vals['priority'] = priority
203 vals.update(self.message_partner_by_email(cr, uid, msg.get('from', False)))
204 self.write(cr, uid, [res_id], vals, context=context)
207 def message_update(self, cr, uid, ids, msg, vals={}, default_act='pending', context=None):
208 if isinstance(ids, (str, int, long)):
211 res_id = super(crm_claim,self).message_update(cr, uid, ids, msg, context=context)
213 if msg.get('priority') in dict(crm.AVAILABLE_PRIORITIES):
214 vals['priority'] = msg.get('priority')
217 'cost':'planned_cost',
218 'revenue': 'planned_revenue',
219 'probability':'probability'
222 for line in msg['body_text'].split('\n'):
224 res = tools.misc.command_re.match(line)
225 if res and maps.get(res.group(1).lower()):
226 key = maps.get(res.group(1).lower())
227 vls[key] = res.group(2).lower()
230 # Unfortunately the API is based on lists
231 # but we want to update the state based on the
232 # previous state, so we have to loop:
233 for case in self.browse(cr, uid, ids, context=context):
235 if case.state in CRM_CLAIM_PENDING_STATES:
236 values.update(state=crm.AVAILABLE_STATES[1][0]) #re-open
237 res = self.write(cr, uid, [case.id], values, context=context)
240 # ---------------------------------------------------
241 # OpenChatter methods and notifications
242 # ---------------------------------------------------
244 def case_get_note_msg_prefix(self, cr, uid, id, context=None):
245 """ Override of default prefix for notifications. """
248 def create_send_note(self, cr, uid, ids, context=None):
249 msg = _('Claim has been <b>created</b>.')
250 return self.message_append_note(cr, uid, ids, body=msg, context=context)
252 def case_refuse_send_note(self, cr, uid, ids, context=None):
253 msg = _('Claim has been <b>refused</b>.')
254 return self.message_append_note(cr, uid, ids, body=msg, context=context)
256 def stage_set_send_note(self, cr, uid, ids, stage_id, context=None):
257 """ Override of the (void) default notification method. """
258 stage_name = self.pool.get('crm.claim.stage').name_get(cr, uid, [stage_id], context=context)[0][1]
259 return self.message_append_note(cr, uid, ids, body= _("Stage changed to <b>%s</b>.") % (stage_name), context=context)
262 class res_partner(osv.osv):
263 _inherit = 'res.partner'
265 'claims_ids': fields.one2many('crm.claim', 'partner_id', 'Claims'),
268 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: