[IMP] crm*: update crm modules to new mail module (wip)
[odoo/odoo.git] / addons / crm / crm_meeting.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 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 from base_calendar import base_calendar
23 from crm import crm_case
24 from osv import fields, osv
25 from tools.translate import _
26 import logging
27
28 class crm_lead(crm_case, osv.osv):
29     """ CRM Leads """
30     _name = 'crm.lead'
31 crm_lead()
32
33 class crm_phonecall(crm_case, osv.osv):
34     """ CRM Phonecall """
35     _name = 'crm.phonecall'
36 crm_phonecall()
37
38
39 class crm_meeting(crm_case, osv.osv):
40     """ CRM Meeting Cases """
41
42     _name = 'crm.meeting'
43     _description = "Meeting"
44     _order = "id desc"
45     _inherit = ['mail.thread',"calendar.event"]
46     _columns = {
47         # From crm.case
48         'name': fields.char('Summary', size=124, required=True, states={'done': [('readonly', True)]}),
49         'partner_id': fields.many2one('res.partner', 'Partner', states={'done': [('readonly', True)]}),
50         'partner_address_id': fields.many2one('res.partner.address', 'Partner Contact', \
51                                  domain="[('partner_id','=',partner_id)]", states={'done': [('readonly', True)]}),
52         'section_id': fields.many2one('crm.case.section', 'Sales Team', states={'done': [('readonly', True)]}, \
53                         select=True, help='Sales team to which Case belongs to.'),
54         'email_from': fields.char('Email', size=128, states={'done': [('readonly', True)]}, help="These people will receive email."),
55         'id': fields.integer('ID'),
56         'create_date': fields.datetime('Creation Date' , readonly=True),
57         'write_date': fields.datetime('Write Date' , readonly=True),
58         'date_action_last': fields.datetime('Last Action', readonly=1),
59         'date_action_next': fields.datetime('Next Action', readonly=1),
60         # Meeting fields
61         'categ_id': fields.many2one('crm.case.categ', 'Meeting Type', \
62                         domain="[('object_id.model', '=', 'crm.meeting')]", \
63             ),
64         'phonecall_id': fields.many2one ('crm.phonecall', 'Phonecall'),
65         'opportunity_id': fields.many2one ('crm.lead', 'Opportunity', domain="[('type', '=', 'opportunity')]"),
66         'attendee_ids': fields.many2many('calendar.attendee', 'meeting_attendee_rel',\
67                                  'event_id', 'attendee_id', 'Attendees', states={'done': [('readonly', True)]}),
68         'date_closed': fields.datetime('Closed', readonly=True),
69         'date_deadline': fields.datetime('Deadline', states={'done': [('readonly', True)]}),
70         'message_ids': fields.one2many('mail.message', 'res_id', 'Messages', domain=[('model','=',_name)]),
71         'state': fields.selection([('open', 'Confirmed'),
72                                     ('draft', 'Unconfirmed'),
73                                     ('cancel', 'Cancelled'),
74                                     ('done', 'Done')], 'State', \
75                                     size=16, readonly=True),
76     }
77     _defaults = {
78         'state': 'draft',
79         'active': 1,
80         'user_id': lambda self, cr, uid, ctx: uid,
81     }
82
83     def case_open(self, cr, uid, ids, *args):
84         """Confirms meeting
85         @param self: The object pointer
86         @param cr: the current row, from the database cursor,
87         @param uid: the current user’s ID for security checks,
88         @param ids: List of Meeting Ids
89         @param *args: Tuple Value for additional Params
90         """
91         res = super(crm_meeting, self).case_open(cr, uid, ids, args)
92         for (id, name) in self.name_get(cr, uid, ids):
93             message = _("The meeting '%s' has been confirmed.") % name
94             id=base_calendar.base_calendar_id2real_id(id)
95             self.log(cr, uid, id, message)
96         return res
97
98 crm_meeting()
99
100 class calendar_attendee(osv.osv):
101     """ Calendar Attendee """
102
103     _inherit = 'calendar.attendee'
104     _description = 'Calendar Attendee'
105
106     def _compute_data(self, cr, uid, ids, name, arg, context=None):
107        """
108         @param self: The object pointer
109         @param cr: the current row, from the database cursor,
110         @param uid: the current user’s ID for security checks,
111         @param ids: List of compute data’s IDs
112         @param context: A standard dictionary for contextual values
113         """
114        name = name[0]
115        result = super(calendar_attendee, self)._compute_data(cr, uid, ids, name, arg, context=context)
116
117        for attdata in self.browse(cr, uid, ids, context=context):
118             id = attdata.id
119             result[id] = {}
120             if name == 'categ_id':
121                 if attdata.ref and 'categ_id' in attdata.ref._columns:
122                     result[id][name] = (attdata.ref.categ_id.id, attdata.ref.categ_id.name,)
123                 else:
124                     result[id][name] = False
125        return result
126
127     _columns = {
128         'categ_id': fields.function(_compute_data, method=True, \
129                         string='Event Type', type="many2one", \
130                         relation="crm.case.categ", multi='categ_id'),
131     }
132
133 calendar_attendee()
134
135 class res_users(osv.osv):
136     _name = 'res.users'
137     _inherit = 'res.users'
138
139     def create(self, cr, uid, data, context=None):
140         user_id = super(res_users, self).create(cr, uid, data, context=context)
141         data_obj = self.pool.get('ir.model.data')
142         try:
143             data_id = data_obj._get_id(cr, uid, 'crm', 'ir_ui_view_sc_calendar0')
144             view_id  = data_obj.browse(cr, uid, data_id, context=context).res_id
145             self.pool.get('ir.ui.view_sc').copy(cr, uid, view_id, default = {
146                                         'user_id': user_id}, context=context)
147         except:
148             # Tolerate a missing shortcut. See product/product.py for similar code.
149             logging.getLogger('orm').debug('Skipped meetings shortcut for user "%s"', data.get('name','<new'))
150
151         return user_id
152
153 res_users()
154
155
156 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: