[IMP] web: allow fields in list view to use the attrs readonly to dynamically show...
[odoo/odoo.git] / addons / event_sale / event_sale.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 openerp.osv import fields, osv
23 from openerp.tools.translate import _
24
25 class product(osv.osv):
26     _inherit = 'product.product'
27     _columns = {
28         'event_ok': fields.boolean('Event Subscription', help='Determine if a product needs to create automatically an event registration at the confirmation of a sales order line.'),
29         'event_type_id': fields.many2one('event.type', 'Type of Event', help='Select event types so when we use this product in sales order lines, it will filter events of this type only.'),
30     }
31
32     def onchange_event_ok(self, cr, uid, ids, event_ok, context=None):
33         return {'value': {'type': event_ok and 'service' or False}}
34
35 product()
36
37 class sale_order_line(osv.osv):
38     _inherit = 'sale.order.line'
39     _columns = {
40         'event_id': fields.many2one('event.event', 'Event', help="Choose an event and it will automatically create a registration for this event."),
41         #those 2 fields are used for dynamic domains and filled by onchange
42         'event_type_id': fields.related('product_id','event_type_id', type='many2one', relation="event.type", string="Event Type"),
43         'event_ok': fields.related('product_id', 'event_ok', string='event_ok', type='boolean'),
44     }
45
46     def product_id_change(self, cr, uid, ids,
47                           pricelist, 
48                           product, qty=0,
49                           uom=False,
50                           qty_uos=0,
51                           uos=False,
52                           name='',
53                           partner_id=False,
54                           lang=False,
55                           update_tax=True,
56                           date_order=False,
57                           packaging=False,
58                           fiscal_position=False,
59                           flag=False, context=None):
60         """
61         check product if event type
62         """
63         res = super(sale_order_line,self).product_id_change(cr, uid, ids, pricelist, product, qty=qty, uom=uom, qty_uos=qty_uos, uos=uos, name=name, partner_id=partner_id, lang=lang, update_tax=update_tax, date_order=date_order, packaging=packaging, fiscal_position=fiscal_position, flag=flag, context=context)
64         if product:
65             product_res = self.pool.get('product.product').browse(cr, uid, product, context=context)
66             if product_res.event_ok:
67                 res['value'].update({'event_type_id': product_res.event_type_id.id, 'event_ok':product_res.event_ok})
68         return res
69
70     def button_confirm(self, cr, uid, ids, context=None):
71         '''
72         create registration with sales order
73         '''
74         registration_obj = self.pool.get('event.registration')
75         sale_obj = self.pool.get('sale.order')
76         for order_line in self.browse(cr, uid, ids, context=context):
77             if order_line.event_id.id:
78                 dic = {
79                     'name': order_line.order_id.partner_invoice_id.name,
80                     'partner_id': order_line.order_id.partner_id.id,
81                     'nb_register': int(order_line.product_uom_qty),
82                     'email': order_line.order_id.partner_id.email,
83                     'phone': order_line.order_id.partner_id.phone,
84                     'origin': order_line.order_id.name,
85                     'event_id': order_line.event_id.id,
86                 }
87                 registration_id = registration_obj.create(cr, uid, dic, context=context)
88                 message = _("The registration %s has been created from the Sales Order %s.") % (registration_id, order_line.order_id.name)
89                 registration_obj.message_post(cr, uid, [registration_id], body=message, context=context)
90         return super(sale_order_line, self).button_confirm(cr, uid, ids, context=context)