[ADD]crm_channel: portal, Im not interested button go to next lead or to list if...
[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
36 class sale_order_line(osv.osv):
37     _inherit = 'sale.order.line'
38     _columns = {
39         'event_id': fields.many2one('event.event', 'Event', help="Choose an event and it will automatically create a registration for this event."),
40         #those 2 fields are used for dynamic domains and filled by onchange
41         'event_type_id': fields.related('product_id','event_type_id', type='many2one', relation="event.type", string="Event Type"),
42         'event_ok': fields.related('product_id', 'event_ok', string='event_ok', type='boolean'),
43     }
44
45     def product_id_change(self, cr, uid, ids,
46                           pricelist, 
47                           product, qty=0,
48                           uom=False,
49                           qty_uos=0,
50                           uos=False,
51                           name='',
52                           partner_id=False,
53                           lang=False,
54                           update_tax=True,
55                           date_order=False,
56                           packaging=False,
57                           fiscal_position=False,
58                           flag=False, context=None):
59         """
60         check product if event type
61         """
62         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)
63         if product:
64             product_res = self.pool.get('product.product').browse(cr, uid, product, context=context)
65             if product_res.event_ok:
66                 res['value'].update({'event_type_id': product_res.event_type_id.id, 'event_ok':product_res.event_ok})
67         return res
68
69     def button_confirm(self, cr, uid, ids, context=None):
70         '''
71         create registration with sales order
72         '''
73         if context is None:
74             context = {}
75         registration_obj = self.pool.get('event.registration')
76         sale_obj = self.pool.get('sale.order')
77         for order_line in self.browse(cr, uid, ids, context=context):
78             if order_line.event_id.id:
79                 dic = {
80                     'name': order_line.order_id.partner_invoice_id.name,
81                     'partner_id': order_line.order_id.partner_id.id,
82                     'nb_register': int(order_line.product_uom_qty),
83                     'email': order_line.order_id.partner_id.email,
84                     'phone': order_line.order_id.partner_id.phone,
85                     'origin': order_line.order_id.name,
86                     'event_id': order_line.event_id.id,
87                 }
88                 message = _("The registration has been created for event <i>%s</i> from the Sale Order %s. ") % (order_line.event_id.name, order_line.order_id.name)
89                 context.update({'mail_create_nolog': True})
90                 registration_id = registration_obj.create(cr, uid, dic, context=context)
91                 registration_obj.message_post(cr, uid, [registration_id], body=message, context=context)
92         return super(sale_order_line, self).button_confirm(cr, uid, ids, context=context)