[FIX]: Add type, storage_id and calendar_collection field in caldav view.
[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 = ['mailgate.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('mailgate.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
78     _defaults = {
79         'state': lambda *a: 'draft', 
80         'active': lambda *a: 1,
81         'user_id': lambda self, cr, uid, ctx: uid,
82     }
83
84     def open_meeting(self, cr, uid, ids, context=None):
85         """
86         Open Crm Meeting Form for Crm Meeting.
87         @param cr: the current row, from the database cursor,
88         @param uid: the current user’s ID for security checks,
89         @param ids: List of crm meeting’s IDs
90         @param context: A standard dictionary for contextual values
91         @return: Dictionary value which open Crm Meeting form.
92         """
93
94         if not context:
95             context = {}
96
97         data_obj = self.pool.get('ir.model.data')
98
99         value = {}
100
101         id2 = data_obj._get_id(cr, uid, 'crm', 'crm_case_form_view_meet')
102         id3 = data_obj._get_id(cr, uid, 'crm', 'crm_case_tree_view_meet')
103         id4 = data_obj._get_id(cr, uid, 'crm', 'crm_case_calendar_view_meet')
104         if id2:
105             id2 = data_obj.browse(cr, uid, id2, context=context).res_id
106         if id3:
107             id3 = data_obj.browse(cr, uid, id3, context=context).res_id
108         if id4:
109             id4 = data_obj.browse(cr, uid, id4, context=context).res_id
110         for id in ids:
111             value = {
112                     'name': _('Meeting'),
113                     'view_type': 'form',
114                     'view_mode': 'form,tree',
115                     'res_model': 'crm.meeting',
116                     'view_id': False,
117                     'views': [(id2, 'form'), (id3, 'tree'), (id4, 'calendar')],
118                     'type': 'ir.actions.act_window',
119                     'res_id': base_calendar.base_calendar_id2real_id(id),
120                     'nodestroy': True
121                     }
122
123         return value
124
125     def case_open(self, cr, uid, ids, *args):
126         """Confirms meeting
127         @param self: The object pointer
128         @param cr: the current row, from the database cursor,
129         @param uid: the current user’s ID for security checks,
130         @param ids: List of Meeting Ids
131         @param *args: Tuple Value for additional Params
132         """
133         res = super(crm_meeting, self).case_open(cr, uid, ids, args)
134         for (id, name) in self.name_get(cr, uid, ids):
135             message = _("The meeting '%s' has been confirmed.") % name
136             id=base_calendar.base_calendar_id2real_id(id)
137             self.log(cr, uid, id, message)
138         return res
139
140 crm_meeting()
141
142 class calendar_attendee(osv.osv):
143     """ Calendar Attendee """
144
145     _inherit = 'calendar.attendee'
146     _description = 'Calendar Attendee'
147
148     def _compute_data(self, cr, uid, ids, name, arg, context):
149        """
150         @param self: The object pointer
151         @param cr: the current row, from the database cursor,
152         @param uid: the current user’s ID for security checks,
153         @param ids: List of compute data’s IDs
154         @param context: A standard dictionary for contextual values
155         """
156        name = name[0]
157        result = super(calendar_attendee, self)._compute_data(cr, uid, ids, name, arg, context)
158
159        for attdata in self.browse(cr, uid, ids, context=context):
160             id = attdata.id
161             result[id] = {}
162             if name == 'categ_id':
163                 if attdata.ref and 'categ_id' in attdata.ref._columns:
164                     result[id][name] = (attdata.ref.categ_id.id, attdata.ref.categ_id.name,)
165                 else:
166                     result[id][name] = False
167        return result
168
169     _columns = {
170         'categ_id': fields.function(_compute_data, method=True, \
171                         string='Event Type', type="many2one", \
172                         relation="crm.case.categ", multi='categ_id'),
173     }
174
175 calendar_attendee()
176
177 class res_users(osv.osv):
178     _name = 'res.users'
179     _inherit = 'res.users'
180
181     def create(self, cr, uid, data, context=None):
182         if context is None:
183             context = {}
184         user_id = super(res_users, self).create(cr, uid, data, context)
185         data_obj = self.pool.get('ir.model.data')
186         try:
187             data_id = data_obj._get_id(cr, uid, 'crm', 'ir_ui_view_sc_calendar0')
188             view_id  = data_obj.browse(cr, uid, data_id, context=context).res_id
189             self.pool.get('ir.ui.view_sc').copy(cr, uid, view_id, default = {
190                                         'user_id': user_id}, context=context)
191         except ValueError:
192             # Tolerate a missing shortcut. See product/product.py for similar code.
193             logging.getLogger('orm').warning('Skipped Products shortcut for user "%s"', data.get('name','<new'))
194             
195         return user_id
196
197 res_users()
198
199
200 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: