b6c968debacac914990241414b5b44971ec46908
[odoo/odoo.git] / addons / crm / crm_opportunity.py
1 #-*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
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 time
23 import re
24 import os
25
26 import mx.DateTime
27 import base64
28
29 from tools.translate import _
30
31 import tools
32 from osv import fields,osv,orm
33 from osv.orm import except_orm
34
35 AVAILABLE_PRIORITIES = [
36     ('5','Lowest'),
37     ('4','Low'),
38     ('3','Normal'),
39     ('2','High'),
40     ('1','Highest')
41 ]
42
43 def _links_get(self, cr, uid, context={}):
44     obj = self.pool.get('res.request.link')
45     ids = obj.search(cr, uid, [])
46     res = obj.read(cr, uid, ids, ['object', 'name'], context)
47     return [(r['object'], r['name']) for r in res]
48
49 class crm_opportunity_categ(osv.osv):
50     _name = "crm.opportunity.categ"
51     _description = "Opportunity Categories"
52     _columns = {
53             'name': fields.char('Category Name', size=64, required=True),
54             'probability': fields.float('Probability (%)', required=True),
55             'section_id': fields.many2one('crm.case.section', 'Case Section'),
56     }
57     _defaults = {
58         'probability': lambda *args: 0.0
59     }
60 crm_opportunity_categ()
61
62 class crm_opportunity(osv.osv):
63     _name = "crm.opportunity"
64     _description = "Opportunity Cases"
65     _order = "id desc"
66     _inherit = 'crm.case'  
67     _columns = {        
68         'stage_id': fields.many2one ('crm.case.stage', 'Stage', domain="[('section_id','=',section_id)]"),
69         'categ_id': fields.many2one('crm.opportunity.categ', 'Category', domain="[('section_id','=',section_id)]"),
70         'category2_id': fields.many2one('crm.case.category2', 'Category Name', domain="[('section_id','=',section_id)]"),
71         'priority': fields.selection(AVAILABLE_PRIORITIES, 'Priority'),
72         'probability': fields.float('Probability (%)'),
73         'planned_revenue': fields.float('Planned Revenue'),
74         'planned_cost': fields.float('Planned Costs'),
75         'canal_id': fields.many2one('res.partner.canal', 'Channel',help="The channels represent the different communication modes available with the customer." \
76                                                                         " With each commercial opportunity, you can indicate the canall which is this opportunity source."),
77         'som': fields.many2one('res.partner.som', 'State of Mind', help="The minds states allow to define a value scale which represents" \
78                                                                    "the partner mentality in relation to our services.The scale has" \
79                                                                    "to be created with a factor for each level from 0 (Very dissatisfied) to 10 (Extremely satisfied)."),
80         'ref' : fields.reference('Reference', selection=_links_get, size=128),
81         'ref2' : fields.reference('Reference 2', selection=_links_get, size=128),
82         'date_closed': fields.datetime('Closed', readonly=True),
83         'lead_id':fields.many2one ('crm.lead', 'Lead'),
84         'phonecall_id':fields.many2one ('crm.phonecall', 'Phonecall'),
85     }
86
87     def msg_new(self, cr, uid, msg):        
88         mailgate_obj = self.pool.get('mail.gateway')
89         msg_body = mailgate_obj.msg_body_get(msg)
90         data = {
91             'name': msg['Subject'],            
92             'email_from': msg['From'],
93             'email_cc': msg['Cc'],            
94             'user_id': False,
95             'description': msg_body['body'],
96             'history_line': [(0, 0, {'description': msg_body['body'], 'email': msg['From'] })],
97         }
98         res = mailgate_obj.partner_get(cr, uid, msg['From'])
99         if res:
100             data.update(res)
101         res = self.create(cr, uid, data)        
102         return res
103
104 crm_opportunity()
105