[TYPO] Set the right category for the Point Of Sale
[odoo/odoo.git] / addons / crm / crm.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-today OpenERP SA (<http://www.openerp.com>)
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 import base64
23 import time
24 from osv import fields
25 from osv import osv
26 import tools
27 from tools.translate import _
28
29 MAX_LEVEL = 15
30 AVAILABLE_STATES = [
31     ('draft', 'New'),
32     ('cancel', 'Cancelled'),
33     ('open', 'In Progress'),
34     ('pending', 'Pending'),
35     ('done', 'Closed')
36 ]
37
38 AVAILABLE_PRIORITIES = [
39     ('1', 'Highest'),
40     ('2', 'High'),
41     ('3', 'Normal'),
42     ('4', 'Low'),
43     ('5', 'Lowest'),
44 ]
45
46 class crm_case_channel(osv.osv):
47     _name = "crm.case.channel"
48     _description = "Channels"
49     _order = 'name'
50     _columns = {
51         'name': fields.char('Channel Name', size=64, required=True),
52         'active': fields.boolean('Active'),
53     }
54     _defaults = {
55         'active': lambda *a: 1,
56     }
57
58 class crm_case_stage(osv.osv):
59     """ Model for case stages. This models the main stages of a document
60         management flow. Main CRM objects (leads, opportunities, project 
61         issues, ...) will now use only stages, instead of state and stages.
62         Stages are for example used to display the kanban view of records.
63     """
64     _name = "crm.case.stage"
65     _description = "Stage of case"
66     _rec_name = 'name'
67     _order = "sequence"
68
69     _columns = {
70         'name': fields.char('Stage Name', size=64, required=True, translate=True),
71         'sequence': fields.integer('Sequence', help="Used to order stages. Lower is better."),
72         'probability': fields.float('Probability (%)', required=True, help="This percentage depicts the default/average probability of the Case for this stage to be a success"),
73         'on_change': fields.boolean('Change Probability Automatically', help="Setting this stage will change the probability automatically on the opportunity."),
74         'requirements': fields.text('Requirements'),
75         'section_ids':fields.many2many('crm.case.section', 'section_stage_rel', 'stage_id', 'section_id', string='Sections',
76                         help="Link between stages and sales teams. When set, this limitate the current stage to the selected sales teams."),
77         'state': fields.selection(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."),
78         'case_default': fields.boolean('Common to All Teams',
79                         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."),
80         'fold': fields.boolean('Hide in Views when Empty',
81                         help="This stage is not visible, for example in status bar or kanban view, when there are no records in that stage to display."),
82         'type': fields.selection([  ('lead','Lead'),
83                                     ('opportunity', 'Opportunity'),
84                                     ('both', 'Both')],
85                                     string='Type', size=16, required=True,
86                                     help="This field is used to distinguish stages related to Leads from stages related to Opportunities, or to specify stages available for both types."),
87     }
88
89     _defaults = {
90         'sequence': lambda *args: 1,
91         'probability': lambda *args: 0.0,
92         'state': 'draft',
93         'fold': False,
94         'type': 'both',
95     }
96
97 class crm_case_section(osv.osv):
98     """ Model for sales teams. """
99     _name = "crm.case.section"
100     _description = "Sales Teams"
101     _order = "complete_name"
102
103     def get_full_name(self, cr, uid, ids, field_name, arg, context=None):
104         return  dict(self.name_get(cr, uid, ids, context=context))
105
106     _columns = {
107         'name': fields.char('Sales Team', size=64, required=True, translate=True),
108         'complete_name': fields.function(get_full_name, type='char', size=256, readonly=True, store=True),
109         'code': fields.char('Code', size=8),
110         'active': fields.boolean('Active', help="If the active field is set to "\
111                         "true, it will allow you to hide the sales team without removing it."),
112         'allow_unlink': fields.boolean('Allow Delete', help="Allows to delete non draft cases"),
113         'change_responsible': fields.boolean('Reassign Escalated', help="When escalating to this team override the salesman with the team leader."),
114         'user_id': fields.many2one('res.users', 'Team Leader'),
115         'member_ids':fields.many2many('res.users', 'sale_member_rel', 'section_id', 'member_id', 'Team Members'),
116         '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"),
117         'parent_id': fields.many2one('crm.case.section', 'Parent Team'),
118         'child_ids': fields.one2many('crm.case.section', 'parent_id', 'Child Teams'),
119         'resource_calendar_id': fields.many2one('resource.calendar', "Working Time", help="Used to compute open days"),
120         'note': fields.text('Description'),
121         'working_hours': fields.float('Working Hours', digits=(16,2 )),
122         'stage_ids': fields.many2many('crm.case.stage', 'section_stage_rel', 'section_id', 'stage_id', 'Stages'),
123     }
124     
125     def _get_stage_common(self, cr, uid, context):
126         ids = self.pool.get('crm.case.stage').search(cr, uid, [('case_default','=',1)], context=context)
127         return ids
128
129     _defaults = {
130         'active': lambda *a: 1,
131         'allow_unlink': lambda *a: 1,
132         'stage_ids': _get_stage_common
133     }
134
135     _sql_constraints = [
136         ('code_uniq', 'unique (code)', 'The code of the sales team must be unique !')
137     ]
138
139     _constraints = [
140         (osv.osv._check_recursion, 'Error ! You cannot create recursive Sales team.', ['parent_id'])
141     ]
142
143     def name_get(self, cr, uid, ids, context=None):
144         """Overrides orm name_get method"""
145         if not isinstance(ids, list) :
146             ids = [ids]
147         res = []
148         if not ids:
149             return res
150         reads = self.read(cr, uid, ids, ['name', 'parent_id'], context)
151
152         for record in reads:
153             name = record['name']
154             if record['parent_id']:
155                 name = record['parent_id'][1] + ' / ' + name
156             res.append((record['id'], name))
157         return res
158
159 class crm_case_categ(osv.osv):
160     """ Category of Case """
161     _name = "crm.case.categ"
162     _description = "Category of Case"
163     _columns = {
164         'name': fields.char('Name', size=64, required=True, translate=True),
165         'section_id': fields.many2one('crm.case.section', 'Sales Team'),
166         'object_id': fields.many2one('ir.model', 'Object Name'),
167     }
168
169     def _find_object_id(self, cr, uid, context=None):
170         """Finds id for case object"""
171         object_id = context and context.get('object_id', False) or False
172         ids = self.pool.get('ir.model').search(cr, uid, [('id', '=', object_id)])
173         return ids and ids[0] or False
174
175     _defaults = {
176         'object_id' : _find_object_id
177     }
178
179 class crm_case_resource_type(osv.osv):
180     """ Resource Type of case """
181     _name = "crm.case.resource.type"
182     _description = "Campaign"
183     _rec_name = "name"
184     _columns = {
185         'name': fields.char('Campaign Name', size=64, required=True, translate=True),
186         'section_id': fields.many2one('crm.case.section', 'Sales Team'),
187     }
188
189
190 def _links_get(self, cr, uid, context=None):
191     """Gets links value for reference field"""
192     obj = self.pool.get('res.request.link')
193     ids = obj.search(cr, uid, [])
194     res = obj.read(cr, uid, ids, ['object', 'name'], context)
195     return [(r['object'], r['name']) for r in res]
196
197 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: