[IMP] event: code cleaning
[odoo/odoo.git] / addons / event / wizard / partner_event_registration.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 netsvc
23 import tools
24 from osv import fields, osv
25 from tools.translate import _
26 from decimal_precision import decimal_precision as dp
27
28
29 class partner_event_registration(osv.osv_memory):
30     """  event Registration """
31
32     _name = "partner.event.registration"
33     _description = __doc__
34     _order = 'event_id'
35
36     _columns = {
37         'event_id': fields.many2one('event.event', 'Event'),
38         'event_type': fields.many2one('event.type', 'Type', readonly=True),
39         'unit_price': fields.float('Registration Cost', digits_compute=dp.get_precision('Sale Price')),
40         'start_date': fields.datetime('Start date', required=True, help="Beginning Date of Event", readonly=True),
41         'end_date': fields.datetime('Closing date', required=True, help="Closing Date of Event", readonly=True),
42         'nb_register': fields.integer('Number of Registration'),
43     }
44     _defaults = {
45         'nb_register': 1,
46     }
47
48     def open_registration(self, cr, uid, ids, context=None):
49         """This Function Open Registration For Given Event id and Partner.
50
51         """
52         value = {}
53         res_obj = self.pool.get('res.partner')
54         job_obj = self.pool.get('res.partner.job')
55         reg_obj = self.pool.get('event.registration')
56         mod_obj = self.pool.get('ir.model.data')
57
58         record_ids = context and context.get('active_ids', []) or []
59         addr = res_obj.address_get(cr, uid, record_ids)
60         contact_id = False
61         email = False
62         if addr.has_key('default'):
63                 job_ids = job_obj.search(cr, uid, [('address_id', '=', addr['default'])], context=context)
64                 if job_ids:
65                     contact = job_obj.browse(cr, uid, job_ids[0], context=context)
66                     if contact:
67                         contact_id = contact.contact_id.id
68                         email = contact.email
69
70         result = mod_obj._get_id(cr, uid, 'event', 'view_registration_search')
71         res = mod_obj.read(cr, uid, result, ['res_id'])
72
73         # Select the view
74         id2 = mod_obj._get_id(cr, uid, 'event', 'view_event_registration_form')
75         id3 = mod_obj._get_id(cr, uid, 'event', 'view_event_registration_tree')
76         if id2:
77             id2 = mod_obj.browse(cr, uid, id2, context=context).res_id
78         if id3:
79             id3 = mod_obj.browse(cr, uid, id3, context=context).res_id
80
81         for current in self.browse(cr, uid, ids, context=context):
82             for partner in res_obj.browse(cr, uid, record_ids, context=context):
83                 new_case = reg_obj.create(cr, uid, {
84                         'name' : 'Registration',
85                         'event_id' : current.event_id and current.event_id.id or False,
86                         'unit_price' : current.unit_price,
87                         'partner_id' : partner.id,
88                         'partner_invoice_id' :  partner.id,
89                         'event_product': current.event_id.product_id.name,
90                         'contact_id': contact_id,
91                         'email_from': email,
92                         'nb_register': current.nb_register,
93
94                 }, context=context)
95
96         value = {
97                 'name': _('Event Registration'),
98                 'view_type': 'form',
99                 'view_mode': 'tree,form',
100                 'res_model': 'event.registration',
101                 'res_id' : new_case,
102                 'views': [(id2, 'form'), (id3, 'tree'), (False, 'calendar'), (False, 'graph')],
103                 'type': 'ir.actions.act_window',
104                 'search_view_id': res['res_id']
105         }
106         return value
107
108     def name_get(self, cr, uid, ids, context=None):
109         """Overrides orm name_get method
110         @param ids: List of partner_event_register ids
111         """
112         if not context:
113             context = {}
114
115         res = []
116         if not ids:
117             return res
118         reads = self.read(cr, uid, ids, ['event_type', 'event_id'], context=context)
119         for record in reads:
120             event_id = record['event_id'][1]
121             if record['event_type']:
122                 event_id = record['event_type'][1] + ' on ' + event_id
123             res.append((record['id'], event_id))
124         return res
125
126     def onchange_event_id(self, cr, uid, ids, event_id, context=None):
127         res = {}
128         event_obj = self.pool.get('event.event')
129         product_obj = self.pool.get('product.product')
130         partner_obj = self.pool.get('res.partner')
131         if not context:
132             context = {}
133         partner_id = context.get('active_id', False)
134         if event_id:
135             event = event_obj.browse(cr, uid, event_id, context=context)
136             pricelist_id = event.pricelist_id and event.pricelist_id.id or False
137             if partner_id:
138                 partner = partner_obj.browse(cr, uid, partner_id, context=context)
139                 pricelist_id = pricelist_id or partner.property_product_pricelist.id
140             unit_price = product_obj._product_price(cr, uid, [event.product_id.id], False, False, {'pricelist': pricelist_id})[event.product_id.id]
141
142             res['value'] = {
143                           'event_type': event.type and event.type.id or False,
144                           'start_date': event.date_begin,
145                           'end_date': event.date_end,
146                           'unit_price': unit_price,
147             }
148         return res
149
150 partner_event_registration()
151
152 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
153