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 ##############################################################################
24 from lxml import etree
25 from openerp.osv import fields
26 from openerp.osv import osv
27 from openerp import tools
28 from openerp.tools.translate import _
33 ('cancel', 'Cancelled'),
34 ('open', 'In Progress'),
35 ('pending', 'Pending'),
39 AVAILABLE_PRIORITIES = [
47 class crm_case_channel(osv.osv):
48 _name = "crm.case.channel"
49 _description = "Channels"
52 'name': fields.char('Channel Name', size=64, required=True),
53 'active': fields.boolean('Active'),
56 'active': lambda *a: 1,
59 class crm_case_stage(osv.osv):
60 """ Model for case stages. This models the main stages of a document
61 management flow. Main CRM objects (leads, opportunities, project
62 issues, ...) will now use only stages, instead of state and stages.
63 Stages are for example used to display the kanban view of records.
65 _name = "crm.case.stage"
66 _description = "Stage of case"
71 'name': fields.char('Stage Name', size=64, required=True, translate=True),
72 'sequence': fields.integer('Sequence', help="Used to order stages. Lower is better."),
73 'probability': fields.float('Probability (%)', required=True, help="This percentage depicts the default/average probability of the Case for this stage to be a success"),
74 'on_change': fields.boolean('Change Probability Automatically', help="Setting this stage will change the probability automatically on the opportunity."),
75 'requirements': fields.text('Requirements'),
76 'section_ids':fields.many2many('crm.case.section', 'section_stage_rel', 'stage_id', 'section_id', string='Sections',
77 help="Link between stages and sales teams. When set, this limitate the current stage to the selected sales teams."),
78 'state': fields.selection(AVAILABLE_STATES, 'Related Status', required=True,
79 help="The status of your document will automatically change regarding the selected stage. " \
80 "For example, if a stage is related to the status 'Close', when your document reaches this stage, it is automatically closed."),
81 'case_default': fields.boolean('Default to New Sales Team',
82 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."),
83 'fold': fields.boolean('Fold by Default',
84 help="This stage is not visible, for example in status bar or kanban view, when there are no records in that stage to display."),
85 'type': fields.selection([ ('lead','Lead'),
86 ('opportunity', 'Opportunity'),
88 string='Type', size=16, required=True,
89 help="This field is used to distinguish stages related to Leads from stages related to Opportunities, or to specify stages available for both types."),
93 'sequence': lambda *args: 1,
94 'probability': lambda *args: 0.0,
101 class crm_case_section(osv.osv):
102 """ Model for sales teams. """
103 _name = "crm.case.section"
104 _inherits = {'mail.alias': 'alias_id'}
105 _inherit = "mail.thread"
106 _description = "Sales Teams"
107 _order = "complete_name"
109 def get_full_name(self, cr, uid, ids, field_name, arg, context=None):
110 return dict(self.name_get(cr, uid, ids, context=context))
113 'name': fields.char('Sales Team', size=64, required=True, translate=True),
114 'complete_name': fields.function(get_full_name, type='char', size=256, readonly=True, store=True),
115 'code': fields.char('Code', size=8),
116 'active': fields.boolean('Active', help="If the active field is set to "\
117 "true, it will allow you to hide the sales team without removing it."),
118 'change_responsible': fields.boolean('Reassign Escalated', help="When escalating to this team override the salesman with the team leader."),
119 'user_id': fields.many2one('res.users', 'Team Leader'),
120 'member_ids':fields.many2many('res.users', 'sale_member_rel', 'section_id', 'member_id', 'Team Members'),
121 'reply_to': fields.char('Reply-To', size=64, help="The email address put in the 'Reply-To' of all emails sent by OpenERP about cases in this sales team"),
122 'parent_id': fields.many2one('crm.case.section', 'Parent Team'),
123 'child_ids': fields.one2many('crm.case.section', 'parent_id', 'Child Teams'),
124 'resource_calendar_id': fields.many2one('resource.calendar', "Working Time", help="Used to compute open days"),
125 'note': fields.text('Description'),
126 'working_hours': fields.float('Working Hours', digits=(16,2 )),
127 'stage_ids': fields.many2many('crm.case.stage', 'section_stage_rel', 'section_id', 'stage_id', 'Stages'),
128 'alias_id': fields.many2one('mail.alias', 'Alias', ondelete="cascade", required=True,
129 help="The email address associated with this team. New emails received will automatically "
130 "create new leads assigned to the team."),
133 def _get_stage_common(self, cr, uid, context):
134 ids = self.pool.get('crm.case.stage').search(cr, uid, [('case_default','=',1)], context=context)
139 'stage_ids': _get_stage_common,
140 'alias_domain': False, # always hide alias during creation
144 ('code_uniq', 'unique (code)', 'The code of the sales team must be unique !')
148 (osv.osv._check_recursion, 'Error ! You cannot create recursive Sales team.', ['parent_id'])
151 def name_get(self, cr, uid, ids, context=None):
152 """Overrides orm name_get method"""
153 if not isinstance(ids, list) :
158 reads = self.read(cr, uid, ids, ['name', 'parent_id'], context)
161 name = record['name']
162 if record['parent_id']:
163 name = record['parent_id'][1] + ' / ' + name
164 res.append((record['id'], name))
167 def create(self, cr, uid, vals, context=None):
168 mail_alias = self.pool.get('mail.alias')
169 if not vals.get('alias_id'):
170 vals.pop('alias_name', None) # prevent errors during copy()
171 alias_id = mail_alias.create_unique_alias(cr, uid,
172 {'alias_name': vals['name']},
173 model_name="crm.lead",
175 vals['alias_id'] = alias_id
176 res = super(crm_case_section, self).create(cr, uid, vals, context)
177 mail_alias.write(cr, uid, [vals['alias_id']], {'alias_defaults': {'section_id': res, 'type':'lead'}}, context)
180 def unlink(self, cr, uid, ids, context=None):
181 # Cascade-delete mail aliases as well, as they should not exist without the sales team.
182 mail_alias = self.pool.get('mail.alias')
183 alias_ids = [team.alias_id.id for team in self.browse(cr, uid, ids, context=context) if team.alias_id ]
184 res = super(crm_case_section, self).unlink(cr, uid, ids, context=context)
185 mail_alias.unlink(cr, uid, alias_ids, context=context)
188 class crm_case_categ(osv.osv):
189 """ Category of Case """
190 _name = "crm.case.categ"
191 _description = "Category of Case"
193 'name': fields.char('Name', size=64, required=True, translate=True),
194 'section_id': fields.many2one('crm.case.section', 'Sales Team'),
195 'object_id': fields.many2one('ir.model', 'Object Name'),
197 def _find_object_id(self, cr, uid, context=None):
198 """Finds id for case object"""
199 context = context or {}
200 object_id = context.get('object_id', False)
201 ids = self.pool.get('ir.model').search(cr, uid, ['|',('id', '=', object_id),('model', '=', context.get('object_name', False))])
202 return ids and ids[0] or False
204 'object_id' : _find_object_id
207 class crm_case_resource_type(osv.osv):
208 """ Resource Type of case """
209 _name = "crm.case.resource.type"
210 _description = "Campaign"
213 'name': fields.char('Campaign Name', size=64, required=True, translate=True),
214 'section_id': fields.many2one('crm.case.section', 'Sales Team'),
217 def _links_get(self, cr, uid, context=None):
218 """Gets links value for reference field"""
219 obj = self.pool.get('res.request.link')
220 ids = obj.search(cr, uid, [])
221 res = obj.read(cr, uid, ids, ['object', 'name'], context)
222 return [(r['object'], r['name']) for r in res]
224 class crm_payment_mode(osv.osv):
225 """ Payment Mode for Fund """
226 _name = "crm.payment.mode"
227 _description = "CRM Payment Mode"
229 'name': fields.char('Name', size=64, required=True),
230 'section_id': fields.many2one('crm.case.section', 'Sales Team'),
233 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: